abstract_polygon_2d_editor.h 6.1 KB

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