particles_2d_editor_plugin.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*************************************************************************/
  2. /* particles_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "particles_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "io/image_loader.h"
  33. #include "scene/gui/separator.h"
  34. void Particles2DEditorPlugin::edit(Object *p_object) {
  35. if (p_object) {
  36. particles = p_object->cast_to<Particles2D>();
  37. } else {
  38. particles = NULL;
  39. }
  40. }
  41. bool Particles2DEditorPlugin::handles(Object *p_object) const {
  42. return p_object->is_type("Particles2D");
  43. }
  44. void Particles2DEditorPlugin::make_visible(bool p_visible) {
  45. if (p_visible) {
  46. toolbar->show();
  47. } else {
  48. toolbar->hide();
  49. }
  50. }
  51. void Particles2DEditorPlugin::_file_selected(const String &p_file) {
  52. print_line("file: " + p_file);
  53. int epc = epoints->get_val();
  54. Image img;
  55. Error err = ImageLoader::load_image(p_file, &img);
  56. ERR_EXPLAIN(TTR("Error loading image:") + " " + p_file);
  57. ERR_FAIL_COND(err != OK);
  58. img.convert(Image::FORMAT_GRAYSCALE_ALPHA);
  59. ERR_FAIL_COND(img.get_format() != Image::FORMAT_GRAYSCALE_ALPHA);
  60. Size2i s = Size2(img.get_width(), img.get_height());
  61. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  62. DVector<uint8_t> data = img.get_data();
  63. DVector<uint8_t>::Read r = data.read();
  64. Vector<Point2i> valid_positions;
  65. valid_positions.resize(s.width * s.height);
  66. int vpc = 0;
  67. for (int i = 0; i < s.width * s.height; i++) {
  68. uint8_t a = r[i * 2 + 1];
  69. if (a > 128) {
  70. valid_positions[vpc++] = Point2i(i % s.width, i / s.width);
  71. }
  72. }
  73. valid_positions.resize(vpc);
  74. ERR_EXPLAIN(TTR("No pixels with transparency > 128 in image.."));
  75. ERR_FAIL_COND(valid_positions.size() == 0);
  76. DVector<Point2> epoints;
  77. epoints.resize(epc);
  78. DVector<Point2>::Write w = epoints.write();
  79. Size2 extents = Size2(img.get_width() * 0.5, img.get_height() * 0.5);
  80. for (int i = 0; i < epc; i++) {
  81. Point2 p = valid_positions[Math::rand() % vpc];
  82. p -= s / 2;
  83. w[i] = p / extents;
  84. }
  85. w = DVector<Point2>::Write();
  86. undo_redo->create_action(TTR("Set Emission Mask"));
  87. undo_redo->add_do_method(particles, "set_emission_points", epoints);
  88. undo_redo->add_do_method(particles, "set_emission_half_extents", extents);
  89. undo_redo->add_undo_method(particles, "set_emission_points", particles->get_emission_points());
  90. undo_redo->add_undo_method(particles, "set_emission_half_extents", particles->get_emission_half_extents());
  91. undo_redo->commit_action();
  92. }
  93. void Particles2DEditorPlugin::_menu_callback(int p_idx) {
  94. switch (p_idx) {
  95. case MENU_LOAD_EMISSION_MASK: {
  96. file->popup_centered_ratio();
  97. } break;
  98. case MENU_CLEAR_EMISSION_MASK: {
  99. undo_redo->create_action(TTR("Clear Emission Mask"));
  100. undo_redo->add_do_method(particles, "set_emission_points", DVector<Vector2>());
  101. undo_redo->add_undo_method(particles, "set_emission_points", particles->get_emission_points());
  102. undo_redo->commit_action();
  103. } break;
  104. }
  105. }
  106. void Particles2DEditorPlugin::_notification(int p_what) {
  107. if (p_what == NOTIFICATION_ENTER_TREE) {
  108. menu->get_popup()->connect("item_pressed", this, "_menu_callback");
  109. menu->set_icon(menu->get_popup()->get_icon("Particles2D", "EditorIcons"));
  110. file->connect("file_selected", this, "_file_selected");
  111. }
  112. }
  113. void Particles2DEditorPlugin::_bind_methods() {
  114. ObjectTypeDB::bind_method(_MD("_menu_callback"), &Particles2DEditorPlugin::_menu_callback);
  115. ObjectTypeDB::bind_method(_MD("_file_selected"), &Particles2DEditorPlugin::_file_selected);
  116. }
  117. Particles2DEditorPlugin::Particles2DEditorPlugin(EditorNode *p_node) {
  118. particles = NULL;
  119. editor = p_node;
  120. undo_redo = editor->get_undo_redo();
  121. toolbar = memnew(HBoxContainer);
  122. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  123. toolbar->hide();
  124. toolbar->add_child(memnew(VSeparator));
  125. menu = memnew(MenuButton);
  126. menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  127. menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
  128. menu->set_text("Particles");
  129. toolbar->add_child(menu);
  130. file = memnew(EditorFileDialog);
  131. List<String> ext;
  132. ImageLoader::get_recognized_extensions(&ext);
  133. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  134. file->add_filter("*." + E->get() + "; " + E->get().to_upper());
  135. }
  136. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  137. toolbar->add_child(file);
  138. epoints = memnew(SpinBox);
  139. epoints->set_min(1);
  140. epoints->set_max(8192);
  141. epoints->set_step(1);
  142. epoints->set_val(512);
  143. file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints);
  144. }
  145. Particles2DEditorPlugin::~Particles2DEditorPlugin() {
  146. }