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

[3.x] Allow pinning property values + Consistent property defaults #52234

Closed
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
186 changes: 186 additions & 0 deletions core/property_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*************************************************************************/
/* property_utils.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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_utils.h"

#include "core/engine.h"
#include "core/local_vector.h"
#include "editor/editor_node.h"
#include "scene/resources/packed_scene.h"

bool PropertyUtils::is_property_value_different(const Variant &p_a, const Variant &p_b) {
if (p_a.get_type() == Variant::REAL && p_b.get_type() == Variant::REAL) {
//this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
return !Math::is_equal_approx((float)p_a, (float)p_b);
} else {
return p_a != p_b;
}
}

Variant PropertyUtils::get_property_default_value(const Object *p_object, const StringName &p_property, const Node *p_owner, const Vector<SceneState::PackState> *p_states_stack_cache, bool *r_is_class_default) {
// This function obeys the way property values are set when an object is instantiated,
// which is the following (the latter wins):
// 1. Default value from builtin class
// 2. Default value from script exported variable (from the topmost script)
// 3. Value overrides from the instantiation/inheritance stack

if (r_is_class_default) {
*r_is_class_default = false;
}

Ref<Script> topmost_script;

if (const Node *node = Object::cast_to<Node>(p_object)) {
// Check inheritance/instantiation ancestors

const Node *owner = p_owner;
#ifdef TOOLS_ENABLED
if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
owner = EditorNode::get_singleton()->get_edited_scene();
}
#endif
ERR_FAIL_COND_V(!owner, Variant());

const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, owner);
for (int i = 0; i < states_stack.size(); ++i) {
const SceneState::PackState &ia = states_stack[i];
bool found = false;
Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found);
if (found) {
return value_in_ancestor;
}
// Save script for later
bool has_script = false;
Variant script = ia.state->get_property_value(ia.node, "script", has_script);
if (has_script) {
Ref<Script> scr = script;
if (scr.is_valid()) {
topmost_script = scr;
}
}
}
}

// Let's see what default is set by the topmost script having a default, if any
if (topmost_script.is_null()) {
topmost_script = p_object->get_script();
}
if (topmost_script.is_valid()) {
Variant default_value;
if (topmost_script->get_property_default_value(p_property, default_value)) {
return default_value;
}
}

// Fall back to the default from the native class
if (r_is_class_default) {
*r_is_class_default = true;
}
return ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property);
}

// Like SceneState::PackState, but using a raw pointer to avoid the cost of
// updating the reference count during the internal work of the functions below
namespace {
struct _FastPackState {
SceneState *state = nullptr;
int node = -1;
};
} // namespace

static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
bool found = false;

LocalVector<_FastPackState> inheritance_states;

Ref<SceneState> state = p_state;
while (state.is_valid()) {
int node = state->find_node_by_path(p_path);
if (node >= 0) {
// This one has state for this node
inheritance_states.push_back({ state.ptr(), node });
found = true;
}
state = state->get_base_scene_state();
}

for (int i = inheritance_states.size() - 1; i >= 0; --i) {
r_states_stack.push_back(inheritance_states[i]);
}

return found;
}

Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instanced_by_owner) {
if (r_instanced_by_owner) {
*r_instanced_by_owner = true;
}

LocalVector<_FastPackState> states_stack;
{
const Node *owner = p_owner;
#ifdef TOOLS_ENABLED
if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
owner = EditorNode::get_singleton()->get_edited_scene();
}
#endif

const Node *n = p_node;
while (n) {
if (n == owner) {
const Ref<SceneState> &state = n->get_scene_inherited_state();
if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
if (r_instanced_by_owner) {
*r_instanced_by_owner = false;
}
}
break;
} else if (n->get_filename() != String()) {
const Ref<SceneState> &state = n->get_scene_instance_state();
_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
}
n = n->get_owner();
}
}

// Convert to the proper type for returning, inverting the vector on the go
// (it was more convenient to fill the vector in reverse order)
Vector<SceneState::PackState> states_stack_ret;
{
states_stack_ret.resize(states_stack.size());
_FastPackState *ps = states_stack.ptr();
for (int i = states_stack.size() - 1; i >= 0; --i) {
states_stack_ret.write[i].state.reference_ptr(ps->state);
states_stack_ret.write[i].node = ps->node;
++ps;
}
}
return states_stack_ret;
}
51 changes: 51 additions & 0 deletions core/property_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*************************************************************************/
/* property_utils.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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_UTILS_H
#define PROPERTY_UTILS_H

#include "scene/main/node.h"
#include "scene/resources/packed_scene.h"

class PropertyUtils {
public:
static bool is_property_value_different(const Variant &p_a, const Variant &p_b);
// Gets the most pure default value, the one that would be set when the node has just been instantiated
static Variant get_property_default_value(const Object *p_object, const StringName &p_property, const Node *p_owner = nullptr, const Vector<SceneState::PackState> *p_states_stack_cache = nullptr, bool *r_is_class_default = nullptr);

// Gets the instance/inheritance states of this node, in order of precedence,
// that is, from the topmost (the most able to override values) to the lowermost
// (Note that in nested instancing the one with the greatest precedence is the furthest
// in the tree, since every owner found while traversing towards the root gets a chance
// to override property values.)
static Vector<SceneState::PackState> get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instanced_by_owner = nullptr);
};

#endif // PROPERTY_UTILS_H
9 changes: 8 additions & 1 deletion doc/classes/EditorProperty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
</signal>
<signal name="property_checked">
<argument index="0" name="property" type="String" />
<argument index="1" name="bool" type="String" />
<argument index="1" name="checked" type="bool" />
<description>
Emitted when a property was checked. Used internally.
</description>
Expand All @@ -120,6 +120,13 @@
Emit it if you want to key a property with a single value.
</description>
</signal>
<signal name="property_pin_changed">
<argument index="0" name="property" type="String" />
<argument index="1" name="pinned" type="bool" />
<description>
Emit it if you want to change the pinned state of a property.
</description>
</signal>
<signal name="resource_selected">
<argument index="0" name="path" type="String" />
<argument index="1" name="resource" type="Resource" />
Expand Down
4 changes: 4 additions & 0 deletions doc/classes/PackedScene.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@
If passed to [method instance], provides local scene resources to the local scene. Only the main scene should receive the main edit state.
[b]Note:[/b] Only available in editor builds.
</constant>
<constant name="GEN_EDIT_STATE_MAIN_INHERITED" value="3" enum="GenEditState">
It's similar to [code]GEN_EDIT_STATE_MAIN[/code], but for the case where the scene is being instantiated to be the base of another one.
[b]Note:[/b] Only available in editor builds.
</constant>
</constants>
</class>
4 changes: 4 additions & 0 deletions doc/classes/SceneState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,9 @@
If passed to [method PackedScene.instance], provides local scene resources to the local scene. Only the main scene should receive the main edit state.
[b]Note:[/b] Only available in editor builds.
</constant>
<constant name="GEN_EDIT_STATE_MAIN_INHERITED" value="3" enum="GenEditState">
If passed to [method PackedScene.instance], it's similar to [code]GEN_EDIT_STATE_MAIN[/code], but for the case where the scene is being instantiated to be the base of another one.
[b]Note:[/b] Only available in editor builds.
</constant>
</constants>
</class>
Loading