tile_map_layer_editor.h 15 KB

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