Skip to content

Commit efbc5aa

Browse files
committed
Allow overriding 2D editor cursor
1 parent 2bb3217 commit efbc5aa

File tree

6 files changed

+98
-37
lines changed

6 files changed

+98
-37
lines changed

editor/plugins/camera_2d_editor_plugin.cpp

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@
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
const Callable update_overlays = callable_mp(plugin, &EditorPlugin::update_overlays);
4747

4848
if (selected_camera) {
4949
selected_camera->disconnect(SceneStringName(draw), update_overlays);
50+
if (drag_type != Drag::NONE) {
51+
selected_camera->set_limit_rect(drag_revert);
52+
}
53+
drag_type = Drag::NONE;
54+
hover_type = Drag::NONE;
55+
CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_ARROW);
5056
}
5157
selected_camera = p_camera;
5258

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

90-
if (limit_rect.has_point(pos)) {
91-
drag_type = Drag::CENTER;
78+
drag_type = hover_type;
79+
drag_revert = selected_camera->get_limit_rect();
9280
center_drag_point = pos - limit_rect.position;
93-
plugin->update_overlays();
9481
return true;
9582
}
9683
} else if (drag_type != Drag::NONE) {
@@ -109,18 +96,21 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
10996
selected_camera->set_limit_rect(drag_revert);
11097
drag_type = Drag::NONE;
11198
plugin->update_overlays();
99+
_update_hover(mb->get_position());
112100
return true;
113101
}
114102
return false;
115103
}
116104

117-
if (drag_type == Drag::NONE) {
118-
return false;
119-
}
120-
121105
Ref<InputEventMouseMotion> mm = p_event;
122106
if (mm.is_valid()) {
123-
Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mm->get_position());
107+
Vector2 pos = mm->get_position();
108+
if (drag_type == Drag::NONE) {
109+
_update_hover(pos);
110+
return false;
111+
}
112+
113+
pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos);
124114
pos = CanvasItemEditor::get_singleton()->snap_point(pos);
125115

126116
switch (drag_type) {
@@ -150,6 +140,9 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
150140
selected_camera->set_limit_rect(target_rect);
151141
plugin->update_overlays();
152142
} break;
143+
144+
case Drag::NONE: {
145+
} break;
153146
}
154147
return true;
155148
}
@@ -193,6 +186,55 @@ void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) {
193186
}
194187
}
195188

189+
void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) {
190+
if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
191+
hover_type = Drag::NONE;
192+
CanvasItemEditor::get_singleton()->set_cursor_shape_override();
193+
return;
194+
}
195+
196+
const Rect2 limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(selected_camera->get_limit_rect());
197+
const float drag_tolerance = 8.0;
198+
199+
hover_type = Drag::NONE;
200+
if (p_mouse_pos.y > limit_rect.position.y && p_mouse_pos.y < limit_rect.get_end().y) {
201+
if (Math::abs(p_mouse_pos.x - limit_rect.position.x) < drag_tolerance) {
202+
hover_type = Drag::LEFT;
203+
} else if (Math::abs(p_mouse_pos.x - limit_rect.get_end().x) < drag_tolerance) {
204+
hover_type = Drag::RIGHT;
205+
}
206+
} else if (p_mouse_pos.x > limit_rect.position.x && p_mouse_pos.x < limit_rect.get_end().x) {
207+
if (Math::abs(p_mouse_pos.y - limit_rect.position.y) < drag_tolerance) {
208+
hover_type = Drag::TOP;
209+
} else if (Math::abs(p_mouse_pos.y - limit_rect.get_end().y) < drag_tolerance) {
210+
hover_type = Drag::BOTTOM;
211+
}
212+
}
213+
214+
/* Temporarily disabled, because it needs more changes.
215+
if (hover_type == Drag::NONE && limit_rect.has_point(p_mouse_pos)) {
216+
hover_type = Drag::CENTER;
217+
}
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+
196238
void Camera2DEditor::_notification(int p_what) {
197239
switch (p_what) {
198240
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
@@ -2802,6 +2802,11 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
28022802
}
28032803

28042804
void CanvasItemEditor::_update_cursor() {
2805+
if (cursor_shape_override != CURSOR_ARROW) {
2806+
set_default_cursor_shape(cursor_shape_override);
2807+
return;
2808+
}
2809+
28052810
// Choose the correct default cursor.
28062811
CursorShape c = CURSOR_ARROW;
28072812
switch (tool) {
@@ -2865,6 +2870,14 @@ void CanvasItemEditor::_update_lock_and_group_button() {
28652870
ungroup_button->set_disabled(!has_canvas_item);
28662871
}
28672872

2873+
void CanvasItemEditor::set_cursor_shape_override(CursorShape p_shape) {
2874+
if (cursor_shape_override == p_shape) {
2875+
return;
2876+
}
2877+
cursor_shape_override = p_shape;
2878+
_update_cursor();
2879+
}
2880+
28682881
Control::CursorShape CanvasItemEditor::get_cursor_shape(const Point2 &p_pos) const {
28692882
// Compute an eventual rotation of the cursor
28702883
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
@@ -368,6 +368,7 @@ class CanvasItemEditor : public VBoxContainer {
368368
Transform2D original_transform;
369369

370370
Point2 box_selecting_to;
371+
CursorShape cursor_shape_override = CURSOR_ARROW;
371372

372373
Ref<StyleBoxTexture> select_sb;
373374
Ref<Texture2D> select_handle;
@@ -584,6 +585,7 @@ class CanvasItemEditor : public VBoxContainer {
584585
void focus_selection();
585586
void center_at(const Point2 &p_pos);
586587

588+
void set_cursor_shape_override(CursorShape p_shape = CURSOR_ARROW);
587589
virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
588590

589591
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
@@ -564,16 +564,16 @@ void Camera2D::_make_current(Object *p_which) {
564564
}
565565
}
566566

567-
void Camera2D::set_limit_rect(const Rect2 &p_limit_rect) {
568-
const Point2 limit_rect_end = p_limit_rect.get_end();
567+
void Camera2D::set_limit_rect(const Rect2i &p_limit_rect) {
568+
const Point2i limit_rect_end = p_limit_rect.get_end();
569569
set_limit(SIDE_LEFT, p_limit_rect.position.x);
570570
set_limit(SIDE_TOP, p_limit_rect.position.y);
571571
set_limit(SIDE_RIGHT, limit_rect_end.x);
572572
set_limit(SIDE_BOTTOM, limit_rect_end.y);
573573
}
574574

575-
Rect2 Camera2D::get_limit_rect() const {
576-
return Rect2(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
575+
Rect2i Camera2D::get_limit_rect() const {
576+
return Rect2i(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
577577
}
578578

579579
void Camera2D::make_current() {

scene/2d/camera_2d.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class Camera2D : public Node2D {
121121
static void _bind_methods();
122122

123123
public:
124-
void set_limit_rect(const Rect2 &p_limit_rect);
125-
Rect2 get_limit_rect() const;
124+
void set_limit_rect(const Rect2i &p_limit_rect);
125+
Rect2i get_limit_rect() const;
126126

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

0 commit comments

Comments
 (0)