Skip to content

Commit

Permalink
Allow configuration warnings to refer to a property
Browse files Browse the repository at this point in the history
This is used by the inspector so it can show a warning icon on
a specific property.
  • Loading branch information
RedMser committed Jul 5, 2023
1 parent e044e13 commit 12ede25
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 25 deletions.
3 changes: 3 additions & 0 deletions doc/classes/EditorProperty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<member name="checked" type="bool" setter="set_checked" getter="is_checked" default="false">
Used by the inspector, set to [code]true[/code] when the property is checked.
</member>
<member name="configuration_warning" type="String" setter="set_configuration_warning" getter="get_configuration_warning" default="&quot;&quot;">
Used by the inspector, set to show a configuration warning on the property.
</member>
<member name="deletable" type="bool" setter="set_deletable" getter="is_deletable" default="false">
Used by the inspector, set to [code]true[/code] when the property can be deleted by the user.
</member>
Expand Down
6 changes: 5 additions & 1 deletion doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
</description>
</method>
<method name="_get_configuration_warnings" qualifiers="virtual const">
<return type="PackedStringArray" />
<return type="Array" />
<description>
The elements in the array returned from this method are displayed as warnings in the Scene dock if the script that overrides it is a [code]tool[/code] script.
Each array element must either be a [String] or a [Dictionary].
A dictionary element must contain a key [code]message[/code] of type [String] which is shown in the user interface.
The dictionary may optionally contain a key [code]property[/code] of type [NodePath], which also shows this warning in the inspector on the corresponding property.
If a string is found in the returned array, it is converted to an equivalent dictionary with the [code]message[/code] field set.
Returning an empty array produces no warnings.
Call [method update_configuration_warnings] when the warnings need to be updated for this node.
[codeblock]
Expand Down
100 changes: 100 additions & 0 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ void EditorProperty::_notification(int p_what) {
text_size -= close->get_width() + 4 * EDSCALE;
}
}

if (!configuration_warning.is_empty() && !read_only) {
Ref<Texture2D> warning;

warning = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));

rect.size.x -= warning->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree"));

if (is_layout_rtl()) {
rect.position.x += warning->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree"));
}

if (no_children) {
text_size -= warning->get_width() + 4 * EDSCALE;
}
}
}

//set children
Expand Down Expand Up @@ -393,6 +409,38 @@ void EditorProperty::_notification(int p_what) {
} else {
delete_rect = Rect2();
}

if (!configuration_warning.is_empty() && !read_only) {
Ref<Texture2D> warning;

StringName warning_icon;
Node *node = Object::cast_to<Node>(object);
if (node) {
const int warning_num = node->get_configuration_warnings_filtered(property_path).size();
warning_icon = Node::get_configuration_warning_icon(warning_num);
} else {
// This shouldn't happen, but let's not crash over an icon.
warning_icon = "NodeWarning";
}
warning = get_theme_icon(warning_icon, SNAME("EditorIcons"));

ofs -= warning->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree"));

Color color2(1, 1, 1);
if (configuration_warning_hover) {
color2.r *= 1.2;
color2.g *= 1.2;
color2.b *= 1.2;
}
configuration_warning_rect = Rect2(ofs, ((size.height - warning->get_height()) / 2), warning->get_width(), warning->get_height());
if (rtl) {
draw_texture(warning, Vector2(size.width - configuration_warning_rect.position.x - warning->get_width(), configuration_warning_rect.position.y), color2);
} else {
draw_texture(warning, configuration_warning_rect.position, color2);
}
} else {
configuration_warning_rect = Rect2();
}
} break;
}
}
Expand Down Expand Up @@ -664,6 +712,12 @@ void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
check_hover = new_check_hover;
queue_redraw();
}

bool new_configuration_warning_hover = configuration_warning_rect.has_point(mpos) && !button_left;
if (new_configuration_warning_hover != configuration_warning_hover) {
configuration_warning_hover = new_configuration_warning_hover;
queue_redraw();
}
}

Ref<InputEventMouseButton> mb = p_event;
Expand Down Expand Up @@ -721,6 +775,19 @@ void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
queue_redraw();
emit_signal(SNAME("property_checked"), property, checked);
}

if (configuration_warning_rect.has_point(mpos)) {
const PackedInt32Array boundaries = TS->string_get_word_breaks(configuration_warning, "", 80);
PackedStringArray lines;
for (int i = 0; i < boundaries.size(); i += 2) {
const int start = boundaries[i];
const int end = boundaries[i + 1];
lines.append(configuration_warning.substr(start, end - start + 1));
}

warning_dialog->set_text(String("\n").join(lines));
warning_dialog->popup_centered();
}
} else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
accept_event();
_update_popup();
Expand Down Expand Up @@ -846,6 +913,16 @@ float EditorProperty::get_name_split_ratio() const {
return split_ratio;
}

