tile_map_editor_plugin.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tile_map_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. #include "os/input.h"
  35. #include "os/keyboard.h"
  36. void TileMapEditor::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_PROCESS: {
  39. if (bucket_queue.size() && canvas_item_editor) {
  40. canvas_item_editor->update();
  41. }
  42. } break;
  43. case NOTIFICATION_ENTER_TREE: {
  44. transp->set_icon(get_icon("Transpose", "EditorIcons"));
  45. mirror_x->set_icon(get_icon("MirrorX", "EditorIcons"));
  46. mirror_y->set_icon(get_icon("MirrorY", "EditorIcons"));
  47. rotate_0->set_icon(get_icon("Rotate0", "EditorIcons"));
  48. rotate_90->set_icon(get_icon("Rotate90", "EditorIcons"));
  49. rotate_180->set_icon(get_icon("Rotate180", "EditorIcons"));
  50. rotate_270->set_icon(get_icon("Rotate270", "EditorIcons"));
  51. search_box->add_icon_override("right_icon", get_icon("Search", "EditorIcons"));
  52. } break;
  53. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  54. bool new_show_tile_info = EditorSettings::get_singleton()->get("editors/tile_map/show_tile_info_on_hover");
  55. if (new_show_tile_info != show_tile_info) {
  56. show_tile_info = new_show_tile_info;
  57. tile_info->set_visible(show_tile_info);
  58. }
  59. if (is_visible_in_tree()) {
  60. _update_palette();
  61. }
  62. } break;
  63. }
  64. }
  65. void TileMapEditor::_menu_option(int p_option) {
  66. switch (p_option) {
  67. case OPTION_PAINTING: {
  68. // NOTE: We do not set tool = TOOL_PAINTING as this begins painting
  69. // immediately without pressing the left mouse button first
  70. tool = TOOL_NONE;
  71. canvas_item_editor->update();
  72. } break;
  73. case OPTION_BUCKET: {
  74. tool = TOOL_BUCKET;
  75. canvas_item_editor->update();
  76. } break;
  77. case OPTION_PICK_TILE: {
  78. tool = TOOL_PICKING;
  79. canvas_item_editor->update();
  80. } break;
  81. case OPTION_SELECT: {
  82. tool = TOOL_SELECTING;
  83. selection_active = false;
  84. canvas_item_editor->update();
  85. } break;
  86. case OPTION_DUPLICATE: {
  87. _update_copydata();
  88. if (selection_active) {
  89. tool = TOOL_DUPLICATING;
  90. canvas_item_editor->update();
  91. }
  92. } break;
  93. case OPTION_ERASE_SELECTION: {
  94. if (!selection_active)
  95. return;
  96. undo_redo->create_action(TTR("Erase Selection"));
  97. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  98. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  99. _set_cell(Point2i(j, i), TileMap::INVALID_CELL, false, false, false, true);
  100. }
  101. }
  102. undo_redo->commit_action();
  103. selection_active = false;
  104. copydata.clear();
  105. canvas_item_editor->update();
  106. } break;
  107. }
  108. }
  109. void TileMapEditor::_canvas_mouse_enter() {
  110. mouse_over = true;
  111. canvas_item_editor->update();
  112. }
  113. void TileMapEditor::_canvas_mouse_exit() {
  114. mouse_over = false;
  115. canvas_item_editor->update();
  116. }
  117. int TileMapEditor::get_selected_tile() const {
  118. int item = palette->get_current();
  119. if (item == -1)
  120. return TileMap::INVALID_CELL;
  121. return palette->get_item_metadata(item);
  122. }
  123. void TileMapEditor::set_selected_tile(int p_tile) {
  124. int idx = palette->find_metadata(p_tile);
  125. if (idx >= 0) {
  126. palette->select(idx, true);
  127. palette->ensure_current_is_visible();
  128. }
  129. }
  130. void TileMapEditor::_set_cell(const Point2i &p_pos, int p_value, bool p_flip_h, bool p_flip_v, bool p_transpose, bool p_with_undo) {
  131. ERR_FAIL_COND(!node);
  132. int prev_val = node->get_cell(p_pos.x, p_pos.y);
  133. bool prev_flip_h = node->is_cell_x_flipped(p_pos.x, p_pos.y);
  134. bool prev_flip_v = node->is_cell_y_flipped(p_pos.x, p_pos.y);
  135. bool prev_transpose = node->is_cell_transposed(p_pos.x, p_pos.y);
  136. if (p_value == prev_val && p_flip_h == prev_flip_h && p_flip_v == prev_flip_v && p_transpose == prev_transpose)
  137. return; //check that it's actually different
  138. if (p_with_undo) {
  139. undo_redo->add_do_method(node, "set_cellv", Point2(p_pos), p_value, p_flip_h, p_flip_v, p_transpose);
  140. undo_redo->add_undo_method(node, "set_cellv", Point2(p_pos), prev_val, prev_flip_h, prev_flip_v, prev_transpose);
  141. } else {
  142. node->set_cell(p_pos.x, p_pos.y, p_value, p_flip_h, p_flip_v, p_transpose);
  143. }
  144. }
  145. void TileMapEditor::_text_entered(const String &p_text) {
  146. canvas_item_editor->grab_focus();
  147. }
  148. void TileMapEditor::_text_changed(const String &p_text) {
  149. _update_palette();
  150. }
  151. void TileMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  152. Ref<InputEventKey> k = p_ie;
  153. if (k.is_valid() && (k->get_scancode() == KEY_UP ||
  154. k->get_scancode() == KEY_DOWN ||
  155. k->get_scancode() == KEY_PAGEUP ||
  156. k->get_scancode() == KEY_PAGEDOWN)) {
  157. palette->call("_gui_input", k);
  158. search_box->accept_event();
  159. }
  160. }
  161. // Implementation detail of TileMapEditor::_update_palette();
  162. // in modern C++ this could have been inside its body
  163. namespace {
  164. struct _PaletteEntry {
  165. int id;
  166. String name;
  167. bool operator<(const _PaletteEntry &p_rhs) const {
  168. return name < p_rhs.name;
  169. }
  170. };
  171. } // namespace
  172. void TileMapEditor::_update_palette() {
  173. if (!node)
  174. return;
  175. int selected = get_selected_tile();
  176. palette->clear();
  177. Ref<TileSet> tileset = node->get_tileset();
  178. if (tileset.is_null())
  179. return;
  180. List<int> tiles;
  181. tileset->get_tile_list(&tiles);
  182. if (tiles.empty())
  183. return;
  184. float min_size = EDITOR_DEF("editors/tile_map/preview_size", 64);
  185. min_size *= EDSCALE;
  186. int hseparation = EDITOR_DEF("editors/tile_map/palette_item_hseparation", 8);
  187. bool show_tile_names = bool(EDITOR_DEF("editors/tile_map/show_tile_names", true));
  188. bool show_tile_ids = bool(EDITOR_DEF("editors/tile_map/show_tile_ids", false));
  189. bool sort_by_name = bool(EDITOR_DEF("editors/tile_map/sort_tiles_by_name", true));
  190. palette->add_constant_override("hseparation", hseparation * EDSCALE);
  191. palette->add_constant_override("vseparation", 8 * EDSCALE);
  192. palette->set_fixed_icon_size(Size2(min_size, min_size));
  193. palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1));
  194. String filter = search_box->get_text().strip_edges();
  195. Vector<_PaletteEntry> entries;
  196. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  197. String name = tileset->tile_get_name(E->get());
  198. if (name != "") {
  199. if (show_tile_ids) {
  200. if (sort_by_name) {
  201. name = name + " - " + itos(E->get());
  202. } else {
  203. name = itos(E->get()) + " - " + name;
  204. }
  205. }
  206. } else {
  207. name = "#" + itos(E->get());
  208. }
  209. if (filter != "" && !filter.is_subsequence_ofi(name))
  210. continue;
  211. const _PaletteEntry entry = { E->get(), name };
  212. entries.push_back(entry);
  213. }
  214. if (sort_by_name) {
  215. entries.sort();
  216. }
  217. for (int i = 0; i < entries.size(); i++) {
  218. if (show_tile_names) {
  219. palette->add_item(entries[i].name);
  220. } else {
  221. palette->add_item(String());
  222. }
  223. Ref<Texture> tex = tileset->tile_get_texture(entries[i].id);
  224. if (tex.is_valid()) {
  225. Rect2 region = tileset->tile_get_region(entries[i].id);
  226. if (!region.has_no_area())
  227. palette->set_item_icon_region(palette->get_item_count() - 1, region);
  228. palette->set_item_icon(palette->get_item_count() - 1, tex);
  229. }
  230. palette->set_item_metadata(palette->get_item_count() - 1, entries[i].id);
  231. }
  232. palette->set_same_column_width(true);
  233. if (selected != -1)
  234. set_selected_tile(selected);
  235. else
  236. palette->select(0);
  237. }
  238. void TileMapEditor::_pick_tile(const Point2 &p_pos) {
  239. int id = node->get_cell(p_pos.x, p_pos.y);
  240. if (id == TileMap::INVALID_CELL)
  241. return;
  242. if (search_box->get_text().strip_edges() != "") {
  243. search_box->set_text("");
  244. _update_palette();
  245. }
  246. set_selected_tile(id);
  247. mirror_x->set_pressed(node->is_cell_x_flipped(p_pos.x, p_pos.y));
  248. mirror_y->set_pressed(node->is_cell_y_flipped(p_pos.x, p_pos.y));
  249. transp->set_pressed(node->is_cell_transposed(p_pos.x, p_pos.y));
  250. _update_transform_buttons();
  251. canvas_item_editor->update();
  252. }
  253. PoolVector<Vector2> TileMapEditor::_bucket_fill(const Point2i &p_start, bool erase, bool preview) {
  254. int prev_id = node->get_cell(p_start.x, p_start.y);
  255. int id = TileMap::INVALID_CELL;
  256. if (!erase) {
  257. id = get_selected_tile();
  258. if (id == TileMap::INVALID_CELL)
  259. return PoolVector<Vector2>();
  260. } else if (prev_id == TileMap::INVALID_CELL) {
  261. return PoolVector<Vector2>();
  262. }
  263. if (id == prev_id) {
  264. return PoolVector<Vector2>();
  265. }
  266. Rect2i r = node->get_item_rect();
  267. r.position = r.position / node->get_cell_size();
  268. r.size = r.size / node->get_cell_size();
  269. int area = r.get_area();
  270. if (preview) {
  271. // Test if we can re-use the result from preview bucket fill
  272. bool invalidate_cache = false;
  273. // Area changed
  274. if (r != bucket_cache_rect)
  275. _clear_bucket_cache();
  276. // Cache grid is not initialized
  277. if (bucket_cache_visited == 0) {
  278. bucket_cache_visited = new bool[area];
  279. invalidate_cache = true;
  280. }
  281. // Tile ID changed or position wasn't visited by the previous fill
  282. int loc = (p_start.x - r.position.x) + (p_start.y - r.position.y) * r.get_size().x;
  283. if (prev_id != bucket_cache_tile || !bucket_cache_visited[loc]) {
  284. invalidate_cache = true;
  285. }
  286. if (invalidate_cache) {
  287. for (int i = 0; i < area; ++i)
  288. bucket_cache_visited[i] = false;
  289. bucket_cache = PoolVector<Vector2>();
  290. bucket_cache_tile = prev_id;
  291. bucket_cache_rect = r;
  292. bucket_queue.clear();
  293. }
  294. }
  295. PoolVector<Vector2> points;
  296. int count = 0;
  297. int limit = 0;
  298. if (preview) {
  299. limit = 1024;
  300. } else {
  301. bucket_queue.clear();
  302. }
  303. bucket_queue.push_back(p_start);
  304. while (bucket_queue.size()) {
  305. Point2i n = bucket_queue.front()->get();
  306. bucket_queue.pop_front();
  307. if (!r.has_point(n))
  308. continue;
  309. if (node->get_cell(n.x, n.y) == prev_id) {
  310. if (preview) {
  311. int loc = (n.x - r.position.x) + (n.y - r.position.y) * r.get_size().x;
  312. if (bucket_cache_visited[loc])
  313. continue;
  314. bucket_cache_visited[loc] = true;
  315. bucket_cache.push_back(n);
  316. } else {
  317. node->set_cellv(n, id, flip_h, flip_v, transpose);
  318. points.push_back(n);
  319. }
  320. bucket_queue.push_back(Point2i(n.x, n.y + 1));
  321. bucket_queue.push_back(Point2i(n.x, n.y - 1));
  322. bucket_queue.push_back(Point2i(n.x + 1, n.y));
  323. bucket_queue.push_back(Point2i(n.x - 1, n.y));
  324. count++;
  325. }
  326. if (limit > 0 && count >= limit) {
  327. break;
  328. }
  329. }
  330. return preview ? bucket_cache : points;
  331. }
  332. void TileMapEditor::_fill_points(const PoolVector<Vector2> p_points, const Dictionary &p_op) {
  333. int len = p_points.size();
  334. PoolVector<Vector2>::Read pr = p_points.read();
  335. int id = p_op["id"];
  336. bool xf = p_op["flip_h"];
  337. bool yf = p_op["flip_v"];
  338. bool tr = p_op["transpose"];
  339. for (int i = 0; i < len; i++) {
  340. _set_cell(pr[i], id, xf, yf, tr);
  341. }
  342. }
  343. void TileMapEditor::_erase_points(const PoolVector<Vector2> p_points) {
  344. int len = p_points.size();
  345. PoolVector<Vector2>::Read pr = p_points.read();
  346. for (int i = 0; i < len; i++) {
  347. _set_cell(pr[i], TileMap::INVALID_CELL);
  348. }
  349. }
  350. void TileMapEditor::_select(const Point2i &p_from, const Point2i &p_to) {
  351. Point2i begin = p_from;
  352. Point2i end = p_to;
  353. if (begin.x > end.x) {
  354. SWAP(begin.x, end.x);
  355. }
  356. if (begin.y > end.y) {
  357. SWAP(begin.y, end.y);
  358. }
  359. rectangle.position = begin;
  360. rectangle.size = end - begin;
  361. canvas_item_editor->update();
  362. }
  363. void TileMapEditor::_draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Transform2D &p_xform) {
  364. Ref<Texture> t = node->get_tileset()->tile_get_texture(p_cell);
  365. if (t.is_null())
  366. return;
  367. Vector2 tile_ofs = node->get_tileset()->tile_get_texture_offset(p_cell);
  368. Rect2 r = node->get_tileset()->tile_get_region(p_cell);
  369. Size2 sc = p_xform.get_scale();
  370. Rect2 rect = Rect2();
  371. rect.position = node->map_to_world(p_point) + node->get_cell_draw_offset();
  372. if (r.has_no_area()) {
  373. rect.size = t->get_size();
  374. } else {
  375. rect.size = r.size;
  376. }
  377. if (rect.size.y > rect.size.x) {
  378. if ((p_flip_h && (p_flip_v || p_transpose)) || (p_flip_v && !p_transpose))
  379. tile_ofs.y += rect.size.y - rect.size.x;
  380. } else if (rect.size.y < rect.size.x) {
  381. if ((p_flip_v && (p_flip_h || p_transpose)) || (p_flip_h && !p_transpose))
  382. tile_ofs.x += rect.size.x - rect.size.y;
  383. }
  384. if (p_transpose) {
  385. SWAP(tile_ofs.x, tile_ofs.y);
  386. }
  387. if (p_flip_h) {
  388. sc.x *= -1.0;
  389. tile_ofs.x *= -1.0;
  390. }
  391. if (p_flip_v) {
  392. sc.y *= -1.0;
  393. tile_ofs.y *= -1.0;
  394. }
  395. if (node->get_tile_origin() == TileMap::TILE_ORIGIN_TOP_LEFT) {
  396. rect.position += tile_ofs;
  397. } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_BOTTOM_LEFT) {
  398. Size2 cell_size = node->get_cell_size();
  399. rect.position += tile_ofs;
  400. if (p_transpose) {
  401. if (p_flip_h)
  402. rect.position.x -= cell_size.x;
  403. else
  404. rect.position.x += cell_size.x;
  405. } else {
  406. if (p_flip_v)
  407. rect.position.y -= cell_size.y;
  408. else
  409. rect.position.y += cell_size.y;
  410. }
  411. } else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_CENTER) {
  412. rect.position += node->get_cell_size() / 2;
  413. Vector2 s = r.size;
  414. Vector2 center = (s / 2) - tile_ofs;
  415. if (p_flip_h)
  416. rect.position.x -= s.x - center.x;
  417. else
  418. rect.position.x -= center.x;
  419. if (p_flip_v)
  420. rect.position.y -= s.y - center.y;
  421. else
  422. rect.position.y -= center.y;
  423. }
  424. rect.position = p_xform.xform(rect.position);
  425. rect.size *= sc;
  426. if (r.has_no_area())
  427. canvas_item_editor->draw_texture_rect(t, rect, false, Color(1, 1, 1, 0.5), p_transpose);
  428. else
  429. canvas_item_editor->draw_texture_rect_region(t, rect, r, Color(1, 1, 1, 0.5), p_transpose);
  430. }
  431. void TileMapEditor::_draw_fill_preview(int p_cell, const Point2i &p_point, bool p_flip_h, bool p_flip_v, bool p_transpose, const Transform2D &p_xform) {
  432. PoolVector<Vector2> points = _bucket_fill(p_point, false, true);
  433. PoolVector<Vector2>::Read pr = points.read();
  434. int len = points.size();
  435. for (int i = 0; i < len; ++i) {
  436. _draw_cell(p_cell, pr[i], p_flip_h, p_flip_v, p_transpose, p_xform);
  437. }
  438. }
  439. void TileMapEditor::_clear_bucket_cache() {
  440. if (bucket_cache_visited) {
  441. delete[] bucket_cache_visited;
  442. bucket_cache_visited = 0;
  443. }
  444. }
  445. void TileMapEditor::_update_copydata() {
  446. copydata.clear();
  447. if (!selection_active)
  448. return;
  449. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  450. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  451. TileData tcd;
  452. tcd.cell = node->get_cell(j, i);
  453. if (tcd.cell != TileMap::INVALID_CELL) {
  454. tcd.pos = Point2i(j, i);
  455. tcd.flip_h = node->is_cell_x_flipped(j, i);
  456. tcd.flip_v = node->is_cell_y_flipped(j, i);
  457. tcd.transpose = node->is_cell_transposed(j, i);
  458. }
  459. copydata.push_back(tcd);
  460. }
  461. }
  462. }
  463. static inline Vector<Point2i> line(int x0, int x1, int y0, int y1) {
  464. Vector<Point2i> points;
  465. float dx = ABS(x1 - x0);
  466. float dy = ABS(y1 - y0);
  467. int x = x0;
  468. int y = y0;
  469. int sx = x0 > x1 ? -1 : 1;
  470. int sy = y0 > y1 ? -1 : 1;
  471. if (dx > dy) {
  472. float err = dx / 2;
  473. for (; x != x1; x += sx) {
  474. points.push_back(Vector2(x, y));
  475. err -= dy;
  476. if (err < 0) {
  477. y += sy;
  478. err += dx;
  479. }
  480. }
  481. } else {
  482. float err = dy / 2;
  483. for (; y != y1; y += sy) {
  484. points.push_back(Vector2(x, y));
  485. err -= dx;
  486. if (err < 0) {
  487. x += sx;
  488. err += dy;
  489. }
  490. }
  491. }
  492. points.push_back(Vector2(x, y));
  493. return points;
  494. }
  495. bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
  496. if (!node || !node->get_tileset().is_valid() || !node->is_visible_in_tree())
  497. return false;
  498. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform();
  499. Transform2D xform_inv = xform.affine_inverse();
  500. Ref<InputEventMouseButton> mb = p_event;
  501. if (mb.is_valid()) {
  502. if (mb->get_button_index() == BUTTON_LEFT) {
  503. if (mb->is_pressed()) {
  504. if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
  505. return false; //drag
  506. if (tool == TOOL_NONE) {
  507. if (mb->get_shift()) {
  508. if (mb->get_control())
  509. tool = TOOL_RECTANGLE_PAINT;
  510. else
  511. tool = TOOL_LINE_PAINT;
  512. selection_active = false;
  513. rectangle_begin = over_tile;
  514. return true;
  515. }
  516. if (mb->get_control()) {
  517. tool = TOOL_PICKING;
  518. _pick_tile(over_tile);
  519. return true;
  520. }
  521. tool = TOOL_PAINTING;
  522. }
  523. if (tool == TOOL_PAINTING) {
  524. int id = get_selected_tile();
  525. if (id != TileMap::INVALID_CELL) {
  526. tool = TOOL_PAINTING;
  527. paint_undo.clear();
  528. paint_undo[over_tile] = _get_op_from_cell(over_tile);
  529. _set_cell(over_tile, id, flip_h, flip_v, transpose);
  530. }
  531. } else if (tool == TOOL_PICKING) {
  532. _pick_tile(over_tile);
  533. } else if (tool == TOOL_SELECTING) {
  534. selection_active = true;
  535. rectangle_begin = over_tile;
  536. }
  537. return true;
  538. } else {
  539. // Mousebutton was released
  540. if (tool != TOOL_NONE) {
  541. if (tool == TOOL_PAINTING) {
  542. int id = get_selected_tile();
  543. if (id != TileMap::INVALID_CELL && paint_undo.size()) {
  544. undo_redo->create_action(TTR("Paint TileMap"));
  545. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  546. Point2 p = E->key();
  547. undo_redo->add_do_method(node, "set_cellv", p, id, flip_h, flip_v, transpose);
  548. undo_redo->add_undo_method(node, "set_cellv", p, E->get().idx, E->get().xf, E->get().yf, E->get().tr);
  549. }
  550. undo_redo->commit_action();
  551. paint_undo.clear();
  552. }
  553. } else if (tool == TOOL_LINE_PAINT) {
  554. int id = get_selected_tile();
  555. if (id != TileMap::INVALID_CELL) {
  556. undo_redo->create_action(TTR("Line Draw"));
  557. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  558. _set_cell(E->key(), id, flip_h, flip_v, transpose, true);
  559. }
  560. undo_redo->commit_action();
  561. paint_undo.clear();
  562. canvas_item_editor->update();
  563. }
  564. } else if (tool == TOOL_RECTANGLE_PAINT) {
  565. int id = get_selected_tile();
  566. if (id != TileMap::INVALID_CELL) {
  567. undo_redo->create_action(TTR("Rectangle Paint"));
  568. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  569. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  570. _set_cell(Point2i(j, i), id, flip_h, flip_v, transpose, true);
  571. }
  572. }
  573. undo_redo->commit_action();
  574. canvas_item_editor->update();
  575. }
  576. } else if (tool == TOOL_DUPLICATING) {
  577. Point2 ofs = over_tile - rectangle.position;
  578. undo_redo->create_action(TTR("Duplicate"));
  579. for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) {
  580. _set_cell(E->get().pos + ofs, E->get().cell, E->get().flip_h, E->get().flip_v, E->get().transpose, true);
  581. }
  582. undo_redo->commit_action();
  583. copydata.clear();
  584. canvas_item_editor->update();
  585. } else if (tool == TOOL_SELECTING) {
  586. canvas_item_editor->update();
  587. } else if (tool == TOOL_BUCKET) {
  588. Dictionary pop;
  589. pop["id"] = node->get_cell(over_tile.x, over_tile.y);
  590. pop["flip_h"] = node->is_cell_x_flipped(over_tile.x, over_tile.y);
  591. pop["flip_v"] = node->is_cell_y_flipped(over_tile.x, over_tile.y);
  592. pop["transpose"] = node->is_cell_transposed(over_tile.x, over_tile.y);
  593. PoolVector<Vector2> points = _bucket_fill(over_tile);
  594. if (points.size() == 0)
  595. return false;
  596. Dictionary op;
  597. op["id"] = get_selected_tile();
  598. op["flip_h"] = flip_h;
  599. op["flip_v"] = flip_v;
  600. op["transpose"] = transpose;
  601. undo_redo->create_action(TTR("Bucket Fill"));
  602. undo_redo->add_do_method(this, "_fill_points", points, op);
  603. undo_redo->add_undo_method(this, "_fill_points", points, pop);
  604. undo_redo->commit_action();
  605. // We want to keep the bucket-tool active
  606. return true;
  607. }
  608. tool = TOOL_NONE;
  609. return true;
  610. }
  611. }
  612. } else if (mb->get_button_index() == BUTTON_RIGHT) {
  613. if (mb->is_pressed()) {
  614. if (tool == TOOL_SELECTING || selection_active) {
  615. tool = TOOL_NONE;
  616. selection_active = false;
  617. canvas_item_editor->update();
  618. return true;
  619. }
  620. if (tool == TOOL_DUPLICATING) {
  621. tool = TOOL_NONE;
  622. copydata.clear();
  623. canvas_item_editor->update();
  624. return true;
  625. }
  626. if (tool == TOOL_NONE) {
  627. paint_undo.clear();
  628. Point2 local = node->world_to_map(xform_inv.xform(mb->get_position()));
  629. if (mb->get_shift()) {
  630. if (mb->get_control())
  631. tool = TOOL_RECTANGLE_ERASE;
  632. else
  633. tool = TOOL_LINE_ERASE;
  634. selection_active = false;
  635. rectangle_begin = local;
  636. } else {
  637. tool = TOOL_ERASING;
  638. paint_undo[local] = _get_op_from_cell(local);
  639. _set_cell(local, TileMap::INVALID_CELL);
  640. }
  641. return true;
  642. }
  643. } else {
  644. if (tool == TOOL_ERASING || tool == TOOL_RECTANGLE_ERASE || tool == TOOL_LINE_ERASE) {
  645. if (paint_undo.size()) {
  646. undo_redo->create_action(TTR("Erase TileMap"));
  647. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  648. Point2 p = E->key();
  649. undo_redo->add_do_method(node, "set_cellv", p, TileMap::INVALID_CELL, false, false, false);
  650. undo_redo->add_undo_method(node, "set_cellv", p, E->get().idx, E->get().xf, E->get().yf, E->get().tr);
  651. }
  652. undo_redo->commit_action();
  653. paint_undo.clear();
  654. }
  655. if (tool == TOOL_RECTANGLE_ERASE || tool == TOOL_LINE_ERASE) {
  656. canvas_item_editor->update();
  657. }
  658. tool = TOOL_NONE;
  659. return true;
  660. } else if (tool == TOOL_BUCKET) {
  661. Dictionary pop;
  662. pop["id"] = node->get_cell(over_tile.x, over_tile.y);
  663. pop["flip_h"] = node->is_cell_x_flipped(over_tile.x, over_tile.y);
  664. pop["flip_v"] = node->is_cell_y_flipped(over_tile.x, over_tile.y);
  665. pop["transpose"] = node->is_cell_transposed(over_tile.x, over_tile.y);
  666. PoolVector<Vector2> points = _bucket_fill(over_tile, true);
  667. if (points.size() == 0)
  668. return false;
  669. undo_redo->create_action("Bucket Fill");
  670. undo_redo->add_do_method(this, "_erase_points", points);
  671. undo_redo->add_undo_method(this, "_fill_points", points, pop);
  672. undo_redo->commit_action();
  673. }
  674. }
  675. }
  676. }
  677. Ref<InputEventMouseMotion> mm = p_event;
  678. if (mm.is_valid()) {
  679. Point2i new_over_tile = node->world_to_map(xform_inv.xform(mm->get_position()));
  680. Point2i old_over_tile = over_tile;
  681. if (new_over_tile != over_tile) {
  682. over_tile = new_over_tile;
  683. canvas_item_editor->update();
  684. }
  685. if (show_tile_info) {
  686. int tile_under = node->get_cell(over_tile.x, over_tile.y);
  687. String tile_name = "none";
  688. if (node->get_tileset()->has_tile(tile_under))
  689. tile_name = node->get_tileset()->tile_get_name(tile_under);
  690. tile_info->set_text(String::num(over_tile.x) + ", " + String::num(over_tile.y) + " [" + tile_name + "]");
  691. }
  692. if (tool == TOOL_PAINTING) {
  693. // Paint using bresenham line to prevent holes in painting if the user moves fast
  694. Vector<Point2i> points = line(old_over_tile.x, over_tile.x, old_over_tile.y, over_tile.y);
  695. int id = get_selected_tile();
  696. for (int i = 0; i < points.size(); ++i) {
  697. Point2i pos = points[i];
  698. if (!paint_undo.has(pos)) {
  699. paint_undo[pos] = _get_op_from_cell(pos);
  700. }
  701. _set_cell(pos, id, flip_h, flip_v, transpose);
  702. }
  703. return true;
  704. }
  705. if (tool == TOOL_ERASING) {
  706. // erase using bresenham line to prevent holes in painting if the user moves fast
  707. Vector<Point2i> points = line(old_over_tile.x, over_tile.x, old_over_tile.y, over_tile.y);
  708. for (int i = 0; i < points.size(); ++i) {
  709. Point2i pos = points[i];
  710. if (!paint_undo.has(pos)) {
  711. paint_undo[pos] = _get_op_from_cell(pos);
  712. }
  713. _set_cell(pos, TileMap::INVALID_CELL);
  714. }
  715. return true;
  716. }
  717. if (tool == TOOL_SELECTING) {
  718. _select(rectangle_begin, over_tile);
  719. return true;
  720. }
  721. if (tool == TOOL_LINE_PAINT || tool == TOOL_LINE_ERASE) {
  722. int id = get_selected_tile();
  723. bool erasing = (tool == TOOL_LINE_ERASE);
  724. if (erasing && paint_undo.size()) {
  725. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  726. _set_cell(E->key(), E->get().idx, E->get().xf, E->get().yf, E->get().tr);
  727. }
  728. }
  729. paint_undo.clear();
  730. if (id != TileMap::INVALID_CELL) {
  731. Vector<Point2i> points = line(rectangle_begin.x, over_tile.x, rectangle_begin.y, over_tile.y);
  732. for (int i = 0; i < points.size(); i++) {
  733. paint_undo[points[i]] = _get_op_from_cell(points[i]);
  734. if (erasing)
  735. _set_cell(points[i], TileMap::INVALID_CELL);
  736. }
  737. canvas_item_editor->update();
  738. }
  739. return true;
  740. }
  741. if (tool == TOOL_RECTANGLE_PAINT || tool == TOOL_RECTANGLE_ERASE) {
  742. _select(rectangle_begin, over_tile);
  743. if (tool == TOOL_RECTANGLE_ERASE) {
  744. if (paint_undo.size()) {
  745. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  746. _set_cell(E->key(), E->get().idx, E->get().xf, E->get().yf, E->get().tr);
  747. }
  748. }
  749. paint_undo.clear();
  750. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  751. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  752. Point2i tile = Point2i(j, i);
  753. paint_undo[tile] = _get_op_from_cell(tile);
  754. _set_cell(tile, TileMap::INVALID_CELL);
  755. }
  756. }
  757. }
  758. return true;
  759. }
  760. if (tool == TOOL_PICKING && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  761. _pick_tile(over_tile);
  762. return true;
  763. }
  764. }
  765. Ref<InputEventKey> k = p_event;
  766. if (k.is_valid() && k->is_pressed()) {
  767. if (k->get_scancode() == KEY_ESCAPE) {
  768. if (tool == TOOL_DUPLICATING)
  769. copydata.clear();
  770. else if (tool == TOOL_SELECTING || selection_active)
  771. selection_active = false;
  772. tool = TOOL_NONE;
  773. canvas_item_editor->update();
  774. return true;
  775. }
  776. if (!mouse_over) {
  777. // Editor shortcuts should not fire if mouse not in viewport
  778. return false;
  779. }
  780. if (ED_IS_SHORTCUT("tile_map_editor/paint_tile", p_event)) {
  781. // NOTE: We do not set tool = TOOL_PAINTING as this begins painting
  782. // immediately without pressing the left mouse button first
  783. tool = TOOL_NONE;
  784. canvas_item_editor->update();
  785. return true;
  786. }
  787. if (ED_IS_SHORTCUT("tile_map_editor/bucket_fill", p_event)) {
  788. tool = TOOL_BUCKET;
  789. canvas_item_editor->update();
  790. return true;
  791. }
  792. if (ED_IS_SHORTCUT("tile_map_editor/erase_selection", p_event)) {
  793. _menu_option(OPTION_ERASE_SELECTION);
  794. return true;
  795. }
  796. if (ED_IS_SHORTCUT("tile_map_editor/select", p_event)) {
  797. tool = TOOL_SELECTING;
  798. selection_active = false;
  799. canvas_item_editor->update();
  800. return true;
  801. }
  802. if (ED_IS_SHORTCUT("tile_map_editor/duplicate_selection", p_event)) {
  803. _update_copydata();
  804. if (selection_active) {
  805. tool = TOOL_DUPLICATING;
  806. canvas_item_editor->update();
  807. return true;
  808. }
  809. }
  810. if (ED_IS_SHORTCUT("tile_map_editor/find_tile", p_event)) {
  811. search_box->select_all();
  812. search_box->grab_focus();
  813. return true;
  814. }
  815. if (ED_IS_SHORTCUT("tile_map_editor/mirror_x", p_event)) {
  816. flip_h = !flip_h;
  817. mirror_x->set_pressed(flip_h);
  818. canvas_item_editor->update();
  819. return true;
  820. }
  821. if (ED_IS_SHORTCUT("tile_map_editor/mirror_y", p_event)) {
  822. flip_v = !flip_v;
  823. mirror_y->set_pressed(flip_v);
  824. canvas_item_editor->update();
  825. return true;
  826. }
  827. if (ED_IS_SHORTCUT("tile_map_editor/transpose", p_event)) {
  828. transpose = !transpose;
  829. transp->set_pressed(transpose);
  830. canvas_item_editor->update();
  831. return true;
  832. }
  833. }
  834. return false;
  835. }
  836. void TileMapEditor::forward_draw_over_canvas(Control *p_canvas) {
  837. if (!node)
  838. return;
  839. Transform2D cell_xf = node->get_cell_transform();
  840. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform();
  841. Transform2D xform_inv = xform.affine_inverse();
  842. Size2 screen_size = canvas_item_editor->get_size();
  843. {
  844. Rect2 aabb;
  845. aabb.position = node->world_to_map(xform_inv.xform(Vector2()));
  846. aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(0, screen_size.height))));
  847. aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(screen_size.width, 0))));
  848. aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size)));
  849. Rect2i si = aabb.grow(1.0);
  850. if (node->get_half_offset() != TileMap::HALF_OFFSET_X) {
  851. int max_lines = 2000; //avoid crash if size too smal
  852. for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
  853. Vector2 from = xform.xform(node->map_to_world(Vector2(i, si.position.y)));
  854. Vector2 to = xform.xform(node->map_to_world(Vector2(i, si.position.y + si.size.y + 1)));
  855. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  856. canvas_item_editor->draw_line(from, to, col, 1);
  857. if (max_lines-- == 0)
  858. break;
  859. }
  860. } else {
  861. int max_lines = 10000; //avoid crash if size too smal
  862. for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
  863. for (int j = (si.position.y) - 1; j <= (si.position.y + si.size.y); j++) {
  864. Vector2 ofs;
  865. if (ABS(j) & 1) {
  866. ofs = cell_xf[0] * 0.5;
  867. }
  868. Vector2 from = xform.xform(node->map_to_world(Vector2(i, j), true) + ofs);
  869. Vector2 to = xform.xform(node->map_to_world(Vector2(i, j + 1), true) + ofs);
  870. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  871. canvas_item_editor->draw_line(from, to, col, 1);
  872. if (max_lines-- == 0)
  873. break;
  874. }
  875. }
  876. }
  877. int max_lines = 10000; //avoid crash if size too smal
  878. if (node->get_half_offset() != TileMap::HALF_OFFSET_Y) {
  879. for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) {
  880. Vector2 from = xform.xform(node->map_to_world(Vector2(si.position.x, i)));
  881. Vector2 to = xform.xform(node->map_to_world(Vector2(si.position.x + si.size.x + 1, i)));
  882. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  883. canvas_item_editor->draw_line(from, to, col, 1);
  884. if (max_lines-- == 0)
  885. break;
  886. }
  887. } else {
  888. for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) {
  889. for (int j = (si.position.x) - 1; j <= (si.position.x + si.size.x); j++) {
  890. Vector2 ofs;
  891. if (ABS(j) & 1) {
  892. ofs = cell_xf[1] * 0.5;
  893. }
  894. Vector2 from = xform.xform(node->map_to_world(Vector2(j, i), true) + ofs);
  895. Vector2 to = xform.xform(node->map_to_world(Vector2(j + 1, i), true) + ofs);
  896. Color col = i == 0 ? Color(1, 0.8, 0.2, 0.5) : Color(1, 0.3, 0.1, 0.2);
  897. canvas_item_editor->draw_line(from, to, col, 1);
  898. if (max_lines-- == 0)
  899. break;
  900. }
  901. }
  902. }
  903. }
  904. if (selection_active) {
  905. Vector<Vector2> points;
  906. points.push_back(xform.xform(node->map_to_world((rectangle.position))));
  907. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, 0)))));
  908. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(rectangle.size.x + 1, rectangle.size.y + 1)))));
  909. points.push_back(xform.xform(node->map_to_world((rectangle.position + Point2(0, rectangle.size.y + 1)))));
  910. canvas_item_editor->draw_colored_polygon(points, Color(0.2, 0.8, 1, 0.4));
  911. }
  912. if (mouse_over) {
  913. Vector2 endpoints[4] = {
  914. node->map_to_world(over_tile, true),
  915. node->map_to_world((over_tile + Point2(1, 0)), true),
  916. node->map_to_world((over_tile + Point2(1, 1)), true),
  917. node->map_to_world((over_tile + Point2(0, 1)), true)
  918. };
  919. for (int i = 0; i < 4; i++) {
  920. if (node->get_half_offset() == TileMap::HALF_OFFSET_X && ABS(over_tile.y) & 1)
  921. endpoints[i] += cell_xf[0] * 0.5;
  922. if (node->get_half_offset() == TileMap::HALF_OFFSET_Y && ABS(over_tile.x) & 1)
  923. endpoints[i] += cell_xf[1] * 0.5;
  924. endpoints[i] = xform.xform(endpoints[i]);
  925. }
  926. Color col;
  927. if (node->get_cell(over_tile.x, over_tile.y) != TileMap::INVALID_CELL)
  928. col = Color(0.2, 0.8, 1.0, 0.8);
  929. else
  930. col = Color(1.0, 0.4, 0.2, 0.8);
  931. for (int i = 0; i < 4; i++)
  932. canvas_item_editor->draw_line(endpoints[i], endpoints[(i + 1) % 4], col, 2);
  933. bool bucket_preview = EditorSettings::get_singleton()->get("editors/tile_map/bucket_fill_preview");
  934. if (tool == TOOL_SELECTING || tool == TOOL_PICKING || !bucket_preview) {
  935. return;
  936. }
  937. if (tool == TOOL_LINE_PAINT) {
  938. if (paint_undo.empty())
  939. return;
  940. int id = get_selected_tile();
  941. if (id == TileMap::INVALID_CELL)
  942. return;
  943. for (Map<Point2i, CellOp>::Element *E = paint_undo.front(); E; E = E->next()) {
  944. _draw_cell(id, E->key(), flip_h, flip_v, transpose, xform);
  945. }
  946. } else if (tool == TOOL_RECTANGLE_PAINT) {
  947. int id = get_selected_tile();
  948. if (id == TileMap::INVALID_CELL)
  949. return;
  950. for (int i = rectangle.position.y; i <= rectangle.position.y + rectangle.size.y; i++) {
  951. for (int j = rectangle.position.x; j <= rectangle.position.x + rectangle.size.x; j++) {
  952. _draw_cell(id, Point2i(j, i), flip_h, flip_v, transpose, xform);
  953. }
  954. }
  955. } else if (tool == TOOL_DUPLICATING) {
  956. if (copydata.empty())
  957. return;
  958. Ref<TileSet> ts = node->get_tileset();
  959. if (ts.is_null())
  960. return;
  961. Point2 ofs = over_tile - rectangle.position;
  962. for (List<TileData>::Element *E = copydata.front(); E; E = E->next()) {
  963. if (!ts->has_tile(E->get().cell))
  964. continue;
  965. TileData tcd = E->get();
  966. _draw_cell(tcd.cell, tcd.pos + ofs, tcd.flip_h, tcd.flip_v, tcd.transpose, xform);
  967. }
  968. Rect2i duplicate = rectangle;
  969. duplicate.position = over_tile;
  970. Vector<Vector2> points;
  971. points.push_back(xform.xform(node->map_to_world(duplicate.position)));
  972. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, 0)))));
  973. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(duplicate.size.x + 1, duplicate.size.y + 1)))));
  974. points.push_back(xform.xform(node->map_to_world((duplicate.position + Point2(0, duplicate.size.y + 1)))));
  975. canvas_item_editor->draw_colored_polygon(points, Color(0.2, 1.0, 0.8, 0.2));
  976. } else if (tool == TOOL_BUCKET) {
  977. int tile = get_selected_tile();
  978. _draw_fill_preview(tile, over_tile, flip_h, flip_v, transpose, xform);
  979. } else {
  980. int st = get_selected_tile();
  981. if (st == TileMap::INVALID_CELL)
  982. return;
  983. _draw_cell(st, over_tile, flip_h, flip_v, transpose, xform);
  984. }
  985. }
  986. }
  987. void TileMapEditor::edit(Node *p_tile_map) {
  988. search_box->set_text("");
  989. if (!canvas_item_editor) {
  990. canvas_item_editor = CanvasItemEditor::get_singleton()->get_viewport_control();
  991. }
  992. if (node)
  993. node->disconnect("settings_changed", this, "_tileset_settings_changed");
  994. if (p_tile_map) {
  995. node = Object::cast_to<TileMap>(p_tile_map);
  996. if (!canvas_item_editor->is_connected("mouse_entered", this, "_canvas_mouse_enter"))
  997. canvas_item_editor->connect("mouse_entered", this, "_canvas_mouse_enter");
  998. if (!canvas_item_editor->is_connected("mouse_exited", this, "_canvas_mouse_exit"))
  999. canvas_item_editor->connect("mouse_exited", this, "_canvas_mouse_exit");
  1000. _update_palette();
  1001. } else {
  1002. node = NULL;
  1003. if (canvas_item_editor->is_connected("mouse_entered", this, "_canvas_mouse_enter"))
  1004. canvas_item_editor->disconnect("mouse_entered", this, "_canvas_mouse_enter");
  1005. if (canvas_item_editor->is_connected("mouse_exited", this, "_canvas_mouse_exit"))
  1006. canvas_item_editor->disconnect("mouse_exited", this, "_canvas_mouse_exit");
  1007. _update_palette();
  1008. }
  1009. if (node)
  1010. node->connect("settings_changed", this, "_tileset_settings_changed");
  1011. _clear_bucket_cache();
  1012. }
  1013. void TileMapEditor::_tileset_settings_changed() {
  1014. _update_palette();
  1015. if (canvas_item_editor)
  1016. canvas_item_editor->update();
  1017. }
  1018. void TileMapEditor::_icon_size_changed(float p_value) {
  1019. if (node) {
  1020. palette->set_icon_scale(p_value);
  1021. _update_palette();
  1022. }
  1023. }
  1024. void TileMapEditor::_bind_methods() {
  1025. ClassDB::bind_method(D_METHOD("_text_entered"), &TileMapEditor::_text_entered);
  1026. ClassDB::bind_method(D_METHOD("_text_changed"), &TileMapEditor::_text_changed);
  1027. ClassDB::bind_method(D_METHOD("_sbox_input"), &TileMapEditor::_sbox_input);
  1028. ClassDB::bind_method(D_METHOD("_menu_option"), &TileMapEditor::_menu_option);
  1029. ClassDB::bind_method(D_METHOD("_canvas_mouse_enter"), &TileMapEditor::_canvas_mouse_enter);
  1030. ClassDB::bind_method(D_METHOD("_canvas_mouse_exit"), &TileMapEditor::_canvas_mouse_exit);
  1031. ClassDB::bind_method(D_METHOD("_tileset_settings_changed"), &TileMapEditor::_tileset_settings_changed);
  1032. ClassDB::bind_method(D_METHOD("_update_transform_buttons"), &TileMapEditor::_update_transform_buttons);
  1033. ClassDB::bind_method(D_METHOD("_fill_points"), &TileMapEditor::_fill_points);
  1034. ClassDB::bind_method(D_METHOD("_erase_points"), &TileMapEditor::_erase_points);
  1035. ClassDB::bind_method(D_METHOD("_icon_size_changed"), &TileMapEditor::_icon_size_changed);
  1036. }
  1037. TileMapEditor::CellOp TileMapEditor::_get_op_from_cell(const Point2i &p_pos) {
  1038. CellOp op;
  1039. op.idx = node->get_cell(p_pos.x, p_pos.y);
  1040. if (op.idx != TileMap::INVALID_CELL) {
  1041. if (node->is_cell_x_flipped(p_pos.x, p_pos.y))
  1042. op.xf = true;
  1043. if (node->is_cell_y_flipped(p_pos.x, p_pos.y))
  1044. op.yf = true;
  1045. if (node->is_cell_transposed(p_pos.x, p_pos.y))
  1046. op.tr = true;
  1047. }
  1048. return op;
  1049. }
  1050. void TileMapEditor::_update_transform_buttons(Object *p_button) {
  1051. //ERR_FAIL_NULL(p_button);
  1052. ToolButton *b = Object::cast_to<ToolButton>(p_button);
  1053. //ERR_FAIL_COND(!b);
  1054. if (b == rotate_0) {
  1055. mirror_x->set_pressed(false);
  1056. mirror_y->set_pressed(false);
  1057. transp->set_pressed(false);
  1058. } else if (b == rotate_90) {
  1059. mirror_x->set_pressed(true);
  1060. mirror_y->set_pressed(false);
  1061. transp->set_pressed(true);
  1062. } else if (b == rotate_180) {
  1063. mirror_x->set_pressed(true);
  1064. mirror_y->set_pressed(true);
  1065. transp->set_pressed(false);
  1066. } else if (b == rotate_270) {
  1067. mirror_x->set_pressed(false);
  1068. mirror_y->set_pressed(true);
  1069. transp->set_pressed(true);
  1070. }
  1071. flip_h = mirror_x->is_pressed();
  1072. flip_v = mirror_y->is_pressed();
  1073. transpose = transp->is_pressed();
  1074. rotate_0->set_pressed(!flip_h && !flip_v && !transpose);
  1075. rotate_90->set_pressed(flip_h && !flip_v && transpose);
  1076. rotate_180->set_pressed(flip_h && flip_v && !transpose);
  1077. rotate_270->set_pressed(!flip_h && flip_v && transpose);
  1078. }
  1079. TileMapEditor::TileMapEditor(EditorNode *p_editor) {
  1080. node = NULL;
  1081. canvas_item_editor = NULL;
  1082. editor = p_editor;
  1083. undo_redo = editor->get_undo_redo();
  1084. tool = TOOL_NONE;
  1085. selection_active = false;
  1086. mouse_over = false;
  1087. show_tile_info = true;
  1088. flip_h = false;
  1089. flip_v = false;
  1090. transpose = false;
  1091. bucket_cache_tile = -1;
  1092. bucket_cache_visited = 0;
  1093. ED_SHORTCUT("tile_map_editor/erase_selection", TTR("Erase selection"), KEY_DELETE);
  1094. ED_SHORTCUT("tile_map_editor/find_tile", TTR("Find tile"), KEY_MASK_CMD + KEY_F);
  1095. ED_SHORTCUT("tile_map_editor/transpose", TTR("Transpose"), KEY_T);
  1096. ED_SHORTCUT("tile_map_editor/mirror_x", TTR("Mirror X"), KEY_A);
  1097. ED_SHORTCUT("tile_map_editor/mirror_y", TTR("Mirror Y"), KEY_S);
  1098. HBoxContainer *tool_hb1 = memnew(HBoxContainer);
  1099. add_child(tool_hb1);
  1100. HBoxContainer *tool_hb2 = memnew(HBoxContainer);
  1101. add_child(tool_hb2);
  1102. search_box = memnew(LineEdit);
  1103. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1104. search_box->connect("text_entered", this, "_text_entered");
  1105. search_box->connect("text_changed", this, "_text_changed");
  1106. search_box->connect("gui_input", this, "_sbox_input");
  1107. add_child(search_box);
  1108. size_slider = memnew(HSlider);
  1109. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1110. size_slider->set_min(0.1f);
  1111. size_slider->set_max(4.0f);
  1112. size_slider->set_step(0.1f);
  1113. size_slider->set_value(1.0f);
  1114. size_slider->connect("value_changed", this, "_icon_size_changed");
  1115. add_child(size_slider);
  1116. int mw = EDITOR_DEF("editors/tile_map/palette_min_width", 80);
  1117. // Add tile palette
  1118. palette = memnew(ItemList);
  1119. palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1120. palette->set_custom_minimum_size(Size2(mw, 0));
  1121. palette->set_max_columns(0);
  1122. palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  1123. palette->set_max_text_lines(2);
  1124. add_child(palette);
  1125. // Add menu items
  1126. toolbar = memnew(HBoxContainer);
  1127. toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  1128. toolbar->set_alignment(BoxContainer::ALIGN_END);
  1129. toolbar->hide();
  1130. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(toolbar);
  1131. // Tile position
  1132. tile_info = memnew(Label);
  1133. toolbar->add_child(tile_info);
  1134. options = memnew(MenuButton);
  1135. options->set_text("Tile Map");
  1136. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("TileMap", "EditorIcons"));
  1137. options->set_process_unhandled_key_input(false);
  1138. PopupMenu *p = options->get_popup();
  1139. p->add_shortcut(ED_SHORTCUT("tile_map_editor/paint_tile", TTR("Paint Tile"), KEY_P), OPTION_PAINTING);
  1140. p->add_shortcut(ED_SHORTCUT("tile_map_editor/bucket_fill", TTR("Bucket Fill"), KEY_G), OPTION_BUCKET);
  1141. p->add_separator();
  1142. p->add_item(TTR("Pick Tile"), OPTION_PICK_TILE, KEY_CONTROL);
  1143. p->add_separator();
  1144. p->add_shortcut(ED_SHORTCUT("tile_map_editor/select", TTR("Select"), KEY_MASK_CMD + KEY_B), OPTION_SELECT);
  1145. p->add_shortcut(ED_SHORTCUT("tile_map_editor/duplicate_selection", TTR("Duplicate Selection"), KEY_MASK_CMD + KEY_D), OPTION_DUPLICATE);
  1146. p->add_shortcut(ED_GET_SHORTCUT("tile_map_editor/erase_selection"), OPTION_ERASE_SELECTION);
  1147. p->connect("id_pressed", this, "_menu_option");
  1148. toolbar->add_child(options);
  1149. transp = memnew(ToolButton);
  1150. transp->set_toggle_mode(true);
  1151. transp->set_tooltip(TTR("Transpose") + " (" + ED_GET_SHORTCUT("tile_map_editor/transpose")->get_as_text() + ")");
  1152. transp->set_focus_mode(FOCUS_NONE);
  1153. transp->connect("pressed", this, "_update_transform_buttons", make_binds(transp));
  1154. tool_hb1->add_child(transp);
  1155. mirror_x = memnew(ToolButton);
  1156. mirror_x->set_toggle_mode(true);
  1157. mirror_x->set_tooltip(TTR("Mirror X") + " (" + ED_GET_SHORTCUT("tile_map_editor/mirror_x")->get_as_text() + ")");
  1158. mirror_x->set_focus_mode(FOCUS_NONE);
  1159. mirror_x->connect("pressed", this, "_update_transform_buttons", make_binds(mirror_x));
  1160. tool_hb1->add_child(mirror_x);
  1161. mirror_y = memnew(ToolButton);
  1162. mirror_y->set_toggle_mode(true);
  1163. mirror_y->set_tooltip(TTR("Mirror Y") + " (" + ED_GET_SHORTCUT("tile_map_editor/mirror_y")->get_as_text() + ")");
  1164. mirror_y->set_focus_mode(FOCUS_NONE);
  1165. mirror_y->connect("pressed", this, "_update_transform_buttons", make_binds(mirror_y));
  1166. tool_hb1->add_child(mirror_y);
  1167. rotate_0 = memnew(ToolButton);
  1168. rotate_0->set_toggle_mode(true);
  1169. rotate_0->set_tooltip(TTR("Rotate 0 degrees"));
  1170. rotate_0->set_focus_mode(FOCUS_NONE);
  1171. rotate_0->connect("pressed", this, "_update_transform_buttons", make_binds(rotate_0));
  1172. tool_hb2->add_child(rotate_0);
  1173. rotate_90 = memnew(ToolButton);
  1174. rotate_90->set_toggle_mode(true);
  1175. rotate_90->set_tooltip(TTR("Rotate 90 degrees"));
  1176. rotate_90->set_focus_mode(FOCUS_NONE);
  1177. rotate_90->connect("pressed", this, "_update_transform_buttons", make_binds(rotate_90));
  1178. tool_hb2->add_child(rotate_90);
  1179. rotate_180 = memnew(ToolButton);
  1180. rotate_180->set_toggle_mode(true);
  1181. rotate_180->set_tooltip(TTR("Rotate 180 degrees"));
  1182. rotate_180->set_focus_mode(FOCUS_NONE);
  1183. rotate_180->connect("pressed", this, "_update_transform_buttons", make_binds(rotate_180));
  1184. tool_hb2->add_child(rotate_180);
  1185. rotate_270 = memnew(ToolButton);
  1186. rotate_270->set_toggle_mode(true);
  1187. rotate_270->set_tooltip(TTR("Rotate 270 degrees"));
  1188. rotate_270->set_focus_mode(FOCUS_NONE);
  1189. rotate_270->connect("pressed", this, "_update_transform_buttons", make_binds(rotate_270));
  1190. tool_hb2->add_child(rotate_270);
  1191. rotate_0->set_pressed(true);
  1192. }
  1193. TileMapEditor::~TileMapEditor() {
  1194. _clear_bucket_cache();
  1195. }
  1196. ///////////////////////////////////////////////////////////////
  1197. ///////////////////////////////////////////////////////////////
  1198. ///////////////////////////////////////////////////////////////
  1199. void TileMapEditorPlugin::edit(Object *p_object) {
  1200. tile_map_editor->edit(Object::cast_to<Node>(p_object));
  1201. }
  1202. bool TileMapEditorPlugin::handles(Object *p_object) const {
  1203. return p_object->is_class("TileMap");
  1204. }
  1205. void TileMapEditorPlugin::make_visible(bool p_visible) {
  1206. if (p_visible) {
  1207. tile_map_editor->show();
  1208. tile_map_editor->get_toolbar()->show();
  1209. } else {
  1210. tile_map_editor->hide();
  1211. tile_map_editor->get_toolbar()->hide();
  1212. tile_map_editor->edit(NULL);
  1213. }
  1214. }
  1215. TileMapEditorPlugin::TileMapEditorPlugin(EditorNode *p_node) {
  1216. EDITOR_DEF("editors/tile_map/preview_size", 64);
  1217. EDITOR_DEF("editors/tile_map/palette_item_hseparation", 8);
  1218. EDITOR_DEF("editors/tile_map/show_tile_names", true);
  1219. EDITOR_DEF("editors/tile_map/show_tile_ids", false);
  1220. EDITOR_DEF("editors/tile_map/sort_tiles_by_name", true);
  1221. EDITOR_DEF("editors/tile_map/bucket_fill_preview", true);
  1222. EDITOR_DEF("editors/tile_map/show_tile_info_on_hover", true);
  1223. tile_map_editor = memnew(TileMapEditor(p_node));
  1224. add_control_to_container(CONTAINER_CANVAS_EDITOR_SIDE, tile_map_editor);
  1225. tile_map_editor->hide();
  1226. tile_map_editor->set_process(true);
  1227. }
  1228. TileMapEditorPlugin::~TileMapEditorPlugin() {
  1229. }