tile_map_editor_plugin.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/label.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/tool_button.h"
  39. /**
  40. @author Juan Linietsky <reduzio@gmail.com>
  41. */
  42. class TileMapEditor : public VBoxContainer {
  43. GDCLASS(TileMapEditor, VBoxContainer);
  44. enum Tool {
  45. TOOL_NONE,
  46. TOOL_PAINTING,
  47. TOOL_ERASING,
  48. TOOL_RECTANGLE_PAINT,
  49. TOOL_RECTANGLE_ERASE,
  50. TOOL_LINE_PAINT,
  51. TOOL_LINE_ERASE,
  52. TOOL_SELECTING,
  53. TOOL_BUCKET,
  54. TOOL_PICKING,
  55. TOOL_DUPLICATING,
  56. };
  57. enum Options {
  58. OPTION_BUCKET,
  59. OPTION_PICK_TILE,
  60. OPTION_SELECT,
  61. OPTION_DUPLICATE,
  62. OPTION_ERASE_SELECTION,
  63. OPTION_PAINTING,
  64. };
  65. TileMap *node;
  66. EditorNode *editor;
  67. UndoRedo *undo_redo;
  68. Control *canvas_item_editor;
  69. LineEdit *search_box;
  70. HSlider *size_slider;
  71. ItemList *palette;
  72. HBoxContainer *toolbar;
  73. Label *tile_info;
  74. MenuButton *options;
  75. ToolButton *transp;
  76. ToolButton *mirror_x;
  77. ToolButton *mirror_y;
  78. ToolButton *rotate_0;
  79. ToolButton *rotate_90;
  80. ToolButton *rotate_180;
  81. ToolButton *rotate_270;
  82. Tool tool;
  83. bool selection_active;
  84. bool mouse_over;
  85. bool show_tile_info;
  86. bool flip_h;
  87. bool flip_v;
  88. bool transpose;
  89. Point2i rectangle_begin;
  90. Rect2i rectangle;
  91. Point2i over_tile;
  92. bool *bucket_cache_visited;
  93. Rect2i bucket_cache_rect;
  94. int bucket_cache_tile;
  95. PoolVector<Vector2> bucket_cache;
  96. List<Point2i> bucket_queue;
  97. struct CellOp {
  98. int idx;
  99. bool xf;
  100. bool yf;
  101. bool tr;
  102. CellOp() {
  103. idx = -1;
  104. xf = false;
  105. yf = false;
  106. tr = false;
  107. }
  108. };
  109. Map<Point2i, CellOp> paint_undo;
  110. struct TileData {
  111. Point2i pos;
  112. int cell;
  113. bool flip_h;
  114. bool flip_v;
  115. bool transpose;
  116. };
  117. List<TileData> copydata;
  118. void _pick_tile(const Point2 &p_pos);
  119. PoolVector<Vector2> _bucket_fill(const Point2i &p_start, bool erase = false, bool preview = false);
  120. void _fill_points(const PoolVector<Vector2> p_points, const Dictionary &p_op);
  121. void _erase_points(const PoolVector<Vector2> p_points);
  122. void _select(const Point2i &p_from, const Point2i &p_to);
  123. void _draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Transform2D &p_xform);
  124. void _draw_fill_preview(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Transform2D &p_xform);
  125. void _clear_bucket_cache();
  126. void _update_copydata();
  127. int get_selected_tile() const;
  128. void set_selected_tile(int p_tile);
  129. void _text_entered(const String &p_text);
  130. void _text_changed(const String &p_text);
  131. void _sbox_input(const Ref<InputEvent> &p_ie);
  132. void _update_palette();
  133. void _menu_option(int p_option);
  134. void _set_cell(const Point2i &p_pos, int p_value, bool p_flip_h = false, bool p_flip_v = false, bool p_transpose = false, bool p_with_undo = false);
  135. void _canvas_mouse_enter();
  136. void _canvas_mouse_exit();
  137. void _tileset_settings_changed();
  138. void _icon_size_changed(float p_value);
  139. protected:
  140. void _notification(int p_what);
  141. static void _bind_methods();
  142. CellOp _get_op_from_cell(const Point2i &p_pos);
  143. void _update_transform_buttons(Object *p_button = NULL);
  144. public:
  145. HBoxContainer *get_toolbar() const { return toolbar; }
  146. bool forward_gui_input(const Ref<InputEvent> &p_event);
  147. void forward_draw_over_canvas(Control *p_canvas);
  148. void edit(Node *p_tile_map);
  149. TileMapEditor(EditorNode *p_editor);
  150. ~TileMapEditor();
  151. };
  152. class TileMapEditorPlugin : public EditorPlugin {
  153. GDCLASS(TileMapEditorPlugin, EditorPlugin);
  154. TileMapEditor *tile_map_editor;
  155. public:
  156. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return tile_map_editor->forward_gui_input(p_event); }
  157. virtual void forward_draw_over_canvas(Control *p_canvas) { tile_map_editor->forward_draw_over_canvas(p_canvas); }
  158. virtual String get_name() const { return "TileMap"; }
  159. bool has_main_screen() const { return false; }
  160. virtual void edit(Object *p_object);
  161. virtual bool handles(Object *p_object) const;
  162. virtual void make_visible(bool p_visible);
  163. TileMapEditorPlugin(EditorNode *p_node);
  164. ~TileMapEditorPlugin();
  165. };
  166. #endif // TILE_MAP_EDITOR_PLUGIN_H