tile_map_editor_plugin.cpp 64 KB

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