canvas_item_editor_plugin.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /**************************************************************************/
  2. /* canvas_item_editor_plugin.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. #ifndef CANVAS_ITEM_EDITOR_PLUGIN_H
  31. #define CANVAS_ITEM_EDITOR_PLUGIN_H
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/2d/canvas_item.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/panel_container.h"
  38. class CanvasItemEditorViewport;
  39. class CanvasItemEditorSelectedItem : public Object {
  40. GDCLASS(CanvasItemEditorSelectedItem, Object);
  41. public:
  42. Transform2D prev_xform;
  43. float prev_rot;
  44. Rect2 prev_rect;
  45. Vector2 prev_pivot;
  46. float prev_anchors[4];
  47. Transform2D pre_drag_xform;
  48. Rect2 pre_drag_rect;
  49. List<float> pre_drag_bones_length;
  50. List<Dictionary> pre_drag_bones_undo_state;
  51. Dictionary undo_state;
  52. CanvasItemEditorSelectedItem() :
  53. prev_anchors() {
  54. prev_rot = 0;
  55. }
  56. };
  57. class CanvasItemEditor : public VBoxContainer {
  58. GDCLASS(CanvasItemEditor, VBoxContainer);
  59. public:
  60. enum Tool {
  61. TOOL_SELECT,
  62. TOOL_LIST_SELECT,
  63. TOOL_MOVE,
  64. TOOL_SCALE,
  65. TOOL_ROTATE,
  66. TOOL_EDIT_PIVOT,
  67. TOOL_PAN,
  68. TOOL_RULER,
  69. TOOL_MAX
  70. };
  71. enum AddNodeOption {
  72. ADD_NODE,
  73. ADD_INSTANCE,
  74. };
  75. private:
  76. EditorNode *editor;
  77. enum SnapTarget {
  78. SNAP_TARGET_NONE = 0,
  79. SNAP_TARGET_PARENT,
  80. SNAP_TARGET_SELF_ANCHORS,
  81. SNAP_TARGET_SELF,
  82. SNAP_TARGET_OTHER_NODE,
  83. SNAP_TARGET_GUIDE,
  84. SNAP_TARGET_GRID,
  85. SNAP_TARGET_PIXEL
  86. };
  87. enum MenuOption {
  88. SNAP_USE,
  89. SNAP_USE_NODE_PARENT,
  90. SNAP_USE_NODE_ANCHORS,
  91. SNAP_USE_NODE_SIDES,
  92. SNAP_USE_NODE_CENTER,
  93. SNAP_USE_OTHER_NODES,
  94. SNAP_USE_GRID,
  95. SNAP_USE_GUIDES,
  96. SNAP_USE_ROTATION,
  97. SNAP_USE_SCALE,
  98. SNAP_RELATIVE,
  99. SNAP_CONFIGURE,
  100. SNAP_USE_PIXEL,
  101. SHOW_HELPERS,
  102. SHOW_RULERS,
  103. SHOW_GUIDES,
  104. SHOW_ORIGIN,
  105. SHOW_VIEWPORT,
  106. SHOW_POSITION_GIZMOS,
  107. SHOW_LOCK_GIZMOS,
  108. SHOW_GROUP_GIZMOS,
  109. LOCK_SELECTED,
  110. UNLOCK_SELECTED,
  111. GROUP_SELECTED,
  112. UNGROUP_SELECTED,
  113. ANCHORS_AND_MARGINS_PRESET_TOP_LEFT,
  114. ANCHORS_AND_MARGINS_PRESET_TOP_RIGHT,
  115. ANCHORS_AND_MARGINS_PRESET_BOTTOM_LEFT,
  116. ANCHORS_AND_MARGINS_PRESET_BOTTOM_RIGHT,
  117. ANCHORS_AND_MARGINS_PRESET_CENTER_LEFT,
  118. ANCHORS_AND_MARGINS_PRESET_CENTER_RIGHT,
  119. ANCHORS_AND_MARGINS_PRESET_CENTER_TOP,
  120. ANCHORS_AND_MARGINS_PRESET_CENTER_BOTTOM,
  121. ANCHORS_AND_MARGINS_PRESET_CENTER,
  122. ANCHORS_AND_MARGINS_PRESET_TOP_WIDE,
  123. ANCHORS_AND_MARGINS_PRESET_LEFT_WIDE,
  124. ANCHORS_AND_MARGINS_PRESET_RIGHT_WIDE,
  125. ANCHORS_AND_MARGINS_PRESET_BOTTOM_WIDE,
  126. ANCHORS_AND_MARGINS_PRESET_VCENTER_WIDE,
  127. ANCHORS_AND_MARGINS_PRESET_HCENTER_WIDE,
  128. ANCHORS_AND_MARGINS_PRESET_WIDE,
  129. ANCHORS_AND_MARGINS_PRESET_KEEP_RATIO,
  130. ANCHORS_PRESET_TOP_LEFT,
  131. ANCHORS_PRESET_TOP_RIGHT,
  132. ANCHORS_PRESET_BOTTOM_LEFT,
  133. ANCHORS_PRESET_BOTTOM_RIGHT,
  134. ANCHORS_PRESET_CENTER_LEFT,
  135. ANCHORS_PRESET_CENTER_RIGHT,
  136. ANCHORS_PRESET_CENTER_TOP,
  137. ANCHORS_PRESET_CENTER_BOTTOM,
  138. ANCHORS_PRESET_CENTER,
  139. ANCHORS_PRESET_TOP_WIDE,
  140. ANCHORS_PRESET_LEFT_WIDE,
  141. ANCHORS_PRESET_RIGHT_WIDE,
  142. ANCHORS_PRESET_BOTTOM_WIDE,
  143. ANCHORS_PRESET_VCENTER_WIDE,
  144. ANCHORS_PRESET_HCENTER_WIDE,
  145. ANCHORS_PRESET_WIDE,
  146. MARGINS_PRESET_TOP_LEFT,
  147. MARGINS_PRESET_TOP_RIGHT,
  148. MARGINS_PRESET_BOTTOM_LEFT,
  149. MARGINS_PRESET_BOTTOM_RIGHT,
  150. MARGINS_PRESET_CENTER_LEFT,
  151. MARGINS_PRESET_CENTER_RIGHT,
  152. MARGINS_PRESET_CENTER_TOP,
  153. MARGINS_PRESET_CENTER_BOTTOM,
  154. MARGINS_PRESET_CENTER,
  155. MARGINS_PRESET_TOP_WIDE,
  156. MARGINS_PRESET_LEFT_WIDE,
  157. MARGINS_PRESET_RIGHT_WIDE,
  158. MARGINS_PRESET_BOTTOM_WIDE,
  159. MARGINS_PRESET_VCENTER_WIDE,
  160. MARGINS_PRESET_HCENTER_WIDE,
  161. MARGINS_PRESET_WIDE,
  162. ANIM_INSERT_KEY,
  163. ANIM_INSERT_KEY_EXISTING,
  164. ANIM_INSERT_POS,
  165. ANIM_INSERT_ROT,
  166. ANIM_INSERT_SCALE,
  167. ANIM_COPY_POSE,
  168. ANIM_PASTE_POSE,
  169. ANIM_CLEAR_POSE,
  170. CLEAR_GUIDES,
  171. VIEW_CENTER_TO_SELECTION,
  172. VIEW_FRAME_TO_SELECTION,
  173. PREVIEW_CANVAS_SCALE,
  174. SKELETON_MAKE_BONES,
  175. SKELETON_CLEAR_BONES,
  176. SKELETON_SHOW_BONES,
  177. SKELETON_SET_IK_CHAIN,
  178. SKELETON_CLEAR_IK_CHAIN
  179. };
  180. enum DragType {
  181. DRAG_NONE,
  182. DRAG_BOX_SELECTION,
  183. DRAG_LEFT,
  184. DRAG_TOP_LEFT,
  185. DRAG_TOP,
  186. DRAG_TOP_RIGHT,
  187. DRAG_RIGHT,
  188. DRAG_BOTTOM_RIGHT,
  189. DRAG_BOTTOM,
  190. DRAG_BOTTOM_LEFT,
  191. DRAG_ANCHOR_TOP_LEFT,
  192. DRAG_ANCHOR_TOP_RIGHT,
  193. DRAG_ANCHOR_BOTTOM_RIGHT,
  194. DRAG_ANCHOR_BOTTOM_LEFT,
  195. DRAG_ANCHOR_ALL,
  196. DRAG_QUEUED,
  197. DRAG_MOVE,
  198. DRAG_SCALE_X,
  199. DRAG_SCALE_Y,
  200. DRAG_SCALE_BOTH,
  201. DRAG_ROTATE,
  202. DRAG_PIVOT,
  203. DRAG_V_GUIDE,
  204. DRAG_H_GUIDE,
  205. DRAG_DOUBLE_GUIDE,
  206. DRAG_KEY_MOVE
  207. };
  208. enum GridVisibility {
  209. GRID_VISIBILITY_SHOW,
  210. GRID_VISIBILITY_SHOW_WHEN_SNAPPING,
  211. GRID_VISIBILITY_HIDE,
  212. };
  213. EditorSelection *editor_selection;
  214. bool selection_menu_additive_selection;
  215. Tool tool;
  216. Control *viewport;
  217. Control *viewport_scrollable;
  218. HScrollBar *h_scroll;
  219. VScrollBar *v_scroll;
  220. // Used for secondary menu items which are displayed depending on the currently selected node
  221. // (such as MeshInstance's "Mesh" menu).
  222. PanelContainer *context_menu_panel = nullptr;
  223. HBoxContainer *context_menu_hbox = nullptr;
  224. ToolButton *zoom_minus;
  225. ToolButton *zoom_reset;
  226. ToolButton *zoom_plus;
  227. Map<Control *, Timer *> popup_temporarily_timers;
  228. Label *warning_child_of_container;
  229. VBoxContainer *info_overlay;
  230. Transform2D transform;
  231. GridVisibility grid_visibility;
  232. bool show_rulers;
  233. bool show_guides;
  234. bool show_origin;
  235. bool show_viewport;
  236. bool show_helpers;
  237. bool show_position_gizmos;
  238. bool show_lock_gizmos;
  239. bool show_group_gizmos;
  240. float zoom;
  241. Point2 view_offset;
  242. Point2 previous_update_view_offset;
  243. bool selected_from_canvas;
  244. bool anchors_mode;
  245. Point2 grid_offset;
  246. Point2 grid_step;
  247. int primary_grid_steps;
  248. int grid_step_multiplier;
  249. float snap_rotation_step;
  250. float snap_rotation_offset;
  251. float snap_scale_step;
  252. bool smart_snap_active;
  253. bool grid_snap_active;
  254. bool snap_node_parent;
  255. bool snap_node_anchors;
  256. bool snap_node_sides;
  257. bool snap_node_center;
  258. bool snap_other_nodes;
  259. bool snap_guides;
  260. bool snap_rotation;
  261. bool snap_scale;
  262. bool snap_relative;
  263. bool snap_pixel;
  264. bool skeleton_show_bones;
  265. bool key_pos;
  266. bool key_rot;
  267. bool key_scale;
  268. bool panning;
  269. bool pan_pressed;
  270. bool ruler_tool_active;
  271. Point2 ruler_tool_origin;
  272. Point2 node_create_position;
  273. MenuOption last_option;
  274. struct _SelectResult {
  275. CanvasItem *item;
  276. float z_index;
  277. bool has_z;
  278. _FORCE_INLINE_ bool operator<(const _SelectResult &p_rr) const {
  279. return has_z && p_rr.has_z ? p_rr.z_index < z_index : p_rr.has_z;
  280. }
  281. };
  282. Vector<_SelectResult> selection_results;
  283. struct _HoverResult {
  284. Point2 position;
  285. Ref<Texture> icon;
  286. String name;
  287. };
  288. Vector<_HoverResult> hovering_results;
  289. struct BoneList {
  290. Transform2D xform;
  291. float length;
  292. uint64_t last_pass;
  293. BoneList() :
  294. length(0.f),
  295. last_pass(0) {}
  296. };
  297. uint64_t bone_last_frame;
  298. struct BoneKey {
  299. ObjectID from;
  300. ObjectID to;
  301. _FORCE_INLINE_ bool operator<(const BoneKey &p_key) const {
  302. if (from == p_key.from) {
  303. return to < p_key.to;
  304. } else {
  305. return from < p_key.from;
  306. }
  307. }
  308. };
  309. Map<BoneKey, BoneList> bone_list;
  310. struct PoseClipboard {
  311. Vector2 pos;
  312. Vector2 scale;
  313. float rot;
  314. ObjectID id;
  315. };
  316. List<PoseClipboard> pose_clipboard;
  317. ToolButton *select_button;
  318. ToolButton *move_button;
  319. ToolButton *scale_button;
  320. ToolButton *rotate_button;
  321. ToolButton *list_select_button;
  322. ToolButton *pivot_button;
  323. ToolButton *pan_button;
  324. ToolButton *ruler_button;
  325. ToolButton *smart_snap_button;
  326. ToolButton *grid_snap_button;
  327. MenuButton *snap_config_menu;
  328. PopupMenu *smartsnap_config_popup;
  329. ToolButton *lock_button;
  330. ToolButton *unlock_button;
  331. ToolButton *group_button;
  332. ToolButton *ungroup_button;
  333. MenuButton *skeleton_menu;
  334. ToolButton *override_camera_button;
  335. MenuButton *view_menu;
  336. PopupMenu *grid_menu;
  337. PopupMenu *gizmos_menu;
  338. HBoxContainer *animation_hb;
  339. MenuButton *animation_menu;
  340. MenuButton *presets_menu;
  341. PopupMenu *anchors_and_margins_popup;
  342. PopupMenu *anchors_popup;
  343. ToolButton *anchor_mode_button;
  344. Button *key_loc_button;
  345. Button *key_rot_button;
  346. Button *key_scale_button;
  347. Button *key_insert_button;
  348. Button *key_auto_insert_button;
  349. PopupMenu *selection_menu;
  350. PopupMenu *add_node_menu;
  351. Control *top_ruler;
  352. Control *left_ruler;
  353. Point2 drag_start_origin;
  354. DragType drag_type;
  355. Point2 drag_from;
  356. Point2 drag_to;
  357. Point2 drag_rotation_center;
  358. List<CanvasItem *> drag_selection;
  359. int dragged_guide_index;
  360. Point2 dragged_guide_pos;
  361. bool is_hovering_h_guide;
  362. bool is_hovering_v_guide;
  363. bool updating_value_dialog;
  364. Point2 box_selecting_to;
  365. Ref<StyleBoxTexture> select_sb;
  366. Ref<Texture> select_handle;
  367. Ref<Texture> anchor_handle;
  368. Ref<ShortCut> drag_pivot_shortcut;
  369. Ref<ShortCut> set_pivot_shortcut;
  370. Ref<ShortCut> multiply_grid_step_shortcut;
  371. Ref<ShortCut> divide_grid_step_shortcut;
  372. Ref<ShortCut> pan_view_shortcut;
  373. bool _is_node_locked(const Node *p_node);
  374. bool _is_node_movable(const Node *p_node, bool p_popup_warning = false);
  375. 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());
  376. void _get_canvas_items_at_pos(const Point2 &p_pos, Vector<_SelectResult> &r_items, bool p_allow_locked = false);
  377. void _get_bones_at_pos(const Point2 &p_pos, Vector<_SelectResult> &r_items);
  378. void _find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, List<CanvasItem *> *r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  379. bool _select_click_on_item(CanvasItem *item, Point2 p_click_pos, bool p_append);
  380. ConfirmationDialog *snap_dialog;
  381. CanvasItem *ref_item;
  382. void _save_canvas_item_ik_chain(const CanvasItem *p_canvas_item, List<float> *p_bones_length, List<Dictionary> *p_bones_state);
  383. void _save_canvas_item_state(List<CanvasItem *> p_canvas_items, bool save_bones = false);
  384. void _restore_canvas_item_ik_chain(CanvasItem *p_canvas_item, const List<Dictionary> *p_bones_state);
  385. void _restore_canvas_item_state(List<CanvasItem *> p_canvas_items, bool restore_bones = false);
  386. void _commit_canvas_item_state(List<CanvasItem *> p_canvas_items, String action_name, bool commit_bones = false);
  387. Vector2 _anchor_to_position(const Control *p_control, Vector2 anchor);
  388. Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
  389. void _popup_callback(int p_op);
  390. bool updating_scroll;
  391. void _update_scroll(float);
  392. void _update_scrollbars();
  393. void _snap_changed();
  394. void _selection_result_pressed(int);
  395. void _selection_menu_hide();
  396. void _add_node_pressed(int p_result);
  397. void _node_created(Node *p_node);
  398. void _reset_create_position();
  399. bool _is_grid_visible() const;
  400. void _prepare_grid_menu();
  401. void _on_grid_menu_id_pressed(int p_id);
  402. UndoRedo *undo_redo;
  403. bool _build_bones_list(Node *p_node);
  404. bool _get_bone_shape(Vector<Vector2> *shape, Vector<Vector2> *outline_shape, Map<BoneKey, BoneList>::Element *bone);
  405. List<CanvasItem *> _get_edited_canvas_items(bool retreive_locked = false, bool remove_canvas_item_if_parent_in_selection = true);
  406. Rect2 _get_encompassing_rect_from_list(List<CanvasItem *> p_list);
  407. 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);
  408. Rect2 _get_encompassing_rect(const Node *p_node);
  409. Object *_get_editor_data(Object *p_what);
  410. void _insert_animation_keys(bool p_location, bool p_rotation, bool p_scale, bool p_on_existing);
  411. void _keying_changed();
  412. void _unhandled_key_input(const Ref<InputEvent> &p_ev);
  413. void _draw_text_at_position(Point2 p_position, String p_string, Margin p_side);
  414. void _draw_margin_at_position(int p_value, Point2 p_position, Margin p_side);
  415. void _draw_percentage_at_position(float p_value, Point2 p_position, Margin p_side);
  416. void _draw_straight_line(Point2 p_from, Point2 p_to, Color p_color);
  417. void _draw_smart_snapping();
  418. void _draw_rulers();
  419. void _draw_guides();
  420. void _draw_focus();
  421. void _draw_grid();
  422. void _draw_ruler_tool();
  423. void _draw_control_anchors(Control *control);
  424. void _draw_control_helpers(Control *control);
  425. void _draw_selection();
  426. void _draw_axis();
  427. void _draw_bones();
  428. void _draw_invisible_nodes_positions(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  429. void _draw_locks_and_groups(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  430. void _draw_hover();
  431. void _draw_viewport();
  432. bool _gui_input_anchors(const Ref<InputEvent> &p_event);
  433. bool _gui_input_move(const Ref<InputEvent> &p_event);
  434. bool _gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event);
  435. bool _gui_input_scale(const Ref<InputEvent> &p_event);
  436. bool _gui_input_pivot(const Ref<InputEvent> &p_event);
  437. bool _gui_input_resize(const Ref<InputEvent> &p_event);
  438. bool _gui_input_rotate(const Ref<InputEvent> &p_event);
  439. bool _gui_input_select(const Ref<InputEvent> &p_event);
  440. bool _gui_input_ruler_tool(const Ref<InputEvent> &p_event);
  441. bool _gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted);
  442. bool _gui_input_rulers_and_guides(const Ref<InputEvent> &p_event);
  443. bool _gui_input_hover(const Ref<InputEvent> &p_event);
  444. void _gui_input_viewport(const Ref<InputEvent> &p_event);
  445. void _selection_changed();
  446. void _focus_selection(int p_op);
  447. void _solve_IK(Node2D *leaf_node, Point2 target_position);
  448. SnapTarget snap_target[2];
  449. Transform2D snap_transform;
  450. void _snap_if_closer_float(
  451. float p_value,
  452. float &r_current_snap, SnapTarget &r_current_snap_target,
  453. float p_target_value, SnapTarget p_snap_target,
  454. float p_radius = 10.0);
  455. void _snap_if_closer_point(
  456. Point2 p_value,
  457. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  458. Point2 p_target_value, SnapTarget p_snap_target,
  459. real_t rotation = 0.0,
  460. float p_radius = 10.0);
  461. void _snap_other_nodes(
  462. const Point2 p_value,
  463. const Transform2D p_transform_to_snap,
  464. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  465. const SnapTarget p_snap_target, List<const CanvasItem *> p_exceptions,
  466. const Node *p_current);
  467. void _set_anchors_preset(Control::LayoutPreset p_preset);
  468. void _set_anchors_and_margins_preset(Control::LayoutPreset p_preset);
  469. void _set_anchors_and_margins_to_keep_ratio();
  470. void _button_toggle_anchor_mode(bool p_status);
  471. VBoxContainer *controls_vb;
  472. HBoxContainer *zoom_hb;
  473. float _get_next_zoom_value(int p_increment_count, bool p_integer_only = false) const;
  474. void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
  475. void _update_zoom_label();
  476. void _button_zoom_minus();
  477. void _button_zoom_reset();
  478. void _button_zoom_plus();
  479. void _shortcut_zoom_set(float p_zoom);
  480. void _button_toggle_smart_snap(bool p_status);
  481. void _button_toggle_grid_snap(bool p_status);
  482. void _button_override_camera(bool p_pressed);
  483. void _button_tool_select(int p_index);
  484. void _update_override_camera_button(bool p_game_running);
  485. HSplitContainer *left_panel_split;
  486. HSplitContainer *right_panel_split;
  487. VSplitContainer *bottom_split;
  488. bool bone_list_dirty;
  489. void _queue_update_bone_list();
  490. void _update_bone_list();
  491. void _tree_changed(Node *);
  492. void _update_context_menu_stylebox();
  493. void _popup_warning_temporarily(Control *p_control, const float p_duration);
  494. void _popup_warning_depop(Control *p_control);
  495. friend class CanvasItemEditorPlugin;
  496. protected:
  497. void _notification(int p_what);
  498. static void _bind_methods();
  499. struct compare_items_x {
  500. bool operator()(const CanvasItem *a, const CanvasItem *b) const {
  501. return a->get_global_transform().elements[2].x < b->get_global_transform().elements[2].x;
  502. }
  503. };
  504. struct compare_items_y {
  505. bool operator()(const CanvasItem *a, const CanvasItem *b) const {
  506. return a->get_global_transform().elements[2].y < b->get_global_transform().elements[2].y;
  507. }
  508. };
  509. struct proj_vector2_x {
  510. float get(const Vector2 &v) { return v.x; }
  511. void set(Vector2 &v, float f) { v.x = f; }
  512. };
  513. struct proj_vector2_y {
  514. float get(const Vector2 &v) { return v.y; }
  515. void set(Vector2 &v, float f) { v.y = f; }
  516. };
  517. static CanvasItemEditor *singleton;
  518. public:
  519. enum SnapMode {
  520. SNAP_GRID = 1 << 0,
  521. SNAP_GUIDES = 1 << 1,
  522. SNAP_PIXEL = 1 << 2,
  523. SNAP_NODE_PARENT = 1 << 3,
  524. SNAP_NODE_ANCHORS = 1 << 4,
  525. SNAP_NODE_SIDES = 1 << 5,
  526. SNAP_NODE_CENTER = 1 << 6,
  527. SNAP_OTHER_NODES = 1 << 7,
  528. SNAP_DEFAULT = SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL,
  529. };
  530. 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, List<CanvasItem *> p_other_nodes_exceptions = List<CanvasItem *>());
  531. float snap_angle(float p_target, float p_start = 0) const;
  532. Transform2D get_canvas_transform() const { return transform; }
  533. static CanvasItemEditor *get_singleton() { return singleton; }
  534. Dictionary get_state() const;
  535. void set_state(const Dictionary &p_state);
  536. void add_control_to_menu_panel(Control *p_control);
  537. void remove_control_from_menu_panel(Control *p_control);
  538. void add_control_to_info_overlay(Control *p_control);
  539. void remove_control_from_info_overlay(Control *p_control);
  540. void add_control_to_left_panel(Control *p_control);
  541. void remove_control_from_left_panel(Control *p_control);
  542. void add_control_to_right_panel(Control *p_control);
  543. void remove_control_from_right_panel(Control *p_control);
  544. void move_control_to_left_panel(Control *p_control);
  545. void move_control_to_right_panel(Control *p_control);
  546. VSplitContainer *get_bottom_split();
  547. Control *get_viewport_control() { return viewport; }
  548. Control *get_controls_container() { return controls_vb; }
  549. void update_viewport();
  550. Tool get_current_tool() { return tool; }
  551. void set_current_tool(Tool p_tool);
  552. void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
  553. void edit(CanvasItem *p_canvas_item);
  554. void focus_selection();
  555. bool is_anchors_mode_enabled() { return anchors_mode; };
  556. CanvasItemEditor(EditorNode *p_editor);
  557. };
  558. class CanvasItemEditorPlugin : public EditorPlugin {
  559. GDCLASS(CanvasItemEditorPlugin, EditorPlugin);
  560. CanvasItemEditor *canvas_item_editor;
  561. EditorNode *editor;
  562. public:
  563. virtual String get_name() const { return "2D"; }
  564. bool has_main_screen() const { return true; }
  565. virtual void edit(Object *p_object);
  566. virtual bool handles(Object *p_object) const;
  567. virtual void make_visible(bool p_visible);
  568. virtual Dictionary get_state() const;
  569. virtual void set_state(const Dictionary &p_state);
  570. CanvasItemEditor *get_canvas_item_editor() { return canvas_item_editor; }
  571. CanvasItemEditorPlugin(EditorNode *p_node);
  572. ~CanvasItemEditorPlugin();
  573. };
  574. class CanvasItemEditorViewport : public Control {
  575. GDCLASS(CanvasItemEditorViewport, Control);
  576. String default_type;
  577. Vector<String> types;
  578. Vector<String> selected_files;
  579. Node *target_node;
  580. Point2 drop_pos;
  581. EditorNode *editor;
  582. EditorData *editor_data;
  583. CanvasItemEditor *canvas_item_editor;
  584. Node2D *preview_node;
  585. AcceptDialog *accept;
  586. WindowDialog *selector;
  587. Label *selector_label;
  588. Label *label;
  589. Label *label_desc;
  590. VBoxContainer *btn_group;
  591. Ref<ButtonGroup> button_group;
  592. void _on_mouse_exit();
  593. void _on_select_type(Object *selected);
  594. void _on_change_type_confirmed();
  595. void _on_change_type_closed();
  596. void _create_preview(const Vector<String> &files) const;
  597. void _remove_preview();
  598. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  599. bool _only_packed_scenes_selected() const;
  600. void _create_nodes(Node *parent, Node *child, String &path, const Point2 &p_point);
  601. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  602. void _perform_drop_data();
  603. void _show_resource_type_selector();
  604. static void _bind_methods();
  605. protected:
  606. void _notification(int p_what);
  607. public:
  608. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  609. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  610. CanvasItemEditorViewport(EditorNode *p_node, CanvasItemEditor *p_canvas_item_editor);
  611. ~CanvasItemEditorViewport();
  612. };
  613. #endif // CANVAS_ITEM_EDITOR_PLUGIN_H