1 /*************************************************************************/
2 /*  tile_map_editor_plugin.h                                             */
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 TILE_MAP_EDITOR_PLUGIN_H
31 #define TILE_MAP_EDITOR_PLUGIN_H
32 
33 #include "editor/editor_node.h"
34 #include "editor/editor_plugin.h"
35 
36 #include "scene/2d/tile_map.h"
37 #include "scene/gui/label.h"
38 #include "scene/gui/line_edit.h"
39 #include "scene/gui/menu_button.h"
40 #include "scene/gui/tool_button.h"
41 
42 /**
43 	@author Juan Linietsky <reduzio@gmail.com>
44 */
45 
46 class TileMapEditor : public VBoxContainer {
47 
48 	OBJ_TYPE(TileMapEditor, VBoxContainer);
49 
50 	enum Tool {
51 
52 		TOOL_NONE,
53 		TOOL_PAINTING,
54 		TOOL_ERASING,
55 		TOOL_RECTANGLE_PAINT,
56 		TOOL_RECTANGLE_ERASE,
57 		TOOL_LINE_PAINT,
58 		TOOL_LINE_ERASE,
59 		TOOL_SELECTING,
60 		TOOL_BUCKET,
61 		TOOL_PICKING,
62 		TOOL_DUPLICATING,
63 	};
64 
65 	enum Options {
66 
67 		OPTION_BUCKET,
68 		OPTION_PICK_TILE,
69 		OPTION_SELECT,
70 		OPTION_DUPLICATE,
71 		OPTION_ERASE_SELECTION
72 	};
73 
74 	TileMap *node;
75 
76 	EditorNode *editor;
77 	UndoRedo *undo_redo;
78 	Control *canvas_item_editor;
79 
80 	LineEdit *search_box;
81 	HSlider *size_slider;
82 	ItemList *palette;
83 
84 	HBoxContainer *toolbar;
85 
86 	Label *tile_info;
87 	MenuButton *options;
88 	ToolButton *transp;
89 	ToolButton *mirror_x;
90 	ToolButton *mirror_y;
91 	ToolButton *rotate_0;
92 	ToolButton *rotate_90;
93 	ToolButton *rotate_180;
94 	ToolButton *rotate_270;
95 
96 	Tool tool;
97 
98 	bool selection_active;
99 	bool mouse_over;
100 	bool show_tile_info;
101 
102 	bool flip_h;
103 	bool flip_v;
104 	bool transpose;
105 
106 	Point2i rectangle_begin;
107 	Rect2i rectangle;
108 
109 	Point2i over_tile;
110 
111 	bool *bucket_cache_visited;
112 	Rect2i bucket_cache_rect;
113 	int bucket_cache_tile;
114 	DVector<Vector2> bucket_cache;
115 
116 	struct CellOp {
117 		int idx;
118 		bool xf;
119 		bool yf;
120 		bool tr;
121 
CellOpCellOp122 		CellOp() {
123 			idx = -1;
124 			xf = false;
125 			yf = false;
126 			tr = false;
127 		}
128 	};
129 
130 	Map<Point2i, CellOp> paint_undo;
131 
132 	struct TileData {
133 		Point2i pos;
134 		int cell;
135 		bool flip_h;
136 		bool flip_v;
137 		bool transpose;
138 	};
139 
140 	List<TileData> copydata;
141 
142 	void _pick_tile(const Point2 &p_pos);
143 
144 	DVector<Vector2> _bucket_fill(const Point2i &p_start, bool erase = false, bool preview = false);
145 
146 	void _fill_points(const DVector<Vector2> p_points, const Dictionary &p_op);
147 	void _erase_points(const DVector<Vector2> p_points);
148 
149 	void _select(const Point2i &p_from, const Point2i &p_to);
150 
151 	void _draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Matrix32 &p_xform);
152 	void _draw_fill_preview(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Matrix32 &p_xform);
153 	void _clear_bucket_cache();
154 
155 	void _update_copydata();
156 
157 	int get_selected_tile() const;
158 	void set_selected_tile(int p_tile);
159 
160 	void _text_entered(const String &p_text);
161 	void _text_changed(const String &p_text);
162 	void _sbox_input(const InputEvent &p_ie);
163 	void _update_palette();
164 	void _canvas_draw();
165 	void _menu_option(int p_option);
166 
167 	void _set_cell(const Point2i &p_pos, int p_value, bool p_flip_h = false, bool p_flip_v = false, bool p_transpose = false, bool p_with_undo = false);
168 
169 	void _canvas_mouse_enter();
170 	void _canvas_mouse_exit();
171 	void _tileset_settings_changed();
172 	void _icon_size_changed(float p_value);
173 
174 protected:
175 	void _notification(int p_what);
176 	static void _bind_methods();
177 	CellOp _get_op_from_cell(const Point2i &p_pos);
178 	void _update_transform_buttons(Object *p_button = NULL);
179 
180 public:
get_toolbar()181 	HBoxContainer *get_toolbar() const { return toolbar; }
182 
183 	bool forward_input_event(const InputEvent &p_event);
184 	void edit(Node *p_tile_map);
185 
186 	TileMapEditor(EditorNode *p_editor);
187 	~TileMapEditor();
188 };
189 
190 class TileMapEditorPlugin : public EditorPlugin {
191 
192 	OBJ_TYPE(TileMapEditorPlugin, EditorPlugin);
193 
194 	TileMapEditor *tile_map_editor;
195 
196 public:
forward_input_event(const InputEvent & p_event)197 	virtual bool forward_input_event(const InputEvent &p_event) { return tile_map_editor->forward_input_event(p_event); }
198 
get_name()199 	virtual String get_name() const { return "TileMap"; }
has_main_screen()200 	bool has_main_screen() const { return false; }
201 	virtual void edit(Object *p_node);
202 	virtual bool handles(Object *p_node) const;
203 	virtual void make_visible(bool p_visible);
204 
205 	TileMapEditorPlugin(EditorNode *p_node);
206 	~TileMapEditorPlugin();
207 };
208 
209 #endif // TILE_MAP_EDITOR_PLUGIN_H
210