canvas_item_editor_plugin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 CONTROL_EDITOR_PLUGIN_H
  31. #define CONTROL_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/button_group.h"
  37. #include "scene/gui/check_box.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/panel_container.h"
  40. #include "scene/gui/spin_box.h"
  41. /**
  42. @author Juan Linietsky <reduzio@gmail.com>
  43. */
  44. class CanvasItemEditorViewport;
  45. class CanvasItemEditorSelectedItem : public Object {
  46. OBJ_TYPE(CanvasItemEditorSelectedItem, Object);
  47. public:
  48. Variant undo_state;
  49. Vector2 undo_pivot;
  50. Matrix32 prev_xform;
  51. float prev_rot;
  52. Rect2 prev_rect;
  53. CanvasItemEditorSelectedItem() { prev_rot = 0; }
  54. };
  55. class CanvasItemEditor : public VBoxContainer {
  56. OBJ_TYPE(CanvasItemEditor, VBoxContainer);
  57. EditorNode *editor;
  58. enum Tool {
  59. TOOL_SELECT,
  60. TOOL_LIST_SELECT,
  61. TOOL_MOVE,
  62. TOOL_ROTATE,
  63. TOOL_EDIT_PIVOT,
  64. TOOL_PAN,
  65. TOOL_MAX
  66. };
  67. enum MenuOption {
  68. SNAP_USE,
  69. SNAP_SHOW_GRID,
  70. SNAP_USE_ROTATION,
  71. SNAP_RELATIVE,
  72. SNAP_CONFIGURE,
  73. SNAP_USE_PIXEL,
  74. ZOOM_IN,
  75. ZOOM_OUT,
  76. ZOOM_RESET,
  77. ZOOM_SET,
  78. LOCK_SELECTED,
  79. UNLOCK_SELECTED,
  80. GROUP_SELECTED,
  81. UNGROUP_SELECTED,
  82. ALIGN_HORIZONTAL,
  83. ALIGN_VERTICAL,
  84. ANCHOR_ALIGN_TOP_LEFT,
  85. ANCHOR_ALIGN_TOP_RIGHT,
  86. ANCHOR_ALIGN_BOTTOM_LEFT,
  87. ANCHOR_ALIGN_BOTTOM_RIGHT,
  88. ANCHOR_ALIGN_CENTER_LEFT,
  89. ANCHOR_ALIGN_CENTER_RIGHT,
  90. ANCHOR_ALIGN_CENTER_TOP,
  91. ANCHOR_ALIGN_CENTER_BOTTOM,
  92. ANCHOR_ALIGN_CENTER,
  93. ANCHOR_ALIGN_TOP_WIDE,
  94. ANCHOR_ALIGN_LEFT_WIDE,
  95. ANCHOR_ALIGN_RIGHT_WIDE,
  96. ANCHOR_ALIGN_BOTTOM_WIDE,
  97. ANCHOR_ALIGN_VCENTER_WIDE,
  98. ANCHOR_ALIGN_HCENTER_WIDE,
  99. ANCHOR_ALIGN_WIDE,
  100. SPACE_HORIZONTAL,
  101. SPACE_VERTICAL,
  102. EXPAND_TO_PARENT,
  103. ANIM_INSERT_KEY,
  104. ANIM_INSERT_KEY_EXISTING,
  105. ANIM_INSERT_POS,
  106. ANIM_INSERT_ROT,
  107. ANIM_INSERT_SCALE,
  108. ANIM_COPY_POSE,
  109. ANIM_PASTE_POSE,
  110. ANIM_CLEAR_POSE,
  111. VIEW_CENTER_TO_SELECTION,
  112. VIEW_FRAME_TO_SELECTION,
  113. SKELETON_MAKE_BONES,
  114. SKELETON_CLEAR_BONES,
  115. SKELETON_SET_IK_CHAIN,
  116. SKELETON_CLEAR_IK_CHAIN
  117. };
  118. enum DragType {
  119. DRAG_NONE,
  120. DRAG_LEFT,
  121. DRAG_TOP_LEFT,
  122. DRAG_TOP,
  123. DRAG_TOP_RIGHT,
  124. DRAG_RIGHT,
  125. DRAG_BOTTOM_RIGHT,
  126. DRAG_BOTTOM,
  127. DRAG_BOTTOM_LEFT,
  128. DRAG_ALL,
  129. DRAG_ROTATE,
  130. DRAG_PIVOT,
  131. DRAG_NODE_2D,
  132. };
  133. enum KeyMoveMODE {
  134. MOVE_VIEW_BASE,
  135. MOVE_LOCAL_BASE,
  136. MOVE_LOCAL_WITH_ROT
  137. };
  138. EditorSelection *editor_selection;
  139. bool additive_selection;
  140. Tool tool;
  141. bool first_update;
  142. Control *viewport;
  143. bool can_move_pivot;
  144. HScrollBar *h_scroll;
  145. VScrollBar *v_scroll;
  146. HBoxContainer *hb;
  147. Matrix32 transform;
  148. float zoom;
  149. Vector2 snap_offset;
  150. Vector2 snap_step;
  151. float snap_rotation_step;
  152. float snap_rotation_offset;
  153. bool snap_grid;
  154. bool snap_show_grid;
  155. bool snap_rotation;
  156. bool snap_relative;
  157. bool snap_pixel;
  158. bool box_selecting;
  159. Point2 box_selecting_to;
  160. bool key_pos;
  161. bool key_rot;
  162. bool key_scale;
  163. void _tool_select(int p_index);
  164. MenuOption last_option;
  165. struct _SelectResult {
  166. CanvasItem *item;
  167. float z;
  168. bool has_z;
  169. _FORCE_INLINE_ bool operator<(const _SelectResult &p_rr) const {
  170. return has_z && p_rr.has_z ? p_rr.z < z : p_rr.has_z;
  171. }
  172. };
  173. Vector<_SelectResult> selection_results;
  174. struct LockList {
  175. Point2 pos;
  176. bool lock;
  177. bool group;
  178. LockList() {
  179. lock = false;
  180. group = false;
  181. }
  182. };
  183. List<LockList> lock_list;
  184. struct BoneList {
  185. Matrix32 xform;
  186. Vector2 from;
  187. Vector2 to;
  188. ObjectID bone;
  189. uint64_t last_pass;
  190. };
  191. uint64_t bone_last_frame;
  192. Map<ObjectID, BoneList> bone_list;
  193. Matrix32 bone_orig_xform;
  194. struct BoneIK {
  195. Variant orig_state;
  196. Vector2 pos;
  197. float len;
  198. Node2D *node;
  199. };
  200. List<BoneIK> bone_ik_list;
  201. struct PoseClipboard {
  202. Vector2 pos;
  203. Vector2 scale;
  204. float rot;
  205. ObjectID id;
  206. };
  207. List<PoseClipboard> pose_clipboard;
  208. ToolButton *select_button;
  209. ToolButton *list_select_button;
  210. ToolButton *move_button;
  211. ToolButton *rotate_button;
  212. ToolButton *pivot_button;
  213. ToolButton *pan_button;
  214. ToolButton *lock_button;
  215. ToolButton *unlock_button;
  216. ToolButton *group_button;
  217. ToolButton *ungroup_button;
  218. MenuButton *edit_menu;
  219. MenuButton *view_menu;
  220. HBoxContainer *animation_hb;
  221. MenuButton *animation_menu;
  222. MenuButton *anchor_menu;
  223. Button *key_loc_button;
  224. Button *key_rot_button;
  225. Button *key_scale_button;
  226. Button *key_insert_button;
  227. PopupMenu *selection_menu;
  228. //PopupMenu *popup;
  229. DragType drag;
  230. Point2 drag_from;
  231. Point2 drag_point_from;
  232. bool updating_value_dialog;
  233. Point2 display_rotate_from;
  234. Point2 display_rotate_to;
  235. #if 0
  236. struct EditInfo {
  237. Variant undo_state;
  238. Matrix32 prev_xform;
  239. float prev_rot;
  240. Rect2 prev_rect;
  241. EditInfo() { prev_rot=0; }
  242. };
  243. typedef Map<CanvasItem*,EditInfo> CanvasItemMap;
  244. CanvasItemMap canvas_items;
  245. #endif
  246. Ref<StyleBoxTexture> select_sb;
  247. Ref<Texture> select_handle;
  248. int handle_len;
  249. bool _is_part_of_subscene(CanvasItem *p_item);
  250. CanvasItem *_select_canvas_item_at_pos(const Point2 &p_pos, Node *p_node, const Matrix32 &p_parent_xform, const Matrix32 &p_canvas_xform);
  251. void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, const Matrix32 &p_parent_xform, const Matrix32 &p_canvas_xform, Vector<_SelectResult> &r_items);
  252. void _find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_node, const Matrix32 &p_parent_xform, const Matrix32 &p_canvas_xform, List<CanvasItem *> *r_items);
  253. bool _select(CanvasItem *item, Point2 p_click_pos, bool p_append, bool p_drag = true);
  254. ConfirmationDialog *snap_dialog;
  255. AcceptDialog *value_dialog;
  256. Label *dialog_label;
  257. SpinBox *dialog_val;
  258. CanvasItem *ref_item;
  259. void _edit_set_pivot(const Vector2 &mouse_pos);
  260. void _add_canvas_item(CanvasItem *p_canvas_item);
  261. void _remove_canvas_item(CanvasItem *p_canvas_item);
  262. void _clear_canvas_items();
  263. void _visibility_changed(ObjectID p_canvas_item);
  264. void _key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE p_move_mode);
  265. void _list_select(const InputEventMouseButton &b);
  266. DragType _find_drag_type(const Matrix32 &p_xform, const Rect2 &p_local_rect, const Point2 &p_click, Vector2 &r_point);
  267. void _prepare_drag(const Point2 &p_click_pos);
  268. void _popup_callback(int p_op);
  269. bool updating_scroll;
  270. void _update_scroll(float);
  271. void _update_scrollbars();
  272. void incbeg(float &beg, float &end, float inc, float minsize, bool p_symmetric);
  273. void incend(float &beg, float &end, float inc, float minsize, bool p_symmetric);
  274. void _append_canvas_item(CanvasItem *p_item);
  275. void _dialog_value_changed(double);
  276. void _snap_changed();
  277. void _selection_result_pressed(int);
  278. void _selection_menu_hide();
  279. UndoRedo *undo_redo;
  280. Point2 _find_topleftmost_point();
  281. void _find_canvas_items_span(Node *p_node, Rect2 &r_rect, const Matrix32 &p_xform);
  282. Object *_get_editor_data(Object *p_what);
  283. CanvasItem *get_single_item();
  284. int get_item_count();
  285. void _keying_changed();
  286. void _unhandled_key_input(const InputEvent &p_ev);
  287. void _viewport_input_event(const InputEvent &p_event);
  288. void _viewport_draw();
  289. void _set_anchor(Control::AnchorType p_left, Control::AnchorType p_top, Control::AnchorType p_right, Control::AnchorType p_bottom);
  290. HSplitContainer *palette_split;
  291. VSplitContainer *bottom_split;
  292. friend class CanvasItemEditorPlugin;
  293. protected:
  294. void _notification(int p_what);
  295. void _node_removed(Node *p_node);
  296. static void _bind_methods();
  297. void end_drag();
  298. void box_selection_start(Point2 &click);
  299. bool box_selection_end();
  300. HBoxContainer *get_panel_hb() { return hb; }
  301. struct compare_items_x {
  302. bool operator()(const CanvasItem *a, const CanvasItem *b) const {
  303. return a->get_global_transform().elements[2].x < b->get_global_transform().elements[2].x;
  304. }
  305. };
  306. struct compare_items_y {
  307. bool operator()(const CanvasItem *a, const CanvasItem *b) const {
  308. return a->get_global_transform().elements[2].y < b->get_global_transform().elements[2].y;
  309. }
  310. };
  311. struct proj_vector2_x {
  312. float get(const Vector2 &v) { return v.x; }
  313. void set(Vector2 &v, float f) { v.x = f; }
  314. };
  315. struct proj_vector2_y {
  316. float get(const Vector2 &v) { return v.y; }
  317. void set(Vector2 &v, float f) { v.y = f; }
  318. };
  319. template <class P, class C>
  320. void space_selected_items();
  321. static CanvasItemEditor *singleton;
  322. public:
  323. Vector2 snap_point(Vector2 p_target, Vector2 p_start = Vector2(0, 0)) const;
  324. float snap_angle(float p_target, float p_start = 0) const;
  325. Matrix32 get_canvas_transform() const { return transform; }
  326. static CanvasItemEditor *get_singleton() { return singleton; }
  327. Dictionary get_state() const;
  328. void set_state(const Dictionary &p_state);
  329. void add_control_to_menu_panel(Control *p_control);
  330. HSplitContainer *get_palette_split();
  331. VSplitContainer *get_bottom_split();
  332. Control *get_viewport_control() { return viewport; }
  333. bool get_remove_list(List<Node *> *p_list);
  334. void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
  335. void edit(CanvasItem *p_canvas_item);
  336. CanvasItemEditor(EditorNode *p_editor);
  337. };
  338. class CanvasItemEditorPlugin : public EditorPlugin {
  339. OBJ_TYPE(CanvasItemEditorPlugin, EditorPlugin);
  340. CanvasItemEditor *canvas_item_editor;
  341. EditorNode *editor;
  342. public:
  343. virtual String get_name() const { return "2D"; }
  344. bool has_main_screen() const { return true; }
  345. virtual void edit(Object *p_object);
  346. virtual bool handles(Object *p_object) const;
  347. virtual void make_visible(bool p_visible);
  348. virtual bool get_remove_list(List<Node *> *p_list) { return canvas_item_editor->get_remove_list(p_list); }
  349. virtual Dictionary get_state() const;
  350. virtual void set_state(const Dictionary &p_state);
  351. CanvasItemEditor *get_canvas_item_editor() { return canvas_item_editor; }
  352. CanvasItemEditorPlugin(EditorNode *p_node);
  353. ~CanvasItemEditorPlugin();
  354. };
  355. class CanvasItemEditorViewport : public Control {
  356. OBJ_TYPE(CanvasItemEditorViewport, Control);
  357. String default_type;
  358. Vector<String> types;
  359. Vector<String> selected_files;
  360. Node *target_node;
  361. Point2 drop_pos;
  362. EditorNode *editor;
  363. EditorData *editor_data;
  364. CanvasItemEditor *canvas;
  365. Node2D *preview;
  366. AcceptDialog *accept;
  367. WindowDialog *selector;
  368. Label *selector_label;
  369. Label *label;
  370. Label *label_desc;
  371. ButtonGroup *btn_group;
  372. void _on_mouse_exit();
  373. void _on_select_type(Object *selected);
  374. void _on_change_type();
  375. void _create_preview(const Vector<String> &files) const;
  376. void _remove_preview();
  377. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  378. void _create_nodes(Node *parent, Node *child, String &path, const Point2 &p_point);
  379. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  380. void _perform_drop_data();
  381. static void _bind_methods();
  382. protected:
  383. void _notification(int p_what);
  384. public:
  385. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  386. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  387. CanvasItemEditorViewport(EditorNode *p_node, CanvasItemEditor *p_canvas);
  388. };
  389. #endif