forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@export_tool_button
annotation for easily creating inspector bu…
…ttons Co-authored-by: jordi <creptthrust@gmail.com> Co-authored-by: K. S. Ernest (iFire) Lee <ernest.lee@chibifire.com> Co-authored-by: Mack <86566939+Macksaur@users.noreply.github.com>
- Loading branch information
1 parent
506d6e4
commit 0827064
Showing
14 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/**************************************************************************/ | ||
/* tool_button_editor_plugin.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "tool_button_editor_plugin.h" | ||
|
||
#include "scene/gui/button.h" | ||
|
||
void EditorInspectorToolButtonPlugin::_update_action_icon(Button *p_action_button) { | ||
p_action_button->set_icon(p_action_button->get_editor_theme_icon(action_icon)); | ||
} | ||
|
||
void EditorInspectorToolButtonPlugin::_call_action(const Variant &p_object, const StringName &p_property) { | ||
Object *object = p_object.get_validated_object(); | ||
ERR_FAIL_NULL_MSG(object, vformat(R"(Failed to get property "%s" on a previously freed instance.)", p_property)); | ||
|
||
const Variant value = object->get(p_property); | ||
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()))); | ||
|
||
const Callable callable = value; | ||
ERR_FAIL_COND_MSG(!callable.is_valid(), vformat(R"(Tool button action "%s" is an invalid callable.)", callable)); | ||
|
||
Variant ret; | ||
Callable::CallError ce; | ||
callable.callp(nullptr, 0, ret, ce); | ||
ERR_FAIL_COND_MSG(ce.error != Callable::CallError::CALL_OK, vformat(R"(Error calling tool button action "%s": %s)", callable, Variant::get_call_error_text(callable.get_method(), nullptr, 0, ce))); | ||
} | ||
|
||
bool EditorInspectorToolButtonPlugin::can_handle(Object *p_object) { | ||
return true; | ||
} | ||
|
||
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) { | ||
if (p_type != Variant::CALLABLE || p_hint != PROPERTY_HINT_TOOL_BUTTON || !p_usage.has_flag(PROPERTY_USAGE_EDITOR)) { | ||
return false; | ||
} | ||
|
||
const PackedStringArray splits = p_hint_text.rsplit(",", true, 1); | ||
const String &hint_text = splits[0]; // Safe since `splits` cannot be empty. | ||
const String &hint_icon = splits.size() > 1 ? splits[1] : "Callable"; | ||
|
||
action_icon = hint_icon; | ||
|
||
Button *action_button = EditorInspector::create_inspector_action_button(hint_text); | ||
action_button->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED); | ||
action_button->set_disabled(p_usage & PROPERTY_USAGE_READ_ONLY); | ||
action_button->connect(SceneStringName(theme_changed), callable_mp(this, &EditorInspectorToolButtonPlugin::_update_action_icon).bind(action_button)); | ||
action_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorToolButtonPlugin::_call_action).bind(p_object, p_path)); | ||
|
||
add_custom_control(action_button); | ||
return true; | ||
} | ||
|
||
ToolButtonEditorPlugin::ToolButtonEditorPlugin() { | ||
Ref<EditorInspectorToolButtonPlugin> plugin; | ||
plugin.instantiate(); | ||
add_inspector_plugin(plugin); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/**************************************************************************/ | ||
/* tool_button_editor_plugin.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef TOOL_BUTTON_EDITOR_PLUGIN_H | ||
#define TOOL_BUTTON_EDITOR_PLUGIN_H | ||
|
||
#include "editor/editor_inspector.h" | ||
#include "editor/plugins/editor_plugin.h" | ||
|
||
class EditorInspectorToolButtonPlugin : public EditorInspectorPlugin { | ||
GDCLASS(EditorInspectorToolButtonPlugin, EditorInspectorPlugin); | ||
|
||
StringName action_icon; | ||
|
||
void _update_action_icon(Button *p_action_button); | ||
void _call_action(const Variant &p_object, const StringName &p_property); | ||
|
||
public: | ||
virtual bool can_handle(Object *p_object) override; | ||
virtual bool 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 = false) override; | ||
}; | ||
|
||
class ToolButtonEditorPlugin : public EditorPlugin { | ||
GDCLASS(ToolButtonEditorPlugin, EditorPlugin); | ||
|
||
public: | ||
virtual String get_name() const override { return "ToolButtonEditorPlugin"; } | ||
|
||
ToolButtonEditorPlugin(); | ||
}; | ||
|
||
#endif // TOOL_BUTTON_EDITOR_PLUGIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
modules/gdscript/tests/scripts/parser/errors/export_tool_button_requires_tool_mode.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@export_tool_button("Click me!") var action |
Oops, something went wrong.