Godot Engine – Multi-platform 2D and 3D game engine
Add cursor overrides for 2D shape editor
kobewi7685d022d607
diff --git a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cppindex fd96fd54df3..997f379b5d8 100644--- a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp+++ b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp@@ -216,7 +216,7 @@ void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {node->set_global_position(original_transform.get_origin());} else {rect->set_size(((Point2)original + (size - (Point2)original) * 0.5).abs());- Point2 pos = original_transform.affine_inverse().xform(original_transform.get_origin());+ Point2 pos;pos += (size - (Point2)original) * 0.5 * RECT_HANDLES[idx] * 0.5;node->set_global_position(original_transform.xform(pos));}@@ -362,21 +362,13 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e}Ref<InputEventMouseButton> mb = p_event;- Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();if (mb.is_valid()) {Vector2 gpoint = mb->get_position();if (mb->get_button_index() == MouseButton::LEFT) {if (mb->is_pressed()) {- for (int i = 0; i < handles.size(); i++) {- if (xform.xform(handles[i]).distance_to(gpoint) < grab_threshold) {- edit_handle = i;-- break;- }- }-+ edit_handle = hover_handle;if (edit_handle == -1) {pressed = false;@@ -412,6 +404,7 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_eset_handle(edit_handle, original_point);}+ CanvasItemEditor::get_singleton()->set_cursor_shape_override();edit_handle = -1;pressed = false;@@ -425,12 +418,17 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_eRef<InputEventMouseMotion> mm = p_event;if (mm.is_valid()) {- if (edit_handle == -1 || !pressed) {+ if (edit_handle == -1) {+ _update_hover(mm->get_position());+ return false;+ }++ if (!pressed) {return false;}Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));- cpoint = node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint);+ cpoint = (node->get_viewport()->get_popup_base_transform() * node->get_canvas_transform()).affine_inverse().xform(cpoint);cpoint = original_transform.affine_inverse().xform(cpoint);last_point = cpoint;@@ -454,6 +452,64 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_ereturn false;}+void CollisionShape2DEditor::_update_hover(const Vector2 &p_mouse_pos) {+ Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();++ int new_hover_handle = -1;+ for (int i = 0; i < handles.size(); i++) {+ if (xform.xform(handles[i]).distance_to(p_mouse_pos) < grab_threshold) {+ new_hover_handle = i;+ break;+ }+ }++ if (new_hover_handle == hover_handle) {+ return;+ }+ hover_handle = new_hover_handle;++ if (hover_handle == -1) {+ CanvasItemEditor::get_singleton()->set_cursor_shape_override();+ return;+ }++ CursorShape cursor_shape = CURSOR_ARROW;+ Vector2 local_dir;+ switch (shape_type) {+ case CAPSULE_SHAPE: {+ if (hover_handle == 0) {+ local_dir = Vector2(1, 0);+ } else if (hover_handle == 1) {+ local_dir = Vector2(0, 1);+ }+ } break;++ case CIRCLE_SHAPE: {+ local_dir = Vector2(1, 0);+ } break;++ case RECTANGLE_SHAPE: {+ local_dir = RECT_HANDLES[hover_handle];+ } break;++ case CONCAVE_POLYGON_SHAPE:+ case CONVEX_POLYGON_SHAPE:+ case WORLD_BOUNDARY_SHAPE:+ case SEPARATION_RAY_SHAPE:+ case SEGMENT_SHAPE: {+ cursor_shape = CURSOR_CROSS;+ } break;+ }++ if (local_dir != Vector2()) {+ real_t screen_angle = xform.basis_xform(local_dir).angle();+ CursorShape rect_shapes[] = { CURSOR_HSIZE, CURSOR_FDIAGSIZE, CURSOR_VSIZE, CURSOR_BDIAGSIZE }; // Handles are ordered angle-wise, cursors are symmetrical, hence 4 instead of 8.+ int cursor_shape_index = Math::posmod(Math::round(screen_angle / (Math::PI / 4)), 4);+ cursor_shape = rect_shapes[cursor_shape_index];+ }+ CanvasItemEditor::get_singleton()->set_cursor_shape_override(cursor_shape);+}+void CollisionShape2DEditor::_shape_changed() {canvas_item_editor->update_viewport();@@ -664,6 +720,7 @@ void CollisionShape2DEditor::edit(Node *p_node) {} else {if (pressed) {set_handle(edit_handle, original_point);+ CanvasItemEditor::get_singleton()->set_cursor_shape_override();pressed = false;}edit_handle = -1;diff --git a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.h b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.hindex 56501fa6222..3b42f0d6bae 100644--- a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.h+++ b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.h@@ -66,6 +66,7 @@ class CollisionShape2DEditor : public Control {Vector<Point2> handles;int shape_type = -1;+ int hover_handle = -1;int edit_handle = -1;bool pressed = false;real_t grab_threshold = 8;@@ -81,6 +82,7 @@ class CollisionShape2DEditor : public Control {void set_handle(int idx, Point2 &p_point);void commit_handle(int idx, Variant &p_org);+ void _update_hover(const Vector2 &p_mouse_pos);void _shape_changed();protected: