2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* particles_2d_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-11-06 00:20:42 +00:00
|
|
|
#include "particles_2d_editor_plugin.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
#include "canvas_item_editor_plugin.h"
|
|
|
|
#include "io/image_loader.h"
|
2016-03-14 19:05:20 +00:00
|
|
|
#include "scene/gui/separator.h"
|
2014-11-06 00:20:42 +00:00
|
|
|
|
|
|
|
void Particles2DEditorPlugin::edit(Object *p_object) {
|
|
|
|
|
|
|
|
if (p_object) {
|
2017-03-05 15:44:50 +00:00
|
|
|
particles = p_object->cast_to<Particles2D>();
|
2014-11-06 00:20:42 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
particles = NULL;
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Particles2DEditorPlugin::handles(Object *p_object) const {
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
return p_object->is_class("Particles2D");
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Particles2DEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
|
|
|
|
if (p_visible) {
|
|
|
|
|
2016-03-14 19:05:20 +00:00
|
|
|
toolbar->show();
|
2014-11-06 00:20:42 +00:00
|
|
|
} else {
|
|
|
|
|
2016-03-14 19:05:20 +00:00
|
|
|
toolbar->hide();
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Particles2DEditorPlugin::_file_selected(const String &p_file) {
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("file: " + p_file);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int epc = epoints->get_value();
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img;
|
|
|
|
img.instance();
|
|
|
|
Error err = ImageLoader::load_image(p_file, img);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN(TTR("Error loading image:") + " " + p_file);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
img->convert(Image::FORMAT_LA8);
|
|
|
|
ERR_FAIL_COND(img->get_format() != Image::FORMAT_LA8);
|
|
|
|
Size2i s = Size2(img->get_width(), img->get_height());
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(s.width == 0 || s.height == 0);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
PoolVector<uint8_t> data = img->get_data();
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<uint8_t>::Read r = data.read();
|
2014-11-06 00:20:42 +00:00
|
|
|
|
|
|
|
Vector<Point2i> valid_positions;
|
2017-03-05 15:44:50 +00:00
|
|
|
valid_positions.resize(s.width * s.height);
|
|
|
|
int vpc = 0;
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < s.width * s.height; i++) {
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t a = r[i * 2 + 1];
|
|
|
|
if (a > 128) {
|
|
|
|
valid_positions[vpc++] = Point2i(i % s.width, i / s.width);
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valid_positions.resize(vpc);
|
|
|
|
|
2016-05-04 01:25:37 +00:00
|
|
|
ERR_EXPLAIN(TTR("No pixels with transparency > 128 in image.."));
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(valid_positions.size() == 0);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Point2> epoints;
|
2014-11-06 00:20:42 +00:00
|
|
|
epoints.resize(epc);
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Point2>::Write w = epoints.write();
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Size2 extents = Size2(img->get_width() * 0.5, img->get_height() * 0.5);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < epc; i++) {
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2 p = valid_positions[Math::rand() % vpc];
|
|
|
|
p -= s / 2;
|
|
|
|
w[i] = p / extents;
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
w = PoolVector<Point2>::Write();
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2016-05-04 01:25:37 +00:00
|
|
|
undo_redo->create_action(TTR("Set Emission Mask"));
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(particles, "set_emission_points", epoints);
|
|
|
|
undo_redo->add_do_method(particles, "set_emission_half_extents", extents);
|
|
|
|
undo_redo->add_undo_method(particles, "set_emission_points", particles->get_emission_points());
|
|
|
|
undo_redo->add_undo_method(particles, "set_emission_half_extents", particles->get_emission_half_extents());
|
2014-11-06 00:20:42 +00:00
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particles2DEditorPlugin::_menu_callback(int p_idx) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_idx) {
|
2014-11-06 00:20:42 +00:00
|
|
|
case MENU_LOAD_EMISSION_MASK: {
|
|
|
|
|
|
|
|
file->popup_centered_ratio();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case MENU_CLEAR_EMISSION_MASK: {
|
|
|
|
|
2016-05-04 01:25:37 +00:00
|
|
|
undo_redo->create_action(TTR("Clear Emission Mask"));
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(particles, "set_emission_points", PoolVector<Vector2>());
|
|
|
|
undo_redo->add_undo_method(particles, "set_emission_points", particles->get_emission_points());
|
2014-11-06 00:20:42 +00:00
|
|
|
undo_redo->commit_action();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particles2DEditorPlugin::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
menu->get_popup()->connect("id_pressed", this, "_menu_callback");
|
|
|
|
menu->set_icon(menu->get_popup()->get_icon("Particles2D", "EditorIcons"));
|
|
|
|
file->connect("file_selected", this, "_file_selected");
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particles2DEditorPlugin::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_menu_callback"), &Particles2DEditorPlugin::_menu_callback);
|
|
|
|
ClassDB::bind_method(D_METHOD("_file_selected"), &Particles2DEditorPlugin::_file_selected);
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Particles2DEditorPlugin::Particles2DEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
particles = NULL;
|
|
|
|
editor = p_node;
|
|
|
|
undo_redo = editor->get_undo_redo();
|
2016-03-14 19:05:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
toolbar = memnew(HBoxContainer);
|
2016-03-14 19:05:20 +00:00
|
|
|
add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
|
|
|
|
toolbar->hide();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
toolbar->add_child(memnew(VSeparator));
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
menu = memnew(MenuButton);
|
|
|
|
menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
|
|
|
|
menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
|
2014-11-06 00:20:42 +00:00
|
|
|
menu->set_text("Particles");
|
2016-03-14 19:05:20 +00:00
|
|
|
toolbar->add_child(menu);
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
file = memnew(EditorFileDialog);
|
2014-11-06 00:20:42 +00:00
|
|
|
List<String> ext;
|
|
|
|
ImageLoader::get_recognized_extensions(&ext);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *E = ext.front(); E; E = E->next()) {
|
|
|
|
file->add_filter("*." + E->get() + "; " + E->get().to_upper());
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
2015-06-06 12:44:38 +00:00
|
|
|
file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
|
2016-03-14 19:05:20 +00:00
|
|
|
toolbar->add_child(file);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
epoints = memnew(SpinBox);
|
2014-11-06 00:20:42 +00:00
|
|
|
epoints->set_min(1);
|
|
|
|
epoints->set_max(8192);
|
|
|
|
epoints->set_step(1);
|
2017-01-04 04:16:14 +00:00
|
|
|
epoints->set_value(512);
|
2017-03-05 15:44:50 +00:00
|
|
|
file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints);
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Particles2DEditorPlugin::~Particles2DEditorPlugin() {
|
2014-11-06 00:20:42 +00:00
|
|
|
}
|