void EditorProperty::set_configuration_warning(const String &p_configuration_warning) {
configuration_warning = p_configuration_warning;
queue_redraw();
queue_sort();
}

String EditorProperty::get_configuration_warning() const {
return configuration_warning;
}

void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
object = p_object;
property = p_property;
Expand Down Expand Up @@ -902,6 +979,12 @@ void EditorProperty::_update_pin_flags() {
}
}

void EditorProperty::_update_configuration_warnings() {
if (Node *node = Object::cast_to<Node>(object)) {
set_configuration_warning(node->get_configuration_warnings_as_string(property_path));
}
}

static Control *make_help_bit(const String &p_text, const String &p_warning, const Color &p_warn_color, bool p_property) {
EditorHelpBit *help_bit = memnew(EditorHelpBit);
help_bit->get_rich_text()->set_custom_minimum_size(Size2(360 * EDSCALE, 1));
Expand Down Expand Up @@ -991,6 +1074,9 @@ void EditorProperty::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);

ClassDB::bind_method(D_METHOD("set_configuration_warning", "configuration_warning"), &EditorProperty::set_configuration_warning);
ClassDB::bind_method(D_METHOD("get_configuration_warning"), &EditorProperty::get_configuration_warning);

ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);

Expand All @@ -1008,6 +1094,7 @@ void EditorProperty::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_warning"), "set_draw_warning", "is_draw_warning");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "configuration_warning"), "set_configuration_warning", "get_configuration_warning");

ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::STRING_NAME, "field"), PropertyInfo(Variant::BOOL, "changing")));
ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
Expand Down Expand Up @@ -1037,6 +1124,10 @@ EditorProperty::EditorProperty() {
bottom_editor = nullptr;
menu = nullptr;
set_process_shortcut_input(true);

warning_dialog = memnew(AcceptDialog);
add_child(warning_dialog);
warning_dialog->set_title(TTR("Node Configuration Warning!"));
}

void EditorProperty::_update_popup() {
Expand Down Expand Up @@ -3291,6 +3382,7 @@ void EditorInspector::update_tree() {
ep->set_keying(keying);
ep->set_read_only(property_read_only || all_read_only);
ep->set_deletable(deletable_properties || p.name.begins_with("metadata/"));
ep->_update_configuration_warnings();
}

current_vbox->add_child(editors[i].property_editor);
Expand Down Expand Up @@ -3911,6 +4003,12 @@ void EditorInspector::_node_removed(Node *p_node) {
}
}

void EditorInspector::_warning_changed(Node *p_node) {
if (p_node == object) {
update_tree_pending = true;
}
}

