tile_set_editor_plugin.cpp 134 KB

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