tool_button_editor_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* tool_button_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 "tool_button_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/inspector/multi_node_edit.h"
  33. void EditorInspectorToolButtonPlugin::_call_action(const Variant &p_object, const StringName &p_property) {
  34. Object *object = p_object.get_validated_object();
  35. ERR_FAIL_NULL_MSG(object, vformat(R"(Failed to get property "%s" on a previously freed instance.)", p_property));
  36. const Variant value = object->get(p_property);
  37. ERR_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())));
  38. const Callable callable = value;
  39. ERR_FAIL_COND_MSG(!callable.is_valid(), vformat(R"(Tool button action "%s" is an invalid callable.)", callable));
  40. Ref<MultiNodeEdit> multinode = p_object;
  41. if (multinode.is_valid()) {
  42. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  43. if (edited_scene) {
  44. const StringName method = callable.get_method();
  45. for (int i = 0; i < multinode->get_node_count(); i++) {
  46. Callable retarget(edited_scene->get_node(multinode->get_node(i)), method);
  47. _invoke_callable(retarget);
  48. }
  49. }
  50. } else {
  51. _invoke_callable(callable);
  52. }
  53. }
  54. void EditorInspectorToolButtonPlugin::_invoke_callable(const Callable &p_callable) {
  55. Variant ret;
  56. Callable::CallError ce;
  57. p_callable.callp(nullptr, 0, ret, ce);
  58. ERR_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)));
  59. }
  60. bool EditorInspectorToolButtonPlugin::can_handle(Object *p_object) {
  61. return true;
  62. }
  63. bool 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) {
  64. if (p_type != Variant::CALLABLE || p_hint != PROPERTY_HINT_TOOL_BUTTON || !p_usage.has_flag(PROPERTY_USAGE_EDITOR)) {
  65. return false;
  66. }
  67. const PackedStringArray splits = p_hint_text.rsplit(",", true, 1);
  68. const String &hint_text = splits[0]; // Safe since `splits` cannot be empty.
  69. const String &hint_icon = splits.size() > 1 ? splits[1] : "Callable";
  70. EditorInspectorActionButton *action_button = memnew(EditorInspectorActionButton(hint_text, hint_icon));
  71. action_button->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  72. bool is_multinode = Object::cast_to<MultiNodeEdit>(p_object);
  73. if (p_usage & PROPERTY_USAGE_READ_ONLY) {
  74. action_button->set_disabled(true);
  75. } else if (is_multinode) {
  76. const Callable callback = p_object->get(p_path);
  77. if (callback.is_custom()) {
  78. action_button->set_disabled(true);
  79. action_button->set_tooltip_text(TTR("The assigned tool button callback can't be used with multiple nodes."));
  80. }
  81. }
  82. action_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorToolButtonPlugin::_call_action).bind(p_object, p_path));
  83. add_custom_control(action_button);
  84. return true;
  85. }
  86. ToolButtonEditorPlugin::ToolButtonEditorPlugin() {
  87. Ref<EditorInspectorToolButtonPlugin> plugin;
  88. plugin.instantiate();
  89. add_inspector_plugin(plugin);
  90. }