Path: blob/master/editor/scene/2d/skeleton_2d_editor_plugin.cpp
9904 views
/**************************************************************************/1/* skeleton_2d_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 "skeleton_2d_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/canvas_item_editor_plugin.h"36#include "scene/gui/dialogs.h"37#include "scene/gui/menu_button.h"3839void Skeleton2DEditor::_node_removed(Node *p_node) {40if (p_node == node) {41node = nullptr;42options->hide();43}44}4546void Skeleton2DEditor::edit(Skeleton2D *p_sprite) {47node = p_sprite;48}4950void Skeleton2DEditor::_menu_option(int p_option) {51if (!node) {52return;53}5455switch (p_option) {56case MENU_OPTION_SET_REST: {57if (node->get_bone_count() == 0) {58err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));59err_dialog->popup_centered();60return;61}62EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();63ur->create_action(TTR("Set Rest Pose to Bones"));64for (int i = 0; i < node->get_bone_count(); i++) {65Bone2D *bone = node->get_bone(i);66ur->add_do_method(bone, "set_transform", bone->get_rest());67ur->add_undo_method(bone, "set_transform", bone->get_transform());68}69ur->commit_action();7071} break;72case MENU_OPTION_MAKE_REST: {73if (node->get_bone_count() == 0) {74err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));75err_dialog->popup_centered();76return;77}78EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();79ur->create_action(TTR("Create Rest Pose from Bones"));80for (int i = 0; i < node->get_bone_count(); i++) {81Bone2D *bone = node->get_bone(i);82ur->add_do_method(bone, "set_rest", bone->get_transform());83ur->add_undo_method(bone, "set_rest", bone->get_rest());84}85ur->commit_action();8687} break;88}89}9091Skeleton2DEditor::Skeleton2DEditor() {92options = memnew(MenuButton);9394CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);9596options->set_text(TTR("Skeleton2D"));97options->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Skeleton2D"), EditorStringName(EditorIcons)));98options->set_flat(false);99options->set_theme_type_variation("FlatMenuButton");100101options->get_popup()->add_item(TTR("Reset to Rest Pose"), MENU_OPTION_SET_REST);102options->get_popup()->add_separator();103// Use the "Overwrite" word to highlight that this is a destructive operation.104options->get_popup()->add_item(TTR("Overwrite Rest Pose"), MENU_OPTION_MAKE_REST);105options->set_switch_on_hover(true);106107options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Skeleton2DEditor::_menu_option));108109err_dialog = memnew(AcceptDialog);110add_child(err_dialog);111}112113void Skeleton2DEditorPlugin::edit(Object *p_object) {114sprite_editor->edit(Object::cast_to<Skeleton2D>(p_object));115}116117bool Skeleton2DEditorPlugin::handles(Object *p_object) const {118return p_object->is_class("Skeleton2D");119}120121void Skeleton2DEditorPlugin::make_visible(bool p_visible) {122if (p_visible) {123sprite_editor->options->show();124} else {125sprite_editor->options->hide();126sprite_editor->edit(nullptr);127}128}129130Skeleton2DEditorPlugin::Skeleton2DEditorPlugin() {131sprite_editor = memnew(Skeleton2DEditor);132EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor);133make_visible(false);134135//sprite_editor->options->hide();136}137138139