1 /*************************************************************************/
2 /*  polygon_2d_editor_plugin.cpp                                         */
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 POLYGON_2D_EDITOR_PLUGIN_H
31 #define POLYGON_2D_EDITOR_PLUGIN_H
32 
33 #include "editor/editor_node.h"
34 #include "editor/editor_plugin.h"
35 #include "scene/2d/polygon_2d.h"
36 #include "scene/gui/button_group.h"
37 #include "scene/gui/tool_button.h"
38 
39 /**
40 	@author Juan Linietsky <reduzio@gmail.com>
41 */
42 class CanvasItemEditor;
43 
44 class Polygon2DEditor : public HBoxContainer {
45 
46 	OBJ_TYPE(Polygon2DEditor, HBoxContainer);
47 
48 	UndoRedo *undo_redo;
49 	enum Mode {
50 
51 		MODE_CREATE,
52 		MODE_EDIT,
53 		MODE_EDIT_UV,
54 		UVEDIT_POLYGON_TO_UV,
55 		UVEDIT_UV_TO_POLYGON,
56 		UVEDIT_UV_CLEAR
57 
58 	};
59 
60 	enum UVMode {
61 		UV_MODE_EDIT_POINT,
62 		UV_MODE_MOVE,
63 		UV_MODE_ROTATE,
64 		UV_MODE_SCALE,
65 		UV_MODE_MAX
66 	};
67 
68 	Mode mode;
69 
70 	UVMode uv_mode;
71 	AcceptDialog *uv_edit;
72 	ToolButton *uv_button[4];
73 	ToolButton *b_snap_enable;
74 	ToolButton *b_snap_grid;
75 	Control *uv_edit_draw;
76 	HSlider *uv_zoom;
77 	SpinBox *uv_zoom_value;
78 	HScrollBar *uv_hscroll;
79 	VScrollBar *uv_vscroll;
80 	MenuButton *uv_menu;
81 	TextureFrame *uv_icon_zoom;
82 
83 	Vector2 uv_draw_ofs;
84 	float uv_draw_zoom;
85 	DVector<Vector2> uv_prev;
86 	int uv_drag_index;
87 	bool uv_drag;
88 	UVMode uv_move_current;
89 	Vector2 uv_drag_from;
90 	bool updating_uv_scroll;
91 
92 	AcceptDialog *error;
93 
94 	ToolButton *button_create;
95 	ToolButton *button_edit;
96 	ToolButton *button_uv;
97 
98 	CanvasItemEditor *canvas_item_editor;
99 	EditorNode *editor;
100 	Panel *panel;
101 	Polygon2D *node;
102 	MenuButton *options;
103 
104 	int edited_point;
105 	Vector2 edited_point_pos;
106 	Vector<Vector2> pre_move_edit;
107 	Vector<Vector2> wip;
108 	bool wip_active;
109 
110 	bool use_snap;
111 	bool snap_show_grid;
112 	Vector2 snap_offset;
113 	Vector2 snap_step;
114 
115 	void _uv_scroll_changed(float);
116 	void _uv_input(const InputEvent &p_input);
117 	void _uv_draw();
118 	void _uv_mode(int p_mode);
119 	void _wip_close();
120 	void _canvas_draw();
121 	void _menu_option(int p_option);
122 
123 	void _set_use_snap(bool p_use);
124 	void _set_show_grid(bool p_show);
125 	void _set_snap_off_x(float p_val);
126 	void _set_snap_off_y(float p_val);
127 	void _set_snap_step_x(float p_val);
128 	void _set_snap_step_y(float p_val);
129 
130 protected:
131 	void _notification(int p_what);
132 	void _node_removed(Node *p_node);
133 	static void _bind_methods();
134 
135 	Vector2 snap_point(Vector2 p_target) const;
136 
137 public:
138 	bool forward_input_event(const InputEvent &p_event);
139 	void edit(Node *p_collision_polygon);
140 	Polygon2DEditor(EditorNode *p_editor);
141 };
142 
143 class Polygon2DEditorPlugin : public EditorPlugin {
144 
145 	OBJ_TYPE(Polygon2DEditorPlugin, EditorPlugin);
146 
147 	Polygon2DEditor *collision_polygon_editor;
148 	EditorNode *editor;
149 
150 public:
forward_input_event(const InputEvent & p_event)151 	virtual bool forward_input_event(const InputEvent &p_event) { return collision_polygon_editor->forward_input_event(p_event); }
152 
get_name()153 	virtual String get_name() const { return "Polygon2D"; }
has_main_screen()154 	bool has_main_screen() const { return false; }
155 	virtual void edit(Object *p_node);
156 	virtual bool handles(Object *p_node) const;
157 	virtual void make_visible(bool p_visible);
158 
159 	Polygon2DEditorPlugin(EditorNode *p_node);
160 	~Polygon2DEditorPlugin();
161 };
162 
163 #endif // POLYGON_2D_EDITOR_PLUGIN_H
164