2
0

tile_data_editors.cpp 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  1. /*************************************************************************/
  2. /* tile_data_editors.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_data_editors.h"
  31. #include "tile_set_editor.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_properties.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #ifdef DEBUG_ENABLED
  39. #include "servers/navigation_server_3d.h"
  40. #endif // DEBUG_ENABLED
  41. void TileDataEditor::_tile_set_changed_plan_update() {
  42. _tile_set_changed_update_needed = true;
  43. call_deferred(SNAME("_tile_set_changed_deferred_update"));
  44. }
  45. void TileDataEditor::_tile_set_changed_deferred_update() {
  46. if (_tile_set_changed_update_needed) {
  47. _tile_set_changed();
  48. _tile_set_changed_update_needed = false;
  49. }
  50. }
  51. TileData *TileDataEditor::_get_tile_data(TileMapCell p_cell) {
  52. ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
  53. ERR_FAIL_COND_V(!tile_set->has_source(p_cell.source_id), nullptr);
  54. TileData *td = nullptr;
  55. TileSetSource *source = *tile_set->get_source(p_cell.source_id);
  56. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  57. if (atlas_source) {
  58. ERR_FAIL_COND_V(!atlas_source->has_tile(p_cell.get_atlas_coords()), nullptr);
  59. ERR_FAIL_COND_V(!atlas_source->has_alternative_tile(p_cell.get_atlas_coords(), p_cell.alternative_tile), nullptr);
  60. td = atlas_source->get_tile_data(p_cell.get_atlas_coords(), p_cell.alternative_tile);
  61. }
  62. return td;
  63. }
  64. void TileDataEditor::_bind_methods() {
  65. ClassDB::bind_method(D_METHOD("_tile_set_changed_deferred_update"), &TileDataEditor::_tile_set_changed_deferred_update);
  66. ADD_SIGNAL(MethodInfo("needs_redraw"));
  67. }
  68. void TileDataEditor::set_tile_set(Ref<TileSet> p_tile_set) {
  69. if (tile_set.is_valid()) {
  70. tile_set->disconnect("changed", callable_mp(this, &TileDataEditor::_tile_set_changed_plan_update));
  71. }
  72. tile_set = p_tile_set;
  73. if (tile_set.is_valid()) {
  74. tile_set->connect("changed", callable_mp(this, &TileDataEditor::_tile_set_changed_plan_update));
  75. }
  76. _tile_set_changed_plan_update();
  77. }
  78. bool DummyObject::_set(const StringName &p_name, const Variant &p_value) {
  79. if (properties.has(p_name)) {
  80. properties[p_name] = p_value;
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool DummyObject::_get(const StringName &p_name, Variant &r_ret) const {
  86. if (properties.has(p_name)) {
  87. r_ret = properties[p_name];
  88. return true;
  89. }
  90. return false;
  91. }
  92. bool DummyObject::has_dummy_property(StringName p_name) {
  93. return properties.has(p_name);
  94. }
  95. void DummyObject::add_dummy_property(StringName p_name) {
  96. ERR_FAIL_COND(properties.has(p_name));
  97. properties[p_name] = Variant();
  98. }
  99. void DummyObject::remove_dummy_property(StringName p_name) {
  100. ERR_FAIL_COND(!properties.has(p_name));
  101. properties.erase(p_name);
  102. }
  103. void DummyObject::clear_dummy_properties() {
  104. properties.clear();
  105. }
  106. void GenericTilePolygonEditor::_base_control_draw() {
  107. ERR_FAIL_COND(!tile_set.is_valid());
  108. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  109. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  110. const Ref<Texture2D> handle = get_theme_icon(SNAME("EditorPathSharpHandle"), SNAME("EditorIcons"));
  111. const Ref<Texture2D> add_handle = get_theme_icon(SNAME("EditorHandleAdd"), SNAME("EditorIcons"));
  112. const Ref<StyleBox> focus_stylebox = get_theme_stylebox(SNAME("Focus"), SNAME("EditorStyles"));
  113. // Draw the focus rectangle.
  114. if (base_control->has_focus()) {
  115. base_control->draw_style_box(focus_stylebox, Rect2(Vector2(), base_control->get_size()));
  116. }
  117. // Draw tile-related things.
  118. Size2 tile_size = tile_set->get_tile_size();
  119. Transform2D xform;
  120. xform.set_origin(base_control->get_size() / 2 + panning);
  121. xform.set_scale(Vector2(editor_zoom_widget->get_zoom(), editor_zoom_widget->get_zoom()));
  122. base_control->draw_set_transform_matrix(xform);
  123. // Draw the tile shape filled.
  124. Transform2D tile_xform;
  125. tile_xform.set_scale(tile_size);
  126. tile_set->draw_tile_shape(base_control, tile_xform, Color(1.0, 1.0, 1.0, 0.3), true);
  127. // Draw the background.
  128. if (background_texture.is_valid()) {
  129. base_control->draw_texture_rect_region(background_texture, Rect2(-background_region.size / 2 - background_offset, background_region.size), background_region, background_modulate, background_transpose);
  130. }
  131. // Draw the polygons.
  132. for (unsigned int i = 0; i < polygons.size(); i++) {
  133. const Vector<Vector2> &polygon = polygons[i];
  134. Color color = polygon_color;
  135. if (!in_creation_polygon.is_empty()) {
  136. color = color.darkened(0.3);
  137. }
  138. color.a = 0.5;
  139. Vector<Color> v_color;
  140. v_color.push_back(color);
  141. base_control->draw_polygon(polygon, v_color);
  142. color.a = 0.7;
  143. for (int j = 0; j < polygon.size(); j++) {
  144. base_control->draw_line(polygon[j], polygon[(j + 1) % polygon.size()], color);
  145. }
  146. }
  147. // Draw the polygon in creation.
  148. if (!in_creation_polygon.is_empty()) {
  149. for (int i = 0; i < in_creation_polygon.size() - 1; i++) {
  150. base_control->draw_line(in_creation_polygon[i], in_creation_polygon[i + 1], Color(1.0, 1.0, 1.0));
  151. }
  152. }
  153. Point2 in_creation_point = xform.affine_inverse().xform(base_control->get_local_mouse_position());
  154. float in_creation_distance = grab_threshold * 2.0;
  155. _snap_to_tile_shape(in_creation_point, in_creation_distance, grab_threshold / editor_zoom_widget->get_zoom());
  156. if (button_pixel_snap->is_pressed()) {
  157. _snap_to_half_pixel(in_creation_point);
  158. }
  159. if (drag_type == DRAG_TYPE_CREATE_POINT && !in_creation_polygon.is_empty()) {
  160. base_control->draw_line(in_creation_polygon[in_creation_polygon.size() - 1], in_creation_point, Color(1.0, 1.0, 1.0));
  161. }
  162. // Draw the handles.
  163. int tinted_polygon_index = -1;
  164. int tinted_point_index = -1;
  165. if (drag_type == DRAG_TYPE_DRAG_POINT) {
  166. tinted_polygon_index = drag_polygon_index;
  167. tinted_point_index = drag_point_index;
  168. } else if (hovered_point_index >= 0) {
  169. tinted_polygon_index = hovered_polygon_index;
  170. tinted_point_index = hovered_point_index;
  171. }
  172. base_control->draw_set_transform_matrix(Transform2D());
  173. if (!in_creation_polygon.is_empty()) {
  174. for (int i = 0; i < in_creation_polygon.size(); i++) {
  175. base_control->draw_texture(handle, xform.xform(in_creation_polygon[i]) - handle->get_size() / 2);
  176. }
  177. } else {
  178. for (int i = 0; i < (int)polygons.size(); i++) {
  179. const Vector<Vector2> &polygon = polygons[i];
  180. for (int j = 0; j < polygon.size(); j++) {
  181. const Color poly_modulate = (tinted_polygon_index == i && tinted_point_index == j) ? Color(0.5, 1, 2) : Color(1, 1, 1);
  182. base_control->draw_texture(handle, xform.xform(polygon[j]) - handle->get_size() / 2, poly_modulate);
  183. }
  184. }
  185. }
  186. // Draw the text on top of the selected point.
  187. if (tinted_polygon_index >= 0) {
  188. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  189. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  190. String text = multiple_polygon_mode ? vformat("%d:%d", tinted_polygon_index, tinted_point_index) : vformat("%d", tinted_point_index);
  191. Size2 text_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  192. base_control->draw_string(font, xform.xform(polygons[tinted_polygon_index][tinted_point_index]) - text_size * 0.5, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1.0, 1.0, 1.0, 0.5));
  193. }
  194. if (drag_type == DRAG_TYPE_CREATE_POINT) {
  195. base_control->draw_texture(handle, xform.xform(in_creation_point) - handle->get_size() / 2, Color(0.5, 1, 2));
  196. }
  197. // Draw the point creation preview in edit mode.
  198. if (hovered_segment_index >= 0) {
  199. base_control->draw_texture(add_handle, xform.xform(hovered_segment_point) - add_handle->get_size() / 2);
  200. }
  201. // Draw the tile shape line.
  202. base_control->draw_set_transform_matrix(xform);
  203. tile_set->draw_tile_shape(base_control, tile_xform, grid_color, false);
  204. base_control->draw_set_transform_matrix(Transform2D());
  205. }
  206. void GenericTilePolygonEditor::_center_view() {
  207. panning = Vector2();
  208. base_control->queue_redraw();
  209. button_center_view->set_disabled(true);
  210. }
  211. void GenericTilePolygonEditor::_zoom_changed() {
  212. base_control->queue_redraw();
  213. }
  214. void GenericTilePolygonEditor::_advanced_menu_item_pressed(int p_item_pressed) {
  215. Ref<EditorUndoRedoManager> undo_redo;
  216. if (use_undo_redo) {
  217. undo_redo = editor_undo_redo;
  218. } else {
  219. // This nice hack allows for discarding undo actions without making code too complex.
  220. undo_redo.instantiate();
  221. }
  222. switch (p_item_pressed) {
  223. case RESET_TO_DEFAULT_TILE: {
  224. undo_redo->create_action(TTR("Reset Polygons"));
  225. undo_redo->add_do_method(this, "clear_polygons");
  226. Vector<Vector2> polygon = tile_set->get_tile_shape_polygon();
  227. for (int i = 0; i < polygon.size(); i++) {
  228. polygon.write[i] = polygon[i] * tile_set->get_tile_size();
  229. }
  230. undo_redo->add_do_method(this, "add_polygon", polygon);
  231. undo_redo->add_do_method(base_control, "queue_redraw");
  232. undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
  233. undo_redo->add_undo_method(this, "clear_polygons");
  234. for (unsigned int i = 0; i < polygons.size(); i++) {
  235. undo_redo->add_undo_method(this, "add_polygon", polygons[i]);
  236. }
  237. undo_redo->add_undo_method(base_control, "queue_redraw");
  238. undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
  239. undo_redo->commit_action(true);
  240. } break;
  241. case CLEAR_TILE: {
  242. undo_redo->create_action(TTR("Clear Polygons"));
  243. undo_redo->add_do_method(this, "clear_polygons");
  244. undo_redo->add_do_method(base_control, "queue_redraw");
  245. undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
  246. undo_redo->add_undo_method(this, "clear_polygons");
  247. for (unsigned int i = 0; i < polygons.size(); i++) {
  248. undo_redo->add_undo_method(this, "add_polygon", polygons[i]);
  249. }
  250. undo_redo->add_undo_method(base_control, "queue_redraw");
  251. undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
  252. undo_redo->commit_action(true);
  253. } break;
  254. case ROTATE_RIGHT:
  255. case ROTATE_LEFT:
  256. case FLIP_HORIZONTALLY:
  257. case FLIP_VERTICALLY: {
  258. undo_redo->create_action(TTR("Rotate Polygons Left"));
  259. for (unsigned int i = 0; i < polygons.size(); i++) {
  260. Vector<Point2> new_polygon;
  261. for (int point_index = 0; point_index < polygons[i].size(); point_index++) {
  262. Vector2 point = polygons[i][point_index];
  263. switch (p_item_pressed) {
  264. case ROTATE_RIGHT: {
  265. point = Vector2(-point.y, point.x);
  266. } break;
  267. case ROTATE_LEFT: {
  268. point = Vector2(point.y, -point.x);
  269. } break;
  270. case FLIP_HORIZONTALLY: {
  271. point = Vector2(-point.x, point.y);
  272. } break;
  273. case FLIP_VERTICALLY: {
  274. point = Vector2(point.x, -point.y);
  275. } break;
  276. default:
  277. break;
  278. }
  279. new_polygon.push_back(point);
  280. }
  281. undo_redo->add_do_method(this, "set_polygon", i, new_polygon);
  282. }
  283. undo_redo->add_do_method(base_control, "queue_redraw");
  284. undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
  285. for (unsigned int i = 0; i < polygons.size(); i++) {
  286. undo_redo->add_undo_method(this, "set_polygon", polygons[i]);
  287. }
  288. undo_redo->add_undo_method(base_control, "queue_redraw");
  289. undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
  290. undo_redo->commit_action(true);
  291. } break;
  292. default:
  293. break;
  294. }
  295. }
  296. void GenericTilePolygonEditor::_grab_polygon_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_point_index) {
  297. const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  298. r_polygon_index = -1;
  299. r_point_index = -1;
  300. float closest_distance = grab_threshold + 1.0;
  301. for (unsigned int i = 0; i < polygons.size(); i++) {
  302. const Vector<Vector2> &polygon = polygons[i];
  303. for (int j = 0; j < polygon.size(); j++) {
  304. float distance = p_pos.distance_to(p_polygon_xform.xform(polygon[j]));
  305. if (distance < grab_threshold && distance < closest_distance) {
  306. r_polygon_index = i;
  307. r_point_index = j;
  308. closest_distance = distance;
  309. }
  310. }
  311. }
  312. }
  313. void GenericTilePolygonEditor::_grab_polygon_segment_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_segment_index, Vector2 &r_point) {
  314. const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  315. Point2 point = p_polygon_xform.affine_inverse().xform(p_pos);
  316. r_polygon_index = -1;
  317. r_segment_index = -1;
  318. float closest_distance = grab_threshold * 2.0;
  319. for (unsigned int i = 0; i < polygons.size(); i++) {
  320. const Vector<Vector2> &polygon = polygons[i];
  321. for (int j = 0; j < polygon.size(); j++) {
  322. Vector2 segment[2] = { polygon[j], polygon[(j + 1) % polygon.size()] };
  323. Vector2 closest_point = Geometry2D::get_closest_point_to_segment(point, segment);
  324. float distance = closest_point.distance_to(point);
  325. if (distance < grab_threshold / editor_zoom_widget->get_zoom() && distance < closest_distance) {
  326. r_polygon_index = i;
  327. r_segment_index = j;
  328. r_point = closest_point;
  329. closest_distance = distance;
  330. }
  331. }
  332. }
  333. }
  334. void GenericTilePolygonEditor::_snap_to_tile_shape(Point2 &r_point, float &r_current_snapped_dist, float p_snap_dist) {
  335. ERR_FAIL_COND(!tile_set.is_valid());
  336. Vector<Point2> polygon = tile_set->get_tile_shape_polygon();
  337. for (int i = 0; i < polygon.size(); i++) {
  338. polygon.write[i] = polygon[i] * tile_set->get_tile_size();
  339. }
  340. Point2 snapped_point = r_point;
  341. // Snap to polygon vertices.
  342. bool snapped = false;
  343. for (int i = 0; i < polygon.size(); i++) {
  344. float distance = r_point.distance_to(polygon[i]);
  345. if (distance < p_snap_dist && distance < r_current_snapped_dist) {
  346. snapped_point = polygon[i];
  347. r_current_snapped_dist = distance;
  348. snapped = true;
  349. }
  350. }
  351. // Snap to edges if we did not snap to vertices.
  352. if (!snapped) {
  353. for (int i = 0; i < polygon.size(); i++) {
  354. Point2 segment[2] = { polygon[i], polygon[(i + 1) % polygon.size()] };
  355. Point2 point = Geometry2D::get_closest_point_to_segment(r_point, segment);
  356. float distance = r_point.distance_to(point);
  357. if (distance < p_snap_dist && distance < r_current_snapped_dist) {
  358. snapped_point = point;
  359. r_current_snapped_dist = distance;
  360. }
  361. }
  362. }
  363. r_point = snapped_point;
  364. }
  365. void GenericTilePolygonEditor::_snap_to_half_pixel(Point2 &r_point) {
  366. r_point = (r_point * 2).round() / 2.0;
  367. }
  368. void GenericTilePolygonEditor::_base_control_gui_input(Ref<InputEvent> p_event) {
  369. Ref<EditorUndoRedoManager> undo_redo;
  370. if (use_undo_redo) {
  371. undo_redo = editor_undo_redo;
  372. } else {
  373. // This nice hack allows for discarding undo actions without making code too complex.
  374. undo_redo.instantiate();
  375. }
  376. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  377. hovered_polygon_index = -1;
  378. hovered_point_index = -1;
  379. hovered_segment_index = -1;
  380. hovered_segment_point = Vector2();
  381. Transform2D xform;
  382. xform.set_origin(base_control->get_size() / 2 + panning);
  383. xform.set_scale(Vector2(editor_zoom_widget->get_zoom(), editor_zoom_widget->get_zoom()));
  384. Ref<InputEventMouseMotion> mm = p_event;
  385. if (mm.is_valid()) {
  386. if (drag_type == DRAG_TYPE_DRAG_POINT) {
  387. ERR_FAIL_INDEX(drag_polygon_index, (int)polygons.size());
  388. ERR_FAIL_INDEX(drag_point_index, polygons[drag_polygon_index].size());
  389. Point2 point = xform.affine_inverse().xform(mm->get_position());
  390. float distance = grab_threshold * 2.0;
  391. _snap_to_tile_shape(point, distance, grab_threshold / editor_zoom_widget->get_zoom());
  392. if (button_pixel_snap->is_pressed()) {
  393. _snap_to_half_pixel(point);
  394. }
  395. polygons[drag_polygon_index].write[drag_point_index] = point;
  396. } else if (drag_type == DRAG_TYPE_PAN) {
  397. panning += mm->get_position() - drag_last_pos;
  398. drag_last_pos = mm->get_position();
  399. button_center_view->set_disabled(panning.is_zero_approx());
  400. } else {
  401. // Update hovered point.
  402. _grab_polygon_point(mm->get_position(), xform, hovered_polygon_index, hovered_point_index);
  403. // If we have no hovered point, check if we hover a segment.
  404. if (hovered_point_index == -1) {
  405. _grab_polygon_segment_point(mm->get_position(), xform, hovered_polygon_index, hovered_segment_index, hovered_segment_point);
  406. }
  407. }
  408. }
  409. Ref<InputEventMouseButton> mb = p_event;
  410. if (mb.is_valid()) {
  411. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_ctrl_pressed()) {
  412. editor_zoom_widget->set_zoom_by_increments(1);
  413. _zoom_changed();
  414. accept_event();
  415. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_ctrl_pressed()) {
  416. editor_zoom_widget->set_zoom_by_increments(-1);
  417. _zoom_changed();
  418. accept_event();
  419. } else if (mb->get_button_index() == MouseButton::LEFT) {
  420. if (mb->is_pressed()) {
  421. if (tools_button_group->get_pressed_button() != button_create) {
  422. in_creation_polygon.clear();
  423. }
  424. if (tools_button_group->get_pressed_button() == button_create) {
  425. // Create points.
  426. if (in_creation_polygon.size() >= 3 && mb->get_position().distance_to(xform.xform(in_creation_polygon[0])) < grab_threshold) {
  427. // Closes and create polygon.
  428. if (!multiple_polygon_mode) {
  429. clear_polygons();
  430. }
  431. int added = add_polygon(in_creation_polygon);
  432. in_creation_polygon.clear();
  433. button_edit->set_pressed(true);
  434. undo_redo->create_action(TTR("Edit Polygons"));
  435. if (!multiple_polygon_mode) {
  436. undo_redo->add_do_method(this, "clear_polygons");
  437. }
  438. undo_redo->add_do_method(this, "add_polygon", in_creation_polygon);
  439. undo_redo->add_do_method(base_control, "queue_redraw");
  440. undo_redo->add_undo_method(this, "remove_polygon", added);
  441. undo_redo->add_undo_method(base_control, "queue_redraw");
  442. undo_redo->commit_action(false);
  443. emit_signal(SNAME("polygons_changed"));
  444. } else {
  445. // Create a new point.
  446. drag_type = DRAG_TYPE_CREATE_POINT;
  447. }
  448. } else if (tools_button_group->get_pressed_button() == button_edit) {
  449. // Edit points.
  450. int closest_polygon;
  451. int closest_point;
  452. _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
  453. if (closest_polygon >= 0) {
  454. drag_type = DRAG_TYPE_DRAG_POINT;
  455. drag_polygon_index = closest_polygon;
  456. drag_point_index = closest_point;
  457. drag_old_polygon = polygons[drag_polygon_index];
  458. } else {
  459. // Create a point.
  460. Vector2 point_to_create;
  461. _grab_polygon_segment_point(mb->get_position(), xform, closest_polygon, closest_point, point_to_create);
  462. if (closest_polygon >= 0) {
  463. polygons[closest_polygon].insert(closest_point + 1, point_to_create);
  464. drag_type = DRAG_TYPE_DRAG_POINT;
  465. drag_polygon_index = closest_polygon;
  466. drag_point_index = closest_point + 1;
  467. drag_old_polygon = polygons[closest_polygon];
  468. }
  469. }
  470. } else if (tools_button_group->get_pressed_button() == button_delete) {
  471. // Remove point.
  472. int closest_polygon;
  473. int closest_point;
  474. _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
  475. if (closest_polygon >= 0) {
  476. PackedVector2Array old_polygon = polygons[closest_polygon];
  477. polygons[closest_polygon].remove_at(closest_point);
  478. undo_redo->create_action(TTR("Edit Polygons"));
  479. if (polygons[closest_polygon].size() < 3) {
  480. remove_polygon(closest_polygon);
  481. undo_redo->add_do_method(this, "remove_polygon", closest_polygon);
  482. undo_redo->add_undo_method(this, "add_polygon", old_polygon, closest_polygon);
  483. } else {
  484. undo_redo->add_do_method(this, "set_polygon", closest_polygon, polygons[closest_polygon]);
  485. undo_redo->add_undo_method(this, "set_polygon", closest_polygon, old_polygon);
  486. }
  487. undo_redo->add_do_method(base_control, "queue_redraw");
  488. undo_redo->add_undo_method(base_control, "queue_redraw");
  489. undo_redo->commit_action(false);
  490. emit_signal(SNAME("polygons_changed"));
  491. }
  492. }
  493. } else {
  494. if (drag_type == DRAG_TYPE_DRAG_POINT) {
  495. undo_redo->create_action(TTR("Edit Polygons"));
  496. undo_redo->add_do_method(this, "set_polygon", drag_polygon_index, polygons[drag_polygon_index]);
  497. undo_redo->add_do_method(base_control, "queue_redraw");
  498. undo_redo->add_undo_method(this, "set_polygon", drag_polygon_index, drag_old_polygon);
  499. undo_redo->add_undo_method(base_control, "queue_redraw");
  500. undo_redo->commit_action(false);
  501. emit_signal(SNAME("polygons_changed"));
  502. } else if (drag_type == DRAG_TYPE_CREATE_POINT) {
  503. Point2 point = xform.affine_inverse().xform(mb->get_position());
  504. float distance = grab_threshold * 2;
  505. _snap_to_tile_shape(point, distance, grab_threshold / editor_zoom_widget->get_zoom());
  506. if (button_pixel_snap->is_pressed()) {
  507. _snap_to_half_pixel(point);
  508. }
  509. in_creation_polygon.push_back(point);
  510. }
  511. drag_type = DRAG_TYPE_NONE;
  512. drag_point_index = -1;
  513. }
  514. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  515. if (mb->is_pressed()) {
  516. if (tools_button_group->get_pressed_button() == button_edit) {
  517. // Remove point or pan.
  518. int closest_polygon;
  519. int closest_point;
  520. _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
  521. if (closest_polygon >= 0) {
  522. PackedVector2Array old_polygon = polygons[closest_polygon];
  523. polygons[closest_polygon].remove_at(closest_point);
  524. undo_redo->create_action(TTR("Edit Polygons"));
  525. if (polygons[closest_polygon].size() < 3) {
  526. remove_polygon(closest_polygon);
  527. undo_redo->add_do_method(this, "remove_polygon", closest_polygon);
  528. undo_redo->add_undo_method(this, "add_polygon", old_polygon, closest_polygon);
  529. } else {
  530. undo_redo->add_do_method(this, "set_polygon", closest_polygon, polygons[closest_polygon]);
  531. undo_redo->add_undo_method(this, "set_polygon", closest_polygon, old_polygon);
  532. }
  533. undo_redo->add_do_method(base_control, "queue_redraw");
  534. undo_redo->add_undo_method(base_control, "queue_redraw");
  535. undo_redo->commit_action(false);
  536. emit_signal(SNAME("polygons_changed"));
  537. } else {
  538. drag_type = DRAG_TYPE_PAN;
  539. drag_last_pos = mb->get_position();
  540. }
  541. } else {
  542. drag_type = DRAG_TYPE_PAN;
  543. drag_last_pos = mb->get_position();
  544. }
  545. } else {
  546. drag_type = DRAG_TYPE_NONE;
  547. }
  548. } else if (mb->get_button_index() == MouseButton::MIDDLE) {
  549. if (mb->is_pressed()) {
  550. drag_type = DRAG_TYPE_PAN;
  551. drag_last_pos = mb->get_position();
  552. } else {
  553. drag_type = DRAG_TYPE_NONE;
  554. }
  555. }
  556. }
  557. base_control->queue_redraw();
  558. }
  559. void GenericTilePolygonEditor::set_use_undo_redo(bool p_use_undo_redo) {
  560. use_undo_redo = p_use_undo_redo;
  561. }
  562. void GenericTilePolygonEditor::set_tile_set(Ref<TileSet> p_tile_set) {
  563. ERR_FAIL_COND(!p_tile_set.is_valid());
  564. if (tile_set == p_tile_set) {
  565. return;
  566. }
  567. // Set the default tile shape
  568. clear_polygons();
  569. if (p_tile_set.is_valid()) {
  570. Vector<Vector2> polygon = p_tile_set->get_tile_shape_polygon();
  571. for (int i = 0; i < polygon.size(); i++) {
  572. polygon.write[i] = polygon[i] * p_tile_set->get_tile_size();
  573. }
  574. add_polygon(polygon);
  575. }
  576. tile_set = p_tile_set;
  577. // Set the default zoom value.
  578. int default_control_y_size = 200 * EDSCALE;
  579. Vector2 zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
  580. while (zoomed_tile.y < default_control_y_size) {
  581. editor_zoom_widget->set_zoom_by_increments(6, false);
  582. zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
  583. }
  584. while (zoomed_tile.y > default_control_y_size) {
  585. editor_zoom_widget->set_zoom_by_increments(-6, false);
  586. zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
  587. }
  588. editor_zoom_widget->set_zoom_by_increments(-6, false);
  589. _zoom_changed();
  590. }
  591. void GenericTilePolygonEditor::set_background(Ref<Texture2D> p_texture, Rect2 p_region, Vector2 p_offset, bool p_flip_h, bool p_flip_v, bool p_transpose, Color p_modulate) {
  592. background_texture = p_texture;
  593. background_region = p_region;
  594. background_offset = p_offset;
  595. background_h_flip = p_flip_h;
  596. background_v_flip = p_flip_v;
  597. background_transpose = p_transpose;
  598. background_modulate = p_modulate;
  599. base_control->queue_redraw();
  600. }
  601. int GenericTilePolygonEditor::get_polygon_count() {
  602. return polygons.size();
  603. }
  604. int GenericTilePolygonEditor::add_polygon(Vector<Point2> p_polygon, int p_index) {
  605. ERR_FAIL_COND_V(p_polygon.size() < 3, -1);
  606. ERR_FAIL_COND_V(!multiple_polygon_mode && polygons.size() >= 1, -1);
  607. if (p_index < 0) {
  608. polygons.push_back(p_polygon);
  609. base_control->queue_redraw();
  610. button_edit->set_pressed(true);
  611. return polygons.size() - 1;
  612. } else {
  613. polygons.insert(p_index, p_polygon);
  614. button_edit->set_pressed(true);
  615. base_control->queue_redraw();
  616. return p_index;
  617. }
  618. }
  619. void GenericTilePolygonEditor::remove_polygon(int p_index) {
  620. ERR_FAIL_INDEX(p_index, (int)polygons.size());
  621. polygons.remove_at(p_index);
  622. if (polygons.size() == 0) {
  623. button_create->set_pressed(true);
  624. }
  625. base_control->queue_redraw();
  626. }
  627. void GenericTilePolygonEditor::clear_polygons() {
  628. polygons.clear();
  629. base_control->queue_redraw();
  630. }
  631. void GenericTilePolygonEditor::set_polygon(int p_polygon_index, Vector<Point2> p_polygon) {
  632. ERR_FAIL_INDEX(p_polygon_index, (int)polygons.size());
  633. ERR_FAIL_COND(p_polygon.size() < 3);
  634. polygons[p_polygon_index] = p_polygon;
  635. button_edit->set_pressed(true);
  636. base_control->queue_redraw();
  637. }
  638. Vector<Point2> GenericTilePolygonEditor::get_polygon(int p_polygon_index) {
  639. ERR_FAIL_INDEX_V(p_polygon_index, (int)polygons.size(), Vector<Point2>());
  640. return polygons[p_polygon_index];
  641. }
  642. void GenericTilePolygonEditor::set_polygons_color(Color p_color) {
  643. polygon_color = p_color;
  644. base_control->queue_redraw();
  645. }
  646. void GenericTilePolygonEditor::set_multiple_polygon_mode(bool p_multiple_polygon_mode) {
  647. multiple_polygon_mode = p_multiple_polygon_mode;
  648. }
  649. void GenericTilePolygonEditor::_notification(int p_what) {
  650. switch (p_what) {
  651. case NOTIFICATION_ENTER_TREE:
  652. case NOTIFICATION_THEME_CHANGED: {
  653. button_create->set_icon(get_theme_icon(SNAME("CurveCreate"), SNAME("EditorIcons")));
  654. button_edit->set_icon(get_theme_icon(SNAME("CurveEdit"), SNAME("EditorIcons")));
  655. button_delete->set_icon(get_theme_icon(SNAME("CurveDelete"), SNAME("EditorIcons")));
  656. button_center_view->set_icon(get_theme_icon(SNAME("CenterView"), SNAME("EditorIcons")));
  657. button_pixel_snap->set_icon(get_theme_icon(SNAME("Snap"), SNAME("EditorIcons")));
  658. button_advanced_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  659. PopupMenu *p = button_advanced_menu->get_popup();
  660. p->set_item_icon(p->get_item_index(ROTATE_RIGHT), get_theme_icon(SNAME("RotateRight"), SNAME("EditorIcons")));
  661. p->set_item_icon(p->get_item_index(ROTATE_LEFT), get_theme_icon(SNAME("RotateLeft"), SNAME("EditorIcons")));
  662. p->set_item_icon(p->get_item_index(FLIP_HORIZONTALLY), get_theme_icon(SNAME("MirrorX"), SNAME("EditorIcons")));
  663. p->set_item_icon(p->get_item_index(FLIP_VERTICALLY), get_theme_icon(SNAME("MirrorY"), SNAME("EditorIcons")));
  664. } break;
  665. }
  666. }
  667. void GenericTilePolygonEditor::_bind_methods() {
  668. ClassDB::bind_method(D_METHOD("get_polygon_count"), &GenericTilePolygonEditor::get_polygon_count);
  669. ClassDB::bind_method(D_METHOD("add_polygon", "polygon", "index"), &GenericTilePolygonEditor::add_polygon, DEFVAL(-1));
  670. ClassDB::bind_method(D_METHOD("remove_polygon", "index"), &GenericTilePolygonEditor::remove_polygon);
  671. ClassDB::bind_method(D_METHOD("clear_polygons"), &GenericTilePolygonEditor::clear_polygons);
  672. ClassDB::bind_method(D_METHOD("set_polygon", "index", "polygon"), &GenericTilePolygonEditor::set_polygon);
  673. ClassDB::bind_method(D_METHOD("get_polygon", "index"), &GenericTilePolygonEditor::set_polygon);
  674. ADD_SIGNAL(MethodInfo("polygons_changed"));
  675. }
  676. GenericTilePolygonEditor::GenericTilePolygonEditor() {
  677. editor_undo_redo = EditorNode::get_undo_redo();
  678. toolbar = memnew(HBoxContainer);
  679. add_child(toolbar);
  680. tools_button_group.instantiate();
  681. button_create = memnew(Button);
  682. button_create->set_flat(true);
  683. button_create->set_toggle_mode(true);
  684. button_create->set_button_group(tools_button_group);
  685. button_create->set_pressed(true);
  686. button_create->set_tooltip_text(TTR("Add polygon tool"));
  687. toolbar->add_child(button_create);
  688. button_edit = memnew(Button);
  689. button_edit->set_flat(true);
  690. button_edit->set_toggle_mode(true);
  691. button_edit->set_button_group(tools_button_group);
  692. button_edit->set_tooltip_text(TTR("Edit points tool"));
  693. toolbar->add_child(button_edit);
  694. button_delete = memnew(Button);
  695. button_delete->set_flat(true);
  696. button_delete->set_toggle_mode(true);
  697. button_delete->set_button_group(tools_button_group);
  698. button_delete->set_tooltip_text(TTR("Delete points tool"));
  699. toolbar->add_child(button_delete);
  700. button_advanced_menu = memnew(MenuButton);
  701. button_advanced_menu->set_flat(true);
  702. button_advanced_menu->set_toggle_mode(true);
  703. button_advanced_menu->get_popup()->add_item(TTR("Reset to default tile shape"), RESET_TO_DEFAULT_TILE, Key::F);
  704. button_advanced_menu->get_popup()->add_item(TTR("Clear"), CLEAR_TILE, Key::C);
  705. button_advanced_menu->get_popup()->add_separator();
  706. button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("RotateRight"), SNAME("EditorIcons")), TTR("Rotate Right"), ROTATE_RIGHT, Key::R);
  707. button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("RotateLeft"), SNAME("EditorIcons")), TTR("Rotate Left"), ROTATE_LEFT, Key::E);
  708. button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("MirrorX"), SNAME("EditorIcons")), TTR("Flip Horizontally"), FLIP_HORIZONTALLY, Key::H);
  709. button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("MirrorY"), SNAME("EditorIcons")), TTR("Flip Vertically"), FLIP_VERTICALLY, Key::V);
  710. button_advanced_menu->get_popup()->connect("id_pressed", callable_mp(this, &GenericTilePolygonEditor::_advanced_menu_item_pressed));
  711. button_advanced_menu->set_focus_mode(FOCUS_ALL);
  712. toolbar->add_child(button_advanced_menu);
  713. toolbar->add_child(memnew(VSeparator));
  714. button_pixel_snap = memnew(Button);
  715. button_pixel_snap->set_flat(true);
  716. button_pixel_snap->set_toggle_mode(true);
  717. button_pixel_snap->set_pressed(true);
  718. button_pixel_snap->set_tooltip_text(TTR("Snap to half-pixel"));
  719. toolbar->add_child(button_pixel_snap);
  720. Control *root = memnew(Control);
  721. root->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  722. root->set_custom_minimum_size(Size2(0, 200 * EDSCALE));
  723. root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  724. add_child(root);
  725. panel = memnew(Panel);
  726. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  727. panel->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  728. root->add_child(panel);
  729. base_control = memnew(Control);
  730. base_control->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  731. base_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  732. base_control->connect("draw", callable_mp(this, &GenericTilePolygonEditor::_base_control_draw));
  733. base_control->connect("gui_input", callable_mp(this, &GenericTilePolygonEditor::_base_control_gui_input));
  734. base_control->set_clip_contents(true);
  735. base_control->set_focus_mode(Control::FOCUS_CLICK);
  736. root->add_child(base_control);
  737. editor_zoom_widget = memnew(EditorZoomWidget);
  738. editor_zoom_widget->set_position(Vector2(5, 5));
  739. editor_zoom_widget->connect("zoom_changed", callable_mp(this, &GenericTilePolygonEditor::_zoom_changed).unbind(1));
  740. editor_zoom_widget->set_shortcut_context(this);
  741. root->add_child(editor_zoom_widget);
  742. button_center_view = memnew(Button);
  743. button_center_view->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("CenterView"), SNAME("EditorIcons")));
  744. button_center_view->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT, Control::PRESET_MODE_MINSIZE, 5);
  745. button_center_view->connect("pressed", callable_mp(this, &GenericTilePolygonEditor::_center_view));
  746. button_center_view->set_flat(true);
  747. button_center_view->set_disabled(true);
  748. root->add_child(button_center_view);
  749. }
  750. void TileDataDefaultEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
  751. ERR_FAIL_COND(!dummy_object);
  752. dummy_object->set(p_property, p_value);
  753. }
  754. Variant TileDataDefaultEditor::_get_painted_value() {
  755. ERR_FAIL_COND_V(!dummy_object, Variant());
  756. return dummy_object->get(property);
  757. }
  758. void TileDataDefaultEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  759. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  760. ERR_FAIL_COND(!tile_data);
  761. Variant value = tile_data->get(property);
  762. dummy_object->set(property, value);
  763. if (property_editor) {
  764. property_editor->update_property();
  765. }
  766. }
  767. void TileDataDefaultEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
  768. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  769. ERR_FAIL_COND(!tile_data);
  770. tile_data->set(property, p_value);
  771. }
  772. Variant TileDataDefaultEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  773. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  774. ERR_FAIL_COND_V(!tile_data, Variant());
  775. return tile_data->get(property);
  776. }
  777. void TileDataDefaultEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
  778. for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
  779. Vector2i coords = E.key.get_atlas_coords();
  780. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/%s", coords.x, coords.y, E.key.alternative_tile, property), E.value);
  781. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/%s", coords.x, coords.y, E.key.alternative_tile, property), p_new_value);
  782. }
  783. }
  784. void TileDataDefaultEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
  785. if (drag_type == DRAG_TYPE_PAINT_RECT) {
  786. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  787. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  788. p_canvas_item->draw_set_transform_matrix(p_transform);
  789. Rect2i rect;
  790. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  791. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position())));
  792. rect = rect.abs();
  793. RBSet<TileMapCell> edited;
  794. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  795. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  796. Vector2i coords = Vector2i(x, y);
  797. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  798. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  799. TileMapCell cell;
  800. cell.source_id = 0;
  801. cell.set_atlas_coords(coords);
  802. cell.alternative_tile = 0;
  803. edited.insert(cell);
  804. }
  805. }
  806. }
  807. for (const TileMapCell &E : edited) {
  808. Vector2i coords = E.get_atlas_coords();
  809. p_canvas_item->draw_rect(p_tile_set_atlas_source->get_tile_texture_region(coords), selection_color, false);
  810. }
  811. p_canvas_item->draw_set_transform_matrix(Transform2D());
  812. }
  813. };
  814. void TileDataDefaultEditor::forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform){
  815. };
  816. void TileDataDefaultEditor::forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
  817. Ref<InputEventMouseMotion> mm = p_event;
  818. if (mm.is_valid()) {
  819. if (drag_type == DRAG_TYPE_PAINT) {
  820. Vector<Vector2i> line = Geometry2D::bresenham_line(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_last_pos), p_tile_atlas_view->get_atlas_tile_coords_at_pos(mm->get_position()));
  821. for (int i = 0; i < line.size(); i++) {
  822. Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
  823. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  824. TileMapCell cell;
  825. cell.source_id = 0;
  826. cell.set_atlas_coords(coords);
  827. cell.alternative_tile = 0;
  828. if (!drag_modified.has(cell)) {
  829. drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
  830. }
  831. _set_value(p_tile_set_atlas_source, coords, 0, drag_painted_value);
  832. }
  833. }
  834. drag_last_pos = mm->get_position();
  835. }
  836. }
  837. Ref<InputEventMouseButton> mb = p_event;
  838. if (mb.is_valid()) {
  839. if (mb->get_button_index() == MouseButton::LEFT) {
  840. if (mb->is_pressed()) {
  841. if (picker_button->is_pressed()) {
  842. Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
  843. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  844. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  845. _set_painted_value(p_tile_set_atlas_source, coords, 0);
  846. picker_button->set_pressed(false);
  847. }
  848. } else if (mb->is_ctrl_pressed()) {
  849. drag_type = DRAG_TYPE_PAINT_RECT;
  850. drag_modified.clear();
  851. drag_painted_value = _get_painted_value();
  852. drag_start_pos = mb->get_position();
  853. } else {
  854. drag_type = DRAG_TYPE_PAINT;
  855. drag_modified.clear();
  856. drag_painted_value = _get_painted_value();
  857. Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
  858. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  859. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  860. TileMapCell cell;
  861. cell.source_id = 0;
  862. cell.set_atlas_coords(coords);
  863. cell.alternative_tile = 0;
  864. drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
  865. _set_value(p_tile_set_atlas_source, coords, 0, drag_painted_value);
  866. }
  867. drag_last_pos = mb->get_position();
  868. }
  869. } else {
  870. if (drag_type == DRAG_TYPE_PAINT_RECT) {
  871. Rect2i rect;
  872. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  873. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
  874. rect = rect.abs();
  875. drag_modified.clear();
  876. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  877. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  878. Vector2i coords = Vector2i(x, y);
  879. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  880. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  881. TileMapCell cell;
  882. cell.source_id = 0;
  883. cell.set_atlas_coords(coords);
  884. cell.alternative_tile = 0;
  885. drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
  886. }
  887. }
  888. }
  889. undo_redo->create_action(TTR("Painting Tiles Property"));
  890. _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
  891. undo_redo->commit_action(true);
  892. drag_type = DRAG_TYPE_NONE;
  893. } else if (drag_type == DRAG_TYPE_PAINT) {
  894. undo_redo->create_action(TTR("Painting Tiles Property"));
  895. _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
  896. undo_redo->commit_action(false);
  897. drag_type = DRAG_TYPE_NONE;
  898. }
  899. }
  900. }
  901. }
  902. }
  903. void TileDataDefaultEditor::forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
  904. Ref<InputEventMouseMotion> mm = p_event;
  905. if (mm.is_valid()) {
  906. if (drag_type == DRAG_TYPE_PAINT) {
  907. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
  908. Vector2i coords = Vector2i(tile.x, tile.y);
  909. int alternative_tile = tile.z;
  910. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  911. TileMapCell cell;
  912. cell.source_id = 0;
  913. cell.set_atlas_coords(coords);
  914. cell.alternative_tile = alternative_tile;
  915. if (!drag_modified.has(cell)) {
  916. drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, alternative_tile);
  917. }
  918. _set_value(p_tile_set_atlas_source, coords, alternative_tile, drag_painted_value);
  919. }
  920. drag_last_pos = mm->get_position();
  921. }
  922. }
  923. Ref<InputEventMouseButton> mb = p_event;
  924. if (mb.is_valid()) {
  925. if (mb->get_button_index() == MouseButton::LEFT) {
  926. if (mb->is_pressed()) {
  927. if (picker_button->is_pressed()) {
  928. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
  929. Vector2i coords = Vector2i(tile.x, tile.y);
  930. int alternative_tile = tile.z;
  931. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  932. _set_painted_value(p_tile_set_atlas_source, coords, alternative_tile);
  933. picker_button->set_pressed(false);
  934. }
  935. } else {
  936. drag_type = DRAG_TYPE_PAINT;
  937. drag_modified.clear();
  938. drag_painted_value = _get_painted_value();
  939. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
  940. Vector2i coords = Vector2i(tile.x, tile.y);
  941. int alternative_tile = tile.z;
  942. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  943. TileMapCell cell;
  944. cell.source_id = 0;
  945. cell.set_atlas_coords(coords);
  946. cell.alternative_tile = alternative_tile;
  947. drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, alternative_tile);
  948. _set_value(p_tile_set_atlas_source, coords, alternative_tile, drag_painted_value);
  949. }
  950. drag_last_pos = mb->get_position();
  951. }
  952. } else {
  953. undo_redo->create_action(TTR("Painting Tiles Property"));
  954. _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
  955. undo_redo->commit_action(false);
  956. drag_type = DRAG_TYPE_NONE;
  957. }
  958. }
  959. }
  960. }
  961. void TileDataDefaultEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  962. TileData *tile_data = _get_tile_data(p_cell);
  963. ERR_FAIL_COND(!tile_data);
  964. bool valid;
  965. Variant value = tile_data->get(property, &valid);
  966. if (!valid) {
  967. return;
  968. }
  969. if (value.get_type() == Variant::BOOL) {
  970. Ref<Texture2D> texture = (bool)value ? tile_bool_checked : tile_bool_unchecked;
  971. int size = MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 3;
  972. Rect2 rect = p_transform.xform(Rect2(Vector2(-size / 2, -size / 2), Vector2(size, size)));
  973. p_canvas_item->draw_texture_rect(texture, rect);
  974. } else if (value.get_type() == Variant::COLOR) {
  975. int size = MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 3;
  976. Rect2 rect = p_transform.xform(Rect2(Vector2(-size / 2, -size / 2), Vector2(size, size)));
  977. p_canvas_item->draw_rect(rect, value);
  978. } else {
  979. Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  980. int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  981. String text;
  982. switch (value.get_type()) {
  983. case Variant::INT:
  984. text = vformat("%d", value);
  985. break;
  986. case Variant::FLOAT:
  987. text = vformat("%.2f", value);
  988. break;
  989. case Variant::STRING:
  990. case Variant::STRING_NAME:
  991. text = value;
  992. break;
  993. default:
  994. return;
  995. break;
  996. }
  997. Color color = Color(1, 1, 1);
  998. if (p_selected) {
  999. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1000. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1001. selection_color.set_v(0.9);
  1002. color = selection_color;
  1003. } else if (is_visible_in_tree()) {
  1004. Variant painted_value = _get_painted_value();
  1005. bool equal = (painted_value.get_type() == Variant::FLOAT && value.get_type() == Variant::FLOAT) ? Math::is_equal_approx(float(painted_value), float(value)) : painted_value == value;
  1006. if (equal) {
  1007. color = Color(0.7, 0.7, 0.7);
  1008. }
  1009. }
  1010. Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  1011. p_canvas_item->draw_string_outline(font, p_transform.get_origin() + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, 1, Color(0, 0, 0, 1));
  1012. p_canvas_item->draw_string(font, p_transform.get_origin() + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, color);
  1013. }
  1014. }
  1015. void TileDataDefaultEditor::setup_property_editor(Variant::Type p_type, String p_property, String p_label, Variant p_default_value) {
  1016. ERR_FAIL_COND_MSG(!property.is_empty(), "Cannot setup TileDataDefaultEditor twice");
  1017. property = p_property;
  1018. // Update everything.
  1019. if (property_editor) {
  1020. property_editor->queue_delete();
  1021. }
  1022. // Update the dummy object.
  1023. dummy_object->add_dummy_property(p_property);
  1024. // Get the default value for the type.
  1025. if (p_default_value == Variant()) {
  1026. Callable::CallError error;
  1027. Variant painted_value;
  1028. Variant::construct(p_type, painted_value, nullptr, 0, error);
  1029. dummy_object->set(p_property, painted_value);
  1030. } else {
  1031. dummy_object->set(p_property, p_default_value);
  1032. }
  1033. // Create and setup the property editor.
  1034. property_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, p_type, p_property, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
  1035. property_editor->set_object_and_property(dummy_object, p_property);
  1036. if (p_label.is_empty()) {
  1037. property_editor->set_label(EditorPropertyNameProcessor::get_singleton()->process_name(p_property, EditorPropertyNameProcessor::get_default_inspector_style()));
  1038. } else {
  1039. property_editor->set_label(p_label);
  1040. }
  1041. property_editor->connect("property_changed", callable_mp(this, &TileDataDefaultEditor::_property_value_changed).unbind(1));
  1042. property_editor->set_tooltip_text(p_property);
  1043. property_editor->update_property();
  1044. add_child(property_editor);
  1045. }
  1046. void TileDataDefaultEditor::_notification(int p_what) {
  1047. switch (p_what) {
  1048. case NOTIFICATION_ENTER_TREE:
  1049. case NOTIFICATION_THEME_CHANGED: {
  1050. picker_button->set_icon(get_theme_icon(SNAME("ColorPick"), SNAME("EditorIcons")));
  1051. tile_bool_checked = get_theme_icon(SNAME("TileChecked"), SNAME("EditorIcons"));
  1052. tile_bool_unchecked = get_theme_icon(SNAME("TileUnchecked"), SNAME("EditorIcons"));
  1053. } break;
  1054. }
  1055. }
  1056. TileDataDefaultEditor::TileDataDefaultEditor() {
  1057. undo_redo = EditorNode::get_undo_redo();
  1058. label = memnew(Label);
  1059. label->set_text(TTR("Painting:"));
  1060. label->set_theme_type_variation("HeaderSmall");
  1061. add_child(label);
  1062. toolbar->add_child(memnew(VSeparator));
  1063. picker_button = memnew(Button);
  1064. picker_button->set_flat(true);
  1065. picker_button->set_toggle_mode(true);
  1066. picker_button->set_shortcut(ED_SHORTCUT("tiles_editor/picker", "Picker", Key::P));
  1067. toolbar->add_child(picker_button);
  1068. }
  1069. TileDataDefaultEditor::~TileDataDefaultEditor() {
  1070. toolbar->queue_delete();
  1071. memdelete(dummy_object);
  1072. }
  1073. void TileDataTextureOffsetEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  1074. TileData *tile_data = _get_tile_data(p_cell);
  1075. ERR_FAIL_COND(!tile_data);
  1076. Vector2i tile_set_tile_size = tile_set->get_tile_size();
  1077. Color color = Color(1.0, 0.0, 0.0);
  1078. if (p_selected) {
  1079. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1080. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1081. color = selection_color;
  1082. }
  1083. Transform2D tile_xform;
  1084. tile_xform.set_scale(tile_set_tile_size);
  1085. tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, color);
  1086. }
  1087. void TileDataPositionEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  1088. TileData *tile_data = _get_tile_data(p_cell);
  1089. ERR_FAIL_COND(!tile_data);
  1090. bool valid;
  1091. Variant value = tile_data->get(property, &valid);
  1092. if (!valid) {
  1093. return;
  1094. }
  1095. ERR_FAIL_COND(value.get_type() != Variant::VECTOR2I && value.get_type() != Variant::VECTOR2);
  1096. Color color = Color(1.0, 1.0, 1.0);
  1097. if (p_selected) {
  1098. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1099. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1100. color = selection_color;
  1101. }
  1102. Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
  1103. p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(value)) - position_icon->get_size() / 2, color);
  1104. }
  1105. void TileDataYSortEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  1106. TileData *tile_data = _get_tile_data(p_cell);
  1107. ERR_FAIL_COND(!tile_data);
  1108. Color color = Color(1.0, 1.0, 1.0);
  1109. if (p_selected) {
  1110. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1111. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1112. color = selection_color;
  1113. }
  1114. Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
  1115. p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(0, tile_data->get_y_sort_origin())) - position_icon->get_size() / 2, color);
  1116. }
  1117. void TileDataOcclusionShapeEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  1118. TileData *tile_data = _get_tile_data(p_cell);
  1119. ERR_FAIL_COND(!tile_data);
  1120. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1121. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1122. Color color = grid_color.darkened(0.2);
  1123. if (p_selected) {
  1124. color = selection_color.darkened(0.2);
  1125. }
  1126. color.a *= 0.5;
  1127. Vector<Color> debug_occlusion_color;
  1128. debug_occlusion_color.push_back(color);
  1129. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  1130. Ref<OccluderPolygon2D> occluder = tile_data->get_occluder(occlusion_layer);
  1131. if (occluder.is_valid() && occluder->get_polygon().size() >= 3) {
  1132. p_canvas_item->draw_polygon(Variant(occluder->get_polygon()), debug_occlusion_color);
  1133. }
  1134. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  1135. }
  1136. Variant TileDataOcclusionShapeEditor::_get_painted_value() {
  1137. Ref<OccluderPolygon2D> occluder_polygon;
  1138. occluder_polygon.instantiate();
  1139. if (polygon_editor->get_polygon_count() >= 1) {
  1140. occluder_polygon->set_polygon(polygon_editor->get_polygon(0));
  1141. }
  1142. return occluder_polygon;
  1143. }
  1144. void TileDataOcclusionShapeEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  1145. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1146. ERR_FAIL_COND(!tile_data);
  1147. Ref<OccluderPolygon2D> occluder_polygon = tile_data->get_occluder(occlusion_layer);
  1148. polygon_editor->clear_polygons();
  1149. if (occluder_polygon.is_valid()) {
  1150. polygon_editor->add_polygon(occluder_polygon->get_polygon());
  1151. }
  1152. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  1153. }
  1154. void TileDataOcclusionShapeEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
  1155. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1156. ERR_FAIL_COND(!tile_data);
  1157. Ref<OccluderPolygon2D> occluder_polygon = p_value;
  1158. tile_data->set_occluder(occlusion_layer, occluder_polygon);
  1159. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  1160. }
  1161. Variant TileDataOcclusionShapeEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  1162. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1163. ERR_FAIL_COND_V(!tile_data, Variant());
  1164. return tile_data->get_occluder(occlusion_layer);
  1165. }
  1166. void TileDataOcclusionShapeEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
  1167. for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
  1168. Vector2i coords = E.key.get_atlas_coords();
  1169. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/occlusion_layer_%d/polygon", coords.x, coords.y, E.key.alternative_tile, occlusion_layer), E.value);
  1170. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/occlusion_layer_%d/polygon", coords.x, coords.y, E.key.alternative_tile, occlusion_layer), p_new_value);
  1171. }
  1172. }
  1173. void TileDataOcclusionShapeEditor::_tile_set_changed() {
  1174. polygon_editor->set_tile_set(tile_set);
  1175. }
  1176. void TileDataOcclusionShapeEditor::_notification(int p_what) {
  1177. switch (p_what) {
  1178. case NOTIFICATION_ENTER_TREE: {
  1179. polygon_editor->set_polygons_color(get_tree()->get_debug_collisions_color());
  1180. } break;
  1181. }
  1182. }
  1183. TileDataOcclusionShapeEditor::TileDataOcclusionShapeEditor() {
  1184. undo_redo = EditorNode::get_undo_redo();
  1185. polygon_editor = memnew(GenericTilePolygonEditor);
  1186. add_child(polygon_editor);
  1187. }
  1188. void TileDataCollisionEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
  1189. dummy_object->set(p_property, p_value);
  1190. }
  1191. void TileDataCollisionEditor::_property_selected(StringName p_path, int p_focusable) {
  1192. // Deselect all other properties
  1193. for (KeyValue<StringName, EditorProperty *> &editor : property_editors) {
  1194. if (editor.key != p_path) {
  1195. editor.value->deselect();
  1196. }
  1197. }
  1198. }
  1199. void TileDataCollisionEditor::_polygons_changed() {
  1200. // Update the dummy object properties and their editors.
  1201. for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
  1202. StringName one_way_property = vformat("polygon_%d_one_way", i);
  1203. StringName one_way_margin_property = vformat("polygon_%d_one_way_margin", i);
  1204. if (!dummy_object->has_dummy_property(one_way_property)) {
  1205. dummy_object->add_dummy_property(one_way_property);
  1206. dummy_object->set(one_way_property, false);
  1207. }
  1208. if (!dummy_object->has_dummy_property(one_way_margin_property)) {
  1209. dummy_object->add_dummy_property(one_way_margin_property);
  1210. dummy_object->set(one_way_margin_property, 1.0);
  1211. }
  1212. if (!property_editors.has(one_way_property)) {
  1213. EditorProperty *one_way_property_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::BOOL, one_way_property, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
  1214. one_way_property_editor->set_object_and_property(dummy_object, one_way_property);
  1215. one_way_property_editor->set_label(one_way_property);
  1216. one_way_property_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
  1217. one_way_property_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
  1218. one_way_property_editor->set_tooltip_text(one_way_property_editor->get_edited_property());
  1219. one_way_property_editor->update_property();
  1220. add_child(one_way_property_editor);
  1221. property_editors[one_way_property] = one_way_property_editor;
  1222. }
  1223. if (!property_editors.has(one_way_margin_property)) {
  1224. EditorProperty *one_way_margin_property_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::FLOAT, one_way_margin_property, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
  1225. one_way_margin_property_editor->set_object_and_property(dummy_object, one_way_margin_property);
  1226. one_way_margin_property_editor->set_label(one_way_margin_property);
  1227. one_way_margin_property_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
  1228. one_way_margin_property_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
  1229. one_way_margin_property_editor->set_tooltip_text(one_way_margin_property_editor->get_edited_property());
  1230. one_way_margin_property_editor->update_property();
  1231. add_child(one_way_margin_property_editor);
  1232. property_editors[one_way_margin_property] = one_way_margin_property_editor;
  1233. }
  1234. }
  1235. // Remove unneeded properties and their editors.
  1236. for (int i = polygon_editor->get_polygon_count(); dummy_object->has_dummy_property(vformat("polygon_%d_one_way", i)); i++) {
  1237. dummy_object->remove_dummy_property(vformat("polygon_%d_one_way", i));
  1238. }
  1239. for (int i = polygon_editor->get_polygon_count(); dummy_object->has_dummy_property(vformat("polygon_%d_one_way_margin", i)); i++) {
  1240. dummy_object->remove_dummy_property(vformat("polygon_%d_one_way_margin", i));
  1241. }
  1242. for (int i = polygon_editor->get_polygon_count(); property_editors.has(vformat("polygon_%d_one_way", i)); i++) {
  1243. property_editors[vformat("polygon_%d_one_way", i)]->queue_delete();
  1244. property_editors.erase(vformat("polygon_%d_one_way", i));
  1245. }
  1246. for (int i = polygon_editor->get_polygon_count(); property_editors.has(vformat("polygon_%d_one_way_margin", i)); i++) {
  1247. property_editors[vformat("polygon_%d_one_way_margin", i)]->queue_delete();
  1248. property_editors.erase(vformat("polygon_%d_one_way_margin", i));
  1249. }
  1250. }
  1251. Variant TileDataCollisionEditor::_get_painted_value() {
  1252. Dictionary dict;
  1253. dict["linear_velocity"] = dummy_object->get("linear_velocity");
  1254. dict["angular_velocity"] = dummy_object->get("angular_velocity");
  1255. Array array;
  1256. for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
  1257. ERR_FAIL_COND_V(polygon_editor->get_polygon(i).size() < 3, Variant());
  1258. Dictionary polygon_dict;
  1259. polygon_dict["points"] = polygon_editor->get_polygon(i);
  1260. polygon_dict["one_way"] = dummy_object->get(vformat("polygon_%d_one_way", i));
  1261. polygon_dict["one_way_margin"] = dummy_object->get(vformat("polygon_%d_one_way_margin", i));
  1262. array.push_back(polygon_dict);
  1263. }
  1264. dict["polygons"] = array;
  1265. return dict;
  1266. }
  1267. void TileDataCollisionEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  1268. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1269. ERR_FAIL_COND(!tile_data);
  1270. polygon_editor->clear_polygons();
  1271. for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
  1272. Vector<Vector2> polygon = tile_data->get_collision_polygon_points(physics_layer, i);
  1273. if (polygon.size() >= 3) {
  1274. polygon_editor->add_polygon(polygon);
  1275. }
  1276. }
  1277. _polygons_changed();
  1278. dummy_object->set("linear_velocity", tile_data->get_constant_linear_velocity(physics_layer));
  1279. dummy_object->set("angular_velocity", tile_data->get_constant_angular_velocity(physics_layer));
  1280. for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
  1281. dummy_object->set(vformat("polygon_%d_one_way", i), tile_data->is_collision_polygon_one_way(physics_layer, i));
  1282. dummy_object->set(vformat("polygon_%d_one_way_margin", i), tile_data->get_collision_polygon_one_way_margin(physics_layer, i));
  1283. }
  1284. for (const KeyValue<StringName, EditorProperty *> &E : property_editors) {
  1285. E.value->update_property();
  1286. }
  1287. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  1288. }
  1289. void TileDataCollisionEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
  1290. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1291. ERR_FAIL_COND(!tile_data);
  1292. Dictionary dict = p_value;
  1293. tile_data->set_constant_linear_velocity(physics_layer, dict["linear_velocity"]);
  1294. tile_data->set_constant_angular_velocity(physics_layer, dict["angular_velocity"]);
  1295. Array array = dict["polygons"];
  1296. tile_data->set_collision_polygons_count(physics_layer, array.size());
  1297. for (int i = 0; i < array.size(); i++) {
  1298. Dictionary polygon_dict = array[i];
  1299. tile_data->set_collision_polygon_points(physics_layer, i, polygon_dict["points"]);
  1300. tile_data->set_collision_polygon_one_way(physics_layer, i, polygon_dict["one_way"]);
  1301. tile_data->set_collision_polygon_one_way_margin(physics_layer, i, polygon_dict["one_way_margin"]);
  1302. }
  1303. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  1304. }
  1305. Variant TileDataCollisionEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  1306. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  1307. ERR_FAIL_COND_V(!tile_data, Variant());
  1308. Dictionary dict;
  1309. dict["linear_velocity"] = tile_data->get_constant_linear_velocity(physics_layer);
  1310. dict["angular_velocity"] = tile_data->get_constant_angular_velocity(physics_layer);
  1311. Array array;
  1312. for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
  1313. Dictionary polygon_dict;
  1314. polygon_dict["points"] = tile_data->get_collision_polygon_points(physics_layer, i);
  1315. polygon_dict["one_way"] = tile_data->is_collision_polygon_one_way(physics_layer, i);
  1316. polygon_dict["one_way_margin"] = tile_data->get_collision_polygon_one_way_margin(physics_layer, i);
  1317. array.push_back(polygon_dict);
  1318. }
  1319. dict["polygons"] = array;
  1320. return dict;
  1321. }
  1322. void TileDataCollisionEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
  1323. Array new_array = p_new_value;
  1324. for (KeyValue<TileMapCell, Variant> &E : p_previous_values) {
  1325. Array old_array = E.value;
  1326. Vector2i coords = E.key.get_atlas_coords();
  1327. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygons_count", coords.x, coords.y, E.key.alternative_tile, physics_layer), old_array.size());
  1328. for (int i = 0; i < old_array.size(); i++) {
  1329. Dictionary dict = old_array[i];
  1330. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/points", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["points"]);
  1331. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/one_way", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["one_way"]);
  1332. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/one_way_margin", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["one_way_margin"]);
  1333. }
  1334. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygons_count", coords.x, coords.y, E.key.alternative_tile, physics_layer), new_array.size());
  1335. for (int i = 0; i < new_array.size(); i++) {
  1336. Dictionary dict = new_array[i];
  1337. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/points", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["points"]);
  1338. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/one_way", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["one_way"]);
  1339. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/physics_layer_%d/polygon_%d/one_way_margin", coords.x, coords.y, E.key.alternative_tile, physics_layer, i), dict["one_way_margin"]);
  1340. }
  1341. }
  1342. }
  1343. void TileDataCollisionEditor::_tile_set_changed() {
  1344. polygon_editor->set_tile_set(tile_set);
  1345. _polygons_changed();
  1346. }
  1347. void TileDataCollisionEditor::_notification(int p_what) {
  1348. switch (p_what) {
  1349. case NOTIFICATION_ENTER_TREE: {
  1350. polygon_editor->set_polygons_color(get_tree()->get_debug_collisions_color());
  1351. } break;
  1352. }
  1353. }
  1354. TileDataCollisionEditor::TileDataCollisionEditor() {
  1355. undo_redo = EditorNode::get_undo_redo();
  1356. polygon_editor = memnew(GenericTilePolygonEditor);
  1357. polygon_editor->set_multiple_polygon_mode(true);
  1358. polygon_editor->connect("polygons_changed", callable_mp(this, &TileDataCollisionEditor::_polygons_changed));
  1359. add_child(polygon_editor);
  1360. dummy_object->add_dummy_property("linear_velocity");
  1361. dummy_object->set("linear_velocity", Vector2());
  1362. dummy_object->add_dummy_property("angular_velocity");
  1363. dummy_object->set("angular_velocity", 0.0);
  1364. EditorProperty *linear_velocity_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::VECTOR2, "linear_velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
  1365. linear_velocity_editor->set_object_and_property(dummy_object, "linear_velocity");
  1366. linear_velocity_editor->set_label("linear_velocity");
  1367. linear_velocity_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
  1368. linear_velocity_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
  1369. linear_velocity_editor->set_tooltip_text(linear_velocity_editor->get_edited_property());
  1370. linear_velocity_editor->update_property();
  1371. add_child(linear_velocity_editor);
  1372. property_editors["linear_velocity"] = linear_velocity_editor;
  1373. EditorProperty *angular_velocity_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::FLOAT, "angular_velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
  1374. angular_velocity_editor->set_object_and_property(dummy_object, "angular_velocity");
  1375. angular_velocity_editor->set_label("angular_velocity");
  1376. angular_velocity_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
  1377. angular_velocity_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
  1378. angular_velocity_editor->set_tooltip_text(angular_velocity_editor->get_edited_property());
  1379. angular_velocity_editor->update_property();
  1380. add_child(angular_velocity_editor);
  1381. property_editors["angular_velocity"] = angular_velocity_editor;
  1382. _polygons_changed();
  1383. }
  1384. TileDataCollisionEditor::~TileDataCollisionEditor() {
  1385. memdelete(dummy_object);
  1386. }
  1387. void TileDataCollisionEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  1388. TileData *tile_data = _get_tile_data(p_cell);
  1389. ERR_FAIL_COND(!tile_data);
  1390. // Draw all shapes.
  1391. Vector<Color> color;
  1392. if (p_selected) {
  1393. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1394. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1395. selection_color.a = 0.7;
  1396. color.push_back(selection_color);
  1397. } else {
  1398. Color debug_collision_color = p_canvas_item->get_tree()->get_debug_collisions_color();
  1399. color.push_back(debug_collision_color);
  1400. }
  1401. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  1402. for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
  1403. Vector<Vector2> polygon = tile_data->get_collision_polygon_points(physics_layer, i);
  1404. if (polygon.size() >= 3) {
  1405. p_canvas_item->draw_polygon(polygon, color);
  1406. }
  1407. }
  1408. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  1409. }
  1410. void TileDataTerrainsEditor::_update_terrain_selector() {
  1411. ERR_FAIL_COND(!tile_set.is_valid());
  1412. // Update the terrain set selector.
  1413. Vector<String> options;
  1414. options.push_back(String(TTR("No terrains")) + String(":-1"));
  1415. for (int i = 0; i < tile_set->get_terrain_sets_count(); i++) {
  1416. options.push_back(vformat("Terrain Set %d", i));
  1417. }
  1418. terrain_set_property_editor->setup(options);
  1419. terrain_set_property_editor->update_property();
  1420. // Update the terrain selector.
  1421. int terrain_set = int(dummy_object->get("terrain_set"));
  1422. if (terrain_set == -1) {
  1423. terrain_property_editor->hide();
  1424. } else {
  1425. options.clear();
  1426. Vector<Vector<Ref<Texture2D>>> icons = tile_set->generate_terrains_icons(Size2(16, 16) * EDSCALE);
  1427. options.push_back(String(TTR("No terrain")) + String(":-1"));
  1428. for (int i = 0; i < tile_set->get_terrains_count(terrain_set); i++) {
  1429. String name = tile_set->get_terrain_name(terrain_set, i);
  1430. if (name.is_empty()) {
  1431. options.push_back(vformat("Terrain %d", i));
  1432. } else {
  1433. options.push_back(name);
  1434. }
  1435. }
  1436. terrain_property_editor->setup(options);
  1437. terrain_property_editor->update_property();
  1438. // Kind of a hack to set icons.
  1439. // We could provide a way to modify that in the EditorProperty.
  1440. OptionButton *option_button = Object::cast_to<OptionButton>(terrain_property_editor->get_child(0));
  1441. for (int terrain = 0; terrain < tile_set->get_terrains_count(terrain_set); terrain++) {
  1442. option_button->set_item_icon(terrain + 1, icons[terrain_set][terrain]);
  1443. }
  1444. terrain_property_editor->show();
  1445. }
  1446. }
  1447. void TileDataTerrainsEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
  1448. Variant old_value = dummy_object->get(p_property);
  1449. dummy_object->set(p_property, p_value);
  1450. if (p_property == "terrain_set") {
  1451. if (p_value != old_value) {
  1452. dummy_object->set("terrain", -1);
  1453. }
  1454. _update_terrain_selector();
  1455. }
  1456. emit_signal(SNAME("needs_redraw"));
  1457. }
  1458. void TileDataTerrainsEditor::_tile_set_changed() {
  1459. ERR_FAIL_COND(!tile_set.is_valid());
  1460. // Fix if wrong values are selected.
  1461. int terrain_set = int(dummy_object->get("terrain_set"));
  1462. if (terrain_set >= tile_set->get_terrain_sets_count()) {
  1463. terrain_set = -1;
  1464. dummy_object->set("terrain_set", -1);
  1465. }
  1466. if (terrain_set >= 0) {
  1467. if (int(dummy_object->get("terrain")) >= tile_set->get_terrains_count(terrain_set)) {
  1468. dummy_object->set("terrain", -1);
  1469. }
  1470. }
  1471. _update_terrain_selector();
  1472. }
  1473. void TileDataTerrainsEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
  1474. ERR_FAIL_COND(!tile_set.is_valid());
  1475. // Draw the hovered terrain bit, or the whole tile if it has the wrong terrain set.
  1476. Vector2i hovered_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1477. if (drag_type == DRAG_TYPE_NONE) {
  1478. Vector2i mouse_pos = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
  1479. hovered_coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_pos);
  1480. hovered_coords = p_tile_set_atlas_source->get_tile_at_coords(hovered_coords);
  1481. if (hovered_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1482. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(hovered_coords, 0);
  1483. int terrain_set = tile_data->get_terrain_set();
  1484. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(hovered_coords);
  1485. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(hovered_coords, 0);
  1486. if (terrain_set >= 0 && terrain_set == int(dummy_object->get("terrain_set"))) {
  1487. // Draw hovered bit.
  1488. Transform2D xform;
  1489. xform.set_origin(position);
  1490. Vector<Color> color;
  1491. color.push_back(Color(1.0, 1.0, 1.0, 0.5));
  1492. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  1493. if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
  1494. p_canvas_item->draw_set_transform_matrix(p_transform * xform);
  1495. p_canvas_item->draw_polygon(polygon, color);
  1496. }
  1497. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1498. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1499. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  1500. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  1501. if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
  1502. p_canvas_item->draw_set_transform_matrix(p_transform * xform);
  1503. p_canvas_item->draw_polygon(polygon, color);
  1504. }
  1505. }
  1506. }
  1507. } else {
  1508. // Draw hovered tile.
  1509. Transform2D tile_xform;
  1510. tile_xform.set_origin(position);
  1511. tile_xform.set_scale(tile_set->get_tile_size());
  1512. tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  1513. }
  1514. }
  1515. }
  1516. // Dim terrains with wrong terrain set.
  1517. Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  1518. int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  1519. for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) {
  1520. Vector2i coords = p_tile_set_atlas_source->get_tile_id(i);
  1521. if (coords != hovered_coords) {
  1522. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1523. if (tile_data->get_terrain_set() != int(dummy_object->get("terrain_set"))) {
  1524. // Dimming
  1525. p_canvas_item->draw_set_transform_matrix(p_transform);
  1526. Rect2i rect = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1527. p_canvas_item->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.3));
  1528. // Text
  1529. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1530. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1531. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1532. Color color = Color(1, 1, 1);
  1533. String text;
  1534. if (tile_data->get_terrain_set() >= 0) {
  1535. text = vformat("%d", tile_data->get_terrain_set());
  1536. } else {
  1537. text = "-";
  1538. }
  1539. Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  1540. p_canvas_item->draw_string_outline(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, 1, Color(0, 0, 0, 1));
  1541. p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, color);
  1542. }
  1543. }
  1544. }
  1545. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1546. if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET_RECT) {
  1547. // Draw selection rectangle.
  1548. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1549. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1550. p_canvas_item->draw_set_transform_matrix(p_transform);
  1551. Rect2i rect;
  1552. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  1553. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position())));
  1554. rect = rect.abs();
  1555. RBSet<TileMapCell> edited;
  1556. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  1557. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  1558. Vector2i coords = Vector2i(x, y);
  1559. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  1560. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1561. TileMapCell cell;
  1562. cell.source_id = 0;
  1563. cell.set_atlas_coords(coords);
  1564. cell.alternative_tile = 0;
  1565. edited.insert(cell);
  1566. }
  1567. }
  1568. }
  1569. for (const TileMapCell &E : edited) {
  1570. Vector2i coords = E.get_atlas_coords();
  1571. p_canvas_item->draw_rect(p_tile_set_atlas_source->get_tile_texture_region(coords), selection_color, false);
  1572. }
  1573. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1574. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS_RECT) {
  1575. // Highlight selected peering bits.
  1576. Dictionary painted = Dictionary(drag_painted_value);
  1577. int terrain_set = int(painted["terrain_set"]);
  1578. Rect2i rect;
  1579. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  1580. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position())));
  1581. rect = rect.abs();
  1582. RBSet<TileMapCell> edited;
  1583. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  1584. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  1585. Vector2i coords = Vector2i(x, y);
  1586. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  1587. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1588. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1589. if (tile_data->get_terrain_set() == terrain_set) {
  1590. TileMapCell cell;
  1591. cell.source_id = 0;
  1592. cell.set_atlas_coords(coords);
  1593. cell.alternative_tile = 0;
  1594. edited.insert(cell);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. Vector2 end = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
  1600. Vector<Point2> mouse_pos_rect_polygon;
  1601. mouse_pos_rect_polygon.push_back(drag_start_pos);
  1602. mouse_pos_rect_polygon.push_back(Vector2(end.x, drag_start_pos.y));
  1603. mouse_pos_rect_polygon.push_back(end);
  1604. mouse_pos_rect_polygon.push_back(Vector2(drag_start_pos.x, end.y));
  1605. Vector<Color> color;
  1606. color.push_back(Color(1.0, 1.0, 1.0, 0.5));
  1607. p_canvas_item->draw_set_transform_matrix(p_transform);
  1608. for (const TileMapCell &E : edited) {
  1609. Vector2i coords = E.get_atlas_coords();
  1610. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1611. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1612. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  1613. for (int j = 0; j < polygon.size(); j++) {
  1614. polygon.write[j] += position;
  1615. }
  1616. if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
  1617. // Draw terrain.
  1618. p_canvas_item->draw_polygon(polygon, color);
  1619. }
  1620. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1621. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1622. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  1623. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  1624. for (int j = 0; j < polygon.size(); j++) {
  1625. polygon.write[j] += position;
  1626. }
  1627. if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
  1628. // Draw bit.
  1629. p_canvas_item->draw_polygon(polygon, color);
  1630. }
  1631. }
  1632. }
  1633. }
  1634. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1635. }
  1636. }
  1637. void TileDataTerrainsEditor::forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
  1638. ERR_FAIL_COND(!tile_set.is_valid());
  1639. // Draw the hovered terrain bit, or the whole tile if it has the wrong terrain set.
  1640. Vector2i hovered_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1641. int hovered_alternative = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1642. if (drag_type == DRAG_TYPE_NONE) {
  1643. Vector2i mouse_pos = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
  1644. Vector3i hovered = p_tile_atlas_view->get_alternative_tile_at_pos(mouse_pos);
  1645. hovered_coords = Vector2i(hovered.x, hovered.y);
  1646. hovered_alternative = hovered.z;
  1647. if (hovered_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1648. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(hovered_coords, hovered_alternative);
  1649. int terrain_set = tile_data->get_terrain_set();
  1650. Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(hovered_coords, hovered_alternative);
  1651. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(hovered_coords, hovered_alternative);
  1652. if (terrain_set == int(dummy_object->get("terrain_set"))) {
  1653. // Draw hovered bit.
  1654. Transform2D xform;
  1655. xform.set_origin(position);
  1656. Vector<Color> color;
  1657. color.push_back(Color(1.0, 1.0, 1.0, 0.5));
  1658. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  1659. if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
  1660. p_canvas_item->draw_set_transform_matrix(p_transform * xform);
  1661. p_canvas_item->draw_polygon(polygon, color);
  1662. }
  1663. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1664. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1665. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  1666. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  1667. if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
  1668. p_canvas_item->draw_set_transform_matrix(p_transform * xform);
  1669. p_canvas_item->draw_polygon(polygon, color);
  1670. }
  1671. }
  1672. }
  1673. } else {
  1674. // Draw hovered tile.
  1675. Transform2D tile_xform;
  1676. tile_xform.set_origin(position);
  1677. tile_xform.set_scale(tile_set->get_tile_size());
  1678. tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  1679. }
  1680. }
  1681. }
  1682. // Dim terrains with wrong terrain set.
  1683. Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  1684. int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  1685. for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) {
  1686. Vector2i coords = p_tile_set_atlas_source->get_tile_id(i);
  1687. for (int j = 1; j < p_tile_set_atlas_source->get_alternative_tiles_count(coords); j++) {
  1688. int alternative_tile = p_tile_set_atlas_source->get_alternative_tile_id(coords, j);
  1689. if (coords != hovered_coords || alternative_tile != hovered_alternative) {
  1690. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
  1691. if (tile_data->get_terrain_set() != int(dummy_object->get("terrain_set"))) {
  1692. // Dimming
  1693. p_canvas_item->draw_set_transform_matrix(p_transform);
  1694. Rect2i rect = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  1695. p_canvas_item->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.3));
  1696. // Text
  1697. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1698. Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  1699. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1700. Color color = Color(1, 1, 1);
  1701. String text;
  1702. if (tile_data->get_terrain_set() >= 0) {
  1703. text = vformat("%d", tile_data->get_terrain_set());
  1704. } else {
  1705. text = "-";
  1706. }
  1707. Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  1708. p_canvas_item->draw_string_outline(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, 1, Color(0, 0, 0, 1));
  1709. p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_CENTER, string_size.x, font_size, color);
  1710. }
  1711. }
  1712. }
  1713. }
  1714. p_canvas_item->draw_set_transform_matrix(Transform2D());
  1715. }
  1716. void TileDataTerrainsEditor::forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
  1717. Ref<InputEventMouseMotion> mm = p_event;
  1718. if (mm.is_valid()) {
  1719. if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
  1720. Vector<Vector2i> line = Geometry2D::bresenham_line(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_last_pos), p_tile_atlas_view->get_atlas_tile_coords_at_pos(mm->get_position()));
  1721. for (int i = 0; i < line.size(); i++) {
  1722. Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
  1723. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1724. int terrain_set = drag_painted_value;
  1725. TileMapCell cell;
  1726. cell.source_id = 0;
  1727. cell.set_atlas_coords(coords);
  1728. cell.alternative_tile = 0;
  1729. // Save the old terrain_set and terrains bits.
  1730. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1731. if (!drag_modified.has(cell)) {
  1732. Dictionary dict;
  1733. dict["terrain_set"] = tile_data->get_terrain_set();
  1734. dict["terrain"] = tile_data->get_terrain();
  1735. Array array;
  1736. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1737. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1738. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  1739. }
  1740. dict["terrain_peering_bits"] = array;
  1741. drag_modified[cell] = dict;
  1742. }
  1743. // Set the terrain_set.
  1744. tile_data->set_terrain_set(terrain_set);
  1745. }
  1746. }
  1747. drag_last_pos = mm->get_position();
  1748. accept_event();
  1749. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
  1750. int terrain_set = Dictionary(drag_painted_value)["terrain_set"];
  1751. int terrain = Dictionary(drag_painted_value)["terrain"];
  1752. Vector<Vector2i> line = Geometry2D::bresenham_line(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_last_pos), p_tile_atlas_view->get_atlas_tile_coords_at_pos(mm->get_position()));
  1753. for (int i = 0; i < line.size(); i++) {
  1754. Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
  1755. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1756. TileMapCell cell;
  1757. cell.source_id = 0;
  1758. cell.set_atlas_coords(coords);
  1759. cell.alternative_tile = 0;
  1760. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1761. if (tile_data->get_terrain_set() == terrain_set) {
  1762. // Save the old terrain_set and terrains bits.
  1763. if (!drag_modified.has(cell)) {
  1764. Dictionary dict;
  1765. dict["terrain_set"] = tile_data->get_terrain_set();
  1766. dict["terrain"] = tile_data->get_terrain();
  1767. Array array;
  1768. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1769. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1770. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  1771. }
  1772. dict["terrain_peering_bits"] = array;
  1773. drag_modified[cell] = dict;
  1774. }
  1775. // Set the terrains bits.
  1776. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1777. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1778. Vector<Vector2> polygon = tile_set->get_terrain_polygon(tile_data->get_terrain_set());
  1779. if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
  1780. tile_data->set_terrain(terrain);
  1781. }
  1782. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1783. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1784. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  1785. polygon = tile_set->get_terrain_peering_bit_polygon(tile_data->get_terrain_set(), bit);
  1786. if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
  1787. tile_data->set_terrain_peering_bit(bit, terrain);
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. }
  1794. drag_last_pos = mm->get_position();
  1795. accept_event();
  1796. }
  1797. }
  1798. Ref<InputEventMouseButton> mb = p_event;
  1799. if (mb.is_valid()) {
  1800. if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
  1801. if (mb->is_pressed()) {
  1802. if (mb->get_button_index() == MouseButton::LEFT && picker_button->is_pressed()) {
  1803. Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
  1804. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  1805. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1806. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1807. int terrain_set = tile_data->get_terrain_set();
  1808. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1809. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1810. dummy_object->set("terrain_set", terrain_set);
  1811. dummy_object->set("terrain", -1);
  1812. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  1813. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  1814. dummy_object->set("terrain", tile_data->get_terrain());
  1815. }
  1816. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1817. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1818. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  1819. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  1820. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  1821. dummy_object->set("terrain", tile_data->get_terrain_peering_bit(bit));
  1822. }
  1823. }
  1824. }
  1825. terrain_set_property_editor->update_property();
  1826. _update_terrain_selector();
  1827. picker_button->set_pressed(false);
  1828. accept_event();
  1829. }
  1830. } else {
  1831. Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
  1832. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  1833. TileData *tile_data = nullptr;
  1834. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1835. tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1836. }
  1837. int terrain_set = int(dummy_object->get("terrain_set"));
  1838. int terrain = int(dummy_object->get("terrain"));
  1839. if (terrain_set == -1 || !tile_data || tile_data->get_terrain_set() != terrain_set) {
  1840. // Paint terrain sets.
  1841. if (mb->get_button_index() == MouseButton::RIGHT) {
  1842. terrain_set = -1;
  1843. }
  1844. if (mb->is_ctrl_pressed()) {
  1845. // Paint terrain set with rect.
  1846. drag_type = DRAG_TYPE_PAINT_TERRAIN_SET_RECT;
  1847. drag_modified.clear();
  1848. drag_painted_value = terrain_set;
  1849. drag_start_pos = mb->get_position();
  1850. } else {
  1851. // Paint terrain set.
  1852. drag_type = DRAG_TYPE_PAINT_TERRAIN_SET;
  1853. drag_modified.clear();
  1854. drag_painted_value = terrain_set;
  1855. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1856. TileMapCell cell;
  1857. cell.source_id = 0;
  1858. cell.set_atlas_coords(coords);
  1859. cell.alternative_tile = 0;
  1860. // Save the old terrain_set and terrains bits.
  1861. Dictionary dict;
  1862. dict["terrain_set"] = tile_data->get_terrain_set();
  1863. dict["terrain"] = tile_data->get_terrain();
  1864. Array array;
  1865. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1866. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1867. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  1868. }
  1869. dict["terrain_peering_bits"] = array;
  1870. drag_modified[cell] = dict;
  1871. // Set the terrain_set.
  1872. tile_data->set_terrain_set(terrain_set);
  1873. }
  1874. drag_last_pos = mb->get_position();
  1875. }
  1876. accept_event();
  1877. } else if (tile_data->get_terrain_set() == terrain_set) {
  1878. // Paint terrain bits.
  1879. if (mb->get_button_index() == MouseButton::RIGHT) {
  1880. terrain = -1;
  1881. }
  1882. if (mb->is_ctrl_pressed()) {
  1883. // Paint terrain bits with rect.
  1884. drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS_RECT;
  1885. drag_modified.clear();
  1886. Dictionary painted_dict;
  1887. painted_dict["terrain_set"] = terrain_set;
  1888. painted_dict["terrain"] = terrain;
  1889. drag_painted_value = painted_dict;
  1890. drag_start_pos = mb->get_position();
  1891. } else {
  1892. // Paint terrain bits.
  1893. drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS;
  1894. drag_modified.clear();
  1895. Dictionary painted_dict;
  1896. painted_dict["terrain_set"] = terrain_set;
  1897. painted_dict["terrain"] = terrain;
  1898. drag_painted_value = painted_dict;
  1899. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1900. TileMapCell cell;
  1901. cell.source_id = 0;
  1902. cell.set_atlas_coords(coords);
  1903. cell.alternative_tile = 0;
  1904. // Save the old terrain_set and terrains bits.
  1905. Dictionary dict;
  1906. dict["terrain_set"] = tile_data->get_terrain_set();
  1907. dict["terrain"] = tile_data->get_terrain();
  1908. Array array;
  1909. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1910. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1911. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  1912. }
  1913. dict["terrain_peering_bits"] = array;
  1914. drag_modified[cell] = dict;
  1915. // Set the terrain bit.
  1916. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  1917. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1918. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  1919. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  1920. tile_data->set_terrain(terrain);
  1921. }
  1922. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1923. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1924. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  1925. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  1926. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  1927. tile_data->set_terrain_peering_bit(bit, terrain);
  1928. }
  1929. }
  1930. }
  1931. }
  1932. drag_last_pos = mb->get_position();
  1933. }
  1934. accept_event();
  1935. }
  1936. }
  1937. } else {
  1938. if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET_RECT) {
  1939. Rect2i rect;
  1940. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  1941. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
  1942. rect = rect.abs();
  1943. RBSet<TileMapCell> edited;
  1944. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  1945. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  1946. Vector2i coords = Vector2i(x, y);
  1947. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  1948. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1949. TileMapCell cell;
  1950. cell.source_id = 0;
  1951. cell.set_atlas_coords(coords);
  1952. cell.alternative_tile = 0;
  1953. edited.insert(cell);
  1954. }
  1955. }
  1956. }
  1957. undo_redo->create_action(TTR("Painting Terrain Set"));
  1958. for (const TileMapCell &E : edited) {
  1959. Vector2i coords = E.get_atlas_coords();
  1960. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  1961. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.alternative_tile), drag_painted_value);
  1962. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.alternative_tile), tile_data->get_terrain_set());
  1963. if (tile_data->get_terrain_set() >= 0) {
  1964. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.alternative_tile), tile_data->get_terrain());
  1965. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1966. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1967. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  1968. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.alternative_tile), tile_data->get_terrain_peering_bit(bit));
  1969. }
  1970. }
  1971. }
  1972. }
  1973. undo_redo->commit_action(true);
  1974. drag_type = DRAG_TYPE_NONE;
  1975. accept_event();
  1976. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
  1977. undo_redo->create_action(TTR("Painting Terrain Set"));
  1978. for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
  1979. Dictionary dict = E.value;
  1980. Vector2i coords = E.key.get_atlas_coords();
  1981. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.key.alternative_tile), drag_painted_value);
  1982. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.key.alternative_tile), dict["terrain_set"]);
  1983. if (int(dict["terrain_set"]) >= 0) {
  1984. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), dict["terrain"]);
  1985. Array array = dict["terrain_peering_bits"];
  1986. for (int i = 0; i < array.size(); i++) {
  1987. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1988. if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
  1989. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), array[i]);
  1990. }
  1991. }
  1992. }
  1993. }
  1994. undo_redo->commit_action(false);
  1995. drag_type = DRAG_TYPE_NONE;
  1996. accept_event();
  1997. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
  1998. Dictionary painted = Dictionary(drag_painted_value);
  1999. int terrain_set = int(painted["terrain_set"]);
  2000. int terrain = int(painted["terrain"]);
  2001. undo_redo->create_action(TTR("Painting Terrain"));
  2002. for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
  2003. Dictionary dict = E.value;
  2004. Vector2i coords = E.key.get_atlas_coords();
  2005. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), terrain);
  2006. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), dict["terrain"]);
  2007. Array array = dict["terrain_peering_bits"];
  2008. for (int i = 0; i < array.size(); i++) {
  2009. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2010. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2011. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), terrain);
  2012. }
  2013. if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
  2014. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), array[i]);
  2015. }
  2016. }
  2017. }
  2018. undo_redo->commit_action(false);
  2019. drag_type = DRAG_TYPE_NONE;
  2020. accept_event();
  2021. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS_RECT) {
  2022. Dictionary painted = Dictionary(drag_painted_value);
  2023. int terrain_set = int(painted["terrain_set"]);
  2024. int terrain = int(painted["terrain"]);
  2025. Rect2i rect;
  2026. rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
  2027. rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
  2028. rect = rect.abs();
  2029. RBSet<TileMapCell> edited;
  2030. for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
  2031. for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
  2032. Vector2i coords = Vector2i(x, y);
  2033. coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
  2034. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2035. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  2036. if (tile_data->get_terrain_set() == terrain_set) {
  2037. TileMapCell cell;
  2038. cell.source_id = 0;
  2039. cell.set_atlas_coords(coords);
  2040. cell.alternative_tile = 0;
  2041. edited.insert(cell);
  2042. }
  2043. }
  2044. }
  2045. }
  2046. Vector<Point2> mouse_pos_rect_polygon;
  2047. mouse_pos_rect_polygon.push_back(drag_start_pos);
  2048. mouse_pos_rect_polygon.push_back(Vector2(mb->get_position().x, drag_start_pos.y));
  2049. mouse_pos_rect_polygon.push_back(mb->get_position());
  2050. mouse_pos_rect_polygon.push_back(Vector2(drag_start_pos.x, mb->get_position().y));
  2051. undo_redo->create_action(TTR("Painting Terrain"));
  2052. for (const TileMapCell &E : edited) {
  2053. Vector2i coords = E.get_atlas_coords();
  2054. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
  2055. Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
  2056. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  2057. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  2058. for (int j = 0; j < polygon.size(); j++) {
  2059. polygon.write[j] += position;
  2060. }
  2061. if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
  2062. // Draw terrain.
  2063. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.alternative_tile), terrain);
  2064. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.alternative_tile), tile_data->get_terrain());
  2065. }
  2066. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2067. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2068. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2069. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  2070. for (int j = 0; j < polygon.size(); j++) {
  2071. polygon.write[j] += position;
  2072. }
  2073. if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
  2074. // Draw bit.
  2075. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.alternative_tile), terrain);
  2076. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.alternative_tile), tile_data->get_terrain_peering_bit(bit));
  2077. }
  2078. }
  2079. }
  2080. }
  2081. undo_redo->commit_action(true);
  2082. drag_type = DRAG_TYPE_NONE;
  2083. accept_event();
  2084. }
  2085. }
  2086. }
  2087. }
  2088. }
  2089. void TileDataTerrainsEditor::forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
  2090. Ref<InputEventMouseMotion> mm = p_event;
  2091. if (mm.is_valid()) {
  2092. if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
  2093. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
  2094. Vector2i coords = Vector2i(tile.x, tile.y);
  2095. int alternative_tile = tile.z;
  2096. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2097. TileMapCell cell;
  2098. cell.source_id = 0;
  2099. cell.set_atlas_coords(coords);
  2100. cell.alternative_tile = alternative_tile;
  2101. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
  2102. if (!drag_modified.has(cell)) {
  2103. Dictionary dict;
  2104. dict["terrain_set"] = tile_data->get_terrain_set();
  2105. dict["terrain"] = tile_data->get_terrain();
  2106. Array array;
  2107. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2108. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2109. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  2110. }
  2111. dict["terrain_peering_bits"] = array;
  2112. drag_modified[cell] = dict;
  2113. }
  2114. tile_data->set_terrain_set(drag_painted_value);
  2115. }
  2116. drag_last_pos = mm->get_position();
  2117. accept_event();
  2118. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
  2119. Dictionary painted = Dictionary(drag_painted_value);
  2120. int terrain_set = int(painted["terrain_set"]);
  2121. int terrain = int(painted["terrain"]);
  2122. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
  2123. Vector2i coords = Vector2i(tile.x, tile.y);
  2124. int alternative_tile = tile.z;
  2125. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2126. TileMapCell cell;
  2127. cell.source_id = 0;
  2128. cell.set_atlas_coords(coords);
  2129. cell.alternative_tile = alternative_tile;
  2130. // Save the old terrain_set and terrains bits.
  2131. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
  2132. if (tile_data->get_terrain_set() == terrain_set) {
  2133. if (!drag_modified.has(cell)) {
  2134. Dictionary dict;
  2135. dict["terrain_set"] = tile_data->get_terrain_set();
  2136. dict["terrain"] = tile_data->get_terrain();
  2137. Array array;
  2138. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2139. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2140. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  2141. }
  2142. dict["terrain_peering_bits"] = array;
  2143. drag_modified[cell] = dict;
  2144. }
  2145. // Set the terrains bits.
  2146. Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  2147. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
  2148. Vector<Vector2> polygon = tile_set->get_terrain_polygon(tile_data->get_terrain_set());
  2149. if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
  2150. tile_data->set_terrain(terrain);
  2151. }
  2152. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2153. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2154. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  2155. polygon = tile_set->get_terrain_peering_bit_polygon(tile_data->get_terrain_set(), bit);
  2156. if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
  2157. tile_data->set_terrain_peering_bit(bit, terrain);
  2158. }
  2159. }
  2160. }
  2161. }
  2162. }
  2163. drag_last_pos = mm->get_position();
  2164. accept_event();
  2165. }
  2166. }
  2167. Ref<InputEventMouseButton> mb = p_event;
  2168. if (mb.is_valid()) {
  2169. if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
  2170. if (mb->is_pressed()) {
  2171. if (mb->get_button_index() == MouseButton::LEFT && picker_button->is_pressed()) {
  2172. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
  2173. Vector2i coords = Vector2i(tile.x, tile.y);
  2174. int alternative_tile = tile.z;
  2175. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2176. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
  2177. int terrain_set = tile_data->get_terrain_set();
  2178. Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  2179. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
  2180. dummy_object->set("terrain_set", terrain_set);
  2181. dummy_object->set("terrain", -1);
  2182. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  2183. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  2184. dummy_object->set("terrain", tile_data->get_terrain());
  2185. }
  2186. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2187. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2188. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2189. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  2190. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  2191. dummy_object->set("terrain", tile_data->get_terrain_peering_bit(bit));
  2192. }
  2193. }
  2194. }
  2195. terrain_set_property_editor->update_property();
  2196. _update_terrain_selector();
  2197. picker_button->set_pressed(false);
  2198. accept_event();
  2199. }
  2200. } else {
  2201. int terrain_set = int(dummy_object->get("terrain_set"));
  2202. int terrain = int(dummy_object->get("terrain"));
  2203. Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
  2204. Vector2i coords = Vector2i(tile.x, tile.y);
  2205. int alternative_tile = tile.z;
  2206. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2207. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
  2208. if (terrain_set == -1 || !tile_data || tile_data->get_terrain_set() != terrain_set) {
  2209. // Paint terrain sets.
  2210. if (mb->get_button_index() == MouseButton::RIGHT) {
  2211. terrain_set = -1;
  2212. }
  2213. drag_type = DRAG_TYPE_PAINT_TERRAIN_SET;
  2214. drag_modified.clear();
  2215. drag_painted_value = int(dummy_object->get("terrain_set"));
  2216. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2217. TileMapCell cell;
  2218. cell.source_id = 0;
  2219. cell.set_atlas_coords(coords);
  2220. cell.alternative_tile = alternative_tile;
  2221. Dictionary dict;
  2222. dict["terrain_set"] = tile_data->get_terrain_set();
  2223. Array array;
  2224. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2225. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2226. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  2227. }
  2228. dict["terrain_peering_bits"] = array;
  2229. drag_modified[cell] = dict;
  2230. tile_data->set_terrain_set(drag_painted_value);
  2231. }
  2232. drag_last_pos = mb->get_position();
  2233. accept_event();
  2234. } else if (tile_data->get_terrain_set() == terrain_set) {
  2235. // Paint terrain bits.
  2236. if (mb->get_button_index() == MouseButton::RIGHT) {
  2237. terrain = -1;
  2238. }
  2239. // Paint terrain bits.
  2240. drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS;
  2241. drag_modified.clear();
  2242. Dictionary painted_dict;
  2243. painted_dict["terrain_set"] = terrain_set;
  2244. painted_dict["terrain"] = terrain;
  2245. drag_painted_value = painted_dict;
  2246. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  2247. TileMapCell cell;
  2248. cell.source_id = 0;
  2249. cell.set_atlas_coords(coords);
  2250. cell.alternative_tile = alternative_tile;
  2251. // Save the old terrain_set and terrains bits.
  2252. Dictionary dict;
  2253. dict["terrain_set"] = tile_data->get_terrain_set();
  2254. dict["terrain"] = tile_data->get_terrain();
  2255. Array array;
  2256. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2257. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2258. array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
  2259. }
  2260. dict["terrain_peering_bits"] = array;
  2261. drag_modified[cell] = dict;
  2262. // Set the terrain bit.
  2263. Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  2264. Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
  2265. Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
  2266. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  2267. tile_data->set_terrain(terrain);
  2268. }
  2269. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2270. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2271. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2272. polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
  2273. if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
  2274. tile_data->set_terrain_peering_bit(bit, terrain);
  2275. }
  2276. }
  2277. }
  2278. }
  2279. drag_last_pos = mb->get_position();
  2280. accept_event();
  2281. }
  2282. }
  2283. }
  2284. } else {
  2285. if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
  2286. undo_redo->create_action(TTR("Painting Tiles Property"));
  2287. for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
  2288. Dictionary dict = E.value;
  2289. Vector2i coords = E.key.get_atlas_coords();
  2290. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.key.alternative_tile), drag_painted_value);
  2291. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain_set", coords.x, coords.y, E.key.alternative_tile), dict["terrain_set"]);
  2292. if (int(dict["terrain_set"]) >= 0) {
  2293. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), dict["terrain"]);
  2294. Array array = dict["terrain_peering_bits"];
  2295. for (int i = 0; i < array.size(); i++) {
  2296. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), array[i]);
  2297. }
  2298. }
  2299. }
  2300. undo_redo->commit_action(false);
  2301. drag_type = DRAG_TYPE_NONE;
  2302. accept_event();
  2303. } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
  2304. Dictionary painted = Dictionary(drag_painted_value);
  2305. int terrain_set = int(painted["terrain_set"]);
  2306. int terrain = int(painted["terrain"]);
  2307. undo_redo->create_action(TTR("Painting Terrain"));
  2308. for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
  2309. Dictionary dict = E.value;
  2310. Vector2i coords = E.key.get_atlas_coords();
  2311. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), terrain);
  2312. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), dict["terrain"]);
  2313. Array array = dict["terrain_peering_bits"];
  2314. for (int i = 0; i < array.size(); i++) {
  2315. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2316. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2317. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), terrain);
  2318. }
  2319. if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
  2320. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]), coords.x, coords.y, E.key.alternative_tile), array[i]);
  2321. }
  2322. }
  2323. }
  2324. undo_redo->commit_action(false);
  2325. drag_type = DRAG_TYPE_NONE;
  2326. accept_event();
  2327. }
  2328. }
  2329. }
  2330. }
  2331. }
  2332. void TileDataTerrainsEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  2333. TileData *tile_data = _get_tile_data(p_cell);
  2334. ERR_FAIL_COND(!tile_data);
  2335. tile_set->draw_terrains(p_canvas_item, p_transform, tile_data);
  2336. }
  2337. void TileDataTerrainsEditor::_notification(int p_what) {
  2338. switch (p_what) {
  2339. case NOTIFICATION_ENTER_TREE:
  2340. case NOTIFICATION_THEME_CHANGED: {
  2341. picker_button->set_icon(get_theme_icon(SNAME("ColorPick"), SNAME("EditorIcons")));
  2342. } break;
  2343. }
  2344. }
  2345. TileDataTerrainsEditor::TileDataTerrainsEditor() {
  2346. undo_redo = EditorNode::get_undo_redo();
  2347. label = memnew(Label);
  2348. label->set_text(TTR("Painting:"));
  2349. label->set_theme_type_variation("HeaderSmall");
  2350. add_child(label);
  2351. // Toolbar
  2352. toolbar->add_child(memnew(VSeparator));
  2353. picker_button = memnew(Button);
  2354. picker_button->set_flat(true);
  2355. picker_button->set_toggle_mode(true);
  2356. picker_button->set_shortcut(ED_SHORTCUT("tiles_editor/picker", "Picker", Key::P));
  2357. toolbar->add_child(picker_button);
  2358. // Setup
  2359. dummy_object->add_dummy_property("terrain_set");
  2360. dummy_object->set("terrain_set", -1);
  2361. dummy_object->add_dummy_property("terrain");
  2362. dummy_object->set("terrain", -1);
  2363. // Get the default value for the type.
  2364. terrain_set_property_editor = memnew(EditorPropertyEnum);
  2365. terrain_set_property_editor->set_object_and_property(dummy_object, "terrain_set");
  2366. terrain_set_property_editor->set_label("Terrain Set");
  2367. terrain_set_property_editor->connect("property_changed", callable_mp(this, &TileDataTerrainsEditor::_property_value_changed).unbind(1));
  2368. terrain_set_property_editor->set_tooltip_text(terrain_set_property_editor->get_edited_property());
  2369. add_child(terrain_set_property_editor);
  2370. terrain_property_editor = memnew(EditorPropertyEnum);
  2371. terrain_property_editor->set_object_and_property(dummy_object, "terrain");
  2372. terrain_property_editor->set_label("Terrain");
  2373. terrain_property_editor->connect("property_changed", callable_mp(this, &TileDataTerrainsEditor::_property_value_changed).unbind(1));
  2374. add_child(terrain_property_editor);
  2375. }
  2376. TileDataTerrainsEditor::~TileDataTerrainsEditor() {
  2377. toolbar->queue_delete();
  2378. memdelete(dummy_object);
  2379. }
  2380. Variant TileDataNavigationEditor::_get_painted_value() {
  2381. Ref<NavigationPolygon> nav_polygon;
  2382. nav_polygon.instantiate();
  2383. for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
  2384. Vector<Vector2> polygon = polygon_editor->get_polygon(i);
  2385. nav_polygon->add_outline(polygon);
  2386. }
  2387. nav_polygon->make_polygons_from_outlines();
  2388. return nav_polygon;
  2389. }
  2390. void TileDataNavigationEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  2391. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  2392. ERR_FAIL_COND(!tile_data);
  2393. Ref<NavigationPolygon> nav_polygon = tile_data->get_navigation_polygon(navigation_layer);
  2394. polygon_editor->clear_polygons();
  2395. if (nav_polygon.is_valid()) {
  2396. for (int i = 0; i < nav_polygon->get_outline_count(); i++) {
  2397. polygon_editor->add_polygon(nav_polygon->get_outline(i));
  2398. }
  2399. }
  2400. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  2401. }
  2402. void TileDataNavigationEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
  2403. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  2404. ERR_FAIL_COND(!tile_data);
  2405. Ref<NavigationPolygon> nav_polygon = p_value;
  2406. tile_data->set_navigation_polygon(navigation_layer, nav_polygon);
  2407. polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), p_tile_set_atlas_source->get_tile_effective_texture_offset(p_coords, p_alternative_tile), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  2408. }
  2409. Variant TileDataNavigationEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
  2410. TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
  2411. ERR_FAIL_COND_V(!tile_data, Variant());
  2412. return tile_data->get_navigation_polygon(navigation_layer);
  2413. }
  2414. void TileDataNavigationEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
  2415. for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
  2416. Vector2i coords = E.key.get_atlas_coords();
  2417. undo_redo->add_undo_property(p_tile_set_atlas_source, vformat("%d:%d/%d/navigation_layer_%d/polygon", coords.x, coords.y, E.key.alternative_tile, navigation_layer), E.value);
  2418. undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/navigation_layer_%d/polygon", coords.x, coords.y, E.key.alternative_tile, navigation_layer), p_new_value);
  2419. }
  2420. }
  2421. void TileDataNavigationEditor::_tile_set_changed() {
  2422. polygon_editor->set_tile_set(tile_set);
  2423. }
  2424. void TileDataNavigationEditor::_notification(int p_what) {
  2425. switch (p_what) {
  2426. case NOTIFICATION_ENTER_TREE: {
  2427. #ifdef DEBUG_ENABLED
  2428. polygon_editor->set_polygons_color(NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color());
  2429. #endif // DEBUG_ENABLED
  2430. } break;
  2431. }
  2432. }
  2433. TileDataNavigationEditor::TileDataNavigationEditor() {
  2434. undo_redo = EditorNode::get_undo_redo();
  2435. polygon_editor = memnew(GenericTilePolygonEditor);
  2436. polygon_editor->set_multiple_polygon_mode(true);
  2437. add_child(polygon_editor);
  2438. }
  2439. void TileDataNavigationEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
  2440. TileData *tile_data = _get_tile_data(p_cell);
  2441. ERR_FAIL_COND(!tile_data);
  2442. // Draw all shapes.
  2443. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  2444. Ref<NavigationPolygon> nav_polygon = tile_data->get_navigation_polygon(navigation_layer);
  2445. if (nav_polygon.is_valid()) {
  2446. Vector<Vector2> verts = nav_polygon->get_vertices();
  2447. if (verts.size() < 3) {
  2448. return;
  2449. }
  2450. Color color = Color(0.5, 1.0, 1.0, 1.0);
  2451. #ifdef DEBUG_ENABLED
  2452. color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  2453. #endif // DEBUG_ENABLED
  2454. if (p_selected) {
  2455. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  2456. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  2457. selection_color.a = 0.7;
  2458. color = selection_color;
  2459. }
  2460. RandomPCG rand;
  2461. for (int i = 0; i < nav_polygon->get_polygon_count(); i++) {
  2462. // An array of vertices for this polygon.
  2463. Vector<int> polygon = nav_polygon->get_polygon(i);
  2464. Vector<Vector2> vertices;
  2465. vertices.resize(polygon.size());
  2466. for (int j = 0; j < polygon.size(); j++) {
  2467. ERR_FAIL_INDEX(polygon[j], verts.size());
  2468. vertices.write[j] = verts[polygon[j]];
  2469. }
  2470. // Generate the polygon color, slightly randomly modified from the settings one.
  2471. Color random_variation_color;
  2472. random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1);
  2473. random_variation_color.a = color.a;
  2474. Vector<Color> colors;
  2475. colors.push_back(random_variation_color);
  2476. RenderingServer::get_singleton()->canvas_item_add_polygon(p_canvas_item->get_canvas_item(), vertices, colors);
  2477. }
  2478. }
  2479. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  2480. }