1 | /**************************************************************************/ |
2 | /* tile_map_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 TILE_MAP_EDITOR_H |
32 | #define TILE_MAP_EDITOR_H |
33 | |
34 | #include "tile_atlas_view.h" |
35 | |
36 | #include "core/os/thread.h" |
37 | #include "core/typedefs.h" |
38 | #include "scene/2d/tile_map.h" |
39 | #include "scene/gui/box_container.h" |
40 | #include "scene/gui/check_box.h" |
41 | #include "scene/gui/flow_container.h" |
42 | #include "scene/gui/item_list.h" |
43 | #include "scene/gui/menu_button.h" |
44 | #include "scene/gui/option_button.h" |
45 | #include "scene/gui/separator.h" |
46 | #include "scene/gui/spin_box.h" |
47 | #include "scene/gui/split_container.h" |
48 | #include "scene/gui/tab_bar.h" |
49 | #include "scene/gui/tree.h" |
50 | |
51 | class TileMapEditor; |
52 | |
53 | class TileMapSubEditorPlugin : public Object { |
54 | public: |
55 | struct TabData { |
56 | Control *toolbar = nullptr; |
57 | Control *panel = nullptr; |
58 | }; |
59 | |
60 | virtual Vector<TabData> get_tabs() const { |
61 | return Vector<TabData>(); |
62 | }; |
63 | |
64 | virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return false; }; |
65 | virtual void forward_canvas_draw_over_viewport(Control *p_overlay){}; |
66 | virtual void tile_set_changed(){}; |
67 | virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer){}; |
68 | }; |
69 | |
70 | class TileMapEditorTilesPlugin : public TileMapSubEditorPlugin { |
71 | GDCLASS(TileMapEditorTilesPlugin, TileMapSubEditorPlugin); |
72 | |
73 | public: |
74 | enum { |
75 | TRANSFORM_ROTATE_LEFT, |
76 | TRANSFORM_ROTATE_RIGHT, |
77 | TRANSFORM_FLIP_H, |
78 | TRANSFORM_FLIP_V, |
79 | }; |
80 | |
81 | private: |
82 | ObjectID tile_map_id; |
83 | int tile_map_layer = -1; |
84 | virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer) override; |
85 | |
86 | ///// Toolbar ///// |
87 | HBoxContainer *toolbar = nullptr; |
88 | |
89 | Ref<ButtonGroup> tool_buttons_group; |
90 | Button *select_tool_button = nullptr; |
91 | Button *paint_tool_button = nullptr; |
92 | Button *line_tool_button = nullptr; |
93 | Button *rect_tool_button = nullptr; |
94 | Button *bucket_tool_button = nullptr; |
95 | |
96 | HBoxContainer *tools_settings = nullptr; |
97 | |
98 | VSeparator *tools_settings_vsep = nullptr; |
99 | Button *picker_button = nullptr; |
100 | Button *erase_button = nullptr; |
101 | |
102 | HBoxContainer *transform_toolbar = nullptr; |
103 | Button *transform_button_rotate_left = nullptr; |
104 | Button *transform_button_rotate_right = nullptr; |
105 | Button *transform_button_flip_h = nullptr; |
106 | Button *transform_button_flip_v = nullptr; |
107 | |
108 | VSeparator *tools_settings_vsep_2 = nullptr; |
109 | CheckBox *bucket_contiguous_checkbox = nullptr; |
110 | Button *random_tile_toggle = nullptr; |
111 | |
112 | HBoxContainer *scatter_controls_container = nullptr; |
113 | float scattering = 0.0; |
114 | Label *scatter_label = nullptr; |
115 | SpinBox *scatter_spinbox = nullptr; |
116 | void _on_random_tile_checkbox_toggled(bool p_pressed); |
117 | void _on_scattering_spinbox_changed(double p_value); |
118 | |
119 | void _update_toolbar(); |
120 | void _update_transform_buttons(); |
121 | |
122 | ///// Tilemap editing. ///// |
123 | bool has_mouse = false; |
124 | void _mouse_exited_viewport(); |
125 | |
126 | enum DragType { |
127 | DRAG_TYPE_NONE = 0, |
128 | DRAG_TYPE_SELECT, |
129 | DRAG_TYPE_MOVE, |
130 | DRAG_TYPE_PAINT, |
131 | DRAG_TYPE_LINE, |
132 | DRAG_TYPE_RECT, |
133 | DRAG_TYPE_BUCKET, |
134 | DRAG_TYPE_PICK, |
135 | DRAG_TYPE_CLIPBOARD_PASTE, |
136 | }; |
137 | DragType drag_type = DRAG_TYPE_NONE; |
138 | bool drag_erasing = false; |
139 | Vector2 drag_start_mouse_pos; |
140 | Vector2 drag_last_mouse_pos; |
141 | HashMap<Vector2i, TileMapCell> drag_modified; |
142 | |
143 | TileMapCell _pick_random_tile(Ref<TileMapPattern> p_pattern); |
144 | HashMap<Vector2i, TileMapCell> _draw_line(Vector2 p_start_drag_mouse_pos, Vector2 p_from_mouse_pos, Vector2 p_to_mouse_pos, bool p_erase); |
145 | HashMap<Vector2i, TileMapCell> _draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase); |
146 | HashMap<Vector2i, TileMapCell> _draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase); |
147 | void _stop_dragging(); |
148 | |
149 | void _apply_transform(int p_type); |
150 | int _get_transformed_alternative(int p_alternative_id, int p_transform); |
151 | |
152 | ///// Selection system. ///// |
153 | RBSet<Vector2i> tile_map_selection; |
154 | Ref<TileMapPattern> tile_map_clipboard; |
155 | Ref<TileMapPattern> selection_pattern; |
156 | void _set_tile_map_selection(const TypedArray<Vector2i> &p_selection); |
157 | TypedArray<Vector2i> _get_tile_map_selection() const; |
158 | |
159 | RBSet<TileMapCell> tile_set_selection; |
160 | |
161 | void _update_selection_pattern_from_tilemap_selection(); |
162 | void _update_selection_pattern_from_tileset_tiles_selection(); |
163 | void _update_selection_pattern_from_tileset_pattern_selection(); |
164 | void _update_tileset_selection_from_selection_pattern(); |
165 | void _update_fix_selected_and_hovered(); |
166 | void _fix_invalid_tiles_in_tile_map_selection(); |
167 | |
168 | void patterns_item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index); |
169 | |
170 | ///// Bottom panel common //// |
171 | void _tab_changed(); |
172 | |
173 | ///// Bottom panel tiles //// |
174 | VBoxContainer *tiles_bottom_panel = nullptr; |
175 | Label *missing_source_label = nullptr; |
176 | Label *invalid_source_label = nullptr; |
177 | |
178 | ItemList *sources_list = nullptr; |
179 | MenuButton *source_sort_button = nullptr; |
180 | |
181 | Ref<Texture2D> missing_atlas_texture_icon; |
182 | void _update_tile_set_sources_list(); |
183 | |
184 | void _update_source_display(); |
185 | |
186 | // Atlas sources. |
187 | TileMapCell hovered_tile; |
188 | TileAtlasView *tile_atlas_view = nullptr; |
189 | HSplitContainer *atlas_sources_split_container = nullptr; |
190 | |
191 | bool tile_set_dragging_selection = false; |
192 | Vector2i tile_set_drag_start_mouse_pos; |
193 | |
194 | Control *tile_atlas_control = nullptr; |
195 | void _tile_atlas_control_mouse_exited(); |
196 | void _tile_atlas_control_gui_input(const Ref<InputEvent> &p_event); |
197 | void _tile_atlas_control_draw(); |
198 | |
199 | Control *alternative_tiles_control = nullptr; |
200 | void _tile_alternatives_control_draw(); |
201 | void _tile_alternatives_control_mouse_exited(); |
202 | void _tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event); |
203 | |
204 | void _update_atlas_view(); |
205 | void _set_source_sort(int p_sort); |
206 | |
207 | // Scenes collection sources. |
208 | ItemList *scene_tiles_list = nullptr; |
209 | |
210 | void _update_scenes_collection_view(); |
211 | void _scene_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud); |
212 | void _scenes_list_multi_selected(int p_index, bool p_selected); |
213 | void _scenes_list_lmb_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index); |
214 | |
215 | ///// Bottom panel patterns //// |
216 | VBoxContainer *patterns_bottom_panel = nullptr; |
217 | ItemList *patterns_item_list = nullptr; |
218 | Label *patterns_help_label = nullptr; |
219 | void _patterns_item_list_gui_input(const Ref<InputEvent> &p_event); |
220 | void _pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture); |
221 | bool select_last_pattern = false; |
222 | void _update_patterns_list(); |
223 | |
224 | // General |
225 | void _update_theme(); |
226 | List<BaseButton *> viewport_shortcut_buttons; |
227 | |
228 | // Update callback |
229 | virtual void tile_set_changed() override; |
230 | |
231 | protected: |
232 | static void _bind_methods(); |
233 | |
234 | public: |
235 | virtual Vector<TabData> get_tabs() const override; |
236 | virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override; |
237 | virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override; |
238 | |
239 | TileMapEditorTilesPlugin(); |
240 | ~TileMapEditorTilesPlugin(); |
241 | }; |
242 | |
243 | class TileMapEditorTerrainsPlugin : public TileMapSubEditorPlugin { |
244 | GDCLASS(TileMapEditorTerrainsPlugin, TileMapSubEditorPlugin); |
245 | |
246 | private: |
247 | ObjectID tile_map_id; |
248 | int tile_map_layer = -1; |
249 | virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer) override; |
250 | |
251 | // Toolbar. |
252 | HBoxContainer *toolbar = nullptr; |
253 | |
254 | Ref<ButtonGroup> tool_buttons_group; |
255 | Button *paint_tool_button = nullptr; |
256 | Button *line_tool_button = nullptr; |
257 | Button *rect_tool_button = nullptr; |
258 | Button *bucket_tool_button = nullptr; |
259 | |
260 | HBoxContainer *tools_settings = nullptr; |
261 | |
262 | VSeparator *tools_settings_vsep = nullptr; |
263 | Button *picker_button = nullptr; |
264 | Button *erase_button = nullptr; |
265 | |
266 | VSeparator *tools_settings_vsep_2 = nullptr; |
267 | CheckBox *bucket_contiguous_checkbox = nullptr; |
268 | void _update_toolbar(); |
269 | |
270 | // Main vbox. |
271 | VBoxContainer *main_vbox_container = nullptr; |
272 | |
273 | // TileMap editing. |
274 | bool has_mouse = false; |
275 | void _mouse_exited_viewport(); |
276 | |
277 | enum DragType { |
278 | DRAG_TYPE_NONE = 0, |
279 | DRAG_TYPE_PAINT, |
280 | DRAG_TYPE_LINE, |
281 | DRAG_TYPE_RECT, |
282 | DRAG_TYPE_BUCKET, |
283 | DRAG_TYPE_PICK, |
284 | }; |
285 | DragType drag_type = DRAG_TYPE_NONE; |
286 | bool drag_erasing = false; |
287 | Vector2 drag_start_mouse_pos; |
288 | Vector2 drag_last_mouse_pos; |
289 | HashMap<Vector2i, TileMapCell> drag_modified; |
290 | |
291 | // Painting |
292 | HashMap<Vector2i, TileMapCell> _draw_terrain_path_or_connect(const Vector<Vector2i> &p_to_paint, int p_terrain_set, int p_terrain, bool p_connect) const; |
293 | HashMap<Vector2i, TileMapCell> _draw_terrain_pattern(const Vector<Vector2i> &p_to_paint, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const; |
294 | HashMap<Vector2i, TileMapCell> _draw_line(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase); |
295 | HashMap<Vector2i, TileMapCell> _draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase); |
296 | RBSet<Vector2i> _get_cells_for_bucket_fill(Vector2i p_coords, bool p_contiguous); |
297 | HashMap<Vector2i, TileMapCell> _draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase); |
298 | void _stop_dragging(); |
299 | |
300 | enum SelectedType { |
301 | SELECTED_TYPE_CONNECT = 0, |
302 | SELECTED_TYPE_PATH, |
303 | SELECTED_TYPE_PATTERN, |
304 | }; |
305 | SelectedType selected_type; |
306 | int selected_terrain_set = -1; |
307 | int selected_terrain = -1; |
308 | TileSet::TerrainsPattern selected_terrains_pattern; |
309 | void _update_selection(); |
310 | |
311 | // Bottom panel. |
312 | Tree *terrains_tree = nullptr; |
313 | ItemList *terrains_tile_list = nullptr; |
314 | |
315 | // Cache. |
316 | LocalVector<LocalVector<RBSet<TileSet::TerrainsPattern>>> per_terrain_terrains_patterns; |
317 | List<BaseButton *> viewport_shortcut_buttons; |
318 | |
319 | // Update functions. |
320 | void _update_terrains_cache(); |
321 | void _update_terrains_tree(); |
322 | void _update_tiles_list(); |
323 | void _update_theme(); |
324 | |
325 | // Update callback |
326 | virtual void tile_set_changed() override; |
327 | |
328 | public: |
329 | virtual Vector<TabData> get_tabs() const override; |
330 | virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override; |
331 | virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override; |
332 | |
333 | TileMapEditorTerrainsPlugin(); |
334 | ~TileMapEditorTerrainsPlugin(); |
335 | }; |
336 | |
337 | class TileMapEditor : public VBoxContainer { |
338 | GDCLASS(TileMapEditor, VBoxContainer); |
339 | |
340 | private: |
341 | bool tileset_changed_needs_update = false; |
342 | ObjectID tile_map_id; |
343 | int tile_map_layer = -1; |
344 | |
345 | // Vector to keep plugins. |
346 | Vector<TileMapSubEditorPlugin *> tile_map_editor_plugins; |
347 | |
348 | // Toolbar. |
349 | HFlowContainer *tile_map_toolbar = nullptr; |
350 | |
351 | OptionButton *layers_selection_button = nullptr; |
352 | Button *toggle_highlight_selected_layer_button = nullptr; |
353 | void _layers_selection_item_selected(int p_index); |
354 | |
355 | Button *toggle_grid_button = nullptr; |
356 | void _on_grid_toggled(bool p_pressed); |
357 | |
358 | MenuButton * = nullptr; |
359 | void (int p_id); |
360 | |
361 | // Bottom panel. |
362 | Label *missing_tileset_label = nullptr; |
363 | TabBar *tabs_bar = nullptr; |
364 | LocalVector<TileMapSubEditorPlugin::TabData> tabs_data; |
365 | LocalVector<TileMapSubEditorPlugin *> tabs_plugins; |
366 | void _update_bottom_panel(); |
367 | |
368 | // TileMap. |
369 | Ref<Texture2D> missing_tile_texture; |
370 | Ref<Texture2D> warning_pattern_texture; |
371 | |
372 | // CallBack. |
373 | void _tile_map_changed(); |
374 | void _tab_changed(int p_tab_changed); |
375 | |
376 | // Updates. |
377 | void _layers_select_next_or_previous(bool p_next); |
378 | void _update_layers_selection(); |
379 | |
380 | // Inspector undo/redo callback. |
381 | void _move_tile_map_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos); |
382 | |
383 | protected: |
384 | void _notification(int p_what); |
385 | void _draw_shape(Control *p_control, Rect2 p_region, TileSet::TileShape p_shape, TileSet::TileOffsetAxis p_offset_axis, Color p_color); |
386 | |
387 | public: |
388 | bool forward_canvas_gui_input(const Ref<InputEvent> &p_event); |
389 | void forward_canvas_draw_over_viewport(Control *p_overlay); |
390 | |
391 | void edit(TileMap *p_tile_map); |
392 | |
393 | TileMapEditor(); |
394 | ~TileMapEditor(); |
395 | |
396 | // Static functions. |
397 | static Vector<Vector2i> get_line(TileMap *p_tile_map, Vector2i p_from_cell, Vector2i p_to_cell); |
398 | }; |
399 | |
400 | #endif // TILE_MAP_EDITOR_H |
401 | |