Path: blob/master/editor/scene/particles_editor_plugin.cpp
9902 views
/**************************************************************************/1/* particles_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "particles_editor_plugin.h"3132#include "editor/docks/scene_tree_dock.h"33#include "editor/editor_undo_redo_manager.h"34#include "editor/settings/editor_settings.h"35#include "scene/gui/box_container.h"36#include "scene/gui/menu_button.h"37#include "scene/gui/spin_box.h"3839void ParticlesEditorPlugin::_notification(int p_what) {40switch (p_what) {41case NOTIFICATION_ENTER_TREE: {42if (handled_type.ends_with("2D")) {43add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);44} else if (handled_type.ends_with("3D")) {45add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, toolbar);46} else {47DEV_ASSERT(false);48}4950menu->set_button_icon(menu->get_editor_theme_icon(handled_type));51menu->set_text(handled_type);5253PopupMenu *popup = menu->get_popup();54popup->add_shortcut(ED_SHORTCUT("particles/restart_emission", TTRC("Restart Emission"), KeyModifierMask::CTRL | Key::R), MENU_RESTART);55_add_menu_options(popup);56popup->add_item(conversion_option_name, MENU_OPTION_CONVERT);57} break;58}59}6061bool ParticlesEditorPlugin::need_show_lifetime_dialog(SpinBox *p_seconds) {62// Add one second to the default generation lifetime, since the progress is updated every second.63p_seconds->set_value(MAX(1.0, std::trunc(edited_node->get("lifetime").operator double()) + 1.0));6465if (p_seconds->get_value() >= 11.0 + CMP_EPSILON) {66// Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.67return true;68} else {69// Generate the visibility rect/AABB immediately.70return false;71}72}7374void ParticlesEditorPlugin::_menu_callback(int p_idx) {75switch (p_idx) {76case MENU_OPTION_CONVERT: {77Node *converted_node = _convert_particles();7879EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();80ur->create_action(conversion_option_name, UndoRedo::MERGE_DISABLE, edited_node);81SceneTreeDock::get_singleton()->replace_node(edited_node, converted_node);82ur->commit_action(false);83} break;8485case MENU_RESTART: {86edited_node->call("restart");87}88}89}9091void ParticlesEditorPlugin::edit(Object *p_object) {92edited_node = Object::cast_to<Node>(p_object);93}9495bool ParticlesEditorPlugin::handles(Object *p_object) const {96return p_object->is_class(handled_type);97}9899void ParticlesEditorPlugin::make_visible(bool p_visible) {100toolbar->set_visible(p_visible);101}102103ParticlesEditorPlugin::ParticlesEditorPlugin() {104toolbar = memnew(HBoxContainer);105toolbar->hide();106107menu = memnew(MenuButton);108menu->set_switch_on_hover(true);109menu->set_flat(false);110menu->set_theme_type_variation("FlatMenuButton");111toolbar->add_child(menu);112menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ParticlesEditorPlugin::_menu_callback));113}114115116