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

Add button to toggle Unique Name when renaming Node #64609

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
71 changes: 69 additions & 2 deletions editor/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_i
undo_redo->add_do_method(this, "_update_tree");
undo_redo->add_undo_method(this, "_update_tree");
undo_redo->commit_action();
} else if (p_id == BUTTON_UNIQUE_TOGGLE) {
bool unique = n->is_unique_name_in_owner();
undo_redo->create_action(TTR("Toggle Scene Unique Name"));
undo_redo->add_do_method(n, "set_unique_name_in_owner", !unique);
undo_redo->add_undo_method(n, "set_unique_name_in_owner", unique);
undo_redo->add_do_method(this, "_update_tree");
undo_redo->add_undo_method(this, "_update_tree");
undo_redo->commit_action();

int button_index = item->get_button_by_id(0, BUTTON_UNIQUE_TOGGLE);
if (button_index == -1) {
return;
}

if (!unique) {
item->set_button_color(0, button_index, Color(1, 0.5, 0.1));
WARN_PRINT("BUTTON PRESSED, UNIQUE ON");
} else {
WARN_PRINT("BUTTON PRESSED, UNIQUE OFF");
if (tree->is_editing()) {
item->set_button_color(0, button_index, Color(1, 1, 1));
} /*else {
item->erase_button(0, button_index);
WARN_PRINT("POOF");
}*/
}
}
}

Expand Down Expand Up @@ -282,7 +308,9 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
}

