From e187599ed54de73c420bad4ee9a7629edca4f7ef Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Sat, 4 Jan 2025 02:36:16 +0100 Subject: [PATCH] Add EditorInterface methods to get the 2D current grid snapping options The following methods are available: - `EditorInterface.is_2d_editor_grid_snap_enabled()` - `EditorInterface.is_2d_editor_smart_snap_enabled()` - `EditorInterface.is_2d_editor_snap_relative_enabled()` - `EditorInterface.is_2d_editor_snap_rotation_enabled()` - `EditorInterface.is_2d_editor_snap_scale_enabled()` - `EditorInterface.get_2d_editor_grid_offset()` - `EditorInterface.get_2d_editor_grid_primary_steps()` - `EditorInterface.get_2d_editor_grid_step()` - `EditorInterface.get_2d_editor_snap_rotation_offset()` - `EditorInterface.get_2d_editor_snap_rotation_step()` - `EditorInterface.get_2d_editor_snap_scale_step()` --- doc/classes/EditorInterface.xml | 66 ++++++++++++++++++++++ editor/editor_interface.cpp | 60 ++++++++++++++++++++ editor/editor_interface.h | 13 +++++ editor/plugins/canvas_item_editor_plugin.h | 14 +++++ 4 files changed, 153 insertions(+) diff --git a/doc/classes/EditorInterface.xml b/doc/classes/EditorInterface.xml index d4acb820232..7722212d73a 100644 --- a/doc/classes/EditorInterface.xml +++ b/doc/classes/EditorInterface.xml @@ -43,6 +43,42 @@ Edits the given [Script]. The line and column on which to open the script can also be specified. The script will be open with the user-configured editor for the script's language which may be an external editor. + + + + Returns the current 2D editor grid offset in pixels. This affects grid snapping when enabled (see [method is_2d_editor_grid_snap_enabled]). + + + + + + Returns the current 2D editor grid offset in pixels. + + + + + + Returns the current 2D editor grid step in pixels. This takes the temporary multiplier into account (when pressing the shortcuts that multiply or divide the grid size by 2). This affects grid snapping when enabled (see [method is_2d_editor_grid_snap_enabled]). + + + + + + Returns the current 2D editor snap rotation offset in radians. + + + + + + Returns the current 2D editor snap rotation step in radians. + + + + + + Returns the current 2D editor snap scale step. + + @@ -208,6 +244,36 @@ Shows the given property on the given [param object] in the editor's Inspector dock. If [param inspector_only] is [code]true[/code], plugins will not attempt to edit [param object]. + + + + Returns [code]true[/code] if grid snapping is enabled in the 2D editor, [code]false[/code] otherwise. See also [method get_2d_editor_grid_step] and [method get_2d_editor_grid_offset]. + + + + + + Returns [code]true[/code] if smart snapping is enabled in the 2D editor, [code]false[/code] otherwise. + + + + + + Returns [code]true[/code] if relative snapping is enabled in the 2D editor, [code]false[/code] otherwise (absolute snapping is used instead). + + + + + + Returns [code]true[/code] if rotation snapping is enabled in the 2D editor, [code]false[/code] otherwise. + + + + + + Returns [code]true[/code] if scale snapping is enabled in the 2D editor, [code]false[/code] otherwise. + + diff --git a/editor/editor_interface.cpp b/editor/editor_interface.cpp index c0d7b1b1fa7..94afe9f0ef1 100644 --- a/editor/editor_interface.cpp +++ b/editor/editor_interface.cpp @@ -48,6 +48,7 @@ #include "editor/gui/editor_toaster.h" #include "editor/gui/scene_tree_editor.h" #include "editor/inspector_dock.h" +#include "editor/plugins/canvas_item_editor_plugin.h" #include "editor/plugins/node_3d_editor_plugin.h" #include "editor/property_selector.h" #include "editor/themes/editor_scale.h" @@ -395,6 +396,50 @@ SubViewport *EditorInterface::get_editor_viewport_3d(int p_idx) const { return Node3DEditor::get_singleton()->get_editor_viewport(p_idx)->get_viewport_node(); } +Vector2 EditorInterface::get_2d_editor_grid_offset() const { + return CanvasItemEditor::get_singleton()->get_grid_offset(); +} + +bool EditorInterface::is_2d_editor_grid_snap_enabled() const { + return CanvasItemEditor::get_singleton()->is_grid_snap_enabled(); +} + +bool EditorInterface::is_2d_editor_smart_snap_enabled() const { + return CanvasItemEditor::get_singleton()->is_smart_snap_enabled(); +} + +bool EditorInterface::is_2d_editor_snap_relative_enabled() const { + return CanvasItemEditor::get_singleton()->is_snap_relative_enabled(); +} + +bool EditorInterface::is_2d_editor_snap_rotation_enabled() const { + return CanvasItemEditor::get_singleton()->is_snap_rotation_enabled(); +} + +bool EditorInterface::is_2d_editor_snap_scale_enabled() const { + return CanvasItemEditor::get_singleton()->is_snap_scale_enabled(); +} + +Vector2 EditorInterface::get_2d_editor_grid_step() const { + return CanvasItemEditor::get_singleton()->get_grid_step(); +} + +Vector2i EditorInterface::get_2d_editor_grid_primary_steps() const { + return CanvasItemEditor::get_singleton()->get_grid_primary_steps(); +} + +float EditorInterface::get_2d_editor_snap_rotation_step() const { + return CanvasItemEditor::get_singleton()->get_snap_rotation_step(); +} + +float EditorInterface::get_2d_editor_snap_rotation_offset() const { + return CanvasItemEditor::get_singleton()->get_snap_rotation_offset(); +} + +float EditorInterface::get_2d_editor_snap_scale_step() const { + return CanvasItemEditor::get_singleton()->get_snap_scale_step(); +} + void EditorInterface::set_main_screen_editor(const String &p_name) { EditorNode::get_singleton()->get_editor_main_screen()->select_by_name(p_name); } @@ -851,6 +896,21 @@ void EditorInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("is_movie_maker_enabled"), &EditorInterface::is_movie_maker_enabled); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_maker_enabled"), "set_movie_maker_enabled", "is_movie_maker_enabled"); + + // 2D (canvas) editor. + + ClassDB::bind_method(D_METHOD("is_2d_editor_grid_snap_enabled"), &EditorInterface::is_2d_editor_grid_snap_enabled); + ClassDB::bind_method(D_METHOD("is_2d_editor_smart_snap_enabled"), &EditorInterface::is_2d_editor_smart_snap_enabled); + ClassDB::bind_method(D_METHOD("is_2d_editor_snap_relative_enabled"), &EditorInterface::is_2d_editor_snap_relative_enabled); + ClassDB::bind_method(D_METHOD("is_2d_editor_snap_rotation_enabled"), &EditorInterface::is_2d_editor_snap_rotation_enabled); + ClassDB::bind_method(D_METHOD("is_2d_editor_snap_scale_enabled"), &EditorInterface::is_2d_editor_snap_scale_enabled); + + ClassDB::bind_method(D_METHOD("get_2d_editor_grid_offset"), &EditorInterface::get_2d_editor_grid_offset); + ClassDB::bind_method(D_METHOD("get_2d_editor_grid_step"), &EditorInterface::get_2d_editor_grid_step); + ClassDB::bind_method(D_METHOD("get_2d_editor_grid_primary_steps"), &EditorInterface::get_2d_editor_grid_primary_steps); + ClassDB::bind_method(D_METHOD("get_2d_editor_snap_rotation_step"), &EditorInterface::get_2d_editor_snap_rotation_step); + ClassDB::bind_method(D_METHOD("get_2d_editor_snap_rotation_offset"), &EditorInterface::get_2d_editor_snap_rotation_offset); + ClassDB::bind_method(D_METHOD("get_2d_editor_snap_scale_step"), &EditorInterface::get_2d_editor_snap_scale_step); } void EditorInterface::create() { diff --git a/editor/editor_interface.h b/editor/editor_interface.h index b6d163d2fdd..f1e7ab55def 100644 --- a/editor/editor_interface.h +++ b/editor/editor_interface.h @@ -190,6 +190,19 @@ public: void set_movie_maker_enabled(bool p_enabled); bool is_movie_maker_enabled() const; + bool is_2d_editor_grid_snap_enabled() const; + bool is_2d_editor_smart_snap_enabled() const; + bool is_2d_editor_snap_relative_enabled() const; + bool is_2d_editor_snap_rotation_enabled() const; + bool is_2d_editor_snap_scale_enabled() const; + + Vector2 get_2d_editor_grid_offset() const; + Vector2 get_2d_editor_grid_step() const; + Vector2i get_2d_editor_grid_primary_steps() const; + float get_2d_editor_snap_rotation_step() const; + float get_2d_editor_snap_rotation_offset() const; + float get_2d_editor_snap_scale_step() const; + #ifdef TOOLS_ENABLED virtual void get_argument_options(const StringName &p_function, int p_idx, List *r_options) const override; #endif diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index 1581db93f8c..d8147905b0d 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -553,6 +553,20 @@ public: Point2 snap_point(Point2 p_target, unsigned int p_modes = SNAP_DEFAULT, unsigned int p_forced_modes = 0, const CanvasItem *p_self_canvas_item = nullptr, const List &p_other_nodes_exceptions = List()); real_t snap_angle(real_t p_target, real_t p_start = 0) const; + bool is_grid_snap_enabled() const { return grid_snap_active; } + bool is_smart_snap_enabled() const { return smart_snap_active; } + bool is_snap_relative_enabled() const { return snap_relative; } + bool is_snap_rotation_enabled() const { return snap_rotation; } + bool is_snap_scale_enabled() const { return snap_scale; } + + Point2 get_grid_offset() const { return grid_offset; } + Point2 get_grid_step() const { return grid_step * Math::pow(2.0, grid_step_multiplier); } + Vector2i get_grid_primary_steps() const { return primary_grid_step; } + + real_t get_snap_rotation_step() const { return snap_rotation_step; } + real_t get_snap_rotation_offset() const { return snap_rotation_offset; } + real_t get_snap_scale_step() const { return snap_scale_step; } + Transform2D get_canvas_transform() const { return transform; } static CanvasItemEditor *get_singleton() { return singleton; }