Skip to content

Commit 896f140

Browse files
committed
Allow overriding 2D editor cursor
1 parent 0a43177 commit 896f140

File tree

6 files changed

+96
-37
lines changed

6 files changed

+96
-37
lines changed

editor/plugins/camera_2d_editor_plugin.cpp

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "scene/gui/menu_button.h"
4141

4242
void Camera2DEditor::edit(Camera2D *p_camera) {
43-
if (p_camera == selected_camera) {
43+
if (p_camera == selected_camera) {
4444
return;
4545
}
4646

@@ -49,6 +49,12 @@ if (p_camera == selected_camera) {
4949

5050
if (selected_camera) {
5151
selected_camera->disconnect(update_signal, update_overlays);
52+
if (drag_type != Drag::NONE) {
53+
selected_camera->set_limit_rect(drag_revert);
54+
}
55+
drag_type = Drag::NONE;
56+
hover_type = Drag::NONE;
57+
CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_ARROW);
5258
}
5359
selected_camera = p_camera;
5460

@@ -67,32 +73,13 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
6773
if (mb.is_valid()) {
6874
if (mb->get_button_index() == MouseButton::LEFT) {
6975
if (mb->is_pressed()) {
70-
const Rect2 limit_rect = selected_camera->get_limit_rect();
71-
const Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position());
72-
drag_revert = limit_rect;
73-
74-
if (pos.y > limit_rect.position.y && pos.y < limit_rect.get_end().y) {
75-
if (ABS(pos.x - limit_rect.position.x) < 8) {
76-
drag_type = Drag::LEFT;
77-
return true;
78-
} else if (ABS(pos.x - limit_rect.get_end().x) < 8) {
79-
drag_type = Drag::RIGHT;
80-
return true;
81-
}
82-
} else if (pos.x > limit_rect.position.x && pos.x < limit_rect.get_end().x) {
83-
if (ABS(pos.y - limit_rect.position.y) < 8) {
84-
drag_type = Drag::TOP;
85-
return true;
86-
} else if (ABS(pos.y - limit_rect.get_end().y) < 8) {
87-
drag_type = Drag::BOTTOM;
88-
return true;
89-
}
90-
}
76+
if (hover_type != Drag::NONE) {
77+
Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position());
78+
const Rect2 limit_rect = selected_camera->get_limit_rect();
9179

92-
if (limit_rect.has_point(pos)) {
93-
drag_type = Drag::CENTER;
80+
drag_type = hover_type;
81+
drag_revert = selected_camera->get_limit_rect();
9482
center_drag_point = pos - limit_rect.position;
95-
plugin->update_overlays();
9683
return true;
9784
}
9885
} else if (drag_type != Drag::NONE) {
@@ -111,18 +98,21 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
11198
selected_camera->set_limit_rect(drag_revert);
11299
drag_type = Drag::NONE;
113100
plugin->update_overlays();
101+
_update_hover(mb->get_position());
114102
return true;
115103
}
116104
return false;
117105
}
118106

119-
if (drag_type == Drag::NONE) {
120-
return false;
121-
}
122-
123107
Ref<InputEventMouseMotion> mm = p_event;
124108
if (mm.is_valid()) {
125-
Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mm->get_position());
109+
Vector2 pos = mm->get_position();
110+
if (drag_type == Drag::NONE) {
111+
_update_hover(pos);
112+
return false;
113+
}
114+
115+
pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos);
126116
pos = CanvasItemEditor::get_singleton()->snap_point(pos);
127117

128118
switch (drag_type) {
@@ -152,6 +142,9 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
152142
selected_camera->set_limit_rect(target_rect);
153143
plugin->update_overlays();
154144
} break;
145+
146+
case Drag::NONE: {
147+
} break;
155148
}
156149
return true;
157150
}
@@ -195,6 +188,53 @@ void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) {
195188
}
196189
}
197190

