tile_set_editor_plugin.cpp 91 KB

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