summaryrefslogtreecommitdiff
path: root/editor/plugins/abstract_polygon_2d_editor.h
blob: 0c7b160db4467228f89e611ea9ca7de9e688c499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*************************************************************************/
/*  abstract_polygon_2d_editor.h                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef ABSTRACT_POLYGON_2D_EDITOR_H
#define ABSTRACT_POLYGON_2D_EDITOR_H

#include "editor/editor_plugin.h"
#include "scene/2d/polygon_2d.h"
#include "scene/gui/box_container.h"

class EditorNode;
class CanvasItemEditor;

class AbstractPolygon2DEditor : public HBoxContainer {
	GDCLASS(AbstractPolygon2DEditor, HBoxContainer);

	Button *button_create;
	Button *button_edit;
	Button *button_delete;

	struct Vertex {
		Vertex() {}
		Vertex(int p_vertex) :
				vertex(p_vertex) {}
		Vertex(int p_polygon, int p_vertex) :
				polygon(p_polygon),
				vertex(p_vertex) {}

		bool operator==(const Vertex &p_vertex) const;
		bool operator!=(const Vertex &p_vertex) const;

		bool valid() const;

		int polygon = -1;
		int vertex = -1;
	};

	struct PosVertex : public Vertex {
		PosVertex() {}
		PosVertex(const Vertex &p_vertex, const Vector2 &p_pos) :
				Vertex(p_vertex.polygon, p_vertex.vertex),
				pos(p_pos) {}
		PosVertex(int p_polygon, int p_vertex, const Vector2 &p_pos) :
				Vertex(p_polygon, p_vertex),
				pos(p_pos) {}

		Vector2 pos;
	};

	PosVertex edited_point;
	Vertex hover_point; // point under mouse cursor
	Vertex selected_point; // currently selected
	PosVertex edge_point; // adding an edge point?

	Vector<Vector2> pre_move_edit;
	Vector<Vector2> wip;
	bool wip_active;
	bool wip_destructive;

	bool _polygon_editing_enabled;

	CanvasItemEditor *canvas_item_editor;
	Panel *panel;
	ConfirmationDialog *create_resource;

protected:
	enum {
		MODE_CREATE,
		MODE_EDIT,
		MODE_DELETE,
		MODE_CONT,
	};

	int mode;

	UndoRedo *undo_redo;

	virtual void _menu_option(int p_option);
	void _wip_changed();
	void _wip_close();
	void _wip_cancel();

	void _notification(int p_what);
	void _node_removed(Node *p_node);
	static void _bind_methods();

	void remove_point(const Vertex &p_vertex);
	Vertex get_active_point() const;
	PosVertex closest_point(const Vector2 &p_pos) const;
	PosVertex closest_edge_point(const Vector2 &p_pos) const;

	bool _is_empty() const;

	virtual Node2D *_get_node() const = 0;
	virtual void _set_node(Node *p_polygon) = 0;

	virtual bool _is_line() const;
	virtual bool _has_uv() const;
	virtual int _get_polygon_count() const;
	virtual Vector2 _get_offset(int p_idx) const;
	virtual Variant _get_polygon(int p_idx) const;
	virtual void _set_polygon(int p_idx, const Variant &p_polygon) const;

	virtual void _action_add_polygon(const Variant &p_polygon);
	virtual void _action_remove_polygon(int p_idx);
	virtual void _action_set_polygon(int p_idx, const Variant &p_polygon);
	virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon);
	virtual void _commit_action();

	virtual bool _has_resource() const;
	virtual void _create_resource();

public:
	void disable_polygon_editing(bool p_disable, String p_reason);

	bool forward_gui_input(const Ref<InputEvent> &p_event);
	void forward_canvas_draw_over_viewport(Control *p_overlay);

	void edit(Node *p_polygon);
	AbstractPolygon2DEditor(bool p_wip_destructive = true);
};

class AbstractPolygon2DEditorPlugin : public EditorPlugin {
	GDCLASS(AbstractPolygon2DEditorPlugin, EditorPlugin);

	AbstractPolygon2DEditor *polygon_editor;
	String klass;

public:
	virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return polygon_editor->forward_gui_input(p_event); }
	virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }

	bool has_main_screen() const override { return false; }
	virtual String get_name() const override { return klass; }
	virtual void edit(Object *p_object) override;
	virtual bool handles(Object *p_object) const override;
	virtual void make_visible(bool p_visible) override;

	AbstractPolygon2DEditorPlugin(AbstractPolygon2DEditor *p_polygon_editor, String p_class);
	~AbstractPolygon2DEditorPlugin();
};

#endif // ABSTRACT_POLYGON_2D_EDITOR_H