tile_map_editor_plugin.cpp 60 KB

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