Skip to content

Commit

Permalink
Improve 2D guide metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Nov 28, 2024
1 parent 9aed9ec commit 06386d5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
81 changes: 42 additions & 39 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@ Point2 CanvasItemEditor::snap_point(Point2 p_target, unsigned int p_modes, unsig
if (((is_snap_active && snap_guides && (p_modes & SNAP_GUIDES)) || (p_forced_modes & SNAP_GUIDES)) && fmod(rotation, (real_t)360.0) == 0.0) {
// Guides.
if (Node *scene = EditorNode::get_singleton()->get_edited_scene()) {
Array vguides = scene->get_meta("_edit_vertical_guides_", Array());
Array vguides = scene->get_meta(vertical_guides_meta, Array());
for (int i = 0; i < vguides.size(); i++) {
_snap_if_closer_float(p_target.x, output.x, snap_target[0], vguides[i], SNAP_TARGET_GUIDE);
}

Array hguides = scene->get_meta("_edit_horizontal_guides_", Array());
Array hguides = scene->get_meta(horizontal_guides_meta, Array());
for (int i = 0; i < hguides.size(); i++) {
_snap_if_closer_float(p_target.y, output.y, snap_target[1], hguides[i], SNAP_TARGET_GUIDE);
}
Expand Down Expand Up @@ -1071,8 +1071,8 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
if (show_guides && show_rulers && EditorNode::get_singleton()->get_edited_scene()) {
Transform2D xform = viewport_scrollable->get_transform() * transform;
// Retrieve the guide lists
Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_", Array());
Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_", Array());
Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta(vertical_guides_meta, Array());
Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta(horizontal_guides_meta, Array());

// Hover over guides
real_t minimum = 1e20;
Expand Down Expand Up @@ -1165,8 +1165,8 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
Transform2D xform = viewport_scrollable->get_transform() * transform;

// Retrieve the guide lists
Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_", Array());
Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_", Array());
Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta(vertical_guides_meta, Array());
Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta(horizontal_guides_meta, Array());

Point2 edited = snap_point(xform.affine_inverse().xform(b->get_position()), SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES);
if (drag_type == DRAG_V_GUIDE) {
Expand All @@ -1176,18 +1176,18 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
if (dragged_guide_index >= 0) {
vguides[dragged_guide_index] = edited.x;
undo_redo->create_action(TTR("Move Vertical Guide"));
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, vguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, prev_vguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
} else {
vguides.push_back(edited.x);
undo_redo->create_action(TTR("Create Vertical Guide"));
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, vguides);
if (prev_vguides.is_empty()) {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", vertical_guides_meta);
} else {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, prev_vguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
Expand All @@ -1197,11 +1197,11 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
vguides.remove_at(dragged_guide_index);
undo_redo->create_action(TTR("Remove Vertical Guide"));
if (vguides.is_empty()) {
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", vertical_guides_meta);
} else {
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, vguides);
}
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, prev_vguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
}
Expand All @@ -1213,18 +1213,18 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
if (dragged_guide_index >= 0) {
hguides[dragged_guide_index] = edited.y;
undo_redo->create_action(TTR("Move Horizontal Guide"));
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, hguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, prev_hguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
} else {
hguides.push_back(edited.y);
undo_redo->create_action(TTR("Create Horizontal Guide"));
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, hguides);
if (prev_hguides.is_empty()) {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", horizontal_guides_meta);
} else {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, prev_hguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
Expand All @@ -1234,11 +1234,11 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
hguides.remove_at(dragged_guide_index);
undo_redo->create_action(TTR("Remove Horizontal Guide"));
if (hguides.is_empty()) {
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", horizontal_guides_meta);
} else {
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, hguides);
}
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, prev_hguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
}
Expand All @@ -1251,17 +1251,17 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
vguides.push_back(edited.x);
hguides.push_back(edited.y);
undo_redo->create_action(TTR("Create Horizontal and Vertical Guides"));
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, vguides);
undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, hguides);
if (prev_vguides.is_empty()) {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", vertical_guides_meta);
} else {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", vertical_guides_meta, prev_vguides);
}
if (prev_hguides.is_empty()) {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", horizontal_guides_meta);
} else {
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", horizontal_guides_meta, prev_hguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
Expand Down Expand Up @@ -2916,7 +2916,7 @@ void CanvasItemEditor::_draw_guides() {

// Guides already there.
if (Node *scene = EditorNode::get_singleton()->get_edited_scene()) {
Array vguides = scene->get_meta("_edit_vertical_guides_", Array());
Array vguides = scene->get_meta(vertical_guides_meta, Array());
for (int i = 0; i < vguides.size(); i++) {
if (drag_type == DRAG_V_GUIDE && i == dragged_guide_index) {
continue;
Expand All @@ -2925,7 +2925,7 @@ void CanvasItemEditor::_draw_guides() {
viewport->draw_line(Point2(x, 0), Point2(x, viewport->get_size().y), guide_color, Math::round(EDSCALE));
}

Array hguides = scene->get_meta("_edit_horizontal_guides_", Array());
Array hguides = scene->get_meta(horizontal_guides_meta, Array());
for (int i = 0; i < hguides.size(); i++) {
if (drag_type == DRAG_H_GUIDE && i == dragged_guide_index) {
continue;
Expand Down Expand Up @@ -4742,19 +4742,19 @@ void CanvasItemEditor::_popup_callback(int p_op) {
case CLEAR_GUIDES: {
Node *const root = EditorNode::get_singleton()->get_edited_scene();

if (root && (root->has_meta("_edit_horizontal_guides_") || root->has_meta("_edit_vertical_guides_"))) {
if (root && (root->has_meta(horizontal_guides_meta) || root->has_meta(vertical_guides_meta))) {
undo_redo->create_action(TTR("Clear Guides"));
if (root->has_meta("_edit_horizontal_guides_")) {
Array hguides = root->get_meta("_edit_horizontal_guides_");
if (root->has_meta(horizontal_guides_meta)) {
Array hguides = root->get_meta(horizontal_guides_meta);

undo_redo->add_do_method(root, "remove_meta", "_edit_horizontal_guides_");
undo_redo->add_undo_method(root, "set_meta", "_edit_horizontal_guides_", hguides);
undo_redo->add_do_method(root, "remove_meta", horizontal_guides_meta);
undo_redo->add_undo_method(root, "set_meta", horizontal_guides_meta, hguides);
}
if (root->has_meta("_edit_vertical_guides_")) {
Array vguides = root->get_meta("_edit_vertical_guides_");
if (root->has_meta(vertical_guides_meta)) {
Array vguides = root->get_meta(vertical_guides_meta);

undo_redo->add_do_method(root, "remove_meta", "_edit_vertical_guides_");
undo_redo->add_undo_method(root, "set_meta", "_edit_vertical_guides_", vguides);
undo_redo->add_do_method(root, "remove_meta", vertical_guides_meta);
undo_redo->add_undo_method(root, "set_meta", vertical_guides_meta, vguides);
}
undo_redo->add_do_method(viewport, "queue_redraw");
undo_redo->add_undo_method(viewport, "queue_redraw");
Expand Down Expand Up @@ -5222,6 +5222,9 @@ CanvasItemEditor::CanvasItemEditor() {
snap_target[0] = SNAP_TARGET_NONE;
snap_target[1] = SNAP_TARGET_NONE;

horizontal_guides_meta = StaticCString::create("_edit_horizontal_guides_");
vertical_guides_meta = StaticCString::create("_edit_vertical_guides_");

editor_selection = EditorNode::get_singleton()->get_editor_selection();
editor_selection->add_editor_plugin(this);
editor_selection->connect("selection_changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/canvas_item_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class CanvasItemEditor : public VBoxContainer {
};

const String locked_transform_warning = TTRC("All selected CanvasItems are either invisible or locked in some way and can't be transformed.");
StringName horizontal_guides_meta;
StringName vertical_guides_meta;

bool selection_menu_additive_selection = false;

Expand Down

0 comments on commit 06386d5

Please sign in to comment.