abstract_polygon_2d_editor.h 5.8 KB

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