Skip to content

Commit

Permalink
Draft: Use PROPERTY_HINT_TOOL_BUTTON as backing instead of a Callable…
Browse files Browse the repository at this point in the history
… type.
  • Loading branch information
Macksaur committed Sep 26, 2024
1 parent 7b3c01f commit b53967b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/core_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ void register_global_constants() {
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE);
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_HIDE_QUATERNION_EDIT);
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PASSWORD);
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_TOOL_BUTTON);
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX);

BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NONE);
Expand Down
2 changes: 2 additions & 0 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum PropertyHint {
PROPERTY_HINT_PASSWORD,
PROPERTY_HINT_LAYERS_AVOIDANCE,
PROPERTY_HINT_DICTIONARY_TYPE,
PROPERTY_HINT_TOOL_BUTTON,
PROPERTY_HINT_MAX,
};

Expand Down Expand Up @@ -135,6 +136,7 @@ enum PropertyUsageFlags {
#define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
#define ADD_SUBGROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix, m_depth)
#define ADD_LINKED_PROPERTY(m_property, m_linked_property) ::ClassDB::add_linked_property(get_class_static(), m_property, m_linked_property)
#define ADD_TOOL_BUTTON(m_method, m_label, m_icon) ::ClassDB::add_property(get_class_static(), PropertyInfo(Variant::NIL, "@tool_button_" + String(m_method), PROPERTY_HINT_TOOL_BUTTON, String(m_method) + "," + String(m_label) + "," + String(m_icon), PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), StringName(), StringName())

#define ADD_ARRAY_COUNT(m_label, m_count_property, m_count_property_setter, m_count_property_getter, m_prefix) ClassDB::add_property_array_count(get_class_static(), m_label, m_count_property, _scs_create(m_count_property_setter), _scs_create(m_count_property_getter), m_prefix)
#define ADD_ARRAY_COUNT_WITH_USAGE_FLAGS(m_label, m_count_property, m_count_property_setter, m_count_property_getter, m_prefix, m_property_usage_flags) ClassDB::add_property_array_count(get_class_static(), m_label, m_count_property, _scs_create(m_count_property_setter), _scs_create(m_count_property_getter), m_prefix, m_property_usage_flags)
Expand Down
4 changes: 3 additions & 1 deletion doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,9 @@
<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.
</constant>
<constant name="PROPERTY_HINT_MAX" value="39" enum="PropertyHint">
<constant name="PROPERTY_HINT_TOOL_BUTTON" value="39" enum="PropertyHint">
</constant>
<constant name="PROPERTY_HINT_MAX" value="40" enum="PropertyHint">
Represents the size of the [enum PropertyHint] enum.
</constant>
<constant name="PROPERTY_USAGE_NONE" value="0" enum="PropertyUsageFlags" is_bitfield="true">
Expand Down
17 changes: 9 additions & 8 deletions editor/plugins/tool_button_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
#include "editor/editor_undo_redo_manager.h"
#include "scene/gui/button.h"

bool ToolButtonInspectorPlugin::can_handle(Object *p_object) {
bool EditorInspectorToolButtonPlugin::can_handle(Object *p_object) {
Ref<Script> scr = p_object->get_script();
return scr.is_valid() && scr->is_tool();
}

void ToolButtonInspectorPlugin::update_action_icon(Button *p_action_button) {
void EditorInspectorToolButtonPlugin::update_action_icon(Button *p_action_button) {
p_action_button->set_icon(p_action_button->get_editor_theme_icon(action_icon));
}

void ToolButtonInspectorPlugin::call_action(Object *p_object, const StringName &p_method_name) {
void EditorInspectorToolButtonPlugin::call_action(Object *p_object, const StringName &p_method_name) {
bool method_is_valid = false;
int method_arg_count = p_object->get_method_argument_count(p_method_name, &method_is_valid);

Expand All @@ -65,8 +65,8 @@ void ToolButtonInspectorPlugin::call_action(Object *p_object, const StringName &
ERR_FAIL_COND_MSG(ce.error != Callable::CallError::CALL_OK, vformat("Error calling tool button method on %s: %s.", p_object->get_class_name(), Variant::get_call_error_text(p_method_name, &args, argc, ce)));
}

bool ToolButtonInspectorPlugin::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_usage.has_flag(PROPERTY_USAGE_EDITOR)) {
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_hint != PROPERTY_HINT_TOOL_BUTTON || !p_usage.has_flag(PROPERTY_USAGE_EDITOR)) {
return false;
}

Expand All @@ -85,15 +85,16 @@ bool ToolButtonInspectorPlugin::parse_property(Object *p_object, const Variant::

Button *action_button = EditorInspector::create_inspector_action_button(action_text);
action_button->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
action_button->connect(SceneStringName(theme_changed), callable_mp(this, &ToolButtonInspectorPlugin::update_action_icon).bind(action_button));
action_button->connect(SceneStringName(pressed), callable_mp(this, &ToolButtonInspectorPlugin::call_action).bind(p_object, method));
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, method));

add_custom_control(action_button);
return true;
}

ToolButtonEditorPlugin::ToolButtonEditorPlugin() {
Ref<ToolButtonInspectorPlugin> plugin;
Ref<EditorInspectorToolButtonPlugin> plugin;
plugin.instantiate();
add_inspector_plugin(plugin);
}
4 changes: 2 additions & 2 deletions editor/plugins/tool_button_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "editor/editor_inspector.h"
#include "editor/plugins/editor_plugin.h"

class ToolButtonInspectorPlugin : public EditorInspectorPlugin {
GDCLASS(ToolButtonInspectorPlugin, EditorInspectorPlugin);
class EditorInspectorToolButtonPlugin : public EditorInspectorPlugin {
GDCLASS(EditorInspectorToolButtonPlugin, EditorInspectorPlugin);

public:
StringName action_icon;
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,7 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP
prop_info.name = annotation->export_info.name;
prop_info.usage = annotation->export_info.usage;
prop_info.type = annotation->export_info.type;
prop_info.hint = annotation->export_info.hint;
prop_info.hint_string = annotation->export_info.hint_string;
minfo.property_info = prop_info;

Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4701,7 +4701,7 @@ bool GDScriptParser::export_tool_button_annotation(AnnotationNode *p_annotation,
const FunctionNode *func = member.function;

p_annotation->export_info.name = "@tool_button_" + func->identifier->name;
p_annotation->export_info.type = Variant::Type::CALLABLE;
p_annotation->export_info.hint = PROPERTY_HINT_TOOL_BUTTON;
p_annotation->export_info.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL;

if (!func->parameters.is_empty() && func->parameters[0]->datatype_specifier != nullptr) {
Expand Down

0 comments on commit b53967b

Please sign in to comment.