2
0

tile_map_editor_plugin.cpp 67 KB

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