tile_set_editor_plugin.cpp 108 KB

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