abstract_polygon_2d_editor.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* abstract_polygon_2d_editor.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 ABSTRACT_POLYGON_2D_EDITOR_H
  31. #define ABSTRACT_POLYGON_2D_EDITOR_H
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/2d/polygon_2d.h"
  35. #include "scene/gui/tool_button.h"
  36. class CanvasItemEditor;
  37. class AbstractPolygon2DEditor : public HBoxContainer {
  38. GDCLASS(AbstractPolygon2DEditor, HBoxContainer);
  39. ToolButton *button_create;
  40. ToolButton *button_edit;
  41. ToolButton *button_delete;
  42. struct Vertex {
  43. Vertex();
  44. Vertex(int p_vertex);
  45. Vertex(int p_polygon, int p_vertex);
  46. bool operator==(const Vertex &p_vertex) const;
  47. bool operator!=(const Vertex &p_vertex) const;
  48. bool valid() const;
  49. int polygon;
  50. int vertex;
  51. };
  52. struct PosVertex : public Vertex {
  53. PosVertex();
  54. PosVertex(const Vertex &p_vertex, const Vector2 &p_pos);
  55. PosVertex(int p_polygon, int p_vertex, const Vector2 &p_pos);
  56. Vector2 pos;
  57. };
  58. PosVertex edited_point;
  59. Vertex hover_point; // point under mouse cursor
  60. Vertex selected_point; // currently selected
  61. PosVertex edge_point; // adding an edge point?
  62. Vector<Vector2> pre_move_edit;
  63. Vector<Vector2> wip;
  64. bool wip_active;
  65. bool wip_destructive;
  66. bool _polygon_editing_enabled;
  67. CanvasItemEditor *canvas_item_editor;
  68. EditorNode *editor;
  69. Panel *panel;
  70. ConfirmationDialog *create_resource;
  71. protected:
  72. enum {
  73. MODE_CREATE,
  74. MODE_EDIT,
  75. MODE_DELETE,
  76. MODE_CONT,
  77. };
  78. int mode;
  79. UndoRedo *undo_redo;
  80. virtual void _menu_option(int p_option);
  81. void _wip_changed();
  82. void _wip_close();
  83. void _wip_cancel();
  84. void _notification(int p_what);
  85. void _node_removed(Node *p_node);
  86. static void _bind_methods();
  87. void remove_point(const Vertex &p_vertex);
  88. Vertex get_active_point() const;
  89. PosVertex closest_point(const Vector2 &p_pos) const;
  90. PosVertex closest_edge_point(const Vector2 &p_pos) const;
  91. bool _is_empty() const;
  92. virtual Node2D *_get_node() const = 0;
  93. virtual void _set_node(Node *p_polygon) = 0;
  94. virtual bool _is_line() const;
  95. virtual bool _has_uv() const;
  96. virtual int _get_polygon_count() const;
  97. virtual Vector2 _get_offset(int p_idx) const;
  98. virtual Variant _get_polygon(int p_idx) const;
  99. virtual void _set_polygon(int p_idx, const Variant &p_polygon) const;
  100. virtual void _action_add_polygon(const Variant &p_polygon);
  101. virtual void _action_remove_polygon(int p_idx);
  102. virtual void _action_set_polygon(int p_idx, const Variant &p_polygon);
  103. virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon);
  104. virtual void _commit_action();
  105. virtual bool _has_resource() const;
  106. virtual void _create_resource();
  107. public:
  108. void disable_polygon_editing(bool p_disable, String p_reason);
  109. bool forward_gui_input(const Ref<InputEvent> &p_event);
  110. void forward_canvas_draw_over_viewport(Control *p_overlay);
  111. void edit(Node *p_polygon);
  112. AbstractPolygon2DEditor(EditorNode *p_editor, bool p_wip_destructive = true);
  113. };
  114. class AbstractPolygon2DEditorPlugin : public EditorPlugin {
  115. GDCLASS(AbstractPolygon2DEditorPlugin, EditorPlugin);
  116. AbstractPolygon2DEditor *polygon_editor;
  117. EditorNode *editor;
  118. String klass;
  119. public:
  120. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return polygon_editor->forward_gui_input(p_event); }
  121. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }
  122. bool has_main_screen() const { return false; }
  123. virtual String get_name() const { return klass; }
  124. virtual void edit(Object *p_object);
  125. virtual bool handles(Object *p_object) const;
  126. virtual void make_visible(bool p_visible);
  127. AbstractPolygon2DEditorPlugin(EditorNode *p_node, AbstractPolygon2DEditor *p_polygon_editor, String p_class);
  128. ~AbstractPolygon2DEditorPlugin();
  129. };
  130. #endif // ABSTRACT_POLYGON_2D_EDITOR_H