Merge pull request #96290 from Macksaur/export-action-callable
Add `@export_tool_button` annotation for easily creating inspector buttons.
This commit is contained in:
commit
a11f970f91
|
@ -677,6 +677,7 @@ void register_global_constants() {
|
||||||
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE);
|
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE);
|
||||||
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_HIDE_QUATERNION_EDIT);
|
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_HIDE_QUATERNION_EDIT);
|
||||||
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PASSWORD);
|
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PASSWORD);
|
||||||
|
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_TOOL_BUTTON);
|
||||||
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX);
|
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX);
|
||||||
|
|
||||||
BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NONE);
|
BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NONE);
|
||||||
|
|
|
@ -87,6 +87,7 @@ enum PropertyHint {
|
||||||
PROPERTY_HINT_PASSWORD,
|
PROPERTY_HINT_PASSWORD,
|
||||||
PROPERTY_HINT_LAYERS_AVOIDANCE,
|
PROPERTY_HINT_LAYERS_AVOIDANCE,
|
||||||
PROPERTY_HINT_DICTIONARY_TYPE,
|
PROPERTY_HINT_DICTIONARY_TYPE,
|
||||||
|
PROPERTY_HINT_TOOL_BUTTON,
|
||||||
PROPERTY_HINT_MAX,
|
PROPERTY_HINT_MAX,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2933,7 +2933,15 @@
|
||||||
<constant name="PROPERTY_HINT_PASSWORD" value="36" enum="PropertyHint">
|
<constant name="PROPERTY_HINT_PASSWORD" value="36" enum="PropertyHint">
|
||||||
Hints that a string property is a password, and every character is replaced with the secret character.
|
Hints that a string property is a password, and every character is replaced with the secret character.
|
||||||
</constant>
|
</constant>
|
||||||
<constant name="PROPERTY_HINT_MAX" value="39" enum="PropertyHint">
|
<constant name="PROPERTY_HINT_TOOL_BUTTON" value="39" enum="PropertyHint">
|
||||||
|
Hints that a [Callable] property should be displayed as a clickable button. When the button is pressed, the callable is called. The hint string specifies the button text and optionally an icon from the [code]"EditorIcons"[/code] theme type.
|
||||||
|
[codeblock lang=text]
|
||||||
|
"Click me!" - A button with the text "Click me!" and the default "Callable" icon.
|
||||||
|
"Click me!,ColorRect" - A button with the text "Click me!" and the "ColorRect" icon.
|
||||||
|
[/codeblock]
|
||||||
|
[b]Note:[/b] A [Callable] cannot be properly serialized and stored in a file, so it is recommended to use [constant PROPERTY_USAGE_EDITOR] instead of [constant PROPERTY_USAGE_DEFAULT].
|
||||||
|
</constant>
|
||||||
|
<constant name="PROPERTY_HINT_MAX" value="40" enum="PropertyHint">
|
||||||
Represents the size of the [enum PropertyHint] enum.
|
Represents the size of the [enum PropertyHint] enum.
|
||||||
</constant>
|
</constant>
|
||||||
<constant name="PROPERTY_USAGE_NONE" value="0" enum="PropertyUsageFlags" is_bitfield="true">
|
<constant name="PROPERTY_USAGE_NONE" value="0" enum="PropertyUsageFlags" is_bitfield="true">
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
/**************************************************************************/
|
||||||
|
/* 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, const String &p_action_icon) {
|
||||||
|
p_action_button->set_icon(p_action_button->get_editor_theme_icon(p_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";
|
||||||
|
|
||||||
|
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, hint_icon));
|
||||||
|
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);
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
/**************************************************************************/
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
void _update_action_icon(Button *p_action_button, const String &p_action_icon);
|
||||||
|
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
|
|
@ -127,6 +127,7 @@
|
||||||
#include "editor/plugins/texture_region_editor_plugin.h"
|
#include "editor/plugins/texture_region_editor_plugin.h"
|
||||||
#include "editor/plugins/theme_editor_plugin.h"
|
#include "editor/plugins/theme_editor_plugin.h"
|
||||||
#include "editor/plugins/tiles/tiles_editor_plugin.h"
|
#include "editor/plugins/tiles/tiles_editor_plugin.h"
|
||||||
|
#include "editor/plugins/tool_button_editor_plugin.h"
|
||||||
#include "editor/plugins/version_control_editor_plugin.h"
|
#include "editor/plugins/version_control_editor_plugin.h"
|
||||||
#include "editor/plugins/visual_shader_editor_plugin.h"
|
#include "editor/plugins/visual_shader_editor_plugin.h"
|
||||||
#include "editor/plugins/voxel_gi_editor_plugin.h"
|
#include "editor/plugins/voxel_gi_editor_plugin.h"
|
||||||
|
@ -247,6 +248,7 @@ void register_editor_types() {
|
||||||
EditorPlugins::add_by_type<TextureLayeredEditorPlugin>();
|
EditorPlugins::add_by_type<TextureLayeredEditorPlugin>();
|
||||||
EditorPlugins::add_by_type<TextureRegionEditorPlugin>();
|
EditorPlugins::add_by_type<TextureRegionEditorPlugin>();
|
||||||
EditorPlugins::add_by_type<ThemeEditorPlugin>();
|
EditorPlugins::add_by_type<ThemeEditorPlugin>();
|
||||||
|
EditorPlugins::add_by_type<ToolButtonEditorPlugin>();
|
||||||
EditorPlugins::add_by_type<VoxelGIEditorPlugin>();
|
EditorPlugins::add_by_type<VoxelGIEditorPlugin>();
|
||||||
|
|
||||||
// 2D
|
// 2D
|
||||||
|
|
|
@ -669,6 +669,41 @@
|
||||||
[b]Note:[/b] Subgroups cannot be nested, they only provide one extra level of depth. Just like the next group ends the previous group, so do the subsequent subgroups.
|
[b]Note:[/b] Subgroups cannot be nested, they only provide one extra level of depth. Just like the next group ends the previous group, so do the subsequent subgroups.
|
||||||
</description>
|
</description>
|
||||||
</annotation>
|
</annotation>
|
||||||
|
<annotation name="@export_tool_button">
|
||||||
|
<return type="void" />
|
||||||
|
<param index="0" name="text" type="String" />
|
||||||
|
<param index="1" name="icon" type="String" default="""" />
|
||||||
|
<description>
|
||||||
|
Export a [Callable] property as a clickable button with the label [param text]. When the button is pressed, the callable is called.
|
||||||
|
If [param icon] is specified, it is used to fetch an icon for the button via [method Control.get_theme_icon], from the [code]"EditorIcons"[/code] theme type. If [param icon] is omitted, the default [code]"Callable"[/code] icon is used instead.
|
||||||
|
Consider using the [EditorUndoRedoManager] to allow the action to be reverted safely.
|
||||||
|
See also [constant PROPERTY_HINT_TOOL_BUTTON].
|
||||||
|
[codeblock]
|
||||||
|
@tool
|
||||||
|
extends Sprite2D
|
||||||
|
|
||||||
|
@export_tool_button("Hello") var hello_action = hello
|
||||||
|
@export_tool_button("Randomize the color!", "ColorRect")
|
||||||
|
var randomize_color_action = randomize_color
|
||||||
|
|
||||||
|
func hello():
|
||||||
|
print("Hello world!")
|
||||||
|
|
||||||
|
func randomize_color():
|
||||||
|
var undo_redo = EditorInterface.get_editor_undo_redo()
|
||||||
|
undo_redo.create_action("Randomized Sprite2D Color")
|
||||||
|
undo_redo.add_do_property(self, &"self_modulate", Color(randf(), randf(), randf()))
|
||||||
|
undo_redo.add_undo_property(self, &"self_modulate", self_modulate)
|
||||||
|
undo_redo.commit_action()
|
||||||
|
[/codeblock]
|
||||||
|
[b]Note:[/b] The property is exported without the [constant PROPERTY_USAGE_STORAGE] flag because a [Callable] cannot be properly serialized and stored in a file.
|
||||||
|
[b]Note:[/b] In an exported project neither [EditorInterface] nor [EditorUndoRedoManager] exist, which may cause some scripts to break. To prevent this, you can use [method Engine.get_singleton] and omit the static type from the variable declaration:
|
||||||
|
[codeblock]
|
||||||
|
var undo_redo = Engine.get_singleton(&"EditorInterface").get_editor_undo_redo()
|
||||||
|
[/codeblock]
|
||||||
|
[b]Note:[/b] Avoid storing lambda callables in member variables of [RefCounted]-based classes (e.g. resources), as this can lead to memory leaks. Use only method callables and optionally [method Callable.bind] or [method Callable.unbind].
|
||||||
|
</description>
|
||||||
|
</annotation>
|
||||||
<annotation name="@icon">
|
<annotation name="@icon">
|
||||||
<return type="void" />
|
<return type="void" />
|
||||||
<param index="0" name="icon_path" type="String" />
|
<param index="0" name="icon_path" type="String" />
|
||||||
|
|
|
@ -122,6 +122,7 @@ GDScriptParser::GDScriptParser() {
|
||||||
register_annotation(MethodInfo("@export_flags_avoidance"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_AVOIDANCE, Variant::INT>);
|
register_annotation(MethodInfo("@export_flags_avoidance"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_AVOIDANCE, Variant::INT>);
|
||||||
register_annotation(MethodInfo("@export_storage"), AnnotationInfo::VARIABLE, &GDScriptParser::export_storage_annotation);
|
register_annotation(MethodInfo("@export_storage"), AnnotationInfo::VARIABLE, &GDScriptParser::export_storage_annotation);
|
||||||
register_annotation(MethodInfo("@export_custom", PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "PropertyHint"), PropertyInfo(Variant::STRING, "hint_string"), PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_BITFIELD, "PropertyUsageFlags")), AnnotationInfo::VARIABLE, &GDScriptParser::export_custom_annotation, varray(PROPERTY_USAGE_DEFAULT));
|
register_annotation(MethodInfo("@export_custom", PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "PropertyHint"), PropertyInfo(Variant::STRING, "hint_string"), PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_BITFIELD, "PropertyUsageFlags")), AnnotationInfo::VARIABLE, &GDScriptParser::export_custom_annotation, varray(PROPERTY_USAGE_DEFAULT));
|
||||||
|
register_annotation(MethodInfo("@export_tool_button", PropertyInfo(Variant::STRING, "text"), PropertyInfo(Variant::STRING, "icon")), AnnotationInfo::VARIABLE, &GDScriptParser::export_tool_button_annotation, varray(""));
|
||||||
// Export grouping annotations.
|
// Export grouping annotations.
|
||||||
register_annotation(MethodInfo("@export_category", PropertyInfo(Variant::STRING, "name")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_CATEGORY>);
|
register_annotation(MethodInfo("@export_category", PropertyInfo(Variant::STRING, "name")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_CATEGORY>);
|
||||||
register_annotation(MethodInfo("@export_group", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_GROUP>, varray(""));
|
register_annotation(MethodInfo("@export_group", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_GROUP>, varray(""));
|
||||||
|
@ -4618,10 +4619,10 @@ bool GDScriptParser::export_annotations(AnnotationNode *p_annotation, Node *p_ta
|
||||||
// For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values,
|
// For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values,
|
||||||
// or handle array exports in a special way, so they are implemented as separate methods.
|
// or handle array exports in a special way, so they are implemented as separate methods.
|
||||||
|
|
||||||
bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) {
|
bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
|
||||||
ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
|
ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
|
||||||
|
|
||||||
VariableNode *variable = static_cast<VariableNode *>(p_node);
|
VariableNode *variable = static_cast<VariableNode *>(p_target);
|
||||||
if (variable->is_static) {
|
if (variable->is_static) {
|
||||||
push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);
|
push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);
|
||||||
return false;
|
return false;
|
||||||
|
@ -4640,11 +4641,11 @@ bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Nod
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_node, ClassNode *p_class) {
|
bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
|
||||||
ERR_FAIL_COND_V_MSG(p_node->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
|
ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
|
||||||
ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)");
|
ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)");
|
||||||
|
|
||||||
VariableNode *variable = static_cast<VariableNode *>(p_node);
|
VariableNode *variable = static_cast<VariableNode *>(p_target);
|
||||||
if (variable->is_static) {
|
if (variable->is_static) {
|
||||||
push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);
|
push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);
|
||||||
return false;
|
return false;
|
||||||
|
@ -4668,12 +4669,56 @@ bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <PropertyUsageFlags t_usage>
|
bool GDScriptParser::export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
|
||||||
bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
|
#ifdef TOOLS_ENABLED
|
||||||
if (p_annotation->resolved_arguments.is_empty()) {
|
ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));
|
||||||
|
ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);
|
||||||
|
|
||||||
|
if (!is_tool()) {
|
||||||
|
push_error(R"(Tool buttons can only be used in tool scripts (add "@tool" to the top of the script).)", p_annotation);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VariableNode *variable = static_cast<VariableNode *>(p_target);
|
||||||
|
|
||||||
|
if (variable->is_static) {
|
||||||
|
push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (variable->exported) {
|
||||||
|
push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DataType variable_type = variable->get_datatype();
|
||||||
|
if (!variable_type.is_variant() && variable_type.is_hard_type()) {
|
||||||
|
if (variable_type.kind != DataType::BUILTIN || variable_type.builtin_type != Variant::CALLABLE) {
|
||||||
|
push_error(vformat(R"("@export_tool_button" annotation requires a variable of type "Callable", but type "%s" was given instead.)", variable_type.to_string()), p_annotation);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable->exported = true;
|
||||||
|
|
||||||
|
// Build the hint string (format: `<text>[,<icon>]`).
|
||||||
|
String hint_string = p_annotation->resolved_arguments[0].operator String(); // Button text.
|
||||||
|
if (p_annotation->resolved_arguments.size() > 1) {
|
||||||
|
hint_string += "," + p_annotation->resolved_arguments[1].operator String(); // Button icon.
|
||||||
|
}
|
||||||
|
|
||||||
|
variable->export_info.type = Variant::CALLABLE;
|
||||||
|
variable->export_info.hint = PROPERTY_HINT_TOOL_BUTTON;
|
||||||
|
variable->export_info.hint_string = hint_string;
|
||||||
|
variable->export_info.usage = PROPERTY_USAGE_EDITOR;
|
||||||
|
#endif // TOOLS_ENABLED
|
||||||
|
|
||||||
|
return true; // Only available in editor.
|
||||||
|
}
|
||||||
|
|
||||||
|
template <PropertyUsageFlags t_usage>
|
||||||
|
bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {
|
||||||
|
ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);
|
||||||
|
|
||||||
p_annotation->export_info.name = p_annotation->resolved_arguments[0];
|
p_annotation->export_info.name = p_annotation->resolved_arguments[0];
|
||||||
|
|
||||||
switch (t_usage) {
|
switch (t_usage) {
|
||||||
|
|
|
@ -1507,6 +1507,7 @@ private:
|
||||||
bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
|
bool export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
template <PropertyUsageFlags t_usage>
|
template <PropertyUsageFlags t_usage>
|
||||||
bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
bool warning_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
bool warning_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
@export_tool_button("Click me!") var action
|
|
@ -0,0 +1,2 @@
|
||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
Tool buttons can only be used in tool scripts (add "@tool" to the top of the script).
|
|
@ -1,3 +1,4 @@
|
||||||
|
@tool
|
||||||
class_name ExportVariableTest
|
class_name ExportVariableTest
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
|
@ -47,6 +48,10 @@ const PreloadedUnnamedClass = preload("./export_variable_unnamed.notest.gd")
|
||||||
@export_custom(PROPERTY_HINT_ENUM, "A,B,C") var test_export_custom_weak_int = 5
|
@export_custom(PROPERTY_HINT_ENUM, "A,B,C") var test_export_custom_weak_int = 5
|
||||||
@export_custom(PROPERTY_HINT_ENUM, "A,B,C") var test_export_custom_hard_int: int = 6
|
@export_custom(PROPERTY_HINT_ENUM, "A,B,C") var test_export_custom_hard_int: int = 6
|
||||||
|
|
||||||
|
# `@export_tool_button`.
|
||||||
|
@export_tool_button("Click me!") var test_tool_button_1: Callable
|
||||||
|
@export_tool_button("Click me!", "ColorRect") var test_tool_button_2: Callable
|
||||||
|
|
||||||
func test():
|
func test():
|
||||||
for property in get_property_list():
|
for property in get_property_list():
|
||||||
if str(property.name).begins_with("test_"):
|
if str(property.name).begins_with("test_"):
|
||||||
|
|
|
@ -55,3 +55,7 @@ var test_export_custom_weak_int: int = 5
|
||||||
hint=ENUM hint_string="A,B,C" usage=DEFAULT|SCRIPT_VARIABLE class_name=&""
|
hint=ENUM hint_string="A,B,C" usage=DEFAULT|SCRIPT_VARIABLE class_name=&""
|
||||||
var test_export_custom_hard_int: int = 6
|
var test_export_custom_hard_int: int = 6
|
||||||
hint=ENUM hint_string="A,B,C" usage=DEFAULT|SCRIPT_VARIABLE class_name=&""
|
hint=ENUM hint_string="A,B,C" usage=DEFAULT|SCRIPT_VARIABLE class_name=&""
|
||||||
|
var test_tool_button_1: Callable = Callable()
|
||||||
|
hint=TOOL_BUTTON hint_string="Click me!" usage=EDITOR|SCRIPT_VARIABLE class_name=&""
|
||||||
|
var test_tool_button_2: Callable = Callable()
|
||||||
|
hint=TOOL_BUTTON hint_string="Click me!,ColorRect" usage=EDITOR|SCRIPT_VARIABLE class_name=&""
|
||||||
|
|
|
@ -205,6 +205,9 @@ static func get_property_hint_name(hint: PropertyHint) -> String:
|
||||||
return "PROPERTY_HINT_HIDE_QUATERNION_EDIT"
|
return "PROPERTY_HINT_HIDE_QUATERNION_EDIT"
|
||||||
PROPERTY_HINT_PASSWORD:
|
PROPERTY_HINT_PASSWORD:
|
||||||
return "PROPERTY_HINT_PASSWORD"
|
return "PROPERTY_HINT_PASSWORD"
|
||||||
|
PROPERTY_HINT_TOOL_BUTTON:
|
||||||
|
return "PROPERTY_HINT_TOOL_BUTTON"
|
||||||
|
|
||||||
printerr("Argument `hint` is invalid. Use `PROPERTY_HINT_*` constants.")
|
printerr("Argument `hint` is invalid. Use `PROPERTY_HINT_*` constants.")
|
||||||
return "<invalid hint>"
|
return "<invalid hint>"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue