tile_set_editor_plugin.cpp 142 KB

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