tile_map_editor_plugin.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. /**
  41. @author Juan Linietsky <reduzio@gmail.com>
  42. */
  43. class TileMapEditor : public VBoxContainer {
  44. GDCLASS(TileMapEditor, VBoxContainer);
  45. enum Tool {
  46. TOOL_NONE,
  47. TOOL_PAINTING,
  48. TOOL_ERASING,
  49. TOOL_RECTANGLE_PAINT,
  50. TOOL_RECTANGLE_ERASE,
  51. TOOL_LINE_PAINT,
  52. TOOL_LINE_ERASE,
  53. TOOL_SELECTING,
  54. TOOL_BUCKET,
  55. TOOL_PICKING,
  56. TOOL_PASTING
  57. };
  58. enum Options {
  59. OPTION_BUCKET,
  60. OPTION_PICK_TILE,
  61. OPTION_SELECT,
  62. OPTION_COPY,
  63. OPTION_ERASE_SELECTION,
  64. OPTION_PAINTING,
  65. OPTION_FIX_INVALID,
  66. OPTION_CUT
  67. };
  68. TileMap *node;
  69. bool manual_autotile;
  70. Vector2 manual_position;
  71. EditorNode *editor;
  72. UndoRedo *undo_redo;
  73. Control *canvas_item_editor_viewport;
  74. LineEdit *search_box;
  75. HSlider *size_slider;
  76. ItemList *palette;
  77. ItemList *manual_palette;
  78. HBoxContainer *toolbar;
  79. Label *tile_info;
  80. MenuButton *options;
  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. Tool tool;
  88. bool selection_active;
  89. bool mouse_over;
  90. bool show_tile_info;
  91. bool flip_h;
  92. bool flip_v;
  93. bool transpose;
  94. Point2i autotile_coord;
  95. Point2i rectangle_begin;
  96. Rect2i rectangle;
  97. Point2i over_tile;
  98. bool *bucket_cache_visited;
  99. Rect2i bucket_cache_rect;
  100. int bucket_cache_tile;
  101. PoolVector<Vector2> bucket_cache;
  102. List<Point2i> bucket_queue;
  103. struct CellOp {
  104. int idx;
  105. bool xf;
  106. bool yf;
  107. bool tr;
  108. Vector2 ac;
  109. CellOp() :
  110. idx(TileMap::INVALID_CELL),
  111. xf(false),
  112. yf(false),
  113. tr(false) {}
  114. };
  115. Map<Point2i, CellOp> paint_undo;
  116. struct TileData {
  117. Point2i pos;
  118. int cell;
  119. bool flip_h;
  120. bool flip_v;
  121. bool transpose;
  122. Point2i autotile_coord;
  123. TileData() :
  124. cell(TileMap::INVALID_CELL),
  125. flip_h(false),
  126. flip_v(false),
  127. transpose(false) {}
  128. };
  129. List<TileData> copydata;
  130. Map<Point2i, CellOp> undo_data;
  131. Vector<int> invalid_cell;
  132. void _pick_tile(const Point2 &p_pos);
  133. PoolVector<Vector2> _bucket_fill(const Point2i &p_start, bool erase = false, bool preview = false);
  134. void _fill_points(const PoolVector<Vector2> p_points, const Dictionary &p_op);
  135. void _erase_points(const PoolVector<Vector2> p_points);
  136. void _select(const Point2i &p_from, const Point2i &p_to);
  137. void _erase_selection();
  138. 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);
  139. 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);
  140. void _clear_bucket_cache();
  141. void _update_copydata();
  142. Vector<int> get_selected_tiles() const;
  143. void set_selected_tiles(Vector<int> p_tile);
  144. void _manual_toggled(bool p_enabled);
  145. void _text_entered(const String &p_text);
  146. void _text_changed(const String &p_text);
  147. void _sbox_input(const Ref<InputEvent> &p_ie);
  148. void _update_palette();
  149. void _menu_option(int p_option);
  150. void _palette_selected(int index);
  151. void _palette_multi_selected(int index, bool selected);
  152. void _start_undo(const String &p_action);
  153. void _finish_undo();
  154. void _create_set_cell_undo(const Vector2 &p_vec, const CellOp &p_cell_old, const CellOp &p_cell_new);
  155. 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());
  156. void _canvas_mouse_enter();
  157. void _canvas_mouse_exit();
  158. void _tileset_settings_changed();
  159. void _icon_size_changed(float p_value);
  160. void _clear_transform();
  161. void _flip_horizontal();
  162. void _flip_vertical();
  163. void _rotate(int steps);
  164. protected:
  165. void _notification(int p_what);
  166. static void _bind_methods();
  167. CellOp _get_op_from_cell(const Point2i &p_pos);
  168. public:
  169. HBoxContainer *get_toolbar() const { return toolbar; }
  170. bool forward_gui_input(const Ref<InputEvent> &p_event);
  171. void forward_canvas_draw_over_viewport(Control *p_overlay);
  172. void edit(Node *p_tile_map);
  173. TileMapEditor(EditorNode *p_editor);
  174. ~TileMapEditor();
  175. };
  176. class TileMapEditorPlugin : public EditorPlugin {
  177. GDCLASS(TileMapEditorPlugin, EditorPlugin);
  178. TileMapEditor *tile_map_editor;
  179. protected:
  180. void _notification(int p_what);
  181. public:
  182. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return tile_map_editor->forward_gui_input(p_event); }
  183. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { tile_map_editor->forward_canvas_draw_over_viewport(p_overlay); }
  184. virtual String get_name() const { return "TileMap"; }
  185. bool has_main_screen() const { return false; }
  186. virtual void edit(Object *p_object);
  187. virtual bool handles(Object *p_object) const;
  188. virtual void make_visible(bool p_visible);
  189. TileMapEditorPlugin(EditorNode *p_node);
  190. ~TileMapEditorPlugin();
  191. };
  192. #endif // TILE_MAP_EDITOR_PLUGIN_H