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