Path: blob/master/editor/inspector/tool_button_editor_plugin.cpp
20829 views
/**************************************************************************/1/* tool_button_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 "tool_button_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/inspector/multi_node_edit.h"3435void EditorInspectorToolButtonPlugin::_call_action(const Variant &p_object, const StringName &p_property) {36Object *object = p_object.get_validated_object();37ERR_FAIL_NULL_MSG(object, vformat(R"(Failed to get property "%s" on a previously freed instance.)", p_property));3839const Variant value = object->get(p_property);40ERR_FAIL_COND_MSG(value.get_type() != Variant::CALLABLE, vformat(R"(The value of property "%s" is %s, but Callable was expected.)", p_property, Variant::get_type_name(value.get_type())));4142const Callable callable = value;43ERR_FAIL_COND_MSG(!callable.is_valid(), vformat(R"(Tool button action "%s" is an invalid callable.)", callable));4445Ref<MultiNodeEdit> multinode = p_object;46if (multinode.is_valid()) {47Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();48if (edited_scene) {49const StringName method = callable.get_method();50for (int i = 0; i < multinode->get_node_count(); i++) {51Callable retarget(edited_scene->get_node(multinode->get_node(i)), method);52_invoke_callable(retarget);53}54}55} else {56_invoke_callable(callable);57}58}5960void EditorInspectorToolButtonPlugin::_invoke_callable(const Callable &p_callable) {61Variant ret;62Callable::CallError ce;63p_callable.callp(nullptr, 0, ret, ce);64ERR_FAIL_COND_MSG(ce.error != Callable::CallError::CALL_OK, vformat(R"(Error calling tool button action "%s": %s)", p_callable, Variant::get_call_error_text(p_callable.get_method(), nullptr, 0, ce)));65}6667bool EditorInspectorToolButtonPlugin::can_handle(Object *p_object) {68return true;69}7071bool EditorInspectorToolButtonPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {72if (p_type != Variant::CALLABLE || p_hint != PROPERTY_HINT_TOOL_BUTTON || !p_usage.has_flag(PROPERTY_USAGE_EDITOR)) {73return false;74}7576const PackedStringArray splits = p_hint_text.rsplit(",", true, 1);77const String &hint_text = splits[0]; // Safe since `splits` cannot be empty.78const String &hint_icon = splits.size() > 1 ? splits[1] : "Callable";7980EditorInspectorActionButton *action_button = memnew(EditorInspectorActionButton(hint_text, hint_icon));81action_button->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);82bool is_multinode = Object::cast_to<MultiNodeEdit>(p_object);83if (p_usage & PROPERTY_USAGE_READ_ONLY) {84action_button->set_disabled(true);85} else if (is_multinode) {86const Callable callback = p_object->get(p_path);87if (callback.is_custom()) {88action_button->set_disabled(true);89action_button->set_tooltip_text(TTR("The assigned tool button callback can't be used with multiple nodes."));90}91}92action_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorToolButtonPlugin::_call_action).bind(p_object, p_path));9394add_custom_control(action_button);95return true;96}9798ToolButtonEditorPlugin::ToolButtonEditorPlugin() {99Ref<EditorInspectorToolButtonPlugin> plugin;100plugin.instantiate();101add_inspector_plugin(plugin);102}103104105