tile_map_editor_plugin.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/2d/tile_map.h"
  35. #include "scene/gui/check_box.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/line_edit.h"
  38. #include "scene/gui/menu_button.h"
  39. #include "scene/gui/tool_button.h"
  40. class TileMapEditor : public VBoxContainer {
  41. GDCLASS(TileMapEditor, VBoxContainer);
  42. enum Tool {
  43. TOOL_NONE,
  44. TOOL_PAINTING,
  45. TOOL_ERASING,
  46. TOOL_RECTANGLE_PAINT,
  47. TOOL_RECTANGLE_ERASE,
  48. TOOL_LINE_PAINT,
  49. TOOL_LINE_ERASE,
  50. TOOL_SELECTING,
  51. TOOL_BUCKET,
  52. TOOL_PICKING,
  53. TOOL_PASTING
  54. };
  55. enum Options {
  56. OPTION_COPY,
  57. OPTION_ERASE_SELECTION,
  58. OPTION_FIX_INVALID,
  59. OPTION_CUT
  60. };
  61. TileMap *node;
  62. bool manual_autotile;
  63. bool priority_atlastile;
  64. Vector2 manual_position;
  65. EditorNode *editor;
  66. UndoRedo *undo_redo;
  67. Control *canvas_item_editor_viewport;
  68. LineEdit *search_box;
  69. HSlider *size_slider;
  70. ItemList *palette;
  71. ItemList *manual_palette;
  72. Label *info_message;
  73. HBoxContainer *toolbar;
  74. HBoxContainer *toolbar_right;
  75. Label *tile_info;
  76. MenuButton *options;
  77. ToolButton *paint_button;
  78. ToolButton *bucket_fill_button;
  79. ToolButton *picker_button;
  80. ToolButton *select_button;
  81. ToolButton *flip_horizontal_button;
  82. ToolButton *flip_vertical_button;
  83. ToolButton *rotate_left_button;
  84. ToolButton *rotate_right_button;
  85. ToolButton *clear_transform_button;
  86. CheckBox *manual_button;
  87. CheckBox *priority_button;
  88. Tool tool;
  89. Tool last_tool;
  90. bool selection_active;
  91. bool mouse_over;
  92. bool flip_h;
  93. bool flip_v;
  94. bool transpose;
  95. Point2i autotile_coord;
  96. Point2i rectangle_begin;
  97. Rect2i rectangle;
  98. Point2i over_tile;
  99. bool refocus_over_tile;
  100. bool *bucket_cache_visited;
  101. Rect2i bucket_cache_rect;
  102. int bucket_cache_tile;
  103. PoolVector<Vector2> bucket_cache;
  104. List<Point2i> bucket_queue;
  105. struct CellOp {
  106. int idx;
  107. bool xf;
  108. bool yf;
  109. bool tr;
  110. Vector2 ac;
  111. CellOp() :
  112. idx(TileMap::INVALID_CELL),
  113. xf(false),
  114. yf(false),
  115. tr(false) {}
  116. };
  117. Map<Point2i, CellOp> paint_undo;
  118. struct TileData {
  119. Point2i pos;
  120. int cell;
  121. bool flip_h;
  122. bool flip_v;
  123. bool transpose;
  124. Point2i autotile_coord;
  125. TileData() :
  126. cell(TileMap::INVALID_CELL),
  127. flip_h(false),
  128. flip_v(false),
  129. transpose(false) {}
  130. };
  131. List<TileData> copydata;
  132. Map<Point2i, CellOp> undo_data;
  133. Vector<int> invalid_cell;
  134. void _pick_tile(const Point2 &p_pos);
  135. PoolVector<Vector2> _bucket_fill(const Point2i &p_start, bool erase = false, bool preview = false);
  136. void _fill_points(const PoolVector<Vector2> &p_points, const Dictionary &p_op);
  137. void _erase_points(const PoolVector<Vector2> &p_points);
  138. void _select(const Point2i &p_from, const Point2i &p_to);
  139. void _erase_selection();
  140. void _draw_cell(Control *p_viewport, int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Point2i &p_autotile_coord, const Transform2D &p_xform);
  141. void _draw_fill_preview(Control *p_viewport, int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Point2i &p_autotile_coord, const Transform2D &p_xform);
  142. void _clear_bucket_cache();
  143. void _update_copydata();
  144. Vector<int> get_selected_tiles() const;
  145. void set_selected_tiles(Vector<int> p_tile);
  146. void _manual_toggled(bool p_enabled);
  147. void _priority_toggled(bool p_enabled);
  148. void _text_entered(const String &p_text);
  149. void _text_changed(const String &p_text);
  150. void _sbox_input(const Ref<InputEvent> &p_ie);
  151. void _update_palette();
  152. void _update_button_tool();
  153. void _button_tool_select(int p_tool);
  154. void _menu_option(int p_option);
  155. void _palette_selected(int index);
  156. void _palette_multi_selected(int index, bool selected);
  157. void _palette_input(const Ref<InputEvent> &p_event);
  158. Dictionary _create_cell_dictionary(int tile, bool flip_x, bool flip_y, bool transpose, Vector2 autotile_coord);
  159. void _start_undo(const String &p_action);
  160. void _finish_undo();
  161. void _create_set_cell_undo_redo(const Vector2 &p_vec, const CellOp &p_cell_old, const CellOp &p_cell_new);
  162. void _set_cell(const Point2i &p_pos, Vector<int> p_values, bool p_flip_h = false, bool p_flip_v = false, bool p_transpose = false, const Point2i &p_autotile_coord = Point2());
  163. void _canvas_mouse_enter();
  164. void _canvas_mouse_exit();
  165. void _tileset_settings_changed();
  166. void _icon_size_changed(float p_value);
  167. void _clear_transform();
  168. void _flip_horizontal();
  169. void _flip_vertical();
  170. void _rotate(int steps);
  171. protected:
  172. void _notification(int p_what);
  173. void _node_removed(Node *p_node);
  174. static void _bind_methods();
  175. CellOp _get_op_from_cell(const Point2i &p_pos);
  176. public:
  177. HBoxContainer *get_toolbar() const { return toolbar; }
  178. HBoxContainer *get_toolbar_right() const { return toolbar_right; }
  179. Label *get_tile_info() const { return tile_info; }
  180. bool forward_gui_input(const Ref<InputEvent> &p_event);
  181. void forward_canvas_draw_over_viewport(Control *p_overlay);
  182. void edit(Node *p_tile_map);
  183. TileMapEditor(EditorNode *p_editor);
  184. ~TileMapEditor();
  185. };
  186. class TileMapEditorPlugin : public EditorPlugin {
  187. GDCLASS(TileMapEditorPlugin, EditorPlugin);
  188. TileMapEditor *tile_map_editor;
  189. protected:
  190. void _notification(int p_what);
  191. public:
  192. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return tile_map_editor->forward_gui_input(p_event); }
  193. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { tile_map_editor->forward_canvas_draw_over_viewport(p_overlay); }
  194. virtual String get_name() const { return "TileMap"; }
  195. bool has_main_screen() const { return false; }
  196. virtual void edit(Object *p_object);
  197. virtual bool handles(Object *p_object) const;
  198. virtual void make_visible(bool p_visible);
  199. TileMapEditorPlugin(EditorNode *p_node);
  200. ~TileMapEditorPlugin();
  201. };
  202. #endif // TILE_MAP_EDITOR_PLUGIN_H