Skip to content

Commit

Permalink
Add editor_only property for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
gaudecker committed Mar 20, 2022
1 parent 5901d91 commit daaf711
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 91 deletions.
31 changes: 1 addition & 30 deletions scene/2d/light_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,7 @@ void Light2D::_update_light_visibility() {
return;
}

bool editor_ok = true;

#ifdef TOOLS_ENABLED
if (editor_only) {
if (!Engine::get_singleton()->is_editor_hint()) {
editor_ok = false;
} else {
editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
}
}
#else
if (editor_only) {
editor_ok = false;
}
#endif

RS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree() && editor_ok);
RS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree());
}

void Light2D::set_enabled(bool p_enabled) {
Expand All @@ -63,15 +47,6 @@ bool Light2D::is_enabled() const {
return enabled;
}

void Light2D::set_editor_only(bool p_editor_only) {
editor_only = p_editor_only;
_update_light_visibility();
}

bool Light2D::is_editor_only() const {
return editor_only;
}

void Light2D::set_color(const Color &p_color) {
color = p_color;
RS::get_singleton()->canvas_light_set_color(canvas_light, color);
Expand Down Expand Up @@ -232,9 +207,6 @@ void Light2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &Light2D::is_enabled);

ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
ClassDB::bind_method(D_METHOD("is_editor_only"), &Light2D::is_editor_only);

ClassDB::bind_method(D_METHOD("set_color", "color"), &Light2D::set_color);
ClassDB::bind_method(D_METHOD("get_color"), &Light2D::get_color);

Expand Down Expand Up @@ -278,7 +250,6 @@ void Light2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_height"), &Light2D::get_height);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_energy", "get_energy");
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Add,Subtract,Mix"), "set_blend_mode", "get_blend_mode");
Expand Down
4 changes: 0 additions & 4 deletions scene/2d/light_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class Light2D : public Node2D {
private:
RID canvas_light;
bool enabled = true;
bool editor_only = false;
bool shadow = false;
Color color = Color(1, 1, 1);
Color shadow_color = Color(0, 0, 0, 0);
Expand Down Expand Up @@ -83,9 +82,6 @@ class Light2D : public Node2D {
void set_enabled(bool p_enabled);
bool is_enabled() const;

void set_editor_only(bool p_editor_only);
bool is_editor_only() const;

void set_color(const Color &p_color);
Color get_color() const;

Expand Down
32 changes: 1 addition & 31 deletions scene/3d/light_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,7 @@ void Light3D::_update_visibility() {
return;
}

bool editor_ok = true;

#ifdef TOOLS_ENABLED
if (editor_only) {
if (!Engine::get_singleton()->is_editor_hint()) {
editor_ok = false;
} else {
editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
}
}
#else
if (editor_only) {
editor_ok = false;
}
#endif

RS::get_singleton()->instance_set_visible(get_instance(), is_visible_in_tree() && editor_ok);
RS::get_singleton()->instance_set_visible(get_instance(), is_visible_in_tree());
}

void Light3D::_notification(int p_what) {
Expand All @@ -209,15 +193,6 @@ void Light3D::_notification(int p_what) {
}
}

void Light3D::set_editor_only(bool p_editor_only) {
editor_only = p_editor_only;
_update_visibility();
}

bool Light3D::is_editor_only() const {
return editor_only;
}

void Light3D::_validate_property(PropertyInfo &property) const {
if (!shadow && (property.name == "shadow_bias" || property.name == "shadow_normal_bias" || property.name == "shadow_reverse_cull_face" || property.name == "shadow_transmittance_bias" || property.name == "shadow_fog_fade" || property.name == "shadow_blur" || property.name == "distance_fade_shadow")) {
property.usage = PROPERTY_USAGE_NO_EDITOR;
Expand All @@ -236,9 +211,6 @@ void Light3D::_validate_property(PropertyInfo &property) const {
}

void Light3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light3D::set_editor_only);
ClassDB::bind_method(D_METHOD("is_editor_only"), &Light3D::is_editor_only);

ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &Light3D::set_param);
ClassDB::bind_method(D_METHOD("get_param", "param"), &Light3D::get_param);

Expand Down Expand Up @@ -299,8 +271,6 @@ void Light3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_begin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater"), "set_distance_fade_begin", "get_distance_fade_begin");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_shadow", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater"), "set_distance_fade_shadow", "get_distance_fade_shadow");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_length", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater"), "set_distance_fade_length", "get_distance_fade_length");
ADD_GROUP("Editor", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
ADD_GROUP("", "");

BIND_ENUM_CONSTANT(PARAM_ENERGY);
Expand Down
4 changes: 0 additions & 4 deletions scene/3d/light_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class Light3D : public VisualInstance3D {
real_t distance_fade_shadow = 50.0;
real_t distance_fade_length = 10.0;
RS::LightType type = RenderingServer::LIGHT_DIRECTIONAL;
bool editor_only = false;
void _update_visibility();
BakeMode bake_mode = BAKE_DYNAMIC;
Ref<Texture2D> projector;
Expand All @@ -98,9 +97,6 @@ class Light3D : public VisualInstance3D {
public:
RS::LightType get_light_type() const { return type; }

void set_editor_only(bool p_editor_only);
bool is_editor_only() const;

void set_param(Param p_param, real_t p_value);
real_t get_param(Param p_param) const;

Expand Down
15 changes: 1 addition & 14 deletions scene/gui/reference_rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void ReferenceRect::_notification(int p_what) {
if (!is_inside_tree()) {
return;
}
if (Engine::get_singleton()->is_editor_hint() || !editor_only) {
if (Engine::get_singleton()->is_editor_hint()) {
draw_rect(Rect2(Point2(), get_size()), border_color, false, border_width);
}
} break;
Expand All @@ -63,26 +63,13 @@ float ReferenceRect::get_border_width() const {
return border_width;
}

void ReferenceRect::set_editor_only(const bool &p_enabled) {
editor_only = p_enabled;
update();
}

bool ReferenceRect::get_editor_only() const {
return editor_only;
}

void ReferenceRect::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_border_color"), &ReferenceRect::get_border_color);
ClassDB::bind_method(D_METHOD("set_border_color", "color"), &ReferenceRect::set_border_color);

ClassDB::bind_method(D_METHOD("get_border_width"), &ReferenceRect::get_border_width);
ClassDB::bind_method(D_METHOD("set_border_width", "width"), &ReferenceRect::set_border_width);

ClassDB::bind_method(D_METHOD("get_editor_only"), &ReferenceRect::get_editor_only);
ClassDB::bind_method(D_METHOD("set_editor_only", "enabled"), &ReferenceRect::set_editor_only);

ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "border_width", PROPERTY_HINT_RANGE, "0.0,5.0,0.1,or_greater"), "set_border_width", "get_border_width");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "get_editor_only");
}
4 changes: 0 additions & 4 deletions scene/gui/reference_rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class ReferenceRect : public Control {

Color border_color = Color(1, 0, 0);
float border_width = 1.0;
bool editor_only = true;

protected:
void _notification(int p_what);
Expand All @@ -50,9 +49,6 @@ class ReferenceRect : public Control {

void set_border_width(float p_width);
float get_border_width() const;

void set_editor_only(const bool &p_enabled);
bool get_editor_only() const;
};

#endif // REFERENCE_RECT_H
18 changes: 15 additions & 3 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,14 @@ String Node::get_editor_description() const {
return data.editor_description;
}

void Node::set_editor_only(bool p_enable) {
data.editor_only = p_enable;
}

bool Node::is_editor_only() const {
return data.editor_only;
}

void Node::set_editable_instance(Node *p_node, bool p_editable) {
ERR_FAIL_NULL(p_node);
ERR_FAIL_COND(!is_ancestor_of(p_node));
Expand Down Expand Up @@ -2744,6 +2752,9 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);

ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Node::set_editor_only);
ClassDB::bind_method(D_METHOD("is_editor_only"), &Node::is_editor_only);

ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);

