tile_set_editor_plugin.cpp 109 KB

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