tile_map_layer_editor.h 15 KB

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