Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/2d/abstract_polygon_2d_editor.h
9903 views
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
#pragma once
32
33
#include "editor/plugins/editor_plugin.h"
34
#include "scene/2d/node_2d.h"
35
#include "scene/gui/box_container.h"
36
37
class Button;
38
class CanvasItemEditor;
39
class ConfirmationDialog;
40
41
class AbstractPolygon2DEditor : public HBoxContainer {
42
GDCLASS(AbstractPolygon2DEditor, HBoxContainer);
43
44
Button *button_create = nullptr;
45
Button *button_edit = nullptr;
46
Button *button_delete = nullptr;
47
Button *button_center = 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
Vector2 original_mouse_pos;
83
84
Vector<Vector2> pre_move_edit;
85
Vector<Vector2> wip;
86
bool wip_active = false;
87
bool wip_destructive = false;
88
89
Vector<Vector<Vector2>> pre_center_move_edit;
90
bool center_drag = false;
91
Vector2 center_drag_origin;
92
bool edit_origin_and_center = false;
93
94
bool _polygon_editing_enabled = false;
95
96
CanvasItemEditor *canvas_item_editor = nullptr;
97
Panel *panel = nullptr;
98
ConfirmationDialog *create_resource = nullptr;
99
100
protected:
101
enum {
102
MODE_CREATE,
103
MODE_EDIT,
104
MODE_DELETE,
105
MODE_CONT,
106
CENTER_POLY,
107
};
108
109
int mode = MODE_EDIT;
110
111
virtual void _menu_option(int p_option);
112
void _wip_changed();
113
void _wip_close();
114
void _wip_cancel();
115
116
void _notification(int p_what);
117
void _node_removed(Node *p_node);
118
119
bool _commit_drag();
120
121
void remove_point(const Vertex &p_vertex);
122
Vertex get_active_point() const;
123
PosVertex closest_point(const Vector2 &p_pos) const;
124
PosVertex closest_edge_point(const Vector2 &p_pos) const;
125
126
bool _is_empty() const;
127
128
virtual Node2D *_get_node() const = 0;
129
virtual void _set_node(Node *p_polygon) = 0;
130
131
virtual bool _is_line() const;
132
virtual bool _has_uv() const;
133
virtual int _get_polygon_count() const;
134
virtual Vector2 _get_offset(int p_idx) const;
135
virtual Variant _get_polygon(int p_idx) const;
136
virtual void _set_polygon(int p_idx, const Variant &p_polygon) const;
137
138
virtual void _action_add_polygon(const Variant &p_polygon);
139
virtual void _action_remove_polygon(int p_idx);
140
virtual void _action_set_polygon(int p_idx, const Variant &p_polygon);
141
virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon);
142
virtual void _commit_action();
143
144
virtual Vector2 _get_geometric_center() const;
145
146
virtual bool _has_resource() const;
147
virtual void _create_resource();
148
149
public:
150
void disable_polygon_editing(bool p_disable, const String &p_reason);
151
152
bool forward_gui_input(const Ref<InputEvent> &p_event);
153
void forward_canvas_draw_over_viewport(Control *p_overlay);
154
void set_edit_origin_and_center(bool p_enabled);
155
156
void edit(Node *p_polygon);
157
AbstractPolygon2DEditor(bool p_wip_destructive = true);
158
};
159
160
class AbstractPolygon2DEditorPlugin : public EditorPlugin {
161
GDCLASS(AbstractPolygon2DEditorPlugin, EditorPlugin);
162
163
AbstractPolygon2DEditor *polygon_editor = nullptr;
164
String klass;
165
166
public:
167
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return polygon_editor->forward_gui_input(p_event); }
168
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }
169
170
bool has_main_screen() const override { return false; }
171
virtual String get_plugin_name() const override { return klass; }
172
virtual void edit(Object *p_object) override;
173
virtual bool handles(Object *p_object) const override;
174
virtual void make_visible(bool p_visible) override;
175
176
AbstractPolygon2DEditorPlugin(AbstractPolygon2DEditor *p_polygon_editor, const String &p_class);
177
};
178
179