Skip to content

Commit

Permalink
node existence toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
torcado194 committed Jun 17, 2024
1 parent a4f2ea9 commit 6a723fd
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5951,6 +5951,7 @@ void EditorNode::reload_instances_with_path_in_edited_scenes(const String &p_ins

bool original_node_is_displayed_folded = original_node->is_displayed_folded();
bool original_node_scene_instance_load_placeholder = original_node->get_scene_instance_load_placeholder();
bool original_node_scene_editor_only = original_node->get_scene_editor_only();

// Update the name to match
instantiated_node->set_name(original_node->get_name());
Expand Down Expand Up @@ -5978,6 +5979,7 @@ void EditorNode::reload_instances_with_path_in_edited_scenes(const String &p_ins
// Restore the folded and placeholder state from the original node.
instantiated_node->set_display_folded(original_node_is_displayed_folded);
instantiated_node->set_scene_instance_load_placeholder(original_node_scene_instance_load_placeholder);
instantiated_node->set_scene_editor_only(original_node_scene_editor_only);

if (owner) {
Ref<SceneState> ss_inst = owner->get_scene_instance_state();
Expand Down
15 changes: 15 additions & 0 deletions editor/gui/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
}
}

Node *node = p_node;
bool editor_only = false;
while (node) {
if (node->get_scene_editor_only()) {
editor_only = true;
break;
}
node = node->get_parent();
}
if (p_node->get_scene_editor_only()) {
_set_item_custom_color(item, Color(0.28, 0.6, 1.0));
} else if (editor_only) {
_set_item_custom_color(item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
}

if (can_rename) { //should be can edit..

const PackedStringArray warnings = p_node->get_configuration_warnings();
Expand Down
1 change: 1 addition & 0 deletions editor/gui/scene_tree_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class SceneTreeEditor : public Control {

void _cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);
void _toggle_visible(Node *p_node);
void _toggle_disabled(Node *p_node);
void _cell_multi_selected(Object *p_object, int p_cell, bool p_selected);
void _update_selection(TreeItem *item);
void _node_script_changed(Node *p_node);
Expand Down
3 changes: 3 additions & 0 deletions editor/icons/GuiExistenceDisabled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions editor/icons/GuiExistenceEnabled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions editor/scene_tree_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ void SceneTreeDock::shortcut_input(const Ref<InputEvent> &p_event) {
_tool_selected(TOOL_REPARENT_TO_NEW_NODE);
} else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
_tool_selected(TOOL_NEW_SCENE_FROM);
} else if (ED_IS_SHORTCUT("scene_tree/editor_only", p_event)) {
_tool_selected(TOOL_EDITOR_ONLY);
} else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
_tool_selected(TOOL_ERASE, true);
} else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
Expand Down Expand Up @@ -538,6 +540,20 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
break;
}
[[fallthrough]];
case TOOL_EDITOR_ONLY: {
List<Node *> selection = editor_selection->get_selected_node_list();
if (selection.size() == 0) {
break;
}

for (Node *node : selection) {
node->set_scene_editor_only(!node->get_scene_editor_only());
scene_tree->_update_tree();
emit_signal("node_changed");
}

scene_tree->update_tree();
} break;
case TOOL_NEW: {
if (!profile_allow_editing) {
break;
Expand Down Expand Up @@ -3553,6 +3569,8 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
}
}

