tile_map_editor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* tile_map_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_H
  31. #define TILE_MAP_EDITOR_H
  32. #include "tile_atlas_view.h"
  33. #include "core/os/thread.h"
  34. #include "core/typedefs.h"
  35. #include "scene/2d/tile_map.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/check_box.h"
  38. #include "scene/gui/item_list.h"
  39. #include "scene/gui/menu_button.h"
  40. #include "scene/gui/option_button.h"
  41. #include "scene/gui/separator.h"
  42. #include "scene/gui/spin_box.h"
  43. #include "scene/gui/split_container.h"
  44. #include "scene/gui/tab_bar.h"
  45. #include "scene/gui/tree.h"
  46. class EditorUndoRedoManager;
  47. class TileMapEditorPlugin : public Object {
  48. public:
  49. struct TabData {
  50. Control *toolbar = nullptr;
  51. Control *panel = nullptr;
  52. };
  53. virtual Vector<TabData> get_tabs() const {
  54. return Vector<TabData>();
  55. };
  56. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return false; };
  57. virtual void forward_canvas_draw_over_viewport(Control *p_overlay){};
  58. virtual void tile_set_changed(){};
  59. virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer){};
  60. };
  61. class TileMapEditorTilesPlugin : public TileMapEditorPlugin {
  62. GDCLASS(TileMapEditorTilesPlugin, TileMapEditorPlugin);
  63. private:
  64. Ref<EditorUndoRedoManager> undo_redo;
  65. ObjectID tile_map_id;
  66. int tile_map_layer = -1;
  67. virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer) override;
  68. ///// Toolbar /////
  69. HBoxContainer *toolbar = nullptr;
  70. Ref<ButtonGroup> tool_buttons_group;
  71. Button *select_tool_button = nullptr;
  72. Button *paint_tool_button = nullptr;
  73. Button *line_tool_button = nullptr;
  74. Button *rect_tool_button = nullptr;
  75. Button *bucket_tool_button = nullptr;
  76. HBoxContainer *tools_settings = nullptr;
  77. VSeparator *tools_settings_vsep = nullptr;
  78. Button *picker_button = nullptr;
  79. Button *erase_button = nullptr;
  80. VSeparator *tools_settings_vsep_2 = nullptr;
  81. CheckBox *bucket_contiguous_checkbox = nullptr;
  82. Button *random_tile_toggle = nullptr;
  83. float scattering = 0.0;
  84. Label *scatter_label = nullptr;
  85. SpinBox *scatter_spinbox = nullptr;
  86. void _on_random_tile_checkbox_toggled(bool p_pressed);
  87. void _on_scattering_spinbox_changed(double p_value);
  88. void _update_toolbar();
  89. ///// Tilemap editing. /////
  90. bool has_mouse = false;
  91. void _mouse_exited_viewport();
  92. enum DragType {
  93. DRAG_TYPE_NONE = 0,
  94. DRAG_TYPE_SELECT,
  95. DRAG_TYPE_MOVE,
  96. DRAG_TYPE_PAINT,
  97. DRAG_TYPE_LINE,
  98. DRAG_TYPE_RECT,
  99. DRAG_TYPE_BUCKET,
  100. DRAG_TYPE_PICK,
  101. DRAG_TYPE_CLIPBOARD_PASTE,
  102. };
  103. DragType drag_type = DRAG_TYPE_NONE;
  104. bool drag_erasing = false;
  105. Vector2 drag_start_mouse_pos;
  106. Vector2 drag_last_mouse_pos;
  107. HashMap<Vector2i, TileMapCell> drag_modified;
  108. TileMapCell _pick_random_tile(Ref<TileMapPattern> p_pattern);
  109. HashMap<Vector2i, TileMapCell> _draw_line(Vector2 p_start_drag_mouse_pos, Vector2 p_from_mouse_pos, Vector2 p_to_mouse_pos, bool p_erase);
  110. HashMap<Vector2i, TileMapCell> _draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase);
  111. HashMap<Vector2i, TileMapCell> _draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase);
  112. void _stop_dragging();
  113. ///// Selection system. /////
  114. RBSet<Vector2i> tile_map_selection;
  115. Ref<TileMapPattern> tile_map_clipboard;
  116. Ref<TileMapPattern> selection_pattern;
  117. void _set_tile_map_selection(const TypedArray<Vector2i> &p_selection);
  118. TypedArray<Vector2i> _get_tile_map_selection() const;
  119. RBSet<TileMapCell> tile_set_selection;
  120. void _update_selection_pattern_from_tilemap_selection();
  121. void _update_selection_pattern_from_tileset_tiles_selection();
  122. void _update_selection_pattern_from_tileset_pattern_selection();
  123. void _update_tileset_selection_from_selection_pattern();
  124. void _update_fix_selected_and_hovered();
  125. void _fix_invalid_tiles_in_tile_map_selection();
  126. void patterns_item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index);
  127. ///// Bottom panel common ////
  128. void _tab_changed();
  129. ///// Bottom panel tiles ////
  130. VBoxContainer *tiles_bottom_panel = nullptr;
  131. Label *missing_source_label = nullptr;
  132. Label *invalid_source_label = nullptr;
  133. ItemList *sources_list = nullptr;
  134. MenuButton *source_sort_button = nullptr;
  135. Ref<Texture2D> missing_atlas_texture_icon;
  136. void _update_tile_set_sources_list();
  137. void _update_source_display();
  138. // Atlas sources.
  139. TileMapCell hovered_tile;
  140. TileAtlasView *tile_atlas_view = nullptr;
  141. HSplitContainer *atlas_sources_split_container = nullptr;
  142. bool tile_set_dragging_selection = false;
  143. Vector2i tile_set_drag_start_mouse_pos;
  144. Control *tile_atlas_control = nullptr;
  145. void _tile_atlas_control_mouse_exited();
  146. void _tile_atlas_control_gui_input(const Ref<InputEvent> &p_event);
  147. void _tile_atlas_control_draw();
  148. Control *alternative_tiles_control = nullptr;
  149. void _tile_alternatives_control_draw();
  150. void _tile_alternatives_control_mouse_exited();
  151. void _tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event);
  152. void _update_atlas_view();
  153. void _set_source_sort(int p_sort);
  154. // Scenes collection sources.
  155. ItemList *scene_tiles_list = nullptr;
  156. void _update_scenes_collection_view();
  157. void _scene_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud);
  158. void _scenes_list_multi_selected(int p_index, bool p_selected);
  159. void _scenes_list_lmb_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index);
  160. ///// Bottom panel patterns ////
  161. VBoxContainer *patterns_bottom_panel = nullptr;
  162. ItemList *patterns_item_list = nullptr;
  163. Label *patterns_help_label = nullptr;
  164. void _patterns_item_list_gui_input(const Ref<InputEvent> &p_event);
  165. void _pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture);
  166. bool select_last_pattern = false;
  167. void _update_patterns_list();
  168. // General
  169. void _update_theme();
  170. // Update callback
  171. virtual void tile_set_changed() override;
  172. protected:
  173. static void _bind_methods();
  174. public:
  175. virtual Vector<TabData> get_tabs() const override;
  176. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override;
  177. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override;
  178. TileMapEditorTilesPlugin();
  179. ~TileMapEditorTilesPlugin();
  180. };
  181. class TileMapEditorTerrainsPlugin : public TileMapEditorPlugin {
  182. GDCLASS(TileMapEditorTerrainsPlugin, TileMapEditorPlugin);
  183. private:
  184. Ref<EditorUndoRedoManager> undo_redo;
  185. ObjectID tile_map_id;
  186. int tile_map_layer = -1;
  187. virtual void edit(ObjectID p_tile_map_id, int p_tile_map_layer) override;
  188. // Toolbar.
  189. HBoxContainer *toolbar = nullptr;
  190. Ref<ButtonGroup> tool_buttons_group;
  191. Button *paint_tool_button = nullptr;
  192. Button *line_tool_button = nullptr;
  193. Button *rect_tool_button = nullptr;
  194. Button *bucket_tool_button = nullptr;
  195. HBoxContainer *tools_settings = nullptr;
  196. VSeparator *tools_settings_vsep = nullptr;
  197. Button *picker_button = nullptr;
  198. Button *erase_button = nullptr;
  199. VSeparator *tools_settings_vsep_2 = nullptr;
  200. CheckBox *bucket_contiguous_checkbox = nullptr;
  201. void _update_toolbar();
  202. // Main vbox.
  203. VBoxContainer *main_vbox_container = nullptr;
  204. // TileMap editing.
  205. bool has_mouse = false;
  206. void _mouse_exited_viewport();
  207. enum DragType {
  208. DRAG_TYPE_NONE = 0,
  209. DRAG_TYPE_PAINT,
  210. DRAG_TYPE_LINE,
  211. DRAG_TYPE_RECT,
  212. DRAG_TYPE_BUCKET,
  213. DRAG_TYPE_PICK,
  214. };
  215. DragType drag_type = DRAG_TYPE_NONE;
  216. bool drag_erasing = false;
  217. Vector2 drag_start_mouse_pos;
  218. Vector2 drag_last_mouse_pos;
  219. HashMap<Vector2i, TileMapCell> drag_modified;
  220. // Painting
  221. 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;
  222. HashMap<Vector2i, TileMapCell> _draw_terrain_pattern(const Vector<Vector2i> &p_to_paint, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const;
  223. HashMap<Vector2i, TileMapCell> _draw_line(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase);
  224. HashMap<Vector2i, TileMapCell> _draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase);
  225. RBSet<Vector2i> _get_cells_for_bucket_fill(Vector2i p_coords, bool p_contiguous);
  226. HashMap<Vector2i, TileMapCell> _draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase);
  227. void _stop_dragging();
  228. enum SelectedType {
  229. SELECTED_TYPE_CONNECT = 0,
  230. SELECTED_TYPE_PATH,
  231. SELECTED_TYPE_PATTERN,
  232. };
  233. SelectedType selected_type;
  234. int selected_terrain_set = -1;
  235. int selected_terrain = -1;
  236. TileSet::TerrainsPattern selected_terrains_pattern;
  237. void _update_selection();
  238. // Bottom panel.
  239. Tree *terrains_tree = nullptr;
  240. ItemList *terrains_tile_list = nullptr;
  241. // Cache.
  242. LocalVector<LocalVector<RBSet<TileSet::TerrainsPattern>>> per_terrain_terrains_patterns;
  243. // Update functions.
  244. void _update_terrains_cache();
  245. void _update_terrains_tree();
  246. void _update_tiles_list();
  247. void _update_theme();
  248. // Update callback
  249. virtual void tile_set_changed() override;
  250. public:
  251. virtual Vector<TabData> get_tabs() const override;
  252. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override;
  253. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override;
  254. TileMapEditorTerrainsPlugin();
  255. ~TileMapEditorTerrainsPlugin();
  256. };
  257. class TileMapEditor : public VBoxContainer {
  258. GDCLASS(TileMapEditor, VBoxContainer);
  259. private:
  260. Ref<EditorUndoRedoManager> undo_redo;
  261. bool tileset_changed_needs_update = false;
  262. ObjectID tile_map_id;
  263. int tile_map_layer = -1;
  264. // Vector to keep plugins.
  265. Vector<TileMapEditorPlugin *> tile_map_editor_plugins;
  266. // Toolbar.
  267. HBoxContainer *tile_map_toolbar = nullptr;
  268. OptionButton *layers_selection_button = nullptr;
  269. Button *toggle_highlight_selected_layer_button = nullptr;
  270. void _layers_selection_item_selected(int p_index);
  271. Button *toggle_grid_button = nullptr;
  272. void _on_grid_toggled(bool p_pressed);
  273. MenuButton *advanced_menu_button = nullptr;
  274. void _advanced_menu_button_id_pressed(int p_id);
  275. // Bottom panel.
  276. Label *missing_tileset_label = nullptr;
  277. TabBar *tabs_bar = nullptr;
  278. LocalVector<TileMapEditorPlugin::TabData> tabs_data;
  279. LocalVector<TileMapEditorPlugin *> tabs_plugins;
  280. void _update_bottom_panel();
  281. // TileMap.
  282. Ref<Texture2D> missing_tile_texture;
  283. Ref<Texture2D> warning_pattern_texture;
  284. // CallBack.
  285. void _tile_map_changed();
  286. void _tab_changed(int p_tab_changed);
  287. // Updates.
  288. void _layers_select_next_or_previous(bool p_next);
  289. void _update_layers_selection();
  290. // Inspector undo/redo callback.
  291. 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);
  292. protected:
  293. void _notification(int p_what);
  294. void _draw_shape(Control *p_control, Rect2 p_region, TileSet::TileShape p_shape, TileSet::TileOffsetAxis p_offset_axis, Color p_color);
  295. public:
  296. bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
  297. void forward_canvas_draw_over_viewport(Control *p_overlay);
  298. void edit(TileMap *p_tile_map);
  299. TileMapEditor();
  300. ~TileMapEditor();
  301. // Static functions.
  302. static Vector<Vector2i> get_line(TileMap *p_tile_map, Vector2i p_from_cell, Vector2i p_to_cell);
  303. };
  304. #endif // TILE_MAP_EDITOR_H