tile_map_editor_plugin.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. /*************************************************************************/
  2. /* tile_map_editor_plugin.cpp */
  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. #include "tile_map_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/gui/split_container.h"
  38. void TileMapEditor::_notification(int p_what) {
  39. switch (p_what) {
  40. case NOTIFICATION_PROCESS: {
  41. if (bucket_queue.size()) {
  42. CanvasItemEditor::get_singleton()->update_viewport();
  43. }
  44. } break;
  45. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  46. bool new_show_tile_info = EditorSettings::get_singleton()->get("editors/tile_map/show_tile_info_on_hover");
  47. if (new_show_tile_info != show_tile_info) {
  48. show_tile_info = new_show_tile_info;
  49. tile_info->set_visible(show_tile_info);
  50. }
  51. if (is_visible_in_tree()) {
  52. _update_palette();
  53. }
  54. } // fallthrough
  55. case NOTIFICATION_ENTER_TREE: {
  56. rotate_left_button->set_icon(get_icon("Rotate270", "EditorIcons"));
  57. rotate_right_button->set_icon(get_icon("Rotate90", "EditorIcons"));
  58. flip_horizontal_button->set_icon(get_icon("MirrorX", "EditorIcons"));
  59. flip_vertical_button->set_icon(get_icon("MirrorY", "EditorIcons"));
  60. clear_transform_button->set_icon(get_icon("Clear", "EditorIcons"));
  61. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  62. search_box->set_clear_button_enabled(true);
  63. PopupMenu *p = options->get_popup();
  64. p->set_item_icon(p->get_item_index(OPTION_PAINTING), get_icon("Edit", "EditorIcons"));
  65. p->set_item_icon(p->get_item_index(OPTION_PICK_TILE), get_icon("ColorPick", "EditorIcons"));
  66. p->set_item_icon(p->get_item_index(OPTION_SELECT), get_icon("ActionCopy", "EditorIcons"));
  67. p->set_item_icon(p->get_item_index(OPTION_CUT), get_icon("ActionCut", "EditorIcons"));
  68. p->set_item_icon(p->get_item_index(OPTION_COPY), get_icon("Duplicate", "EditorIcons"));
  69. p->set_item_icon(p->get_item_index(OPTION_ERASE_SELECTION), get_icon("Remove", "EditorIcons"));
  70. } break;
  71. }
  72. }
  73. void TileMapEditor::_menu_option(int p_option) {
  74. switch (p_option) {
  75. case OPTION_PAINTING: {
  76. // NOTE: We do not set tool = TOOL_PAINTING as this begins painting
  77. // immediately without pressing the left mouse button first
  78. tool = TOOL_NONE;
  79. CanvasItemEditor::get_singleton()->update_viewport();
  80. } break;
  81. case OPTION_BUCKET: {
  82. tool = TOOL_BUCKET;
  83. CanvasItemEditor::get_singleton()->update_viewport();
  84. } break;
  85. case OPTION_PICK_TILE: {
  86. tool = TOOL_PICKING;
  87. CanvasItemEditor::get_singleton()->update_viewport();
  88. } break;
  89. case OPTION_SELECT: {
  90. tool = TOOL_SELECTING;
  91. selection_active = false;
  92. CanvasItemEditor::get_singleton()->update_viewport();
  93. } break;
  94. case OPTION_COPY: {
  95. _update_copydata();
  96. if (selection_active) {
  97. tool = TOOL_PASTING;
  98. CanvasItemEditor::get_singleton()->update_viewport();
  99. }
  100. } break;
  101. case OPTION_ERASE_SELECTION: {
  102. if (!selection_active)
  103. return;
  104. _start_undo(TTR("Erase Selection"));
  105. _erase_selection();
  106. _finish_undo();
  107. selection_active = false;
  108. copydata.clear();
  109. CanvasItemEditor::get_singleton()->update_viewport();
  110. } break;
  111. case OPTION_FIX_INVALID: {
  112. undo_redo->create_action(TTR("Fix Invalid Tiles"));
  113. undo_redo->add_undo_method(node, "set", "tile_data", node->get("tile_data"));
  114. node->fix_invalid_tiles();
  115. undo_redo->add_do_method(node, "set", "tile_data", node->get("tile_data"));
  116. undo_redo->commit_action();
  117. } break;
  118. case OPTION_CUT: {
  119. if (selection_active) {
  120. _update_copydata();
  121. _start_undo(TTR("Cut Selection"));
  122. _erase_selection();
  123. _finish_undo();
  124. selection_active = false;
  125. tool = TOOL_PASTING;
  126. CanvasItemEditor::get_singleton()->update_viewport();
  127. }
  128. } break;
  129. }
  130. }
  131. void TileMapEditor::_palette_selected(int index) {
  132. _update_palette();
  133. }
  134. void TileMapEditor::_palette_multi_selected(int index, bool selected) {
  135. _update_palette();
  136. }
  137. void TileMapEditor::_canvas_mouse_enter() {
  138. mouse_over = true;
  139. CanvasItemEditor::get_singleton()->update_viewport();
  140. }
  141. void TileMapEditor::_canvas_mouse_exit() {
  142. mouse_over = false;
  143. CanvasItemEditor::get_singleton()->update_viewport();
  144. }
  145. Vector<int> TileMapEditor::get_selected_tiles() const {
  146. Vector<int> items = palette->get_selected_items();
  147. if (items.size() == 0) {
  148. items.push_back(TileMap::INVALID_CELL);
  149. return items;
  150. }
  151. for (int i = items.size() - 1; i >= 0; i--) {
  152. items.write[i] = palette->get_item_metadata(items[i]);
  153. }
  154. return items;
  155. }
  156. void TileMapEditor::set_selected_tiles(Vector<int> p_tiles) {
  157. palette->unselect_all();
  158. for (int i = p_tiles.size() - 1; i >= 0; i--) {
  159. int idx = palette->find_metadata(p_tiles[i]);
  160. if (idx >= 0) {
  161. palette->select(idx, false);
  162. }
  163. }
  164. palette->ensure_current_is_visible();
  165. }
  166. void TileMapEditor::_create_set_cell_undo(const Vector2 &p_vec, const CellOp &p_cell_old, const CellOp &p_cell_new) {
  167. Dictionary cell_old;
  168. Dictionary cell_new;
  169. cell_old["id"] = p_cell_old.idx;
  170. cell_old["flip_h"] = p_cell_old.xf;
  171. cell_old["flip_y"] = p_cell_old.yf;
  172. cell_old["transpose"] = p_cell_old.tr;
  173. cell_old["auto_coord"] = p_cell_old.ac;
  174. cell_new["id"] = p_cell_new.idx;
  175. cell_new["flip_h"] = p_cell_new.xf;
  176. cell_new["flip_y"] = p_cell_new.yf;
  177. cell_new["transpose"] = p_cell_new.tr;
  178. cell_new["auto_coord"] = p_cell_new.ac;
  179. undo_redo->add_undo_method(node, "_set_celld", p_vec, cell_old);
  180. undo_redo->add_do_method(node, "_set_celld", p_vec, cell_new);
  181. }
  182. void TileMapEditor::_start_undo(const String &p_action) {
  183. undo_data.clear();
  184. undo_redo->create_action(p_action);
  185. }
  186. void TileMapEditor::_finish_undo() {
  187. if (undo_data.size()) {
  188. for (Map<Point2i, CellOp>::Element *E = undo_data.front(); E; E = E->next()) {
  189. _create_set_cell_undo(E->key(), E->get(), _get_op_from_cell(E->key()));
  190. }
  191. undo_data.clear();
  192. }
  193. undo_redo->commit_action();
  194. }
  195. void TileMapEditor::_set_cell(const Point2i &p_pos, Vector<int> p_values, bool p_flip_h, bool p_flip_v, bool p_transpose, const Point2i p_autotile_coord) {
  196. ERR_FAIL_COND(!node);
  197. if (p_values.size() == 0)
  198. return;
  199. int p_value = p_values[Math::rand() % p_values.size()];
  200. int prev_val = node->get_cell(p_pos.x, p_pos.y);
  201. bool prev_flip_h = node->is_cell_x_flipped(p_pos.x, p_pos.y);
  202. bool prev_flip_v = node->is_cell_y_flipped(p_pos.x, p_pos.y);
  203. bool prev_transpose = node->is_cell_transposed(p_pos.x, p_pos.y);
  204. Vector2 prev_position = node->get_cell_autotile_coord(p_pos.x, p_pos.y);
  205. Vector2 position;
  206. int current = manual_palette->get_current();
  207. if (current != -1) {
  208. position = manual_palette->get_item_metadata(current);
  209. } else {
  210. // if there is no manual tile selected, that either means that
  211. // autotiling is enabled, or the given tile is not autotiling. Either
  212. // way, the coordinate of the tile does not matter, so assigning it to
  213. // the coordinate of the existing tile works fine.
  214. position = prev_position;
  215. }
  216. if (p_value == prev_val && p_flip_h == prev_flip_h && p_flip_v == prev_flip_v && p_transpose == prev_transpose && prev_position == position)
  217. return; //check that it's actually different
  218. for (int y = p_pos.y - 1; y <= p_pos.y + 1; y++) {
  219. for (int x = p_pos.x - 1; x <= p_pos.x + 1; x++) {
  220. Point2i p = Point2i(x, y);
  221. if (!undo_data.has(p)) {
  222. undo_data[p] = _get_op_from_cell(p);
  223. }
  224. }
  225. }
  226. node->set_cell(p_pos.x, p_pos.y, p_value, p_flip_h, p_flip_v, p_transpose, p_autotile_coord);
  227. if (manual_autotile || (p_value != -1 && node->get_tileset()->tile_get_tile_mode(p_value) == TileSet::ATLAS_TILE)) {
  228. if (current != -1) {
  229. node->set_cell_autotile_coord(p_pos.x, p_pos.y, position);
  230. }
  231. } else {
  232. // manually placing tiles should not update bitmasks
  233. if (tool != TOOL_PASTING) {
  234. node->update_bitmask_area(Point2(p_pos));
  235. }
  236. }
  237. }
  238. void TileMapEditor::_manual_toggled(bool p_enabled) {
  239. manual_autotile = p_enabled;
  240. _update_palette();
  241. }
  242. void TileMapEditor::_text_entered(const String &p_text) {
  243. canvas_item_editor_viewport->grab_focus();
  244. }
  245. void TileMapEditor::_text_changed(const String &p_text) {
  246. _update_palette();
  247. }
  248. void TileMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  249. Ref<InputEventKey> k = p_ie;
  250. if (k.is_valid() && (k->get_scancode() == KEY_UP ||
  251. k->get_scancode() == KEY_DOWN ||
  252. k->get_scancode() == KEY_PAGEUP ||
  253. k->get_scancode() == KEY_PAGEDOWN)) {
  254. palette->call("_gui_input", k);
  255. search_box->accept_event();
  256. }
  257. }
  258. // Implementation detail of TileMapEditor::_update_palette();
  259. // in modern C++ this could have been inside its body
  260. namespace {
  261. struct _PaletteEntry {
  262. int id;
  263. String name;
  264. bool operator<(const _PaletteEntry &p_rhs) const {
  265. return name < p_rhs.name;
  266. }
  267. };
  268. } // namespace
  269. void TileMapEditor::_update_palette() {
  270. if (!node)
  271. return;
  272. // Update the clear button
  273. clear_transform_button->set_disabled(!flip_h && !flip_v && !transpose);
  274. // Update the palette
  275. Vector<int> selected = get_selected_tiles();
  276. palette->clear();
  277. manual_palette->clear();
  278. manual_palette->hide();
  279. Ref<TileSet> tileset = node->get_tileset();
  280. if (tileset.is_null())
  281. return;
  282. List<int> tiles;
  283. tileset->get_tile_list(&tiles);
  284. if (tiles.empty())
  285. return;
  286. float min_size = EDITOR_DEF("editors/tile_map/preview_size", 64);
  287. min_size *= EDSCALE;
  288. int hseparation = EDITOR_DEF("editors/tile_map/palette_item_hseparation", 8);
  289. bool show_tile_names = bool(EDITOR_DEF("editors/tile_map/show_tile_names", true));
  290. bool show_tile_ids = bool(EDITOR_DEF("editors/tile_map/show_tile_ids", false));
  291. bool sort_by_name = bool(EDITOR_DEF("editors/tile_map/sort_tiles_by_name", true));
  292. palette->add_constant_override("hseparation", hseparation * EDSCALE);
  293. palette->add_constant_override("vseparation", 8 * EDSCALE);
  294. palette->set_fixed_icon_size(Size2(min_size, min_size));
  295. palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1));
  296. palette->set_same_column_width(true);
  297. manual_palette->set_fixed_icon_size(Size2(min_size, min_size));
  298. manual_palette->set_same_column_width(true);
  299. String filter = search_box->get_text().strip_edges();
  300. Vector<_PaletteEntry> entries;
  301. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  302. String name = tileset->tile_get_name(E->get());
  303. if (name != "") {
  304. if (show_tile_ids) {
  305. if (sort_by_name) {
  306. name = name + " - " + itos(E->get());
  307. } else {
  308. name = itos(E->get()) + " - " + name;
  309. }
  310. }
  311. } else {
  312. name = "#" + itos(E->get());
  313. }
  314. if (filter != "" && !filter.is_subsequence_ofi(name))
  315. continue;
  316. const _PaletteEntry entry = { E->get(), name };
  317. entries.push_back(entry);
  318. }
  319. if (sort_by_name) {
  320. entries.sort();
  321. }
  322. for (int i = 0; i < entries.size(); i++) {
  323. if (show_tile_names) {
  324. palette->add_item(entries[i].name);
  325. } else {
  326. palette->add_item(String());
  327. }
  328. Ref<Texture> tex = tileset->tile_get_texture(entries[i].id);
  329. if (tex.is_valid()) {
  330. Rect2 region = tileset->tile_get_region(entries[i].id);
  331. if (tileset->tile_get_tile_mode(entries[i].id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(entries[i].id) == TileSet::ATLAS_TILE) {
  332. int spacing = tileset->autotile_get_spacing(entries[i].id);
  333. region.size = tileset->autotile_get_size(entries[i].id);
  334. region.position += (region.size + Vector2(spacing, spacing)) * tileset->autotile_get_icon_coordinate(entries[i].id);
  335. }
  336. // Transpose and flip
  337. palette->set_item_icon_transposed(palette->get_item_count() - 1, transpose);
  338. if (flip_h) {
  339. region.size.x = -region.size.x;
  340. }
  341. if (flip_v) {
  342. region.size.y = -region.size.y;
  343. }
  344. // Set region
  345. if (region.size != Size2())
  346. palette->set_item_icon_region(palette->get_item_count() - 1, region);
  347. // Set icon
  348. palette->set_item_icon(palette->get_item_count() - 1, tex);
  349. // Modulation
  350. Color color = tileset->tile_get_modulate(entries[i].id);
  351. palette->set_item_icon_modulate(palette->get_item_count() - 1, color);
  352. }
  353. palette->set_item_metadata(palette->get_item_count() - 1, entries[i].id);
  354. }
  355. int sel_tile = selected.get(0);
  356. if (selected.get(0) != TileMap::INVALID_CELL) {
  357. set_selected_tiles(selected);
  358. sel_tile = selected.get(Math::rand() % selected.size());
  359. } else {
  360. palette->select(0);
  361. }
  362. if (sel_tile != TileMap::INVALID_CELL) {
  363. if ((manual_autotile && tileset->tile_get_tile_mode(sel_tile) == TileSet::AUTO_TILE) || tileset->tile_get_tile_mode(sel_tile) == TileSet::ATLAS_TILE) {
  364. const Map<Vector2, uint16_t> &tiles = tileset->autotile_get_bitmask_map(sel_tile);
  365. Vector<Vector2> entries;
  366. for (const Map<Vector2, uint16_t>::Element *E = tiles.front(); E; E = E->next()) {
  367. entries.push_back(E->key());
  368. }
  369. entries.sort();
  370. Ref<Texture> tex = tileset->tile_get_texture(sel_tile);
  371. for (int i = 0; i < entries.size(); i++) {
  372. manual_palette->add_item(String());
  373. if (tex.is_valid()) {
  374. Rect2 region = tileset->tile_get_region(sel_tile);
  375. int spacing = tileset->autotile_get_spacing(sel_tile);
  376. region.size = tileset->autotile_get_size(sel_tile); // !!
  377. region.position += (region.size + Vector2(spacing, spacing)) * entries[i];
  378. if (!region.has_no_area())
  379. manual_palette->set_item_icon_region(manual_palette->get_item_count() - 1, region);
  380. manual_palette->set_item_icon(manual_palette->get_item_count() - 1, tex);
  381. }
  382. manual_palette->set_item_metadata(manual_palette->get_item_count() - 1, entries[i]);
  383. }
  384. }
  385. }
  386. if (manual_palette->get_item_count() > 0) {
  387. // Only show the manual palette if at least tile exists in it
  388. int selected = manual_palette->get_current();
  389. if (selected == -1) selected = 0;
  390. manual_palette->set_current(selected);
  391. manual_palette->show();
  392. }
  393. if (sel_tile != TileMap::INVALID_CELL && tileset->tile_get_tile_mode(sel_tile) == TileSet::AUTO_TILE) {
  394. manual_button->show();
  395. } else {
  396. manual_button->hide();
  397. }
  398. }
  399. void TileMapEditor::_pick_tile(const Point2 &p_pos) {
  400. int id = node->get_cell(p_pos.x, p_pos.y);
  401. if (id == TileMap::INVALID_CELL)
  402. return;
  403. if (search_box->get_text().strip_edges() != "") {
  404. search_box->set_text("");
  405. _update_palette();
  406. }
  407. Vector<int> selected;
  408. selected.push_back(id);
  409. set_selected_tiles(selected);
  410. flip_h = node->is_cell_x_flipped(p_pos.x, p_pos.y);
  411. flip_v = node->is_cell_y_flipped(p_pos.x, p_pos.y);
  412. transpose = node->is_cell_transposed(p_pos.x, p_pos.y);
  413. autotile_coord = node->get_cell_autotile_coord(p_pos.x, p_pos.y);
  414. _update_palette();
  415. CanvasItemEditor::get_singleton()->update_viewport();
  416. }
  417. PoolVector<Vector2> TileMapEditor::_bucket_fill(const Point2i &p_start, bool erase, bool preview) {
  418. int prev_id = node->get_cell(p_start.x, p_start.y);
  419. Vector<int> ids;
  420. ids.push_back(TileMap::INVALID_CELL);
  421. if (!erase) {
  422. ids = get_selected_tiles();
  423. if (ids.size() == 0 || ids[0] == TileMap::INVALID_CELL)
  424. return PoolVector<Vector2>();
  425. } else if (prev_id == TileMap::INVALID_CELL) {
  426. return PoolVector<Vector2>();
  427. }
  428. if (ids.size() == 1 && ids[0] == prev_id) {
  429. // Same ID, nothing to change
  430. return PoolVector<Vector2>();
  431. }
  432. Rect2i r = node->get_used_rect();
  433. int area = r.get_area();
  434. if (preview) {
  435. // Test if we can re-use the result from preview bucket fill
  436. bool invalidate_cache = false;
  437. // Area changed
  438. if (r != bucket_cache_rect)
  439. _clear_bucket_cache();
  440. // Cache grid is not initialized
  441. if (bucket_cache_visited == 0) {
  442. bucket_cache_visited = new bool[area];
  443. invalidate_cache = true;
  444. }
  445. // Tile ID changed or position wasn't visited by the previous fill
  446. int loc = (p_start.x - r.position.x) + (p_start.y - r.position.y) * r.get_size().x;
  447. if (prev_id != bucket_cache_tile || !bucket_cache_visited[loc]) {
  448. invalidate_cache = true;
  449. }
  450. if (invalidate_cache) {
  451. for (int i = 0; i < area; ++i)
  452. bucket_cache_visited[i] = false;
  453. bucket_cache = PoolVector<Vector2>();
  454. bucket_cache_tile = prev_id;
  455. bucket_cache_rect = r;
  456. bucket_queue.clear();
  457. }
  458. }
  459. PoolVector<Vector2> points;
  460. Vector<Vector2> non_preview_cache;
  461. int count = 0;
  462. int limit = 0;
  463. if (preview) {
  464. limit = 1024;
  465. } else {
  466. bucket_queue.clear();
  467. }
  468. bucket_queue.push_back(p_start);
  469. while (bucket_queue.size()) {
  470. Point2i n = bucket_queue.front()->get();
  471. bucket_queue.pop_front();
  472. if (!r.has_point(n))
  473. continue;
  474. if (node->get_cell(n.x, n.y) == prev_id) {
  475. if (preview) {
  476. int loc = (n.x - r.position.x) + (n.y - r.position.y) * r.get_size().x;
  477. if (bucket_cache_visited[loc])
  478. continue;
  479. bucket_cache_visited[loc] = true;
  480. bucket_cache.push_back(n);
  481. } else {
  482. if (non_preview_cache.find(n) >= 0)
  483. continue;
  484. points.push_back(n);
  485. non_preview_cache.push_back(n);
  486. }
  487. bucket_queue.push_back(Point2i(n.x, n.y + 1));
  488. bucket_queue.push_back(Point2i(n.x, n.y - 1));
  489. bucket_queue.push_back(Point2i(n.x + 1, n.y));
  490. bucket_queue.push_back(Point2i(n.x - 1, n.y));
  491. count++;
  492. }
  493. if (limit > 0 && count >= limit) {
  494. break;
  495. }
  496. }
  497. return preview ? bucket_cache : points;
  498. }
  499. void TileMapEditor::_fill_points(const PoolVector<Vector2> p_points, const Dictionary &p_op) {
  500. int len = p_points.size();
  501. PoolVector<Vector2>::Read pr = p_points.read();
  502. Vector<int> ids = p_op["id"];
  503. bool xf = p_op["flip_h"];
  504. bool yf = p_op["flip_v"];
  505. bool tr = p_op["transpose"];
  506. for (int i = 0; i < len; i++) {
  507. _set_cell(pr[i], ids, xf, yf, tr);
  508. node->make_bitmask_area_dirty(pr[i]);
  509. }
  510. node->update_dirty_bitmask();
  511. }
  512. void TileMapEditor::_erase_points(const PoolVector<Vector2> p_points) {
  513. int len = p_points.size();
  514. PoolVector<Vector2>::Read pr = p_points.read();
  515. for (int i = 0; i < len; i++) {
  516. _set_cell(pr[i], invalid_cell);
  517. }
  518. }
  519. void TileMapEditor::_select(const Point2i &p_from, const Point2i &p_to) {
  520. Point2i begin = p_from;
  521. Point2i end = p_to;
  522. if (begin.x > end.x) {
  523. SWAP(begin.x, end.x);
  524. }
  525. if (begin.y > end.y) {
  526. SWAP(begin.y, end.y);
  527. }
  528. rectangle.position = begin;
  529. rectangle.size = end - begin;
  530. CanvasItemEditor::get_singleton()->update_viewport();
  531. }
  532. void TileMapEditor::_erase_selection() {
  533. if (!selection_active)
  534. return;
  535. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  536. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  537. _set_cell(Point2i(j, i), invalid_cell, false, false, false);
  538. }
  539. }
  540. }
  541. void TileMapEditor::_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) {
  542. Ref<Texture> t = node->get_tileset()->tile_get_texture(p_cell);
  543. if (t.is_null())
  544. return;
  545. Vector2 tile_ofs = node->get_tileset()->tile_get_texture_offset(p_cell);
  546. Rect2 r = node->get_tileset()->tile_get_region(p_cell);
  547. if (node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::AUTO_TILE || node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::ATLAS_TILE) {
  548. Vector2 offset;
  549. int selected = manual_palette->get_current();
  550. if ((manual_autotile || node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::ATLAS_TILE) && selected != -1) {
  551. offset = manual_palette->get_item_metadata(selected);
  552. } else {
  553. if (tool != TOOL_PASTING) {
  554. offset = node->get_tileset()->autotile_get_icon_coordinate(p_cell);
  555. } else {
  556. offset = p_autotile_coord;
  557. }
  558. }
  559. int spacing = node->get_tileset()->autotile_get_spacing(p_cell);
  560. r.size = node->get_tileset()->autotile_get_size(p_cell);
  561. r.position += (r.size + Vector2(spacing, spacing)) * offset;
  562. }
  563. Size2 sc = p_xform.get_scale();
  564. Rect2 rect = Rect2();
  565. rect.position = node->map_to_world(p_point) + node->get_cell_draw_offset();
  566. if (r.has_no_area()) {
  567. rect.size = t->get_size();
  568. } else {
  569. rect.size = r.size;
  570. }
  571. if (rect.size.y > rect.size.x) {
  572. if ((p_flip_h && (p_flip_v || p_transpose)) || (p_flip_v && !p_transpose))
  573. tile_ofs.y += rect.size.y - rect.size.x;
  574. } else if (rect.size.y < rect.size.x) {
  575. if ((p_flip_v && (p_flip_h || p_transpose)) || (p_flip_h && !p_transpose))
  576. tile_ofs.x += rect.size.x - rect.size.y;
  577. }
  578. if (p_transpose) {
  579. SWAP(tile_ofs.x, tile_ofs.y);
  580. }
  581. if (p_flip_h) {
  582. sc.x *= -1.0;
  583. tile_ofs.x *= -1.0;
  584. }
  585. if (p_flip_v) {
  586. sc.y *= -1.0;
  587. tile_ofs.y *= -1.0;
  588. }
  589. if (node->get_tile_origin() == TileMap::TILE_ORIGIN_TOP_LEFT) {
  590. rect.position += tile_ofs;
  591. } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_BOTTOM_LEFT) {
  592. Size2 cell_size = node->get_cell_size();
  593. rect.position += tile_ofs;
  594. if (p_transpose) {
  595. if (p_flip_h)
  596. rect.position.x -= cell_size.x;
  597. else
  598. rect.position.x += cell_size.x;
  599. } else {
  600. if (p_flip_v)
  601. rect.position.y -= cell_size.y;
  602. else
  603. rect.position.y += cell_size.y;
  604. }
  605. } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_CENTER) {
  606. Size2 cell_size = node->get_cell_size();
  607. rect.position += tile_ofs;
  608. if (p_flip_h)
  609. rect.position.x -= cell_size.x / 2;
  610. else
  611. rect.position.x += cell_size.x / 2;
  612. if (p_flip_v)
  613. rect.position.y -= cell_size.y / 2;
  614. else
  615. rect.position.y += cell_size.y / 2;
  616. }
  617. rect.position = p_xform.xform(rect.position);
  618. rect.size *= sc;
  619. Color modulate = node->get_tileset()->tile_get_modulate(p_cell);
  620. modulate.a = 0.5;
  621. if (r.has_no_area())
  622. p_viewport->draw_texture_rect(t, rect, false, modulate, p_transpose);
  623. else
  624. p_viewport->draw_texture_rect_region(t, rect, r, modulate, p_transpose);
  625. }
  626. void TileMapEditor::_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) {
  627. PoolVector<Vector2> points = _bucket_fill(p_point, false, true);
  628. PoolVector<Vector2>::Read pr = points.read();
  629. int len = points.size();
  630. for (int i = 0; i < len; ++i) {
  631. _draw_cell(p_viewport, p_cell, pr[i], p_flip_h, p_flip_v, p_transpose, p_autotile_coord, p_xform);
  632. }
  633. }
  634. void TileMapEditor::_clear_bucket_cache() {
  635. if (bucket_cache_visited) {
  636. delete[] bucket_cache_visited;
  637. bucket_cache_visited = 0;
  638. }
  639. }
  640. void TileMapEditor::_update_copydata() {
  641. copydata.clear();
  642. if (!selection_active)
  643. return;
  644. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  645. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  646. TileData tcd;
  647. tcd.cell = node->get_cell(j, i);
  648. if (tcd.cell != TileMap::INVALID_CELL) {
  649. tcd.pos = Point2i(j, i);
  650. tcd.flip_h = node->is_cell_x_flipped(j, i);
  651. tcd.flip_v = node->is_cell_y_flipped(j, i);
  652. tcd.transpose = node->is_cell_transposed(j, i);
  653. tcd.autotile_coord = node->get_cell_autotile_coord(j, i);
  654. }
  655. copydata.push_back(tcd);
  656. }
  657. }
  658. }
  659. static inline Vector<Point2i> line(int x0, int x1, int y0, int y1) {
  660. Vector<Point2i> points;
  661. float dx = ABS(x1 - x0);
  662. float dy = ABS(y1 - y0);
  663. int x = x0;
  664. int y = y0;
  665. int sx = x0 > x1 ? -1 : 1;
  666. int sy = y0 > y1 ? -1 : 1;
  667. if (dx > dy) {
  668. float err = dx / 2;
  669. for (; x != x1; x += sx) {
  670. points.push_back(Vector2(x, y));
  671. err -= dy;
  672. if (err < 0) {
  673. y += sy;
  674. err += dx;
  675. }
  676. }
  677. } else {
  678. float err = dy / 2;
  679. for (; y != y1; y += sy) {
  680. points.push_back(Vector2(x, y));
  681. err -= dx;
  682. if (err < 0) {
  683. x += sx;
  684. err += dy;
  685. }
  686. }
  687. }
  688. points.push_back(Vector2(x, y));
  689. return points;
  690. }
  691. bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
  692. if (!node || !node->get_tileset().is_valid() || !node->is_visible_in_tree())
  693. return false;
  694. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform();
  695. Transform2D xform_inv = xform.affine_inverse();
  696. Ref<InputEventMouseButton> mb = p_event;
  697. if (mb.is_valid()) {
  698. if (mb->get_button_index() == BUTTON_LEFT) {
  699. if (mb->is_pressed()) {
  700. if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
  701. return false; //drag
  702. if (tool == TOOL_NONE) {
  703. if (mb->get_shift()) {
  704. #ifdef APPLE_STYLE_KEYS
  705. if (mb->get_command())
  706. #else
  707. if (mb->get_control())
  708. #endif
  709. tool = TOOL_RECTANGLE_PAINT;
  710. else
  711. tool = TOOL_LINE_PAINT;
  712. selection_active = false;
  713. rectangle_begin = over_tile;
  714. return true;
  715. }
  716. #ifdef APPLE_STYLE_KEYS
  717. if (mb->get_command()) {
  718. #else
  719. if (mb->get_control()) {
  720. #endif
  721. tool = TOOL_PICKING;
  722. _pick_tile(over_tile);
  723. return true;
  724. }
  725. tool = TOOL_PAINTING;
  726. }
  727. if (tool == TOOL_PAINTING) {
  728. Vector<int> ids = get_selected_tiles();
  729. if (ids.size() > 0 && ids[0] != TileMap::INVALID_CELL) {
  730. tool = TOOL_PAINTING;
  731. _start_undo(TTR("Paint TileMap"));
  732. }
  733. } else if (tool == TOOL_PICKING) {
  734. _pick_tile(over_tile);
  735. } else if (tool == TOOL_SELECTING) {
  736. selection_active = true;
  737. rectangle_begin = over_tile;
  738. }
  739. return true;
  740. } else {
  741. // Mousebutton was released
  742. if (tool != TOOL_NONE) {
  743. if (tool == TOOL_PAINTING) {
  744. Vector<int> ids = get_selected_tiles();
  745. if (ids.size() > 0 && ids[0] != TileMap::INVALID_CELL) {
  746. _set_cell(over_tile, ids, flip_h, flip_v, transpose);
  747. _finish_undo();
  748. paint_undo.clear();
  749. }
  750. } else if (tool == TOOL_LINE_PAINT) {
  751. Vector<int> ids = get_selected_tiles();
  752. if (ids.size() > 0 && ids[0] != TileMap::INVALID_CELL) {
  753. _start_undo(TTR("Line Draw"));
  754. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  755. _set_cell(E->key(), ids, flip_h, flip_v, transpose);
  756. }
  757. _finish_undo();
  758. paint_undo.clear();
  759. CanvasItemEditor::get_singleton()->update_viewport();
  760. }
  761. } else if (tool == TOOL_RECTANGLE_PAINT) {
  762. Vector<int> ids = get_selected_tiles();
  763. if (ids.size() > 0 && ids[0] != TileMap::INVALID_CELL) {
  764. _start_undo(TTR("Rectangle Paint"));
  765. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  766. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  767. _set_cell(Point2i(j, i), ids, flip_h, flip_v, transpose);
  768. }
  769. }
  770. _finish_undo();
  771. CanvasItemEditor::get_singleton()->update_viewport();
  772. }
  773. } else if (tool == TOOL_PASTING) {
  774. Point2 ofs = over_tile - rectangle.position;
  775. Vector<int> ids;
  776. _start_undo(TTR("Paste"));
  777. ids.push_back(0);
  778. for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) {
  779. ids.write[0] = E->get().cell;
  780. _set_cell(E->get().pos + ofs, ids, E->get().flip_h, E->get().flip_v, E->get().transpose, E->get().autotile_coord);
  781. }
  782. _finish_undo();
  783. CanvasItemEditor::get_singleton()->update_viewport();
  784. return true; // We want to keep the Pasting tool
  785. } else if (tool == TOOL_SELECTING) {
  786. CanvasItemEditor::get_singleton()->update_viewport();
  787. } else if (tool == TOOL_BUCKET) {
  788. PoolVector<Vector2> points = _bucket_fill(over_tile);
  789. if (points.size() == 0)
  790. return false;
  791. _start_undo(TTR("Bucket Fill"));
  792. Dictionary op;
  793. op["id"] = get_selected_tiles();
  794. op["flip_h"] = flip_h;
  795. op["flip_v"] = flip_v;
  796. op["transpose"] = transpose;
  797. _fill_points(points, op);
  798. _finish_undo();
  799. // We want to keep the bucket-tool active
  800. return true;
  801. }
  802. tool = TOOL_NONE;
  803. return true;
  804. }
  805. }
  806. } else if (mb->get_button_index() == BUTTON_RIGHT) {
  807. if (mb->is_pressed()) {
  808. if (tool == TOOL_SELECTING || selection_active) {
  809. tool = TOOL_NONE;
  810. selection_active = false;
  811. CanvasItemEditor::get_singleton()->update_viewport();
  812. return true;
  813. }
  814. if (tool == TOOL_PASTING) {
  815. tool = TOOL_NONE;
  816. copydata.clear();
  817. CanvasItemEditor::get_singleton()->update_viewport();
  818. return true;
  819. }
  820. if (tool == TOOL_NONE) {
  821. paint_undo.clear();
  822. Point2 local = node->world_to_map(xform_inv.xform(mb->get_position()));
  823. _start_undo(TTR("Erase TileMap"));
  824. if (mb->get_shift()) {
  825. #ifdef APPLE_STYLE_KEYS
  826. if (mb->get_command())
  827. #else
  828. if (mb->get_control())
  829. #endif
  830. tool = TOOL_RECTANGLE_ERASE;
  831. else
  832. tool = TOOL_LINE_ERASE;
  833. selection_active = false;
  834. rectangle_begin = local;
  835. } else {
  836. tool = TOOL_ERASING;
  837. _set_cell(local, invalid_cell);
  838. }
  839. return true;
  840. }
  841. } else {
  842. if (tool == TOOL_ERASING || tool == TOOL_RECTANGLE_ERASE || tool == TOOL_LINE_ERASE) {
  843. _finish_undo();
  844. if (tool == TOOL_RECTANGLE_ERASE || tool == TOOL_LINE_ERASE) {
  845. CanvasItemEditor::get_singleton()->update_viewport();
  846. }
  847. tool = TOOL_NONE;
  848. return true;
  849. } else if (tool == TOOL_BUCKET) {
  850. Vector<int> ids;
  851. ids.push_back(node->get_cell(over_tile.x, over_tile.y));
  852. Dictionary pop;
  853. pop["id"] = ids;
  854. pop["flip_h"] = node->is_cell_x_flipped(over_tile.x, over_tile.y);
  855. pop["flip_v"] = node->is_cell_y_flipped(over_tile.x, over_tile.y);
  856. pop["transpose"] = node->is_cell_transposed(over_tile.x, over_tile.y);
  857. PoolVector<Vector2> points = _bucket_fill(over_tile, true);
  858. if (points.size() == 0)
  859. return false;
  860. undo_redo->create_action("Bucket Fill");
  861. undo_redo->add_do_method(this, "_erase_points", points);
  862. undo_redo->add_undo_method(this, "_fill_points", points, pop);
  863. undo_redo->commit_action();
  864. }
  865. }
  866. }
  867. }
  868. Ref<InputEventMouseMotion> mm = p_event;
  869. if (mm.is_valid()) {
  870. Point2i new_over_tile = node->world_to_map(xform_inv.xform(mm->get_position()));
  871. Point2i old_over_tile = over_tile;
  872. if (new_over_tile != over_tile) {
  873. over_tile = new_over_tile;
  874. CanvasItemEditor::get_singleton()->update_viewport();
  875. }
  876. if (show_tile_info) {
  877. int tile_under = node->get_cell(over_tile.x, over_tile.y);
  878. String tile_name = "none";
  879. if (node->get_tileset()->has_tile(tile_under))
  880. tile_name = node->get_tileset()->tile_get_name(tile_under);
  881. tile_info->set_text(String::num(over_tile.x) + ", " + String::num(over_tile.y) + " [" + tile_name + "]");
  882. }
  883. if (tool == TOOL_PAINTING) {
  884. // Paint using bresenham line to prevent holes in painting if the user moves fast
  885. Vector<Point2i> points = line(old_over_tile.x, over_tile.x, old_over_tile.y, over_tile.y);
  886. Vector<int> ids = get_selected_tiles();
  887. for (int i = 0; i < points.size(); ++i) {
  888. Point2i pos = points[i];
  889. if (!paint_undo.has(pos)) {
  890. paint_undo[pos] = _get_op_from_cell(pos);
  891. }
  892. _set_cell(pos, ids, flip_h, flip_v, transpose);
  893. }
  894. return true;
  895. }
  896. if (tool == TOOL_ERASING) {
  897. // erase using bresenham line to prevent holes in painting if the user moves fast
  898. Vector<Point2i> points = line(old_over_tile.x, over_tile.x, old_over_tile.y, over_tile.y);
  899. for (int i = 0; i < points.size(); ++i) {
  900. Point2i pos = points[i];
  901. _set_cell(pos, invalid_cell);
  902. }
  903. return true;
  904. }
  905. if (tool == TOOL_SELECTING) {
  906. _select(rectangle_begin, over_tile);
  907. return true;
  908. }
  909. if (tool == TOOL_LINE_PAINT || tool == TOOL_LINE_ERASE) {
  910. Vector<int> ids = get_selected_tiles();
  911. Vector<int> tmp_cell;
  912. bool erasing = (tool == TOOL_LINE_ERASE);
  913. tmp_cell.push_back(0);
  914. if (erasing && paint_undo.size()) {
  915. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  916. tmp_cell.write[0] = E->get().idx;
  917. _set_cell(E->key(), tmp_cell, E->get().xf, E->get().yf, E->get().tr);
  918. }
  919. }
  920. paint_undo.clear();
  921. if (ids.size() > 0 && ids[0] != TileMap::INVALID_CELL) {
  922. Vector<Point2i> points = line(rectangle_begin.x, over_tile.x, rectangle_begin.y, over_tile.y);
  923. for (int i = 0; i < points.size(); i++) {
  924. paint_undo[points[i]] = _get_op_from_cell(points[i]);
  925. if (erasing)
  926. _set_cell(points[i], invalid_cell);
  927. }
  928. CanvasItemEditor::get_singleton()->update_viewport();
  929. }
  930. return true;
  931. }
  932. if (tool == TOOL_RECTANGLE_PAINT || tool == TOOL_RECTANGLE_ERASE) {
  933. Vector<int> tmp_cell;
  934. tmp_cell.push_back(0);
  935. _select(rectangle_begin, over_tile);
  936. if (tool == TOOL_RECTANGLE_ERASE) {
  937. if (paint_undo.size()) {
  938. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  939. tmp_cell.write[0] = E->get().idx;
  940. _set_cell(E->key(), tmp_cell, E->get().xf, E->get().yf, E->get().tr);
  941. }
  942. }
  943. paint_undo.clear();
  944. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  945. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  946. Point2i tile = Point2i(j, i);
  947. paint_undo[tile] = _get_op_from_cell(tile);
  948. _set_cell(tile, invalid_cell);
  949. }
  950. }
  951. }
  952. return true;
  953. }
  954. if (tool == TOOL_PICKING && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  955. _pick_tile(over_tile);
  956. return true;
  957. }
  958. }
  959. Ref<InputEventKey> k = p_event;
  960. if (k.is_valid() && k->is_pressed()) {
  961. if (k->get_scancode() == KEY_ESCAPE) {
  962. if (tool == TOOL_PASTING)
  963. copydata.clear();
  964. else if (tool == TOOL_SELECTING || selection_active)
  965. selection_active = false;
  966. tool = TOOL_NONE;
  967. CanvasItemEditor::get_singleton()->update_viewport();
  968. return true;
  969. }
  970. if (!mouse_over) {
  971. // Editor shortcuts should not fire if mouse not in viewport
  972. return false;
  973. }
  974. if (ED_IS_SHORTCUT("tile_map_editor/paint_tile", p_event)) {
  975. // NOTE: We do not set tool = TOOL_PAINTING as this begins painting
  976. // immediately without pressing the left mouse button first
  977. tool = TOOL_NONE;
  978. CanvasItemEditor::get_singleton()->update_viewport();
  979. return true;
  980. }
  981. if (ED_IS_SHORTCUT("tile_map_editor/bucket_fill", p_event)) {
  982. tool = TOOL_BUCKET;
  983. CanvasItemEditor::get_singleton()->update_viewport();
  984. return true;
  985. }
  986. if (ED_IS_SHORTCUT("tile_map_editor/erase_selection", p_event)) {
  987. _menu_option(OPTION_ERASE_SELECTION);
  988. return true;
  989. }
  990. if (ED_IS_SHORTCUT("tile_map_editor/select", p_event)) {
  991. tool = TOOL_SELECTING;
  992. selection_active = false;
  993. CanvasItemEditor::get_singleton()->update_viewport();
  994. return true;
  995. }
  996. if (ED_IS_SHORTCUT("tile_map_editor/copy_selection", p_event)) {
  997. _update_copydata();
  998. if (selection_active) {
  999. tool = TOOL_PASTING;
  1000. CanvasItemEditor::get_singleton()->update_viewport();
  1001. return true;
  1002. }
  1003. }
  1004. if (ED_IS_SHORTCUT("tile_map_editor/cut_selection", p_event)) {
  1005. if (selection_active) {
  1006. _update_copydata();
  1007. _start_undo(TTR("Cut Selection"));
  1008. _erase_selection();
  1009. _finish_undo();
  1010. selection_active = false;
  1011. tool = TOOL_PASTING;
  1012. CanvasItemEditor::get_singleton()->update_viewport();
  1013. return true;
  1014. }
  1015. }
  1016. if (ED_IS_SHORTCUT("tile_map_editor/find_tile", p_event)) {
  1017. search_box->select_all();
  1018. search_box->grab_focus();
  1019. return true;
  1020. }
  1021. if (ED_IS_SHORTCUT("tile_map_editor/mirror_x", p_event)) {
  1022. flip_h = !flip_h;
  1023. _update_palette();
  1024. CanvasItemEditor::get_singleton()->update_viewport();
  1025. return true;
  1026. }
  1027. if (ED_IS_SHORTCUT("tile_map_editor/mirror_y", p_event)) {
  1028. flip_v = !flip_v;
  1029. _update_palette();
  1030. CanvasItemEditor::get_singleton()->update_viewport();
  1031. return true;
  1032. }
  1033. if (ED_IS_SHORTCUT("tile_map_editor/transpose", p_event)) {
  1034. transpose = !transpose;
  1035. _update_palette();
  1036. CanvasItemEditor::get_singleton()->update_viewport();
  1037. return true;
  1038. }
  1039. }
  1040. return false;
  1041. }
  1042. void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  1043. if (!node)
  1044. return;
  1045. Transform2D cell_xf = node->get_cell_transform();
  1046. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform();
  1047. Transform2D xform_inv = xform.affine_inverse();
  1048. Size2 screen_size = p_overlay->get_size();
  1049. {
  1050. Rect2 aabb;
  1051. aabb.position = node->world_to_map(xform_inv.xform(Vector2()));
  1052. aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(0, screen_size.height))));
  1053. aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(screen_size.width, 0))));
  1054. aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size)));
  1055. Rect2i si = aabb.grow(1.0);
  1056. if (node->get_half_offset() != TileMap::HALF_OFFSET_X) {
  1057. int max_lines = 2000; //avoid crash if size too smal
  1058. for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
  1059. Vector2 from = xform.xform(node->map_to_world(Vector2(i, si.position.y)));
  1060. Vector2 to = xform.xform(node->map_to_world(Vector2(i, si.position.y + si.size.y + 1)));
  1061. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  1062. p_overlay->draw_line(from, to, col, 1);
  1063. if (max_lines-- == 0)
  1064. break;
  1065. }
  1066. } else {
  1067. int max_lines = 10000; //avoid crash if size too smal
  1068. for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
  1069. for (int j = (si.position.y) - 1; j <= (si.position.y + si.size.y); j++) {
  1070. Vector2 ofs;
  1071. if (ABS(j) & 1) {
  1072. ofs = cell_xf[0] * 0.5;
  1073. }
  1074. Vector2 from = xform.xform(node->map_to_world(Vector2(i, j), true) + ofs);
  1075. Vector2 to = xform.xform(node->map_to_world(Vector2(i, j + 1), true) + ofs);
  1076. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  1077. p_overlay->draw_line(from, to, col, 1);
  1078. if (max_lines-- == 0)
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. int max_lines = 10000; //avoid crash if size too smal
  1084. if (node->get_half_offset() != TileMap::HALF_OFFSET_Y) {
  1085. for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) {
  1086. Vector2 from = xform.xform(node->map_to_world(Vector2(si.position.x, i)));
  1087. Vector2 to = xform.xform(node->map_to_world(Vector2(si.position.x + si.size.x + 1, i)));
  1088. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  1089. p_overlay->draw_line(from, to, col, 1);
  1090. if (max_lines-- == 0)
  1091. break;
  1092. }
  1093. } else {
  1094. for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) {
  1095. for (int j = (si.position.x) - 1; j <= (si.position.x + si.size.x); j++) {
  1096. Vector2 ofs;
  1097. if (ABS(j) & 1) {
  1098. ofs = cell_xf[1] * 0.5;
  1099. }
  1100. Vector2 from = xform.xform(node->map_to_world(Vector2(j, i), true) + ofs);
  1101. Vector2 to = xform.xform(node->map_to_world(Vector2(j + 1, i), true) + ofs);
  1102. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  1103. p_overlay->draw_line(from, to, col, 1);
  1104. if (max_lines-- == 0)
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. if (selection_active) {
  1111. Vector<Vector2> points;
  1112. points.push_back(xform.xform(node->map_to_world((rectangle.position))));
  1113. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, 0)))));
  1114. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, rectangle.size.y + 1)))));
  1115. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(0, rectangle.size.y + 1)))));
  1116. p_overlay->draw_colored_polygon(points, Color(0.2, 0.8, 1, 0.4));
  1117. }
  1118. if (mouse_over) {
  1119. Vector2 endpoints[4] = {
  1120. node->map_to_world(over_tile, true),
  1121. node->map_to_world((over_tile + Point2(1, 0)), true),
  1122. node->map_to_world((over_tile + Point2(1, 1)), true),
  1123. node->map_to_world((over_tile + Point2(0, 1)), true)
  1124. };
  1125. for (int i = 0; i < 4; i++) {
  1126. if (node->get_half_offset() == TileMap::HALF_OFFSET_X && ABS(over_tile.y) & 1)
  1127. endpoints[i] += cell_xf[0] * 0.5;
  1128. if (node->get_half_offset() == TileMap::HALF_OFFSET_Y && ABS(over_tile.x) & 1)
  1129. endpoints[i] += cell_xf[1] * 0.5;
  1130. endpoints[i] = xform.xform(endpoints[i]);
  1131. }
  1132. Color col;
  1133. if (node->get_cell(over_tile.x, over_tile.y) != TileMap::INVALID_CELL)
  1134. col = Color(0.2, 0.8, 1.0, 0.8);
  1135. else
  1136. col = Color(1.0, 0.4, 0.2, 0.8);
  1137. for (int i = 0; i < 4; i++)
  1138. p_overlay->draw_line(endpoints[i], endpoints[(i + 1) % 4], col, 2);
  1139. bool bucket_preview = EditorSettings::get_singleton()->get("editors/tile_map/bucket_fill_preview");
  1140. if (tool == TOOL_SELECTING || tool == TOOL_PICKING || !bucket_preview) {
  1141. return;
  1142. }
  1143. if (tool == TOOL_LINE_PAINT) {
  1144. if (paint_undo.empty())
  1145. return;
  1146. Vector<int> ids = get_selected_tiles();
  1147. if (ids.size() == 1 && ids[0] == TileMap::INVALID_CELL)
  1148. return;
  1149. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  1150. _draw_cell(p_overlay, ids[0], E->key(), flip_h, flip_v, transpose, autotile_coord, xform);
  1151. }
  1152. } else if (tool == TOOL_RECTANGLE_PAINT) {
  1153. Vector<int> ids = get_selected_tiles();
  1154. if (ids.size() == 1 && ids[0] == TileMap::INVALID_CELL)
  1155. return;
  1156. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  1157. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  1158. _draw_cell(p_overlay, ids[0], Point2i(j, i), flip_h, flip_v, transpose, autotile_coord, xform);
  1159. }
  1160. }
  1161. } else if (tool == TOOL_PASTING) {
  1162. if (copydata.empty())
  1163. return;
  1164. Ref<TileSet> ts = node->get_tileset();
  1165. if (ts.is_null())
  1166. return;
  1167. Point2 ofs = over_tile - rectangle.position;
  1168. for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) {
  1169. if (!ts->has_tile(E->get().cell))
  1170. continue;
  1171. TileData tcd = E->get();
  1172. _draw_cell(p_overlay, tcd.cell, tcd.pos + ofs, tcd.flip_h, tcd.flip_v, tcd.transpose, tcd.autotile_coord, xform);
  1173. }
  1174. Rect2i duplicate = rectangle;
  1175. duplicate.position = over_tile;
  1176. Vector<Vector2> points;
  1177. points.push_back(xform.xform(node->map_to_world(duplicate.position)));
  1178. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, 0)))));
  1179. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, duplicate.size.y + 1)))));
  1180. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(0, duplicate.size.y + 1)))));
  1181. p_overlay->draw_colored_polygon(points, Color(0.2, 1.0, 0.8, 0.2));
  1182. } else if (tool == TOOL_BUCKET) {
  1183. Vector<int> tiles = get_selected_tiles();
  1184. _draw_fill_preview(p_overlay, tiles[0], over_tile, flip_h, flip_v, transpose, autotile_coord, xform);
  1185. } else {
  1186. Vector<int> st = get_selected_tiles();
  1187. if (st.size() == 1 && st[0] == TileMap::INVALID_CELL)
  1188. return;
  1189. _draw_cell(p_overlay, st[0], over_tile, flip_h, flip_v, transpose, autotile_coord, xform);
  1190. }
  1191. }
  1192. }
  1193. void TileMapEditor::edit(Node *p_tile_map) {
  1194. search_box->set_text("");
  1195. if (!canvas_item_editor_viewport) {
  1196. canvas_item_editor_viewport = CanvasItemEditor::get_singleton()->get_viewport_control();
  1197. }
  1198. if (node)
  1199. node->disconnect("settings_changed", this, "_tileset_settings_changed");
  1200. if (p_tile_map) {
  1201. node = Object::cast_to<TileMap>(p_tile_map);
  1202. if (!canvas_item_editor_viewport->is_connected("mouse_entered", this, "_canvas_mouse_enter"))
  1203. canvas_item_editor_viewport->connect("mouse_entered", this, "_canvas_mouse_enter");
  1204. if (!canvas_item_editor_viewport->is_connected("mouse_exited", this, "_canvas_mouse_exit"))
  1205. canvas_item_editor_viewport->connect("mouse_exited", this, "_canvas_mouse_exit");
  1206. _update_palette();
  1207. } else {
  1208. node = NULL;
  1209. if (canvas_item_editor_viewport->is_connected("mouse_entered", this, "_canvas_mouse_enter"))
  1210. canvas_item_editor_viewport->disconnect("mouse_entered", this, "_canvas_mouse_enter");
  1211. if (canvas_item_editor_viewport->is_connected("mouse_exited", this, "_canvas_mouse_exit"))
  1212. canvas_item_editor_viewport->disconnect("mouse_exited", this, "_canvas_mouse_exit");
  1213. _update_palette();
  1214. }
  1215. if (node)
  1216. node->connect("settings_changed", this, "_tileset_settings_changed");
  1217. _clear_bucket_cache();
  1218. }
  1219. void TileMapEditor::_tileset_settings_changed() {
  1220. _update_palette();
  1221. CanvasItemEditor::get_singleton()->update_viewport();
  1222. }
  1223. void TileMapEditor::_icon_size_changed(float p_value) {
  1224. if (node) {
  1225. palette->set_icon_scale(p_value);
  1226. manual_palette->set_icon_scale(p_value);
  1227. _update_palette();
  1228. }
  1229. }
  1230. void TileMapEditor::_bind_methods() {
  1231. ClassDB::bind_method(D_METHOD("_manual_toggled"), &TileMapEditor::_manual_toggled);
  1232. ClassDB::bind_method(D_METHOD("_text_entered"), &TileMapEditor::_text_entered);
  1233. ClassDB::bind_method(D_METHOD("_text_changed"), &TileMapEditor::_text_changed);
  1234. ClassDB::bind_method(D_METHOD("_sbox_input"), &TileMapEditor::_sbox_input);
  1235. ClassDB::bind_method(D_METHOD("_menu_option"), &TileMapEditor::_menu_option);
  1236. ClassDB::bind_method(D_METHOD("_canvas_mouse_enter"), &TileMapEditor::_canvas_mouse_enter);
  1237. ClassDB::bind_method(D_METHOD("_canvas_mouse_exit"), &TileMapEditor::_canvas_mouse_exit);
  1238. ClassDB::bind_method(D_METHOD("_tileset_settings_changed"), &TileMapEditor::_tileset_settings_changed);
  1239. ClassDB::bind_method(D_METHOD("_rotate"), &TileMapEditor::_rotate);
  1240. ClassDB::bind_method(D_METHOD("_flip_horizontal"), &TileMapEditor::_flip_horizontal);
  1241. ClassDB::bind_method(D_METHOD("_flip_vertical"), &TileMapEditor::_flip_vertical);
  1242. ClassDB::bind_method(D_METHOD("_clear_transform"), &TileMapEditor::_clear_transform);
  1243. ClassDB::bind_method(D_METHOD("_palette_selected"), &TileMapEditor::_palette_selected);
  1244. ClassDB::bind_method(D_METHOD("_palette_multi_selected"), &TileMapEditor::_palette_multi_selected);
  1245. ClassDB::bind_method(D_METHOD("_fill_points"), &TileMapEditor::_fill_points);
  1246. ClassDB::bind_method(D_METHOD("_erase_points"), &TileMapEditor::_erase_points);
  1247. ClassDB::bind_method(D_METHOD("_icon_size_changed"), &TileMapEditor::_icon_size_changed);
  1248. }
  1249. TileMapEditor::CellOp TileMapEditor::_get_op_from_cell(const Point2i &p_pos) {
  1250. CellOp op;
  1251. op.idx = node->get_cell(p_pos.x, p_pos.y);
  1252. if (op.idx != TileMap::INVALID_CELL) {
  1253. if (node->is_cell_x_flipped(p_pos.x, p_pos.y))
  1254. op.xf = true;
  1255. if (node->is_cell_y_flipped(p_pos.x, p_pos.y))
  1256. op.yf = true;
  1257. if (node->is_cell_transposed(p_pos.x, p_pos.y))
  1258. op.tr = true;
  1259. op.ac = node->get_cell_autotile_coord(p_pos.x, p_pos.y);
  1260. }
  1261. return op;
  1262. }
  1263. void TileMapEditor::_rotate(int steps) {
  1264. const bool normal_rotation_matrix[][3] = {
  1265. { false, false, false },
  1266. { true, true, false },
  1267. { false, true, true },
  1268. { true, false, true }
  1269. };
  1270. const bool mirrored_rotation_matrix[][3] = {
  1271. { false, true, false },
  1272. { true, true, true },
  1273. { false, false, true },
  1274. { true, false, false }
  1275. };
  1276. if (transpose ^ flip_h ^ flip_v) {
  1277. // Odd number of flags activated = mirrored rotation
  1278. for (int i = 0; i < 4; i++) {
  1279. if (transpose == mirrored_rotation_matrix[i][0] &&
  1280. flip_h == mirrored_rotation_matrix[i][1] &&
  1281. flip_v == mirrored_rotation_matrix[i][2]) {
  1282. int new_id = Math::wrapi(i + steps, 0, 4);
  1283. transpose = mirrored_rotation_matrix[new_id][0];
  1284. flip_h = mirrored_rotation_matrix[new_id][1];
  1285. flip_v = mirrored_rotation_matrix[new_id][2];
  1286. break;
  1287. }
  1288. }
  1289. } else {
  1290. // Even number of flags activated = normal rotation
  1291. for (int i = 0; i < 4; i++) {
  1292. if (transpose == normal_rotation_matrix[i][0] &&
  1293. flip_h == normal_rotation_matrix[i][1] &&
  1294. flip_v == normal_rotation_matrix[i][2]) {
  1295. int new_id = Math::wrapi(i + steps, 0, 4);
  1296. transpose = normal_rotation_matrix[new_id][0];
  1297. flip_h = normal_rotation_matrix[new_id][1];
  1298. flip_v = normal_rotation_matrix[new_id][2];
  1299. break;
  1300. }
  1301. }
  1302. }
  1303. _update_palette();
  1304. }
  1305. void TileMapEditor::_flip_horizontal() {
  1306. flip_h = !flip_h;
  1307. _update_palette();
  1308. }
  1309. void TileMapEditor::_flip_vertical() {
  1310. flip_v = !flip_v;
  1311. _update_palette();
  1312. }
  1313. void TileMapEditor::_clear_transform() {
  1314. transpose = false;
  1315. flip_h = false;
  1316. flip_v = false;
  1317. _update_palette();
  1318. }
  1319. TileMapEditor::TileMapEditor(EditorNode *p_editor) {
  1320. node = NULL;
  1321. manual_autotile = false;
  1322. manual_position = Vector2(0, 0);
  1323. canvas_item_editor_viewport = NULL;
  1324. editor = p_editor;
  1325. undo_redo = editor->get_undo_redo();
  1326. tool = TOOL_NONE;
  1327. selection_active = false;
  1328. mouse_over = false;
  1329. show_tile_info = true;
  1330. flip_h = false;
  1331. flip_v = false;
  1332. transpose = false;
  1333. bucket_cache_tile = -1;
  1334. bucket_cache_visited = 0;
  1335. invalid_cell.resize(1);
  1336. invalid_cell.write[0] = TileMap::INVALID_CELL;
  1337. ED_SHORTCUT("tile_map_editor/erase_selection", TTR("Erase Selection"), KEY_DELETE);
  1338. ED_SHORTCUT("tile_map_editor/find_tile", TTR("Find Tile"), KEY_MASK_CMD + KEY_F);
  1339. ED_SHORTCUT("tile_map_editor/transpose", TTR("Transpose"), KEY_T);
  1340. ED_SHORTCUT("tile_map_editor/mirror_x", TTR("Mirror X"), KEY_A);
  1341. ED_SHORTCUT("tile_map_editor/mirror_y", TTR("Mirror Y"), KEY_S);
  1342. HBoxContainer *tool_hb = memnew(HBoxContainer);
  1343. add_child(tool_hb);
  1344. manual_button = memnew(CheckBox);
  1345. manual_button->set_text("Disable Autotile");
  1346. manual_button->connect("toggled", this, "_manual_toggled");
  1347. add_child(manual_button);
  1348. search_box = memnew(LineEdit);
  1349. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1350. search_box->connect("text_entered", this, "_text_entered");
  1351. search_box->connect("text_changed", this, "_text_changed");
  1352. search_box->connect("gui_input", this, "_sbox_input");
  1353. add_child(search_box);
  1354. size_slider = memnew(HSlider);
  1355. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1356. size_slider->set_min(0.1f);
  1357. size_slider->set_max(4.0f);
  1358. size_slider->set_step(0.1f);
  1359. size_slider->set_value(1.0f);
  1360. size_slider->connect("value_changed", this, "_icon_size_changed");
  1361. add_child(size_slider);
  1362. int mw = EDITOR_DEF("editors/tile_map/palette_min_width", 80);
  1363. VSplitContainer *palette_container = memnew(VSplitContainer);
  1364. palette_container->set_v_size_flags(SIZE_EXPAND_FILL);
  1365. palette_container->set_custom_minimum_size(Size2(mw, 0));
  1366. add_child(palette_container);
  1367. // Add tile palette
  1368. palette = memnew(ItemList);
  1369. palette->set_h_size_flags(SIZE_EXPAND_FILL);
  1370. palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1371. palette->set_max_columns(0);
  1372. palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  1373. palette->set_max_text_lines(2);
  1374. palette->set_select_mode(ItemList::SELECT_MULTI);
  1375. palette->connect("item_selected", this, "_palette_selected");
  1376. palette->connect("multi_selected", this, "_palette_multi_selected");
  1377. palette_container->add_child(palette);
  1378. // Add autotile override palette
  1379. manual_palette = memnew(ItemList);
  1380. manual_palette->set_h_size_flags(SIZE_EXPAND_FILL);
  1381. manual_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1382. manual_palette->set_max_columns(0);
  1383. manual_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  1384. manual_palette->set_max_text_lines(2);
  1385. manual_palette->hide();
  1386. palette_container->add_child(manual_palette);
  1387. // Add menu items
  1388. toolbar = memnew(HBoxContainer);
  1389. toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  1390. toolbar->set_alignment(BoxContainer::ALIGN_END);
  1391. toolbar->hide();
  1392. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(toolbar);
  1393. // Tile position
  1394. tile_info = memnew(Label);
  1395. toolbar->add_child(tile_info);
  1396. options = memnew(MenuButton);
  1397. options->set_text("TileMap");
  1398. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("TileMap", "EditorIcons"));
  1399. options->set_process_unhandled_key_input(false);
  1400. PopupMenu *p = options->get_popup();
  1401. p->add_shortcut(ED_SHORTCUT("tile_map_editor/paint_tile", TTR("Paint Tile"), KEY_P), OPTION_PAINTING);
  1402. p->add_shortcut(ED_SHORTCUT("tile_map_editor/bucket_fill", TTR("Bucket Fill"), KEY_G), OPTION_BUCKET);
  1403. p->add_separator();
  1404. p->add_item(TTR("Pick Tile"), OPTION_PICK_TILE, KEY_CONTROL);
  1405. p->add_separator();
  1406. p->add_shortcut(ED_SHORTCUT("tile_map_editor/select", TTR("Select"), KEY_MASK_CMD + KEY_B), OPTION_SELECT);
  1407. p->add_shortcut(ED_SHORTCUT("tile_map_editor/cut_selection", TTR("Cut Selection"), KEY_MASK_CMD + KEY_X), OPTION_CUT);
  1408. p->add_shortcut(ED_SHORTCUT("tile_map_editor/copy_selection", TTR("Copy Selection"), KEY_MASK_CMD + KEY_C), OPTION_COPY);
  1409. p->add_shortcut(ED_GET_SHORTCUT("tile_map_editor/erase_selection"), OPTION_ERASE_SELECTION);
  1410. p->add_separator();
  1411. p->add_item(TTR("Fix Invalid Tiles"), OPTION_FIX_INVALID);
  1412. p->connect("id_pressed", this, "_menu_option");
  1413. toolbar->add_child(options);
  1414. rotate_left_button = memnew(ToolButton);
  1415. rotate_left_button->set_tooltip(TTR("Rotate left"));
  1416. rotate_left_button->set_focus_mode(FOCUS_NONE);
  1417. rotate_left_button->connect("pressed", this, "_rotate", varray(-1));
  1418. tool_hb->add_child(rotate_left_button);
  1419. rotate_right_button = memnew(ToolButton);
  1420. rotate_right_button->set_tooltip(TTR("Rotate right"));
  1421. rotate_right_button->set_focus_mode(FOCUS_NONE);
  1422. rotate_right_button->connect("pressed", this, "_rotate", varray(1));
  1423. tool_hb->add_child(rotate_right_button);
  1424. flip_horizontal_button = memnew(ToolButton);
  1425. flip_horizontal_button->set_tooltip(TTR("Flip horizontally"));
  1426. flip_horizontal_button->set_focus_mode(FOCUS_NONE);
  1427. flip_horizontal_button->connect("pressed", this, "_flip_horizontal");
  1428. tool_hb->add_child(flip_horizontal_button);
  1429. flip_vertical_button = memnew(ToolButton);
  1430. flip_vertical_button->set_tooltip(TTR("Flip vertically"));
  1431. flip_vertical_button->set_focus_mode(FOCUS_NONE);
  1432. flip_vertical_button->connect("pressed", this, "_flip_vertical");
  1433. tool_hb->add_child(flip_vertical_button);
  1434. clear_transform_button = memnew(ToolButton);
  1435. clear_transform_button->set_tooltip(TTR("Clear transform"));
  1436. clear_transform_button->set_focus_mode(FOCUS_NONE);
  1437. clear_transform_button->connect("pressed", this, "_clear_transform");
  1438. tool_hb->add_child(clear_transform_button);
  1439. clear_transform_button->set_disabled(true);
  1440. }
  1441. TileMapEditor::~TileMapEditor() {
  1442. _clear_bucket_cache();
  1443. copydata.clear();
  1444. }
  1445. ///////////////////////////////////////////////////////////////
  1446. ///////////////////////////////////////////////////////////////
  1447. ///////////////////////////////////////////////////////////////
  1448. void TileMapEditorPlugin::_notification(int p_what) {
  1449. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  1450. switch ((int)EditorSettings::get_singleton()->get("editors/tile_map/editor_side")) {
  1451. case 0: { // Left.
  1452. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(tile_map_editor, 0);
  1453. } break;
  1454. case 1: { // Right.
  1455. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(tile_map_editor, 1);
  1456. } break;
  1457. }
  1458. }
  1459. }
  1460. void TileMapEditorPlugin::edit(Object *p_object) {
  1461. tile_map_editor->edit(Object::cast_to<Node>(p_object));
  1462. }
  1463. bool TileMapEditorPlugin::handles(Object *p_object) const {
  1464. return p_object->is_class("TileMap");
  1465. }
  1466. void TileMapEditorPlugin::make_visible(bool p_visible) {
  1467. if (p_visible) {
  1468. tile_map_editor->show();
  1469. tile_map_editor->get_toolbar()->show();
  1470. } else {
  1471. tile_map_editor->hide();
  1472. tile_map_editor->get_toolbar()->hide();
  1473. tile_map_editor->edit(NULL);
  1474. }
  1475. }
  1476. TileMapEditorPlugin::TileMapEditorPlugin(EditorNode *p_node) {
  1477. EDITOR_DEF("editors/tile_map/preview_size", 64);
  1478. EDITOR_DEF("editors/tile_map/palette_item_hseparation", 8);
  1479. EDITOR_DEF("editors/tile_map/show_tile_names", true);
  1480. EDITOR_DEF("editors/tile_map/show_tile_ids", false);
  1481. EDITOR_DEF("editors/tile_map/sort_tiles_by_name", true);
  1482. EDITOR_DEF("editors/tile_map/bucket_fill_preview", true);
  1483. EDITOR_DEF("editors/tile_map/show_tile_info_on_hover", true);
  1484. EDITOR_DEF("editors/tile_map/editor_side", 1);
  1485. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/tile_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1486. tile_map_editor = memnew(TileMapEditor(p_node));
  1487. switch ((int)EditorSettings::get_singleton()->get("editors/tile_map/editor_side")) {
  1488. case 0: { // Left.
  1489. add_control_to_container(CONTAINER_CANVAS_EDITOR_SIDE_LEFT, tile_map_editor);
  1490. } break;
  1491. case 1: { // Right.
  1492. add_control_to_container(CONTAINER_CANVAS_EDITOR_SIDE_RIGHT, tile_map_editor);
  1493. } break;
  1494. }
  1495. tile_map_editor->hide();
  1496. }
  1497. TileMapEditorPlugin::~TileMapEditorPlugin() {
  1498. }