diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index e9d34fae3b1..dce2888266a 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -35,6 +35,8 @@ #include "core/string/translation.h" #include "scene/theme/theme_db.h" +PropertyListHelper ItemList::base_property_helper; + void ItemList::_shape_text(int p_idx) { Item &item = items.write[p_idx]; @@ -1678,23 +1680,10 @@ TextServer::OverrunBehavior ItemList::get_text_overrun_behavior() const { } bool ItemList::_set(const StringName &p_name, const Variant &p_value) { - Vector components = String(p_name).split("/", true, 2); - if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) { - int item_index = components[0].trim_prefix("item_").to_int(); - if (components[1] == "text") { - set_item_text(item_index, p_value); - return true; - } else if (components[1] == "icon") { - set_item_icon(item_index, p_value); - return true; - } else if (components[1] == "disabled") { - set_item_disabled(item_index, p_value); - return true; - } else if (components[1] == "selectable") { - set_item_selectable(item_index, p_value); - return true; - } + if (property_helper.property_set_value(p_name, p_value)) { + return true; } + #ifndef DISABLE_DEPRECATED // Compatibility. if (p_name == "items") { @@ -1717,42 +1706,19 @@ bool ItemList::_set(const StringName &p_name, const Variant &p_value) { } bool ItemList::_get(const StringName &p_name, Variant &r_ret) const { - Vector components = String(p_name).split("/", true, 2); - if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) { - int item_index = components[0].trim_prefix("item_").to_int(); - if (components[1] == "text") { - r_ret = get_item_text(item_index); - return true; - } else if (components[1] == "icon") { - r_ret = get_item_icon(item_index); - return true; - } else if (components[1] == "disabled") { - r_ret = is_item_disabled(item_index); - return true; - } else if (components[1] == "selectable") { - r_ret = is_item_selectable(item_index); - return true; - } - } - return false; + return property_helper.property_get_value(p_name, r_ret); } void ItemList::_get_property_list(List *p_list) const { - for (int i = 0; i < items.size(); i++) { - p_list->push_back(PropertyInfo(Variant::STRING, vformat("item_%d/text", i))); + property_helper.get_property_list(p_list, items.size()); +} - PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"); - pi.usage &= ~(get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); - p_list->push_back(pi); +bool ItemList::_property_can_revert(const StringName &p_name) const { + return property_helper.property_can_revert(p_name); +} - pi = PropertyInfo(Variant::BOOL, vformat("item_%d/selectable", i)); - pi.usage &= ~(is_item_selectable(i) ? PROPERTY_USAGE_STORAGE : 0); - p_list->push_back(pi); - - pi = PropertyInfo(Variant::BOOL, vformat("item_%d/disabled", i)); - pi.usage &= ~(!is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0); - p_list->push_back(pi); - } +bool ItemList::_property_get_revert(const StringName &p_name, Variant &r_property) const { + return property_helper.property_get_revert(p_name, r_property); } void ItemList::_bind_methods() { @@ -1919,6 +1885,14 @@ void ItemList::_bind_methods() { BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_style, "cursor_unfocused"); BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_focus_style, "cursor"); BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, guide_color); + + Item defaults(true); + + base_property_helper.set_prefix("item_"); + base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, "set_item_text", "get_item_text"); + base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, "set_item_icon", "get_item_icon"); + base_property_helper.register_property(PropertyInfo(Variant::BOOL, "selectable"), defaults.selectable, "set_item_selectable", "is_item_selectable"); + base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, "set_item_disabled", "is_item_disabled"); } ItemList::ItemList() { @@ -1930,6 +1904,8 @@ ItemList::ItemList() { set_focus_mode(FOCUS_ALL); set_clip_contents(true); + + property_helper.setup_for_instance(base_property_helper, this); } ItemList::~ItemList() { diff --git a/scene/gui/item_list.h b/scene/gui/item_list.h index 28f90120583..571f5b77e68 100644 --- a/scene/gui/item_list.h +++ b/scene/gui/item_list.h @@ -33,6 +33,7 @@ #include "scene/gui/control.h" #include "scene/gui/scroll_bar.h" +#include "scene/property_list_helper.h" #include "scene/resources/text_paragraph.h" class ItemList : public Control { @@ -82,8 +83,13 @@ private: Item() { text_buf.instantiate(); } + + Item(bool p_dummy) {} }; + static PropertyListHelper base_property_helper; + PropertyListHelper property_helper; + int current = -1; int hovered = -1; @@ -157,6 +163,8 @@ protected: bool _set(const StringName &p_name, const Variant &p_value); bool _get(const StringName &p_name, Variant &r_ret) const; void _get_property_list(List *p_list) const; + bool _property_can_revert(const StringName &p_name) const; + bool _property_get_revert(const StringName &p_name, Variant &r_property) const; static void _bind_methods(); public: diff --git a/scene/property_list_helper.cpp b/scene/property_list_helper.cpp new file mode 100644 index 00000000000..596a6a050a6 --- /dev/null +++ b/scene/property_list_helper.cpp @@ -0,0 +1,138 @@ +/**************************************************************************/ +/* property_list_helper.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 "property_list_helper.h" + +const PropertyListHelper::Property *PropertyListHelper::_get_property(const String &p_property, int *r_index) const { + const Vector components = p_property.split("/", true, 2); + if (components.size() < 2 || !components[0].begins_with(prefix)) { + return nullptr; + } + + { + const String index_string = components[0].trim_prefix(prefix); + if (!index_string.is_valid_int()) { + return nullptr; + } + *r_index = index_string.to_int(); + } + + return property_list.getptr(components[1]); +} + +void PropertyListHelper::_bind_property(const Property &p_property, const Object *p_object) { + Property property = p_property; + property.info = p_property.info; + property.default_value = p_property.default_value; + property.setter = Callable(p_object, p_property.setter_name); + property.getter = Callable(p_object, p_property.getter_name); + + property_list[property.info.name] = property; +} + +void PropertyListHelper::set_prefix(const String &p_prefix) { + prefix = p_prefix; +} + +void PropertyListHelper::register_property(const PropertyInfo &p_info, const Variant &p_default, const StringName &p_setter, const StringName &p_getter) { + Property property; + property.info = p_info; + property.default_value = p_default; + property.setter_name = p_setter; + property.getter_name = p_getter; + + property_list[p_info.name] = property; +} + +void PropertyListHelper::setup_for_instance(const PropertyListHelper &p_base, const Object *p_object) { + prefix = p_base.prefix; + for (const KeyValue &E : p_base.property_list) { + _bind_property(E.value, p_object); + } +} + +void PropertyListHelper::get_property_list(List *p_list, int p_count) const { + for (int i = 0; i < p_count; i++) { + for (const KeyValue &E : property_list) { + const Property &property = E.value; + + PropertyInfo info = property.info; + if (property.getter.call(i) == property.default_value) { + info.usage &= (~PROPERTY_USAGE_STORAGE); + } + + info.name = vformat("%s%d/%s", prefix, i, info.name); + p_list->push_back(info); + } + } +} + +bool PropertyListHelper::property_get_value(const String &p_property, Variant &r_ret) const { + int index; + const Property *property = _get_property(p_property, &index); + + if (property) { + r_ret = property->getter.call(index); + return true; + } + return false; +} + +bool PropertyListHelper::property_set_value(const String &p_property, const Variant &p_value) const { + int index; + const Property *property = _get_property(p_property, &index); + + if (property) { + property->setter.call(index, p_value); + return true; + } + return false; +} + +bool PropertyListHelper::property_can_revert(const String &p_property) const { + int index; + const Property *property = _get_property(p_property, &index); + + if (property) { + return property->getter.call(index) != property->default_value; + } + return false; +} + +bool PropertyListHelper::property_get_revert(const String &p_property, Variant &r_value) const { + int index; + const Property *property = _get_property(p_property, &index); + + if (property) { + r_value = property->default_value; + return true; + } + return false; +} diff --git a/scene/property_list_helper.h b/scene/property_list_helper.h new file mode 100644 index 00000000000..f3cf0239a14 --- /dev/null +++ b/scene/property_list_helper.h @@ -0,0 +1,64 @@ +/**************************************************************************/ +/* property_list_helper.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 PROPERTY_LIST_HELPER_H +#define PROPERTY_LIST_HELPER_H + +#include "core/object/object.h" + +class PropertyListHelper { + struct Property { + PropertyInfo info; + Variant default_value; + StringName setter_name; + StringName getter_name; + Callable setter; + Callable getter; + }; + + String prefix; + HashMap property_list; + + const Property *_get_property(const String &p_property, int *r_index) const; + void _bind_property(const Property &p_property, const Object *p_object); + +public: + void set_prefix(const String &p_prefix); + void register_property(const PropertyInfo &p_info, const Variant &p_default, const StringName &p_setter, const StringName &p_getter); + void setup_for_instance(const PropertyListHelper &p_base, const Object *p_object); + + void get_property_list(List *p_list, int p_count) const; + bool property_get_value(const String &p_property, Variant &r_ret) const; + bool property_set_value(const String &p_property, const Variant &p_value) const; + bool property_can_revert(const String &p_property) const; + bool property_get_revert(const String &p_property, Variant &r_value) const; +}; + +#endif // PROPERTY_LIST_HELPER_H