Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different icons for revertable instanced properties in the inspector #47782

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions editor/editor_folding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ void EditorFolding::_do_object_unfolds(Object *p_object, Set<RES> &resources) {
if (E->get().usage & PROPERTY_USAGE_EDITOR) {
if (group != "") { //group
if (group_base == String() || E->get().name.begins_with(group_base)) {
bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name).has_revert;
if (can_revert) {
unfold_group.insert(group);
}
}
} else { //path
int last = E->get().name.rfind("/");
if (last != -1) {
bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name).has_revert;
if (can_revert) {
unfold_group.insert(E->get().name.substr(0, last));
}
Expand Down
43 changes: 31 additions & 12 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,21 @@ void EditorProperty::_notification(int p_what) {
check_rect = Rect2();
}

if (can_revert) {
Ref<Texture2D> reload_icon = get_theme_icon("ReloadSmall", "EditorIcons");
if (can_revert || differs_from_class_default) {
Ref<Texture2D> reload_icon;
if (revert_from_instanced) {
reload_icon = get_theme_icon("ReloadSmallInstanced", "EditorIcons");
} else if (can_revert) {
reload_icon = get_theme_icon("ReloadSmall", "EditorIcons");
} else if (differs_from_class_default) {
reload_icon = get_theme_icon("PropertyModified", "EditorIcons");
}
text_limit -= reload_icon->get_width() + get_theme_constant("hseparator", "Tree") * 2;
revert_rect = Rect2(text_limit + get_theme_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height());

Color color2(1, 1, 1);
if (revert_hover) {
if (can_revert && revert_hover) {
// Only brighten the icon when hovered if the property can actually be reverted.
color2.r *= 1.2;
color2.g *= 1.2;
color2.b *= 1.2;
Expand Down Expand Up @@ -516,8 +524,8 @@ bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Varian
return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig));
}

bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) {
bool has_revert = false;
PropertyRevertStatus EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) {
PropertyRevertStatus status;

Node *node = Object::cast_to<Node>(p_object);

Expand All @@ -528,45 +536,56 @@ bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringNam
Variant v = p_object->get(p_property);

if (EditorPropertyRevert::is_node_property_different(node, v, vorig)) {
has_revert = true;
status.has_revert = true;
status.revert_from_instanced = true;
}
}

Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property);
if (default_value != Variant() && default_value != p_object->get(p_property)) {
status.differs_from_class_default = true;
}
} else {
//check for difference against default class value instead
Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property);
if (default_value != Variant() && default_value != p_object->get(p_property)) {
has_revert = true;
status.has_revert = true;
}
}

// If the object implements property_can_revert, rely on that completely
// (i.e. don't then try to revert to default value - the property_get_revert implementation
// can do that if so desired)
if (p_object->has_method("property_can_revert")) {
has_revert = p_object->call("property_can_revert", p_property).operator bool();
status.has_revert = p_object->call("property_can_revert", p_property).operator bool();
status.differs_from_class_default = status.has_revert;
} else {
if (!has_revert && !p_object->get_script().is_null()) {
if (!status.has_revert && !p_object->get_script().is_null()) {
Ref<Script> scr = p_object->get_script();
if (scr.is_valid()) {
Variant orig_value;
if (scr->get_property_default_value(p_property, orig_value)) {
if (orig_value != p_object->get(p_property)) {
has_revert = true;
status.has_revert = true;
status.differs_from_class_default = true;
}
}
}
}
}

return has_revert;
return status;
}

void EditorProperty::update_reload_status() {
if (property == StringName()) {
return; //no property, so nothing to do
}

bool has_reload = EditorPropertyRevert::can_property_revert(object, property);
const PropertyRevertStatus status = EditorPropertyRevert::can_property_revert(object, property);
const bool has_reload = status.has_revert;
revert_from_instanced = EditorPropertyRevert::can_property_revert(object, property).revert_from_instanced;
differs_from_class_default = EditorPropertyRevert::can_property_revert(object, property).differs_from_class_default;

if (has_reload != can_revert) {
can_revert = has_reload;
Expand Down
11 changes: 10 additions & 1 deletion editor/editor_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@

class UndoRedo;

struct PropertyRevertStatus {
bool has_revert = false;
// If `true`, we are reverting to an instanced scene's value instead of the class' default value.
bool revert_from_instanced = false;
bool differs_from_class_default = false;
};

class EditorPropertyRevert {
public:
static bool may_node_be_in_instance(Node *p_node);
static bool get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value);
static bool is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig);

static bool can_property_revert(Object *p_object, const StringName &p_property);
static PropertyRevertStatus can_property_revert(Object *p_object, const StringName &p_property);
};

class EditorProperty : public Container {
Expand Down Expand Up @@ -78,6 +85,8 @@ class EditorProperty : public Container {
bool delete_hover = false;

bool can_revert;
bool revert_from_instanced = false;
bool differs_from_class_default = false;

bool use_folding;
bool draw_top_bg;
Expand Down
1 change: 1 addition & 0 deletions editor/icons/PropertyModified.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ReloadSmallInstanced.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.