tile_set_editor_plugin.cpp 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700
  1. /**************************************************************************/
  2. /* tile_set_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_set_editor_plugin.h"
  31. #include "core/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/plugins/canvas_item_editor_plugin.h"
  35. #include "scene/2d/physics_body_2d.h"
  36. #include "scene/2d/sprite.h"
  37. void TileSetEditor::edit(const Ref<TileSet> &p_tileset) {
  38. tileset = p_tileset;
  39. tileset->add_change_receptor(this);
  40. texture_list->clear();
  41. texture_map.clear();
  42. update_texture_list();
  43. }
  44. void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {
  45. for (int i = 0; i < p_node->get_child_count(); i++) {
  46. Node *child = p_node->get_child(i);
  47. if (!Object::cast_to<Sprite>(child)) {
  48. if (child->get_child_count() > 0) {
  49. _import_node(child, p_library);
  50. }
  51. continue;
  52. }
  53. Sprite *mi = Object::cast_to<Sprite>(child);
  54. Ref<Texture> texture = mi->get_texture();
  55. Ref<Texture> normal_map = mi->get_normal_map();
  56. Ref<ShaderMaterial> material = mi->get_material();
  57. if (texture.is_null()) {
  58. continue;
  59. }
  60. int id = p_library->find_tile_by_name(mi->get_name());
  61. if (id < 0) {
  62. id = p_library->get_last_unused_tile_id();
  63. p_library->create_tile(id);
  64. p_library->tile_set_name(id, mi->get_name());
  65. }
  66. p_library->tile_set_texture(id, texture);
  67. p_library->tile_set_normal_map(id, normal_map);
  68. p_library->tile_set_material(id, material);
  69. p_library->tile_set_modulate(id, mi->get_modulate());
  70. Vector2 phys_offset;
  71. Size2 s;
  72. if (mi->is_region()) {
  73. s = mi->get_region_rect().size;
  74. p_library->tile_set_region(id, mi->get_region_rect());
  75. } else {
  76. const int frame = mi->get_frame();
  77. const int hframes = mi->get_hframes();
  78. s = texture->get_size() / Size2(hframes, mi->get_vframes());
  79. p_library->tile_set_region(id, Rect2(Vector2(frame % hframes, frame / hframes) * s, s));
  80. }
  81. if (mi->is_centered()) {
  82. phys_offset += -s / 2;
  83. }
  84. Vector<TileSet::ShapeData> collisions;
  85. Ref<NavigationPolygon> nav_poly;
  86. Ref<OccluderPolygon2D> occluder;
  87. bool found_collisions = false;
  88. for (int j = 0; j < mi->get_child_count(); j++) {
  89. Node *child2 = mi->get_child(j);
  90. if (Object::cast_to<NavigationPolygonInstance>(child2)) {
  91. nav_poly = Object::cast_to<NavigationPolygonInstance>(child2)->get_navigation_polygon();
  92. }
  93. if (Object::cast_to<LightOccluder2D>(child2)) {
  94. occluder = Object::cast_to<LightOccluder2D>(child2)->get_occluder_polygon();
  95. }
  96. if (!Object::cast_to<StaticBody2D>(child2)) {
  97. continue;
  98. }
  99. found_collisions = true;
  100. StaticBody2D *sb = Object::cast_to<StaticBody2D>(child2);
  101. List<uint32_t> shapes;
  102. sb->get_shape_owners(&shapes);
  103. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  104. if (sb->is_shape_owner_disabled(E->get())) {
  105. continue;
  106. }
  107. Transform2D shape_transform = sb->get_transform() * sb->shape_owner_get_transform(E->get());
  108. bool one_way = sb->is_shape_owner_one_way_collision_enabled(E->get());
  109. shape_transform[2] -= phys_offset;
  110. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  111. Ref<Shape2D> shape = sb->shape_owner_get_shape(E->get(), k);
  112. TileSet::ShapeData shape_data;
  113. shape_data.shape = shape;
  114. shape_data.shape_transform = shape_transform;
  115. shape_data.one_way_collision = one_way;
  116. collisions.push_back(shape_data);
  117. }
  118. }
  119. }
  120. if (found_collisions) {
  121. p_library->tile_set_shapes(id, collisions);
  122. }
  123. p_library->tile_set_texture_offset(id, mi->get_offset());
  124. p_library->tile_set_navigation_polygon(id, nav_poly);
  125. p_library->tile_set_light_occluder(id, occluder);
  126. p_library->tile_set_occluder_offset(id, -phys_offset);
  127. p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
  128. p_library->tile_set_z_index(id, mi->get_z_index());
  129. }
  130. }
  131. void TileSetEditor::_import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge) {
  132. if (!p_merge) {
  133. p_library->clear();
  134. }
  135. _import_node(p_scene, p_library);
  136. }
  137. void TileSetEditor::_undo_redo_import_scene(Node *p_scene, bool p_merge) {
  138. _import_scene(p_scene, tileset, p_merge);
  139. }
  140. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge) {
  141. _import_scene(p_base_scene, ml, p_merge);
  142. return OK;
  143. }
  144. Variant TileSetEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  145. return false;
  146. }
  147. bool TileSetEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  148. Dictionary d = p_data;
  149. if (!d.has("type")) {
  150. return false;
  151. }
  152. if (d.has("from") && (Object *)(d["from"]) == texture_list) {
  153. return false;
  154. }
  155. if (String(d["type"]) == "resource" && d.has("resource")) {
  156. RES r = d["resource"];
  157. Ref<Texture> texture = r;
  158. if (texture.is_valid()) {
  159. return true;
  160. }
  161. }
  162. if (String(d["type"]) == "files") {
  163. Vector<String> files = d["files"];
  164. if (files.size() == 0) {
  165. return false;
  166. }
  167. for (int i = 0; i < files.size(); i++) {
  168. String file = files[i];
  169. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  170. if (!ClassDB::is_parent_class(ftype, "Texture")) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. return false;
  177. }
  178. void TileSetEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  179. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  180. return;
  181. }
  182. Dictionary d = p_data;
  183. if (!d.has("type")) {
  184. return;
  185. }
  186. if (String(d["type"]) == "resource" && d.has("resource")) {
  187. RES r = d["resource"];
  188. Ref<Texture> texture = r;
  189. if (texture.is_valid()) {
  190. add_texture(texture);
  191. }
  192. if (texture_list->get_item_count() > 0) {
  193. update_texture_list_icon();
  194. texture_list->select(texture_list->get_item_count() - 1);
  195. _on_texture_list_selected(texture_list->get_item_count() - 1);
  196. }
  197. }
  198. if (String(d["type"]) == "files") {
  199. PoolVector<String> files = d["files"];
  200. _on_textures_added(files);
  201. }
  202. }
  203. void TileSetEditor::_bind_methods() {
  204. ClassDB::bind_method("_undo_redo_import_scene", &TileSetEditor::_undo_redo_import_scene);
  205. ClassDB::bind_method("_on_tileset_toolbar_button_pressed", &TileSetEditor::_on_tileset_toolbar_button_pressed);
  206. ClassDB::bind_method("_on_textures_added", &TileSetEditor::_on_textures_added);
  207. ClassDB::bind_method("_on_tileset_toolbar_confirm", &TileSetEditor::_on_tileset_toolbar_confirm);
  208. ClassDB::bind_method("_on_texture_list_selected", &TileSetEditor::_on_texture_list_selected);
  209. ClassDB::bind_method("_on_edit_mode_changed", &TileSetEditor::_on_edit_mode_changed);
  210. ClassDB::bind_method("_on_scroll_container_input", &TileSetEditor::_on_scroll_container_input);
  211. ClassDB::bind_method("_on_workspace_mode_changed", &TileSetEditor::_on_workspace_mode_changed);
  212. ClassDB::bind_method("_on_workspace_overlay_draw", &TileSetEditor::_on_workspace_overlay_draw);
  213. ClassDB::bind_method("_on_workspace_process", &TileSetEditor::_on_workspace_process);
  214. ClassDB::bind_method("_on_workspace_draw", &TileSetEditor::_on_workspace_draw);
  215. ClassDB::bind_method("_on_workspace_input", &TileSetEditor::_on_workspace_input);
  216. ClassDB::bind_method("_on_tool_clicked", &TileSetEditor::_on_tool_clicked);
  217. ClassDB::bind_method("_on_priority_changed", &TileSetEditor::_on_priority_changed);
  218. ClassDB::bind_method("_on_z_index_changed", &TileSetEditor::_on_z_index_changed);
  219. ClassDB::bind_method("_on_grid_snap_toggled", &TileSetEditor::_on_grid_snap_toggled);
  220. ClassDB::bind_method("_set_snap_step", &TileSetEditor::_set_snap_step);
  221. ClassDB::bind_method("_set_snap_off", &TileSetEditor::_set_snap_off);
  222. ClassDB::bind_method("_set_snap_sep", &TileSetEditor::_set_snap_sep);
  223. ClassDB::bind_method("_validate_current_tile_id", &TileSetEditor::_validate_current_tile_id);
  224. ClassDB::bind_method("_zoom_in", &TileSetEditor::_zoom_in);
  225. ClassDB::bind_method("_zoom_out", &TileSetEditor::_zoom_out);
  226. ClassDB::bind_method("_zoom_reset", &TileSetEditor::_zoom_reset);
  227. ClassDB::bind_method("_select_edited_shape_coord", &TileSetEditor::_select_edited_shape_coord);
  228. ClassDB::bind_method("_sort_tiles", &TileSetEditor::_sort_tiles);
  229. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &TileSetEditor::get_drag_data_fw);
  230. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &TileSetEditor::can_drop_data_fw);
  231. ClassDB::bind_method(D_METHOD("drop_data_fw"), &TileSetEditor::drop_data_fw);
  232. ClassDB::bind_method("edit", &TileSetEditor::edit);
  233. ClassDB::bind_method("add_texture", &TileSetEditor::add_texture);
  234. ClassDB::bind_method("remove_texture", &TileSetEditor::remove_texture);
  235. ClassDB::bind_method("update_texture_list_icon", &TileSetEditor::update_texture_list_icon);
  236. ClassDB::bind_method("update_workspace_minsize", &TileSetEditor::update_workspace_minsize);
  237. }
  238. void TileSetEditor::_notification(int p_what) {
  239. switch (p_what) {
  240. case NOTIFICATION_READY: {
  241. add_constant_override("autohide", 1); // Fixes the dragger always showing up.
  242. } break;
  243. case NOTIFICATION_ENTER_TREE:
  244. case NOTIFICATION_THEME_CHANGED: {
  245. tileset_toolbar_buttons[TOOL_TILESET_ADD_TEXTURE]->set_icon(get_icon("ToolAddNode", "EditorIcons"));
  246. tileset_toolbar_buttons[TOOL_TILESET_REMOVE_TEXTURE]->set_icon(get_icon("Remove", "EditorIcons"));
  247. tileset_toolbar_tools->set_icon(get_icon("Tools", "EditorIcons"));
  248. tool_workspacemode[WORKSPACE_EDIT]->set_icon(get_icon("Edit", "EditorIcons"));
  249. tool_workspacemode[WORKSPACE_CREATE_SINGLE]->set_icon(get_icon("AddSingleTile", "EditorIcons"));
  250. tool_workspacemode[WORKSPACE_CREATE_AUTOTILE]->set_icon(get_icon("AddAutotile", "EditorIcons"));
  251. tool_workspacemode[WORKSPACE_CREATE_ATLAS]->set_icon(get_icon("AddAtlasTile", "EditorIcons"));
  252. tools[TOOL_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  253. tools[BITMASK_COPY]->set_icon(get_icon("ActionCopy", "EditorIcons"));
  254. tools[BITMASK_PASTE]->set_icon(get_icon("ActionPaste", "EditorIcons"));
  255. tools[BITMASK_CLEAR]->set_icon(get_icon("Clear", "EditorIcons"));
  256. tools[SHAPE_NEW_POLYGON]->set_icon(get_icon("CollisionPolygon2D", "EditorIcons"));
  257. tools[SHAPE_NEW_RECTANGLE]->set_icon(get_icon("CollisionShape2D", "EditorIcons"));
  258. tools[SELECT_PREVIOUS]->set_icon(get_icon("ArrowLeft", "EditorIcons"));
  259. tools[SELECT_NEXT]->set_icon(get_icon("ArrowRight", "EditorIcons"));
  260. tools[SHAPE_DELETE]->set_icon(get_icon("Remove", "EditorIcons"));
  261. tools[SHAPE_KEEP_INSIDE_TILE]->set_icon(get_icon("Snap", "EditorIcons"));
  262. tools[TOOL_GRID_SNAP]->set_icon(get_icon("SnapGrid", "EditorIcons"));
  263. tools[ZOOM_OUT]->set_icon(get_icon("ZoomLess", "EditorIcons"));
  264. tools[ZOOM_1]->set_icon(get_icon("ZoomReset", "EditorIcons"));
  265. tools[ZOOM_IN]->set_icon(get_icon("ZoomMore", "EditorIcons"));
  266. tools[VISIBLE_INFO]->set_icon(get_icon("InformationSign", "EditorIcons"));
  267. _update_toggle_shape_button();
  268. tool_editmode[EDITMODE_REGION]->set_icon(get_icon("RegionEdit", "EditorIcons"));
  269. tool_editmode[EDITMODE_COLLISION]->set_icon(get_icon("StaticBody2D", "EditorIcons"));
  270. tool_editmode[EDITMODE_OCCLUSION]->set_icon(get_icon("LightOccluder2D", "EditorIcons"));
  271. tool_editmode[EDITMODE_NAVIGATION]->set_icon(get_icon("Navigation2D", "EditorIcons"));
  272. tool_editmode[EDITMODE_BITMASK]->set_icon(get_icon("PackedDataContainer", "EditorIcons"));
  273. tool_editmode[EDITMODE_PRIORITY]->set_icon(get_icon("MaterialPreviewLight1", "EditorIcons"));
  274. tool_editmode[EDITMODE_ICON]->set_icon(get_icon("LargeTexture", "EditorIcons"));
  275. tool_editmode[EDITMODE_Z_INDEX]->set_icon(get_icon("Sort", "EditorIcons"));
  276. scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
  277. } break;
  278. }
  279. }
  280. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  281. editor = p_editor;
  282. undo_redo = EditorNode::get_undo_redo();
  283. current_tile = -1;
  284. VBoxContainer *left_container = memnew(VBoxContainer);
  285. add_child(left_container);
  286. texture_list = memnew(ItemList);
  287. left_container->add_child(texture_list);
  288. texture_list->set_v_size_flags(SIZE_EXPAND_FILL);
  289. texture_list->set_custom_minimum_size(Size2(200, 0));
  290. texture_list->connect("item_selected", this, "_on_texture_list_selected");
  291. texture_list->set_drag_forwarding(this);
  292. HBoxContainer *tileset_toolbar_container = memnew(HBoxContainer);
  293. left_container->add_child(tileset_toolbar_container);
  294. tileset_toolbar_buttons[TOOL_TILESET_ADD_TEXTURE] = memnew(ToolButton);
  295. tileset_toolbar_buttons[TOOL_TILESET_ADD_TEXTURE]->connect("pressed", this, "_on_tileset_toolbar_button_pressed", varray(TOOL_TILESET_ADD_TEXTURE));
  296. tileset_toolbar_container->add_child(tileset_toolbar_buttons[TOOL_TILESET_ADD_TEXTURE]);
  297. tileset_toolbar_buttons[TOOL_TILESET_ADD_TEXTURE]->set_tooltip(TTR("Add Texture(s) to TileSet."));
  298. tileset_toolbar_buttons[TOOL_TILESET_REMOVE_TEXTURE] = memnew(ToolButton);
  299. tileset_toolbar_buttons[TOOL_TILESET_REMOVE_TEXTURE]->connect("pressed", this, "_on_tileset_toolbar_button_pressed", varray(TOOL_TILESET_REMOVE_TEXTURE));
  300. tileset_toolbar_container->add_child(tileset_toolbar_buttons[TOOL_TILESET_REMOVE_TEXTURE]);
  301. tileset_toolbar_buttons[TOOL_TILESET_REMOVE_TEXTURE]->set_tooltip(TTR("Remove selected Texture from TileSet."));
  302. Control *toolbar_separator = memnew(Control);
  303. toolbar_separator->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  304. tileset_toolbar_container->add_child(toolbar_separator);
  305. tileset_toolbar_tools = memnew(MenuButton);
  306. tileset_toolbar_tools->set_text(TTR("Tools"));
  307. tileset_toolbar_tools->get_popup()->add_item(TTR("Create from Scene"), TOOL_TILESET_CREATE_SCENE);
  308. tileset_toolbar_tools->get_popup()->add_item(TTR("Merge from Scene"), TOOL_TILESET_MERGE_SCENE);
  309. tileset_toolbar_tools->get_popup()->connect("id_pressed", this, "_on_tileset_toolbar_button_pressed");
  310. tileset_toolbar_container->add_child(tileset_toolbar_tools);
  311. //---------------
  312. VBoxContainer *right_container = memnew(VBoxContainer);
  313. right_container->set_v_size_flags(SIZE_EXPAND_FILL);
  314. add_child(right_container);
  315. dragging_point = -1;
  316. creating_shape = false;
  317. snap_step = Vector2(32, 32);
  318. snap_offset = WORKSPACE_MARGIN;
  319. set_custom_minimum_size(Size2(0, 150));
  320. VBoxContainer *main_vb = memnew(VBoxContainer);
  321. right_container->add_child(main_vb);
  322. main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  323. HBoxContainer *tool_hb = memnew(HBoxContainer);
  324. Ref<ButtonGroup> g(memnew(ButtonGroup));
  325. String workspace_label[WORKSPACE_MODE_MAX] = {
  326. TTR("Edit"),
  327. TTR("New Single Tile"),
  328. TTR("New Autotile"),
  329. TTR("New Atlas")
  330. };
  331. for (int i = 0; i < (int)WORKSPACE_MODE_MAX; i++) {
  332. tool_workspacemode[i] = memnew(Button);
  333. tool_workspacemode[i]->set_text(workspace_label[i]);
  334. tool_workspacemode[i]->set_toggle_mode(true);
  335. tool_workspacemode[i]->set_button_group(g);
  336. tool_workspacemode[i]->connect("pressed", this, "_on_workspace_mode_changed", varray(i));
  337. tool_hb->add_child(tool_workspacemode[i]);
  338. }
  339. Control *spacer = memnew(Control);
  340. spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  341. tool_hb->add_child(spacer);
  342. tool_hb->move_child(spacer, WORKSPACE_CREATE_SINGLE);
  343. tools[SELECT_NEXT] = memnew(ToolButton);
  344. tool_hb->add_child(tools[SELECT_NEXT]);
  345. tool_hb->move_child(tools[SELECT_NEXT], WORKSPACE_CREATE_SINGLE);
  346. tools[SELECT_NEXT]->set_shortcut(ED_SHORTCUT("tileset_editor/next_shape", TTR("Next Coordinate"), KEY_PAGEDOWN));
  347. tools[SELECT_NEXT]->connect("pressed", this, "_on_tool_clicked", varray(SELECT_NEXT));
  348. tools[SELECT_NEXT]->set_tooltip(TTR("Select the next shape, subtile, or Tile."));
  349. tools[SELECT_PREVIOUS] = memnew(ToolButton);
  350. tool_hb->add_child(tools[SELECT_PREVIOUS]);
  351. tool_hb->move_child(tools[SELECT_PREVIOUS], WORKSPACE_CREATE_SINGLE);
  352. tools[SELECT_PREVIOUS]->set_shortcut(ED_SHORTCUT("tileset_editor/previous_shape", TTR("Previous Coordinate"), KEY_PAGEUP));
  353. tools[SELECT_PREVIOUS]->set_tooltip(TTR("Select the previous shape, subtile, or Tile."));
  354. tools[SELECT_PREVIOUS]->connect("pressed", this, "_on_tool_clicked", varray(SELECT_PREVIOUS));
  355. VSeparator *separator_shape_selection = memnew(VSeparator);
  356. tool_hb->add_child(separator_shape_selection);
  357. tool_hb->move_child(separator_shape_selection, WORKSPACE_CREATE_SINGLE);
  358. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  359. workspace_mode = WORKSPACE_EDIT;
  360. main_vb->add_child(tool_hb);
  361. main_vb->add_child(memnew(HSeparator));
  362. tool_hb = memnew(HBoxContainer);
  363. g = Ref<ButtonGroup>(memnew(ButtonGroup));
  364. String label[EDITMODE_MAX] = {
  365. TTR("Region"),
  366. TTR("Collision"),
  367. TTR("Occlusion"),
  368. TTR("Navigation"),
  369. TTR("Bitmask"),
  370. TTR("Priority"),
  371. TTR("Icon"),
  372. TTR("Z Index")
  373. };
  374. for (int i = 0; i < (int)EDITMODE_MAX; i++) {
  375. tool_editmode[i] = memnew(Button);
  376. tool_editmode[i]->set_text(label[i]);
  377. tool_editmode[i]->set_toggle_mode(true);
  378. tool_editmode[i]->set_button_group(g);
  379. tool_editmode[i]->connect("pressed", this, "_on_edit_mode_changed", varray(i));
  380. tool_hb->add_child(tool_editmode[i]);
  381. }
  382. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  383. edit_mode = EDITMODE_COLLISION;
  384. tool_editmode[EDITMODE_REGION]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_region", TTR("Region Mode"), KEY_1));
  385. tool_editmode[EDITMODE_COLLISION]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_collision", TTR("Collision Mode"), KEY_2));
  386. tool_editmode[EDITMODE_OCCLUSION]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_occlusion", TTR("Occlusion Mode"), KEY_3));
  387. tool_editmode[EDITMODE_NAVIGATION]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_navigation", TTR("Navigation Mode"), KEY_4));
  388. tool_editmode[EDITMODE_BITMASK]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_bitmask", TTR("Bitmask Mode"), KEY_5));
  389. tool_editmode[EDITMODE_PRIORITY]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_priority", TTR("Priority Mode"), KEY_6));
  390. tool_editmode[EDITMODE_ICON]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_icon", TTR("Icon Mode"), KEY_7));
  391. tool_editmode[EDITMODE_Z_INDEX]->set_shortcut(ED_SHORTCUT("tileset_editor/editmode_z_index", TTR("Z Index Mode"), KEY_8));
  392. main_vb->add_child(tool_hb);
  393. separator_editmode = memnew(HSeparator);
  394. main_vb->add_child(separator_editmode);
  395. toolbar = memnew(HBoxContainer);
  396. Ref<ButtonGroup> tg(memnew(ButtonGroup));
  397. tools[TOOL_SELECT] = memnew(ToolButton);
  398. toolbar->add_child(tools[TOOL_SELECT]);
  399. tools[TOOL_SELECT]->set_toggle_mode(true);
  400. tools[TOOL_SELECT]->set_button_group(tg);
  401. tools[TOOL_SELECT]->set_pressed(true);
  402. tools[TOOL_SELECT]->connect("pressed", this, "_on_tool_clicked", varray(TOOL_SELECT));
  403. separator_bitmask = memnew(VSeparator);
  404. toolbar->add_child(separator_bitmask);
  405. tools[BITMASK_COPY] = memnew(ToolButton);
  406. tools[BITMASK_COPY]->set_tooltip(TTR("Copy bitmask."));
  407. tools[BITMASK_COPY]->connect("pressed", this, "_on_tool_clicked", varray(BITMASK_COPY));
  408. toolbar->add_child(tools[BITMASK_COPY]);
  409. tools[BITMASK_PASTE] = memnew(ToolButton);
  410. tools[BITMASK_PASTE]->set_tooltip(TTR("Paste bitmask."));
  411. tools[BITMASK_PASTE]->connect("pressed", this, "_on_tool_clicked", varray(BITMASK_PASTE));
  412. toolbar->add_child(tools[BITMASK_PASTE]);
  413. tools[BITMASK_CLEAR] = memnew(ToolButton);
  414. tools[BITMASK_CLEAR]->set_tooltip(TTR("Erase bitmask."));
  415. tools[BITMASK_CLEAR]->connect("pressed", this, "_on_tool_clicked", varray(BITMASK_CLEAR));
  416. toolbar->add_child(tools[BITMASK_CLEAR]);
  417. tools[SHAPE_NEW_RECTANGLE] = memnew(ToolButton);
  418. toolbar->add_child(tools[SHAPE_NEW_RECTANGLE]);
  419. tools[SHAPE_NEW_RECTANGLE]->set_toggle_mode(true);
  420. tools[SHAPE_NEW_RECTANGLE]->set_button_group(tg);
  421. tools[SHAPE_NEW_RECTANGLE]->set_tooltip(TTR("Create a new rectangle."));
  422. tools[SHAPE_NEW_RECTANGLE]->connect("pressed", this, "_on_tool_clicked", varray(SHAPE_NEW_RECTANGLE));
  423. tools[SHAPE_NEW_RECTANGLE]->set_shortcut(ED_SHORTCUT("tileset_editor/shape_new_rectangle", TTR("New Rectangle"), KEY_MASK_SHIFT | KEY_R));
  424. tools[SHAPE_NEW_POLYGON] = memnew(ToolButton);
  425. toolbar->add_child(tools[SHAPE_NEW_POLYGON]);
  426. tools[SHAPE_NEW_POLYGON]->set_toggle_mode(true);
  427. tools[SHAPE_NEW_POLYGON]->set_button_group(tg);
  428. tools[SHAPE_NEW_POLYGON]->set_tooltip(TTR("Create a new polygon."));
  429. tools[SHAPE_NEW_POLYGON]->connect("pressed", this, "_on_tool_clicked", varray(SHAPE_NEW_POLYGON));
  430. tools[SHAPE_NEW_POLYGON]->set_shortcut(ED_SHORTCUT("tileset_editor/shape_new_polygon", TTR("New Polygon"), KEY_MASK_SHIFT | KEY_P));
  431. separator_shape_toggle = memnew(VSeparator);
  432. toolbar->add_child(separator_shape_toggle);
  433. tools[SHAPE_TOGGLE_TYPE] = memnew(ToolButton);
  434. tools[SHAPE_TOGGLE_TYPE]->connect("pressed", this, "_on_tool_clicked", varray(SHAPE_TOGGLE_TYPE));
  435. toolbar->add_child(tools[SHAPE_TOGGLE_TYPE]);
  436. separator_delete = memnew(VSeparator);
  437. toolbar->add_child(separator_delete);
  438. tools[SHAPE_DELETE] = memnew(ToolButton);
  439. tools[SHAPE_DELETE]->connect("pressed", this, "_on_tool_clicked", varray(SHAPE_DELETE));
  440. tools[SHAPE_DELETE]->set_shortcut(ED_SHORTCUT("tileset_editor/shape_delete", TTR("Delete Selected Shape"), KEY_MASK_SHIFT | KEY_BACKSPACE));
  441. toolbar->add_child(tools[SHAPE_DELETE]);
  442. spin_priority = memnew(SpinBox);
  443. spin_priority->set_min(1);
  444. spin_priority->set_max(255);
  445. spin_priority->set_step(1);
  446. spin_priority->set_custom_minimum_size(Size2(100, 0));
  447. spin_priority->connect("value_changed", this, "_on_priority_changed");
  448. spin_priority->hide();
  449. toolbar->add_child(spin_priority);
  450. spin_z_index = memnew(SpinBox);
  451. spin_z_index->set_min(VS::CANVAS_ITEM_Z_MIN);
  452. spin_z_index->set_max(VS::CANVAS_ITEM_Z_MAX);
  453. spin_z_index->set_step(1);
  454. spin_z_index->set_custom_minimum_size(Size2(100, 0));
  455. spin_z_index->connect("value_changed", this, "_on_z_index_changed");
  456. spin_z_index->hide();
  457. toolbar->add_child(spin_z_index);
  458. separator_grid = memnew(VSeparator);
  459. toolbar->add_child(separator_grid);
  460. tools[SHAPE_KEEP_INSIDE_TILE] = memnew(ToolButton);
  461. tools[SHAPE_KEEP_INSIDE_TILE]->set_toggle_mode(true);
  462. tools[SHAPE_KEEP_INSIDE_TILE]->set_pressed(true);
  463. tools[SHAPE_KEEP_INSIDE_TILE]->set_tooltip(TTR("Keep polygon inside region Rect."));
  464. toolbar->add_child(tools[SHAPE_KEEP_INSIDE_TILE]);
  465. tools[TOOL_GRID_SNAP] = memnew(ToolButton);
  466. tools[TOOL_GRID_SNAP]->set_toggle_mode(true);
  467. tools[TOOL_GRID_SNAP]->set_tooltip(TTR("Enable snap and show grid (configurable via the Inspector)."));
  468. tools[TOOL_GRID_SNAP]->connect("toggled", this, "_on_grid_snap_toggled");
  469. toolbar->add_child(tools[TOOL_GRID_SNAP]);
  470. Control *separator = memnew(Control);
  471. separator->set_h_size_flags(SIZE_EXPAND_FILL);
  472. toolbar->add_child(separator);
  473. tools[ZOOM_OUT] = memnew(ToolButton);
  474. tools[ZOOM_OUT]->connect("pressed", this, "_zoom_out");
  475. toolbar->add_child(tools[ZOOM_OUT]);
  476. tools[ZOOM_OUT]->set_tooltip(TTR("Zoom Out"));
  477. tools[ZOOM_1] = memnew(ToolButton);
  478. tools[ZOOM_1]->connect("pressed", this, "_zoom_reset");
  479. toolbar->add_child(tools[ZOOM_1]);
  480. tools[ZOOM_1]->set_tooltip(TTR("Zoom Reset"));
  481. tools[ZOOM_IN] = memnew(ToolButton);
  482. tools[ZOOM_IN]->connect("pressed", this, "_zoom_in");
  483. toolbar->add_child(tools[ZOOM_IN]);
  484. tools[ZOOM_IN]->set_tooltip(TTR("Zoom In"));
  485. tools[VISIBLE_INFO] = memnew(ToolButton);
  486. tools[VISIBLE_INFO]->set_toggle_mode(true);
  487. tools[VISIBLE_INFO]->set_tooltip(TTR("Display Tile Names (Hold Alt Key)"));
  488. toolbar->add_child(tools[VISIBLE_INFO]);
  489. main_vb->add_child(toolbar);
  490. scroll = memnew(ScrollContainer);
  491. main_vb->add_child(scroll);
  492. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  493. scroll->connect("gui_input", this, "_on_scroll_container_input");
  494. scroll->set_clip_contents(true);
  495. empty_message = memnew(Label);
  496. empty_message->set_text(TTR("Add or select a texture on the left panel to edit the tiles bound to it."));
  497. empty_message->set_valign(Label::VALIGN_CENTER);
  498. empty_message->set_align(Label::ALIGN_CENTER);
  499. empty_message->set_autowrap(true);
  500. empty_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  501. empty_message->set_v_size_flags(SIZE_EXPAND_FILL);
  502. main_vb->add_child(empty_message);
  503. workspace_container = memnew(Control);
  504. scroll->add_child(workspace_container);
  505. workspace_overlay = memnew(Control);
  506. workspace_overlay->connect("draw", this, "_on_workspace_overlay_draw");
  507. workspace_container->add_child(workspace_overlay);
  508. workspace = memnew(Control);
  509. workspace->set_focus_mode(FOCUS_ALL);
  510. workspace->connect("draw", this, "_on_workspace_draw");
  511. workspace->connect("gui_input", this, "_on_workspace_input");
  512. workspace->set_draw_behind_parent(true);
  513. workspace_overlay->add_child(workspace);
  514. preview = memnew(Sprite);
  515. workspace->add_child(preview);
  516. preview->set_centered(false);
  517. preview->set_draw_behind_parent(true);
  518. preview->set_position(WORKSPACE_MARGIN);
  519. //---------------
  520. cd = memnew(ConfirmationDialog);
  521. add_child(cd);
  522. cd->connect("confirmed", this, "_on_tileset_toolbar_confirm");
  523. //---------------
  524. err_dialog = memnew(AcceptDialog);
  525. add_child(err_dialog);
  526. //---------------
  527. texture_dialog = memnew(EditorFileDialog);
  528. texture_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  529. texture_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  530. texture_dialog->clear_filters();
  531. List<String> extensions;
  532. ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
  533. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  534. texture_dialog->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  535. }
  536. add_child(texture_dialog);
  537. texture_dialog->connect("files_selected", this, "_on_textures_added");
  538. //---------------
  539. helper = memnew(TilesetEditorContext(this));
  540. tile_names_visible = false;
  541. // Config scale.
  542. max_scale = 16.0f;
  543. min_scale = 0.01f;
  544. scale_ratio = 1.2f;
  545. }
  546. TileSetEditor::~TileSetEditor() {
  547. if (helper) {
  548. memdelete(helper);
  549. }
  550. }
  551. void TileSetEditor::_on_tileset_toolbar_button_pressed(int p_index) {
  552. option = p_index;
  553. switch (option) {
  554. case TOOL_TILESET_ADD_TEXTURE: {
  555. texture_dialog->popup_centered_ratio();
  556. } break;
  557. case TOOL_TILESET_REMOVE_TEXTURE: {
  558. if (get_current_texture().is_valid()) {
  559. cd->set_text(TTR("Remove selected texture? This will remove all tiles which use it."));
  560. cd->popup_centered(Size2(300, 60));
  561. } else {
  562. err_dialog->set_text(TTR("You haven't selected a texture to remove."));
  563. err_dialog->popup_centered(Size2(300, 60));
  564. }
  565. } break;
  566. case TOOL_TILESET_CREATE_SCENE: {
  567. cd->set_text(TTR("Create from scene? This will overwrite all current tiles."));
  568. cd->popup_centered(Size2(300, 60));
  569. } break;
  570. case TOOL_TILESET_MERGE_SCENE: {
  571. cd->set_text(TTR("Merge from scene?"));
  572. cd->popup_centered(Size2(300, 60));
  573. } break;
  574. }
  575. }
  576. void TileSetEditor::_on_tileset_toolbar_confirm() {
  577. switch (option) {
  578. case TOOL_TILESET_REMOVE_TEXTURE: {
  579. String current_texture_path = get_current_texture()->get_path();
  580. List<int> ids;
  581. tileset->get_tile_list(&ids);
  582. undo_redo->create_action(TTR("Remove Texture"));
  583. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  584. if (tileset->tile_get_texture(E->get())->get_path() == current_texture_path) {
  585. undo_redo->add_do_method(tileset.ptr(), "remove_tile", E->get());
  586. _undo_tile_removal(E->get());
  587. }
  588. }
  589. undo_redo->add_do_method(this, "remove_texture", get_current_texture());
  590. undo_redo->add_undo_method(this, "add_texture", get_current_texture());
  591. undo_redo->add_undo_method(this, "update_texture_list_icon");
  592. undo_redo->commit_action();
  593. } break;
  594. case TOOL_TILESET_MERGE_SCENE:
  595. case TOOL_TILESET_CREATE_SCENE: {
  596. EditorNode *en = editor;
  597. Node *scene = en->get_edited_scene();
  598. if (!scene) {
  599. break;
  600. }
  601. List<int> ids;
  602. tileset->get_tile_list(&ids);
  603. undo_redo->create_action(TTR(option == TOOL_TILESET_MERGE_SCENE ? "Merge Tileset from Scene" : "Create Tileset from Scene"));
  604. undo_redo->add_do_method(this, "_undo_redo_import_scene", scene, option == TOOL_TILESET_MERGE_SCENE);
  605. undo_redo->add_undo_method(tileset.ptr(), "clear");
  606. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  607. _undo_tile_removal(E->get());
  608. }
  609. undo_redo->add_do_method(this, "edit", tileset);
  610. undo_redo->add_undo_method(this, "edit", tileset);
  611. undo_redo->commit_action();
  612. } break;
  613. }
  614. }
  615. void TileSetEditor::_on_texture_list_selected(int p_index) {
  616. if (get_current_texture().is_valid()) {
  617. current_item_index = p_index;
  618. preview->set_texture(get_current_texture());
  619. update_workspace_tile_mode();
  620. update_workspace_minsize();
  621. } else {
  622. current_item_index = -1;
  623. preview->set_texture(nullptr);
  624. workspace->set_custom_minimum_size(Size2i());
  625. update_workspace_tile_mode();
  626. }
  627. set_current_tile(-1);
  628. workspace->update();
  629. }
  630. void TileSetEditor::_on_textures_added(const PoolStringArray &p_paths) {
  631. int invalid_count = 0;
  632. for (int i = 0; i < p_paths.size(); i++) {
  633. Ref<Texture> t = Ref<Texture>(ResourceLoader::load(p_paths[i]));
  634. ERR_CONTINUE_MSG(!t.is_valid(), "'" + p_paths[i] + "' is not a valid texture.");
  635. if (texture_map.has(t->get_path())) {
  636. invalid_count++;
  637. } else {
  638. add_texture(t);
  639. }
  640. }
  641. if (texture_list->get_item_count() > 0) {
  642. update_texture_list_icon();
  643. texture_list->select(texture_list->get_item_count() - 1);
  644. _on_texture_list_selected(texture_list->get_item_count() - 1);
  645. }
  646. if (invalid_count > 0) {
  647. err_dialog->set_text(vformat(TTR("%s file(s) were not added because was already on the list."), String::num(invalid_count, 0)));
  648. err_dialog->popup_centered(Size2(300, 60));
  649. }
  650. }
  651. void TileSetEditor::_on_edit_mode_changed(int p_edit_mode) {
  652. draw_handles = false;
  653. creating_shape = false;
  654. edit_mode = (EditMode)p_edit_mode;
  655. switch (edit_mode) {
  656. case EDITMODE_REGION: {
  657. tools[TOOL_SELECT]->show();
  658. separator_bitmask->hide();
  659. tools[BITMASK_COPY]->hide();
  660. tools[BITMASK_PASTE]->hide();
  661. tools[BITMASK_CLEAR]->hide();
  662. tools[SHAPE_NEW_POLYGON]->hide();
  663. tools[SHAPE_NEW_RECTANGLE]->hide();
  664. if (workspace_mode == WORKSPACE_EDIT) {
  665. separator_delete->show();
  666. tools[SHAPE_DELETE]->show();
  667. } else {
  668. separator_delete->hide();
  669. tools[SHAPE_DELETE]->hide();
  670. }
  671. separator_grid->show();
  672. tools[SHAPE_KEEP_INSIDE_TILE]->hide();
  673. tools[TOOL_GRID_SNAP]->show();
  674. tools[TOOL_SELECT]->set_pressed(true);
  675. tools[TOOL_SELECT]->set_tooltip(TTR("Drag handles to edit Rect.\nClick on another Tile to edit it."));
  676. tools[SHAPE_DELETE]->set_tooltip(TTR("Delete selected Rect."));
  677. spin_priority->hide();
  678. spin_z_index->hide();
  679. } break;
  680. case EDITMODE_COLLISION:
  681. case EDITMODE_OCCLUSION:
  682. case EDITMODE_NAVIGATION: {
  683. tools[TOOL_SELECT]->show();
  684. separator_bitmask->hide();
  685. tools[BITMASK_COPY]->hide();
  686. tools[BITMASK_PASTE]->hide();
  687. tools[BITMASK_CLEAR]->hide();
  688. tools[SHAPE_NEW_POLYGON]->show();
  689. tools[SHAPE_NEW_RECTANGLE]->show();
  690. separator_delete->show();
  691. tools[SHAPE_DELETE]->show();
  692. separator_grid->show();
  693. tools[SHAPE_KEEP_INSIDE_TILE]->show();
  694. tools[TOOL_GRID_SNAP]->show();
  695. tools[TOOL_SELECT]->set_tooltip(TTR("Select current edited sub-tile.\nClick on another Tile to edit it."));
  696. tools[SHAPE_DELETE]->set_tooltip(TTR("Delete polygon."));
  697. spin_priority->hide();
  698. spin_z_index->hide();
  699. _select_edited_shape_coord();
  700. } break;
  701. case EDITMODE_BITMASK: {
  702. tools[TOOL_SELECT]->show();
  703. separator_bitmask->show();
  704. tools[BITMASK_COPY]->show();
  705. tools[BITMASK_PASTE]->show();
  706. tools[BITMASK_CLEAR]->show();
  707. tools[SHAPE_NEW_POLYGON]->hide();
  708. tools[SHAPE_NEW_RECTANGLE]->hide();
  709. separator_delete->hide();
  710. tools[SHAPE_DELETE]->hide();
  711. tools[SHAPE_KEEP_INSIDE_TILE]->hide();
  712. tools[TOOL_SELECT]->set_pressed(true);
  713. tools[TOOL_SELECT]->set_tooltip(TTR("LMB: Set bit on.\nRMB: Set bit off.\nShift+LMB: Set wildcard bit.\nClick on another Tile to edit it."));
  714. spin_priority->hide();
  715. } break;
  716. case EDITMODE_Z_INDEX:
  717. case EDITMODE_PRIORITY:
  718. case EDITMODE_ICON: {
  719. tools[TOOL_SELECT]->show();
  720. separator_bitmask->hide();
  721. tools[BITMASK_COPY]->hide();
  722. tools[BITMASK_PASTE]->hide();
  723. tools[BITMASK_CLEAR]->hide();
  724. tools[SHAPE_NEW_POLYGON]->hide();
  725. tools[SHAPE_NEW_RECTANGLE]->hide();
  726. separator_delete->hide();
  727. tools[SHAPE_DELETE]->hide();
  728. separator_grid->show();
  729. tools[SHAPE_KEEP_INSIDE_TILE]->hide();
  730. tools[TOOL_GRID_SNAP]->show();
  731. if (edit_mode == EDITMODE_ICON) {
  732. tools[TOOL_SELECT]->set_tooltip(TTR("Select sub-tile to use as icon, this will be also used on invalid autotile bindings.\nClick on another Tile to edit it."));
  733. spin_priority->hide();
  734. spin_z_index->hide();
  735. } else if (edit_mode == EDITMODE_PRIORITY) {
  736. tools[TOOL_SELECT]->set_tooltip(TTR("Select sub-tile to change its priority.\nClick on another Tile to edit it."));
  737. spin_priority->show();
  738. spin_z_index->hide();
  739. } else {
  740. tools[TOOL_SELECT]->set_tooltip(TTR("Select sub-tile to change its z index.\nClick on another Tile to edit it."));
  741. spin_priority->hide();
  742. spin_z_index->show();
  743. }
  744. } break;
  745. default: {
  746. }
  747. }
  748. _update_toggle_shape_button();
  749. workspace->update();
  750. }
  751. void TileSetEditor::_on_workspace_mode_changed(int p_workspace_mode) {
  752. workspace_mode = (WorkspaceMode)p_workspace_mode;
  753. if (p_workspace_mode == WORKSPACE_EDIT) {
  754. update_workspace_tile_mode();
  755. } else {
  756. for (int i = 0; i < EDITMODE_MAX; i++) {
  757. tool_editmode[i]->hide();
  758. }
  759. tool_editmode[EDITMODE_REGION]->show();
  760. tool_editmode[EDITMODE_REGION]->set_pressed(true);
  761. _on_edit_mode_changed(EDITMODE_REGION);
  762. separator_editmode->show();
  763. }
  764. }
  765. void TileSetEditor::_on_workspace_draw() {
  766. if (tileset.is_null() || !get_current_texture().is_valid()) {
  767. return;
  768. }
  769. const Color COLOR_AUTOTILE = Color(0.3, 0.6, 1);
  770. const Color COLOR_SINGLE = Color(1, 1, 0.3);
  771. const Color COLOR_ATLAS = Color(0.8, 0.8, 0.8);
  772. const Color COLOR_SUBDIVISION = Color(0.3, 0.7, 0.6);
  773. draw_handles = false;
  774. draw_highlight_current_tile();
  775. draw_grid_snap();
  776. if (get_current_tile() >= 0) {
  777. int spacing = tileset->autotile_get_spacing(get_current_tile());
  778. Vector2 size = tileset->autotile_get_size(get_current_tile());
  779. Rect2i region = tileset->tile_get_region(get_current_tile());
  780. switch (edit_mode) {
  781. case EDITMODE_ICON: {
  782. Vector2 coord = tileset->autotile_get_icon_coordinate(get_current_tile());
  783. draw_highlight_subtile(coord);
  784. } break;
  785. case EDITMODE_BITMASK: {
  786. Color c(1, 0, 0, 0.5);
  787. Color ci(0.3, 0.6, 1, 0.5);
  788. for (int x = 0; x < region.size.x / (spacing + size.x); x++) {
  789. for (int y = 0; y < region.size.y / (spacing + size.y); y++) {
  790. Vector2 coord(x, y);
  791. Point2 anchor(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  792. anchor += WORKSPACE_MARGIN;
  793. anchor += region.position;
  794. uint32_t mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  795. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  796. if (mask & TileSet::BIND_IGNORE_TOPLEFT) {
  797. workspace->draw_rect(Rect2(anchor, size / 4), ci);
  798. workspace->draw_rect(Rect2(anchor + size / 4, size / 4), ci);
  799. } else if (mask & TileSet::BIND_TOPLEFT) {
  800. workspace->draw_rect(Rect2(anchor, size / 2), c);
  801. }
  802. if (mask & TileSet::BIND_IGNORE_TOPRIGHT) {
  803. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, 0), size / 4), ci);
  804. workspace->draw_rect(Rect2(anchor + Vector2(size.x * 3 / 4, size.y / 4), size / 4), ci);
  805. } else if (mask & TileSet::BIND_TOPRIGHT) {
  806. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, 0), size / 2), c);
  807. }
  808. if (mask & TileSet::BIND_IGNORE_BOTTOMLEFT) {
  809. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 2), size / 4), ci);
  810. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 4, size.y * 3 / 4), size / 4), ci);
  811. } else if (mask & TileSet::BIND_BOTTOMLEFT) {
  812. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 2), size / 2), c);
  813. }
  814. if (mask & TileSet::BIND_IGNORE_BOTTOMRIGHT) {
  815. workspace->draw_rect(Rect2(anchor + size / 2, size / 4), ci);
  816. workspace->draw_rect(Rect2(anchor + size * 3 / 4, size / 4), ci);
  817. } else if (mask & TileSet::BIND_BOTTOMRIGHT) {
  818. workspace->draw_rect(Rect2(anchor + size / 2, size / 2), c);
  819. }
  820. } else {
  821. if (mask & TileSet::BIND_IGNORE_TOPLEFT) {
  822. workspace->draw_rect(Rect2(anchor, size / 6), ci);
  823. workspace->draw_rect(Rect2(anchor + size / 6, size / 6), ci);
  824. } else if (mask & TileSet::BIND_TOPLEFT) {
  825. workspace->draw_rect(Rect2(anchor, size / 3), c);
  826. }
  827. if (mask & TileSet::BIND_IGNORE_TOP) {
  828. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, 0), size / 6), ci);
  829. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, size.y / 6), size / 6), ci);
  830. } else if (mask & TileSet::BIND_TOP) {
  831. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, 0), size / 3), c);
  832. }
  833. if (mask & TileSet::BIND_IGNORE_TOPRIGHT) {
  834. workspace->draw_rect(Rect2(anchor + Vector2(size.x * 4 / 6, 0), size / 6), ci);
  835. workspace->draw_rect(Rect2(anchor + Vector2(size.x * 5 / 6, size.y / 6), size / 6), ci);
  836. } else if (mask & TileSet::BIND_TOPRIGHT) {
  837. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, 0), size / 3), c);
  838. }
  839. if (mask & TileSet::BIND_IGNORE_LEFT) {
  840. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 3), size / 6), ci);
  841. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 6, size.y / 2), size / 6), ci);
  842. } else if (mask & TileSet::BIND_LEFT) {
  843. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 3), size / 3), c);
  844. }
  845. if (mask & TileSet::BIND_IGNORE_CENTER) {
  846. workspace->draw_rect(Rect2(anchor + size / 3, size / 6), ci);
  847. workspace->draw_rect(Rect2(anchor + size / 2, size / 6), ci);
  848. } else if (mask & TileSet::BIND_CENTER) {
  849. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, size.y / 3), size / 3), c);
  850. }
  851. if (mask & TileSet::BIND_IGNORE_RIGHT) {
  852. workspace->draw_rect(Rect2(anchor + Vector2(size.x * 4 / 6, size.y / 3), size / 6), ci);
  853. workspace->draw_rect(Rect2(anchor + Vector2(size.x * 5 / 6, size.y / 2), size / 6), ci);
  854. } else if (mask & TileSet::BIND_RIGHT) {
  855. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, size.y / 3), size / 3), c);
  856. }
  857. if (mask & TileSet::BIND_IGNORE_BOTTOMLEFT) {
  858. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y * 4 / 6), size / 6), ci);
  859. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 6, size.y * 5 / 6), size / 6), ci);
  860. } else if (mask & TileSet::BIND_BOTTOMLEFT) {
  861. workspace->draw_rect(Rect2(anchor + Vector2(0, (size.y / 3) * 2), size / 3), c);
  862. }
  863. if (mask & TileSet::BIND_IGNORE_BOTTOM) {
  864. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, size.y * 4 / 6), size / 6), ci);
  865. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, size.y * 5 / 6), size / 6), ci);
  866. } else if (mask & TileSet::BIND_BOTTOM) {
  867. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, (size.y / 3) * 2), size / 3), c);
  868. }
  869. if (mask & TileSet::BIND_IGNORE_BOTTOMRIGHT) {
  870. workspace->draw_rect(Rect2(anchor + size * 4 / 6, size / 6), ci);
  871. workspace->draw_rect(Rect2(anchor + size * 5 / 6, size / 6), ci);
  872. } else if (mask & TileSet::BIND_BOTTOMRIGHT) {
  873. workspace->draw_rect(Rect2(anchor + (size / 3) * 2, size / 3), c);
  874. }
  875. }
  876. }
  877. }
  878. } break;
  879. case EDITMODE_COLLISION:
  880. case EDITMODE_OCCLUSION:
  881. case EDITMODE_NAVIGATION: {
  882. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  883. draw_highlight_subtile(edited_shape_coord);
  884. }
  885. draw_polygon_shapes();
  886. draw_grid_snap();
  887. } break;
  888. case EDITMODE_PRIORITY: {
  889. spin_priority->set_value(tileset->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord));
  890. uint32_t mask = tileset->autotile_get_bitmask(get_current_tile(), edited_shape_coord);
  891. Vector<Vector2> queue_others;
  892. int total = 0;
  893. for (Map<Vector2, uint32_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  894. if (E->value() == mask) {
  895. total += tileset->autotile_get_subtile_priority(get_current_tile(), E->key());
  896. if (E->key() != edited_shape_coord) {
  897. queue_others.push_back(E->key());
  898. }
  899. }
  900. }
  901. spin_priority->set_suffix(" / " + String::num(total, 0));
  902. draw_highlight_subtile(edited_shape_coord, queue_others);
  903. } break;
  904. case EDITMODE_Z_INDEX: {
  905. spin_z_index->set_value(tileset->autotile_get_z_index(get_current_tile(), edited_shape_coord));
  906. draw_highlight_subtile(edited_shape_coord);
  907. } break;
  908. default: {
  909. }
  910. }
  911. }
  912. String current_texture_path = get_current_texture()->get_path();
  913. List<int> tiles;
  914. tileset->get_tile_list(&tiles);
  915. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  916. int t_id = E->get();
  917. if (tileset->tile_get_texture(t_id)->get_path() == current_texture_path && (t_id != get_current_tile() || edit_mode != EDITMODE_REGION || workspace_mode != WORKSPACE_EDIT)) {
  918. Rect2i region = tileset->tile_get_region(t_id);
  919. region.position += WORKSPACE_MARGIN;
  920. Color c;
  921. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE) {
  922. c = COLOR_SINGLE;
  923. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE) {
  924. c = COLOR_AUTOTILE;
  925. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE) {
  926. c = COLOR_ATLAS;
  927. }
  928. draw_tile_subdivision(t_id, COLOR_SUBDIVISION);
  929. workspace->draw_rect(region, c, false);
  930. }
  931. }
  932. if (edit_mode == EDITMODE_REGION) {
  933. if (workspace_mode != WORKSPACE_EDIT) {
  934. Rect2i region = edited_region;
  935. Color c;
  936. if (workspace_mode == WORKSPACE_CREATE_SINGLE) {
  937. c = COLOR_SINGLE;
  938. } else if (workspace_mode == WORKSPACE_CREATE_AUTOTILE) {
  939. c = COLOR_AUTOTILE;
  940. } else if (workspace_mode == WORKSPACE_CREATE_ATLAS) {
  941. c = COLOR_ATLAS;
  942. }
  943. workspace->draw_rect(region, c, false);
  944. draw_edited_region_subdivision();
  945. } else {
  946. int t_id = get_current_tile();
  947. if (t_id < 0) {
  948. return;
  949. }
  950. Rect2i region;
  951. if (draw_edited_region) {
  952. region = edited_region;
  953. } else {
  954. region = tileset->tile_get_region(t_id);
  955. region.position += WORKSPACE_MARGIN;
  956. }
  957. if (draw_edited_region) {
  958. draw_edited_region_subdivision();
  959. } else {
  960. draw_tile_subdivision(t_id, COLOR_SUBDIVISION);
  961. }
  962. Color c;
  963. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE) {
  964. c = COLOR_SINGLE;
  965. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE) {
  966. c = COLOR_AUTOTILE;
  967. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE) {
  968. c = COLOR_ATLAS;
  969. }
  970. workspace->draw_rect(region, c, false);
  971. }
  972. }
  973. workspace_overlay->update();
  974. }
  975. void TileSetEditor::_on_workspace_process() {
  976. if (Input::get_singleton()->is_key_pressed(KEY_ALT) || tools[VISIBLE_INFO]->is_pressed()) {
  977. if (!tile_names_visible) {
  978. tile_names_visible = true;
  979. workspace_overlay->update();
  980. }
  981. } else if (tile_names_visible) {
  982. tile_names_visible = false;
  983. workspace_overlay->update();
  984. }
  985. }
  986. void TileSetEditor::_on_workspace_overlay_draw() {
  987. if (!tileset.is_valid() || !get_current_texture().is_valid()) {
  988. return;
  989. }
  990. const Color COLOR_AUTOTILE = Color(0.266373, 0.565288, 0.988281);
  991. const Color COLOR_SINGLE = Color(0.988281, 0.909323, 0.266373);
  992. const Color COLOR_ATLAS = Color(0.78653, 0.812835, 0.832031);
  993. if (tile_names_visible) {
  994. String current_texture_path = get_current_texture()->get_path();
  995. List<int> tiles;
  996. tileset->get_tile_list(&tiles);
  997. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  998. int t_id = E->get();
  999. if (tileset->tile_get_texture(t_id)->get_path() != current_texture_path) {
  1000. continue;
  1001. }
  1002. Rect2 region = tileset->tile_get_region(t_id);
  1003. region.position += WORKSPACE_MARGIN;
  1004. region.position *= workspace->get_scale().x;
  1005. Color c;
  1006. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE) {
  1007. c = COLOR_SINGLE;
  1008. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE) {
  1009. c = COLOR_AUTOTILE;
  1010. } else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE) {
  1011. c = COLOR_ATLAS;
  1012. }
  1013. String tile_id_name = String::num(t_id, 0) + ": " + tileset->tile_get_name(t_id);
  1014. Ref<Font> font = get_font("font", "Label");
  1015. region.set_size(font->get_string_size(tile_id_name));
  1016. workspace_overlay->draw_rect(region, c);
  1017. region.position.y += region.size.y - 2;
  1018. c = Color(0.1, 0.1, 0.1);
  1019. workspace_overlay->draw_string(font, region.position, tile_id_name, c);
  1020. }
  1021. }
  1022. int t_id = get_current_tile();
  1023. if (t_id < 0) {
  1024. return;
  1025. }
  1026. Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  1027. if (draw_handles) {
  1028. for (int i = 0; i < current_shape.size(); i++) {
  1029. workspace_overlay->draw_texture(handle, current_shape[i] * workspace->get_scale().x - handle->get_size() * 0.5);
  1030. }
  1031. }
  1032. }
  1033. int TileSetEditor::get_grabbed_point(const Vector2 &p_mouse_pos, real_t p_grab_threshold) {
  1034. Transform2D xform = workspace->get_transform();
  1035. int grabbed_point = -1;
  1036. real_t min_distance = 1e10;
  1037. for (int i = 0; i < current_shape.size(); i++) {
  1038. const real_t distance = xform.xform(current_shape[i]).distance_to(xform.xform(p_mouse_pos));
  1039. if (distance < p_grab_threshold && distance < min_distance) {
  1040. min_distance = distance;
  1041. grabbed_point = i;
  1042. }
  1043. }
  1044. return grabbed_point;
  1045. }
  1046. bool TileSetEditor::is_within_grabbing_distance_of_first_point(const Vector2 &p_pos, real_t p_grab_threshold) {
  1047. Transform2D xform = workspace->get_transform();
  1048. const real_t distance = xform.xform(current_shape[0]).distance_to(xform.xform(p_pos));
  1049. return distance < p_grab_threshold;
  1050. }
  1051. void TileSetEditor::_on_scroll_container_input(const Ref<InputEvent> &p_event) {
  1052. const Ref<InputEventMouseButton> mb = p_event;
  1053. if (mb.is_valid()) {
  1054. // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
  1055. // to allow performing this action anywhere, even if the cursor isn't
  1056. // hovering the texture in the workspace.
  1057. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed() && mb->get_control()) {
  1058. _zoom_on_position(scale_ratio, mb->get_position());
  1059. // Don't scroll up after zooming in.
  1060. accept_event();
  1061. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed() && mb->get_control()) {
  1062. _zoom_on_position(1 / scale_ratio, mb->get_position());
  1063. // Don't scroll down after zooming out.
  1064. accept_event();
  1065. }
  1066. }
  1067. }
  1068. void TileSetEditor::_on_workspace_input(const Ref<InputEvent> &p_ie) {
  1069. if (tileset.is_null() || !get_current_texture().is_valid()) {
  1070. return;
  1071. }
  1072. static bool dragging;
  1073. static bool erasing;
  1074. static bool alternative;
  1075. draw_edited_region = false;
  1076. Rect2 current_tile_region = Rect2();
  1077. if (get_current_tile() >= 0) {
  1078. current_tile_region = tileset->tile_get_region(get_current_tile());
  1079. }
  1080. current_tile_region.position += WORKSPACE_MARGIN;
  1081. const Ref<InputEventMouseButton> mb = p_ie;
  1082. const Ref<InputEventMouseMotion> mm = p_ie;
  1083. if (mb.is_valid()) {
  1084. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && !creating_shape) {
  1085. if (!current_tile_region.has_point(mb->get_position())) {
  1086. String current_texture_path = get_current_texture()->get_path();
  1087. List<int> tiles;
  1088. tileset->get_tile_list(&tiles);
  1089. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  1090. int t_id = E->get();
  1091. if (tileset->tile_get_texture(t_id)->get_path() == current_texture_path) {
  1092. Rect2 r = tileset->tile_get_region(t_id);
  1093. r.position += WORKSPACE_MARGIN;
  1094. if (r.has_point(mb->get_position())) {
  1095. set_current_tile(t_id);
  1096. workspace->update();
  1097. workspace_overlay->update();
  1098. return;
  1099. }
  1100. }
  1101. }
  1102. }
  1103. }
  1104. }
  1105. // Drag Middle Mouse
  1106. if (mm.is_valid()) {
  1107. if (mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  1108. Vector2 dragged(mm->get_relative().x, mm->get_relative().y);
  1109. scroll->set_h_scroll(scroll->get_h_scroll() - dragged.x * workspace->get_scale().x);
  1110. scroll->set_v_scroll(scroll->get_v_scroll() - dragged.y * workspace->get_scale().x);
  1111. }
  1112. }
  1113. if (edit_mode == EDITMODE_REGION) {
  1114. if (mb.is_valid()) {
  1115. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1116. if (get_current_tile() >= 0 || workspace_mode != WORKSPACE_EDIT) {
  1117. dragging = true;
  1118. region_from = mb->get_position();
  1119. edited_region = Rect2(region_from, Size2());
  1120. workspace->update();
  1121. workspace_overlay->update();
  1122. return;
  1123. }
  1124. } else if (dragging && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1125. dragging = false;
  1126. edited_region = Rect2();
  1127. workspace->update();
  1128. workspace_overlay->update();
  1129. return;
  1130. } else if (dragging && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1131. dragging = false;
  1132. update_edited_region(mb->get_position());
  1133. edited_region.position -= WORKSPACE_MARGIN;
  1134. if (!edited_region.has_no_area()) {
  1135. if (get_current_tile() >= 0 && workspace_mode == WORKSPACE_EDIT) {
  1136. undo_redo->create_action(TTR("Set Tile Region"));
  1137. undo_redo->add_do_method(tileset.ptr(), "tile_set_region", get_current_tile(), edited_region);
  1138. undo_redo->add_undo_method(tileset.ptr(), "tile_set_region", get_current_tile(), tileset->tile_get_region(get_current_tile()));
  1139. Size2 tile_workspace_size = edited_region.position + edited_region.size + WORKSPACE_MARGIN * 2;
  1140. Size2 workspace_minsize = workspace->get_custom_minimum_size();
  1141. // If the new region is bigger, just directly change the workspace size to avoid checking all other tiles.
  1142. if (tile_workspace_size.x > workspace_minsize.x || tile_workspace_size.y > workspace_minsize.y) {
  1143. Size2 max_workspace_size = Size2(MAX(tile_workspace_size.x, workspace_minsize.x), MAX(tile_workspace_size.y, workspace_minsize.y));
  1144. undo_redo->add_do_method(workspace, "set_custom_minimum_size", max_workspace_size);
  1145. undo_redo->add_undo_method(workspace, "set_custom_minimum_size", workspace_minsize);
  1146. undo_redo->add_do_method(workspace_container, "set_custom_minimum_size", max_workspace_size);
  1147. undo_redo->add_undo_method(workspace_container, "set_custom_minimum_size", workspace_minsize);
  1148. undo_redo->add_do_method(workspace_overlay, "set_custom_minimum_size", max_workspace_size);
  1149. undo_redo->add_undo_method(workspace_overlay, "set_custom_minimum_size", workspace_minsize);
  1150. } else if (workspace_minsize.x > get_current_texture()->get_size().x + WORKSPACE_MARGIN.x * 2 || workspace_minsize.y > get_current_texture()->get_size().y + WORKSPACE_MARGIN.y * 2) {
  1151. undo_redo->add_do_method(this, "update_workspace_minsize");
  1152. undo_redo->add_undo_method(this, "update_workspace_minsize");
  1153. }
  1154. edited_region = Rect2();
  1155. undo_redo->add_do_method(workspace, "update");
  1156. undo_redo->add_undo_method(workspace, "update");
  1157. undo_redo->add_do_method(workspace_overlay, "update");
  1158. undo_redo->add_undo_method(workspace_overlay, "update");
  1159. undo_redo->commit_action();
  1160. } else {
  1161. int t_id = tileset->get_last_unused_tile_id();
  1162. undo_redo->create_action(TTR("Create Tile"));
  1163. undo_redo->add_do_method(tileset.ptr(), "create_tile", t_id);
  1164. undo_redo->add_undo_method(tileset.ptr(), "remove_tile", t_id);
  1165. undo_redo->add_undo_method(this, "_validate_current_tile_id");
  1166. undo_redo->add_do_method(tileset.ptr(), "tile_set_texture", t_id, get_current_texture());
  1167. undo_redo->add_do_method(tileset.ptr(), "tile_set_region", t_id, edited_region);
  1168. undo_redo->add_do_method(tileset.ptr(), "tile_set_name", t_id, get_current_texture()->get_path().get_file() + " " + String::num(t_id, 0));
  1169. if (workspace_mode != WORKSPACE_CREATE_SINGLE) {
  1170. undo_redo->add_do_method(tileset.ptr(), "autotile_set_size", t_id, snap_step);
  1171. undo_redo->add_do_method(tileset.ptr(), "autotile_set_spacing", t_id, snap_separation.x);
  1172. undo_redo->add_do_method(tileset.ptr(), "tile_set_tile_mode", t_id, workspace_mode == WORKSPACE_CREATE_AUTOTILE ? TileSet::AUTO_TILE : TileSet::ATLAS_TILE);
  1173. }
  1174. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  1175. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  1176. edit_mode = EDITMODE_COLLISION;
  1177. Size2 tile_workspace_size = edited_region.position + edited_region.size + WORKSPACE_MARGIN * 2;
  1178. Size2 workspace_minsize = workspace->get_custom_minimum_size();
  1179. if (tile_workspace_size.x > workspace_minsize.x || tile_workspace_size.y > workspace_minsize.y) {
  1180. Size2 new_workspace_minsize = Size2(MAX(tile_workspace_size.x, workspace_minsize.x), MAX(tile_workspace_size.y, workspace_minsize.y));
  1181. undo_redo->add_do_method(workspace, "set_custom_minimum_size", new_workspace_minsize);
  1182. undo_redo->add_undo_method(workspace, "set_custom_minimum_size", workspace_minsize);
  1183. undo_redo->add_do_method(workspace_container, "set_custom_minimum_size", new_workspace_minsize);
  1184. undo_redo->add_undo_method(workspace_container, "set_custom_minimum_size", workspace_minsize);
  1185. undo_redo->add_do_method(workspace_overlay, "set_custom_minimum_size", new_workspace_minsize);
  1186. undo_redo->add_undo_method(workspace_overlay, "set_custom_minimum_size", workspace_minsize);
  1187. }
  1188. edited_region = Rect2();
  1189. undo_redo->add_do_method(workspace, "update");
  1190. undo_redo->add_undo_method(workspace, "update");
  1191. undo_redo->add_do_method(workspace_overlay, "update");
  1192. undo_redo->add_undo_method(workspace_overlay, "update");
  1193. undo_redo->commit_action();
  1194. set_current_tile(t_id);
  1195. _on_workspace_mode_changed(WORKSPACE_EDIT);
  1196. }
  1197. } else {
  1198. edited_region = Rect2();
  1199. workspace->update();
  1200. workspace_overlay->update();
  1201. }
  1202. return;
  1203. }
  1204. } else if (mm.is_valid()) {
  1205. if (dragging) {
  1206. update_edited_region(mm->get_position());
  1207. draw_edited_region = true;
  1208. workspace->update();
  1209. workspace_overlay->update();
  1210. return;
  1211. }
  1212. }
  1213. }
  1214. if (workspace_mode == WORKSPACE_EDIT) {
  1215. if (get_current_tile() >= 0) {
  1216. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1217. Vector2 size = tileset->autotile_get_size(get_current_tile());
  1218. switch (edit_mode) {
  1219. case EDITMODE_ICON: {
  1220. if (mb.is_valid()) {
  1221. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && current_tile_region.has_point(mb->get_position())) {
  1222. Vector2 coord((int)((mb->get_position().x - current_tile_region.position.x) / (spacing + size.x)), (int)((mb->get_position().y - current_tile_region.position.y) / (spacing + size.y)));
  1223. undo_redo->create_action(TTR("Set Tile Icon"));
  1224. undo_redo->add_do_method(tileset.ptr(), "autotile_set_icon_coordinate", get_current_tile(), coord);
  1225. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_icon_coordinate", get_current_tile(), tileset->autotile_get_icon_coordinate(get_current_tile()));
  1226. undo_redo->add_do_method(workspace, "update");
  1227. undo_redo->add_undo_method(workspace, "update");
  1228. undo_redo->commit_action();
  1229. }
  1230. }
  1231. } break;
  1232. case EDITMODE_BITMASK: {
  1233. if (mb.is_valid()) {
  1234. if (mb->is_pressed()) {
  1235. if (dragging) {
  1236. return;
  1237. }
  1238. if ((mb->get_button_index() == BUTTON_RIGHT || mb->get_button_index() == BUTTON_LEFT) && current_tile_region.has_point(mb->get_position())) {
  1239. dragging = true;
  1240. erasing = (mb->get_button_index() == BUTTON_RIGHT);
  1241. alternative = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  1242. Vector2 coord((int)((mb->get_position().x - current_tile_region.position.x) / (spacing + size.x)), (int)((mb->get_position().y - current_tile_region.position.y) / (spacing + size.y)));
  1243. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  1244. pos = mb->get_position() - (pos + current_tile_region.position);
  1245. uint32_t bit = 0;
  1246. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  1247. if (pos.x < size.x / 2) {
  1248. if (pos.y < size.y / 2) {
  1249. bit = TileSet::BIND_TOPLEFT;
  1250. } else {
  1251. bit = TileSet::BIND_BOTTOMLEFT;
  1252. }
  1253. } else {
  1254. if (pos.y < size.y / 2) {
  1255. bit = TileSet::BIND_TOPRIGHT;
  1256. } else {
  1257. bit = TileSet::BIND_BOTTOMRIGHT;
  1258. }
  1259. }
  1260. } else {
  1261. if (pos.x < size.x / 3) {
  1262. if (pos.y < size.y / 3) {
  1263. bit = TileSet::BIND_TOPLEFT;
  1264. } else if (pos.y > (size.y / 3) * 2) {
  1265. bit = TileSet::BIND_BOTTOMLEFT;
  1266. } else {
  1267. bit = TileSet::BIND_LEFT;
  1268. }
  1269. } else if (pos.x > (size.x / 3) * 2) {
  1270. if (pos.y < size.y / 3) {
  1271. bit = TileSet::BIND_TOPRIGHT;
  1272. } else if (pos.y > (size.y / 3) * 2) {
  1273. bit = TileSet::BIND_BOTTOMRIGHT;
  1274. } else {
  1275. bit = TileSet::BIND_RIGHT;
  1276. }
  1277. } else {
  1278. if (pos.y < size.y / 3) {
  1279. bit = TileSet::BIND_TOP;
  1280. } else if (pos.y > (size.y / 3) * 2) {
  1281. bit = TileSet::BIND_BOTTOM;
  1282. } else {
  1283. bit = TileSet::BIND_CENTER;
  1284. }
  1285. }
  1286. }
  1287. uint32_t old_mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  1288. uint32_t new_mask = old_mask;
  1289. if (alternative) {
  1290. new_mask &= ~bit;
  1291. new_mask |= (bit << 16);
  1292. } else if (erasing) {
  1293. new_mask &= ~bit;
  1294. new_mask &= ~(bit << 16);
  1295. } else {
  1296. new_mask |= bit;
  1297. new_mask &= ~(bit << 16);
  1298. }
  1299. if (old_mask != new_mask) {
  1300. undo_redo->create_action(TTR("Edit Tile Bitmask"));
  1301. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, new_mask);
  1302. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, old_mask);
  1303. undo_redo->add_do_method(workspace, "update");
  1304. undo_redo->add_undo_method(workspace, "update");
  1305. undo_redo->commit_action();
  1306. }
  1307. }
  1308. } else {
  1309. if ((erasing && mb->get_button_index() == BUTTON_RIGHT) || (!erasing && mb->get_button_index() == BUTTON_LEFT)) {
  1310. dragging = false;
  1311. erasing = false;
  1312. alternative = false;
  1313. }
  1314. }
  1315. }
  1316. if (mm.is_valid()) {
  1317. if (dragging && current_tile_region.has_point(mm->get_position())) {
  1318. Vector2 coord((int)((mm->get_position().x - current_tile_region.position.x) / (spacing + size.x)), (int)((mm->get_position().y - current_tile_region.position.y) / (spacing + size.y)));
  1319. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  1320. pos = mm->get_position() - (pos + current_tile_region.position);
  1321. uint32_t bit = 0;
  1322. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  1323. if (pos.x < size.x / 2) {
  1324. if (pos.y < size.y / 2) {
  1325. bit = TileSet::BIND_TOPLEFT;
  1326. } else {
  1327. bit = TileSet::BIND_BOTTOMLEFT;
  1328. }
  1329. } else {
  1330. if (pos.y < size.y / 2) {
  1331. bit = TileSet::BIND_TOPRIGHT;
  1332. } else {
  1333. bit = TileSet::BIND_BOTTOMRIGHT;
  1334. }
  1335. }
  1336. } else {
  1337. if (pos.x < size.x / 3) {
  1338. if (pos.y < size.y / 3) {
  1339. bit = TileSet::BIND_TOPLEFT;
  1340. } else if (pos.y > (size.y / 3) * 2) {
  1341. bit = TileSet::BIND_BOTTOMLEFT;
  1342. } else {
  1343. bit = TileSet::BIND_LEFT;
  1344. }
  1345. } else if (pos.x > (size.x / 3) * 2) {
  1346. if (pos.y < size.y / 3) {
  1347. bit = TileSet::BIND_TOPRIGHT;
  1348. } else if (pos.y > (size.y / 3) * 2) {
  1349. bit = TileSet::BIND_BOTTOMRIGHT;
  1350. } else {
  1351. bit = TileSet::BIND_RIGHT;
  1352. }
  1353. } else {
  1354. if (pos.y < size.y / 3) {
  1355. bit = TileSet::BIND_TOP;
  1356. } else if (pos.y > (size.y / 3) * 2) {
  1357. bit = TileSet::BIND_BOTTOM;
  1358. } else {
  1359. bit = TileSet::BIND_CENTER;
  1360. }
  1361. }
  1362. }
  1363. uint32_t old_mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  1364. uint32_t new_mask = old_mask;
  1365. if (alternative) {
  1366. new_mask &= ~bit;
  1367. new_mask |= (bit << 16);
  1368. } else if (erasing) {
  1369. new_mask &= ~bit;
  1370. new_mask &= ~(bit << 16);
  1371. } else {
  1372. new_mask |= bit;
  1373. new_mask &= ~(bit << 16);
  1374. }
  1375. if (old_mask != new_mask) {
  1376. undo_redo->create_action(TTR("Edit Tile Bitmask"));
  1377. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, new_mask);
  1378. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, old_mask);
  1379. undo_redo->add_do_method(workspace, "update");
  1380. undo_redo->add_undo_method(workspace, "update");
  1381. undo_redo->commit_action();
  1382. }
  1383. }
  1384. }
  1385. } break;
  1386. case EDITMODE_COLLISION:
  1387. case EDITMODE_OCCLUSION:
  1388. case EDITMODE_NAVIGATION:
  1389. case EDITMODE_PRIORITY:
  1390. case EDITMODE_Z_INDEX: {
  1391. Vector2 shape_anchor = Vector2(0, 0);
  1392. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  1393. shape_anchor = edited_shape_coord;
  1394. shape_anchor.x *= (size.x + spacing);
  1395. shape_anchor.y *= (size.y + spacing);
  1396. }
  1397. const real_t grab_threshold = EDITOR_GET("editors/poly_editor/point_grab_radius");
  1398. shape_anchor += current_tile_region.position;
  1399. if (tools[TOOL_SELECT]->is_pressed()) {
  1400. if (current_shape.size() > 0) {
  1401. for (int i = 0; i < current_shape.size(); i++) {
  1402. current_shape.set(i, snap_point(current_shape[i]));
  1403. }
  1404. workspace->update();
  1405. }
  1406. if (mb.is_valid()) {
  1407. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1408. if (edit_mode != EDITMODE_PRIORITY && current_shape.size() > 0) {
  1409. int grabbed_point = get_grabbed_point(mb->get_position(), grab_threshold);
  1410. if (grabbed_point >= 0) {
  1411. dragging_point = grabbed_point;
  1412. workspace->update();
  1413. return;
  1414. }
  1415. }
  1416. if ((tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) && current_tile_region.has_point(mb->get_position())) {
  1417. Vector2 coord((int)((mb->get_position().x - current_tile_region.position.x) / (spacing + size.x)), (int)((mb->get_position().y - current_tile_region.position.y) / (spacing + size.y)));
  1418. if (edited_shape_coord != coord) {
  1419. edited_shape_coord = coord;
  1420. _select_edited_shape_coord();
  1421. }
  1422. }
  1423. workspace->update();
  1424. } else if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1425. if (edit_mode == EDITMODE_COLLISION) {
  1426. if (dragging_point >= 0) {
  1427. dragging_point = -1;
  1428. Vector<Vector2> points;
  1429. for (int i = 0; i < current_shape.size(); i++) {
  1430. Vector2 p = current_shape[i];
  1431. if (tools[TOOL_GRID_SNAP]->is_pressed() || tools[SHAPE_KEEP_INSIDE_TILE]->is_pressed()) {
  1432. p = snap_point(p);
  1433. }
  1434. points.push_back(p - shape_anchor);
  1435. }
  1436. undo_redo->create_action(TTR("Edit Collision Polygon"));
  1437. _set_edited_shape_points(points);
  1438. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1439. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1440. undo_redo->commit_action();
  1441. }
  1442. } else if (edit_mode == EDITMODE_OCCLUSION) {
  1443. if (dragging_point >= 0) {
  1444. dragging_point = -1;
  1445. PoolVector<Vector2> polygon;
  1446. polygon.resize(current_shape.size());
  1447. PoolVector<Vector2>::Write w = polygon.write();
  1448. for (int i = 0; i < current_shape.size(); i++) {
  1449. w[i] = current_shape[i] - shape_anchor;
  1450. }
  1451. w.release();
  1452. undo_redo->create_action(TTR("Edit Occlusion Polygon"));
  1453. undo_redo->add_do_method(edited_occlusion_shape.ptr(), "set_polygon", polygon);
  1454. undo_redo->add_undo_method(edited_occlusion_shape.ptr(), "set_polygon", edited_occlusion_shape->get_polygon());
  1455. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1456. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1457. undo_redo->commit_action();
  1458. }
  1459. } else if (edit_mode == EDITMODE_NAVIGATION) {
  1460. if (dragging_point >= 0) {
  1461. dragging_point = -1;
  1462. PoolVector<Vector2> polygon;
  1463. Vector<int> indices;
  1464. polygon.resize(current_shape.size());
  1465. PoolVector<Vector2>::Write w = polygon.write();
  1466. for (int i = 0; i < current_shape.size(); i++) {
  1467. w[i] = current_shape[i] - shape_anchor;
  1468. indices.push_back(i);
  1469. }
  1470. w.release();
  1471. edited_navigation_shape->clear_outlines();
  1472. edited_navigation_shape->add_outline(polygon);
  1473. edited_navigation_shape->make_polygons_from_outlines();
  1474. // FIXME Couldn't figure out the undo_redo quagmire
  1475. //undo_redo->create_action(TTR("Edit Navigation Polygon"));
  1476. //undo_redo->add_do_method(edited_navigation_shape.ptr(), "set_vertices", polygon);
  1477. //undo_redo->add_undo_method(edited_navigation_shape.ptr(), "set_vertices", edited_navigation_shape->get_vertices());
  1478. //undo_redo->add_do_method(edited_navigation_shape.ptr(), "clear_polygons");
  1479. //undo_redo->add_undo_method(edited_navigation_shape.ptr(), "clear_polygons");
  1480. //undo_redo->add_do_method(edited_navigation_shape.ptr(), "add_polygon", indices);
  1481. //undo_redo->add_undo_method(edited_navigation_shape.ptr(), "add_polygon", edited_navigation_shape->get_polygon(0));
  1482. //undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1483. //undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1484. //undo_redo->commit_action();
  1485. }
  1486. }
  1487. }
  1488. } else if (mm.is_valid()) {
  1489. if (dragging_point >= 0) {
  1490. current_shape.set(dragging_point, snap_point(mm->get_position()));
  1491. workspace->update();
  1492. }
  1493. }
  1494. } else if (tools[SHAPE_NEW_POLYGON]->is_pressed()) {
  1495. if (mb.is_valid()) {
  1496. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1497. Vector2 pos = mb->get_position();
  1498. pos = snap_point(pos);
  1499. if (creating_shape) {
  1500. if (current_shape.size() > 2) {
  1501. if (is_within_grabbing_distance_of_first_point(mb->get_position(), grab_threshold)) {
  1502. close_shape(shape_anchor);
  1503. workspace->update();
  1504. return;
  1505. }
  1506. }
  1507. current_shape.push_back(pos);
  1508. workspace->update();
  1509. } else {
  1510. creating_shape = true;
  1511. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>());
  1512. current_shape.resize(0);
  1513. current_shape.push_back(snap_point(pos));
  1514. workspace->update();
  1515. }
  1516. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1517. if (creating_shape) {
  1518. creating_shape = false;
  1519. _select_edited_shape_coord();
  1520. workspace->update();
  1521. }
  1522. }
  1523. } else if (mm.is_valid()) {
  1524. if (creating_shape) {
  1525. workspace->update();
  1526. }
  1527. }
  1528. } else if (tools[SHAPE_NEW_RECTANGLE]->is_pressed()) {
  1529. if (mb.is_valid()) {
  1530. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1531. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>());
  1532. current_shape.resize(0);
  1533. Vector2 pos = mb->get_position();
  1534. pos = snap_point(pos);
  1535. current_shape.push_back(pos);
  1536. current_shape.push_back(pos);
  1537. current_shape.push_back(pos);
  1538. current_shape.push_back(pos);
  1539. creating_shape = true;
  1540. workspace->update();
  1541. return;
  1542. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1543. if (creating_shape) {
  1544. creating_shape = false;
  1545. _select_edited_shape_coord();
  1546. workspace->update();
  1547. }
  1548. } else if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1549. if (creating_shape) {
  1550. // if the first two corners are within grabbing distance of one another, expand the rect to fill the tile
  1551. if (is_within_grabbing_distance_of_first_point(current_shape[1], grab_threshold)) {
  1552. current_shape.set(0, snap_point(shape_anchor));
  1553. current_shape.set(1, snap_point(shape_anchor + Vector2(current_tile_region.size.x, 0)));
  1554. current_shape.set(2, snap_point(shape_anchor + current_tile_region.size));
  1555. current_shape.set(3, snap_point(shape_anchor + Vector2(0, current_tile_region.size.y)));
  1556. }
  1557. close_shape(shape_anchor);
  1558. workspace->update();
  1559. return;
  1560. }
  1561. }
  1562. } else if (mm.is_valid()) {
  1563. if (creating_shape) {
  1564. Vector2 pos = mm->get_position();
  1565. pos = snap_point(pos);
  1566. Vector2 p = current_shape[2];
  1567. current_shape.set(3, snap_point(Vector2(pos.x, p.y)));
  1568. current_shape.set(0, snap_point(pos));
  1569. current_shape.set(1, snap_point(Vector2(p.x, pos.y)));
  1570. workspace->update();
  1571. }
  1572. }
  1573. }
  1574. } break;
  1575. default: {
  1576. }
  1577. }
  1578. }
  1579. }
  1580. }
  1581. void TileSetEditor::_on_tool_clicked(int p_tool) {
  1582. if (p_tool == BITMASK_COPY) {
  1583. bitmask_map_copy = tileset->autotile_get_bitmask_map(get_current_tile());
  1584. } else if (p_tool == BITMASK_PASTE) {
  1585. undo_redo->create_action(TTR("Paste Tile Bitmask"));
  1586. undo_redo->add_do_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1587. undo_redo->add_undo_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1588. for (Map<Vector2, uint32_t>::Element *E = bitmask_map_copy.front(); E; E = E->next()) {
  1589. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1590. }
  1591. for (Map<Vector2, uint32_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  1592. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1593. }
  1594. undo_redo->add_do_method(workspace, "update");
  1595. undo_redo->add_undo_method(workspace, "update");
  1596. undo_redo->commit_action();
  1597. } else if (p_tool == BITMASK_CLEAR) {
  1598. undo_redo->create_action(TTR("Clear Tile Bitmask"));
  1599. undo_redo->add_do_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1600. for (Map<Vector2, uint32_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  1601. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1602. }
  1603. undo_redo->add_do_method(workspace, "update");
  1604. undo_redo->add_undo_method(workspace, "update");
  1605. undo_redo->commit_action();
  1606. } else if (p_tool == SHAPE_TOGGLE_TYPE) {
  1607. if (edited_collision_shape.is_valid()) {
  1608. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1609. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1610. Ref<Shape2D> previous_shape = edited_collision_shape;
  1611. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  1612. if (convex.is_valid()) {
  1613. // Make concave.
  1614. undo_redo->create_action(TTR("Make Polygon Concave"));
  1615. Ref<ConcavePolygonShape2D> _concave = memnew(ConcavePolygonShape2D);
  1616. edited_collision_shape = _concave;
  1617. _set_edited_shape_points(_get_collision_shape_points(convex));
  1618. } else if (concave.is_valid()) {
  1619. // Make convex.
  1620. undo_redo->create_action(TTR("Make Polygon Convex"));
  1621. Ref<ConvexPolygonShape2D> _convex = memnew(ConvexPolygonShape2D);
  1622. edited_collision_shape = _convex;
  1623. _set_edited_shape_points(_get_collision_shape_points(concave));
  1624. }
  1625. for (int i = 0; i < sd.size(); i++) {
  1626. if (sd[i].get("shape") == previous_shape) {
  1627. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  1628. sd.remove(i);
  1629. break;
  1630. }
  1631. }
  1632. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  1633. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  1634. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), edited_collision_shape, Transform2D(), false, edited_shape_coord);
  1635. } else {
  1636. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), edited_collision_shape, Transform2D());
  1637. }
  1638. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1639. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1640. undo_redo->commit_action();
  1641. _update_toggle_shape_button();
  1642. workspace->update();
  1643. workspace_container->update();
  1644. helper->_change_notify("");
  1645. }
  1646. } else if (p_tool == SELECT_NEXT) {
  1647. _select_next_shape();
  1648. } else if (p_tool == SELECT_PREVIOUS) {
  1649. _select_previous_shape();
  1650. } else if (p_tool == SHAPE_DELETE) {
  1651. if (creating_shape) {
  1652. creating_shape = false;
  1653. current_shape.resize(0);
  1654. workspace->update();
  1655. } else {
  1656. switch (edit_mode) {
  1657. case EDITMODE_REGION: {
  1658. int t_id = get_current_tile();
  1659. if (workspace_mode == WORKSPACE_EDIT && t_id >= 0) {
  1660. undo_redo->create_action(TTR("Remove Tile"));
  1661. undo_redo->add_do_method(tileset.ptr(), "remove_tile", t_id);
  1662. _undo_tile_removal(t_id);
  1663. undo_redo->add_do_method(this, "_validate_current_tile_id");
  1664. Rect2 tile_region = tileset->tile_get_region(get_current_tile());
  1665. Size2 tile_workspace_size = tile_region.position + tile_region.size;
  1666. if (tile_workspace_size.x > get_current_texture()->get_size().x || tile_workspace_size.y > get_current_texture()->get_size().y) {
  1667. undo_redo->add_do_method(this, "update_workspace_minsize");
  1668. undo_redo->add_undo_method(this, "update_workspace_minsize");
  1669. }
  1670. undo_redo->add_do_method(workspace, "update");
  1671. undo_redo->add_undo_method(workspace, "update");
  1672. undo_redo->add_do_method(workspace_overlay, "update");
  1673. undo_redo->add_undo_method(workspace_overlay, "update");
  1674. undo_redo->commit_action();
  1675. }
  1676. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  1677. workspace_mode = WORKSPACE_EDIT;
  1678. update_workspace_tile_mode();
  1679. } break;
  1680. case EDITMODE_COLLISION: {
  1681. if (!edited_collision_shape.is_null()) {
  1682. // Necessary to get the version that returns a Array instead of a Vector.
  1683. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  1684. for (int i = 0; i < sd.size(); i++) {
  1685. if (sd[i].get("shape") == edited_collision_shape) {
  1686. undo_redo->create_action(TTR("Remove Collision Polygon"));
  1687. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  1688. sd.remove(i);
  1689. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  1690. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1691. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1692. undo_redo->commit_action();
  1693. break;
  1694. }
  1695. }
  1696. }
  1697. } break;
  1698. case EDITMODE_OCCLUSION: {
  1699. if (!edited_occlusion_shape.is_null()) {
  1700. undo_redo->create_action(TTR("Remove Occlusion Polygon"));
  1701. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1702. undo_redo->add_do_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), Ref<OccluderPolygon2D>());
  1703. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), tileset->tile_get_light_occluder(get_current_tile()));
  1704. } else {
  1705. undo_redo->add_do_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), Ref<OccluderPolygon2D>(), edited_shape_coord);
  1706. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), tileset->autotile_get_light_occluder(get_current_tile(), edited_shape_coord), edited_shape_coord);
  1707. }
  1708. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1709. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1710. undo_redo->commit_action();
  1711. }
  1712. } break;
  1713. case EDITMODE_NAVIGATION: {
  1714. if (!edited_navigation_shape.is_null()) {
  1715. undo_redo->create_action(TTR("Remove Navigation Polygon"));
  1716. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1717. undo_redo->add_do_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), Ref<NavigationPolygon>());
  1718. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), tileset->tile_get_navigation_polygon(get_current_tile()));
  1719. } else {
  1720. undo_redo->add_do_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), Ref<NavigationPolygon>(), edited_shape_coord);
  1721. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), tileset->autotile_get_navigation_polygon(get_current_tile(), edited_shape_coord), edited_shape_coord);
  1722. }
  1723. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1724. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1725. undo_redo->commit_action();
  1726. }
  1727. } break;
  1728. default: {
  1729. }
  1730. }
  1731. }
  1732. } else if (p_tool == TOOL_SELECT || p_tool == SHAPE_NEW_POLYGON || p_tool == SHAPE_NEW_RECTANGLE) {
  1733. if (creating_shape) {
  1734. // Cancel Creation
  1735. creating_shape = false;
  1736. current_shape.resize(0);
  1737. workspace->update();
  1738. }
  1739. }
  1740. }
  1741. void TileSetEditor::_on_priority_changed(float val) {
  1742. if ((int)val == tileset->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord)) {
  1743. return;
  1744. }
  1745. undo_redo->create_action(TTR("Edit Tile Priority"));
  1746. undo_redo->add_do_method(tileset.ptr(), "autotile_set_subtile_priority", get_current_tile(), edited_shape_coord, (int)val);
  1747. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_subtile_priority", get_current_tile(), edited_shape_coord, tileset->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord));
  1748. undo_redo->add_do_method(workspace, "update");
  1749. undo_redo->add_undo_method(workspace, "update");
  1750. undo_redo->commit_action();
  1751. }
  1752. void TileSetEditor::_on_z_index_changed(float val) {
  1753. if ((int)val == tileset->autotile_get_z_index(get_current_tile(), edited_shape_coord)) {
  1754. return;
  1755. }
  1756. undo_redo->create_action(TTR("Edit Tile Z Index"));
  1757. undo_redo->add_do_method(tileset.ptr(), "autotile_set_z_index", get_current_tile(), edited_shape_coord, (int)val);
  1758. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_z_index", get_current_tile(), edited_shape_coord, tileset->autotile_get_z_index(get_current_tile(), edited_shape_coord));
  1759. undo_redo->add_do_method(workspace, "update");
  1760. undo_redo->add_undo_method(workspace, "update");
  1761. undo_redo->commit_action();
  1762. }
  1763. void TileSetEditor::_on_grid_snap_toggled(bool p_val) {
  1764. helper->set_snap_options_visible(p_val);
  1765. workspace->update();
  1766. }
  1767. Vector<Vector2> TileSetEditor::_get_collision_shape_points(const Ref<Shape2D> &p_shape) {
  1768. Ref<ConvexPolygonShape2D> convex = p_shape;
  1769. Ref<ConcavePolygonShape2D> concave = p_shape;
  1770. if (convex.is_valid()) {
  1771. return convex->get_points();
  1772. } else if (concave.is_valid()) {
  1773. Vector<Vector2> points;
  1774. for (int i = 0; i < concave->get_segments().size(); i += 2) {
  1775. points.push_back(concave->get_segments()[i]);
  1776. }
  1777. return points;
  1778. } else {
  1779. return Vector<Vector2>();
  1780. }
  1781. }
  1782. Vector<Vector2> TileSetEditor::_get_edited_shape_points() {
  1783. return _get_collision_shape_points(edited_collision_shape);
  1784. }
  1785. void TileSetEditor::_set_edited_shape_points(const Vector<Vector2> &points) {
  1786. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1787. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1788. if (convex.is_valid()) {
  1789. undo_redo->add_do_method(convex.ptr(), "set_points", points);
  1790. undo_redo->add_undo_method(convex.ptr(), "set_points", _get_edited_shape_points());
  1791. } else if (concave.is_valid() && points.size() > 1) {
  1792. PoolVector2Array segments;
  1793. for (int i = 0; i < points.size() - 1; i++) {
  1794. segments.push_back(points[i]);
  1795. segments.push_back(points[i + 1]);
  1796. }
  1797. segments.push_back(points[points.size() - 1]);
  1798. segments.push_back(points[0]);
  1799. undo_redo->add_do_method(concave.ptr(), "set_segments", segments);
  1800. undo_redo->add_undo_method(concave.ptr(), "set_segments", concave->get_segments());
  1801. }
  1802. }
  1803. void TileSetEditor::_update_tile_data() {
  1804. current_tile_data.clear();
  1805. if (get_current_tile() < 0) {
  1806. return;
  1807. }
  1808. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(get_current_tile());
  1809. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1810. SubtileData data;
  1811. for (int i = 0; i < sd.size(); i++) {
  1812. data.collisions.push_back(sd[i].shape);
  1813. }
  1814. data.navigation_shape = tileset->tile_get_navigation_polygon(get_current_tile());
  1815. data.occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  1816. current_tile_data[Vector2i()] = data;
  1817. } else {
  1818. Vector2 cell_count = _get_subtiles_count(get_current_tile());
  1819. for (int y = 0; y < cell_count.y; y++) {
  1820. for (int x = 0; x < cell_count.x; x++) {
  1821. SubtileData data;
  1822. Vector2i coord(x, y);
  1823. for (int i = 0; i < sd.size(); i++) {
  1824. if (sd[i].autotile_coord == coord) {
  1825. data.collisions.push_back(sd[i].shape);
  1826. }
  1827. }
  1828. data.navigation_shape = tileset->autotile_get_navigation_polygon(get_current_tile(), coord);
  1829. data.occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  1830. current_tile_data[coord] = data;
  1831. }
  1832. }
  1833. }
  1834. }
  1835. void TileSetEditor::_update_toggle_shape_button() {
  1836. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1837. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1838. separator_shape_toggle->show();
  1839. tools[SHAPE_TOGGLE_TYPE]->show();
  1840. if (edit_mode != EDITMODE_COLLISION || !edited_collision_shape.is_valid()) {
  1841. separator_shape_toggle->hide();
  1842. tools[SHAPE_TOGGLE_TYPE]->hide();
  1843. } else if (concave.is_valid()) {
  1844. tools[SHAPE_TOGGLE_TYPE]->set_icon(get_icon("ConvexPolygonShape2D", "EditorIcons"));
  1845. tools[SHAPE_TOGGLE_TYPE]->set_text(TTR("Make Convex"));
  1846. } else if (convex.is_valid()) {
  1847. tools[SHAPE_TOGGLE_TYPE]->set_icon(get_icon("ConcavePolygonShape2D", "EditorIcons"));
  1848. tools[SHAPE_TOGGLE_TYPE]->set_text(TTR("Make Concave"));
  1849. } else {
  1850. // Shouldn't happen
  1851. separator_shape_toggle->hide();
  1852. tools[SHAPE_TOGGLE_TYPE]->hide();
  1853. }
  1854. }
  1855. void TileSetEditor::_select_next_tile() {
  1856. Array tiles = _get_tiles_in_current_texture(true);
  1857. if (tiles.size() == 0) {
  1858. set_current_tile(-1);
  1859. } else if (get_current_tile() == -1) {
  1860. set_current_tile(tiles[0]);
  1861. } else {
  1862. int index = tiles.find(get_current_tile());
  1863. if (index < 0) {
  1864. set_current_tile(tiles[0]);
  1865. } else if (index == tiles.size() - 1) {
  1866. set_current_tile(tiles[0]);
  1867. } else {
  1868. set_current_tile(tiles[index + 1]);
  1869. }
  1870. }
  1871. if (get_current_tile() == -1) {
  1872. return;
  1873. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1874. return;
  1875. } else {
  1876. switch (edit_mode) {
  1877. case EDITMODE_COLLISION:
  1878. case EDITMODE_OCCLUSION:
  1879. case EDITMODE_NAVIGATION:
  1880. case EDITMODE_PRIORITY:
  1881. case EDITMODE_Z_INDEX: {
  1882. edited_shape_coord = Vector2();
  1883. _select_edited_shape_coord();
  1884. } break;
  1885. default: {
  1886. }
  1887. }
  1888. }
  1889. }
  1890. void TileSetEditor::_select_previous_tile() {
  1891. Array tiles = _get_tiles_in_current_texture(true);
  1892. if (tiles.size() == 0) {
  1893. set_current_tile(-1);
  1894. } else if (get_current_tile() == -1) {
  1895. set_current_tile(tiles[tiles.size() - 1]);
  1896. } else {
  1897. int index = tiles.find(get_current_tile());
  1898. if (index <= 0) {
  1899. set_current_tile(tiles[tiles.size() - 1]);
  1900. } else {
  1901. set_current_tile(tiles[index - 1]);
  1902. }
  1903. }
  1904. if (get_current_tile() == -1) {
  1905. return;
  1906. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1907. return;
  1908. } else {
  1909. switch (edit_mode) {
  1910. case EDITMODE_COLLISION:
  1911. case EDITMODE_OCCLUSION:
  1912. case EDITMODE_NAVIGATION:
  1913. case EDITMODE_PRIORITY:
  1914. case EDITMODE_Z_INDEX: {
  1915. edited_shape_coord = _get_subtiles_count(get_current_tile()) - Vector2(1, 1);
  1916. _select_edited_shape_coord();
  1917. } break;
  1918. default: {
  1919. }
  1920. }
  1921. }
  1922. }
  1923. Array TileSetEditor::_get_tiles_in_current_texture(bool sorted) {
  1924. Array a;
  1925. List<int> all_tiles;
  1926. if (!get_current_texture().is_valid()) {
  1927. return a;
  1928. }
  1929. tileset->get_tile_list(&all_tiles);
  1930. for (int i = 0; i < all_tiles.size(); i++) {
  1931. if (tileset->tile_get_texture(all_tiles[i]) == get_current_texture()) {
  1932. a.push_back(all_tiles[i]);
  1933. }
  1934. }
  1935. if (sorted) {
  1936. a.sort_custom(this, "_sort_tiles");
  1937. }
  1938. return a;
  1939. }
  1940. bool TileSetEditor::_sort_tiles(Variant p_a, Variant p_b) {
  1941. int a = p_a;
  1942. int b = p_b;
  1943. Vector2 pos_a = tileset->tile_get_region(a).position;
  1944. Vector2 pos_b = tileset->tile_get_region(b).position;
  1945. if (pos_a.y < pos_b.y) {
  1946. return true;
  1947. } else if (pos_a.y == pos_b.y) {
  1948. return (pos_a.x < pos_b.x);
  1949. } else {
  1950. return false;
  1951. }
  1952. }
  1953. Vector2 TileSetEditor::_get_subtiles_count(int p_tile_id) {
  1954. const int spacing = tileset->autotile_get_spacing(p_tile_id);
  1955. const Vector2 region_size = tileset->tile_get_region(p_tile_id).size;
  1956. const Vector2 subtile_size = tileset->autotile_get_size(p_tile_id);
  1957. // In case of not perfect fit the last row/column is allowed to exceed the tile region.
  1958. // The return value is the biggest integer-only `(m, n)` satisfying the formula:
  1959. // (m, n) * subtile_size + (m - 1, n - 1) * spacing < region_size + subtile_size
  1960. Vector2 mn = Vector2(1, 1) + (region_size / (subtile_size + Vector2(spacing, spacing)));
  1961. return mn == mn.floor() ? mn.floor() - Vector2(1, 1) : mn.floor();
  1962. }
  1963. void TileSetEditor::_select_next_subtile() {
  1964. if (get_current_tile() == -1) {
  1965. _select_next_tile();
  1966. return;
  1967. }
  1968. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1969. _select_next_tile();
  1970. } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) {
  1971. _select_next_tile();
  1972. } else {
  1973. Vector2 cell_count = _get_subtiles_count(get_current_tile());
  1974. if (edited_shape_coord.x >= cell_count.x - 1 && edited_shape_coord.y >= cell_count.y - 1) {
  1975. _select_next_tile();
  1976. } else {
  1977. edited_shape_coord.x++;
  1978. if (edited_shape_coord.x > cell_count.x - 1) {
  1979. edited_shape_coord.x = 0;
  1980. edited_shape_coord.y++;
  1981. }
  1982. _select_edited_shape_coord();
  1983. }
  1984. }
  1985. }
  1986. void TileSetEditor::_select_previous_subtile() {
  1987. if (get_current_tile() == -1) {
  1988. _select_previous_tile();
  1989. return;
  1990. }
  1991. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1992. _select_previous_tile();
  1993. } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) {
  1994. _select_previous_tile();
  1995. } else {
  1996. Vector2 cell_count = _get_subtiles_count(get_current_tile());
  1997. if (edited_shape_coord.x <= 0 && edited_shape_coord.y <= 0) {
  1998. _select_previous_tile();
  1999. } else {
  2000. edited_shape_coord.x--;
  2001. if (edited_shape_coord.x < 0) {
  2002. edited_shape_coord.x = cell_count.x - 1;
  2003. edited_shape_coord.y--;
  2004. }
  2005. _select_edited_shape_coord();
  2006. }
  2007. }
  2008. }
  2009. void TileSetEditor::_select_next_shape() {
  2010. if (get_current_tile() == -1) {
  2011. _select_next_subtile();
  2012. } else if (edit_mode != EDITMODE_COLLISION) {
  2013. _select_next_subtile();
  2014. } else {
  2015. Vector2i edited_coord = Vector2i();
  2016. if (tileset->tile_get_tile_mode(get_current_tile()) != TileSet::SINGLE_TILE) {
  2017. edited_coord = Vector2i(edited_shape_coord);
  2018. }
  2019. SubtileData data = current_tile_data[edited_coord];
  2020. if (data.collisions.size() == 0) {
  2021. _select_next_subtile();
  2022. } else {
  2023. int index = data.collisions.find(edited_collision_shape);
  2024. if (index < 0) {
  2025. _set_edited_collision_shape(data.collisions[0]);
  2026. } else if (index == data.collisions.size() - 1) {
  2027. _select_next_subtile();
  2028. } else {
  2029. _set_edited_collision_shape(data.collisions[index + 1]);
  2030. }
  2031. }
  2032. current_shape.resize(0);
  2033. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  2034. current_tile_region.position += WORKSPACE_MARGIN;
  2035. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2036. Vector2 size = tileset->autotile_get_size(get_current_tile());
  2037. Vector2 shape_anchor = edited_shape_coord;
  2038. shape_anchor.x *= (size.x + spacing);
  2039. shape_anchor.y *= (size.y + spacing);
  2040. current_tile_region.position += shape_anchor;
  2041. if (edited_collision_shape.is_valid()) {
  2042. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  2043. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  2044. }
  2045. }
  2046. workspace->update();
  2047. workspace_container->update();
  2048. helper->_change_notify("");
  2049. }
  2050. }
  2051. void TileSetEditor::_select_previous_shape() {
  2052. if (get_current_tile() == -1) {
  2053. _select_previous_subtile();
  2054. if (get_current_tile() != -1 && edit_mode == EDITMODE_COLLISION) {
  2055. SubtileData data = current_tile_data[Vector2i(edited_shape_coord)];
  2056. if (data.collisions.size() > 1) {
  2057. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  2058. }
  2059. } else {
  2060. return;
  2061. }
  2062. } else if (edit_mode != EDITMODE_COLLISION) {
  2063. _select_previous_subtile();
  2064. } else {
  2065. Vector2i edited_coord = Vector2i();
  2066. if (tileset->tile_get_tile_mode(get_current_tile()) != TileSet::SINGLE_TILE) {
  2067. edited_coord = Vector2i(edited_shape_coord);
  2068. }
  2069. SubtileData data = current_tile_data[edited_coord];
  2070. if (data.collisions.size() == 0) {
  2071. _select_previous_subtile();
  2072. data = current_tile_data[Vector2i(edited_shape_coord)];
  2073. if (data.collisions.size() > 1) {
  2074. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  2075. }
  2076. } else {
  2077. int index = data.collisions.find(edited_collision_shape);
  2078. if (index < 0) {
  2079. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  2080. } else if (index == 0) {
  2081. _select_previous_subtile();
  2082. data = current_tile_data[Vector2i(edited_shape_coord)];
  2083. if (data.collisions.size() > 1) {
  2084. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  2085. }
  2086. } else {
  2087. _set_edited_collision_shape(data.collisions[index - 1]);
  2088. }
  2089. }
  2090. current_shape.resize(0);
  2091. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  2092. current_tile_region.position += WORKSPACE_MARGIN;
  2093. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2094. Vector2 size = tileset->autotile_get_size(get_current_tile());
  2095. Vector2 shape_anchor = edited_shape_coord;
  2096. shape_anchor.x *= (size.x + spacing);
  2097. shape_anchor.y *= (size.y + spacing);
  2098. current_tile_region.position += shape_anchor;
  2099. if (edited_collision_shape.is_valid()) {
  2100. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  2101. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  2102. }
  2103. }
  2104. workspace->update();
  2105. workspace_container->update();
  2106. helper->_change_notify("");
  2107. }
  2108. }
  2109. void TileSetEditor::_set_edited_collision_shape(const Ref<Shape2D> &p_shape) {
  2110. edited_collision_shape = p_shape;
  2111. _update_toggle_shape_button();
  2112. }
  2113. void TileSetEditor::_set_snap_step(Vector2 p_val) {
  2114. snap_step.x = CLAMP(p_val.x, 1, 1024);
  2115. snap_step.y = CLAMP(p_val.y, 1, 1024);
  2116. workspace->update();
  2117. }
  2118. void TileSetEditor::_set_snap_off(Vector2 p_val) {
  2119. snap_offset.x = CLAMP(p_val.x, 0, 1024 + WORKSPACE_MARGIN.x);
  2120. snap_offset.y = CLAMP(p_val.y, 0, 1024 + WORKSPACE_MARGIN.y);
  2121. workspace->update();
  2122. }
  2123. void TileSetEditor::_set_snap_sep(Vector2 p_val) {
  2124. snap_separation.x = CLAMP(p_val.x, 0, 1024);
  2125. snap_separation.y = CLAMP(p_val.y, 0, 1024);
  2126. workspace->update();
  2127. }
  2128. void TileSetEditor::_validate_current_tile_id() {
  2129. if (get_current_tile() >= 0 && !tileset->has_tile(get_current_tile())) {
  2130. set_current_tile(-1);
  2131. }
  2132. }
  2133. void TileSetEditor::_select_edited_shape_coord() {
  2134. select_coord(edited_shape_coord);
  2135. }
  2136. void TileSetEditor::_undo_tile_removal(int p_id) {
  2137. undo_redo->add_undo_method(tileset.ptr(), "create_tile", p_id);
  2138. undo_redo->add_undo_method(tileset.ptr(), "tile_set_name", p_id, tileset->tile_get_name(p_id));
  2139. undo_redo->add_undo_method(tileset.ptr(), "tile_set_normal_map", p_id, tileset->tile_get_normal_map(p_id));
  2140. undo_redo->add_undo_method(tileset.ptr(), "tile_set_texture_offset", p_id, tileset->tile_get_texture_offset(p_id));
  2141. undo_redo->add_undo_method(tileset.ptr(), "tile_set_material", p_id, tileset->tile_get_material(p_id));
  2142. undo_redo->add_undo_method(tileset.ptr(), "tile_set_modulate", p_id, tileset->tile_get_modulate(p_id));
  2143. undo_redo->add_undo_method(tileset.ptr(), "tile_set_occluder_offset", p_id, tileset->tile_get_occluder_offset(p_id));
  2144. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon_offset", p_id, tileset->tile_get_navigation_polygon_offset(p_id));
  2145. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shape_offset", p_id, 0, tileset->tile_get_shape_offset(p_id, 0));
  2146. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shape_transform", p_id, 0, tileset->tile_get_shape_transform(p_id, 0));
  2147. undo_redo->add_undo_method(tileset.ptr(), "tile_set_z_index", p_id, tileset->tile_get_z_index(p_id));
  2148. undo_redo->add_undo_method(tileset.ptr(), "tile_set_texture", p_id, tileset->tile_get_texture(p_id));
  2149. undo_redo->add_undo_method(tileset.ptr(), "tile_set_region", p_id, tileset->tile_get_region(p_id));
  2150. // Necessary to get the version that returns a Array instead of a Vector.
  2151. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", p_id, tileset->call("tile_get_shapes", p_id));
  2152. if (tileset->tile_get_tile_mode(p_id) == TileSet::SINGLE_TILE) {
  2153. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", p_id, tileset->tile_get_light_occluder(p_id));
  2154. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", p_id, tileset->tile_get_navigation_polygon(p_id));
  2155. } else {
  2156. Map<Vector2, Ref<OccluderPolygon2D>> oclusion_map = tileset->autotile_get_light_oclusion_map(p_id);
  2157. for (Map<Vector2, Ref<OccluderPolygon2D>>::Element *E = oclusion_map.front(); E; E = E->next()) {
  2158. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_light_occluder", p_id, E->value(), E->key());
  2159. }
  2160. Map<Vector2, Ref<NavigationPolygon>> navigation_map = tileset->autotile_get_navigation_map(p_id);
  2161. for (Map<Vector2, Ref<NavigationPolygon>>::Element *E = navigation_map.front(); E; E = E->next()) {
  2162. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_navigation_polygon", p_id, E->value(), E->key());
  2163. }
  2164. Map<Vector2, uint32_t> bitmask_map = tileset->autotile_get_bitmask_map(p_id);
  2165. for (Map<Vector2, uint32_t>::Element *E = bitmask_map.front(); E; E = E->next()) {
  2166. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", p_id, E->key(), E->value());
  2167. }
  2168. Map<Vector2, int> priority_map = tileset->autotile_get_priority_map(p_id);
  2169. for (Map<Vector2, int>::Element *E = priority_map.front(); E; E = E->next()) {
  2170. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_subtile_priority", p_id, E->key(), E->value());
  2171. }
  2172. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_icon_coordinate", p_id, tileset->autotile_get_icon_coordinate(p_id));
  2173. Map<Vector2, int> z_map = tileset->autotile_get_z_index_map(p_id);
  2174. for (Map<Vector2, int>::Element *E = z_map.front(); E; E = E->next()) {
  2175. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_z_index", p_id, E->key(), E->value());
  2176. }
  2177. undo_redo->add_undo_method(tileset.ptr(), "tile_set_tile_mode", p_id, tileset->tile_get_tile_mode(p_id));
  2178. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_size", p_id, tileset->autotile_get_size(p_id));
  2179. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_spacing", p_id, tileset->autotile_get_spacing(p_id));
  2180. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask_mode", p_id, tileset->autotile_get_bitmask_mode(p_id));
  2181. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_fallback_mode", p_id, tileset->autotile_get_fallback_mode(p_id));
  2182. }
  2183. }
  2184. void TileSetEditor::_zoom_in() {
  2185. _zoom_on_position(scale_ratio, Vector2());
  2186. }
  2187. void TileSetEditor::_zoom_out() {
  2188. _zoom_on_position(1 / scale_ratio, Vector2());
  2189. }
  2190. void TileSetEditor::_zoom_reset() {
  2191. workspace->set_scale(Vector2(1, 1));
  2192. workspace_container->set_custom_minimum_size(workspace->get_rect().size);
  2193. workspace_overlay->set_custom_minimum_size(workspace->get_rect().size);
  2194. }
  2195. void TileSetEditor::_zoom_on_position(float p_zoom, const Vector2 &p_position) {
  2196. const float old_scale = workspace->get_scale().x;
  2197. const float new_scale = CLAMP(old_scale * p_zoom, min_scale, max_scale);
  2198. workspace->set_scale(Vector2(new_scale, new_scale));
  2199. workspace_container->set_custom_minimum_size(workspace->get_rect().size * new_scale);
  2200. workspace_overlay->set_custom_minimum_size(workspace->get_rect().size * new_scale);
  2201. Vector2 offset = Vector2(scroll->get_h_scroll(), scroll->get_v_scroll());
  2202. offset = (offset + p_position) / old_scale * new_scale - p_position;
  2203. scroll->set_h_scroll(offset.x);
  2204. scroll->set_v_scroll(offset.y);
  2205. }
  2206. void TileSetEditor::draw_highlight_current_tile() {
  2207. Color shadow_color = Color(0.3, 0.3, 0.3, 0.3);
  2208. if ((workspace_mode == WORKSPACE_EDIT && get_current_tile() >= 0) || !edited_region.has_no_area()) {
  2209. Rect2 region;
  2210. if (edited_region.has_no_area()) {
  2211. region = tileset->tile_get_region(get_current_tile());
  2212. region.position += WORKSPACE_MARGIN;
  2213. } else {
  2214. region = edited_region;
  2215. }
  2216. if (region.position.y >= 0) {
  2217. workspace->draw_rect(Rect2(0, 0, workspace->get_rect().size.x, region.position.y), shadow_color);
  2218. }
  2219. if (region.position.x >= 0) {
  2220. workspace->draw_rect(Rect2(0, MAX(0, region.position.y), region.position.x, MIN(workspace->get_rect().size.y - region.position.y, MIN(region.size.y, region.position.y + region.size.y))), shadow_color);
  2221. }
  2222. if (region.position.x + region.size.x <= workspace->get_rect().size.x) {
  2223. workspace->draw_rect(Rect2(region.position.x + region.size.x, MAX(0, region.position.y), workspace->get_rect().size.x - region.position.x - region.size.x, MIN(workspace->get_rect().size.y - region.position.y, MIN(region.size.y, region.position.y + region.size.y))), shadow_color);
  2224. }
  2225. if (region.position.y + region.size.y <= workspace->get_rect().size.y) {
  2226. workspace->draw_rect(Rect2(0, region.position.y + region.size.y, workspace->get_rect().size.x, workspace->get_rect().size.y - region.size.y - region.position.y), shadow_color);
  2227. }
  2228. } else {
  2229. workspace->draw_rect(Rect2(Point2(0, 0), workspace->get_rect().size), shadow_color);
  2230. }
  2231. }
  2232. void TileSetEditor::draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted) {
  2233. Color shadow_color = Color(0.3, 0.3, 0.3, 0.3);
  2234. Vector2 size = tileset->autotile_get_size(get_current_tile());
  2235. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2236. Rect2 region = tileset->tile_get_region(get_current_tile());
  2237. coord.x *= (size.x + spacing);
  2238. coord.y *= (size.y + spacing);
  2239. coord += region.position;
  2240. coord += WORKSPACE_MARGIN;
  2241. if (coord.y >= 0) {
  2242. workspace->draw_rect(Rect2(0, 0, workspace->get_rect().size.x, coord.y), shadow_color);
  2243. }
  2244. if (coord.x >= 0) {
  2245. workspace->draw_rect(Rect2(0, MAX(0, coord.y), coord.x, MIN(workspace->get_rect().size.y - coord.y, MIN(size.y, coord.y + size.y))), shadow_color);
  2246. }
  2247. if (coord.x + size.x <= workspace->get_rect().size.x) {
  2248. workspace->draw_rect(Rect2(coord.x + size.x, MAX(0, coord.y), workspace->get_rect().size.x - coord.x - size.x, MIN(workspace->get_rect().size.y - coord.y, MIN(size.y, coord.y + size.y))), shadow_color);
  2249. }
  2250. if (coord.y + size.y <= workspace->get_rect().size.y) {
  2251. workspace->draw_rect(Rect2(0, coord.y + size.y, workspace->get_rect().size.x, workspace->get_rect().size.y - size.y - coord.y), shadow_color);
  2252. }
  2253. coord += Vector2(1, 1) / workspace->get_scale().x;
  2254. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2) / workspace->get_scale().x), Color(1, 0, 0), false);
  2255. for (int i = 0; i < other_highlighted.size(); i++) {
  2256. coord = other_highlighted[i];
  2257. coord.x *= (size.x + spacing);
  2258. coord.y *= (size.y + spacing);
  2259. coord += region.position;
  2260. coord += WORKSPACE_MARGIN;
  2261. coord += Vector2(1, 1) / workspace->get_scale().x;
  2262. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2) / workspace->get_scale().x), Color(1, 0.5, 0.5), false);
  2263. }
  2264. }
  2265. void TileSetEditor::draw_tile_subdivision(int p_id, Color p_color) const {
  2266. Color c = p_color;
  2267. if (tileset->tile_get_tile_mode(p_id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(p_id) == TileSet::ATLAS_TILE) {
  2268. Rect2 region = tileset->tile_get_region(p_id);
  2269. Size2 size = tileset->autotile_get_size(p_id);
  2270. int spacing = tileset->autotile_get_spacing(p_id);
  2271. float j = size.x;
  2272. while (j < region.size.x) {
  2273. if (spacing <= 0) {
  2274. workspace->draw_line(region.position + WORKSPACE_MARGIN + Point2(j, 0), region.position + WORKSPACE_MARGIN + Point2(j, region.size.y), c);
  2275. } else {
  2276. workspace->draw_rect(Rect2(region.position + WORKSPACE_MARGIN + Point2(j, 0), Size2(spacing, region.size.y)), c);
  2277. }
  2278. j += spacing + size.x;
  2279. }
  2280. j = size.y;
  2281. while (j < region.size.y) {
  2282. if (spacing <= 0) {
  2283. workspace->draw_line(region.position + WORKSPACE_MARGIN + Point2(0, j), region.position + WORKSPACE_MARGIN + Point2(region.size.x, j), c);
  2284. } else {
  2285. workspace->draw_rect(Rect2(region.position + WORKSPACE_MARGIN + Point2(0, j), Size2(region.size.x, spacing)), c);
  2286. }
  2287. j += spacing + size.y;
  2288. }
  2289. }
  2290. }
  2291. void TileSetEditor::draw_edited_region_subdivision() const {
  2292. Color c = Color(0.3, 0.7, 0.6);
  2293. Rect2 region = edited_region;
  2294. Size2 size;
  2295. int spacing;
  2296. bool draw;
  2297. if (workspace_mode == WORKSPACE_EDIT) {
  2298. int p_id = get_current_tile();
  2299. size = tileset->autotile_get_size(p_id);
  2300. spacing = tileset->autotile_get_spacing(p_id);
  2301. draw = tileset->tile_get_tile_mode(p_id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(p_id) == TileSet::ATLAS_TILE;
  2302. } else {
  2303. size = snap_step;
  2304. spacing = snap_separation.x;
  2305. draw = workspace_mode != WORKSPACE_CREATE_SINGLE;
  2306. }
  2307. if (draw) {
  2308. float j = size.x;
  2309. while (j < region.size.x) {
  2310. if (spacing <= 0) {
  2311. workspace->draw_line(region.position + Point2(j, 0), region.position + Point2(j, region.size.y), c);
  2312. } else {
  2313. workspace->draw_rect(Rect2(region.position + Point2(j, 0), Size2(spacing, region.size.y)), c);
  2314. }
  2315. j += spacing + size.x;
  2316. }
  2317. j = size.y;
  2318. while (j < region.size.y) {
  2319. if (spacing <= 0) {
  2320. workspace->draw_line(region.position + Point2(0, j), region.position + Point2(region.size.x, j), c);
  2321. } else {
  2322. workspace->draw_rect(Rect2(region.position + Point2(0, j), Size2(region.size.x, spacing)), c);
  2323. }
  2324. j += spacing + size.y;
  2325. }
  2326. }
  2327. }
  2328. void TileSetEditor::draw_grid_snap() {
  2329. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  2330. Color grid_color = Color(0.4, 0, 1);
  2331. Size2 s = workspace->get_size();
  2332. int width_count = Math::floor((s.width - WORKSPACE_MARGIN.x) / (snap_step.x + snap_separation.x));
  2333. int height_count = Math::floor((s.height - WORKSPACE_MARGIN.y) / (snap_step.y + snap_separation.y));
  2334. int last_p = 0;
  2335. if (snap_step.x != 0) {
  2336. for (int i = 0; i <= width_count; i++) {
  2337. if (i == 0 && snap_offset.x != 0) {
  2338. last_p = snap_offset.x;
  2339. }
  2340. if (snap_separation.x != 0) {
  2341. if (i != 0) {
  2342. workspace->draw_rect(Rect2(last_p, 0, snap_separation.x, s.height), grid_color);
  2343. last_p += snap_separation.x;
  2344. } else {
  2345. workspace->draw_rect(Rect2(last_p, 0, -snap_separation.x, s.height), grid_color);
  2346. }
  2347. } else {
  2348. workspace->draw_line(Point2(last_p, 0), Point2(last_p, s.height), grid_color);
  2349. }
  2350. last_p += snap_step.x;
  2351. }
  2352. }
  2353. last_p = 0;
  2354. if (snap_step.y != 0) {
  2355. for (int i = 0; i <= height_count; i++) {
  2356. if (i == 0 && snap_offset.y != 0) {
  2357. last_p = snap_offset.y;
  2358. }
  2359. if (snap_separation.y != 0) {
  2360. if (i != 0) {
  2361. workspace->draw_rect(Rect2(0, last_p, s.width, snap_separation.y), grid_color);
  2362. last_p += snap_separation.y;
  2363. } else {
  2364. workspace->draw_rect(Rect2(0, last_p, s.width, -snap_separation.y), grid_color);
  2365. }
  2366. } else {
  2367. workspace->draw_line(Point2(0, last_p), Point2(s.width, last_p), grid_color);
  2368. }
  2369. last_p += snap_step.y;
  2370. }
  2371. }
  2372. }
  2373. }
  2374. void TileSetEditor::draw_polygon_shapes() {
  2375. int t_id = get_current_tile();
  2376. if (t_id < 0) {
  2377. return;
  2378. }
  2379. switch (edit_mode) {
  2380. case EDITMODE_COLLISION: {
  2381. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(t_id);
  2382. for (int i = 0; i < sd.size(); i++) {
  2383. Vector2 coord = Vector2(0, 0);
  2384. Vector2 anchor = Vector2(0, 0);
  2385. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2386. coord = sd[i].autotile_coord;
  2387. anchor = tileset->autotile_get_size(t_id);
  2388. anchor.x += tileset->autotile_get_spacing(t_id);
  2389. anchor.y += tileset->autotile_get_spacing(t_id);
  2390. anchor.x *= coord.x;
  2391. anchor.y *= coord.y;
  2392. }
  2393. anchor += WORKSPACE_MARGIN;
  2394. anchor += tileset->tile_get_region(t_id).position;
  2395. Ref<Shape2D> shape = sd[i].shape;
  2396. if (shape.is_valid()) {
  2397. Color c_bg;
  2398. Color c_border;
  2399. Ref<ConvexPolygonShape2D> convex = shape;
  2400. bool is_convex = convex.is_valid();
  2401. if ((tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE || coord == edited_shape_coord) && sd[i].shape == edited_collision_shape) {
  2402. if (is_convex) {
  2403. c_bg = Color(0, 1, 1, 0.5);
  2404. c_border = Color(0, 1, 1);
  2405. } else {
  2406. c_bg = Color(0.8, 0, 1, 0.5);
  2407. c_border = Color(0.8, 0, 1);
  2408. }
  2409. } else {
  2410. if (is_convex) {
  2411. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2412. c_border = Color(0.9, 0.7, 0.07, 1);
  2413. } else {
  2414. c_bg = Color(0.9, 0.45, 0.075, 0.5);
  2415. c_border = Color(0.9, 0.45, 0.075);
  2416. }
  2417. }
  2418. Vector<Vector2> polygon;
  2419. Vector<Color> colors;
  2420. if (!creating_shape && shape == edited_collision_shape && current_shape.size() > 2) {
  2421. for (int j = 0; j < current_shape.size(); j++) {
  2422. polygon.push_back(current_shape[j]);
  2423. colors.push_back(c_bg);
  2424. }
  2425. } else {
  2426. for (int j = 0; j < _get_collision_shape_points(shape).size(); j++) {
  2427. polygon.push_back(_get_collision_shape_points(shape)[j] + anchor);
  2428. colors.push_back(c_bg);
  2429. }
  2430. }
  2431. if (polygon.size() < 3) {
  2432. continue;
  2433. }
  2434. workspace->draw_polygon(polygon, colors);
  2435. if (coord == edited_shape_coord || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2436. if (!creating_shape && polygon.size() > 1) {
  2437. for (int j = 0; j < polygon.size() - 1; j++) {
  2438. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2439. }
  2440. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2441. }
  2442. if (shape == edited_collision_shape) {
  2443. draw_handles = true;
  2444. }
  2445. }
  2446. }
  2447. }
  2448. } break;
  2449. case EDITMODE_OCCLUSION: {
  2450. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2451. Ref<OccluderPolygon2D> shape = edited_occlusion_shape;
  2452. if (shape.is_valid()) {
  2453. Color c_bg = Color(0, 1, 1, 0.5);
  2454. Color c_border = Color(0, 1, 1);
  2455. Vector<Vector2> polygon;
  2456. Vector<Color> colors;
  2457. Vector2 anchor = WORKSPACE_MARGIN;
  2458. anchor += tileset->tile_get_region(get_current_tile()).position;
  2459. if (!creating_shape && shape == edited_occlusion_shape && current_shape.size() > 2) {
  2460. for (int j = 0; j < current_shape.size(); j++) {
  2461. polygon.push_back(current_shape[j]);
  2462. colors.push_back(c_bg);
  2463. }
  2464. } else {
  2465. for (int j = 0; j < shape->get_polygon().size(); j++) {
  2466. polygon.push_back(shape->get_polygon()[j] + anchor);
  2467. colors.push_back(c_bg);
  2468. }
  2469. }
  2470. workspace->draw_polygon(polygon, colors);
  2471. if (!creating_shape && polygon.size() > 1) {
  2472. for (int j = 0; j < polygon.size() - 1; j++) {
  2473. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2474. }
  2475. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2476. }
  2477. if (shape == edited_occlusion_shape) {
  2478. draw_handles = true;
  2479. }
  2480. }
  2481. } else {
  2482. Map<Vector2, Ref<OccluderPolygon2D>> map = tileset->autotile_get_light_oclusion_map(t_id);
  2483. for (Map<Vector2, Ref<OccluderPolygon2D>>::Element *E = map.front(); E; E = E->next()) {
  2484. Vector2 coord = E->key();
  2485. Vector2 anchor = tileset->autotile_get_size(t_id);
  2486. anchor.x += tileset->autotile_get_spacing(t_id);
  2487. anchor.y += tileset->autotile_get_spacing(t_id);
  2488. anchor.x *= coord.x;
  2489. anchor.y *= coord.y;
  2490. anchor += WORKSPACE_MARGIN;
  2491. anchor += tileset->tile_get_region(t_id).position;
  2492. Ref<OccluderPolygon2D> shape = E->value();
  2493. if (shape.is_valid()) {
  2494. Color c_bg;
  2495. Color c_border;
  2496. if (coord == edited_shape_coord && shape == edited_occlusion_shape) {
  2497. c_bg = Color(0, 1, 1, 0.5);
  2498. c_border = Color(0, 1, 1);
  2499. } else {
  2500. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2501. c_border = Color(0.9, 0.7, 0.07, 1);
  2502. }
  2503. Vector<Vector2> polygon;
  2504. Vector<Color> colors;
  2505. if (!creating_shape && shape == edited_occlusion_shape && current_shape.size() > 2) {
  2506. for (int j = 0; j < current_shape.size(); j++) {
  2507. polygon.push_back(current_shape[j]);
  2508. colors.push_back(c_bg);
  2509. }
  2510. } else {
  2511. for (int j = 0; j < shape->get_polygon().size(); j++) {
  2512. polygon.push_back(shape->get_polygon()[j] + anchor);
  2513. colors.push_back(c_bg);
  2514. }
  2515. }
  2516. workspace->draw_polygon(polygon, colors);
  2517. if (coord == edited_shape_coord) {
  2518. if (!creating_shape && polygon.size() > 1) {
  2519. for (int j = 0; j < polygon.size() - 1; j++) {
  2520. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2521. }
  2522. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2523. }
  2524. if (shape == edited_occlusion_shape) {
  2525. draw_handles = true;
  2526. }
  2527. }
  2528. }
  2529. }
  2530. }
  2531. } break;
  2532. case EDITMODE_NAVIGATION: {
  2533. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2534. Ref<NavigationPolygon> shape = edited_navigation_shape;
  2535. if (shape.is_valid()) {
  2536. Color c_bg = Color(0, 1, 1, 0.5);
  2537. Color c_border = Color(0, 1, 1);
  2538. Vector<Vector2> polygon;
  2539. Vector<Color> colors;
  2540. Vector2 anchor = WORKSPACE_MARGIN;
  2541. anchor += tileset->tile_get_region(get_current_tile()).position;
  2542. if (!creating_shape && shape == edited_navigation_shape && current_shape.size() > 2) {
  2543. for (int j = 0; j < current_shape.size(); j++) {
  2544. polygon.push_back(current_shape[j]);
  2545. colors.push_back(c_bg);
  2546. }
  2547. } else {
  2548. for (int outline_idx = 0; outline_idx < shape->get_outline_count(); outline_idx++) {
  2549. PoolVector<Vector2> outline_vertices = shape->get_outline(outline_idx);
  2550. for (int vertex_idx = 0; vertex_idx < outline_vertices.size(); vertex_idx++) {
  2551. polygon.push_back(outline_vertices[vertex_idx] + anchor);
  2552. }
  2553. }
  2554. colors.resize(polygon.size());
  2555. colors.fill(c_bg);
  2556. }
  2557. workspace->draw_polygon(polygon, colors);
  2558. if (!creating_shape && polygon.size() > 1) {
  2559. for (int j = 0; j < polygon.size() - 1; j++) {
  2560. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2561. }
  2562. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2563. }
  2564. if (shape == edited_navigation_shape) {
  2565. draw_handles = true;
  2566. }
  2567. }
  2568. } else {
  2569. Map<Vector2, Ref<NavigationPolygon>> map = tileset->autotile_get_navigation_map(t_id);
  2570. for (Map<Vector2, Ref<NavigationPolygon>>::Element *E = map.front(); E; E = E->next()) {
  2571. Vector2 coord = E->key();
  2572. Vector2 anchor = tileset->autotile_get_size(t_id);
  2573. anchor.x += tileset->autotile_get_spacing(t_id);
  2574. anchor.y += tileset->autotile_get_spacing(t_id);
  2575. anchor.x *= coord.x;
  2576. anchor.y *= coord.y;
  2577. anchor += WORKSPACE_MARGIN;
  2578. anchor += tileset->tile_get_region(t_id).position;
  2579. Ref<NavigationPolygon> shape = E->value();
  2580. if (shape.is_valid()) {
  2581. Color c_bg;
  2582. Color c_border;
  2583. if (coord == edited_shape_coord && shape == edited_navigation_shape) {
  2584. c_bg = Color(0, 1, 1, 0.5);
  2585. c_border = Color(0, 1, 1);
  2586. } else {
  2587. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2588. c_border = Color(0.9, 0.7, 0.07, 1);
  2589. }
  2590. Vector<Vector2> polygon;
  2591. Vector<Color> colors;
  2592. if (!creating_shape && shape == edited_navigation_shape && current_shape.size() > 2) {
  2593. for (int j = 0; j < current_shape.size(); j++) {
  2594. polygon.push_back(current_shape[j]);
  2595. colors.push_back(c_bg);
  2596. }
  2597. } else {
  2598. for (int outline_idx = 0; outline_idx < shape->get_outline_count(); outline_idx++) {
  2599. PoolVector<Vector2> outline_vertices = shape->get_outline(outline_idx);
  2600. for (int vertex_idx = 0; vertex_idx < outline_vertices.size(); vertex_idx++) {
  2601. polygon.push_back(outline_vertices[vertex_idx] + anchor);
  2602. }
  2603. }
  2604. colors.resize(polygon.size());
  2605. colors.fill(c_bg);
  2606. }
  2607. workspace->draw_polygon(polygon, colors);
  2608. if (coord == edited_shape_coord) {
  2609. if (!creating_shape && polygon.size() > 1) {
  2610. for (int j = 0; j < polygon.size() - 1; j++) {
  2611. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2612. }
  2613. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2614. }
  2615. if (shape == edited_navigation_shape) {
  2616. draw_handles = true;
  2617. }
  2618. }
  2619. }
  2620. }
  2621. }
  2622. } break;
  2623. default: {
  2624. }
  2625. }
  2626. if (creating_shape && current_shape.size() > 1) {
  2627. for (int j = 0; j < current_shape.size() - 1; j++) {
  2628. workspace->draw_line(current_shape[j], current_shape[j + 1], Color(0, 1, 1), 1, true);
  2629. }
  2630. workspace->draw_line(current_shape[current_shape.size() - 1], snap_point(workspace->get_local_mouse_position()), Color(0, 1, 1), 1, true);
  2631. draw_handles = true;
  2632. }
  2633. }
  2634. void TileSetEditor::close_shape(const Vector2 &shape_anchor) {
  2635. creating_shape = false;
  2636. if (edit_mode == EDITMODE_COLLISION) {
  2637. if (current_shape.size() >= 3) {
  2638. Ref<ConvexPolygonShape2D> shape = memnew(ConvexPolygonShape2D);
  2639. Vector<Vector2> points;
  2640. float p_total = 0;
  2641. for (int i = 0; i < current_shape.size(); i++) {
  2642. points.push_back(current_shape[i] - shape_anchor);
  2643. if (i != current_shape.size() - 1) {
  2644. p_total += ((current_shape[i + 1].x - current_shape[i].x) * (-current_shape[i + 1].y + (-current_shape[i].y)));
  2645. } else {
  2646. p_total += ((current_shape[0].x - current_shape[i].x) * (-current_shape[0].y + (-current_shape[i].y)));
  2647. }
  2648. }
  2649. if (p_total < 0) {
  2650. points.invert();
  2651. }
  2652. shape->set_points(points);
  2653. undo_redo->create_action(TTR("Create Collision Polygon"));
  2654. // Necessary to get the version that returns a Array instead of a Vector.
  2655. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  2656. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  2657. for (int i = 0; i < sd.size(); i++) {
  2658. if (sd[i].get("shape") == edited_collision_shape) {
  2659. sd.remove(i);
  2660. break;
  2661. }
  2662. }
  2663. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  2664. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2665. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), shape, Transform2D(), false, edited_shape_coord);
  2666. } else {
  2667. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), shape, Transform2D());
  2668. }
  2669. tools[TOOL_SELECT]->set_pressed(true);
  2670. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2671. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2672. undo_redo->commit_action();
  2673. } else {
  2674. tools[TOOL_SELECT]->set_pressed(true);
  2675. workspace->update();
  2676. }
  2677. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2678. Ref<OccluderPolygon2D> shape = memnew(OccluderPolygon2D);
  2679. PoolVector<Vector2> polygon;
  2680. polygon.resize(current_shape.size());
  2681. PoolVector<Vector2>::Write w = polygon.write();
  2682. for (int i = 0; i < current_shape.size(); i++) {
  2683. w[i] = current_shape[i] - shape_anchor;
  2684. }
  2685. w.release();
  2686. shape->set_polygon(polygon);
  2687. undo_redo->create_action(TTR("Create Occlusion Polygon"));
  2688. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2689. undo_redo->add_do_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), shape, edited_shape_coord);
  2690. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), tileset->autotile_get_light_occluder(get_current_tile(), edited_shape_coord), edited_shape_coord);
  2691. } else {
  2692. undo_redo->add_do_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), shape);
  2693. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), tileset->tile_get_light_occluder(get_current_tile()));
  2694. }
  2695. tools[TOOL_SELECT]->set_pressed(true);
  2696. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2697. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2698. undo_redo->commit_action();
  2699. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2700. Ref<NavigationPolygon> shape = memnew(NavigationPolygon);
  2701. PoolVector<Vector2> polygon;
  2702. Vector<int> indices;
  2703. polygon.resize(current_shape.size());
  2704. PoolVector<Vector2>::Write w = polygon.write();
  2705. for (int i = 0; i < current_shape.size(); i++) {
  2706. w[i] = current_shape[i] - shape_anchor;
  2707. indices.push_back(i);
  2708. }
  2709. w.release();
  2710. shape->clear_outlines();
  2711. shape->add_outline(polygon);
  2712. shape->make_polygons_from_outlines();
  2713. undo_redo->create_action(TTR("Create Navigation Polygon"));
  2714. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2715. undo_redo->add_do_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), shape, edited_shape_coord);
  2716. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), tileset->autotile_get_navigation_polygon(get_current_tile(), edited_shape_coord), edited_shape_coord);
  2717. } else {
  2718. undo_redo->add_do_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), shape);
  2719. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), tileset->tile_get_navigation_polygon(get_current_tile()));
  2720. }
  2721. tools[TOOL_SELECT]->set_pressed(true);
  2722. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2723. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2724. undo_redo->commit_action();
  2725. }
  2726. tileset->_change_notify("");
  2727. }
  2728. void TileSetEditor::select_coord(const Vector2 &coord) {
  2729. _update_tile_data();
  2730. current_shape = PoolVector2Array();
  2731. if (get_current_tile() == -1) {
  2732. return;
  2733. }
  2734. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  2735. current_tile_region.position += WORKSPACE_MARGIN;
  2736. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2737. if (edited_collision_shape != tileset->tile_get_shape(get_current_tile(), 0)) {
  2738. _set_edited_collision_shape(tileset->tile_get_shape(get_current_tile(), 0));
  2739. }
  2740. if (edited_occlusion_shape != tileset->tile_get_light_occluder(get_current_tile())) {
  2741. edited_occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  2742. }
  2743. if (edited_navigation_shape != tileset->tile_get_navigation_polygon(get_current_tile())) {
  2744. edited_navigation_shape = tileset->tile_get_navigation_polygon(get_current_tile());
  2745. }
  2746. if (edit_mode == EDITMODE_COLLISION) {
  2747. current_shape.resize(0);
  2748. if (edited_collision_shape.is_valid()) {
  2749. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  2750. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  2751. }
  2752. }
  2753. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2754. current_shape.resize(0);
  2755. if (edited_occlusion_shape.is_valid()) {
  2756. for (int i = 0; i < edited_occlusion_shape->get_polygon().size(); i++) {
  2757. current_shape.push_back(edited_occlusion_shape->get_polygon()[i] + current_tile_region.position);
  2758. }
  2759. }
  2760. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2761. current_shape.resize(0);
  2762. if (edited_navigation_shape.is_valid()) {
  2763. for (int outline_idx = 0; outline_idx < edited_navigation_shape->get_outline_count(); outline_idx++) {
  2764. PoolVector<Vector2> outline_vertices = edited_navigation_shape->get_outline(outline_idx);
  2765. for (int vertex_inx = 0; vertex_inx < outline_vertices.size(); vertex_inx++) {
  2766. current_shape.push_back(outline_vertices[vertex_inx] + current_tile_region.position);
  2767. }
  2768. }
  2769. }
  2770. }
  2771. } else {
  2772. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(get_current_tile());
  2773. bool found_collision_shape = false;
  2774. for (int i = 0; i < sd.size(); i++) {
  2775. if (sd[i].autotile_coord == coord) {
  2776. if (edited_collision_shape != sd[i].shape) {
  2777. _set_edited_collision_shape(sd[i].shape);
  2778. }
  2779. found_collision_shape = true;
  2780. break;
  2781. }
  2782. }
  2783. if (!found_collision_shape) {
  2784. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>(nullptr));
  2785. }
  2786. if (edited_occlusion_shape != tileset->autotile_get_light_occluder(get_current_tile(), coord)) {
  2787. edited_occlusion_shape = tileset->autotile_get_light_occluder(get_current_tile(), coord);
  2788. }
  2789. if (edited_navigation_shape != tileset->autotile_get_navigation_polygon(get_current_tile(), coord)) {
  2790. edited_navigation_shape = tileset->autotile_get_navigation_polygon(get_current_tile(), coord);
  2791. }
  2792. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2793. Vector2 size = tileset->autotile_get_size(get_current_tile());
  2794. Vector2 shape_anchor = coord;
  2795. shape_anchor.x *= (size.x + spacing);
  2796. shape_anchor.y *= (size.y + spacing);
  2797. shape_anchor += current_tile_region.position;
  2798. if (edit_mode == EDITMODE_COLLISION) {
  2799. current_shape.resize(0);
  2800. if (edited_collision_shape.is_valid()) {
  2801. for (int j = 0; j < _get_edited_shape_points().size(); j++) {
  2802. current_shape.push_back(_get_edited_shape_points()[j] + shape_anchor);
  2803. }
  2804. }
  2805. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2806. current_shape.resize(0);
  2807. if (edited_occlusion_shape.is_valid()) {
  2808. for (int i = 0; i < edited_occlusion_shape->get_polygon().size(); i++) {
  2809. current_shape.push_back(edited_occlusion_shape->get_polygon()[i] + shape_anchor);
  2810. }
  2811. }
  2812. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2813. current_shape.resize(0);
  2814. if (edited_navigation_shape.is_valid()) {
  2815. for (int outline_idx = 0; outline_idx < edited_navigation_shape->get_outline_count(); outline_idx++) {
  2816. PoolVector<Vector2> outline_vertices = edited_navigation_shape->get_outline(outline_idx);
  2817. for (int vertex_inx = 0; vertex_inx < outline_vertices.size(); vertex_inx++) {
  2818. current_shape.push_back(outline_vertices[vertex_inx] + shape_anchor);
  2819. }
  2820. }
  2821. }
  2822. }
  2823. }
  2824. workspace->update();
  2825. workspace_container->update();
  2826. helper->_change_notify("");
  2827. }
  2828. Vector2 TileSetEditor::snap_point(const Vector2 &point) {
  2829. Vector2 p = point;
  2830. Vector2 coord = edited_shape_coord;
  2831. Vector2 tile_size = tileset->autotile_get_size(get_current_tile());
  2832. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2833. Vector2 anchor = coord;
  2834. anchor.x *= (tile_size.x + spacing);
  2835. anchor.y *= (tile_size.y + spacing);
  2836. anchor += tileset->tile_get_region(get_current_tile()).position;
  2837. anchor += WORKSPACE_MARGIN;
  2838. Rect2 region(anchor, tile_size);
  2839. Rect2 tile_region(tileset->tile_get_region(get_current_tile()).position + WORKSPACE_MARGIN, tileset->tile_get_region(get_current_tile()).size);
  2840. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2841. region.position = tileset->tile_get_region(get_current_tile()).position + WORKSPACE_MARGIN;
  2842. region.size = tileset->tile_get_region(get_current_tile()).size;
  2843. }
  2844. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  2845. p.x = Math::snap_scalar_separation(snap_offset.x, snap_step.x, p.x, snap_separation.x);
  2846. p.y = Math::snap_scalar_separation(snap_offset.y, snap_step.y, p.y, snap_separation.y);
  2847. }
  2848. if (tools[SHAPE_KEEP_INSIDE_TILE]->is_pressed()) {
  2849. if (p.x < region.position.x) {
  2850. p.x = region.position.x;
  2851. }
  2852. if (p.y < region.position.y) {
  2853. p.y = region.position.y;
  2854. }
  2855. if (p.x > region.position.x + region.size.x) {
  2856. p.x = region.position.x + region.size.x;
  2857. }
  2858. if (p.y > region.position.y + region.size.y) {
  2859. p.y = region.position.y + region.size.y;
  2860. }
  2861. }
  2862. if (p.x < tile_region.position.x) {
  2863. p.x = tile_region.position.x;
  2864. }
  2865. if (p.y < tile_region.position.y) {
  2866. p.y = tile_region.position.y;
  2867. }
  2868. if (p.x > (tile_region.position.x + tile_region.size.x)) {
  2869. p.x = (tile_region.position.x + tile_region.size.x);
  2870. }
  2871. if (p.y > (tile_region.position.y + tile_region.size.y)) {
  2872. p.y = (tile_region.position.y + tile_region.size.y);
  2873. }
  2874. return p;
  2875. }
  2876. void TileSetEditor::add_texture(Ref<Texture> p_texture) {
  2877. texture_list->add_item(p_texture->get_path().get_file());
  2878. texture_map.insert(p_texture->get_path(), p_texture);
  2879. texture_list->set_item_metadata(texture_list->get_item_count() - 1, p_texture->get_path());
  2880. }
  2881. void TileSetEditor::remove_texture(Ref<Texture> p_texture) {
  2882. texture_list->remove_item(texture_list->find_metadata(p_texture->get_path()));
  2883. texture_map.erase(p_texture->get_path());
  2884. _validate_current_tile_id();
  2885. if (!get_current_texture().is_valid()) {
  2886. _on_texture_list_selected(-1);
  2887. workspace_overlay->update();
  2888. }
  2889. }
  2890. void TileSetEditor::update_texture_list() {
  2891. Ref<Texture> selected_texture = get_current_texture();
  2892. helper->set_tileset(tileset);
  2893. List<int> ids;
  2894. tileset->get_tile_list(&ids);
  2895. Vector<int> ids_to_remove;
  2896. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  2897. // Clear tiles referencing gone textures (user has been already given the chance to fix broken deps)
  2898. if (!tileset->tile_get_texture(E->get()).is_valid()) {
  2899. ids_to_remove.push_back(E->get());
  2900. ERR_CONTINUE(!tileset->tile_get_texture(E->get()).is_valid());
  2901. }
  2902. if (!texture_map.has(tileset->tile_get_texture(E->get())->get_path())) {
  2903. add_texture(tileset->tile_get_texture(E->get()));
  2904. }
  2905. }
  2906. for (int i = 0; i < ids_to_remove.size(); i++) {
  2907. tileset->remove_tile(ids_to_remove[i]);
  2908. }
  2909. if (texture_list->get_item_count() > 0 && selected_texture.is_valid()) {
  2910. texture_list->select(texture_list->find_metadata(selected_texture->get_path()));
  2911. if (texture_list->get_selected_items().size() > 0) {
  2912. _on_texture_list_selected(texture_list->get_selected_items()[0]);
  2913. }
  2914. } else if (get_current_texture().is_valid()) {
  2915. _on_texture_list_selected(texture_list->find_metadata(get_current_texture()->get_path()));
  2916. } else {
  2917. _validate_current_tile_id();
  2918. _on_texture_list_selected(-1);
  2919. workspace_overlay->update();
  2920. }
  2921. update_texture_list_icon();
  2922. helper->_change_notify("");
  2923. }
  2924. void TileSetEditor::update_texture_list_icon() {
  2925. for (int current_idx = 0; current_idx < texture_list->get_item_count(); current_idx++) {
  2926. String path = texture_list->get_item_metadata(current_idx);
  2927. texture_list->set_item_icon(current_idx, texture_map[path]);
  2928. Size2 texture_size = texture_map[path]->get_size();
  2929. texture_list->set_item_icon_region(current_idx, Rect2(0, 0, MIN(texture_size.x, 150), MIN(texture_size.y, 100)));
  2930. }
  2931. texture_list->update();
  2932. }
  2933. void TileSetEditor::update_workspace_tile_mode() {
  2934. if (!get_current_texture().is_valid()) {
  2935. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  2936. workspace_mode = WORKSPACE_EDIT;
  2937. for (int i = 1; i < WORKSPACE_MODE_MAX; i++) {
  2938. tool_workspacemode[i]->set_disabled(true);
  2939. }
  2940. tools[SELECT_NEXT]->set_disabled(true);
  2941. tools[SELECT_PREVIOUS]->set_disabled(true);
  2942. tools[ZOOM_OUT]->hide();
  2943. tools[ZOOM_1]->hide();
  2944. tools[ZOOM_IN]->hide();
  2945. tools[VISIBLE_INFO]->hide();
  2946. scroll->hide();
  2947. empty_message->show();
  2948. } else {
  2949. for (int i = 1; i < WORKSPACE_MODE_MAX; i++) {
  2950. tool_workspacemode[i]->set_disabled(false);
  2951. }
  2952. tools[SELECT_NEXT]->set_disabled(false);
  2953. tools[SELECT_PREVIOUS]->set_disabled(false);
  2954. tools[ZOOM_OUT]->show();
  2955. tools[ZOOM_1]->show();
  2956. tools[ZOOM_IN]->show();
  2957. tools[VISIBLE_INFO]->show();
  2958. scroll->show();
  2959. empty_message->hide();
  2960. }
  2961. if (workspace_mode != WORKSPACE_EDIT) {
  2962. for (int i = 0; i < EDITMODE_MAX; i++) {
  2963. tool_editmode[i]->hide();
  2964. }
  2965. tool_editmode[EDITMODE_REGION]->show();
  2966. tool_editmode[EDITMODE_REGION]->set_pressed(true);
  2967. _on_edit_mode_changed(EDITMODE_REGION);
  2968. separator_editmode->show();
  2969. return;
  2970. }
  2971. if (get_current_tile() < 0) {
  2972. for (int i = 0; i < EDITMODE_MAX; i++) {
  2973. tool_editmode[i]->hide();
  2974. }
  2975. for (int i = TOOL_SELECT; i < ZOOM_OUT; i++) {
  2976. tools[i]->hide();
  2977. }
  2978. separator_editmode->hide();
  2979. separator_bitmask->hide();
  2980. separator_delete->hide();
  2981. separator_grid->hide();
  2982. return;
  2983. }
  2984. for (int i = 0; i < EDITMODE_MAX; i++) {
  2985. tool_editmode[i]->show();
  2986. }
  2987. separator_editmode->show();
  2988. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2989. if (tool_editmode[EDITMODE_ICON]->is_pressed() || tool_editmode[EDITMODE_PRIORITY]->is_pressed() || tool_editmode[EDITMODE_BITMASK]->is_pressed() || tool_editmode[EDITMODE_Z_INDEX]->is_pressed()) {
  2990. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  2991. edit_mode = EDITMODE_COLLISION;
  2992. }
  2993. select_coord(Vector2(0, 0));
  2994. tool_editmode[EDITMODE_ICON]->hide();
  2995. tool_editmode[EDITMODE_BITMASK]->hide();
  2996. tool_editmode[EDITMODE_PRIORITY]->hide();
  2997. tool_editmode[EDITMODE_Z_INDEX]->hide();
  2998. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE) {
  2999. if (edit_mode == EDITMODE_ICON) {
  3000. select_coord(tileset->autotile_get_icon_coordinate(get_current_tile()));
  3001. } else {
  3002. _select_edited_shape_coord();
  3003. }
  3004. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  3005. if (tool_editmode[EDITMODE_PRIORITY]->is_pressed() || tool_editmode[EDITMODE_BITMASK]->is_pressed()) {
  3006. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  3007. edit_mode = EDITMODE_COLLISION;
  3008. }
  3009. if (edit_mode == EDITMODE_ICON) {
  3010. select_coord(tileset->autotile_get_icon_coordinate(get_current_tile()));
  3011. } else {
  3012. _select_edited_shape_coord();
  3013. }
  3014. tool_editmode[EDITMODE_BITMASK]->hide();
  3015. }
  3016. _on_edit_mode_changed(edit_mode);
  3017. }
  3018. void TileSetEditor::update_workspace_minsize() {
  3019. Size2 workspace_min_size = get_current_texture()->get_size();
  3020. String current_texture_path = get_current_texture()->get_path();
  3021. List<int> tiles;
  3022. tileset->get_tile_list(&tiles);
  3023. for (List<int>::Element *E = tiles.front(); E; E = E->next()) {
  3024. if (tileset->tile_get_texture(E->get())->get_path() != current_texture_path) {
  3025. continue;
  3026. }
  3027. Rect2i region = tileset->tile_get_region(E->get());
  3028. if (region.position.x + region.size.x > workspace_min_size.x) {
  3029. workspace_min_size.x = region.position.x + region.size.x;
  3030. }
  3031. if (region.position.y + region.size.y > workspace_min_size.y) {
  3032. workspace_min_size.y = region.position.y + region.size.y;
  3033. }
  3034. }
  3035. workspace_container->set_custom_minimum_size(workspace_min_size * workspace->get_scale() + WORKSPACE_MARGIN * 2);
  3036. workspace_overlay->set_custom_minimum_size(workspace_min_size * workspace->get_scale() + WORKSPACE_MARGIN * 2);
  3037. // Make sure workspace size is initialized last (otherwise it might be incorrect).
  3038. workspace->call_deferred("set_custom_minimum_size", workspace_min_size + WORKSPACE_MARGIN * 2);
  3039. }
  3040. void TileSetEditor::update_edited_region(const Vector2 &end_point) {
  3041. edited_region = Rect2(region_from, Size2());
  3042. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  3043. Vector2 grid_coord;
  3044. grid_coord = ((region_from - snap_offset) / (snap_step + snap_separation)).floor();
  3045. grid_coord *= (snap_step + snap_separation);
  3046. grid_coord += snap_offset;
  3047. edited_region.expand_to(grid_coord);
  3048. grid_coord += snap_step;
  3049. edited_region.expand_to(grid_coord);
  3050. grid_coord = ((end_point - snap_offset) / (snap_step + snap_separation)).floor();
  3051. grid_coord *= (snap_step + snap_separation);
  3052. grid_coord += snap_offset;
  3053. edited_region.expand_to(grid_coord);
  3054. grid_coord += snap_step;
  3055. edited_region.expand_to(grid_coord);
  3056. } else {
  3057. edited_region.expand_to(end_point);
  3058. }
  3059. }
  3060. int TileSetEditor::get_current_tile() const {
  3061. return current_tile;
  3062. }
  3063. void TileSetEditor::set_current_tile(int p_id) {
  3064. if (current_tile != p_id) {
  3065. current_tile = p_id;
  3066. helper->_change_notify("");
  3067. select_coord(Vector2(0, 0));
  3068. update_workspace_tile_mode();
  3069. if (p_id == -1) {
  3070. editor->get_inspector()->edit(tileset.ptr());
  3071. } else {
  3072. editor->get_inspector()->edit(helper);
  3073. }
  3074. }
  3075. }
  3076. Ref<Texture> TileSetEditor::get_current_texture() {
  3077. if (texture_list->get_selected_items().size() == 0) {
  3078. return Ref<Texture>();
  3079. } else {
  3080. return texture_map[texture_list->get_item_metadata(texture_list->get_selected_items()[0])];
  3081. }
  3082. }
  3083. void TilesetEditorContext::set_tileset(const Ref<TileSet> &p_tileset) {
  3084. tileset = p_tileset;
  3085. }
  3086. void TilesetEditorContext::set_snap_options_visible(bool p_visible) {
  3087. snap_options_visible = p_visible;
  3088. _change_notify("");
  3089. }
  3090. bool TilesetEditorContext::_set(const StringName &p_name, const Variant &p_value) {
  3091. String name = p_name.operator String();
  3092. if (name == "options_offset") {
  3093. Vector2 snap = p_value;
  3094. tileset_editor->_set_snap_off(snap + WORKSPACE_MARGIN);
  3095. return true;
  3096. } else if (name == "options_step") {
  3097. Vector2 snap = p_value;
  3098. tileset_editor->_set_snap_step(snap);
  3099. return true;
  3100. } else if (name == "options_separation") {
  3101. Vector2 snap = p_value;
  3102. tileset_editor->_set_snap_sep(snap);
  3103. return true;
  3104. } else if (p_name.operator String().left(5) == "tile_") {
  3105. String name2 = p_name.operator String().right(5);
  3106. bool v = false;
  3107. if (tileset_editor->get_current_tile() < 0 || tileset.is_null()) {
  3108. return false;
  3109. }
  3110. if (name2 == "autotile_bitmask_mode") {
  3111. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", p_value, &v);
  3112. } else if (name2 == "subtile_size") {
  3113. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/tile_size", p_value, &v);
  3114. } else if (name2 == "subtile_spacing") {
  3115. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/spacing", p_value, &v);
  3116. } else if (name2 == "autotile_fallback_mode") {
  3117. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/fallback_mode", p_value, &v);
  3118. } else {
  3119. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/" + name2, p_value, &v);
  3120. }
  3121. if (v) {
  3122. tileset->_change_notify("");
  3123. tileset_editor->workspace->update();
  3124. tileset_editor->workspace_overlay->update();
  3125. }
  3126. return v;
  3127. } else if (name == "tileset_script") {
  3128. tileset->set_script(p_value);
  3129. return true;
  3130. } else if (name == "selected_collision_one_way") {
  3131. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  3132. for (int index = 0; index < sd.size(); index++) {
  3133. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  3134. tileset->tile_set_shape_one_way(tileset_editor->get_current_tile(), index, p_value);
  3135. return true;
  3136. }
  3137. }
  3138. return false;
  3139. } else if (name == "selected_collision_one_way_margin") {
  3140. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  3141. for (int index = 0; index < sd.size(); index++) {
  3142. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  3143. tileset->tile_set_shape_one_way_margin(tileset_editor->get_current_tile(), index, p_value);
  3144. return true;
  3145. }
  3146. }
  3147. return false;
  3148. }
  3149. tileset_editor->err_dialog->set_text(TTR("This property can't be changed."));
  3150. tileset_editor->err_dialog->popup_centered(Size2(300, 60));
  3151. return false;
  3152. }
  3153. bool TilesetEditorContext::_get(const StringName &p_name, Variant &r_ret) const {
  3154. String name = p_name.operator String();
  3155. bool v = false;
  3156. if (name == "options_offset") {
  3157. r_ret = tileset_editor->snap_offset - WORKSPACE_MARGIN;
  3158. v = true;
  3159. } else if (name == "options_step") {
  3160. r_ret = tileset_editor->snap_step;
  3161. v = true;
  3162. } else if (name == "options_separation") {
  3163. r_ret = tileset_editor->snap_separation;
  3164. v = true;
  3165. } else if (name.left(5) == "tile_") {
  3166. name = name.right(5);
  3167. if (tileset_editor->get_current_tile() < 0 || tileset.is_null()) {
  3168. return false;
  3169. }
  3170. if (!tileset->has_tile(tileset_editor->get_current_tile())) {
  3171. return false;
  3172. }
  3173. if (name == "autotile_bitmask_mode") {
  3174. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", &v);
  3175. } else if (name == "subtile_size") {
  3176. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/tile_size", &v);
  3177. } else if (name == "subtile_spacing") {
  3178. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/spacing", &v);
  3179. } else if (name == "autotile_fallback_mode") {
  3180. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/fallback_mode", &v);
  3181. } else {
  3182. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/" + name, &v);
  3183. }
  3184. return v;
  3185. } else if (name == "selected_collision") {
  3186. r_ret = tileset_editor->edited_collision_shape;
  3187. v = true;
  3188. } else if (name == "selected_collision_one_way") {
  3189. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  3190. for (int index = 0; index < sd.size(); index++) {
  3191. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  3192. r_ret = sd[index].one_way_collision;
  3193. v = true;
  3194. break;
  3195. }
  3196. }
  3197. } else if (name == "selected_collision_one_way_margin") {
  3198. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  3199. for (int index = 0; index < sd.size(); index++) {
  3200. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  3201. r_ret = sd[index].one_way_collision_margin;
  3202. v = true;
  3203. break;
  3204. }
  3205. }
  3206. } else if (name == "selected_navigation") {
  3207. r_ret = tileset_editor->edited_navigation_shape;
  3208. v = true;
  3209. } else if (name == "selected_occlusion") {
  3210. r_ret = tileset_editor->edited_occlusion_shape;
  3211. v = true;
  3212. } else if (name == "tileset_script") {
  3213. r_ret = tileset->get_script();
  3214. v = true;
  3215. }
  3216. return v;
  3217. }
  3218. void TilesetEditorContext::_get_property_list(List<PropertyInfo> *p_list) const {
  3219. if (snap_options_visible) {
  3220. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Snap Options", "options_"), PROPERTY_HINT_NONE, "options_", PROPERTY_USAGE_GROUP));
  3221. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("options_offset")));
  3222. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("options_step")));
  3223. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("options_separation")));
  3224. }
  3225. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_COLLISION && tileset_editor->edited_collision_shape.is_valid()) {
  3226. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("selected_collision"), PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_collision_shape->get_class()));
  3227. if (tileset_editor->edited_collision_shape.is_valid()) {
  3228. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Collision Options", "selected_collision_"), PROPERTY_HINT_NONE, "selected_collision_", PROPERTY_USAGE_GROUP));
  3229. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("selected_collision_one_way"), PROPERTY_HINT_NONE));
  3230. p_list->push_back(PropertyInfo(Variant::REAL, PNAME("selected_collision_one_way_margin"), PROPERTY_HINT_NONE));
  3231. }
  3232. }
  3233. if (tileset_editor->get_current_tile() >= 0 && !tileset.is_null()) {
  3234. int id = tileset_editor->get_current_tile();
  3235. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Selected Tile", "tile_"), PROPERTY_HINT_NONE, "tile_", PROPERTY_USAGE_GROUP));
  3236. p_list->push_back(PropertyInfo(Variant::STRING, PNAME("tile_name")));
  3237. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("tile_texture"), PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  3238. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("tile_normal_map"), PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  3239. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_tex_offset")));
  3240. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("tile_material"), PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"));
  3241. p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("tile_modulate")));
  3242. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_tile_mode"), PROPERTY_HINT_ENUM, "SINGLE_TILE,AUTO_TILE,ATLAS_TILE"));
  3243. if (tileset->tile_get_tile_mode(id) == TileSet::AUTO_TILE) {
  3244. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_autotile_bitmask_mode"), PROPERTY_HINT_ENUM, "2x2,3x3 (minimal),3x3"));
  3245. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_subtile_size")));
  3246. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_subtile_spacing"), PROPERTY_HINT_RANGE, "0, 1024, 1"));
  3247. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_autotile_fallback_mode"), PROPERTY_HINT_ENUM, "Auto,Icon"));
  3248. } else if (tileset->tile_get_tile_mode(id) == TileSet::ATLAS_TILE) {
  3249. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_subtile_size")));
  3250. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_subtile_spacing"), PROPERTY_HINT_RANGE, "0, 1024, 1"));
  3251. }
  3252. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_occluder_offset")));
  3253. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_navigation_offset")));
  3254. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_shape_offset"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  3255. p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("tile_shape_transform"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  3256. p_list->push_back(PropertyInfo(Variant::INT, PNAME("tile_z_index"), PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"));
  3257. }
  3258. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_NAVIGATION && tileset_editor->edited_navigation_shape.is_valid()) {
  3259. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("selected_navigation"), PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_navigation_shape->get_class()));
  3260. }
  3261. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_OCCLUSION && tileset_editor->edited_occlusion_shape.is_valid()) {
  3262. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("selected_occlusion"), PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_occlusion_shape->get_class()));
  3263. }
  3264. if (!tileset.is_null()) {
  3265. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("TileSet", "tileset"), PROPERTY_HINT_NONE, "tileset", PROPERTY_USAGE_CATEGORY));
  3266. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("tileset_script"), PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  3267. }
  3268. }
  3269. void TilesetEditorContext::_bind_methods() {
  3270. ClassDB::bind_method("_hide_script_from_inspector", &TilesetEditorContext::_hide_script_from_inspector);
  3271. }
  3272. TilesetEditorContext::TilesetEditorContext(TileSetEditor *p_tileset_editor) {
  3273. tileset_editor = p_tileset_editor;
  3274. snap_options_visible = false;
  3275. }
  3276. void TileSetEditorPlugin::edit(Object *p_node) {
  3277. if (Object::cast_to<TileSet>(p_node)) {
  3278. tileset_editor->edit(Object::cast_to<TileSet>(p_node));
  3279. }
  3280. }
  3281. bool TileSetEditorPlugin::handles(Object *p_node) const {
  3282. return p_node->is_class("TileSet") || p_node->is_class("TilesetEditorContext");
  3283. }
  3284. void TileSetEditorPlugin::make_visible(bool p_visible) {
  3285. if (p_visible) {
  3286. tileset_editor_button->show();
  3287. editor->make_bottom_panel_item_visible(tileset_editor);
  3288. if (!get_tree()->is_connected("idle_frame", tileset_editor, "_on_workspace_process")) {
  3289. get_tree()->connect("idle_frame", tileset_editor, "_on_workspace_process");
  3290. }
  3291. } else {
  3292. editor->hide_bottom_panel();
  3293. tileset_editor_button->hide();
  3294. if (get_tree()->is_connected("idle_frame", tileset_editor, "_on_workspace_process")) {
  3295. get_tree()->disconnect("idle_frame", tileset_editor, "_on_workspace_process");
  3296. }
  3297. }
  3298. }
  3299. Dictionary TileSetEditorPlugin::get_state() const {
  3300. Dictionary state;
  3301. state["snap_offset"] = tileset_editor->snap_offset;
  3302. state["snap_step"] = tileset_editor->snap_step;
  3303. state["snap_separation"] = tileset_editor->snap_separation;
  3304. state["snap_enabled"] = tileset_editor->tools[TileSetEditor::TOOL_GRID_SNAP]->is_pressed();
  3305. state["keep_inside_tile"] = tileset_editor->tools[TileSetEditor::SHAPE_KEEP_INSIDE_TILE]->is_pressed();
  3306. state["show_information"] = tileset_editor->tools[TileSetEditor::VISIBLE_INFO]->is_pressed();
  3307. return state;
  3308. }
  3309. void TileSetEditorPlugin::set_state(const Dictionary &p_state) {
  3310. Dictionary state = p_state;
  3311. if (state.has("snap_step")) {
  3312. tileset_editor->_set_snap_step(state["snap_step"]);
  3313. }
  3314. if (state.has("snap_offset")) {
  3315. tileset_editor->_set_snap_off(state["snap_offset"]);
  3316. }
  3317. if (state.has("snap_separation")) {
  3318. tileset_editor->_set_snap_sep(state["snap_separation"]);
  3319. }
  3320. if (state.has("snap_enabled")) {
  3321. tileset_editor->tools[TileSetEditor::TOOL_GRID_SNAP]->set_pressed(state["snap_enabled"]);
  3322. if (tileset_editor->helper) {
  3323. tileset_editor->_on_grid_snap_toggled(state["snap_enabled"]);
  3324. }
  3325. }
  3326. if (state.has("keep_inside_tile")) {
  3327. tileset_editor->tools[TileSetEditor::SHAPE_KEEP_INSIDE_TILE]->set_pressed(state["keep_inside_tile"]);
  3328. }
  3329. if (state.has("show_information")) {
  3330. tileset_editor->tools[TileSetEditor::VISIBLE_INFO]->set_pressed(state["show_information"]);
  3331. }
  3332. }
  3333. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  3334. editor = p_node;
  3335. tileset_editor = memnew(TileSetEditor(p_node));
  3336. tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  3337. tileset_editor->hide();
  3338. tileset_editor_button = p_node->add_bottom_panel_item(TTR("TileSet"), tileset_editor);
  3339. tileset_editor_button->hide();
  3340. }