瀏覽代碼

Allow overriding 2D editor cursor

kobewi 5 月之前
父節點
當前提交
efbc5aabe1

+ 72 - 30
editor/plugins/camera_2d_editor_plugin.cpp

@@ -40,13 +40,19 @@
 #include "scene/gui/menu_button.h"
 
 void Camera2DEditor::edit(Camera2D *p_camera) {
-if (p_camera == selected_camera) {
+	if (p_camera == selected_camera) {
 		return;
 	}
 	const Callable update_overlays = callable_mp(plugin, &EditorPlugin::update_overlays);
 
 	if (selected_camera) {
 		selected_camera->disconnect(SceneStringName(draw), update_overlays);
+		if (drag_type != Drag::NONE) {
+			selected_camera->set_limit_rect(drag_revert);
+		}
+		drag_type = Drag::NONE;
+		hover_type = Drag::NONE;
+		CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_ARROW);
 	}
 	selected_camera = p_camera;
 
@@ -65,32 +71,13 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
 	if (mb.is_valid()) {
 		if (mb->get_button_index() == MouseButton::LEFT) {
 			if (mb->is_pressed()) {
-				const Rect2 limit_rect = selected_camera->get_limit_rect();
-				const Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position());
-				drag_revert = limit_rect;
-
-				if (pos.y > limit_rect.position.y && pos.y < limit_rect.get_end().y) {
-					if (Math::abs(pos.x - limit_rect.position.x) < 8) {
-						drag_type = Drag::LEFT;
-						return true;
-					} else if (Math::abs(pos.x - limit_rect.get_end().x) < 8) {
-						drag_type = Drag::RIGHT;
-						return true;
-					}
-				} else if (pos.x > limit_rect.position.x && pos.x < limit_rect.get_end().x) {
-					if (Math::abs(pos.y - limit_rect.position.y) < 8) {
-						drag_type = Drag::TOP;
-						return true;
-					} else if (Math::abs(pos.y - limit_rect.get_end().y) < 8) {
-						drag_type = Drag::BOTTOM;
-						return true;
-					}
-				}
+				if (hover_type != Drag::NONE) {
+					Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position());
+					const Rect2 limit_rect = selected_camera->get_limit_rect();
 
-				if (limit_rect.has_point(pos)) {
-					drag_type = Drag::CENTER;
+					drag_type = hover_type;
+					drag_revert = selected_camera->get_limit_rect();
 					center_drag_point = pos - limit_rect.position;
-					plugin->update_overlays();
 					return true;
 				}
 			} else if (drag_type != Drag::NONE) {
@@ -109,18 +96,21 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
 			selected_camera->set_limit_rect(drag_revert);
 			drag_type = Drag::NONE;
 			plugin->update_overlays();
+			_update_hover(mb->get_position());
 			return true;
 		}
 		return false;
 	}
 
-	if (drag_type == Drag::NONE) {
-		return false;
-	}
-
 	Ref<InputEventMouseMotion> mm = p_event;
 	if (mm.is_valid()) {
-		Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mm->get_position());
+		Vector2 pos = mm->get_position();
+		if (drag_type == Drag::NONE) {
+			_update_hover(pos);
+			return false;
+		}
+
+		pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos);
 		pos = CanvasItemEditor::get_singleton()->snap_point(pos);
 
 		switch (drag_type) {
@@ -150,6 +140,9 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
 				selected_camera->set_limit_rect(target_rect);
 				plugin->update_overlays();
 			} break;
+
+			case Drag::NONE: {
+			} break;
 		}
 		return true;
 	}
@@ -193,6 +186,55 @@ void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) {
 	}
 }
 
