canvas_item_editor_plugin.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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_plugin.h"
  33. #include "scene/gui/base_button.h"
  34. #include "scene/gui/box_container.h"
  35. class AcceptDialog;
  36. class CanvasItemEditorViewport;
  37. class ConfirmationDialog;
  38. class EditorData;
  39. class EditorZoomWidget;
  40. class HScrollBar;
  41. class HSplitContainer;
  42. class MenuButton;
  43. class PanelContainer;
  44. class ViewPanner;
  45. class VScrollBar;
  46. class VSplitContainer;
  47. class CanvasItemEditorSelectedItem : public Object {
  48. GDCLASS(CanvasItemEditorSelectedItem, Object);
  49. public:
  50. Transform2D prev_xform;
  51. real_t prev_rot = 0;
  52. Rect2 prev_rect;
  53. Vector2 prev_pivot;
  54. real_t prev_anchors[4] = { (real_t)0.0 };
  55. Transform2D pre_drag_xform;
  56. Rect2 pre_drag_rect;
  57. List<real_t> pre_drag_bones_length;
  58. List<Dictionary> pre_drag_bones_undo_state;
  59. Dictionary undo_state;
  60. CanvasItemEditorSelectedItem() {}
  61. };
  62. class CanvasItemEditor : public VBoxContainer {
  63. GDCLASS(CanvasItemEditor, VBoxContainer);
  64. public:
  65. enum Tool {
  66. TOOL_SELECT,
  67. TOOL_LIST_SELECT,
  68. TOOL_MOVE,
  69. TOOL_SCALE,
  70. TOOL_ROTATE,
  71. TOOL_EDIT_PIVOT,
  72. TOOL_PAN,
  73. TOOL_RULER,
  74. TOOL_MAX
  75. };
  76. enum AddNodeOption {
  77. ADD_NODE,
  78. ADD_INSTANCE,
  79. ADD_PASTE,
  80. ADD_MOVE,
  81. };
  82. private:
  83. enum SnapTarget {
  84. SNAP_TARGET_NONE = 0,
  85. SNAP_TARGET_PARENT,
  86. SNAP_TARGET_SELF_ANCHORS,
  87. SNAP_TARGET_SELF,
  88. SNAP_TARGET_OTHER_NODE,
  89. SNAP_TARGET_GUIDE,
  90. SNAP_TARGET_GRID,
  91. SNAP_TARGET_PIXEL
  92. };
  93. enum MenuOption {
  94. SNAP_USE,
  95. SNAP_USE_NODE_PARENT,
  96. SNAP_USE_NODE_ANCHORS,
  97. SNAP_USE_NODE_SIDES,
  98. SNAP_USE_NODE_CENTER,
  99. SNAP_USE_OTHER_NODES,
  100. SNAP_USE_GRID,
  101. SNAP_USE_GUIDES,
  102. SNAP_USE_ROTATION,
  103. SNAP_USE_SCALE,
  104. SNAP_RELATIVE,
  105. SNAP_CONFIGURE,
  106. SNAP_USE_PIXEL,
  107. SHOW_HELPERS,
  108. SHOW_RULERS,
  109. SHOW_GUIDES,
  110. SHOW_ORIGIN,
  111. SHOW_VIEWPORT,
  112. SHOW_EDIT_LOCKS,
  113. SHOW_TRANSFORMATION_GIZMOS,
  114. LOCK_SELECTED,
  115. UNLOCK_SELECTED,
  116. GROUP_SELECTED,
  117. UNGROUP_SELECTED,
  118. ANIM_INSERT_KEY,
  119. ANIM_INSERT_KEY_EXISTING,
  120. ANIM_INSERT_POS,
  121. ANIM_INSERT_ROT,
  122. ANIM_INSERT_SCALE,
  123. ANIM_COPY_POSE,
  124. ANIM_PASTE_POSE,
  125. ANIM_CLEAR_POSE,
  126. CLEAR_GUIDES,
  127. VIEW_CENTER_TO_SELECTION,
  128. VIEW_FRAME_TO_SELECTION,
  129. PREVIEW_CANVAS_SCALE,
  130. SKELETON_MAKE_BONES,
  131. SKELETON_SHOW_BONES
  132. };
  133. enum DragType {
  134. DRAG_NONE,
  135. DRAG_BOX_SELECTION,
  136. DRAG_LEFT,
  137. DRAG_TOP_LEFT,
  138. DRAG_TOP,
  139. DRAG_TOP_RIGHT,
  140. DRAG_RIGHT,
  141. DRAG_BOTTOM_RIGHT,
  142. DRAG_BOTTOM,
  143. DRAG_BOTTOM_LEFT,
  144. DRAG_ANCHOR_TOP_LEFT,
  145. DRAG_ANCHOR_TOP_RIGHT,
  146. DRAG_ANCHOR_BOTTOM_RIGHT,
  147. DRAG_ANCHOR_BOTTOM_LEFT,
  148. DRAG_ANCHOR_ALL,
  149. DRAG_QUEUED,
  150. DRAG_MOVE,
  151. DRAG_MOVE_X,
  152. DRAG_MOVE_Y,
  153. DRAG_SCALE_X,
  154. DRAG_SCALE_Y,
  155. DRAG_SCALE_BOTH,
  156. DRAG_ROTATE,
  157. DRAG_PIVOT,
  158. DRAG_V_GUIDE,
  159. DRAG_H_GUIDE,
  160. DRAG_DOUBLE_GUIDE,
  161. DRAG_KEY_MOVE
  162. };
  163. enum GridVisibility {
  164. GRID_VISIBILITY_SHOW,
  165. GRID_VISIBILITY_SHOW_WHEN_SNAPPING,
  166. GRID_VISIBILITY_HIDE,
  167. };
  168. bool selection_menu_additive_selection = false;
  169. Tool tool = TOOL_SELECT;
  170. Control *viewport = nullptr;
  171. Control *viewport_scrollable = nullptr;
  172. HScrollBar *h_scroll = nullptr;
  173. VScrollBar *v_scroll = nullptr;
  174. // Used for secondary menu items which are displayed depending on the currently selected node
  175. // (such as MeshInstance's "Mesh" menu).
  176. PanelContainer *context_menu_panel = nullptr;
  177. HBoxContainer *context_menu_hbox = nullptr;
  178. Transform2D transform;
  179. GridVisibility grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
  180. bool show_rulers = true;
  181. bool show_guides = true;
  182. bool show_origin = true;
  183. bool show_viewport = true;
  184. bool show_helpers = false;
  185. bool show_edit_locks = true;
  186. bool show_transformation_gizmos = true;
  187. real_t zoom = 1.0;
  188. Point2 view_offset;
  189. Point2 previous_update_view_offset;
  190. bool selected_from_canvas = false;
  191. // Defaults are defined in clear().
  192. Point2 grid_offset;
  193. Point2 grid_step;
  194. int primary_grid_steps = 0;
  195. int grid_step_multiplier = 0;
  196. real_t snap_rotation_step = 0.0;
  197. real_t snap_rotation_offset = 0.0;
  198. real_t snap_scale_step = 0.0;
  199. bool smart_snap_active = false;
  200. bool grid_snap_active = false;
  201. bool snap_node_parent = true;
  202. bool snap_node_anchors = true;
  203. bool snap_node_sides = true;
  204. bool snap_node_center = true;
  205. bool snap_other_nodes = true;
  206. bool snap_guides = true;
  207. bool snap_rotation = false;
  208. bool snap_scale = false;
  209. bool snap_relative = false;
  210. // Enable pixel snapping even if pixel snap rendering is disabled in the Project Settings.
  211. // This results in crisper visuals by preventing 2D nodes from being placed at subpixel coordinates.
  212. bool snap_pixel = true;
  213. bool key_pos = true;
  214. bool key_rot = true;
  215. bool key_scale = false;
  216. bool pan_pressed = false;
  217. bool ruler_tool_active = false;
  218. Point2 ruler_tool_origin;
  219. Point2 node_create_position;
  220. MenuOption last_option;
  221. struct _SelectResult {
  222. CanvasItem *item = nullptr;
  223. real_t z_index = 0;
  224. bool has_z = true;
  225. _FORCE_INLINE_ bool operator<(const _SelectResult &p_rr) const {
  226. return has_z && p_rr.has_z ? p_rr.z_index < z_index : p_rr.has_z;
  227. }
  228. };
  229. Vector<_SelectResult> selection_results;
  230. Vector<_SelectResult> selection_results_menu;
  231. struct _HoverResult {
  232. Point2 position;
  233. Ref<Texture2D> icon;
  234. String name;
  235. };
  236. Vector<_HoverResult> hovering_results;
  237. struct BoneList {
  238. Transform2D xform;
  239. real_t length = 0;
  240. uint64_t last_pass = 0;
  241. };
  242. uint64_t bone_last_frame = 0;
  243. struct BoneKey {
  244. ObjectID from;
  245. ObjectID to;
  246. _FORCE_INLINE_ bool operator<(const BoneKey &p_key) const {
  247. if (from == p_key.from) {
  248. return to < p_key.to;
  249. } else {
  250. return from < p_key.from;
  251. }
  252. }
  253. };
  254. HashMap<BoneKey, BoneList> bone_list;
  255. struct PoseClipboard {
  256. Vector2 pos;
  257. Vector2 scale;
  258. real_t rot = 0;
  259. ObjectID id;
  260. };
  261. List<PoseClipboard> pose_clipboard;
  262. Button *select_button = nullptr;
  263. Button *move_button = nullptr;
  264. Button *scale_button = nullptr;
  265. Button *rotate_button = nullptr;
  266. Button *list_select_button = nullptr;
  267. Button *pivot_button = nullptr;
  268. Button *pan_button = nullptr;
  269. Button *ruler_button = nullptr;
  270. Button *smart_snap_button = nullptr;
  271. Button *grid_snap_button = nullptr;
  272. MenuButton *snap_config_menu = nullptr;
  273. PopupMenu *smartsnap_config_popup = nullptr;
  274. Button *lock_button = nullptr;
  275. Button *unlock_button = nullptr;
  276. Button *group_button = nullptr;
  277. Button *ungroup_button = nullptr;
  278. MenuButton *skeleton_menu = nullptr;
  279. Button *override_camera_button = nullptr;
  280. MenuButton *view_menu = nullptr;
  281. PopupMenu *grid_menu = nullptr;
  282. HBoxContainer *animation_hb = nullptr;
  283. MenuButton *animation_menu = nullptr;
  284. Button *key_loc_button = nullptr;
  285. Button *key_rot_button = nullptr;
  286. Button *key_scale_button = nullptr;
  287. Button *key_insert_button = nullptr;
  288. Button *key_auto_insert_button = nullptr;
  289. PopupMenu *selection_menu = nullptr;
  290. PopupMenu *add_node_menu = nullptr;
  291. Control *top_ruler = nullptr;
  292. Control *left_ruler = nullptr;
  293. Point2 drag_start_origin;
  294. DragType drag_type = DRAG_NONE;
  295. Point2 drag_from;
  296. Point2 drag_to;
  297. Point2 drag_rotation_center;
  298. List<CanvasItem *> drag_selection;
  299. int dragged_guide_index = -1;
  300. Point2 dragged_guide_pos;
  301. bool is_hovering_h_guide = false;
  302. bool is_hovering_v_guide = false;
  303. bool updating_value_dialog = false;
  304. Transform2D original_transform;
  305. Point2 box_selecting_to;
  306. Ref<StyleBoxTexture> select_sb;
  307. Ref<Texture2D> select_handle;
  308. Ref<Texture2D> anchor_handle;
  309. Ref<Shortcut> drag_pivot_shortcut;
  310. Ref<Shortcut> set_pivot_shortcut;
  311. Ref<Shortcut> multiply_grid_step_shortcut;
  312. Ref<Shortcut> divide_grid_step_shortcut;
  313. Ref<ViewPanner> panner;
  314. bool warped_panning = true;
  315. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  316. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  317. bool _is_node_locked(const Node *p_node) const;
  318. bool _is_node_movable(const Node *p_node, bool p_popup_warning = false);
  319. 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());
  320. void _get_canvas_items_at_pos(const Point2 &p_pos, Vector<_SelectResult> &r_items, bool p_allow_locked = false);
  321. 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());
  322. bool _select_click_on_item(CanvasItem *item, Point2 p_click_pos, bool p_append);
  323. ConfirmationDialog *snap_dialog = nullptr;
  324. CanvasItem *ref_item = nullptr;
  325. void _save_canvas_item_state(List<CanvasItem *> p_canvas_items, bool save_bones = false);
  326. void _restore_canvas_item_state(List<CanvasItem *> p_canvas_items, bool restore_bones = false);
  327. void _commit_canvas_item_state(List<CanvasItem *> p_canvas_items, String action_name, bool commit_bones = false);
  328. Vector2 _anchor_to_position(const Control *p_control, Vector2 anchor);
  329. Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
  330. void _popup_callback(int p_op);
  331. bool updating_scroll = false;
  332. void _update_scroll(real_t);
  333. void _update_scrollbars();
  334. void _snap_changed();
  335. void _selection_result_pressed(int);
  336. void _selection_menu_hide();
  337. void _add_node_pressed(int p_result);
  338. void _node_created(Node *p_node);
  339. void _reset_create_position();
  340. void _update_editor_settings();
  341. bool _is_grid_visible() const;
  342. void _prepare_grid_menu();
  343. void _on_grid_menu_id_pressed(int p_id);
  344. List<CanvasItem *> _get_edited_canvas_items(bool retrieve_locked = false, bool remove_canvas_item_if_parent_in_selection = true) const;
  345. Rect2 _get_encompassing_rect_from_list(List<CanvasItem *> p_list);
  346. 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);
  347. Rect2 _get_encompassing_rect(const Node *p_node);
  348. Object *_get_editor_data(Object *p_what);
  349. void _insert_animation_keys(bool p_location, bool p_rotation, bool p_scale, bool p_on_existing);
  350. void _keying_changed();
  351. virtual void shortcut_input(const Ref<InputEvent> &p_ev) override;
  352. void _draw_text_at_position(Point2 p_position, String p_string, Side p_side);
  353. void _draw_margin_at_position(int p_value, Point2 p_position, Side p_side);
  354. void _draw_percentage_at_position(real_t p_value, Point2 p_position, Side p_side);
  355. void _draw_straight_line(Point2 p_from, Point2 p_to, Color p_color);
  356. void _draw_smart_snapping();
  357. void _draw_rulers();
  358. void _draw_guides();
  359. void _draw_focus();
  360. void _draw_grid();
  361. void _draw_ruler_tool();
  362. void _draw_control_anchors(Control *control);
  363. void _draw_control_helpers(Control *control);
  364. void _draw_selection();
  365. void _draw_axis();
  366. void _draw_invisible_nodes_positions(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  367. void _draw_locks_and_groups(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  368. void _draw_hover();
  369. void _draw_transform_message();
  370. void _draw_viewport();
  371. bool _gui_input_anchors(const Ref<InputEvent> &p_event);
  372. bool _gui_input_move(const Ref<InputEvent> &p_event);
  373. bool _gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event);
  374. bool _gui_input_scale(const Ref<InputEvent> &p_event);
  375. bool _gui_input_pivot(const Ref<InputEvent> &p_event);
  376. bool _gui_input_resize(const Ref<InputEvent> &p_event);
  377. bool _gui_input_rotate(const Ref<InputEvent> &p_event);
  378. bool _gui_input_select(const Ref<InputEvent> &p_event);
  379. bool _gui_input_ruler_tool(const Ref<InputEvent> &p_event);
  380. bool _gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted);
  381. bool _gui_input_rulers_and_guides(const Ref<InputEvent> &p_event);
  382. bool _gui_input_hover(const Ref<InputEvent> &p_event);
  383. void _gui_input_viewport(const Ref<InputEvent> &p_event);
  384. void _update_cursor();
  385. void _selection_changed();
  386. void _focus_selection(int p_op);
  387. void _reset_drag();
  388. SnapTarget snap_target[2];
  389. Transform2D snap_transform;
  390. void _snap_if_closer_float(
  391. const real_t p_value,
  392. real_t &r_current_snap, SnapTarget &r_current_snap_target,
  393. const real_t p_target_value, const SnapTarget p_snap_target,
  394. const real_t p_radius = 10.0);
  395. void _snap_if_closer_point(
  396. Point2 p_value,
  397. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  398. Point2 p_target_value, const SnapTarget p_snap_target,
  399. const real_t rotation = 0.0,
  400. const real_t p_radius = 10.0);
  401. void _snap_other_nodes(
  402. const Point2 p_value,
  403. const Transform2D p_transform_to_snap,
  404. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  405. const SnapTarget p_snap_target, List<const CanvasItem *> p_exceptions,
  406. const Node *p_current);
  407. VBoxContainer *controls_vb = nullptr;
  408. EditorZoomWidget *zoom_widget = nullptr;
  409. void _update_zoom(real_t p_zoom);
  410. void _shortcut_zoom_set(real_t p_zoom);
  411. void _zoom_on_position(real_t p_zoom, Point2 p_position = Point2());
  412. void _button_toggle_smart_snap(bool p_status);
  413. void _button_toggle_grid_snap(bool p_status);
  414. void _button_override_camera(bool p_pressed);
  415. void _button_tool_select(int p_index);
  416. void _update_override_camera_button(bool p_game_running);
  417. HSplitContainer *left_panel_split = nullptr;
  418. HSplitContainer *right_panel_split = nullptr;
  419. VSplitContainer *bottom_split = nullptr;
  420. void _set_owner_for_node_and_children(Node *p_node, Node *p_owner);
  421. friend class CanvasItemEditorPlugin;
  422. protected:
  423. void _notification(int p_what);
  424. static void _bind_methods();
  425. static CanvasItemEditor *singleton;
  426. public:
  427. enum SnapMode {
  428. SNAP_GRID = 1 << 0,
  429. SNAP_GUIDES = 1 << 1,
  430. SNAP_PIXEL = 1 << 2,
  431. SNAP_NODE_PARENT = 1 << 3,
  432. SNAP_NODE_ANCHORS = 1 << 4,
  433. SNAP_NODE_SIDES = 1 << 5,
  434. SNAP_NODE_CENTER = 1 << 6,
  435. SNAP_OTHER_NODES = 1 << 7,
  436. SNAP_DEFAULT = SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL,
  437. };
  438. 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 *>());
  439. real_t snap_angle(real_t p_target, real_t p_start = 0) const;
  440. Transform2D get_canvas_transform() const { return transform; }
  441. static CanvasItemEditor *get_singleton() { return singleton; }
  442. Dictionary get_state() const;
  443. void set_state(const Dictionary &p_state);
  444. void clear();
  445. void add_control_to_menu_panel(Control *p_control);
  446. void remove_control_from_menu_panel(Control *p_control);
  447. void add_control_to_left_panel(Control *p_control);
  448. void remove_control_from_left_panel(Control *p_control);
  449. void add_control_to_right_panel(Control *p_control);
  450. void remove_control_from_right_panel(Control *p_control);
  451. VSplitContainer *get_bottom_split();
  452. Control *get_viewport_control() { return viewport; }
  453. Control *get_controls_container() { return controls_vb; }
  454. void update_viewport();
  455. Tool get_current_tool() { return tool; }
  456. void set_current_tool(Tool p_tool);
  457. void edit(CanvasItem *p_canvas_item);
  458. void focus_selection();
  459. void center_at(const Point2 &p_pos);
  460. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  461. EditorSelection *editor_selection = nullptr;
  462. CanvasItemEditor();
  463. };
  464. class CanvasItemEditorPlugin : public EditorPlugin {
  465. GDCLASS(CanvasItemEditorPlugin, EditorPlugin);
  466. CanvasItemEditor *canvas_item_editor = nullptr;
  467. protected:
  468. void _notification(int p_what);
  469. public:
  470. virtual String get_name() const override { return "2D"; }
  471. bool has_main_screen() const override { return true; }
  472. virtual void edit(Object *p_object) override;
  473. virtual bool handles(Object *p_object) const override;
  474. virtual void make_visible(bool p_visible) override;
  475. virtual Dictionary get_state() const override;
  476. virtual void set_state(const Dictionary &p_state) override;
  477. virtual void clear() override;
  478. CanvasItemEditor *get_canvas_item_editor() { return canvas_item_editor; }
  479. CanvasItemEditorPlugin();
  480. ~CanvasItemEditorPlugin();
  481. };
  482. class CanvasItemEditorViewport : public Control {
  483. GDCLASS(CanvasItemEditorViewport, Control);
  484. // The type of node that will be created when dropping texture into the viewport.
  485. String default_texture_node_type;
  486. // Node types that are available to select from when dropping texture into viewport.
  487. Vector<String> texture_node_types;
  488. Vector<String> selected_files;
  489. Node *target_node = nullptr;
  490. Point2 drop_pos;
  491. CanvasItemEditor *canvas_item_editor = nullptr;
  492. Control *preview_node = nullptr;
  493. AcceptDialog *accept = nullptr;
  494. AcceptDialog *selector = nullptr;
  495. Label *selector_label = nullptr;
  496. Label *label = nullptr;
  497. Label *label_desc = nullptr;
  498. VBoxContainer *btn_group = nullptr;
  499. Ref<ButtonGroup> button_group;
  500. void _on_mouse_exit();
  501. void _on_select_type(Object *selected);
  502. void _on_change_type_confirmed();
  503. void _on_change_type_closed();
  504. Node *_make_texture_node_type(String texture_node_type);
  505. void _create_preview(const Vector<String> &files) const;
  506. void _remove_preview();
  507. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  508. bool _only_packed_scenes_selected() const;
  509. void _create_nodes(Node *parent, Node *child, String &path, const Point2 &p_point);
  510. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  511. void _perform_drop_data();
  512. void _show_resource_type_selector();
  513. void _update_theme();
  514. protected:
  515. void _notification(int p_what);
  516. public:
  517. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  518. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  519. CanvasItemEditorViewport(CanvasItemEditor *p_canvas_item_editor);
  520. ~CanvasItemEditorViewport();
  521. };
  522. #endif // CANVAS_ITEM_EDITOR_PLUGIN_H