void EditorInspector::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
Expand All @@ -3922,6 +4020,7 @@ void EditorInspector::_notification(int p_what) {
case NOTIFICATION_ENTER_TREE: {
if (!sub_inspector) {
get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
get_tree()->connect("node_configuration_warning_changed", callable_mp(this, &EditorInspector::_warning_changed));
}
} break;

Expand All @@ -3938,6 +4037,7 @@ void EditorInspector::_notification(int p_what) {
case NOTIFICATION_EXIT_TREE: {
if (!sub_inspector) {
get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
get_tree()->disconnect("node_configuration_warning_changed", callable_mp(this, &EditorInspector::_warning_changed));
}
edit(nullptr);
} break;
Expand Down
10 changes: 10 additions & 0 deletions editor/editor_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class EditorProperty : public Container {
StringName property;
String property_path;
String doc_path;
AcceptDialog *warning_dialog = nullptr;

int property_usage;

Expand All @@ -94,6 +95,8 @@ class EditorProperty : public Container {
bool check_hover = false;
Rect2 delete_rect;
bool delete_hover = false;
Rect2 configuration_warning_rect;
bool configuration_warning_hover = false;

bool can_revert = false;
bool can_pin = false;
Expand All @@ -117,12 +120,15 @@ class EditorProperty : public Container {
Control *bottom_editor = nullptr;
PopupMenu *menu = nullptr;

String configuration_warning;

HashMap<StringName, Variant> cache;

GDVIRTUAL0(_update_property)
GDVIRTUAL1(_set_read_only, bool)

void _update_pin_flags();
void _update_configuration_warnings();

protected:
void _notification(int p_what);
Expand Down Expand Up @@ -198,6 +204,9 @@ class EditorProperty : public Container {
void set_name_split_ratio(float p_ratio);
float get_name_split_ratio() const;

void set_configuration_warning(const String &p_configuration_warning);
String get_configuration_warning() const;

void set_object_and_property(Object *p_object, const StringName &p_property);
virtual Control *make_custom_tooltip(const String &p_text) const override;

Expand Down Expand Up @@ -519,6 +528,7 @@ class EditorInspector : public ScrollContainer {
void _object_id_selected(const String &p_path, ObjectID p_id);

void _node_removed(Node *p_node);
void _warning_changed(Node *p_node);

HashMap<StringName, int> per_array_page;
void _page_change_request(int p_new_page, const StringName &p_array_prefix);
Expand Down
9 changes: 1 addition & 8 deletions editor/gui/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,7 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
String conf_warning = p_node->get_configuration_warnings_as_string();
if (!conf_warning.is_empty()) {
const int num_warnings = p_node->get_configuration_warnings().size();
String warning_icon;
if (num_warnings == 1) {
warning_icon = SNAME("NodeWarning");
} else if (num_warnings <= 3) {
warning_icon = vformat("NodeWarnings%d", num_warnings);
} else {
warning_icon = SNAME("NodeWarnings4Plus");
}
const StringName warning_icon = Node::get_configuration_warning_icon(num_warnings);

// Improve looks on tooltip, extra spacing on non-bullet point newlines.
const String bullet_point = String::utf8("");
Expand Down
84 changes: 71 additions & 13 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3017,30 +3017,78 @@ void Node::clear_internal_tree_resource_paths() {
}
}

PackedStringArray Node::get_configuration_warnings() const {
ERR_THREAD_GUARD_V(PackedStringArray());
PackedStringArray ret;
Array Node::get_configuration_warnings() const {
ERR_THREAD_GUARD_V(Array());
Array warnings;
GDVIRTUAL_CALL(_get_configuration_warnings, warnings);
return warnings;
}

Vector<String> warnings;
if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
ret.append_array(warnings);
Dictionary Node::configuration_warning_to_dict(const Variant &p_warning) const {
switch (p_warning.get_type()) {
case Variant::Type::DICTIONARY:
return p_warning;
case Variant::Type::STRING: {
// Convert string to dictionary.
Dictionary warning;
warning["message"] = p_warning;
return warning;
}
default: {
ERR_FAIL_V_MSG(Dictionary(), "Node::get_configuration_warnings returned a value which is neither a string nor a dictionary, but a " + Variant::get_type_name(p_warning.get_type()));
}
}
}

TypedArray<Dictionary> Node::get_configuration_warnings_as_dicts() const {
TypedArray<Dictionary> ret;
Array mixed = get_configuration_warnings();
for (int i = 0; i < mixed.size(); i++) {
ret.append(configuration_warning_to_dict(mixed[i]));
}
return ret;
}

String Node::get_configuration_warnings_as_string() const {
PackedStringArray warnings = get_configuration_warnings();
String all_warnings;
TypedArray<Dictionary> Node::get_configuration_warnings_filtered(const String &p_property) const {
TypedArray<Dictionary> ret;
TypedArray<Dictionary> warnings = get_configuration_warnings_as_dicts();
if (p_property.is_empty()) {
ret.append_array(warnings);
} else {
// Filter by property path.
for (int i = 0; i < warnings.size(); i++) {
Dictionary warning = warnings[i];
String warning_property = warning.get("property", String());
if (p_property == warning_property) {
ret.append(warning);
}
}
}
return ret;
}

String Node::get_configuration_warnings_as_string(const String &p_property) const {
TypedArray<Dictionary> warnings = get_configuration_warnings_filtered(p_property);
Vector<String> warning_lines;
for (int i = 0; i < warnings.size(); i++) {
if (i > 0) {
all_warnings += "\n\n";
Dictionary warning = warnings[i];

String warning_text = warning.get("message", String());
if (warning_text.is_empty()) {
continue;
}

String warning_property = warning.get("property", String());
if (p_property.is_empty() && !warning_property.is_empty()) {
// When showing the complete list of configuration warnings, prepend the property path.
warning_text = "[" + warning_property + "] " + warning_text;
}

// Format as a bullet point list to make multiple warnings easier to distinguish
// from each other.
all_warnings += String::utf8("") + warnings[i];
warning_lines.append(String::utf8("") + warning_text);
}
return all_warnings;
return String("\n\n").join(warning_lines);
}

void Node::update_configuration_warnings() {
Expand Down Expand Up @@ -3510,6 +3558,16 @@ String Node::_get_name_num_separator() {
return " ";
}

StringName Node::get_configuration_warning_icon(int p_count) {
if (p_count == 1) {
return SNAME("NodeWarning");
} else if (p_count <= 3) {
return vformat("NodeWarnings%d", p_count);
} else {
return SNAME("NodeWarnings4Plus");
}
}

Node::Node() {
orphan_node_count++;
}
Expand Down
Loading

0 comments on commit 12ede25

Please sign in to comment.