particles_editor_plugin.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************/
  2. /* particles_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_editor_plugin.h"
  31. #include "editor/docks/scene_tree_dock.h"
  32. #include "editor/editor_undo_redo_manager.h"
  33. #include "editor/settings/editor_settings.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/menu_button.h"
  36. #include "scene/gui/spin_box.h"
  37. void ParticlesEditorPlugin::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_ENTER_TREE: {
  40. if (handled_type.ends_with("2D")) {
  41. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  42. } else if (handled_type.ends_with("3D")) {
  43. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, toolbar);
  44. } else {
  45. DEV_ASSERT(false);
  46. }
  47. menu->set_button_icon(menu->get_editor_theme_icon(handled_type));
  48. menu->set_text(handled_type);
  49. PopupMenu *popup = menu->get_popup();
  50. popup->add_shortcut(ED_SHORTCUT("particles/restart_emission", TTRC("Restart Emission"), KeyModifierMask::CTRL | Key::R), MENU_RESTART);
  51. _add_menu_options(popup);
  52. popup->add_item(conversion_option_name, MENU_OPTION_CONVERT);
  53. } break;
  54. }
  55. }
  56. bool ParticlesEditorPlugin::need_show_lifetime_dialog(SpinBox *p_seconds) {
  57. // Add one second to the default generation lifetime, since the progress is updated every second.
  58. p_seconds->set_value(MAX(1.0, std::trunc(edited_node->get("lifetime").operator double()) + 1.0));
  59. if (p_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  60. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  61. return true;
  62. } else {
  63. // Generate the visibility rect/AABB immediately.
  64. return false;
  65. }
  66. }
  67. void ParticlesEditorPlugin::_menu_callback(int p_idx) {
  68. switch (p_idx) {
  69. case MENU_OPTION_CONVERT: {
  70. Node *converted_node = _convert_particles();
  71. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  72. ur->create_action(conversion_option_name, UndoRedo::MERGE_DISABLE, edited_node);
  73. SceneTreeDock::get_singleton()->replace_node(edited_node, converted_node);
  74. ur->commit_action(false);
  75. } break;
  76. case MENU_RESTART: {
  77. edited_node->call("restart");
  78. }
  79. }
  80. }
  81. void ParticlesEditorPlugin::edit(Object *p_object) {
  82. edited_node = Object::cast_to<Node>(p_object);
  83. }
  84. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  85. return p_object->is_class(handled_type);
  86. }
  87. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  88. toolbar->set_visible(p_visible);
  89. }
  90. ParticlesEditorPlugin::ParticlesEditorPlugin() {
  91. toolbar = memnew(HBoxContainer);
  92. toolbar->hide();
  93. menu = memnew(MenuButton);
  94. menu->set_switch_on_hover(true);
  95. menu->set_flat(false);
  96. menu->set_theme_type_variation("FlatMenuButton");
  97. toolbar->add_child(menu);
  98. menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ParticlesEditorPlugin::_menu_callback));
  99. }