if (p_node->is_unique_name_in_owner()) {
item->add_button(0, get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons")), BUTTON_UNIQUE, false, vformat(TTR("This node can be accessed from within anywhere in the scene by preceding it with the '%s' prefix in a node path.\nClick to disable this."), UNIQUE_NODE_PREFIX));
String tooltip = vformat(TTR("This node can be accessed from within anywhere in the scene by preceding it with the '%s' prefix in a node path.\nClick to disable this."), UNIQUE_NODE_PREFIX);
item->add_button(0, get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons")), BUTTON_UNIQUE_TOGGLE, false, tooltip);
item->set_button_color(0, item->get_button_by_id(0, BUTTON_UNIQUE_TOGGLE), Color(1, 0.5, 0.1));
}

int num_connections = p_node->get_persistent_signal_connection_count();
Expand Down Expand Up @@ -839,6 +867,42 @@ void SceneTreeEditor::_rename_node(ObjectID p_node, const String &p_name) {
item->set_text(0, p_name);
}

void SceneTreeEditor::_begin_rename() {
TreeItem *which = tree->get_selected();

ERR_FAIL_COND(!which);
NodePath nodepath = which->get_metadata(0);
Node *edited_node = get_node(nodepath);
ERR_FAIL_COND(!edited_node);

renaming_item = which;

Ref<Texture2D> icon = get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons"));
String tooltip = "Potato";
if (!edited_node->is_unique_name_in_owner()) {
renaming_item->add_button(0, icon, BUTTON_UNIQUE_TOGGLE, false, tooltip);
WARN_PRINT("Add Unique Toggle");
}
//WARN_PRINT("Begin Rename");
}

void SceneTreeEditor::_end_rename() {
if (renaming_item) {
NodePath nodepath = renaming_item->get_metadata(0);
Node *edited_node = get_node(nodepath);
ERR_FAIL_COND(!edited_node);

// Hide Scene Unique toggle.
if (!edited_node->is_unique_name_in_owner()) {
int button_index = renaming_item->get_button_by_id(0, BUTTON_UNIQUE_TOGGLE);
if (button_index != -1) {
renaming_item->erase_button(0, button_index);
WARN_PRINT("Remove Unique Toggle");
}
}
}
}

void SceneTreeEditor::_renamed() {
TreeItem *which = tree->get_edited();

Expand All @@ -847,7 +911,7 @@ void SceneTreeEditor::_renamed() {
Node *n = get_node(np);
ERR_FAIL_COND(!n);

// Empty node names are not allowed, so resets it to previous text and show warning
// Empty node names are not allowed, so resets it to previous text and show warning.
if (which->get_text(0).strip_edges().is_empty()) {
which->set_text(0, n->get_name());
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
Expand Down Expand Up @@ -1297,6 +1361,9 @@ SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_ope
tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));

tree->get_text_editor()->connect("focus_entered", callable_mp(this, &SceneTreeEditor::_begin_rename));
tree->get_text_editor()->connect("focus_exited", callable_mp(this, &SceneTreeEditor::_end_rename));

error = memnew(AcceptDialog);
add_child(error);

Expand Down
4 changes: 4 additions & 0 deletions editor/scene_tree_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class SceneTreeEditor : public Control {
BUTTON_GROUPS = 7,
BUTTON_PIN = 8,
BUTTON_UNIQUE = 9,
BUTTON_UNIQUE_TOGGLE = 10,
};

Tree *tree = nullptr;
Expand Down Expand Up @@ -97,6 +98,9 @@ class SceneTreeEditor : public Control {
bool updating_tree = false;
bool show_enabled_subscene = false;

TreeItem *renaming_item;
void _begin_rename();
void _end_rename();
void _renamed();
UndoRedo *undo_redo = nullptr;

Expand Down
84 changes: 80 additions & 4 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,10 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2

p_item->set_meta("__focus_rect", Rect2(r.position, r.size));

if (popup_edited_item && popup_edited_item_col >= 0 && popup_edited_item_col <= columns.size()) {
_update_popup_editor_size();
}

if (select_mode != SELECT_ROW) {
if (rtl) {
r.position.x = get_size().width - r.position.x - r.size.x;
Expand Down Expand Up @@ -2759,7 +2763,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int
return item_h; // nothing found
}

void Tree::_text_editor_modal_close() {
void Tree::_text_editor_modal_close_requested() {
if (Input::get_singleton()->is_key_pressed(Key::ESCAPE) ||
Input::get_singleton()->is_key_pressed(Key::KP_ENTER) ||
Input::get_singleton()->is_key_pressed(Key::ENTER)) {
Expand All @@ -2770,11 +2774,43 @@ void Tree::_text_editor_modal_close() {
return;
}

// Don't close when clicking cell buttons.
if (popup_edited_item && popup_edited_item_col >= 0 && popup_edited_item_col < popup_edited_item->cells.size()) {
TreeItem *item = popup_edited_item;
int item_column = popup_edited_item_col;

Rect2 focus_rect = item->get_meta("__focus_rect");
Vector2 mouse_position = get_local_mouse_position();

focus_rect.size.x = get_column_width(item_column);

//Rect2 button_rect = Rect2(0, 0, 240, 240);
if (focus_rect.has_point(mouse_position)) {
//WARN_PRINT("Point found");

Ref<InputEventMouseButton> mouse_button_event(memnew(InputEventMouseButton));

mouse_button_event->set_global_position(get_global_mouse_position());
mouse_button_event->set_position(get_local_mouse_position());
mouse_button_event->set_button_index(MouseButton::LEFT);
mouse_button_event->set_pressed(true);
gui_input(mouse_button_event);

mouse_button_event->set_pressed(false);
gui_input(mouse_button_event);

return;
}
}

//WARN_PRINT("Hide the popup");
popup_editor->hide();

_text_editor_submit(text_editor->get_text());
}

void Tree::_text_editor_submit(String p_text) {
popup_editor->hide();
//popup_editor->hide();

if (!popup_edited_item) {
return;
Expand Down Expand Up @@ -3621,6 +3657,9 @@ bool Tree::edit_selected() {
popup_editor->popup();
popup_editor->child_controls_changed();

//update();
//popup_editor->call_deferred("_update_popup_editor_size");

text_editor->grab_focus();

return true;
Expand All @@ -3629,6 +3668,31 @@ bool Tree::edit_selected() {
return false;
}

void Tree::_update_popup_editor_size() {
TreeItem *selected_item = popup_edited_item;
ERR_FAIL_COND_MSG(!selected_item, "Can't update size with no item selected.");
int selected_column = popup_edited_item_col;
ERR_FAIL_INDEX_MSG(selected_column, columns.size(), "Can't update size with no column selected.");

const TreeItem::Cell cell = selected_item->cells[selected_column];

Rect2i focus_rect = selected_item->get_meta("__focus_rect");

if (cell.mode == TreeItem::CELL_MODE_STRING || cell.mode == TreeItem::CELL_MODE_RANGE) {
Rect2 popup_rect;

Vector2 offset(0, (text_editor->get_size().height - focus_rect.size.height) / 2);

Point2i textedpos = get_screen_position() + focus_rect.position - offset;
cache.text_editor_position = textedpos;
popup_rect.position = textedpos;
popup_rect.size = focus_rect.size;

popup_editor->set_position(popup_rect.position);
popup_editor->set_size(popup_rect.size);
}
}

bool Tree::is_editing() {
return popup_editor->is_visible();
}
Expand Down Expand Up @@ -4139,6 +4203,10 @@ int Tree::get_edited_column() const {
return edited_col;
}

LineEdit *Tree::get_text_editor() const {
return text_editor;
}

TreeItem *Tree::get_next_selected(TreeItem *p_item) {
if (!root) {
return nullptr;
Expand Down Expand Up @@ -5018,8 +5086,16 @@ Tree::Tree() {
popup_menu->hide();
add_child(popup_menu, false, INTERNAL_MODE_FRONT);

popup_editor = memnew(Popup);
popup_editor = memnew(Window);
popup_editor->set_wrap_controls(true);

popup_editor->set_wrap_controls(true);
popup_editor->set_visible(false);
popup_editor->set_transient(true);
popup_editor->set_flag(Window::FLAG_BORDERLESS, true);
popup_editor->set_flag(Window::FLAG_RESIZE_DISABLED, true);
popup_editor->set_flag(Window::FLAG_POPUP, true);

add_child(popup_editor, false, INTERNAL_MODE_FRONT);
popup_editor_vb = memnew(VBoxContainer);
popup_editor->add_child(popup_editor_vb);
Expand Down Expand Up @@ -5048,7 +5124,7 @@ Tree::Tree() {
h_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
v_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
text_editor->connect("text_submitted", callable_mp(this, &Tree::_text_editor_submit));
popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_modal_close));
popup_editor->connect("close_requested", callable_mp(this, &Tree::_text_editor_modal_close_requested));
popup_menu->connect("id_pressed", callable_mp(this, &Tree::popup_select));
value_editor->connect("value_changed", callable_mp(this, &Tree::value_editor_changed));

Expand Down
6 changes: 4 additions & 2 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class Tree : public Control {

VBoxContainer *popup_editor_vb = nullptr;

Popup *popup_editor = nullptr;
Window *popup_editor = nullptr;
LineEdit *text_editor = nullptr;
HSlider *value_editor = nullptr;
bool updating_value_editor = false;
Expand All @@ -461,7 +461,7 @@ class Tree : public Control {
void select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev = nullptr, bool *r_in_range = nullptr, bool p_force_deselect = false);
int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod);
void _text_editor_submit(String p_text);
void _text_editor_modal_close();
void _text_editor_modal_close_requested();
void value_editor_changed(double p_value);

void popup_select(int p_option);
Expand Down Expand Up @@ -670,6 +670,7 @@ class Tree : public Control {

TreeItem *get_edited() const;
int get_edited_column() const;
LineEdit *get_text_editor() const;

void ensure_cursor_is_visible();

Expand All @@ -678,6 +679,7 @@ class Tree : public Control {
int get_item_offset(TreeItem *p_item) const;
Rect2 get_item_rect(TreeItem *p_item, int p_column = -1, int p_button = -1) const;
bool edit_selected();
void _update_popup_editor_size();
bool is_editing();

// First item that starts with the text, from the current focused item down and wraps around.
Expand Down