191+
void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) {
192+
if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
193+
hover_type = Drag::NONE;
194+
CanvasItemEditor::get_singleton()->set_cursor_shape_override();
195+
return;
196+
}
197+
198+
const Rect2 limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(selected_camera->get_limit_rect());
199+
const float drag_tolerance = 8.0;
200+
201+
hover_type = Drag::NONE;
202+
if (p_mouse_pos.y > limit_rect.position.y && p_mouse_pos.y < limit_rect.get_end().y) {
203+
if (Math::abs(p_mouse_pos.x - limit_rect.position.x) < drag_tolerance) {
204+
hover_type = Drag::LEFT;
205+
} else if (Math::abs(p_mouse_pos.x - limit_rect.get_end().x) < drag_tolerance) {
206+
hover_type = Drag::RIGHT;
207+
}
208+
} else if (p_mouse_pos.x > limit_rect.position.x && p_mouse_pos.x < limit_rect.get_end().x) {
209+
if (Math::abs(p_mouse_pos.y - limit_rect.position.y) < drag_tolerance) {
210+
hover_type = Drag::TOP;
211+
} else if (Math::abs(p_mouse_pos.y - limit_rect.get_end().y) < drag_tolerance) {
212+
hover_type = Drag::BOTTOM;
213+
}
214+
}
215+
216+
if (hover_type == Drag::NONE && limit_rect.has_point(p_mouse_pos)) {
217+
hover_type = Drag::CENTER;
218+
}
219+
220+
switch (hover_type) {
221+
case Drag::NONE: {
222+
CanvasItemEditor::get_singleton()->set_cursor_shape_override();
223+
} break;
224+
case Drag::LEFT:
225+
case Drag::RIGHT: {
226+
CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_HSIZE);
227+
} break;
228+
case Drag::TOP:
229+
case Drag::BOTTOM: {
230+
CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_VSIZE);
231+
} break;
232+
case Drag::CENTER: {
233+
CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_MOVE);
234+
} break;
235+
}
236+
}
237+
198238
void Camera2DEditor::_notification(int p_what) {
199239
switch (p_what) {
200240
case NOTIFICATION_THEME_CHANGED: {

editor/plugins/camera_2d_editor_plugin.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ class Camera2DEditor : public Control {
5252
RIGHT,
5353
BOTTOM,
5454
CENTER,
55-
} drag_type;
55+
};
56+
Drag drag_type = Drag::NONE;
57+
Drag hover_type = Drag::NONE;
58+
5659
Rect2 drag_revert;
5760
Vector2 center_drag_point;
5861

@@ -64,6 +67,7 @@ class Camera2DEditor : public Control {
6467
void _menu_option(int p_option);
6568
void _snap_limits_to_viewport(Camera2D *p_camera);
6669
void _update_overlays_if_needed(Camera2D *p_camera);
70+
void _update_hover(const Vector2 &p_mouse_pos);
6771

6872
protected:
6973
static void _bind_methods();

editor/plugins/canvas_item_editor_plugin.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,11 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
28012801
}
28022802

28032803
void CanvasItemEditor::_update_cursor() {
2804+
if (cursor_shape_override != CURSOR_ARROW) {
2805+
set_default_cursor_shape(cursor_shape_override);
2806+
return;
2807+
}
2808+
28042809
// Choose the correct default cursor.
28052810
CursorShape c = CURSOR_ARROW;
28062811
switch (tool) {
@@ -2864,6 +2869,14 @@ void CanvasItemEditor::_update_lock_and_group_button() {
28642869
ungroup_button->set_disabled(!has_canvas_item);
28652870
}
28662871

2872+
void CanvasItemEditor::set_cursor_shape_override(CursorShape p_shape) {
2873+
if (cursor_shape_override == p_shape) {
2874+
return;
2875+
}
2876+
cursor_shape_override = p_shape;
2877+
_update_cursor();
2878+
}
2879+
28672880
Control::CursorShape CanvasItemEditor::get_cursor_shape(const Point2 &p_pos) const {
28682881
// Compute an eventual rotation of the cursor
28692882
const CursorShape rotation_array[4] = { CURSOR_HSIZE, CURSOR_BDIAGSIZE, CURSOR_VSIZE, CURSOR_FDIAGSIZE };

editor/plugins/canvas_item_editor_plugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ class CanvasItemEditor : public VBoxContainer {
366366
Transform2D original_transform;
367367

368368
Point2 box_selecting_to;
369+
CursorShape cursor_shape_override = CURSOR_ARROW;
369370

370371
Ref<StyleBoxTexture> select_sb;
371372
Ref<Texture2D> select_handle;
@@ -582,6 +583,7 @@ class CanvasItemEditor : public VBoxContainer {
582583
void focus_selection();
583584
void center_at(const Point2 &p_pos);
584585

586+
void set_cursor_shape_override(CursorShape p_shape = CURSOR_ARROW);
585587
virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
586588

587589
ThemePreviewMode get_theme_preview() const { return theme_preview; }

scene/2d/camera_2d.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,16 +551,16 @@ void Camera2D::_make_current(Object *p_which) {
551551
}
552552
}
553553

554-
void Camera2D::set_limit_rect(const Rect2 &p_limit_rect) {
555-
const Point2 limit_rect_end = p_limit_rect.get_end();
554+
void Camera2D::set_limit_rect(const Rect2i &p_limit_rect) {
555+
const Point2i limit_rect_end = p_limit_rect.get_end();
556556
set_limit(SIDE_LEFT, p_limit_rect.position.x);
557557
set_limit(SIDE_TOP, p_limit_rect.position.y);
558558
set_limit(SIDE_RIGHT, limit_rect_end.x);
559559
set_limit(SIDE_BOTTOM, limit_rect_end.y);
560560
}
561561

562-
Rect2 Camera2D::get_limit_rect() const {
563-
return Rect2(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
562+
Rect2i Camera2D::get_limit_rect() const {
563+
return Rect2i(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
564564
}
565565

566566
void Camera2D::make_current() {

scene/2d/camera_2d.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ class Camera2D : public Node2D {
122122
void _validate_property(PropertyInfo &p_property) const;
123123

124124
public:
125-
void set_limit_rect(const Rect2 &p_limit_rect);
126-
Rect2 get_limit_rect() const;
125+
void set_limit_rect(const Rect2i &p_limit_rect);
126+
Rect2i get_limit_rect() const;
127127

128128
void set_offset(const Vector2 &p_offset);
129129
Vector2 get_offset() const;

0 commit comments

Comments
 (0)