summaryrefslogtreecommitdiffstats
path: root/editor
diff options
context:
space:
mode:
authorSpartan322 <Megacake1234@gmail.com>2024-10-25 07:47:07 -0400
committerSpartan322 <Megacake1234@gmail.com>2024-10-25 07:47:07 -0400
commitf6edeb0e210c16f5c93da3e7bf305cd88bb7a7e8 (patch)
treea71e5b7922a78ca12e9b0730d1962bc21caa33b4 /editor
parent9056ffee0983e8d7285f53a18835fcb43428f314 (diff)
downloadredot-engine-f6edeb0e210c16f5c93da3e7bf305cd88bb7a7e8.tar.gz
Overhaul `CanvasItemEditor` snap lines drawing and input
(cherry picked from commit blazium-engine/blazium@5ca9caa0f88c7ee4c9cad38decf0a064eb126bab) Co-authored-by: Mounir Tohami <53877170+WhalesState@users.noreply.github.com>
Diffstat (limited to 'editor')
-rw-r--r--editor/plugins/canvas_item_editor_plugin.cpp252
-rw-r--r--editor/plugins/canvas_item_editor_plugin.h2
2 files changed, 137 insertions, 117 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp
index d81faa1ef9..63a7f8a414 100644
--- a/editor/plugins/canvas_item_editor_plugin.cpp
+++ b/editor/plugins/canvas_item_editor_plugin.cpp
@@ -1063,6 +1063,11 @@ void CanvasItemEditor::_switch_theme_preview(int p_mode) {
}
bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_event) {
+ Node *const scene = EditorNode::get_singleton()->get_edited_scene();
+ if (!scene) {
+ return true;
+ }
+
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
Ref<InputEventMouseButton> b = p_event;
Ref<InputEventMouseMotion> m = p_event;
@@ -1155,18 +1160,19 @@ bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_eve
drag_to = xform.affine_inverse().xform(m->get_position());
dragged_guide_pos = xform.xform(snap_point(drag_to, SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES));
+ if (drag_type != DRAG_DOUBLE_GUIDE) {
+ snap_target[(drag_type == DRAG_H_GUIDE) ? 0 : 1] = SNAP_TARGET_NONE;
+ }
viewport->queue_redraw();
return true;
}
// Release confirms the guide move
if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- if (show_guides && EditorNode::get_singleton()->get_edited_scene()) {
+ if (show_guides) {
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 = scene->get_meta("_edit_vertical_guides_", Array());
+ Array hguides = scene->get_meta("_edit_horizontal_guides_", 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) {
@@ -1176,18 +1182,20 @@ 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(scene, "set_meta", "_edit_vertical_guides_", vguides);
+ undo_redo->add_do_method(viewport, "queue_redraw");
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_vertical_guides_", 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(scene, "set_meta", "_edit_vertical_guides_", vguides);
+ undo_redo->add_do_method(viewport, "queue_redraw");
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(scene, "remove_meta", "_edit_vertical_guides_");
} else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_vertical_guides_", prev_vguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
@@ -1197,11 +1205,12 @@ 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(scene, "remove_meta", "_edit_vertical_guides_");
} else {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
+ undo_redo->add_do_method(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(viewport, "queue_redraw");
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_vertical_guides_", prev_vguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
}
@@ -1213,18 +1222,20 @@ 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(scene, "set_meta", "_edit_horizontal_guides_", hguides);
+ undo_redo->add_do_method(viewport, "queue_redraw");
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_horizontal_guides_", 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(scene, "set_meta", "_edit_horizontal_guides_", hguides);
+ undo_redo->add_do_method(viewport, "queue_redraw");
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(scene, "remove_meta", "_edit_horizontal_guides_");
} else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_horizontal_guides_", prev_hguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
@@ -1234,11 +1245,12 @@ 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(scene, "remove_meta", "_edit_horizontal_guides_");
} else {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
+ undo_redo->add_do_method(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(viewport, "queue_redraw");
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_horizontal_guides_", prev_hguides);
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
}
@@ -1251,27 +1263,24 @@ 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(scene, "set_meta", "_edit_vertical_guides_", vguides);
+ undo_redo->add_do_method(scene, "set_meta", "_edit_horizontal_guides_", hguides);
+ undo_redo->add_do_method(viewport, "queue_redraw");
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(scene, "remove_meta", "_edit_vertical_guides_");
} else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_vertical_guides_", 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(scene, "remove_meta", "_edit_horizontal_guides_");
} else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
+ undo_redo->add_undo_method(scene, "set_meta", "_edit_horizontal_guides_", prev_hguides);
}
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
}
}
}
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- _reset_drag();
- viewport->queue_redraw();
return true;
}
}
@@ -1699,18 +1708,13 @@ bool CanvasItemEditor::_gui_input_anchors(const Ref<InputEvent> &p_event) {
_commit_canvas_item_state(
drag_selection,
vformat(TTR("Move CanvasItem \"%s\" Anchor"), drag_selection.front()->get()->get_name()));
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
_reset_drag();
- viewport->queue_redraw();
return true;
}
// Cancel a drag
if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) {
_restore_canvas_item_state(drag_selection);
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
_reset_drag();
viewport->queue_redraw();
return true;
@@ -1798,7 +1802,7 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
bool symmetric = m->is_alt_pressed();
Rect2 local_rect = ci->_edit_get_rect();
- real_t aspect = local_rect.has_area() ? (local_rect.get_size().y / local_rect.get_size().x) : (local_rect.get_size().y + 1.0) / (local_rect.get_size().x + 1.0);
+ real_t aspect = local_rect.get_size().y / local_rect.get_size().x;
Point2 current_begin = local_rect.get_position();
Point2 current_end = local_rect.get_position() + local_rect.get_size();
Point2 max_begin = (symmetric) ? (current_begin + current_end - ci->_edit_get_minimum_size()) / 2.0 : current_end - ci->_edit_get_minimum_size();
@@ -1812,8 +1816,30 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
Point2 drag_to_snapped_begin;
Point2 drag_to_snapped_end;
- drag_to_snapped_end = snap_point(xform.xform(current_end) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
- drag_to_snapped_begin = snap_point(xform.xform(current_begin) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ Point2 from = drag_from;
+ Point2 to = drag_to;
+ if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP) {
+ drag_to_snapped_begin = snap_point(xform.xform(current_begin) + (to - from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ drag_to_snapped_end = xform.xform(current_end) + (drag_to - drag_from);
+ if (drag_type != DRAG_TOP_LEFT) {
+ snap_target[(drag_type == DRAG_TOP) ? 0 : 1] = SNAP_TARGET_NONE;
+ }
+ } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_RIGHT || drag_type == DRAG_RIGHT) {
+ drag_to_snapped_begin = xform.xform(current_begin) + (drag_to - drag_from);
+ drag_to_snapped_end = snap_point(xform.xform(current_end) + (to - from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ if (drag_type != DRAG_BOTTOM_RIGHT) {
+ snap_target[(drag_type == DRAG_BOTTOM) ? 0 : 1] = SNAP_TARGET_NONE;
+ }
+ } else {
+ drag_to_snapped_begin = snap_point(xform.xform(current_begin) + (to - from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ snap_transform2 = snap_transform;
+ from = drag_from;
+ to = drag_to;
+ int idx = (drag_type == DRAG_TOP_RIGHT) ? 1 : 0;
+ snap_target2[idx] = snap_target[idx];
+ drag_to_snapped_end = snap_point(xform.xform(current_end) + (to - from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ snap_target[(drag_type == DRAG_TOP_RIGHT) ? 1 : 0] = SNAP_TARGET_NONE;
+ }
Point2 drag_begin = xform.affine_inverse().xform(drag_to_snapped_begin);
Point2 drag_end = xform.affine_inverse().xform(drag_to_snapped_end);
@@ -1836,8 +1862,10 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
if (uniform) {
if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT) {
current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
+ snap_target[1] = SNAP_TARGET_NONE;
} else if (drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM) {
current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
+ snap_target[0] = SNAP_TARGET_NONE;
} else {
if (aspect >= 1.0) {
if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
@@ -1845,12 +1873,16 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
} else {
current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
}
+ snap_target[1] = SNAP_TARGET_NONE;
+ snap_target2[1] = SNAP_TARGET_NONE;
} else {
if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
current_begin.x = current_end.x - (current_end.y - current_begin.y) / aspect;
} else {
current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
}
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target2[0] = SNAP_TARGET_NONE;
}
}
}
@@ -1901,11 +1933,6 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
if (key_auto_insert_button->is_pressed()) {
_insert_animation_keys(false, false, true, true);
}
-
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- _reset_drag();
- viewport->queue_redraw();
return true;
}
@@ -1914,6 +1941,8 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
_restore_canvas_item_state(drag_selection);
snap_target[0] = SNAP_TARGET_NONE;
snap_target[1] = SNAP_TARGET_NONE;
+ snap_target2[0] = SNAP_TARGET_NONE;
+ snap_target2[1] = SNAP_TARGET_NONE;
_reset_drag();
viewport->queue_redraw();
return true;
@@ -2188,13 +2217,6 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
if (key_auto_insert_button->is_pressed()) {
_insert_animation_keys(true, false, false, true);
}
-
- //Make sure smart snapping lines disappear.
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
-
- _reset_drag();
- viewport->queue_redraw();
return true;
}
@@ -2203,6 +2225,8 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
_restore_canvas_item_state(drag_selection, true);
snap_target[0] = SNAP_TARGET_NONE;
snap_target[1] = SNAP_TARGET_NONE;
+ snap_target2[0] = SNAP_TARGET_NONE;
+ snap_target2[1] = SNAP_TARGET_NONE;
_reset_drag();
viewport->queue_redraw();
return true;
@@ -2490,8 +2514,7 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) {
if (drag_type == DRAG_BOX_SELECTION) {
if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
// Confirms box selection.
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
- if (scene) {
+ if (Node *const scene = EditorNode::get_singleton()->get_edited_scene()) {
List<CanvasItem *> selitems;
Point2 bsfrom = drag_from;
@@ -2512,8 +2535,6 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) {
}
}
- _reset_drag();
- viewport->queue_redraw();
return true;
}
@@ -2636,9 +2657,8 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
bool accepted = false;
Ref<InputEventMouseButton> mb = p_event;
- bool release_lmb = (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT); // Required to properly release some stuff (e.g. selection box) while panning.
- if (EDITOR_GET("editors/panning/simple_panning") || !pan_pressed || release_lmb) {
+ if (EDITOR_GET("editors/panning/simple_panning") || !pan_pressed) {
accepted = true;
if (_gui_input_rulers_and_guides(p_event)) {
// print_line("Rulers and guides");
@@ -2677,7 +2697,15 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
// Handles the mouse hovering
_gui_input_hover(p_event);
- if (mb.is_valid()) {
+ if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
+ if (!mb->is_pressed()) {
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ snap_target2[0] = SNAP_TARGET_NONE;
+ snap_target2[1] = SNAP_TARGET_NONE;
+ _reset_drag();
+ viewport->queue_redraw();
+ }
// Update the default cursor.
_update_cursor();
}
@@ -2914,16 +2942,24 @@ void CanvasItemEditor::_draw_guides() {
}
void CanvasItemEditor::_draw_smart_snapping() {
+ if (drag_type == DRAG_NONE) {
+ return;
+ }
+
Color line_color = EDITOR_GET("editors/2d/smart_snapping_line_color");
+ Transform2D xform = transform * snap_transform;
if (snap_target[0] != SNAP_TARGET_NONE && snap_target[0] != SNAP_TARGET_GRID) {
- viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
- viewport->draw_line(Point2(0, -1.0e+10F), Point2(0, 1.0e+10F), line_color);
- viewport->draw_set_transform_matrix(viewport->get_transform());
+ _draw_straight_line(xform.xform(Point2(0, 0)), xform.xform(Point2(0, 1)), line_color);
}
if (snap_target[1] != SNAP_TARGET_NONE && snap_target[1] != SNAP_TARGET_GRID) {
- viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
- viewport->draw_line(Point2(-1.0e+10F, 0), Point2(1.0e+10F, 0), line_color);
- viewport->draw_set_transform_matrix(viewport->get_transform());
+ _draw_straight_line(xform.xform(Point2(0, 0)), xform.xform(Point2(1, 0)), line_color);
+ }
+ xform = transform * snap_transform2;
+ if (snap_target2[0] != SNAP_TARGET_NONE && snap_target2[0] != SNAP_TARGET_GRID) {
+ _draw_straight_line(xform.xform(Point2(0, 0)), xform.xform(Point2(0, 1)), line_color);
+ }
+ if (snap_target2[1] != SNAP_TARGET_NONE && snap_target2[1] != SNAP_TARGET_GRID) {
+ _draw_straight_line(xform.xform(Point2(0, 0)), xform.xform(Point2(1, 0)), line_color);
}
}
@@ -3093,7 +3129,6 @@ void CanvasItemEditor::_draw_ruler_tool() {
return;
}
- const Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPosition"));
if (ruler_tool_active) {
Color ruler_primary_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
Color ruler_secondary_color = ruler_primary_color;
@@ -3162,6 +3197,7 @@ void CanvasItemEditor::_draw_ruler_tool() {
if (begin.is_equal_approx(end)) {
viewport->draw_string_outline(font, text_pos, (String)ruler_tool_origin, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
viewport->draw_string(font, text_pos, (String)ruler_tool_origin, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
+ Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPosition"));
viewport->draw_texture(position_icon, (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
return;
}
@@ -3233,6 +3269,7 @@ void CanvasItemEditor::_draw_ruler_tool() {
}
} else {
if (grid_snap_active) {
+ Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPosition"));
viewport->draw_texture(position_icon, (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
}
}
@@ -3501,33 +3538,18 @@ void CanvasItemEditor::_draw_selection() {
xform.xform(rect.position + Vector2(0, rect.size.y))
};
- Color c = Color(1, 0.6, 0.4, 0.7);
-
- if (item_locked) {
- c = Color(0.7, 0.7, 0.7, 0.7);
- }
+ Color c = item_locked ? Color(0.7, 0.7, 0.7, 0.7) : Color(1, 0.6, 0.4, 0.7);
for (int i = 0; i < 4; i++) {
viewport->draw_line(endpoints[i], endpoints[(i + 1) % 4], c, Math::round(2 * EDSCALE));
}
} else {
- Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
- Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
- viewport->draw_set_transform_matrix(simple_xform);
- viewport->draw_texture(position_icon, -(position_icon->get_size() / 2));
- viewport->draw_set_transform_matrix(viewport->get_transform());
+ viewport->draw_texture(position_icon, xform.xform(Point2()) - (position_icon->get_size() / 2).floor());
}
if (single && !item_locked && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE || tool == TOOL_EDIT_PIVOT)) { //kind of sucks
- // Draw the pivot
if (ci->_edit_use_pivot()) {
- // Draw the node's pivot
- Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
- Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
-
- viewport->draw_set_transform_matrix(simple_xform);
- viewport->draw_texture(pivot_icon, -(pivot_icon->get_size() / 2).floor());
- viewport->draw_set_transform_matrix(viewport->get_transform());
+ viewport->draw_texture(pivot_icon, xform.xform(ci->_edit_get_pivot()) - (pivot_icon->get_size() / 2).floor());
}
// Draw control-related helpers
@@ -3663,25 +3685,22 @@ void CanvasItemEditor::_draw_selection() {
void CanvasItemEditor::_draw_straight_line(Point2 p_from, Point2 p_to, Color p_color) {
// Draw a line going through the whole screen from a vector
- RID ci = viewport->get_canvas_item();
Vector<Point2> points;
- Point2 from = transform.xform(p_from);
- Point2 to = transform.xform(p_to);
Size2 viewport_size = viewport->get_size();
- if (to.x == from.x) {
+ if (p_to.x == p_from.x) {
// Vertical line
- points.push_back(Point2(to.x, 0));
- points.push_back(Point2(to.x, viewport_size.y));
- } else if (to.y == from.y) {
+ points.push_back(Point2(p_to.x, 0));
+ points.push_back(Point2(p_to.x, viewport_size.y));
+ } else if (p_to.y == p_from.y) {
// Horizontal line
- points.push_back(Point2(0, to.y));
- points.push_back(Point2(viewport_size.x, to.y));
+ points.push_back(Point2(0, p_to.y));
+ points.push_back(Point2(viewport_size.x, p_to.y));
} else {
- real_t y_for_zero_x = (to.y * from.x - from.y * to.x) / (from.x - to.x);
- real_t x_for_zero_y = (to.x * from.y - from.x * to.y) / (from.y - to.y);
- real_t y_for_viewport_x = ((to.y - from.y) * (viewport_size.x - from.x)) / (to.x - from.x) + from.y;
- real_t x_for_viewport_y = ((to.x - from.x) * (viewport_size.y - from.y)) / (to.y - from.y) + from.x; // faux
+ real_t y_for_zero_x = (p_to.y * p_from.x - p_from.y * p_to.x) / (p_from.x - p_to.x);
+ real_t x_for_zero_y = (p_to.x * p_from.y - p_from.x * p_to.y) / (p_from.y - p_to.y);
+ real_t y_for_viewport_x = ((p_to.y - p_from.y) * (viewport_size.x - p_from.x)) / (p_to.x - p_from.x) + p_from.y;
+ real_t x_for_viewport_y = ((p_to.x - p_from.x) * (viewport_size.y - p_from.y)) / (p_to.y - p_from.y) + p_from.x; // faux
//bool start_set = false;
if (y_for_zero_x >= 0 && y_for_zero_x <= viewport_size.y) {
@@ -3698,19 +3717,17 @@ void CanvasItemEditor::_draw_straight_line(Point2 p_from, Point2 p_to, Color p_c
}
}
if (points.size() >= 2) {
- RenderingServer::get_singleton()->canvas_item_add_line(ci, points[0], points[1], p_color);
+ viewport->draw_line(points[0], points[1], p_color, Math::round(EDSCALE));
}
}
void CanvasItemEditor::_draw_axis() {
if (show_origin) {
- _draw_straight_line(Point2(), Point2(1, 0), get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.75));
- _draw_straight_line(Point2(), Point2(0, 1), get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.75));
+ _draw_straight_line(transform.xform(Point2()), transform.xform(Point2(1, 0)), get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.75));
+ _draw_straight_line(transform.xform(Point2()), transform.xform(Point2(0, 1)), get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.75));
}
if (show_viewport) {
- RID ci = viewport->get_canvas_item();
-
Color area_axis_color = EDITOR_GET("editors/2d/viewport_border_color");
Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
@@ -3723,7 +3740,7 @@ void CanvasItemEditor::_draw_axis() {
};
for (int i = 0; i < 4; i++) {
- RenderingServer::get_singleton()->canvas_item_add_line(ci, screen_endpoints[i], screen_endpoints[(i + 1) % 4], area_axis_color);
+ viewport->draw_line(screen_endpoints[i], screen_endpoints[(i + 1) % 4], area_axis_color, Math::round(EDSCALE));
}
}
}
@@ -3760,11 +3777,7 @@ void CanvasItemEditor::_draw_invisible_nodes_positions(Node *p_node, const Trans
// Draw the node's position
Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPositionUnselected"));
- Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
- Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
- viewport->draw_set_transform_matrix(simple_xform);
- viewport->draw_texture(position_icon, -position_icon->get_size() / 2, Color(1.0, 1.0, 1.0, 0.5));
- viewport->draw_set_transform_matrix(viewport->get_transform());
+ viewport->draw_texture(position_icon, xform.xform(Point2()) - (position_icon->get_size() / 2).floor(), Color(1.0, 1.0, 1.0, 0.5));
}
}
@@ -3913,9 +3926,9 @@ void CanvasItemEditor::_draw_viewport() {
_draw_grid();
_draw_ruler_tool();
_draw_axis();
- if (EditorNode::get_singleton()->get_edited_scene()) {
- _draw_locks_and_groups(EditorNode::get_singleton()->get_edited_scene());
- _draw_invisible_nodes_positions(EditorNode::get_singleton()->get_edited_scene());
+ if (Node *const scene = EditorNode::get_singleton()->get_edited_scene()) {
+ _draw_locks_and_groups(scene);
+ _draw_invisible_nodes_positions(scene);
}
_draw_selection();
@@ -4163,10 +4176,12 @@ void CanvasItemEditor::_update_scrollbars() {
// Calculate scrollable area.
Rect2 canvas_item_rect = Rect2(Point2(), screen_rect);
- if (EditorNode::get_singleton()->is_inside_tree() && EditorNode::get_singleton()->get_edited_scene()) {
- Rect2 content_rect = _get_encompassing_rect(EditorNode::get_singleton()->get_edited_scene());
- canvas_item_rect.expand_to(content_rect.position);
- canvas_item_rect.expand_to(content_rect.position + content_rect.size);
+ if (EditorNode::get_singleton()->is_inside_tree()) {
+ if (Node *const scene = EditorNode::get_singleton()->get_edited_scene()) {
+ Rect2 content_rect = _get_encompassing_rect(scene);
+ canvas_item_rect.expand_to(content_rect.position);
+ canvas_item_rect.expand_to(content_rect.position + content_rect.size);
+ }
}
canvas_item_rect.size += screen_rect * 2;
canvas_item_rect.position -= screen_rect;
@@ -4824,9 +4839,10 @@ void CanvasItemEditor::_focus_selection(int p_op) {
item_rect = Rect2();
}
- Vector2 pos = ci->get_global_transform().get_origin();
- Vector2 scale = ci->get_global_transform().get_scale();
- real_t angle = ci->get_global_transform().get_rotation();
+ const Transform2D xform = ci->get_global_transform();
+ Vector2 pos = xform.get_origin();
+ Vector2 scale = xform.get_scale();
+ real_t angle = xform.get_rotation();
Transform2D t(angle, Vector2(0.f, 0.f));
item_rect = t.xform(item_rect);
@@ -5194,6 +5210,8 @@ void CanvasItemEditor::center_at(const Point2 &p_pos) {
CanvasItemEditor::CanvasItemEditor() {
snap_target[0] = SNAP_TARGET_NONE;
snap_target[1] = SNAP_TARGET_NONE;
+ snap_target2[0] = SNAP_TARGET_NONE;
+ snap_target2[1] = SNAP_TARGET_NONE;
editor_selection = EditorNode::get_singleton()->get_editor_selection();
editor_selection->add_editor_plugin(this);
diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h
index 9c6ae3ac05..fcfbb656ae 100644
--- a/editor/plugins/canvas_item_editor_plugin.h
+++ b/editor/plugins/canvas_item_editor_plugin.h
@@ -493,7 +493,9 @@ private:
void _project_settings_changed();
SnapTarget snap_target[2];
+ SnapTarget snap_target2[2];
Transform2D snap_transform;
+ Transform2D snap_transform2;
void _snap_if_closer_float(
const real_t p_value,
real_t &r_current_snap, SnapTarget &r_current_snap_target,