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