menu->add_icon_shortcut(get_theme_icon(SNAME("GuiExistenceDisabled"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/editor_only"), TOOL_EDITOR_ONLY);

if (profile_allow_editing) {
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Rename")), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);

Expand Down Expand Up @@ -4366,6 +4384,7 @@ SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selec
ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node..."));
ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene..."));
ED_SHORTCUT("scene_tree/editor_only", TTR("Toggle Editor-Only"));
ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
ED_SHORTCUT("scene_tree/show_in_file_system", TTR("Show in FileSystem"));
ED_SHORTCUT("scene_tree/toggle_unique_name", TTR("Toggle Access as Unique Name"));
Expand Down
1 change: 1 addition & 0 deletions editor/scene_tree_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class SceneTreeDock : public VBoxContainer {
TOOL_CREATE_USER_INTERFACE,
TOOL_CREATE_FAVORITE,
TOOL_CENTER_PARENT,
TOOL_EDITOR_ONLY

};

Expand Down
8 changes: 8 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,14 @@ bool Node::get_scene_instance_load_placeholder() const {
return data.use_placeholder;
}

void Node::set_scene_editor_only(bool p_enable) {
set_meta("_editor_only_", p_enable);
}

bool Node::get_scene_editor_only() const {
return get_meta("_editor_only_", false);
}

Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) const {
ERR_THREAD_GUARD_V(nullptr);
Node *node = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ class Node : public Object {
void set_scene_instance_load_placeholder(bool p_enable);
bool get_scene_instance_load_placeholder() const;

void set_scene_editor_only(bool p_enable);
bool get_scene_editor_only() const;

template <typename... VarArgs>
Vector<Variant> make_binds(VarArgs... p_args) {
Vector<Variant> binds = { p_args... };
Expand Down
31 changes: 30 additions & 1 deletion scene/resources/packed_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,19 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
MissingNode *missing_node = nullptr;
bool is_inherited_scene = false;

if (i == 0 && base_scene_idx >= 0) {
bool editor_only = false;
if (!Engine::get_singleton()->is_editor_hint()) {
editor_only = n.editor_only > 0;
if (parent) {
if (parent->get_scene_editor_only()) {
editor_only = true;
}
}
}
if (editor_only) {
node = Object::cast_to<Node>(memnew(Node));
node->set_scene_editor_only(true);
} else if (i == 0 && base_scene_idx >= 0) {
// Scene inheritance on root node.
Ref<PackedScene> sdata = props[base_scene_idx];
ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
Expand Down Expand Up @@ -300,6 +312,8 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
// may not have found the node (part of instantiated scene and removed)
// if found all is good, otherwise ignore

node->set_scene_editor_only(editor_only || n.editor_only > 0);

//properties
int nprop_count = n.properties.size();
if (nprop_count) {
Expand Down Expand Up @@ -337,6 +351,15 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {

ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);

if (!Engine::get_singleton()->is_editor_hint()) {
if (snames[nprops[j].name] == "metadata/_editor_only_" && props[nprops[j].value]) {
editor_only = true;
node = Object::cast_to<Node>(memnew(Node));
node->set_scene_editor_only(true);
break;
}
}

if (snames[nprops[j].name] == CoreStringName(script)) {
//work around to avoid old script variables from disappearing, should be the proper fix to:
//https://github.com/godotengine/godot/issues/2958
Expand Down Expand Up @@ -898,6 +921,8 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
nd.type = TYPE_INSTANTIATED;
}

nd.editor_only = p_node->get_scene_editor_only();

// determine whether to save this node or not
// if this node is part of an instantiated sub-scene, we can skip storing it if basically
// no properties changed and no groups were added to it.
Expand Down Expand Up @@ -1677,6 +1702,10 @@ String SceneState::get_node_instance_placeholder(int p_idx) const {
return String();
}

bool SceneState::get_node_editor_only(int p_idx) const {
return nodes[p_idx].editor_only > 0;
}

Vector<StringName> SceneState::get_node_groups(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, nodes.size(), Vector<StringName>());
Vector<StringName> groups;
Expand Down
2 changes: 2 additions & 0 deletions scene/resources/packed_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SceneState : public RefCounted {
int name = 0;
int instance = 0;
int index = 0;
int editor_only = 0;

struct Property {
int name = 0;
Expand Down Expand Up @@ -175,6 +176,7 @@ class SceneState : public RefCounted {
Ref<PackedScene> get_node_instance(int p_idx) const;
String get_node_instance_placeholder(int p_idx) const;
bool is_node_instance_placeholder(int p_idx) const;
bool get_node_editor_only(int p_idx) const;
Vector<StringName> get_node_groups(int p_idx) const;
int get_node_index(int p_idx) const;

Expand Down

0 comments on commit 6a723fd

Please sign in to comment.