2
0

tile_set_editor_plugin.cpp 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353
  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.\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. _update_toggle_shape_button();
  639. workspace->update();
  640. }
  641. void TileSetEditor::_on_workspace_mode_changed(int p_workspace_mode) {
  642. workspace_mode = (WorkspaceMode)p_workspace_mode;
  643. if (p_workspace_mode == WORKSPACE_EDIT) {
  644. update_workspace_tile_mode();
  645. } else {
  646. for (int i = 0; i < EDITMODE_MAX; i++) {
  647. tool_editmode[i]->hide();
  648. }
  649. tool_editmode[EDITMODE_REGION]->show();
  650. tool_editmode[EDITMODE_REGION]->set_pressed(true);
  651. _on_edit_mode_changed(EDITMODE_REGION);
  652. separator_editmode->show();
  653. }
  654. }
  655. void TileSetEditor::_on_workspace_draw() {
  656. if (tileset.is_null() || !get_current_texture().is_valid())
  657. return;
  658. const Color COLOR_AUTOTILE = Color(0.3, 0.6, 1);
  659. const Color COLOR_SINGLE = Color(1, 1, 0.3);
  660. const Color COLOR_ATLAS = Color(0.8, 0.8, 0.8);
  661. const Color COLOR_SUBDIVISION = Color(0.3, 0.7, 0.6);
  662. draw_handles = false;
  663. draw_highlight_current_tile();
  664. draw_grid_snap();
  665. if (get_current_tile() >= 0) {
  666. int spacing = tileset->autotile_get_spacing(get_current_tile());
  667. Vector2 size = tileset->autotile_get_size(get_current_tile());
  668. Rect2i region = tileset->tile_get_region(get_current_tile());
  669. switch (edit_mode) {
  670. case EDITMODE_ICON: {
  671. Vector2 coord = tileset->autotile_get_icon_coordinate(get_current_tile());
  672. draw_highlight_subtile(coord);
  673. } break;
  674. case EDITMODE_BITMASK: {
  675. Color c(1, 0, 0, 0.5);
  676. for (float x = 0; x < region.size.x / (spacing + size.x); x++) {
  677. for (float y = 0; y < region.size.y / (spacing + size.y); y++) {
  678. Vector2 coord(x, y);
  679. Point2 anchor(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  680. anchor += WORKSPACE_MARGIN;
  681. anchor += region.position;
  682. uint16_t mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  683. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  684. if (mask & TileSet::BIND_TOPLEFT) {
  685. workspace->draw_rect(Rect2(anchor, size / 2), c);
  686. }
  687. if (mask & TileSet::BIND_TOPRIGHT) {
  688. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 2, 0), size / 2), c);
  689. }
  690. if (mask & TileSet::BIND_BOTTOMLEFT) {
  691. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 2), size / 2), c);
  692. }
  693. if (mask & TileSet::BIND_BOTTOMRIGHT) {
  694. workspace->draw_rect(Rect2(anchor + size / 2, size / 2), c);
  695. }
  696. } else {
  697. if (mask & TileSet::BIND_TOPLEFT) {
  698. workspace->draw_rect(Rect2(anchor, size / 3), c);
  699. }
  700. if (mask & TileSet::BIND_TOP) {
  701. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, 0), size / 3), c);
  702. }
  703. if (mask & TileSet::BIND_TOPRIGHT) {
  704. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, 0), size / 3), c);
  705. }
  706. if (mask & TileSet::BIND_LEFT) {
  707. workspace->draw_rect(Rect2(anchor + Vector2(0, size.y / 3), size / 3), c);
  708. }
  709. if (mask & TileSet::BIND_CENTER) {
  710. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, size.y / 3), size / 3), c);
  711. }
  712. if (mask & TileSet::BIND_RIGHT) {
  713. workspace->draw_rect(Rect2(anchor + Vector2((size.x / 3) * 2, size.y / 3), size / 3), c);
  714. }
  715. if (mask & TileSet::BIND_BOTTOMLEFT) {
  716. workspace->draw_rect(Rect2(anchor + Vector2(0, (size.y / 3) * 2), size / 3), c);
  717. }
  718. if (mask & TileSet::BIND_BOTTOM) {
  719. workspace->draw_rect(Rect2(anchor + Vector2(size.x / 3, (size.y / 3) * 2), size / 3), c);
  720. }
  721. if (mask & TileSet::BIND_BOTTOMRIGHT) {
  722. workspace->draw_rect(Rect2(anchor + (size / 3) * 2, size / 3), c);
  723. }
  724. }
  725. }
  726. }
  727. } break;
  728. case EDITMODE_COLLISION:
  729. case EDITMODE_OCCLUSION:
  730. case EDITMODE_NAVIGATION: {
  731. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  732. Vector2 coord = edited_shape_coord;
  733. draw_highlight_subtile(coord);
  734. }
  735. draw_polygon_shapes();
  736. draw_grid_snap();
  737. } break;
  738. case EDITMODE_PRIORITY: {
  739. spin_priority->set_value(tileset->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord));
  740. uint16_t mask = tileset->autotile_get_bitmask(get_current_tile(), edited_shape_coord);
  741. Vector<Vector2> queue_others;
  742. int total = 0;
  743. for (Map<Vector2, uint16_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  744. if (E->value() == mask) {
  745. total += tileset->autotile_get_subtile_priority(get_current_tile(), E->key());
  746. if (E->key() != edited_shape_coord) {
  747. queue_others.push_back(E->key());
  748. }
  749. }
  750. }
  751. spin_priority->set_suffix(" / " + String::num(total, 0));
  752. draw_highlight_subtile(edited_shape_coord, queue_others);
  753. } break;
  754. case EDITMODE_Z_INDEX: {
  755. spin_z_index->set_value(tileset->autotile_get_z_index(get_current_tile(), edited_shape_coord));
  756. draw_highlight_subtile(edited_shape_coord);
  757. } break;
  758. default: {}
  759. }
  760. }
  761. RID current_texture_rid = get_current_texture()->get_rid();
  762. List<int> *tiles = new List<int>();
  763. tileset->get_tile_list(tiles);
  764. for (List<int>::Element *E = tiles->front(); E; E = E->next()) {
  765. int t_id = E->get();
  766. 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)) {
  767. Rect2i region = tileset->tile_get_region(t_id);
  768. region.position += WORKSPACE_MARGIN;
  769. Color c;
  770. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE)
  771. c = COLOR_SINGLE;
  772. else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE)
  773. c = COLOR_AUTOTILE;
  774. else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE)
  775. c = COLOR_ATLAS;
  776. draw_tile_subdivision(t_id, COLOR_SUBDIVISION);
  777. workspace->draw_rect(region, c, false);
  778. }
  779. }
  780. if (edit_mode == EDITMODE_REGION) {
  781. if (workspace_mode != WORKSPACE_EDIT) {
  782. Rect2i region = edited_region;
  783. Color c;
  784. if (workspace_mode == WORKSPACE_CREATE_SINGLE)
  785. c = COLOR_SINGLE;
  786. else if (workspace_mode == WORKSPACE_CREATE_AUTOTILE)
  787. c = COLOR_AUTOTILE;
  788. else if (workspace_mode == WORKSPACE_CREATE_ATLAS)
  789. c = COLOR_ATLAS;
  790. workspace->draw_rect(region, c, false);
  791. draw_edited_region_subdivision();
  792. } else {
  793. int t_id = get_current_tile();
  794. if (t_id < 0)
  795. return;
  796. Rect2i region;
  797. if (draw_edited_region)
  798. region = edited_region;
  799. else {
  800. region = tileset->tile_get_region(t_id);
  801. region.position += WORKSPACE_MARGIN;
  802. }
  803. if (draw_edited_region)
  804. draw_edited_region_subdivision();
  805. else
  806. draw_tile_subdivision(t_id, COLOR_SUBDIVISION);
  807. Color c;
  808. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE)
  809. c = COLOR_SINGLE;
  810. else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE)
  811. c = COLOR_AUTOTILE;
  812. else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE)
  813. c = COLOR_ATLAS;
  814. workspace->draw_rect(region, c, false);
  815. }
  816. }
  817. workspace_overlay->update();
  818. }
  819. void TileSetEditor::_on_workspace_process() {
  820. if (Input::get_singleton()->is_key_pressed(KEY_ALT) || tools[VISIBLE_INFO]->is_pressed()) {
  821. if (!tile_names_visible) {
  822. tile_names_visible = true;
  823. workspace_overlay->update();
  824. }
  825. } else if (tile_names_visible) {
  826. tile_names_visible = false;
  827. workspace_overlay->update();
  828. }
  829. }
  830. void TileSetEditor::_on_workspace_overlay_draw() {
  831. if (!tileset.is_valid() || !get_current_texture().is_valid())
  832. return;
  833. const Color COLOR_AUTOTILE = Color(0.266373, 0.565288, 0.988281);
  834. const Color COLOR_SINGLE = Color(0.988281, 0.909323, 0.266373);
  835. const Color COLOR_ATLAS = Color(0.78653, 0.812835, 0.832031);
  836. if (tile_names_visible) {
  837. RID current_texture_rid = get_current_texture()->get_rid();
  838. List<int> *tiles = new List<int>();
  839. tileset->get_tile_list(tiles);
  840. for (List<int>::Element *E = tiles->front(); E; E = E->next()) {
  841. int t_id = E->get();
  842. if (tileset->tile_get_texture(t_id)->get_rid() == current_texture_rid) {
  843. Rect2i region = tileset->tile_get_region(t_id);
  844. region.position += WORKSPACE_MARGIN;
  845. region.position *= workspace->get_scale().x;
  846. Color c;
  847. if (tileset->tile_get_tile_mode(t_id) == TileSet::SINGLE_TILE)
  848. c = COLOR_SINGLE;
  849. else if (tileset->tile_get_tile_mode(t_id) == TileSet::AUTO_TILE)
  850. c = COLOR_AUTOTILE;
  851. else if (tileset->tile_get_tile_mode(t_id) == TileSet::ATLAS_TILE)
  852. c = COLOR_ATLAS;
  853. String tile_id_name = String::num(t_id, 0) + ": " + tileset->tile_get_name(t_id);
  854. Ref<Font> font = get_font("font", "Label");
  855. region.set_size(font->get_string_size(tile_id_name));
  856. workspace_overlay->draw_rect(region, c);
  857. region.position.y += region.size.y - 2;
  858. c = Color(0.1, 0.1, 0.1);
  859. workspace_overlay->draw_string(font, region.position, tile_id_name, c);
  860. }
  861. }
  862. }
  863. int t_id = get_current_tile();
  864. if (t_id < 0)
  865. return;
  866. Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  867. if (draw_handles) {
  868. for (int i = 0; i < current_shape.size(); i++) {
  869. workspace_overlay->draw_texture(handle, current_shape[i] * workspace->get_scale().x - handle->get_size() * 0.5);
  870. }
  871. }
  872. }
  873. void TileSetEditor::_on_workspace_input(const Ref<InputEvent> &p_ie) {
  874. if (tileset.is_null() || !get_current_texture().is_valid())
  875. return;
  876. static bool dragging;
  877. static bool erasing;
  878. draw_edited_region = false;
  879. Rect2 current_tile_region = Rect2();
  880. if (get_current_tile() >= 0) {
  881. current_tile_region = tileset->tile_get_region(get_current_tile());
  882. }
  883. current_tile_region.position += WORKSPACE_MARGIN;
  884. Ref<InputEventMouseButton> mb = p_ie;
  885. Ref<InputEventMouseMotion> mm = p_ie;
  886. if (mb.is_valid()) {
  887. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && !creating_shape) {
  888. if (!current_tile_region.has_point(mb->get_position())) {
  889. List<int> *tiles = new List<int>();
  890. tileset->get_tile_list(tiles);
  891. for (List<int>::Element *E = tiles->front(); E; E = E->next()) {
  892. int t_id = E->get();
  893. if (get_current_texture()->get_rid() == tileset->tile_get_texture(t_id)->get_rid()) {
  894. Rect2 r = tileset->tile_get_region(t_id);
  895. r.position += WORKSPACE_MARGIN;
  896. if (r.has_point(mb->get_position())) {
  897. set_current_tile(t_id);
  898. workspace->update();
  899. workspace_overlay->update();
  900. return;
  901. }
  902. }
  903. }
  904. }
  905. }
  906. // Mouse Wheel Event
  907. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed() && mb->get_control()) {
  908. _zoom_in();
  909. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed() && mb->get_control()) {
  910. _zoom_out();
  911. }
  912. }
  913. // Drag Middle Mouse
  914. if (mm.is_valid()) {
  915. if (mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  916. Vector2 dragged(mm->get_relative().x, mm->get_relative().y);
  917. scroll->set_h_scroll(scroll->get_h_scroll() - dragged.x * workspace->get_scale().x);
  918. scroll->set_v_scroll(scroll->get_v_scroll() - dragged.y * workspace->get_scale().x);
  919. }
  920. }
  921. if (edit_mode == EDITMODE_REGION) {
  922. if (mb.is_valid()) {
  923. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  924. if (get_current_tile() >= 0 || workspace_mode != WORKSPACE_EDIT) {
  925. dragging = true;
  926. region_from = mb->get_position();
  927. edited_region = Rect2(region_from, Size2());
  928. workspace->update();
  929. workspace_overlay->update();
  930. return;
  931. }
  932. } else if (dragging && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  933. dragging = false;
  934. edited_region = Rect2();
  935. workspace->update();
  936. workspace_overlay->update();
  937. return;
  938. } else if (dragging && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  939. dragging = false;
  940. update_edited_region(mb->get_position());
  941. edited_region.position -= WORKSPACE_MARGIN;
  942. if (!edited_region.has_no_area()) {
  943. if (get_current_tile() >= 0 && workspace_mode == WORKSPACE_EDIT) {
  944. undo_redo->create_action(TTR("Set Tile Region"));
  945. undo_redo->add_do_method(tileset.ptr(), "tile_set_region", get_current_tile(), edited_region);
  946. undo_redo->add_undo_method(tileset.ptr(), "tile_set_region", get_current_tile(), tileset->tile_get_region(get_current_tile()));
  947. Size2 tile_workspace_size = edited_region.position + edited_region.size + WORKSPACE_MARGIN * 2;
  948. Size2 workspace_minsize = workspace->get_custom_minimum_size();
  949. if (tile_workspace_size.x > workspace_minsize.x && tile_workspace_size.y > workspace_minsize.y) {
  950. undo_redo->add_do_method(workspace, "set_custom_minimum_size", tile_workspace_size);
  951. undo_redo->add_undo_method(workspace, "set_custom_minimum_size", workspace_minsize);
  952. undo_redo->add_do_method(workspace_container, "set_custom_minimum_size", tile_workspace_size);
  953. undo_redo->add_undo_method(workspace_container, "set_custom_minimum_size", workspace_minsize);
  954. undo_redo->add_do_method(workspace_overlay, "set_custom_minimum_size", tile_workspace_size);
  955. undo_redo->add_undo_method(workspace_overlay, "set_custom_minimum_size", workspace_minsize);
  956. } 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) {
  957. undo_redo->add_do_method(this, "update_workspace_minsize");
  958. undo_redo->add_undo_method(this, "update_workspace_minsize");
  959. }
  960. edited_region = Rect2();
  961. undo_redo->add_do_method(workspace, "update");
  962. undo_redo->add_undo_method(workspace, "update");
  963. undo_redo->add_do_method(workspace_overlay, "update");
  964. undo_redo->add_undo_method(workspace_overlay, "update");
  965. undo_redo->commit_action();
  966. } else {
  967. int t_id = tileset->get_last_unused_tile_id();
  968. undo_redo->create_action(TTR("Create Tile"));
  969. undo_redo->add_do_method(tileset.ptr(), "create_tile", t_id);
  970. undo_redo->add_undo_method(tileset.ptr(), "remove_tile", t_id);
  971. undo_redo->add_undo_method(this, "_validate_current_tile_id");
  972. undo_redo->add_do_method(tileset.ptr(), "tile_set_texture", t_id, get_current_texture());
  973. undo_redo->add_do_method(tileset.ptr(), "tile_set_region", t_id, edited_region);
  974. undo_redo->add_do_method(tileset.ptr(), "tile_set_name", t_id, get_current_texture()->get_path().get_file() + " " + String::num(t_id, 0));
  975. if (workspace_mode != WORKSPACE_CREATE_SINGLE) {
  976. undo_redo->add_do_method(tileset.ptr(), "autotile_set_size", t_id, snap_step);
  977. undo_redo->add_do_method(tileset.ptr(), "autotile_set_spacing", t_id, snap_separation.x);
  978. undo_redo->add_do_method(tileset.ptr(), "tile_set_tile_mode", t_id, workspace_mode == WORKSPACE_CREATE_AUTOTILE ? TileSet::AUTO_TILE : TileSet::ATLAS_TILE);
  979. }
  980. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  981. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  982. edit_mode = EDITMODE_COLLISION;
  983. Size2 tile_workspace_size = edited_region.position + edited_region.size + WORKSPACE_MARGIN * 2;
  984. Size2 workspace_minsize = workspace->get_custom_minimum_size();
  985. if (tile_workspace_size.x > workspace_minsize.x || tile_workspace_size.y > workspace_minsize.y) {
  986. Size2 new_workspace_minsize = Size2(MAX(tile_workspace_size.x, workspace_minsize.x), MAX(tile_workspace_size.y, workspace_minsize.y));
  987. undo_redo->add_do_method(workspace, "set_custom_minimum_size", new_workspace_minsize);
  988. undo_redo->add_undo_method(workspace, "set_custom_minimum_size", workspace_minsize);
  989. undo_redo->add_do_method(workspace_container, "set_custom_minimum_size", new_workspace_minsize);
  990. undo_redo->add_undo_method(workspace_container, "set_custom_minimum_size", workspace_minsize);
  991. undo_redo->add_do_method(workspace_overlay, "set_custom_minimum_size", new_workspace_minsize);
  992. undo_redo->add_undo_method(workspace_overlay, "set_custom_minimum_size", workspace_minsize);
  993. }
  994. edited_region = Rect2();
  995. undo_redo->add_do_method(workspace, "update");
  996. undo_redo->add_undo_method(workspace, "update");
  997. undo_redo->add_do_method(workspace_overlay, "update");
  998. undo_redo->add_undo_method(workspace_overlay, "update");
  999. undo_redo->commit_action();
  1000. set_current_tile(t_id);
  1001. _on_workspace_mode_changed(WORKSPACE_EDIT);
  1002. }
  1003. } else {
  1004. edited_region = Rect2();
  1005. workspace->update();
  1006. workspace_overlay->update();
  1007. }
  1008. return;
  1009. }
  1010. } else if (mm.is_valid()) {
  1011. if (dragging) {
  1012. update_edited_region(mm->get_position());
  1013. draw_edited_region = true;
  1014. workspace->update();
  1015. workspace_overlay->update();
  1016. return;
  1017. }
  1018. }
  1019. }
  1020. if (workspace_mode == WORKSPACE_EDIT) {
  1021. if (get_current_tile() >= 0) {
  1022. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1023. Vector2 size = tileset->autotile_get_size(get_current_tile());
  1024. switch (edit_mode) {
  1025. case EDITMODE_ICON: {
  1026. if (mb.is_valid()) {
  1027. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && current_tile_region.has_point(mb->get_position())) {
  1028. 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)));
  1029. undo_redo->create_action(TTR("Set Tile Icon"));
  1030. undo_redo->add_do_method(tileset.ptr(), "autotile_set_icon_coordinate", get_current_tile(), coord);
  1031. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_icon_coordinate", get_current_tile(), tileset->autotile_get_icon_coordinate(get_current_tile()));
  1032. undo_redo->add_do_method(workspace, "update");
  1033. undo_redo->add_undo_method(workspace, "update");
  1034. undo_redo->commit_action();
  1035. }
  1036. }
  1037. } break;
  1038. case EDITMODE_BITMASK: {
  1039. if (mb.is_valid()) {
  1040. if (mb->is_pressed()) {
  1041. if (dragging) {
  1042. return;
  1043. }
  1044. if ((mb->get_button_index() == BUTTON_RIGHT || mb->get_button_index() == BUTTON_LEFT) && current_tile_region.has_point(mb->get_position())) {
  1045. dragging = true;
  1046. erasing = (mb->get_button_index() == BUTTON_RIGHT);
  1047. 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)));
  1048. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  1049. pos = mb->get_position() - (pos + current_tile_region.position);
  1050. uint16_t bit = 0;
  1051. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  1052. if (pos.x < size.x / 2) {
  1053. if (pos.y < size.y / 2) {
  1054. bit = TileSet::BIND_TOPLEFT;
  1055. } else {
  1056. bit = TileSet::BIND_BOTTOMLEFT;
  1057. }
  1058. } else {
  1059. if (pos.y < size.y / 2) {
  1060. bit = TileSet::BIND_TOPRIGHT;
  1061. } else {
  1062. bit = TileSet::BIND_BOTTOMRIGHT;
  1063. }
  1064. }
  1065. } else {
  1066. if (pos.x < size.x / 3) {
  1067. if (pos.y < size.y / 3) {
  1068. bit = TileSet::BIND_TOPLEFT;
  1069. } else if (pos.y > (size.y / 3) * 2) {
  1070. bit = TileSet::BIND_BOTTOMLEFT;
  1071. } else {
  1072. bit = TileSet::BIND_LEFT;
  1073. }
  1074. } else if (pos.x > (size.x / 3) * 2) {
  1075. if (pos.y < size.y / 3) {
  1076. bit = TileSet::BIND_TOPRIGHT;
  1077. } else if (pos.y > (size.y / 3) * 2) {
  1078. bit = TileSet::BIND_BOTTOMRIGHT;
  1079. } else {
  1080. bit = TileSet::BIND_RIGHT;
  1081. }
  1082. } else {
  1083. if (pos.y < size.y / 3) {
  1084. bit = TileSet::BIND_TOP;
  1085. } else if (pos.y > (size.y / 3) * 2) {
  1086. bit = TileSet::BIND_BOTTOM;
  1087. } else {
  1088. bit = TileSet::BIND_CENTER;
  1089. }
  1090. }
  1091. }
  1092. uint16_t old_mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  1093. uint16_t new_mask = old_mask;
  1094. if (erasing) {
  1095. new_mask &= ~bit;
  1096. } else {
  1097. new_mask |= bit;
  1098. }
  1099. if (old_mask != new_mask) {
  1100. undo_redo->create_action(TTR("Edit Tile Bitmask"));
  1101. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, new_mask);
  1102. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, old_mask);
  1103. undo_redo->add_do_method(workspace, "update");
  1104. undo_redo->add_undo_method(workspace, "update");
  1105. undo_redo->commit_action();
  1106. }
  1107. }
  1108. } else {
  1109. if ((erasing && mb->get_button_index() == BUTTON_RIGHT) || (!erasing && mb->get_button_index() == BUTTON_LEFT)) {
  1110. dragging = false;
  1111. erasing = false;
  1112. }
  1113. }
  1114. }
  1115. if (mm.is_valid()) {
  1116. if (dragging && current_tile_region.has_point(mm->get_position())) {
  1117. 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)));
  1118. Vector2 pos(coord.x * (spacing + size.x), coord.y * (spacing + size.y));
  1119. pos = mm->get_position() - (pos + current_tile_region.position);
  1120. uint16_t bit = 0;
  1121. if (tileset->autotile_get_bitmask_mode(get_current_tile()) == TileSet::BITMASK_2X2) {
  1122. if (pos.x < size.x / 2) {
  1123. if (pos.y < size.y / 2) {
  1124. bit = TileSet::BIND_TOPLEFT;
  1125. } else {
  1126. bit = TileSet::BIND_BOTTOMLEFT;
  1127. }
  1128. } else {
  1129. if (pos.y < size.y / 2) {
  1130. bit = TileSet::BIND_TOPRIGHT;
  1131. } else {
  1132. bit = TileSet::BIND_BOTTOMRIGHT;
  1133. }
  1134. }
  1135. } else {
  1136. if (pos.x < size.x / 3) {
  1137. if (pos.y < size.y / 3) {
  1138. bit = TileSet::BIND_TOPLEFT;
  1139. } else if (pos.y > (size.y / 3) * 2) {
  1140. bit = TileSet::BIND_BOTTOMLEFT;
  1141. } else {
  1142. bit = TileSet::BIND_LEFT;
  1143. }
  1144. } else if (pos.x > (size.x / 3) * 2) {
  1145. if (pos.y < size.y / 3) {
  1146. bit = TileSet::BIND_TOPRIGHT;
  1147. } else if (pos.y > (size.y / 3) * 2) {
  1148. bit = TileSet::BIND_BOTTOMRIGHT;
  1149. } else {
  1150. bit = TileSet::BIND_RIGHT;
  1151. }
  1152. } else {
  1153. if (pos.y < size.y / 3) {
  1154. bit = TileSet::BIND_TOP;
  1155. } else if (pos.y > (size.y / 3) * 2) {
  1156. bit = TileSet::BIND_BOTTOM;
  1157. } else {
  1158. bit = TileSet::BIND_CENTER;
  1159. }
  1160. }
  1161. }
  1162. uint16_t old_mask = tileset->autotile_get_bitmask(get_current_tile(), coord);
  1163. uint16_t new_mask = old_mask;
  1164. if (erasing) {
  1165. new_mask &= ~bit;
  1166. } else {
  1167. new_mask |= bit;
  1168. }
  1169. if (old_mask != new_mask) {
  1170. undo_redo->create_action(TTR("Edit Tile Bitmask"));
  1171. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, new_mask);
  1172. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), coord, old_mask);
  1173. undo_redo->add_do_method(workspace, "update");
  1174. undo_redo->add_undo_method(workspace, "update");
  1175. undo_redo->commit_action();
  1176. }
  1177. }
  1178. }
  1179. } break;
  1180. case EDITMODE_COLLISION:
  1181. case EDITMODE_OCCLUSION:
  1182. case EDITMODE_NAVIGATION:
  1183. case EDITMODE_PRIORITY:
  1184. case EDITMODE_Z_INDEX: {
  1185. Vector2 shape_anchor = Vector2(0, 0);
  1186. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  1187. shape_anchor = edited_shape_coord;
  1188. shape_anchor.x *= (size.x + spacing);
  1189. shape_anchor.y *= (size.y + spacing);
  1190. }
  1191. const real_t grab_threshold = EDITOR_GET("editors/poly_editor/point_grab_radius");
  1192. shape_anchor += current_tile_region.position;
  1193. if (tools[TOOL_SELECT]->is_pressed()) {
  1194. if (mb.is_valid()) {
  1195. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1196. if (edit_mode != EDITMODE_PRIORITY && current_shape.size() > 0) {
  1197. for (int i = 0; i < current_shape.size(); i++) {
  1198. if ((current_shape[i] - mb->get_position()).length_squared() <= grab_threshold) {
  1199. dragging_point = i;
  1200. workspace->update();
  1201. return;
  1202. }
  1203. }
  1204. }
  1205. 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())) {
  1206. 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)));
  1207. if (edited_shape_coord != coord) {
  1208. edited_shape_coord = coord;
  1209. _select_edited_shape_coord();
  1210. }
  1211. }
  1212. workspace->update();
  1213. } else if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1214. if (edit_mode == EDITMODE_COLLISION) {
  1215. if (dragging_point >= 0) {
  1216. dragging_point = -1;
  1217. Vector<Vector2> points;
  1218. for (int i = 0; i < current_shape.size(); i++) {
  1219. Vector2 p = current_shape[i];
  1220. if (tools[TOOL_GRID_SNAP]->is_pressed() || tools[SHAPE_KEEP_INSIDE_TILE]->is_pressed()) {
  1221. p = snap_point(p);
  1222. }
  1223. points.push_back(p - shape_anchor);
  1224. }
  1225. undo_redo->create_action(TTR("Edit Collision Polygon"));
  1226. _set_edited_shape_points(points);
  1227. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1228. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1229. undo_redo->commit_action();
  1230. }
  1231. } else if (edit_mode == EDITMODE_OCCLUSION) {
  1232. if (dragging_point >= 0) {
  1233. dragging_point = -1;
  1234. PoolVector<Vector2> polygon;
  1235. polygon.resize(current_shape.size());
  1236. PoolVector<Vector2>::Write w = polygon.write();
  1237. for (int i = 0; i < current_shape.size(); i++) {
  1238. w[i] = current_shape[i] - shape_anchor;
  1239. }
  1240. w = PoolVector<Vector2>::Write();
  1241. undo_redo->create_action(TTR("Edit Occlusion Polygon"));
  1242. undo_redo->add_do_method(edited_occlusion_shape.ptr(), "set_polygon", polygon);
  1243. undo_redo->add_undo_method(edited_occlusion_shape.ptr(), "set_polygon", edited_occlusion_shape->get_polygon());
  1244. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1245. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1246. undo_redo->commit_action();
  1247. }
  1248. } else if (edit_mode == EDITMODE_NAVIGATION) {
  1249. if (dragging_point >= 0) {
  1250. dragging_point = -1;
  1251. PoolVector<Vector2> polygon;
  1252. Vector<int> indices;
  1253. polygon.resize(current_shape.size());
  1254. PoolVector<Vector2>::Write w = polygon.write();
  1255. for (int i = 0; i < current_shape.size(); i++) {
  1256. w[i] = current_shape[i] - shape_anchor;
  1257. indices.push_back(i);
  1258. }
  1259. w = PoolVector<Vector2>::Write();
  1260. undo_redo->create_action(TTR("Edit Navigation Polygon"));
  1261. undo_redo->add_do_method(edited_navigation_shape.ptr(), "set_vertices", polygon);
  1262. undo_redo->add_undo_method(edited_navigation_shape.ptr(), "set_vertices", edited_navigation_shape->get_vertices());
  1263. undo_redo->add_do_method(edited_navigation_shape.ptr(), "add_polygon", indices);
  1264. undo_redo->add_undo_method(edited_navigation_shape.ptr(), "add_polygon", edited_navigation_shape->get_polygon(0));
  1265. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1266. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1267. undo_redo->commit_action();
  1268. }
  1269. }
  1270. }
  1271. } else if (mm.is_valid()) {
  1272. if (dragging_point >= 0) {
  1273. current_shape.set(dragging_point, snap_point(mm->get_position()));
  1274. workspace->update();
  1275. }
  1276. }
  1277. } else if (tools[SHAPE_NEW_POLYGON]->is_pressed()) {
  1278. if (mb.is_valid()) {
  1279. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1280. Vector2 pos = mb->get_position();
  1281. pos = snap_point(pos);
  1282. if (creating_shape) {
  1283. if (current_shape.size() > 0) {
  1284. if ((pos - current_shape[0]).length_squared() <= grab_threshold) {
  1285. if (current_shape.size() > 2) {
  1286. close_shape(shape_anchor);
  1287. workspace->update();
  1288. return;
  1289. }
  1290. }
  1291. }
  1292. current_shape.push_back(pos);
  1293. workspace->update();
  1294. } else {
  1295. creating_shape = true;
  1296. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>());
  1297. current_shape.resize(0);
  1298. current_shape.push_back(snap_point(pos));
  1299. workspace->update();
  1300. }
  1301. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1302. if (creating_shape) {
  1303. creating_shape = false;
  1304. _select_edited_shape_coord();
  1305. workspace->update();
  1306. }
  1307. }
  1308. } else if (mm.is_valid()) {
  1309. if (creating_shape) {
  1310. workspace->update();
  1311. }
  1312. }
  1313. } else if (tools[SHAPE_NEW_RECTANGLE]->is_pressed()) {
  1314. if (mb.is_valid()) {
  1315. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1316. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>());
  1317. current_shape.resize(0);
  1318. current_shape.push_back(snap_point(shape_anchor));
  1319. current_shape.push_back(snap_point(shape_anchor + Vector2(current_tile_region.size.x, 0)));
  1320. current_shape.push_back(snap_point(shape_anchor + current_tile_region.size));
  1321. current_shape.push_back(snap_point(shape_anchor + Vector2(0, current_tile_region.size.y)));
  1322. close_shape(shape_anchor);
  1323. workspace->update();
  1324. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1325. workspace->update();
  1326. }
  1327. }
  1328. }
  1329. } break;
  1330. default: {}
  1331. }
  1332. }
  1333. }
  1334. }
  1335. void TileSetEditor::_on_tool_clicked(int p_tool) {
  1336. if (p_tool == BITMASK_COPY) {
  1337. bitmask_map_copy = tileset->autotile_get_bitmask_map(get_current_tile());
  1338. } else if (p_tool == BITMASK_PASTE) {
  1339. undo_redo->create_action(TTR("Paste Tile Bitmask"));
  1340. undo_redo->add_do_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1341. undo_redo->add_undo_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1342. for (Map<Vector2, uint16_t>::Element *E = bitmask_map_copy.front(); E; E = E->next()) {
  1343. undo_redo->add_do_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1344. }
  1345. for (Map<Vector2, uint16_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  1346. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1347. }
  1348. undo_redo->add_do_method(workspace, "update");
  1349. undo_redo->add_undo_method(workspace, "update");
  1350. undo_redo->commit_action();
  1351. } else if (p_tool == BITMASK_CLEAR) {
  1352. undo_redo->create_action(TTR("Clear Tile Bitmask"));
  1353. undo_redo->add_do_method(tileset.ptr(), "autotile_clear_bitmask_map", get_current_tile());
  1354. for (Map<Vector2, uint16_t>::Element *E = tileset->autotile_get_bitmask_map(get_current_tile()).front(); E; E = E->next()) {
  1355. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", get_current_tile(), E->key(), E->value());
  1356. }
  1357. undo_redo->add_do_method(workspace, "update");
  1358. undo_redo->add_undo_method(workspace, "update");
  1359. undo_redo->commit_action();
  1360. } else if (p_tool == SHAPE_TOGGLE_TYPE) {
  1361. if (edited_collision_shape.is_valid()) {
  1362. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1363. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1364. Ref<Shape2D> previous_shape = edited_collision_shape;
  1365. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  1366. if (convex.is_valid()) {
  1367. // Make concave
  1368. undo_redo->create_action(TTR("Make Polygon Concave"));
  1369. Ref<ConcavePolygonShape2D> _concave = memnew(ConcavePolygonShape2D);
  1370. edited_collision_shape = _concave;
  1371. _set_edited_shape_points(_get_collision_shape_points(convex));
  1372. } else if (concave.is_valid()) {
  1373. // Make convex
  1374. undo_redo->create_action(TTR("Make Polygon Convex"));
  1375. Ref<ConvexPolygonShape2D> _convex = memnew(ConvexPolygonShape2D);
  1376. edited_collision_shape = _convex;
  1377. _set_edited_shape_points(_get_collision_shape_points(concave));
  1378. } else {
  1379. // Shoudn't haphen
  1380. }
  1381. for (int i = 0; i < sd.size(); i++) {
  1382. if (sd[i].get("shape") == previous_shape) {
  1383. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  1384. sd.remove(i);
  1385. sd.insert(i, edited_collision_shape);
  1386. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  1387. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1388. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1389. undo_redo->commit_action();
  1390. break;
  1391. }
  1392. }
  1393. _update_toggle_shape_button();
  1394. workspace->update();
  1395. workspace_container->update();
  1396. helper->_change_notify("");
  1397. }
  1398. } else if (p_tool == SELECT_NEXT) {
  1399. _select_next_shape();
  1400. } else if (p_tool == SELECT_PREVIOUS) {
  1401. _select_previous_shape();
  1402. } else if (p_tool == SHAPE_DELETE) {
  1403. if (creating_shape) {
  1404. creating_shape = false;
  1405. current_shape.resize(0);
  1406. workspace->update();
  1407. } else {
  1408. switch (edit_mode) {
  1409. case EDITMODE_REGION: {
  1410. int t_id = get_current_tile();
  1411. if (workspace_mode == WORKSPACE_EDIT && t_id >= 0) {
  1412. undo_redo->create_action(TTR("Remove Tile"));
  1413. undo_redo->add_do_method(tileset.ptr(), "remove_tile", t_id);
  1414. _undo_tile_removal(t_id);
  1415. undo_redo->add_do_method(this, "_validate_current_tile_id");
  1416. Rect2 tile_region = tileset->tile_get_region(get_current_tile());
  1417. Size2 tile_workspace_size = tile_region.position + tile_region.size;
  1418. if (tile_workspace_size.x > get_current_texture()->get_size().x || tile_workspace_size.y > get_current_texture()->get_size().y) {
  1419. undo_redo->add_do_method(this, "update_workspace_minsize");
  1420. undo_redo->add_undo_method(this, "update_workspace_minsize");
  1421. }
  1422. undo_redo->add_do_method(workspace, "update");
  1423. undo_redo->add_undo_method(workspace, "update");
  1424. undo_redo->add_do_method(workspace_overlay, "update");
  1425. undo_redo->add_undo_method(workspace_overlay, "update");
  1426. undo_redo->commit_action();
  1427. }
  1428. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  1429. workspace_mode = WORKSPACE_EDIT;
  1430. update_workspace_tile_mode();
  1431. } break;
  1432. case EDITMODE_COLLISION: {
  1433. if (!edited_collision_shape.is_null()) {
  1434. // Necessary to get the version that returns a Array instead of a Vector.
  1435. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  1436. for (int i = 0; i < sd.size(); i++) {
  1437. if (sd[i].get("shape") == edited_collision_shape) {
  1438. undo_redo->create_action(TTR("Remove Collision Polygon"));
  1439. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  1440. sd.remove(i);
  1441. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  1442. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1443. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1444. undo_redo->commit_action();
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. } break;
  1450. case EDITMODE_OCCLUSION: {
  1451. if (!edited_occlusion_shape.is_null()) {
  1452. undo_redo->create_action(TTR("Remove Occlusion Polygon"));
  1453. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1454. undo_redo->add_do_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), Ref<OccluderPolygon2D>());
  1455. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), tileset->tile_get_light_occluder(get_current_tile()));
  1456. } else {
  1457. undo_redo->add_do_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), Ref<OccluderPolygon2D>(), edited_shape_coord);
  1458. 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);
  1459. }
  1460. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1461. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1462. undo_redo->commit_action();
  1463. }
  1464. } break;
  1465. case EDITMODE_NAVIGATION: {
  1466. if (!edited_navigation_shape.is_null()) {
  1467. undo_redo->create_action(TTR("Remove Navigation Polygon"));
  1468. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1469. undo_redo->add_do_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), Ref<NavigationPolygon>());
  1470. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), tileset->tile_get_navigation_polygon(get_current_tile()));
  1471. } else {
  1472. undo_redo->add_do_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), Ref<NavigationPolygon>(), edited_shape_coord);
  1473. 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);
  1474. }
  1475. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  1476. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  1477. undo_redo->commit_action();
  1478. }
  1479. } break;
  1480. default: {}
  1481. }
  1482. }
  1483. } else if (p_tool == TOOL_SELECT) {
  1484. if (creating_shape) {
  1485. // Cancel Creation
  1486. creating_shape = false;
  1487. current_shape.resize(0);
  1488. workspace->update();
  1489. }
  1490. }
  1491. }
  1492. void TileSetEditor::_on_priority_changed(float val) {
  1493. if ((int)val == tileset->autotile_get_subtile_priority(get_current_tile(), edited_shape_coord))
  1494. return;
  1495. undo_redo->create_action(TTR("Edit Tile Priority"));
  1496. undo_redo->add_do_method(tileset.ptr(), "autotile_set_subtile_priority", get_current_tile(), edited_shape_coord, (int)val);
  1497. 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));
  1498. undo_redo->add_do_method(workspace, "update");
  1499. undo_redo->add_undo_method(workspace, "update");
  1500. undo_redo->commit_action();
  1501. }
  1502. void TileSetEditor::_on_z_index_changed(float val) {
  1503. if ((int)val == tileset->autotile_get_z_index(get_current_tile(), edited_shape_coord))
  1504. return;
  1505. undo_redo->create_action(TTR("Edit Tile Z Index"));
  1506. undo_redo->add_do_method(tileset.ptr(), "autotile_set_z_index", get_current_tile(), edited_shape_coord, (int)val);
  1507. 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));
  1508. undo_redo->add_do_method(workspace, "update");
  1509. undo_redo->add_undo_method(workspace, "update");
  1510. undo_redo->commit_action();
  1511. }
  1512. void TileSetEditor::_on_grid_snap_toggled(bool p_val) {
  1513. helper->set_snap_options_visible(p_val);
  1514. workspace->update();
  1515. }
  1516. Vector<Vector2> TileSetEditor::_get_collision_shape_points(const Ref<Shape2D> &p_shape) {
  1517. Ref<ConvexPolygonShape2D> convex = p_shape;
  1518. Ref<ConcavePolygonShape2D> concave = p_shape;
  1519. if (convex.is_valid()) {
  1520. return convex->get_points();
  1521. } else if (concave.is_valid()) {
  1522. Vector<Vector2> points;
  1523. for (int i = 0; i < concave->get_segments().size(); i += 2) {
  1524. points.push_back(concave->get_segments()[i]);
  1525. }
  1526. return points;
  1527. } else {
  1528. return Vector<Vector2>();
  1529. }
  1530. }
  1531. Vector<Vector2> TileSetEditor::_get_edited_shape_points() {
  1532. return _get_collision_shape_points(edited_collision_shape);
  1533. }
  1534. void TileSetEditor::_set_edited_shape_points(const Vector<Vector2> points) {
  1535. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1536. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1537. if (convex.is_valid()) {
  1538. undo_redo->add_do_method(convex.ptr(), "set_points", points);
  1539. undo_redo->add_undo_method(convex.ptr(), "set_points", _get_edited_shape_points());
  1540. } else if (concave.is_valid()) {
  1541. PoolVector2Array segments;
  1542. for (int i = 0; i < points.size() - 1; i++) {
  1543. segments.push_back(points[i]);
  1544. segments.push_back(points[i + 1]);
  1545. }
  1546. segments.push_back(points[points.size() - 1]);
  1547. segments.push_back(points[0]);
  1548. concave->set_segments(segments);
  1549. undo_redo->add_do_method(concave.ptr(), "set_segments", segments);
  1550. undo_redo->add_undo_method(concave.ptr(), "set_segments", concave->get_segments());
  1551. } else {
  1552. // Invalid shape
  1553. }
  1554. }
  1555. void TileSetEditor::_update_tile_data() {
  1556. current_tile_data.clear();
  1557. if (get_current_tile() < 0)
  1558. return;
  1559. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(get_current_tile());
  1560. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1561. SubtileData data;
  1562. for (int i = 0; i < sd.size(); i++) {
  1563. data.collisions.push_back(sd[i].shape);
  1564. }
  1565. data.navigation_shape = tileset->tile_get_navigation_polygon(get_current_tile());
  1566. data.occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  1567. current_tile_data[Vector2i()] = data;
  1568. } else {
  1569. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1570. Vector2 size = tileset->tile_get_region(get_current_tile()).size;
  1571. Vector2i cell_count = size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing));
  1572. for (int y = 0; y < cell_count.y; y++) {
  1573. for (int x = 0; x < cell_count.x; x++) {
  1574. SubtileData data;
  1575. Vector2i coord(x, y);
  1576. for (int i = 0; i < sd.size(); i++) {
  1577. if (sd[i].autotile_coord == coord) {
  1578. data.collisions.push_back(sd[i].shape);
  1579. }
  1580. }
  1581. data.navigation_shape = tileset->autotile_get_navigation_polygon(get_current_tile(), coord);
  1582. data.occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  1583. current_tile_data[coord] = data;
  1584. }
  1585. }
  1586. }
  1587. }
  1588. void TileSetEditor::_update_toggle_shape_button() {
  1589. Ref<ConvexPolygonShape2D> convex = edited_collision_shape;
  1590. Ref<ConcavePolygonShape2D> concave = edited_collision_shape;
  1591. separator_shape_toggle->show();
  1592. tools[SHAPE_TOGGLE_TYPE]->show();
  1593. if (edit_mode != EDITMODE_COLLISION || !edited_collision_shape.is_valid()) {
  1594. separator_shape_toggle->hide();
  1595. tools[SHAPE_TOGGLE_TYPE]->hide();
  1596. } else if (concave.is_valid()) {
  1597. tools[SHAPE_TOGGLE_TYPE]->set_icon(get_icon("ConvexPolygonShape2D", "EditorIcons"));
  1598. tools[SHAPE_TOGGLE_TYPE]->set_text("Make Convex");
  1599. } else if (convex.is_valid()) {
  1600. tools[SHAPE_TOGGLE_TYPE]->set_icon(get_icon("ConcavePolygonShape2D", "EditorIcons"));
  1601. tools[SHAPE_TOGGLE_TYPE]->set_text("Make Concave");
  1602. } else {
  1603. // Shoudn't happen
  1604. separator_shape_toggle->hide();
  1605. tools[SHAPE_TOGGLE_TYPE]->hide();
  1606. }
  1607. }
  1608. void TileSetEditor::_select_next_tile() {
  1609. Array tiles = _get_tiles_in_current_texture(true);
  1610. if (tiles.size() == 0) {
  1611. set_current_tile(-1);
  1612. } else if (get_current_tile() == -1) {
  1613. set_current_tile(tiles[0]);
  1614. } else {
  1615. int index = tiles.find(get_current_tile());
  1616. if (index < 0) {
  1617. set_current_tile(tiles[0]);
  1618. } else if (index == tiles.size() - 1) {
  1619. set_current_tile(tiles[0]);
  1620. } else {
  1621. set_current_tile(tiles[index + 1]);
  1622. }
  1623. }
  1624. if (get_current_tile() == -1) {
  1625. return;
  1626. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1627. return;
  1628. } else {
  1629. switch (edit_mode) {
  1630. case EDITMODE_COLLISION:
  1631. case EDITMODE_OCCLUSION:
  1632. case EDITMODE_NAVIGATION:
  1633. case EDITMODE_PRIORITY:
  1634. case EDITMODE_Z_INDEX: {
  1635. edited_shape_coord = Vector2();
  1636. _select_edited_shape_coord();
  1637. } break;
  1638. default: {}
  1639. }
  1640. }
  1641. }
  1642. void TileSetEditor::_select_previous_tile() {
  1643. Array tiles = _get_tiles_in_current_texture(true);
  1644. if (tiles.size() == 0) {
  1645. set_current_tile(-1);
  1646. } else if (get_current_tile() == -1) {
  1647. set_current_tile(tiles[tiles.size() - 1]);
  1648. } else {
  1649. int index = tiles.find(get_current_tile());
  1650. if (index <= 0) {
  1651. set_current_tile(tiles[tiles.size() - 1]);
  1652. } else {
  1653. set_current_tile(tiles[index - 1]);
  1654. }
  1655. }
  1656. if (get_current_tile() == -1) {
  1657. return;
  1658. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1659. return;
  1660. } else {
  1661. switch (edit_mode) {
  1662. case EDITMODE_COLLISION:
  1663. case EDITMODE_OCCLUSION:
  1664. case EDITMODE_NAVIGATION:
  1665. case EDITMODE_PRIORITY:
  1666. case EDITMODE_Z_INDEX: {
  1667. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1668. Vector2 size = tileset->tile_get_region(get_current_tile()).size;
  1669. Vector2i cell_count = size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing));
  1670. cell_count -= Vector2(1, 1);
  1671. edited_shape_coord = cell_count;
  1672. _select_edited_shape_coord();
  1673. } break;
  1674. default: {}
  1675. }
  1676. }
  1677. }
  1678. Array TileSetEditor::_get_tiles_in_current_texture(bool sorted) {
  1679. Array a;
  1680. List<int> all_tiles;
  1681. if (!get_current_texture().is_valid()) {
  1682. return a;
  1683. }
  1684. tileset->get_tile_list(&all_tiles);
  1685. for (int i = 0; i < all_tiles.size(); i++) {
  1686. if (tileset->tile_get_texture(all_tiles[i]) == get_current_texture()) {
  1687. a.push_back(all_tiles[i]);
  1688. }
  1689. }
  1690. if (sorted) {
  1691. a.sort_custom(this, "_sort_tiles");
  1692. }
  1693. return a;
  1694. }
  1695. bool TileSetEditor::_sort_tiles(Variant p_a, Variant p_b) {
  1696. int a = p_a;
  1697. int b = p_b;
  1698. Vector2 pos_a = tileset->tile_get_region(a).position;
  1699. Vector2 pos_b = tileset->tile_get_region(b).position;
  1700. if (pos_a.y < pos_b.y) {
  1701. return true;
  1702. } else if (pos_a.y == pos_b.y) {
  1703. if (pos_a.x < pos_b.x) {
  1704. return true;
  1705. } else {
  1706. return false;
  1707. }
  1708. } else {
  1709. return false;
  1710. }
  1711. }
  1712. void TileSetEditor::_select_next_subtile() {
  1713. if (get_current_tile() == -1) {
  1714. _select_next_tile();
  1715. return;
  1716. }
  1717. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1718. _select_next_tile();
  1719. } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) {
  1720. _select_next_tile();
  1721. } else {
  1722. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1723. Vector2 size = tileset->tile_get_region(get_current_tile()).size;
  1724. Vector2i cell_count = size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing));
  1725. if (edited_shape_coord.x >= cell_count.x - 1 && edited_shape_coord.y >= cell_count.y - 1) {
  1726. _select_next_tile();
  1727. } else {
  1728. edited_shape_coord.x++;
  1729. if (edited_shape_coord.x >= cell_count.x) {
  1730. edited_shape_coord.x = 0;
  1731. edited_shape_coord.y++;
  1732. }
  1733. _select_edited_shape_coord();
  1734. }
  1735. }
  1736. }
  1737. void TileSetEditor::_select_previous_subtile() {
  1738. if (get_current_tile() == -1) {
  1739. _select_previous_tile();
  1740. return;
  1741. }
  1742. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  1743. _select_previous_tile();
  1744. } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) {
  1745. _select_previous_tile();
  1746. } else {
  1747. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1748. Vector2 size = tileset->tile_get_region(get_current_tile()).size;
  1749. Vector2i cell_count = size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing));
  1750. if (edited_shape_coord.x <= 0 && edited_shape_coord.y <= 0) {
  1751. _select_previous_tile();
  1752. } else {
  1753. edited_shape_coord.x--;
  1754. if (edited_shape_coord.x == -1) {
  1755. edited_shape_coord.x = cell_count.x - 1;
  1756. edited_shape_coord.y--;
  1757. }
  1758. _select_edited_shape_coord();
  1759. }
  1760. }
  1761. }
  1762. void TileSetEditor::_select_next_shape() {
  1763. if (get_current_tile() == -1) {
  1764. _select_next_subtile();
  1765. } else if (edit_mode != EDITMODE_COLLISION) {
  1766. _select_next_subtile();
  1767. } else {
  1768. Vector2i edited_coord = Vector2();
  1769. if (tileset->tile_get_tile_mode(get_current_tile()) != TileSet::SINGLE_TILE) {
  1770. edited_coord = edited_shape_coord;
  1771. }
  1772. SubtileData data = current_tile_data[edited_coord];
  1773. if (data.collisions.size() == 0) {
  1774. _select_next_subtile();
  1775. } else {
  1776. int index = data.collisions.find(edited_collision_shape);
  1777. if (index < 0) {
  1778. _set_edited_collision_shape(data.collisions[0]);
  1779. } else if (index == data.collisions.size() - 1) {
  1780. _select_next_subtile();
  1781. } else {
  1782. _set_edited_collision_shape(data.collisions[index + 1]);
  1783. }
  1784. }
  1785. current_shape.resize(0);
  1786. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  1787. current_tile_region.position += WORKSPACE_MARGIN;
  1788. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1789. Vector2 size = tileset->autotile_get_size(get_current_tile());
  1790. Vector2 shape_anchor = edited_shape_coord;
  1791. shape_anchor.x *= (size.x + spacing);
  1792. shape_anchor.y *= (size.y + spacing);
  1793. current_tile_region.position += shape_anchor;
  1794. if (edited_collision_shape.is_valid()) {
  1795. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  1796. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  1797. }
  1798. }
  1799. workspace->update();
  1800. workspace_container->update();
  1801. helper->_change_notify("");
  1802. }
  1803. }
  1804. void TileSetEditor::_select_previous_shape() {
  1805. if (get_current_tile() == -1) {
  1806. _select_previous_subtile();
  1807. if (get_current_tile() != -1 && edit_mode == EDITMODE_COLLISION) {
  1808. SubtileData data = current_tile_data[Vector2i(edited_shape_coord)];
  1809. if (data.collisions.size() > 1) {
  1810. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  1811. }
  1812. } else {
  1813. return;
  1814. }
  1815. } else if (edit_mode != EDITMODE_COLLISION) {
  1816. _select_previous_subtile();
  1817. } else {
  1818. Vector2i edited_coord = Vector2();
  1819. if (tileset->tile_get_tile_mode(get_current_tile()) != TileSet::SINGLE_TILE) {
  1820. edited_coord = edited_shape_coord;
  1821. }
  1822. SubtileData data = current_tile_data[edited_coord];
  1823. if (data.collisions.size() == 0) {
  1824. _select_previous_subtile();
  1825. data = current_tile_data[Vector2i(edited_shape_coord)];
  1826. if (data.collisions.size() > 1) {
  1827. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  1828. }
  1829. } else {
  1830. int index = data.collisions.find(edited_collision_shape);
  1831. if (index < 0) {
  1832. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  1833. } else if (index == 0) {
  1834. _select_previous_subtile();
  1835. data = current_tile_data[Vector2i(edited_shape_coord)];
  1836. if (data.collisions.size() > 1) {
  1837. _set_edited_collision_shape(data.collisions[data.collisions.size() - 1]);
  1838. }
  1839. } else {
  1840. _set_edited_collision_shape(data.collisions[index - 1]);
  1841. }
  1842. }
  1843. current_shape.resize(0);
  1844. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  1845. current_tile_region.position += WORKSPACE_MARGIN;
  1846. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1847. Vector2 size = tileset->autotile_get_size(get_current_tile());
  1848. Vector2 shape_anchor = edited_shape_coord;
  1849. shape_anchor.x *= (size.x + spacing);
  1850. shape_anchor.y *= (size.y + spacing);
  1851. current_tile_region.position += shape_anchor;
  1852. if (edited_collision_shape.is_valid()) {
  1853. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  1854. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  1855. }
  1856. }
  1857. workspace->update();
  1858. workspace_container->update();
  1859. helper->_change_notify("");
  1860. }
  1861. }
  1862. void TileSetEditor::_set_edited_collision_shape(const Ref<Shape2D> &p_shape) {
  1863. edited_collision_shape = p_shape;
  1864. _update_toggle_shape_button();
  1865. }
  1866. void TileSetEditor::_set_snap_step(Vector2 p_val) {
  1867. snap_step.x = CLAMP(p_val.x, 0, 256);
  1868. snap_step.y = CLAMP(p_val.y, 0, 256);
  1869. workspace->update();
  1870. }
  1871. void TileSetEditor::_set_snap_off(Vector2 p_val) {
  1872. snap_offset.x = CLAMP(p_val.x, 0, 256 + WORKSPACE_MARGIN.x);
  1873. snap_offset.y = CLAMP(p_val.y, 0, 256 + WORKSPACE_MARGIN.y);
  1874. workspace->update();
  1875. }
  1876. void TileSetEditor::_set_snap_sep(Vector2 p_val) {
  1877. snap_separation.x = CLAMP(p_val.x, 0, 256);
  1878. snap_separation.y = CLAMP(p_val.y, 0, 256);
  1879. workspace->update();
  1880. }
  1881. void TileSetEditor::_validate_current_tile_id() {
  1882. if (get_current_tile() >= 0 && !tileset->has_tile(get_current_tile()))
  1883. set_current_tile(-1);
  1884. }
  1885. void TileSetEditor::_select_edited_shape_coord() {
  1886. select_coord(edited_shape_coord);
  1887. }
  1888. void TileSetEditor::_undo_tile_removal(int p_id) {
  1889. undo_redo->add_undo_method(tileset.ptr(), "create_tile", p_id);
  1890. undo_redo->add_undo_method(tileset.ptr(), "tile_set_name", p_id, tileset->tile_get_name(p_id));
  1891. undo_redo->add_undo_method(tileset.ptr(), "tile_set_normal_map", p_id, tileset->tile_get_normal_map(p_id));
  1892. undo_redo->add_undo_method(tileset.ptr(), "tile_set_texture_offset", p_id, tileset->tile_get_texture_offset(p_id));
  1893. undo_redo->add_undo_method(tileset.ptr(), "tile_set_material", p_id, tileset->tile_get_material(p_id));
  1894. undo_redo->add_undo_method(tileset.ptr(), "tile_set_modulate", p_id, tileset->tile_get_modulate(p_id));
  1895. undo_redo->add_undo_method(tileset.ptr(), "tile_set_occluder_offset", p_id, tileset->tile_get_occluder_offset(p_id));
  1896. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon_offset", p_id, tileset->tile_get_navigation_polygon_offset(p_id));
  1897. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shape_offset", p_id, 0, tileset->tile_get_shape_offset(p_id, 0));
  1898. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shape_transform", p_id, 0, tileset->tile_get_shape_transform(p_id, 0));
  1899. undo_redo->add_undo_method(tileset.ptr(), "tile_set_z_index", p_id, tileset->tile_get_z_index(p_id));
  1900. undo_redo->add_undo_method(tileset.ptr(), "tile_set_texture", p_id, tileset->tile_get_texture(p_id));
  1901. undo_redo->add_undo_method(tileset.ptr(), "tile_set_region", p_id, tileset->tile_get_region(p_id));
  1902. // Necessary to get the version that returns a Array instead of a Vector.
  1903. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", p_id, tileset->call("tile_get_shapes", p_id));
  1904. if (tileset->tile_get_tile_mode(p_id) == TileSet::SINGLE_TILE) {
  1905. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", p_id, tileset->tile_get_light_occluder(p_id));
  1906. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", p_id, tileset->tile_get_navigation_polygon(p_id));
  1907. } else {
  1908. Map<Vector2, Ref<OccluderPolygon2D> > oclusion_map = tileset->autotile_get_light_oclusion_map(p_id);
  1909. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = oclusion_map.front(); E; E = E->next()) {
  1910. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_light_occluder", p_id, E->value(), E->key());
  1911. }
  1912. Map<Vector2, Ref<NavigationPolygon> > navigation_map = tileset->autotile_get_navigation_map(p_id);
  1913. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = navigation_map.front(); E; E = E->next()) {
  1914. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_navigation_polygon", p_id, E->value(), E->key());
  1915. }
  1916. Map<Vector2, uint16_t> bitmask_map = tileset->autotile_get_bitmask_map(p_id);
  1917. for (Map<Vector2, uint16_t>::Element *E = bitmask_map.front(); E; E = E->next()) {
  1918. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask", p_id, E->key(), E->value());
  1919. }
  1920. Map<Vector2, int> priority_map = tileset->autotile_get_priority_map(p_id);
  1921. for (Map<Vector2, int>::Element *E = priority_map.front(); E; E = E->next()) {
  1922. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_subtile_priority", p_id, E->key(), E->value());
  1923. }
  1924. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_icon_coordinate", p_id, tileset->autotile_get_icon_coordinate(p_id));
  1925. Map<Vector2, int> z_map = tileset->autotile_get_z_index_map(p_id);
  1926. for (Map<Vector2, int>::Element *E = z_map.front(); E; E = E->next()) {
  1927. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_z_index", p_id, E->key(), E->value());
  1928. }
  1929. undo_redo->add_undo_method(tileset.ptr(), "tile_set_tile_mode", p_id, tileset->tile_get_tile_mode(p_id));
  1930. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_size", p_id, tileset->autotile_get_size(p_id));
  1931. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_spacing", p_id, tileset->autotile_get_spacing(p_id));
  1932. undo_redo->add_undo_method(tileset.ptr(), "autotile_set_bitmask_mode", p_id, tileset->autotile_get_bitmask_mode(p_id));
  1933. }
  1934. }
  1935. void TileSetEditor::_zoom_in() {
  1936. float scale = workspace->get_scale().x;
  1937. if (scale < max_scale) {
  1938. scale *= scale_ratio;
  1939. workspace->set_scale(Vector2(scale, scale));
  1940. workspace_container->set_custom_minimum_size(workspace->get_rect().size * scale);
  1941. workspace_overlay->set_custom_minimum_size(workspace->get_rect().size * scale);
  1942. }
  1943. }
  1944. void TileSetEditor::_zoom_out() {
  1945. float scale = workspace->get_scale().x;
  1946. if (scale > min_scale) {
  1947. scale /= scale_ratio;
  1948. workspace->set_scale(Vector2(scale, scale));
  1949. workspace_container->set_custom_minimum_size(workspace->get_rect().size * scale);
  1950. workspace_overlay->set_custom_minimum_size(workspace->get_rect().size * scale);
  1951. }
  1952. }
  1953. void TileSetEditor::_zoom_reset() {
  1954. workspace->set_scale(Vector2(1, 1));
  1955. workspace_container->set_custom_minimum_size(workspace->get_rect().size);
  1956. workspace_overlay->set_custom_minimum_size(workspace->get_rect().size);
  1957. }
  1958. void TileSetEditor::draw_highlight_current_tile() {
  1959. Color shadow_color = Color(0.3, 0.3, 0.3, 0.3);
  1960. if ((workspace_mode == WORKSPACE_EDIT && get_current_tile() >= 0) || !edited_region.has_no_area()) {
  1961. Rect2 region;
  1962. if (edited_region.has_no_area()) {
  1963. region = tileset->tile_get_region(get_current_tile());
  1964. region.position += WORKSPACE_MARGIN;
  1965. } else {
  1966. region = edited_region;
  1967. }
  1968. if (region.position.y >= 0)
  1969. workspace->draw_rect(Rect2(0, 0, workspace->get_rect().size.x, region.position.y), shadow_color);
  1970. if (region.position.x >= 0)
  1971. 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);
  1972. if (region.position.x + region.size.x <= workspace->get_rect().size.x)
  1973. 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);
  1974. if (region.position.y + region.size.y <= workspace->get_rect().size.y)
  1975. 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);
  1976. } else {
  1977. workspace->draw_rect(Rect2(Point2(0, 0), workspace->get_rect().size), shadow_color);
  1978. }
  1979. }
  1980. void TileSetEditor::draw_highlight_subtile(Vector2 coord, const Vector<Vector2> &other_highlighted) {
  1981. Color shadow_color = Color(0.3, 0.3, 0.3, 0.3);
  1982. Vector2 size = tileset->autotile_get_size(get_current_tile());
  1983. int spacing = tileset->autotile_get_spacing(get_current_tile());
  1984. Rect2 region = tileset->tile_get_region(get_current_tile());
  1985. coord.x *= (size.x + spacing);
  1986. coord.y *= (size.y + spacing);
  1987. coord += region.position;
  1988. coord += WORKSPACE_MARGIN;
  1989. if (coord.y >= 0)
  1990. workspace->draw_rect(Rect2(0, 0, workspace->get_rect().size.x, coord.y), shadow_color);
  1991. if (coord.x >= 0)
  1992. 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);
  1993. if (coord.x + size.x <= workspace->get_rect().size.x)
  1994. 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);
  1995. if (coord.y + size.y <= workspace->get_rect().size.y)
  1996. 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);
  1997. coord += Vector2(1, 1) / workspace->get_scale().x;
  1998. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2) / workspace->get_scale().x), Color(1, 0, 0), false);
  1999. for (int i = 0; i < other_highlighted.size(); i++) {
  2000. coord = other_highlighted[i];
  2001. coord.x *= (size.x + spacing);
  2002. coord.y *= (size.y + spacing);
  2003. coord += region.position;
  2004. coord += WORKSPACE_MARGIN;
  2005. coord += Vector2(1, 1) / workspace->get_scale().x;
  2006. workspace->draw_rect(Rect2(coord, size - Vector2(2, 2) / workspace->get_scale().x), Color(1, 0.5, 0.5), false);
  2007. }
  2008. }
  2009. void TileSetEditor::draw_tile_subdivision(int p_id, Color p_color) const {
  2010. Color c = p_color;
  2011. if (tileset->tile_get_tile_mode(p_id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(p_id) == TileSet::ATLAS_TILE) {
  2012. Rect2 region = tileset->tile_get_region(p_id);
  2013. Size2 size = tileset->autotile_get_size(p_id);
  2014. int spacing = tileset->autotile_get_spacing(p_id);
  2015. float j = size.x;
  2016. while (j < region.size.x) {
  2017. if (spacing <= 0) {
  2018. workspace->draw_line(region.position + WORKSPACE_MARGIN + Point2(j, 0), region.position + WORKSPACE_MARGIN + Point2(j, region.size.y), c);
  2019. } else {
  2020. workspace->draw_rect(Rect2(region.position + WORKSPACE_MARGIN + Point2(j, 0), Size2(spacing, region.size.y)), c);
  2021. }
  2022. j += spacing + size.x;
  2023. }
  2024. j = size.y;
  2025. while (j < region.size.y) {
  2026. if (spacing <= 0) {
  2027. workspace->draw_line(region.position + WORKSPACE_MARGIN + Point2(0, j), region.position + WORKSPACE_MARGIN + Point2(region.size.x, j), c);
  2028. } else {
  2029. workspace->draw_rect(Rect2(region.position + WORKSPACE_MARGIN + Point2(0, j), Size2(region.size.x, spacing)), c);
  2030. }
  2031. j += spacing + size.y;
  2032. }
  2033. }
  2034. }
  2035. void TileSetEditor::draw_edited_region_subdivision() const {
  2036. Color c = Color(0.3, 0.7, 0.6);
  2037. Rect2 region = edited_region;
  2038. Size2 size;
  2039. int spacing;
  2040. bool draw;
  2041. if (workspace_mode == WORKSPACE_EDIT) {
  2042. int p_id = get_current_tile();
  2043. size = tileset->autotile_get_size(p_id);
  2044. spacing = tileset->autotile_get_spacing(p_id);
  2045. draw = tileset->tile_get_tile_mode(p_id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(p_id) == TileSet::ATLAS_TILE;
  2046. } else {
  2047. size = snap_step;
  2048. spacing = snap_separation.x;
  2049. draw = workspace_mode != WORKSPACE_CREATE_SINGLE;
  2050. }
  2051. if (draw) {
  2052. float j = size.x;
  2053. while (j < region.size.x) {
  2054. if (spacing <= 0) {
  2055. workspace->draw_line(region.position + Point2(j, 0), region.position + Point2(j, region.size.y), c);
  2056. } else {
  2057. workspace->draw_rect(Rect2(region.position + Point2(j, 0), Size2(spacing, region.size.y)), c);
  2058. }
  2059. j += spacing + size.x;
  2060. }
  2061. j = size.y;
  2062. while (j < region.size.y) {
  2063. if (spacing <= 0) {
  2064. workspace->draw_line(region.position + Point2(0, j), region.position + Point2(region.size.x, j), c);
  2065. } else {
  2066. workspace->draw_rect(Rect2(region.position + Point2(0, j), Size2(region.size.x, spacing)), c);
  2067. }
  2068. j += spacing + size.y;
  2069. }
  2070. }
  2071. }
  2072. void TileSetEditor::draw_grid_snap() {
  2073. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  2074. Color grid_color = Color(0.4, 0, 1);
  2075. Size2 s = workspace->get_size();
  2076. int width_count = Math::floor((s.width - WORKSPACE_MARGIN.x) / (snap_step.x + snap_separation.x));
  2077. int height_count = Math::floor((s.height - WORKSPACE_MARGIN.y) / (snap_step.y + snap_separation.y));
  2078. int last_p = 0;
  2079. if (snap_step.x != 0) {
  2080. for (int i = 0; i <= width_count; i++) {
  2081. if (i == 0 && snap_offset.x != 0) {
  2082. last_p = snap_offset.x;
  2083. }
  2084. if (snap_separation.x != 0) {
  2085. if (i != 0) {
  2086. workspace->draw_rect(Rect2(last_p, 0, snap_separation.x, s.height), grid_color);
  2087. last_p += snap_separation.x;
  2088. } else {
  2089. workspace->draw_rect(Rect2(last_p, 0, -snap_separation.x, s.height), grid_color);
  2090. }
  2091. } else {
  2092. workspace->draw_line(Point2(last_p, 0), Point2(last_p, s.height), grid_color);
  2093. }
  2094. last_p += snap_step.x;
  2095. }
  2096. }
  2097. last_p = 0;
  2098. if (snap_step.y != 0) {
  2099. for (int i = 0; i <= height_count; i++) {
  2100. if (i == 0 && snap_offset.y != 0) {
  2101. last_p = snap_offset.y;
  2102. }
  2103. if (snap_separation.x != 0) {
  2104. if (i != 0) {
  2105. workspace->draw_rect(Rect2(0, last_p, s.width, snap_separation.y), grid_color);
  2106. last_p += snap_separation.y;
  2107. } else {
  2108. workspace->draw_rect(Rect2(0, last_p, s.width, -snap_separation.y), grid_color);
  2109. }
  2110. } else {
  2111. workspace->draw_line(Point2(0, last_p), Point2(s.width, last_p), grid_color);
  2112. }
  2113. last_p += snap_step.y;
  2114. }
  2115. }
  2116. }
  2117. }
  2118. void TileSetEditor::draw_polygon_shapes() {
  2119. int t_id = get_current_tile();
  2120. if (t_id < 0)
  2121. return;
  2122. switch (edit_mode) {
  2123. case EDITMODE_COLLISION: {
  2124. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(t_id);
  2125. for (int i = 0; i < sd.size(); i++) {
  2126. Vector2 coord = Vector2(0, 0);
  2127. Vector2 anchor = Vector2(0, 0);
  2128. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2129. coord = sd[i].autotile_coord;
  2130. anchor = tileset->autotile_get_size(t_id);
  2131. anchor.x += tileset->autotile_get_spacing(t_id);
  2132. anchor.y += tileset->autotile_get_spacing(t_id);
  2133. anchor.x *= coord.x;
  2134. anchor.y *= coord.y;
  2135. }
  2136. anchor += WORKSPACE_MARGIN;
  2137. anchor += tileset->tile_get_region(t_id).position;
  2138. Ref<Shape2D> shape = sd[i].shape;
  2139. if (shape.is_valid()) {
  2140. Color c_bg;
  2141. Color c_border;
  2142. Ref<ConvexPolygonShape2D> convex = shape;
  2143. bool is_convex = convex.is_valid();
  2144. if ((tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE || coord == edited_shape_coord) && sd[i].shape == edited_collision_shape) {
  2145. if (is_convex) {
  2146. c_bg = Color(0, 1, 1, 0.5);
  2147. c_border = Color(0, 1, 1);
  2148. } else {
  2149. c_bg = Color(0.8, 0, 1, 0.5);
  2150. c_border = Color(0.8, 0, 1);
  2151. }
  2152. } else {
  2153. if (is_convex) {
  2154. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2155. c_border = Color(0.9, 0.7, 0.07, 1);
  2156. } else {
  2157. c_bg = Color(0.9, 0.45, 0.075, 0.5);
  2158. c_border = Color(0.9, 0.45, 0.075);
  2159. }
  2160. }
  2161. Vector<Vector2> polygon;
  2162. Vector<Color> colors;
  2163. if (!creating_shape && shape == edited_collision_shape && current_shape.size() > 2) {
  2164. for (int j = 0; j < current_shape.size(); j++) {
  2165. polygon.push_back(current_shape[j]);
  2166. colors.push_back(c_bg);
  2167. }
  2168. } else {
  2169. for (int j = 0; j < _get_collision_shape_points(shape).size(); j++) {
  2170. polygon.push_back(_get_collision_shape_points(shape)[j] + anchor);
  2171. colors.push_back(c_bg);
  2172. }
  2173. }
  2174. if (polygon.size() > 2) {
  2175. workspace->draw_polygon(polygon, colors);
  2176. }
  2177. if (coord == edited_shape_coord || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2178. if (!creating_shape) {
  2179. for (int j = 0; j < polygon.size() - 1; j++) {
  2180. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2181. }
  2182. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2183. }
  2184. if (shape == edited_collision_shape) {
  2185. draw_handles = true;
  2186. }
  2187. }
  2188. }
  2189. }
  2190. } break;
  2191. case EDITMODE_OCCLUSION: {
  2192. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2193. Ref<OccluderPolygon2D> shape = edited_occlusion_shape;
  2194. if (shape.is_valid()) {
  2195. Color c_bg = Color(0, 1, 1, 0.5);
  2196. Color c_border = Color(0, 1, 1);
  2197. Vector<Vector2> polygon;
  2198. Vector<Color> colors;
  2199. Vector2 anchor = WORKSPACE_MARGIN;
  2200. anchor += tileset->tile_get_region(get_current_tile()).position;
  2201. if (!creating_shape && shape == edited_occlusion_shape && current_shape.size() > 2) {
  2202. for (int j = 0; j < current_shape.size(); j++) {
  2203. polygon.push_back(current_shape[j]);
  2204. colors.push_back(c_bg);
  2205. }
  2206. } else {
  2207. for (int j = 0; j < shape->get_polygon().size(); j++) {
  2208. polygon.push_back(shape->get_polygon()[j] + anchor);
  2209. colors.push_back(c_bg);
  2210. }
  2211. }
  2212. workspace->draw_polygon(polygon, colors);
  2213. if (!creating_shape) {
  2214. for (int j = 0; j < polygon.size() - 1; j++) {
  2215. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2216. }
  2217. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2218. }
  2219. if (shape == edited_occlusion_shape) {
  2220. draw_handles = true;
  2221. }
  2222. }
  2223. } else {
  2224. Map<Vector2, Ref<OccluderPolygon2D> > map = tileset->autotile_get_light_oclusion_map(t_id);
  2225. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = map.front(); E; E = E->next()) {
  2226. Vector2 coord = E->key();
  2227. Vector2 anchor = tileset->autotile_get_size(t_id);
  2228. anchor.x += tileset->autotile_get_spacing(t_id);
  2229. anchor.y += tileset->autotile_get_spacing(t_id);
  2230. anchor.x *= coord.x;
  2231. anchor.y *= coord.y;
  2232. anchor += WORKSPACE_MARGIN;
  2233. anchor += tileset->tile_get_region(t_id).position;
  2234. Ref<OccluderPolygon2D> shape = E->value();
  2235. if (shape.is_valid()) {
  2236. Color c_bg;
  2237. Color c_border;
  2238. if (coord == edited_shape_coord && shape == edited_occlusion_shape) {
  2239. c_bg = Color(0, 1, 1, 0.5);
  2240. c_border = Color(0, 1, 1);
  2241. } else {
  2242. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2243. c_border = Color(0.9, 0.7, 0.07, 1);
  2244. }
  2245. Vector<Vector2> polygon;
  2246. Vector<Color> colors;
  2247. if (!creating_shape && shape == edited_occlusion_shape && current_shape.size() > 2) {
  2248. for (int j = 0; j < current_shape.size(); j++) {
  2249. polygon.push_back(current_shape[j]);
  2250. colors.push_back(c_bg);
  2251. }
  2252. } else {
  2253. for (int j = 0; j < shape->get_polygon().size(); j++) {
  2254. polygon.push_back(shape->get_polygon()[j] + anchor);
  2255. colors.push_back(c_bg);
  2256. }
  2257. }
  2258. workspace->draw_polygon(polygon, colors);
  2259. if (coord == edited_shape_coord) {
  2260. if (!creating_shape) {
  2261. for (int j = 0; j < polygon.size() - 1; j++) {
  2262. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2263. }
  2264. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2265. }
  2266. if (shape == edited_occlusion_shape) {
  2267. draw_handles = true;
  2268. }
  2269. }
  2270. }
  2271. }
  2272. }
  2273. } break;
  2274. case EDITMODE_NAVIGATION: {
  2275. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2276. Ref<NavigationPolygon> shape = edited_navigation_shape;
  2277. if (shape.is_valid()) {
  2278. Color c_bg = Color(0, 1, 1, 0.5);
  2279. Color c_border = Color(0, 1, 1);
  2280. Vector<Vector2> polygon;
  2281. Vector<Color> colors;
  2282. Vector2 anchor = WORKSPACE_MARGIN;
  2283. anchor += tileset->tile_get_region(get_current_tile()).position;
  2284. if (!creating_shape && shape == edited_navigation_shape && current_shape.size() > 2) {
  2285. for (int j = 0; j < current_shape.size(); j++) {
  2286. polygon.push_back(current_shape[j]);
  2287. colors.push_back(c_bg);
  2288. }
  2289. } else {
  2290. PoolVector<Vector2> vertices = shape->get_vertices();
  2291. for (int j = 0; j < shape->get_polygon(0).size(); j++) {
  2292. polygon.push_back(vertices[shape->get_polygon(0)[j]] + anchor);
  2293. colors.push_back(c_bg);
  2294. }
  2295. }
  2296. workspace->draw_polygon(polygon, colors);
  2297. if (!creating_shape) {
  2298. for (int j = 0; j < polygon.size() - 1; j++) {
  2299. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2300. }
  2301. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2302. }
  2303. if (shape == edited_navigation_shape) {
  2304. draw_handles = true;
  2305. }
  2306. }
  2307. } else {
  2308. Map<Vector2, Ref<NavigationPolygon> > map = tileset->autotile_get_navigation_map(t_id);
  2309. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = map.front(); E; E = E->next()) {
  2310. Vector2 coord = E->key();
  2311. Vector2 anchor = tileset->autotile_get_size(t_id);
  2312. anchor.x += tileset->autotile_get_spacing(t_id);
  2313. anchor.y += tileset->autotile_get_spacing(t_id);
  2314. anchor.x *= coord.x;
  2315. anchor.y *= coord.y;
  2316. anchor += WORKSPACE_MARGIN;
  2317. anchor += tileset->tile_get_region(t_id).position;
  2318. Ref<NavigationPolygon> shape = E->value();
  2319. if (shape.is_valid()) {
  2320. Color c_bg;
  2321. Color c_border;
  2322. if (coord == edited_shape_coord && shape == edited_navigation_shape) {
  2323. c_bg = Color(0, 1, 1, 0.5);
  2324. c_border = Color(0, 1, 1);
  2325. } else {
  2326. c_bg = Color(0.9, 0.7, 0.07, 0.5);
  2327. c_border = Color(0.9, 0.7, 0.07, 1);
  2328. }
  2329. Vector<Vector2> polygon;
  2330. Vector<Color> colors;
  2331. if (!creating_shape && shape == edited_navigation_shape && current_shape.size() > 2) {
  2332. for (int j = 0; j < current_shape.size(); j++) {
  2333. polygon.push_back(current_shape[j]);
  2334. colors.push_back(c_bg);
  2335. }
  2336. } else {
  2337. PoolVector<Vector2> vertices = shape->get_vertices();
  2338. for (int j = 0; j < shape->get_polygon(0).size(); j++) {
  2339. polygon.push_back(vertices[shape->get_polygon(0)[j]] + anchor);
  2340. colors.push_back(c_bg);
  2341. }
  2342. }
  2343. workspace->draw_polygon(polygon, colors);
  2344. if (coord == edited_shape_coord) {
  2345. if (!creating_shape) {
  2346. for (int j = 0; j < polygon.size() - 1; j++) {
  2347. workspace->draw_line(polygon[j], polygon[j + 1], c_border, 1, true);
  2348. }
  2349. workspace->draw_line(polygon[polygon.size() - 1], polygon[0], c_border, 1, true);
  2350. }
  2351. if (shape == edited_navigation_shape) {
  2352. draw_handles = true;
  2353. }
  2354. }
  2355. }
  2356. }
  2357. }
  2358. } break;
  2359. default: {}
  2360. }
  2361. if (creating_shape) {
  2362. for (int j = 0; j < current_shape.size() - 1; j++) {
  2363. workspace->draw_line(current_shape[j], current_shape[j + 1], Color(0, 1, 1), 1, true);
  2364. }
  2365. workspace->draw_line(current_shape[current_shape.size() - 1], snap_point(workspace->get_local_mouse_position()), Color(0, 1, 1), 1, true);
  2366. draw_handles = true;
  2367. }
  2368. }
  2369. void TileSetEditor::close_shape(const Vector2 &shape_anchor) {
  2370. creating_shape = false;
  2371. if (edit_mode == EDITMODE_COLLISION) {
  2372. if (current_shape.size() >= 3) {
  2373. Ref<ConvexPolygonShape2D> shape = memnew(ConvexPolygonShape2D);
  2374. Vector<Vector2> points;
  2375. float p_total = 0;
  2376. for (int i = 0; i < current_shape.size(); i++) {
  2377. points.push_back(current_shape[i] - shape_anchor);
  2378. if (i != current_shape.size() - 1)
  2379. p_total += ((current_shape[i + 1].x - current_shape[i].x) * (-current_shape[i + 1].y + (-current_shape[i].y)));
  2380. else
  2381. p_total += ((current_shape[0].x - current_shape[i].x) * (-current_shape[0].y + (-current_shape[i].y)));
  2382. }
  2383. if (p_total < 0)
  2384. points.invert();
  2385. shape->set_points(points);
  2386. undo_redo->create_action(TTR("Create Collision Polygon"));
  2387. // Necessary to get the version that returns a Array instead of a Vector.
  2388. Array sd = tileset->call("tile_get_shapes", get_current_tile());
  2389. undo_redo->add_undo_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd.duplicate());
  2390. for (int i = 0; i < sd.size(); i++) {
  2391. if (sd[i].get("shape") == edited_collision_shape) {
  2392. sd.remove(i);
  2393. break;
  2394. }
  2395. }
  2396. undo_redo->add_do_method(tileset.ptr(), "tile_set_shapes", get_current_tile(), sd);
  2397. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE)
  2398. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), shape, Transform2D(), false, edited_shape_coord);
  2399. else
  2400. undo_redo->add_do_method(tileset.ptr(), "tile_add_shape", get_current_tile(), shape, Transform2D());
  2401. tools[TOOL_SELECT]->set_pressed(true);
  2402. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2403. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2404. undo_redo->commit_action();
  2405. } else {
  2406. tools[TOOL_SELECT]->set_pressed(true);
  2407. workspace->update();
  2408. }
  2409. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2410. Ref<OccluderPolygon2D> shape = memnew(OccluderPolygon2D);
  2411. PoolVector<Vector2> polygon;
  2412. polygon.resize(current_shape.size());
  2413. PoolVector<Vector2>::Write w = polygon.write();
  2414. for (int i = 0; i < current_shape.size(); i++) {
  2415. w[i] = current_shape[i] - shape_anchor;
  2416. }
  2417. w = PoolVector<Vector2>::Write();
  2418. shape->set_polygon(polygon);
  2419. undo_redo->create_action(TTR("Create Occlusion Polygon"));
  2420. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2421. undo_redo->add_do_method(tileset.ptr(), "autotile_set_light_occluder", get_current_tile(), shape, edited_shape_coord);
  2422. 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);
  2423. } else {
  2424. undo_redo->add_do_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), shape);
  2425. undo_redo->add_undo_method(tileset.ptr(), "tile_set_light_occluder", get_current_tile(), tileset->tile_get_light_occluder(get_current_tile()));
  2426. }
  2427. tools[TOOL_SELECT]->set_pressed(true);
  2428. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2429. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2430. undo_redo->commit_action();
  2431. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2432. Ref<NavigationPolygon> shape = memnew(NavigationPolygon);
  2433. PoolVector<Vector2> polygon;
  2434. Vector<int> indices;
  2435. polygon.resize(current_shape.size());
  2436. PoolVector<Vector2>::Write w = polygon.write();
  2437. for (int i = 0; i < current_shape.size(); i++) {
  2438. w[i] = current_shape[i] - shape_anchor;
  2439. indices.push_back(i);
  2440. }
  2441. w = PoolVector<Vector2>::Write();
  2442. shape->set_vertices(polygon);
  2443. shape->add_polygon(indices);
  2444. undo_redo->create_action(TTR("Create Navigation Polygon"));
  2445. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2446. undo_redo->add_do_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), shape, edited_shape_coord);
  2447. 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);
  2448. } else {
  2449. undo_redo->add_do_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), shape);
  2450. undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), tileset->tile_get_navigation_polygon(get_current_tile()));
  2451. }
  2452. tools[TOOL_SELECT]->set_pressed(true);
  2453. undo_redo->add_do_method(this, "_select_edited_shape_coord");
  2454. undo_redo->add_undo_method(this, "_select_edited_shape_coord");
  2455. undo_redo->commit_action();
  2456. }
  2457. tileset->_change_notify("");
  2458. }
  2459. void TileSetEditor::select_coord(const Vector2 &coord) {
  2460. _update_tile_data();
  2461. current_shape = PoolVector2Array();
  2462. if (get_current_tile() == -1)
  2463. return;
  2464. Rect2 current_tile_region = tileset->tile_get_region(get_current_tile());
  2465. current_tile_region.position += WORKSPACE_MARGIN;
  2466. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2467. if (edited_collision_shape != tileset->tile_get_shape(get_current_tile(), 0))
  2468. _set_edited_collision_shape(tileset->tile_get_shape(get_current_tile(), 0));
  2469. if (edited_occlusion_shape != tileset->tile_get_light_occluder(get_current_tile()))
  2470. edited_occlusion_shape = tileset->tile_get_light_occluder(get_current_tile());
  2471. if (edited_navigation_shape != tileset->tile_get_navigation_polygon(get_current_tile()))
  2472. edited_navigation_shape = tileset->tile_get_navigation_polygon(get_current_tile());
  2473. if (edit_mode == EDITMODE_COLLISION) {
  2474. current_shape.resize(0);
  2475. if (edited_collision_shape.is_valid()) {
  2476. for (int i = 0; i < _get_edited_shape_points().size(); i++) {
  2477. current_shape.push_back(_get_edited_shape_points()[i] + current_tile_region.position);
  2478. }
  2479. }
  2480. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2481. current_shape.resize(0);
  2482. if (edited_occlusion_shape.is_valid()) {
  2483. for (int i = 0; i < edited_occlusion_shape->get_polygon().size(); i++) {
  2484. current_shape.push_back(edited_occlusion_shape->get_polygon()[i] + current_tile_region.position);
  2485. }
  2486. }
  2487. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2488. current_shape.resize(0);
  2489. if (edited_navigation_shape.is_valid()) {
  2490. if (edited_navigation_shape->get_polygon_count() > 0) {
  2491. PoolVector<Vector2> vertices = edited_navigation_shape->get_vertices();
  2492. for (int i = 0; i < edited_navigation_shape->get_polygon(0).size(); i++) {
  2493. current_shape.push_back(vertices[edited_navigation_shape->get_polygon(0)[i]] + current_tile_region.position);
  2494. }
  2495. }
  2496. }
  2497. }
  2498. } else {
  2499. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(get_current_tile());
  2500. bool found_collision_shape = false;
  2501. for (int i = 0; i < sd.size(); i++) {
  2502. if (sd[i].autotile_coord == coord) {
  2503. if (edited_collision_shape != sd[i].shape)
  2504. _set_edited_collision_shape(sd[i].shape);
  2505. found_collision_shape = true;
  2506. break;
  2507. }
  2508. }
  2509. if (!found_collision_shape)
  2510. _set_edited_collision_shape(Ref<ConvexPolygonShape2D>(NULL));
  2511. if (edited_occlusion_shape != tileset->autotile_get_light_occluder(get_current_tile(), coord))
  2512. edited_occlusion_shape = tileset->autotile_get_light_occluder(get_current_tile(), coord);
  2513. if (edited_navigation_shape != tileset->autotile_get_navigation_polygon(get_current_tile(), coord))
  2514. edited_navigation_shape = tileset->autotile_get_navigation_polygon(get_current_tile(), coord);
  2515. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2516. Vector2 size = tileset->autotile_get_size(get_current_tile());
  2517. Vector2 shape_anchor = coord;
  2518. shape_anchor.x *= (size.x + spacing);
  2519. shape_anchor.y *= (size.y + spacing);
  2520. shape_anchor += current_tile_region.position;
  2521. if (edit_mode == EDITMODE_COLLISION) {
  2522. current_shape.resize(0);
  2523. if (edited_collision_shape.is_valid()) {
  2524. for (int j = 0; j < _get_edited_shape_points().size(); j++) {
  2525. current_shape.push_back(_get_edited_shape_points()[j] + shape_anchor);
  2526. }
  2527. }
  2528. } else if (edit_mode == EDITMODE_OCCLUSION) {
  2529. current_shape.resize(0);
  2530. if (edited_occlusion_shape.is_valid()) {
  2531. for (int i = 0; i < edited_occlusion_shape->get_polygon().size(); i++) {
  2532. current_shape.push_back(edited_occlusion_shape->get_polygon()[i] + shape_anchor);
  2533. }
  2534. }
  2535. } else if (edit_mode == EDITMODE_NAVIGATION) {
  2536. current_shape.resize(0);
  2537. if (edited_navigation_shape.is_valid()) {
  2538. if (edited_navigation_shape->get_polygon_count() > 0) {
  2539. PoolVector<Vector2> vertices = edited_navigation_shape->get_vertices();
  2540. for (int i = 0; i < edited_navigation_shape->get_polygon(0).size(); i++) {
  2541. current_shape.push_back(vertices[edited_navigation_shape->get_polygon(0)[i]] + shape_anchor);
  2542. }
  2543. }
  2544. }
  2545. }
  2546. }
  2547. workspace->update();
  2548. workspace_container->update();
  2549. helper->_change_notify("");
  2550. }
  2551. Vector2 TileSetEditor::snap_point(const Vector2 &point) {
  2552. Vector2 p = point;
  2553. Vector2 coord = edited_shape_coord;
  2554. Vector2 tile_size = tileset->autotile_get_size(get_current_tile());
  2555. int spacing = tileset->autotile_get_spacing(get_current_tile());
  2556. Vector2 anchor = coord;
  2557. anchor.x *= (tile_size.x + spacing);
  2558. anchor.y *= (tile_size.y + spacing);
  2559. anchor += tileset->tile_get_region(get_current_tile()).position;
  2560. anchor += WORKSPACE_MARGIN;
  2561. Rect2 region(anchor, tile_size);
  2562. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2563. region.position = tileset->tile_get_region(get_current_tile()).position + WORKSPACE_MARGIN;
  2564. region.size = tileset->tile_get_region(get_current_tile()).size;
  2565. }
  2566. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  2567. p.x = Math::snap_scalar_seperation(snap_offset.x, snap_step.x, p.x, snap_separation.x);
  2568. p.y = Math::snap_scalar_seperation(snap_offset.y, snap_step.y, p.y, snap_separation.y);
  2569. }
  2570. if (tools[SHAPE_KEEP_INSIDE_TILE]->is_pressed()) {
  2571. if (p.x < region.position.x)
  2572. p.x = region.position.x;
  2573. if (p.y < region.position.y)
  2574. p.y = region.position.y;
  2575. if (p.x > region.position.x + region.size.x)
  2576. p.x = region.position.x + region.size.x;
  2577. if (p.y > region.position.y + region.size.y)
  2578. p.y = region.position.y + region.size.y;
  2579. }
  2580. return p;
  2581. }
  2582. void TileSetEditor::add_texture(Ref<Texture> p_texture) {
  2583. texture_list->add_item(p_texture->get_path().get_file());
  2584. texture_map.insert(p_texture->get_rid(), p_texture);
  2585. texture_list->set_item_metadata(texture_list->get_item_count() - 1, p_texture->get_rid());
  2586. }
  2587. void TileSetEditor::remove_texture(Ref<Texture> p_texture) {
  2588. texture_list->remove_item(texture_list->find_metadata(p_texture->get_rid()));
  2589. texture_map.erase(p_texture->get_rid());
  2590. _validate_current_tile_id();
  2591. if (!get_current_texture().is_valid()) {
  2592. _on_texture_list_selected(-1);
  2593. workspace_overlay->update();
  2594. }
  2595. }
  2596. void TileSetEditor::update_texture_list() {
  2597. Ref<Texture> selected_texture = get_current_texture();
  2598. helper->set_tileset(tileset);
  2599. List<int> ids;
  2600. tileset->get_tile_list(&ids);
  2601. Vector<int> ids_to_remove;
  2602. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  2603. // Clear tiles referencing gone textures (user has been already given the chance to fix broken deps)
  2604. if (!tileset->tile_get_texture(E->get()).is_valid()) {
  2605. ids_to_remove.push_back(E->get());
  2606. ERR_CONTINUE(!tileset->tile_get_texture(E->get()).is_valid());
  2607. }
  2608. if (!texture_map.has(tileset->tile_get_texture(E->get())->get_rid())) {
  2609. add_texture(tileset->tile_get_texture(E->get()));
  2610. }
  2611. }
  2612. for (int i = 0; i < ids_to_remove.size(); i++) {
  2613. tileset->remove_tile(ids_to_remove[i]);
  2614. }
  2615. if (texture_list->get_item_count() > 0 && selected_texture.is_valid()) {
  2616. texture_list->select(texture_list->find_metadata(selected_texture->get_rid()));
  2617. if (texture_list->get_selected_items().size() > 0)
  2618. _on_texture_list_selected(texture_list->get_selected_items()[0]);
  2619. } else if (get_current_texture().is_valid()) {
  2620. _on_texture_list_selected(texture_list->find_metadata(get_current_texture()->get_rid()));
  2621. } else {
  2622. _validate_current_tile_id();
  2623. _on_texture_list_selected(-1);
  2624. workspace_overlay->update();
  2625. }
  2626. update_texture_list_icon();
  2627. helper->_change_notify("");
  2628. }
  2629. void TileSetEditor::update_texture_list_icon() {
  2630. for (int current_idx = 0; current_idx < texture_list->get_item_count(); current_idx++) {
  2631. RID rid = texture_list->get_item_metadata(current_idx);
  2632. texture_list->set_item_icon(current_idx, texture_map[rid]);
  2633. Size2 texture_size = texture_map[rid]->get_size();
  2634. texture_list->set_item_icon_region(current_idx, Rect2(0, 0, MIN(texture_size.x, 150), MIN(texture_size.y, 100)));
  2635. }
  2636. texture_list->update();
  2637. }
  2638. void TileSetEditor::update_workspace_tile_mode() {
  2639. if (!get_current_texture().is_valid()) {
  2640. tool_workspacemode[WORKSPACE_EDIT]->set_pressed(true);
  2641. workspace_mode = WORKSPACE_EDIT;
  2642. for (int i = 1; i < WORKSPACE_MODE_MAX; i++) {
  2643. tool_workspacemode[i]->set_disabled(true);
  2644. }
  2645. tools[SELECT_NEXT]->set_disabled(true);
  2646. tools[SELECT_PREVIOUS]->set_disabled(true);
  2647. } else {
  2648. for (int i = 1; i < WORKSPACE_MODE_MAX; i++) {
  2649. tool_workspacemode[i]->set_disabled(false);
  2650. }
  2651. tools[SELECT_NEXT]->set_disabled(false);
  2652. tools[SELECT_PREVIOUS]->set_disabled(false);
  2653. }
  2654. if (workspace_mode != WORKSPACE_EDIT) {
  2655. for (int i = 0; i < EDITMODE_MAX; i++) {
  2656. tool_editmode[i]->hide();
  2657. }
  2658. tool_editmode[EDITMODE_REGION]->show();
  2659. tool_editmode[EDITMODE_REGION]->set_pressed(true);
  2660. _on_edit_mode_changed(EDITMODE_REGION);
  2661. separator_editmode->show();
  2662. return;
  2663. }
  2664. if (get_current_tile() < 0) {
  2665. for (int i = 0; i < EDITMODE_MAX; i++) {
  2666. tool_editmode[i]->hide();
  2667. }
  2668. for (int i = TOOL_SELECT; i < ZOOM_OUT; i++) {
  2669. tools[i]->hide();
  2670. }
  2671. separator_editmode->hide();
  2672. separator_bitmask->hide();
  2673. separator_delete->hide();
  2674. separator_grid->hide();
  2675. return;
  2676. }
  2677. for (int i = 0; i < EDITMODE_MAX; i++) {
  2678. tool_editmode[i]->show();
  2679. }
  2680. separator_editmode->show();
  2681. if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::SINGLE_TILE) {
  2682. 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()) {
  2683. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  2684. edit_mode = EDITMODE_COLLISION;
  2685. }
  2686. select_coord(Vector2(0, 0));
  2687. tool_editmode[EDITMODE_ICON]->hide();
  2688. tool_editmode[EDITMODE_BITMASK]->hide();
  2689. tool_editmode[EDITMODE_PRIORITY]->hide();
  2690. tool_editmode[EDITMODE_Z_INDEX]->hide();
  2691. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE) {
  2692. if (edit_mode == EDITMODE_ICON)
  2693. select_coord(tileset->autotile_get_icon_coordinate(get_current_tile()));
  2694. else
  2695. _select_edited_shape_coord();
  2696. } else if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
  2697. if (tool_editmode[EDITMODE_PRIORITY]->is_pressed() || tool_editmode[EDITMODE_BITMASK]->is_pressed()) {
  2698. tool_editmode[EDITMODE_COLLISION]->set_pressed(true);
  2699. edit_mode = EDITMODE_COLLISION;
  2700. }
  2701. if (edit_mode == EDITMODE_ICON)
  2702. select_coord(tileset->autotile_get_icon_coordinate(get_current_tile()));
  2703. else
  2704. _select_edited_shape_coord();
  2705. tool_editmode[EDITMODE_BITMASK]->hide();
  2706. tool_editmode[EDITMODE_PRIORITY]->hide();
  2707. }
  2708. _on_edit_mode_changed(edit_mode);
  2709. }
  2710. void TileSetEditor::update_workspace_minsize() {
  2711. Size2 workspace_min_size = get_current_texture()->get_size();
  2712. RID current_texture_rid = get_current_texture()->get_rid();
  2713. List<int> *tiles = new List<int>();
  2714. tileset->get_tile_list(tiles);
  2715. for (List<int>::Element *E = tiles->front(); E; E = E->next()) {
  2716. if (tileset->tile_get_texture(E->get())->get_rid() == current_texture_rid) {
  2717. Rect2i region = tileset->tile_get_region(E->get());
  2718. if (region.position.x + region.size.x > workspace_min_size.x)
  2719. workspace_min_size.x = region.position.x + region.size.x;
  2720. if (region.position.y + region.size.y > workspace_min_size.y)
  2721. workspace_min_size.y = region.position.y + region.size.y;
  2722. }
  2723. }
  2724. workspace->set_custom_minimum_size(workspace_min_size + WORKSPACE_MARGIN * 2);
  2725. workspace_container->set_custom_minimum_size(workspace_min_size + WORKSPACE_MARGIN * 2);
  2726. workspace_overlay->set_custom_minimum_size(workspace_min_size + WORKSPACE_MARGIN * 2);
  2727. }
  2728. void TileSetEditor::update_edited_region(const Vector2 &end_point) {
  2729. edited_region = Rect2(region_from, Size2());
  2730. if (tools[TOOL_GRID_SNAP]->is_pressed()) {
  2731. Vector2 grid_coord;
  2732. grid_coord = ((region_from - snap_offset) / (snap_step + snap_separation)).floor();
  2733. grid_coord *= (snap_step + snap_separation);
  2734. grid_coord += snap_offset;
  2735. edited_region.expand_to(grid_coord);
  2736. grid_coord += snap_step;
  2737. edited_region.expand_to(grid_coord);
  2738. grid_coord = ((end_point - snap_offset) / (snap_step + snap_separation)).floor();
  2739. grid_coord *= (snap_step + snap_separation);
  2740. grid_coord += snap_offset;
  2741. edited_region.expand_to(grid_coord);
  2742. grid_coord += snap_step;
  2743. edited_region.expand_to(grid_coord);
  2744. } else {
  2745. edited_region.expand_to(end_point);
  2746. }
  2747. }
  2748. int TileSetEditor::get_current_tile() const {
  2749. return current_tile;
  2750. }
  2751. void TileSetEditor::set_current_tile(int p_id) {
  2752. if (current_tile != p_id) {
  2753. current_tile = p_id;
  2754. helper->_change_notify("");
  2755. select_coord(Vector2(0, 0));
  2756. update_workspace_tile_mode();
  2757. if (p_id == -1) {
  2758. editor->get_inspector()->edit(tileset.ptr());
  2759. } else {
  2760. editor->get_inspector()->edit(helper);
  2761. }
  2762. }
  2763. }
  2764. Ref<Texture> TileSetEditor::get_current_texture() {
  2765. if (texture_list->get_selected_items().size() == 0)
  2766. return Ref<Texture>();
  2767. else
  2768. return texture_map[texture_list->get_item_metadata(texture_list->get_selected_items()[0])];
  2769. }
  2770. void TilesetEditorContext::set_tileset(const Ref<TileSet> &p_tileset) {
  2771. tileset = p_tileset;
  2772. }
  2773. void TilesetEditorContext::set_snap_options_visible(bool p_visible) {
  2774. snap_options_visible = p_visible;
  2775. _change_notify("");
  2776. }
  2777. bool TilesetEditorContext::_set(const StringName &p_name, const Variant &p_value) {
  2778. String name = p_name.operator String();
  2779. if (name == "options_offset") {
  2780. Vector2 snap = p_value;
  2781. tileset_editor->_set_snap_off(snap + WORKSPACE_MARGIN);
  2782. return true;
  2783. } else if (name == "options_step") {
  2784. Vector2 snap = p_value;
  2785. tileset_editor->_set_snap_step(snap);
  2786. return true;
  2787. } else if (name == "options_separation") {
  2788. Vector2 snap = p_value;
  2789. tileset_editor->_set_snap_sep(snap);
  2790. return true;
  2791. } else if (p_name.operator String().left(5) == "tile_") {
  2792. String name2 = p_name.operator String().right(5);
  2793. bool v = false;
  2794. if (tileset_editor->get_current_tile() < 0 || tileset.is_null())
  2795. return false;
  2796. if (name2 == "autotile_bitmask_mode") {
  2797. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", p_value, &v);
  2798. } else if (name2 == "subtile_size") {
  2799. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/tile_size", p_value, &v);
  2800. } else if (name2 == "subtile_spacing") {
  2801. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/spacing", p_value, &v);
  2802. } else {
  2803. tileset->set(String::num(tileset_editor->get_current_tile(), 0) + "/" + name2, p_value, &v);
  2804. }
  2805. if (v) {
  2806. tileset->_change_notify("");
  2807. tileset_editor->workspace->update();
  2808. tileset_editor->workspace_overlay->update();
  2809. }
  2810. return v;
  2811. } else if (name == "tileset_script") {
  2812. tileset->set_script(p_value);
  2813. return true;
  2814. } else if (name == "selected_collision_one_way") {
  2815. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  2816. for (int index = 0; index < sd.size(); index++) {
  2817. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  2818. tileset->tile_set_shape_one_way(tileset_editor->get_current_tile(), index, p_value);
  2819. return true;
  2820. }
  2821. }
  2822. return false;
  2823. } else if (name == "selected_collision_one_way_margin") {
  2824. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  2825. for (int index = 0; index < sd.size(); index++) {
  2826. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  2827. tileset->tile_set_shape_one_way_margin(tileset_editor->get_current_tile(), index, p_value);
  2828. return true;
  2829. }
  2830. }
  2831. return false;
  2832. }
  2833. tileset_editor->err_dialog->set_text(TTR("This property can't be changed."));
  2834. tileset_editor->err_dialog->popup_centered(Size2(300, 60));
  2835. return false;
  2836. }
  2837. bool TilesetEditorContext::_get(const StringName &p_name, Variant &r_ret) const {
  2838. String name = p_name.operator String();
  2839. bool v = false;
  2840. if (name == "options_offset") {
  2841. r_ret = tileset_editor->snap_offset - WORKSPACE_MARGIN;
  2842. v = true;
  2843. } else if (name == "options_step") {
  2844. r_ret = tileset_editor->snap_step;
  2845. v = true;
  2846. } else if (name == "options_separation") {
  2847. r_ret = tileset_editor->snap_separation;
  2848. v = true;
  2849. } else if (name.left(5) == "tile_") {
  2850. name = name.right(5);
  2851. if (tileset_editor->get_current_tile() < 0 || tileset.is_null())
  2852. return false;
  2853. if (!tileset->has_tile(tileset_editor->get_current_tile()))
  2854. return false;
  2855. if (name == "autotile_bitmask_mode") {
  2856. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/bitmask_mode", &v);
  2857. } else if (name == "subtile_size") {
  2858. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/tile_size", &v);
  2859. } else if (name == "subtile_spacing") {
  2860. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/autotile/spacing", &v);
  2861. } else {
  2862. r_ret = tileset->get(String::num(tileset_editor->get_current_tile(), 0) + "/" + name, &v);
  2863. }
  2864. return v;
  2865. } else if (name == "selected_collision") {
  2866. r_ret = tileset_editor->edited_collision_shape;
  2867. v = true;
  2868. } else if (name == "selected_collision_one_way") {
  2869. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  2870. for (int index = 0; index < sd.size(); index++) {
  2871. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  2872. r_ret = sd[index].one_way_collision;
  2873. v = true;
  2874. break;
  2875. }
  2876. }
  2877. } else if (name == "selected_collision_one_way_margin") {
  2878. Vector<TileSet::ShapeData> sd = tileset->tile_get_shapes(tileset_editor->get_current_tile());
  2879. for (int index = 0; index < sd.size(); index++) {
  2880. if (sd[index].shape == tileset_editor->edited_collision_shape) {
  2881. r_ret = sd[index].one_way_collision_margin;
  2882. v = true;
  2883. break;
  2884. }
  2885. }
  2886. } else if (name == "selected_navigation") {
  2887. r_ret = tileset_editor->edited_navigation_shape;
  2888. v = true;
  2889. } else if (name == "selected_occlusion") {
  2890. r_ret = tileset_editor->edited_occlusion_shape;
  2891. v = true;
  2892. } else if (name == "tileset_script") {
  2893. r_ret = tileset->get_script();
  2894. v = true;
  2895. }
  2896. return v;
  2897. }
  2898. void TilesetEditorContext::_get_property_list(List<PropertyInfo> *p_list) const {
  2899. if (snap_options_visible) {
  2900. p_list->push_back(PropertyInfo(Variant::NIL, "Snap Options", PROPERTY_HINT_NONE, "options_", PROPERTY_USAGE_GROUP));
  2901. p_list->push_back(PropertyInfo(Variant::VECTOR2, "options_offset"));
  2902. p_list->push_back(PropertyInfo(Variant::VECTOR2, "options_step"));
  2903. p_list->push_back(PropertyInfo(Variant::VECTOR2, "options_separation"));
  2904. }
  2905. if (tileset_editor->get_current_tile() >= 0 && !tileset.is_null()) {
  2906. int id = tileset_editor->get_current_tile();
  2907. p_list->push_back(PropertyInfo(Variant::NIL, "Selected Tile", PROPERTY_HINT_NONE, "tile_", PROPERTY_USAGE_GROUP));
  2908. p_list->push_back(PropertyInfo(Variant::STRING, "tile_name"));
  2909. p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  2910. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_tex_offset"));
  2911. p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"));
  2912. p_list->push_back(PropertyInfo(Variant::COLOR, "tile_modulate"));
  2913. p_list->push_back(PropertyInfo(Variant::INT, "tile_tile_mode", PROPERTY_HINT_ENUM, "SINGLE_TILE,AUTO_TILE,ATLAS_TILE"));
  2914. if (tileset->tile_get_tile_mode(id) == TileSet::AUTO_TILE) {
  2915. p_list->push_back(PropertyInfo(Variant::INT, "tile_autotile_bitmask_mode", PROPERTY_HINT_ENUM, "2X2,3X3 (minimal),3X3"));
  2916. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_subtile_size"));
  2917. p_list->push_back(PropertyInfo(Variant::INT, "tile_subtile_spacing", PROPERTY_HINT_RANGE, "0, 256, 1"));
  2918. } else if (tileset->tile_get_tile_mode(id) == TileSet::ATLAS_TILE) {
  2919. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_subtile_size"));
  2920. p_list->push_back(PropertyInfo(Variant::INT, "tile_subtile_spacing", PROPERTY_HINT_RANGE, "0, 256, 1"));
  2921. }
  2922. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_occluder_offset"));
  2923. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_navigation_offset"));
  2924. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  2925. p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_shape_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  2926. 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"));
  2927. }
  2928. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_COLLISION && tileset_editor->edited_collision_shape.is_valid()) {
  2929. p_list->push_back(PropertyInfo(Variant::OBJECT, "selected_collision", PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_collision_shape->get_class()));
  2930. if (tileset_editor->edited_collision_shape.is_valid()) {
  2931. p_list->push_back(PropertyInfo(Variant::BOOL, "selected_collision_one_way", PROPERTY_HINT_NONE));
  2932. p_list->push_back(PropertyInfo(Variant::REAL, "selected_collision_one_way_margin", PROPERTY_HINT_NONE));
  2933. }
  2934. }
  2935. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_NAVIGATION && tileset_editor->edited_navigation_shape.is_valid()) {
  2936. p_list->push_back(PropertyInfo(Variant::OBJECT, "selected_navigation", PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_navigation_shape->get_class()));
  2937. }
  2938. if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_OCCLUSION && tileset_editor->edited_occlusion_shape.is_valid()) {
  2939. p_list->push_back(PropertyInfo(Variant::OBJECT, "selected_occlusion", PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_occlusion_shape->get_class()));
  2940. }
  2941. if (!tileset.is_null()) {
  2942. p_list->push_back(PropertyInfo(Variant::OBJECT, "tileset_script", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  2943. }
  2944. }
  2945. void TilesetEditorContext::_bind_methods() {
  2946. ClassDB::bind_method("_hide_script_from_inspector", &TilesetEditorContext::_hide_script_from_inspector);
  2947. }
  2948. TilesetEditorContext::TilesetEditorContext(TileSetEditor *p_tileset_editor) {
  2949. tileset_editor = p_tileset_editor;
  2950. snap_options_visible = false;
  2951. }
  2952. void TileSetEditorPlugin::edit(Object *p_node) {
  2953. if (Object::cast_to<TileSet>(p_node)) {
  2954. tileset_editor->edit(Object::cast_to<TileSet>(p_node));
  2955. }
  2956. }
  2957. bool TileSetEditorPlugin::handles(Object *p_node) const {
  2958. return p_node->is_class("TileSet") || p_node->is_class("TilesetEditorContext");
  2959. }
  2960. void TileSetEditorPlugin::make_visible(bool p_visible) {
  2961. if (p_visible) {
  2962. tileset_editor_button->show();
  2963. editor->make_bottom_panel_item_visible(tileset_editor);
  2964. get_tree()->connect("idle_frame", tileset_editor, "_on_workspace_process");
  2965. } else {
  2966. editor->hide_bottom_panel();
  2967. tileset_editor_button->hide();
  2968. get_tree()->disconnect("idle_frame", tileset_editor, "_on_workspace_process");
  2969. }
  2970. }
  2971. Dictionary TileSetEditorPlugin::get_state() const {
  2972. Dictionary state;
  2973. state["snap_offset"] = tileset_editor->snap_offset;
  2974. state["snap_step"] = tileset_editor->snap_step;
  2975. state["snap_separation"] = tileset_editor->snap_separation;
  2976. state["snap_enabled"] = tileset_editor->tools[TileSetEditor::TOOL_GRID_SNAP]->is_pressed();
  2977. state["keep_inside_tile"] = tileset_editor->tools[TileSetEditor::SHAPE_KEEP_INSIDE_TILE]->is_pressed();
  2978. state["show_information"] = tileset_editor->tools[TileSetEditor::VISIBLE_INFO]->is_pressed();
  2979. return state;
  2980. }
  2981. void TileSetEditorPlugin::set_state(const Dictionary &p_state) {
  2982. Dictionary state = p_state;
  2983. if (state.has("snap_step")) {
  2984. tileset_editor->_set_snap_step(state["snap_step"]);
  2985. }
  2986. if (state.has("snap_offset")) {
  2987. tileset_editor->_set_snap_off(state["snap_offset"]);
  2988. }
  2989. if (state.has("snap_separation")) {
  2990. tileset_editor->_set_snap_sep(state["snap_separation"]);
  2991. }
  2992. if (state.has("snap_enabled")) {
  2993. tileset_editor->tools[TileSetEditor::TOOL_GRID_SNAP]->set_pressed(state["snap_enabled"]);
  2994. if (tileset_editor->helper) {
  2995. tileset_editor->_on_grid_snap_toggled(state["snap_enabled"]);
  2996. }
  2997. }
  2998. if (state.has("keep_inside_tile")) {
  2999. tileset_editor->tools[TileSetEditor::SHAPE_KEEP_INSIDE_TILE]->set_pressed(state["keep_inside_tile"]);
  3000. }
  3001. if (state.has("show_information")) {
  3002. tileset_editor->tools[TileSetEditor::VISIBLE_INFO]->set_pressed(state["show_information"]);
  3003. }
  3004. }
  3005. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  3006. editor = p_node;
  3007. tileset_editor = memnew(TileSetEditor(p_node));
  3008. tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  3009. tileset_editor->hide();
  3010. tileset_editor_button = p_node->add_bottom_panel_item(TTR("TileSet"), tileset_editor);
  3011. tileset_editor_button->hide();
  3012. }