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