Skip to content

Commit

Permalink
Merge pull request #59081 from Sauermann/fix-cursorshape-canvasitemed…
Browse files Browse the repository at this point in the history
…itor

Use get_cursor_shape for identifying the cursor shape in CanvasItemEditor
  • Loading branch information
akien-mga committed Jan 23, 2023
2 parents a98431d + 69fca92 commit d065281
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
67 changes: 33 additions & 34 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class SnapDialog : public ConfirmationDialog {
}
};

bool CanvasItemEditor::_is_node_locked(const Node *p_node) {
bool CanvasItemEditor::_is_node_locked(const Node *p_node) const {
return p_node->get_meta("_edit_lock_", false);
}

Expand Down Expand Up @@ -770,7 +770,7 @@ bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_po
return still_selected;
}

List<CanvasItem *> CanvasItemEditor::_get_edited_canvas_items(bool retrieve_locked, bool remove_canvas_item_if_parent_in_selection) {
List<CanvasItem *> CanvasItemEditor::_get_edited_canvas_items(bool retrieve_locked, bool remove_canvas_item_if_parent_in_selection) const {
List<CanvasItem *> selection;
for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
CanvasItem *ci = Object::cast_to<CanvasItem>(E.key);
Expand Down Expand Up @@ -1226,7 +1226,6 @@ bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bo
bool panner_active = panner->gui_input(p_event, warped_panning ? viewport->get_global_rect() : Rect2());
if (panner->is_panning() != pan_pressed) {
pan_pressed = panner->is_panning();
_update_cursor();
}

if (panner_active) {
Expand Down Expand Up @@ -2570,8 +2569,10 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
// Handles the mouse hovering
_gui_input_hover(p_event);

// Change the cursor
_update_cursor();
if (mb.is_valid()) {
// Update the default cursor.
_update_cursor();
}

// Grab focus
if (!viewport->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
Expand All @@ -2580,6 +2581,31 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
}

void CanvasItemEditor::_update_cursor() {
// Choose the correct default cursor.
CursorShape c = CURSOR_ARROW;
switch (tool) {
case TOOL_MOVE:
c = CURSOR_MOVE;
break;
case TOOL_EDIT_PIVOT:
c = CURSOR_CROSS;
break;
case TOOL_PAN:
c = CURSOR_DRAG;
break;
case TOOL_RULER:
c = CURSOR_CROSS;
break;
default:
break;
}
if (pan_pressed) {
c = CURSOR_DRAG;
}
set_default_cursor_shape(c);
}

Control::CursorShape CanvasItemEditor::get_cursor_shape(const Point2 &p_pos) const {
// Compute an eventual rotation of the cursor
const CursorShape rotation_array[4] = { CURSOR_HSIZE, CURSOR_BDIAGSIZE, CURSOR_VSIZE, CURSOR_FDIAGSIZE };
int rotation_array_index = 0;
Expand All @@ -2601,26 +2627,8 @@ void CanvasItemEditor::_update_cursor() {
}

// Choose the correct cursor
CursorShape c = CURSOR_ARROW;
CursorShape c = get_default_cursor_shape();
switch (drag_type) {
case DRAG_NONE:
switch (tool) {
case TOOL_MOVE:
c = CURSOR_MOVE;
break;
case TOOL_EDIT_PIVOT:
c = CURSOR_CROSS;
break;
case TOOL_PAN:
c = CURSOR_DRAG;
break;
case TOOL_RULER:
c = CURSOR_CROSS;
break;
default:
break;
}
break;
case DRAG_LEFT:
case DRAG_RIGHT:
c = rotation_array[rotation_array_index];
Expand Down Expand Up @@ -2662,16 +2670,7 @@ void CanvasItemEditor::_update_cursor() {
if (pan_pressed) {
c = CURSOR_DRAG;
}

if (c != viewport->get_default_cursor_shape()) {
viewport->set_default_cursor_shape(c);

// Force refresh cursor if it's over the viewport.
if (viewport->get_global_rect().has_point(get_global_mouse_position())) {
DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)viewport->get_default_cursor_shape();
DisplayServer::get_singleton()->cursor_set_shape(ds_cursor_shape);
}
}
return c;
}

void CanvasItemEditor::_draw_text_at_position(Point2 p_position, String p_string, Side p_side) {
Expand Down
6 changes: 4 additions & 2 deletions editor/plugins/canvas_item_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class CanvasItemEditor : public VBoxContainer {
void _pan_callback(Vector2 p_scroll_vec);
void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt);

bool _is_node_locked(const Node *p_node);
bool _is_node_locked(const Node *p_node) const;
bool _is_node_movable(const Node *p_node, bool p_popup_warning = false);
void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<_SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
void _get_canvas_items_at_pos(const Point2 &p_pos, Vector<_SelectResult> &r_items, bool p_allow_locked = false);
Expand Down Expand Up @@ -402,7 +402,7 @@ class CanvasItemEditor : public VBoxContainer {
void _prepare_grid_menu();
void _on_grid_menu_id_pressed(int p_id);

List<CanvasItem *> _get_edited_canvas_items(bool retrieve_locked = false, bool remove_canvas_item_if_parent_in_selection = true);
List<CanvasItem *> _get_edited_canvas_items(bool retrieve_locked = false, bool remove_canvas_item_if_parent_in_selection = true) const;
Rect2 _get_encompassing_rect_from_list(List<CanvasItem *> p_list);
void _expand_encompassing_rect_using_children(Rect2 &r_rect, const Node *p_node, bool &r_first, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D(), bool include_locked_nodes = true);
Rect2 _get_encompassing_rect(const Node *p_node);
Expand Down Expand Up @@ -552,6 +552,8 @@ class CanvasItemEditor : public VBoxContainer {
void focus_selection();
void center_at(const Point2 &p_pos);

virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;

EditorSelection *editor_selection = nullptr;

CanvasItemEditor();
Expand Down

0 comments on commit d065281

Please sign in to comment.