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