tile_map_editor_plugin.cpp 49 KB

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