+void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) {
+	if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
+		hover_type = Drag::NONE;
+		CanvasItemEditor::get_singleton()->set_cursor_shape_override();
+		return;
+	}
+
+	const Rect2 limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(selected_camera->get_limit_rect());
+	const float drag_tolerance = 8.0;
+
+	hover_type = Drag::NONE;
+	if (p_mouse_pos.y > limit_rect.position.y && p_mouse_pos.y < limit_rect.get_end().y) {
+		if (Math::abs(p_mouse_pos.x - limit_rect.position.x) < drag_tolerance) {
+			hover_type = Drag::LEFT;
+		} else if (Math::abs(p_mouse_pos.x - limit_rect.get_end().x) < drag_tolerance) {
+			hover_type = Drag::RIGHT;
+		}
+	} else if (p_mouse_pos.x > limit_rect.position.x && p_mouse_pos.x < limit_rect.get_end().x) {
+		if (Math::abs(p_mouse_pos.y - limit_rect.position.y) < drag_tolerance) {
+			hover_type = Drag::TOP;
+		} else if (Math::abs(p_mouse_pos.y - limit_rect.get_end().y) < drag_tolerance) {
+			hover_type = Drag::BOTTOM;
+		}
+	}
+
+	/* Temporarily disabled, because it needs more changes.
+	if (hover_type == Drag::NONE && limit_rect.has_point(p_mouse_pos)) {
+		hover_type = Drag::CENTER;
+	}
+	*/
+
+	switch (hover_type) {
+		case Drag::NONE: {
+			CanvasItemEditor::get_singleton()->set_cursor_shape_override();
+		} break;
+		case Drag::LEFT:
+		case Drag::RIGHT: {
+			CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_HSIZE);
+		} break;
+		case Drag::TOP:
+		case Drag::BOTTOM: {
+			CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_VSIZE);
+		} break;
+		case Drag::CENTER: {
+			CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_MOVE);
+		} break;
+	}
+}
+
 void Camera2DEditor::_notification(int p_what) {
 	switch (p_what) {
 		case NOTIFICATION_THEME_CHANGED: {

+ 5 - 1
editor/plugins/camera_2d_editor_plugin.h

@@ -52,7 +52,10 @@ class Camera2DEditor : public Control {
 		RIGHT,
 		BOTTOM,
 		CENTER,
-	} drag_type;
+	};
+	Drag drag_type = Drag::NONE;
+	Drag hover_type = Drag::NONE;
+
 	Rect2 drag_revert;
 	Vector2 center_drag_point;
 
@@ -64,6 +67,7 @@ class Camera2DEditor : public Control {
 	void _menu_option(int p_option);
 	void _snap_limits_to_viewport(Camera2D *p_camera);
 	void _update_overlays_if_needed(Camera2D *p_camera);
+	void _update_hover(const Vector2 &p_mouse_pos);
 
 protected:
 	static void _bind_methods();

+ 13 - 0
editor/plugins/canvas_item_editor_plugin.cpp

@@ -2802,6 +2802,11 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
 }
 
 void CanvasItemEditor::_update_cursor() {
+	if (cursor_shape_override != CURSOR_ARROW) {
+		set_default_cursor_shape(cursor_shape_override);
+		return;
+	}
+
 	// Choose the correct default cursor.
 	CursorShape c = CURSOR_ARROW;
 	switch (tool) {
@@ -2865,6 +2870,14 @@ void CanvasItemEditor::_update_lock_and_group_button() {
 	ungroup_button->set_disabled(!has_canvas_item);
 }
 
+void CanvasItemEditor::set_cursor_shape_override(CursorShape p_shape) {
+	if (cursor_shape_override == p_shape) {
+		return;
+	}
+	cursor_shape_override = p_shape;
+	_update_cursor();
+}
+
 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 };

+ 2 - 0
editor/plugins/canvas_item_editor_plugin.h

@@ -368,6 +368,7 @@ private:
 	Transform2D original_transform;
 
 	Point2 box_selecting_to;
+	CursorShape cursor_shape_override = CURSOR_ARROW;
 
 	Ref<StyleBoxTexture> select_sb;
 	Ref<Texture2D> select_handle;
@@ -584,6 +585,7 @@ public:
 	void focus_selection();
 	void center_at(const Point2 &p_pos);
 
+	void set_cursor_shape_override(CursorShape p_shape = CURSOR_ARROW);
 	virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
 
 	ThemePreviewMode get_theme_preview() const { return theme_preview; }

+ 4 - 4
scene/2d/camera_2d.cpp

@@ -564,16 +564,16 @@ void Camera2D::_make_current(Object *p_which) {
 	}
 }
 
-void Camera2D::set_limit_rect(const Rect2 &p_limit_rect) {
-	const Point2 limit_rect_end = p_limit_rect.get_end();
+void Camera2D::set_limit_rect(const Rect2i &p_limit_rect) {
+	const Point2i limit_rect_end = p_limit_rect.get_end();
 	set_limit(SIDE_LEFT, p_limit_rect.position.x);
 	set_limit(SIDE_TOP, p_limit_rect.position.y);
 	set_limit(SIDE_RIGHT, limit_rect_end.x);
 	set_limit(SIDE_BOTTOM, limit_rect_end.y);
 }
 
-Rect2 Camera2D::get_limit_rect() const {
-	return Rect2(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
+Rect2i Camera2D::get_limit_rect() const {
+	return Rect2i(limit[SIDE_LEFT], limit[SIDE_TOP], limit[SIDE_RIGHT] - limit[SIDE_LEFT], limit[SIDE_BOTTOM] - limit[SIDE_TOP]);
 }
 
 void Camera2D::make_current() {

+ 2 - 2
scene/2d/camera_2d.h

@@ -121,8 +121,8 @@ protected:
 	static void _bind_methods();
 
 public:
-	void set_limit_rect(const Rect2 &p_limit_rect);
-	Rect2 get_limit_rect() const;
+	void set_limit_rect(const Rect2i &p_limit_rect);
+	Rect2i get_limit_rect() const;
 
 	void set_offset(const Vector2 &p_offset);
 	Vector2 get_offset() const;