From 9853a691447cd4e279f48820067174d3833b0065 Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Sat, 24 Jun 2023 13:03:28 -0500 Subject: Implement typed dictionaries --- editor/connections_dialog.cpp | 16 ++++ editor/doc_tools.cpp | 2 + editor/editor_help.cpp | 22 +++++- editor/editor_properties.cpp | 2 +- editor/editor_properties_array_dict.cpp | 127 +++++++++++++++++++++++++++++--- editor/editor_properties_array_dict.h | 12 ++- 6 files changed, 164 insertions(+), 17 deletions(-) (limited to 'editor') diff --git a/editor/connections_dialog.cpp b/editor/connections_dialog.cpp index eb0ab1174b..063241fd1b 100644 --- a/editor/connections_dialog.cpp +++ b/editor/connections_dialog.cpp @@ -576,6 +576,22 @@ String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArra type_name = "Array"; } break; + case Variant::DICTIONARY: + type_name = "Dictionary"; + if (pi.hint == PROPERTY_HINT_DICTIONARY_TYPE && !pi.hint_string.is_empty()) { + String key_hint = pi.hint_string.get_slice(";", 0); + String value_hint = pi.hint_string.get_slice(";", 1); + if (key_hint.is_empty() || key_hint.begins_with("res://")) { + key_hint = "Variant"; + } + if (value_hint.is_empty() || value_hint.begins_with("res://")) { + value_hint = "Variant"; + } + if (key_hint != "Variant" || value_hint != "Variant") { + type_name += "[" + key_hint + ", " + value_hint + "]"; + } + } + break; case Variant::OBJECT: if (pi.class_name != StringName()) { type_name = pi.class_name; diff --git a/editor/doc_tools.cpp b/editor/doc_tools.cpp index bf5b717c19..ae62504768 100644 --- a/editor/doc_tools.cpp +++ b/editor/doc_tools.cpp @@ -571,6 +571,8 @@ void DocTools::generate(BitField p_flags) { prop.type = retinfo.class_name; } else if (retinfo.type == Variant::ARRAY && retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) { prop.type = retinfo.hint_string + "[]"; + } else if (retinfo.type == Variant::DICTIONARY && retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) { + prop.type = "Dictionary[" + retinfo.hint_string.replace(";", ", ") + "]"; } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { prop.type = retinfo.hint_string; } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index 9b0c05d910..c596319851 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -376,10 +376,10 @@ static void _add_type_to_rt(const String &p_type, const String &p_enum, bool p_i } p_rt->push_color(type_color); - bool add_array = false; + bool add_typed_container = false; if (can_ref) { if (link_t.ends_with("[]")) { - add_array = true; + add_typed_container = true; link_t = link_t.trim_suffix("[]"); display_t = display_t.trim_suffix("[]"); @@ -387,6 +387,22 @@ static void _add_type_to_rt(const String &p_type, const String &p_enum, bool p_i p_rt->add_text("Array"); p_rt->pop(); // meta p_rt->add_text("["); + } else if (link_t.begins_with("Dictionary[")) { + add_typed_container = true; + link_t = link_t.trim_prefix("Dictionary[").trim_suffix("]"); + display_t = display_t.trim_prefix("Dictionary[").trim_suffix("]"); + + p_rt->push_meta("#Dictionary", RichTextLabel::META_UNDERLINE_ON_HOVER); // class + p_rt->add_text("Dictionary"); + p_rt->pop(); // meta + p_rt->add_text("["); + p_rt->push_meta("#" + link_t.get_slice(", ", 0), RichTextLabel::META_UNDERLINE_ON_HOVER); // class + p_rt->add_text(_contextualize_class_specifier(display_t.get_slice(", ", 0), p_class)); + p_rt->pop(); // meta + p_rt->add_text(", "); + + link_t = link_t.get_slice(", ", 1); + display_t = _contextualize_class_specifier(display_t.get_slice(", ", 1), p_class); } else if (is_bitfield) { p_rt->push_color(Color(type_color, 0.5)); p_rt->push_hint(TTR("This value is an integer composed as a bitmask of the following flags.")); @@ -405,7 +421,7 @@ static void _add_type_to_rt(const String &p_type, const String &p_enum, bool p_i p_rt->add_text(display_t); if (can_ref) { p_rt->pop(); // meta - if (add_array) { + if (add_typed_container) { p_rt->add_text("]"); } else if (is_bitfield) { p_rt->push_color(Color(type_color, 0.5)); diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 19a4165041..123d903220 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -3771,7 +3771,7 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_ return editor; } else { EditorPropertyDictionary *editor = memnew(EditorPropertyDictionary); - editor->setup(p_hint); + editor->setup(p_hint, p_hint_text); return editor; } } break; diff --git a/editor/editor_properties_array_dict.cpp b/editor/editor_properties_array_dict.cpp index f5d016629f..f03eef4d4d 100644 --- a/editor/editor_properties_array_dict.cpp +++ b/editor/editor_properties_array_dict.cpp @@ -863,6 +863,26 @@ EditorPropertyArray::EditorPropertyArray() { ///////////////////// DICTIONARY /////////////////////////// +void EditorPropertyDictionary::initialize_dictionary(Variant &p_dictionary) { + if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) { + Dictionary dict; + StringName key_subtype_class; + Ref