Expand Down Expand Up @@ -2842,13 +2853,14 @@ void Node::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "", "get_multiplayer");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "set_custom_multiplayer", "get_custom_multiplayer");

ADD_GROUP("Editor", "");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");

ADD_GROUP("Process", "process_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");

ADD_GROUP("Editor Description", "editor_");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");

GDVIRTUAL_BIND(_process, "delta");
GDVIRTUAL_BIND(_physics_process, "delta");
GDVIRTUAL_BIND(_enter_tree);
Expand Down
4 changes: 4 additions & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Node : public Object {
NodePath import_path; // Path used when imported, used by scene editors to keep tracking.
#endif
String editor_description;
bool editor_only = false;

Viewport *viewport = nullptr;

Expand Down Expand Up @@ -356,6 +357,9 @@ class Node : public Object {
void set_editor_description(const String &p_editor_description);
String get_editor_description() const;

void set_editor_only(bool p_enable);
bool is_editor_only() const;

void set_editable_instance(Node *p_node, bool p_editable);
bool is_editable_instance(const Node *p_node) const;
Node *get_deepest_editable_node(Node *p_start_node) const;
Expand Down
13 changes: 12 additions & 1 deletion scene/resources/packed_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
int nprop_count = n.properties.size();
if (nprop_count) {
const NodeData::Property *nprops = &n.properties[0];

bool is_editor_only = false;

for (int j = 0; j < nprop_count; j++) {
bool valid;
ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);
Expand All @@ -239,6 +240,11 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
} else {
Variant value = props[nprops[j].value];

if (snames[nprops[j].name] == StringName("editor_only") && value.get_type() == Variant::BOOL && value.operator bool() == true && !Engine::get_singleton()->is_editor_hint()) {
is_editor_only = true;
break;
}

if (value.get_type() == Variant::OBJECT) {
//handle resources that are local to scene by duplicating them if needed
Ref<Resource> res = value;
Expand Down Expand Up @@ -274,6 +280,11 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
node->set(snames[nprops[j].name], value, &valid);
}
}

if (is_editor_only) {
stray_instances.push_back(node); //editor only, go to stray list
continue;
}
}

//name
Expand Down

0 comments on commit daaf711

Please sign in to comment.