0.2.0-dev Download ZIP
345 lines ·12.7 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* canvas_item_manipulator.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #ifdef DEBUG_ENABLED |
| 34 | #include "core/input/input.h" |
| 35 | #include "core/object/ref_counted.h" |
| 36 | #include "scene/debugger/debugger_helpers.h" |
| 37 | |
| 38 | class CanvasItem; |
| 39 | class Control; |
| 40 | class InputEvent; |
| 41 | class Node; |
| 42 | class Shortcut; |
| 43 | class Viewport; |
| 44 | |
| 45 | class CanvasItemManipulator : public RefCounted { |
| 46 | GDCLASS(CanvasItemManipulator, RefCounted); |
| 47 | |
| 48 | public: |
| 49 | enum Tool { |
| 50 | TOOL_SELECT, |
| 51 | TOOL_SCENE_PAINT, |
| 52 | TOOL_LIST_SELECT, |
| 53 | TOOL_MOVE, |
| 54 | TOOL_SCALE, |
| 55 | TOOL_ROTATE, |
| 56 | TOOL_EDIT_PIVOT, |
| 57 | TOOL_PAN, |
| 58 | TOOL_RULER, |
| 59 | TOOL_MAX, |
| 60 | }; |
| 61 | |
| 62 | enum DragType { |
| 63 | DRAG_NONE, |
| 64 | DRAG_BOX_SELECTION, |
| 65 | DRAG_LEFT, |
| 66 | DRAG_TOP_LEFT, |
| 67 | DRAG_TOP, |
| 68 | DRAG_TOP_RIGHT, |
| 69 | DRAG_RIGHT, |
| 70 | DRAG_BOTTOM_RIGHT, |
| 71 | DRAG_BOTTOM, |
| 72 | DRAG_BOTTOM_LEFT, |
| 73 | DRAG_ANCHOR_TOP_LEFT, |
| 74 | DRAG_ANCHOR_TOP_RIGHT, |
| 75 | DRAG_ANCHOR_BOTTOM_RIGHT, |
| 76 | DRAG_ANCHOR_BOTTOM_LEFT, |
| 77 | DRAG_ANCHOR_ALL, |
| 78 | DRAG_QUEUED, |
| 79 | DRAG_MOVE, |
| 80 | DRAG_MOVE_X, |
| 81 | DRAG_MOVE_Y, |
| 82 | DRAG_SCALE_X, |
| 83 | DRAG_SCALE_Y, |
| 84 | DRAG_SCALE_BOTH, |
| 85 | DRAG_ROTATE, |
| 86 | DRAG_PIVOT, |
| 87 | DRAG_TEMP_PIVOT, |
| 88 | DRAG_V_GUIDE, |
| 89 | DRAG_H_GUIDE, |
| 90 | DRAG_DOUBLE_GUIDE, |
| 91 | DRAG_KEY_MOVE, |
| 92 | }; |
| 93 | |
| 94 | constexpr static real_t DRAG_THRESHOLD = 8; |
| 95 | #ifdef TOOLS_ENABLED |
| 96 | constexpr static real_t GIZMO_HANDLE_DISTANCE = 25; |
| 97 | constexpr static Rect2 GIZMO_HANDLE_X_RECT = Rect2(GIZMO_HANDLE_DISTANCE, -5, 10, 10); |
| 98 | constexpr static Rect2 GIZMO_HANDLE_Y_RECT = Rect2(-5, GIZMO_HANDLE_DISTANCE, 10, 10); |
| 99 | |
| 100 | constexpr static real_t RESIZE_HANDLE_DIAMETER = 10; |
| 101 | |
| 102 | constexpr static Size2 ANCHOR_HANDLE_SIZE = Size2(16, 16); |
| 103 | |
| 104 | enum ShortcutName { |
| 105 | SHORTCUT_CANCEL_TRANSFORM, |
| 106 | SHORTCUT_MAX, |
| 107 | }; |
| 108 | |
| 109 | enum SnapTarget { |
| 110 | SNAP_TARGET_NONE, |
| 111 | SNAP_TARGET_PARENT, |
| 112 | SNAP_TARGET_SELF_ANCHORS, |
| 113 | SNAP_TARGET_SELF, |
| 114 | SNAP_TARGET_OTHER_NODE, |
| 115 | SNAP_TARGET_GUIDE, |
| 116 | SNAP_TARGET_GRID, |
| 117 | SNAP_TARGET_PIXEL, |
| 118 | }; |
| 119 | |
| 120 | enum SnapMode { |
| 121 | SNAP_GRID = 1 << 0, |
| 122 | SNAP_GUIDES = 1 << 1, |
| 123 | SNAP_PIXEL = 1 << 2, |
| 124 | SNAP_NODE_PARENT = 1 << 3, |
| 125 | SNAP_NODE_ANCHORS = 1 << 4, |
| 126 | SNAP_NODE_SIDES = 1 << 5, |
| 127 | SNAP_NODE_CENTER = 1 << 6, |
| 128 | SNAP_OTHER_NODES = 1 << 7, |
| 129 | |
| 130 | SNAP_DEFAULT = SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL, |
| 131 | }; |
| 132 | #endif |
| 133 | |
| 134 | private: |
| 135 | StringName update_canvas_signal = "update_canvas_requested"; |
| 136 | |
| 137 | Tool tool = TOOL_SELECT; |
| 138 | DragType drag_type = DRAG_NONE; |
| 139 | Input::CursorShape cursor_shape = Input::CursorShape::CURSOR_ARROW; |
| 140 | bool editor_mode = false; // Set it manually instead of using the hint, to allow debugging the editor itself. |
| 141 | |
| 142 | real_t grab_distance = 1; |
| 143 | real_t scale = 1; |
| 144 | real_t zoom = 1; |
| 145 | Point2 view_offset; |
| 146 | Viewport *viewport = nullptr; |
| 147 | |
| 148 | Point2 box_selecting_to; |
| 149 | Point2 drag_start_origin; |
| 150 | |
| 151 | Point2 drag_from; |
| 152 | Point2 drag_to; |
| 153 | Point2 drag_rotation_center; |
| 154 | Point2 temp_pivot = Point2(Math::INF, Math::INF); |
| 155 | Array drag_selection; |
| 156 | |
| 157 | Callable find_items_start_callback; |
| 158 | Callable point_selected_callback; |
| 159 | Callable get_selection_callback; |
| 160 | Callable global_transform_callback; |
| 161 | |
| 162 | bool _gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event); |
| 163 | bool _gui_input_select(const Ref<InputEvent> &p_event); |
| 164 | |
| 165 | Rect2 _encompass_selection_rect(const Array &p_selected); |
| 166 | |
| 167 | void _update_cursor_shape(); |
| 168 | |
| 169 | #ifdef TOOLS_ENABLED |
| 170 | bool local_space = false; |
| 171 | bool show_transformation_gizmos = false; |
| 172 | bool anchors_mode = false; |
| 173 | |
| 174 | int snap_mode = SNAP_DEFAULT; |
| 175 | bool smart_snap = false; |
| 176 | bool snap_relative = false; |
| 177 | bool snap_rotation = false; |
| 178 | real_t snap_rotation_step = 0; |
| 179 | real_t snap_rotation_offset = 0; |
| 180 | bool snap_scale = false; |
| 181 | real_t snap_scale_step = 0; |
| 182 | Transform2D snap_transform; |
| 183 | SnapTarget snap_target[2]; |
| 184 | |
| 185 | bool grid_snap = false; |
| 186 | Point2 grid_step; |
| 187 | int grid_step_multiplier = 0; |
| 188 | Point2 grid_offset; |
| 189 | |
| 190 | bool ruler_tool_active = false; |
| 191 | Point2 ruler_tool_origin; |
| 192 | |
| 193 | bool show_guides = false; |
| 194 | bool show_rulers = false; |
| 195 | bool is_hovering_h_guide = false; |
| 196 | bool is_hovering_v_guide = false; |
| 197 | Point2 dragged_guide_pos; |
| 198 | int dragged_guide_index = -1; |
| 199 | real_t ruler_width = 0; |
| 200 | |
| 201 | HashMap<int, Ref<Shortcut>> inputs; |
| 202 | |
| 203 | Callable local_transform_callback; |
| 204 | Callable local_mouse_pos_callback; |
| 205 | Callable plugin_input_callback; |
| 206 | |
| 207 | bool _gui_input_rulers_and_guides(const Ref<InputEvent> &p_event); |
| 208 | bool _gui_input_scale(const Ref<InputEvent> &p_event); |
| 209 | bool _gui_input_pivot(const Ref<InputEvent> &p_event); |
| 210 | bool _gui_input_resize(const Ref<InputEvent> &p_event); |
| 211 | bool _gui_input_rotate(const Ref<InputEvent> &p_event); |
| 212 | bool _gui_input_move(const Ref<InputEvent> &p_event); |
| 213 | bool _gui_input_anchors(const Ref<InputEvent> &p_event); |
| 214 | bool _gui_input_ruler_tool(const Ref<InputEvent> &p_event); |
| 215 | bool _gui_input_cancel_drag(const Ref<InputEvent> &p_event, bool p_reset_snap_targets = false, bool p_restore_bones = false); |
| 216 | |
| 217 | bool _validate_drag_selection(); |
| 218 | |
| 219 | void _snap_if_closer_float( |
| 220 | const real_t p_value, |
| 221 | real_t &r_current_snap, SnapTarget &r_current_snap_target, |
| 222 | const real_t p_target_value, const SnapTarget p_snap_target, |
| 223 | const real_t p_radius = 10.0); |
| 224 | void _snap_if_closer_point( |
| 225 | Point2 p_value, |
| 226 | Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2], |
| 227 | Point2 p_target_value, const SnapTarget p_snap_target, |
| 228 | const real_t rotation = 0.0, |
| 229 | const real_t p_radius = 10.0); |
| 230 | void _snap_other_nodes( |
| 231 | const Point2 p_value, |
| 232 | const Transform2D p_transform_to_snap, |
| 233 | Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2], |
| 234 | const SnapTarget p_snap_target, const Array &p_exceptions, |
| 235 | const Node *p_current); |
| 236 | |
| 237 | void _emit_locked_warn_signal(); |
| 238 | #endif |
| 239 | |
| 240 | protected: |
| 241 | static void _bind_methods(); |
| 242 | |
| 243 | public: |
| 244 | bool gui_input(const Ref<InputEvent> &p_event); |
| 245 | |
| 246 | void get_canvas_items_at_pos(const Point2 &p_pos, Vector<DebuggerHelpers::SelectResult> &r_items, bool p_allow_locked = false); |
| 247 | void find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D()); |
| 248 | void find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D()); |
| 249 | |
| 250 | Input::CursorShape get_cursor_shape() const { return cursor_shape; } |
| 251 | |
| 252 | void reset_drag(); |
| 253 | |
| 254 | void set_editor_mode_enabled(bool p_enabled) { editor_mode = p_enabled; } |
| 255 | |
| 256 | void set_tool(Tool p_tool); |
| 257 | void set_drag_type(DragType p_type) { drag_type = p_type; } |
| 258 | void set_grab_distance(const real_t p_distance) { grab_distance = p_distance; } |
| 259 | void set_scale(const real_t p_scale) { scale = p_scale; } |
| 260 | void set_zoom(const real_t p_zoom) { zoom = p_zoom; } |
| 261 | void set_view_offset(const Point2 p_offset) { view_offset = p_offset; } |
| 262 | void set_viewport(Viewport *p_viewport) { viewport = p_viewport; } |
| 263 | |
| 264 | void set_callbacks(const Callable &p_find_items_start, const Callable &p_point_selected, const Callable &p_get_selection, const Callable &p_local_xform, const Callable &p_global_xform, const Callable &p_local_mouse_pos, const Callable &p_plugin_input); |
| 265 | |
| 266 | static bool is_node_locked(const Node *p_node); |
| 267 | |
| 268 | Tool get_tool() const { return tool; } |
| 269 | DragType get_drag_type() const { return drag_type; } |
| 270 | |
| 271 | Point2 get_drag_from() const { return drag_from; } |
| 272 | |
| 273 | #ifdef TOOLS_ENABLED |
| 274 | 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 Array &p_other_nodes_exceptions = Array()); |
| 275 | static Point2 anchor_to_position(const Control *p_control, const Point2 p_anchor); |
| 276 | static Point2 position_to_anchor(const Control *p_control, const Point2 &p_position); |
| 277 | |
| 278 | void commit_drag(); |
| 279 | |
| 280 | bool reset_temp_pivot(); |
| 281 | void center_temp_pivot_on_selection(); |
| 282 | |
| 283 | void set_local_space_enabled(bool p_enabled) { local_space = p_enabled; } |
| 284 | void set_anchors_mode_enabled(bool p_enabled) { anchors_mode = p_enabled; } |
| 285 | |
| 286 | void set_grid_snap_enabled(bool p_enabled) { grid_snap = p_enabled; } |
| 287 | void set_grid_step(const Point2 &p_step) { grid_step = p_step; } |
| 288 | void set_grid_step_multiplier(const int &p_multiplier) { grid_step_multiplier = p_multiplier; } |
| 289 | void set_grid_offset(const Point2 p_offset) { grid_offset = p_offset; } |
| 290 | |
| 291 | void set_snap_mode(const int p_flag) { snap_mode = p_flag; } |
| 292 | void set_smart_snap_enabled(bool p_enabled) { smart_snap = p_enabled; } |
| 293 | void set_snap_relative_enabled(bool p_enabled) { snap_relative = p_enabled; } |
| 294 | void set_snap_rotation_enabled(bool p_enabled) { snap_rotation = p_enabled; } |
| 295 | void set_snap_rotation_step(const real_t p_step) { snap_rotation_step = p_step; } |
| 296 | void set_snap_rotation_offset(const real_t p_offset) { snap_rotation_offset = p_offset; } |
| 297 | void set_snap_scale_enabled(bool p_enabled) { snap_scale = p_enabled; } |
| 298 | void set_snap_scale_step(const real_t p_step) { snap_scale_step = p_step; } |
| 299 | |
| 300 | void set_show_transformation_gizmos(bool p_enabled) { show_transformation_gizmos = p_enabled; } |
| 301 | void set_show_guides(bool p_show) { show_guides = p_show; } |
| 302 | void set_show_rulers(bool p_show) { show_rulers = p_show; } |
| 303 | void set_ruler_width(const real_t p_width) { ruler_width = p_width; } |
| 304 | |
| 305 | void set_shortcut(const ShortcutName p_name, const Ref<Shortcut> &p_shortcut); |
| 306 | |
| 307 | bool is_node_movable(const Node *p_node, bool p_popup_warning = false); |
| 308 | |
| 309 | Point2 get_drag_to() const { return drag_to; } |
| 310 | Point2 get_drag_rotation_center() const { return drag_rotation_center; } |
| 311 | Array get_drag_selection() const { return drag_selection.duplicate(); } |
| 312 | |
| 313 | bool is_local_space_enabled() { return local_space; } |
| 314 | |
| 315 | Point2 get_temp_pivot() const { return temp_pivot; } |
| 316 | |
| 317 | bool is_grid_snap_enabled() { return grid_snap; } |
| 318 | Point2 get_grid_step() const { return grid_step; } |
| 319 | Point2 get_grid_offset() const { return grid_offset; } |
| 320 | int get_grid_step_multiplier() const { return grid_step_multiplier; } |
| 321 | |
| 322 | int get_snap_mode() const { return snap_mode; } |
| 323 | bool is_smart_snap_enabled() { return smart_snap; } |
| 324 | bool is_snap_relative_enabled() { return snap_relative; } |
| 325 | bool is_snap_rotation_enabled() { return snap_rotation; } |
| 326 | real_t get_snap_rotation_step() { return snap_rotation_step; } |
| 327 | real_t get_snap_rotation_offset() { return snap_rotation_offset; } |
| 328 | bool is_snap_scale_enabled() { return snap_scale; } |
| 329 | real_t get_snap_scale_step() { return snap_scale_step; } |
| 330 | Transform2D get_snap_transform() const { return snap_transform; } |
| 331 | Pair<SnapTarget, SnapTarget> get_snap_target() const { return Pair(snap_target[0], snap_target[1]); } |
| 332 | |
| 333 | Point2 get_dragged_guide_position() const { return dragged_guide_pos; } |
| 334 | int get_dragged_guide_index() const { return dragged_guide_index; } |
| 335 | |
| 336 | bool is_showing_transformation_gizmos() { return show_transformation_gizmos; } |
| 337 | bool is_showing_guides() { return show_guides; } |
| 338 | bool is_showing_rulers() { return show_rulers; } |
| 339 | real_t get_ruler_width() const { return ruler_width; } |
| 340 | |
| 341 | bool is_ruler_tool_active() { return ruler_tool_active; } |
| 342 | Point2 get_ruler_tool_origin() const { return ruler_tool_origin; } |
| 343 | #endif |
| 344 | }; |
| 345 | #endif // DEBUG_ENABLED |