1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785 |
- /*************************************************************************/
- /* tile_data_editors.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "tile_data_editors.h"
- #include "tile_set_editor.h"
- #include "core/math/geometry_2d.h"
- #include "core/os/keyboard.h"
- #include "editor/editor_node.h"
- #include "editor/editor_properties.h"
- #include "editor/editor_scale.h"
- #include "editor/editor_undo_redo_manager.h"
- #ifdef DEBUG_ENABLED
- #include "servers/navigation_server_3d.h"
- #endif // DEBUG_ENABLED
- void TileDataEditor::_tile_set_changed_plan_update() {
- _tile_set_changed_update_needed = true;
- call_deferred(SNAME("_tile_set_changed_deferred_update"));
- }
- void TileDataEditor::_tile_set_changed_deferred_update() {
- if (_tile_set_changed_update_needed) {
- _tile_set_changed();
- _tile_set_changed_update_needed = false;
- }
- }
- TileData *TileDataEditor::_get_tile_data(TileMapCell p_cell) {
- ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
- ERR_FAIL_COND_V(!tile_set->has_source(p_cell.source_id), nullptr);
- TileData *td = nullptr;
- TileSetSource *source = *tile_set->get_source(p_cell.source_id);
- TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
- if (atlas_source) {
- ERR_FAIL_COND_V(!atlas_source->has_tile(p_cell.get_atlas_coords()), nullptr);
- ERR_FAIL_COND_V(!atlas_source->has_alternative_tile(p_cell.get_atlas_coords(), p_cell.alternative_tile), nullptr);
- td = atlas_source->get_tile_data(p_cell.get_atlas_coords(), p_cell.alternative_tile);
- }
- return td;
- }
- void TileDataEditor::_bind_methods() {
- ClassDB::bind_method(D_METHOD("_tile_set_changed_deferred_update"), &TileDataEditor::_tile_set_changed_deferred_update);
- ADD_SIGNAL(MethodInfo("needs_redraw"));
- }
- void TileDataEditor::set_tile_set(Ref<TileSet> p_tile_set) {
- if (tile_set.is_valid()) {
- tile_set->disconnect("changed", callable_mp(this, &TileDataEditor::_tile_set_changed_plan_update));
- }
- tile_set = p_tile_set;
- if (tile_set.is_valid()) {
- tile_set->connect("changed", callable_mp(this, &TileDataEditor::_tile_set_changed_plan_update));
- }
- _tile_set_changed_plan_update();
- }
- bool DummyObject::_set(const StringName &p_name, const Variant &p_value) {
- if (properties.has(p_name)) {
- properties[p_name] = p_value;
- return true;
- }
- return false;
- }
- bool DummyObject::_get(const StringName &p_name, Variant &r_ret) const {
- if (properties.has(p_name)) {
- r_ret = properties[p_name];
- return true;
- }
- return false;
- }
- bool DummyObject::has_dummy_property(StringName p_name) {
- return properties.has(p_name);
- }
- void DummyObject::add_dummy_property(StringName p_name) {
- ERR_FAIL_COND(properties.has(p_name));
- properties[p_name] = Variant();
- }
- void DummyObject::remove_dummy_property(StringName p_name) {
- ERR_FAIL_COND(!properties.has(p_name));
- properties.erase(p_name);
- }
- void DummyObject::clear_dummy_properties() {
- properties.clear();
- }
- void GenericTilePolygonEditor::_base_control_draw() {
- ERR_FAIL_COND(!tile_set.is_valid());
- real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- const Ref<Texture2D> handle = get_theme_icon(SNAME("EditorPathSharpHandle"), SNAME("EditorIcons"));
- const Ref<Texture2D> add_handle = get_theme_icon(SNAME("EditorHandleAdd"), SNAME("EditorIcons"));
- const Ref<StyleBox> focus_stylebox = get_theme_stylebox(SNAME("Focus"), SNAME("EditorStyles"));
- // Draw the focus rectangle.
- if (base_control->has_focus()) {
- base_control->draw_style_box(focus_stylebox, Rect2(Vector2(), base_control->get_size()));
- }
- // Draw tile-related things.
- Size2 tile_size = tile_set->get_tile_size();
- Transform2D xform;
- xform.set_origin(base_control->get_size() / 2 + panning);
- xform.set_scale(Vector2(editor_zoom_widget->get_zoom(), editor_zoom_widget->get_zoom()));
- base_control->draw_set_transform_matrix(xform);
- // Draw the tile shape filled.
- Transform2D tile_xform;
- tile_xform.set_scale(tile_size);
- tile_set->draw_tile_shape(base_control, tile_xform, Color(1.0, 1.0, 1.0, 0.3), true);
- // Draw the background.
- if (background_texture.is_valid()) {
- base_control->draw_texture_rect_region(background_texture, Rect2(-background_region.size / 2 - background_offset, background_region.size), background_region, background_modulate, background_transpose);
- }
- // Draw the polygons.
- for (unsigned int i = 0; i < polygons.size(); i++) {
- const Vector<Vector2> &polygon = polygons[i];
- Color color = polygon_color;
- if (!in_creation_polygon.is_empty()) {
- color = color.darkened(0.3);
- }
- color.a = 0.5;
- Vector<Color> v_color;
- v_color.push_back(color);
- base_control->draw_polygon(polygon, v_color);
- color.a = 0.7;
- for (int j = 0; j < polygon.size(); j++) {
- base_control->draw_line(polygon[j], polygon[(j + 1) % polygon.size()], color);
- }
- }
- // Draw the polygon in creation.
- if (!in_creation_polygon.is_empty()) {
- for (int i = 0; i < in_creation_polygon.size() - 1; i++) {
- base_control->draw_line(in_creation_polygon[i], in_creation_polygon[i + 1], Color(1.0, 1.0, 1.0));
- }
- }
- Point2 in_creation_point = xform.affine_inverse().xform(base_control->get_local_mouse_position());
- float in_creation_distance = grab_threshold * 2.0;
- _snap_to_tile_shape(in_creation_point, in_creation_distance, grab_threshold / editor_zoom_widget->get_zoom());
- if (button_pixel_snap->is_pressed()) {
- _snap_to_half_pixel(in_creation_point);
- }
- if (drag_type == DRAG_TYPE_CREATE_POINT && !in_creation_polygon.is_empty()) {
- base_control->draw_line(in_creation_polygon[in_creation_polygon.size() - 1], in_creation_point, Color(1.0, 1.0, 1.0));
- }
- // Draw the handles.
- int tinted_polygon_index = -1;
- int tinted_point_index = -1;
- if (drag_type == DRAG_TYPE_DRAG_POINT) {
- tinted_polygon_index = drag_polygon_index;
- tinted_point_index = drag_point_index;
- } else if (hovered_point_index >= 0) {
- tinted_polygon_index = hovered_polygon_index;
- tinted_point_index = hovered_point_index;
- }
- base_control->draw_set_transform_matrix(Transform2D());
- if (!in_creation_polygon.is_empty()) {
- for (int i = 0; i < in_creation_polygon.size(); i++) {
- base_control->draw_texture(handle, xform.xform(in_creation_polygon[i]) - handle->get_size() / 2);
- }
- } else {
- for (int i = 0; i < (int)polygons.size(); i++) {
- const Vector<Vector2> &polygon = polygons[i];
- for (int j = 0; j < polygon.size(); j++) {
- const Color poly_modulate = (tinted_polygon_index == i && tinted_point_index == j) ? Color(0.5, 1, 2) : Color(1, 1, 1);
- base_control->draw_texture(handle, xform.xform(polygon[j]) - handle->get_size() / 2, poly_modulate);
- }
- }
- }
- // Draw the text on top of the selected point.
- if (tinted_polygon_index >= 0) {
- Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
- int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
- String text = multiple_polygon_mode ? vformat("%d:%d", tinted_polygon_index, tinted_point_index) : vformat("%d", tinted_point_index);
- Size2 text_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- 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));
- }
- if (drag_type == DRAG_TYPE_CREATE_POINT) {
- base_control->draw_texture(handle, xform.xform(in_creation_point) - handle->get_size() / 2, Color(0.5, 1, 2));
- }
- // Draw the point creation preview in edit mode.
- if (hovered_segment_index >= 0) {
- base_control->draw_texture(add_handle, xform.xform(hovered_segment_point) - add_handle->get_size() / 2);
- }
- // Draw the tile shape line.
- base_control->draw_set_transform_matrix(xform);
- tile_set->draw_tile_shape(base_control, tile_xform, grid_color, false);
- base_control->draw_set_transform_matrix(Transform2D());
- }
- void GenericTilePolygonEditor::_center_view() {
- panning = Vector2();
- base_control->queue_redraw();
- button_center_view->set_disabled(true);
- }
- void GenericTilePolygonEditor::_zoom_changed() {
- base_control->queue_redraw();
- }
- void GenericTilePolygonEditor::_advanced_menu_item_pressed(int p_item_pressed) {
- Ref<EditorUndoRedoManager> undo_redo;
- if (use_undo_redo) {
- undo_redo = editor_undo_redo;
- } else {
- // This nice hack allows for discarding undo actions without making code too complex.
- undo_redo.instantiate();
- }
- switch (p_item_pressed) {
- case RESET_TO_DEFAULT_TILE: {
- undo_redo->create_action(TTR("Reset Polygons"));
- undo_redo->add_do_method(this, "clear_polygons");
- Vector<Vector2> polygon = tile_set->get_tile_shape_polygon();
- for (int i = 0; i < polygon.size(); i++) {
- polygon.write[i] = polygon[i] * tile_set->get_tile_size();
- }
- undo_redo->add_do_method(this, "add_polygon", polygon);
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
- undo_redo->add_undo_method(this, "clear_polygons");
- for (unsigned int i = 0; i < polygons.size(); i++) {
- undo_redo->add_undo_method(this, "add_polygon", polygons[i]);
- }
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
- undo_redo->commit_action(true);
- } break;
- case CLEAR_TILE: {
- undo_redo->create_action(TTR("Clear Polygons"));
- undo_redo->add_do_method(this, "clear_polygons");
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
- undo_redo->add_undo_method(this, "clear_polygons");
- for (unsigned int i = 0; i < polygons.size(); i++) {
- undo_redo->add_undo_method(this, "add_polygon", polygons[i]);
- }
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
- undo_redo->commit_action(true);
- } break;
- case ROTATE_RIGHT:
- case ROTATE_LEFT:
- case FLIP_HORIZONTALLY:
- case FLIP_VERTICALLY: {
- undo_redo->create_action(TTR("Rotate Polygons Left"));
- for (unsigned int i = 0; i < polygons.size(); i++) {
- Vector<Point2> new_polygon;
- for (int point_index = 0; point_index < polygons[i].size(); point_index++) {
- Vector2 point = polygons[i][point_index];
- switch (p_item_pressed) {
- case ROTATE_RIGHT: {
- point = Vector2(-point.y, point.x);
- } break;
- case ROTATE_LEFT: {
- point = Vector2(point.y, -point.x);
- } break;
- case FLIP_HORIZONTALLY: {
- point = Vector2(-point.x, point.y);
- } break;
- case FLIP_VERTICALLY: {
- point = Vector2(point.x, -point.y);
- } break;
- default:
- break;
- }
- new_polygon.push_back(point);
- }
- undo_redo->add_do_method(this, "set_polygon", i, new_polygon);
- }
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_do_method(this, "emit_signal", "polygons_changed");
- for (unsigned int i = 0; i < polygons.size(); i++) {
- undo_redo->add_undo_method(this, "set_polygon", polygons[i]);
- }
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(this, "emit_signal", "polygons_changed");
- undo_redo->commit_action(true);
- } break;
- default:
- break;
- }
- }
- void GenericTilePolygonEditor::_grab_polygon_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_point_index) {
- const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
- r_polygon_index = -1;
- r_point_index = -1;
- float closest_distance = grab_threshold + 1.0;
- for (unsigned int i = 0; i < polygons.size(); i++) {
- const Vector<Vector2> &polygon = polygons[i];
- for (int j = 0; j < polygon.size(); j++) {
- float distance = p_pos.distance_to(p_polygon_xform.xform(polygon[j]));
- if (distance < grab_threshold && distance < closest_distance) {
- r_polygon_index = i;
- r_point_index = j;
- closest_distance = distance;
- }
- }
- }
- }
- 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) {
- const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
- Point2 point = p_polygon_xform.affine_inverse().xform(p_pos);
- r_polygon_index = -1;
- r_segment_index = -1;
- float closest_distance = grab_threshold * 2.0;
- for (unsigned int i = 0; i < polygons.size(); i++) {
- const Vector<Vector2> &polygon = polygons[i];
- for (int j = 0; j < polygon.size(); j++) {
- Vector2 segment[2] = { polygon[j], polygon[(j + 1) % polygon.size()] };
- Vector2 closest_point = Geometry2D::get_closest_point_to_segment(point, segment);
- float distance = closest_point.distance_to(point);
- if (distance < grab_threshold / editor_zoom_widget->get_zoom() && distance < closest_distance) {
- r_polygon_index = i;
- r_segment_index = j;
- r_point = closest_point;
- closest_distance = distance;
- }
- }
- }
- }
- void GenericTilePolygonEditor::_snap_to_tile_shape(Point2 &r_point, float &r_current_snapped_dist, float p_snap_dist) {
- ERR_FAIL_COND(!tile_set.is_valid());
- Vector<Point2> polygon = tile_set->get_tile_shape_polygon();
- for (int i = 0; i < polygon.size(); i++) {
- polygon.write[i] = polygon[i] * tile_set->get_tile_size();
- }
- Point2 snapped_point = r_point;
- // Snap to polygon vertices.
- bool snapped = false;
- for (int i = 0; i < polygon.size(); i++) {
- float distance = r_point.distance_to(polygon[i]);
- if (distance < p_snap_dist && distance < r_current_snapped_dist) {
- snapped_point = polygon[i];
- r_current_snapped_dist = distance;
- snapped = true;
- }
- }
- // Snap to edges if we did not snap to vertices.
- if (!snapped) {
- for (int i = 0; i < polygon.size(); i++) {
- Point2 segment[2] = { polygon[i], polygon[(i + 1) % polygon.size()] };
- Point2 point = Geometry2D::get_closest_point_to_segment(r_point, segment);
- float distance = r_point.distance_to(point);
- if (distance < p_snap_dist && distance < r_current_snapped_dist) {
- snapped_point = point;
- r_current_snapped_dist = distance;
- }
- }
- }
- r_point = snapped_point;
- }
- void GenericTilePolygonEditor::_snap_to_half_pixel(Point2 &r_point) {
- r_point = (r_point * 2).round() / 2.0;
- }
- void GenericTilePolygonEditor::_base_control_gui_input(Ref<InputEvent> p_event) {
- Ref<EditorUndoRedoManager> undo_redo;
- if (use_undo_redo) {
- undo_redo = editor_undo_redo;
- } else {
- // This nice hack allows for discarding undo actions without making code too complex.
- undo_redo.instantiate();
- }
- real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
- hovered_polygon_index = -1;
- hovered_point_index = -1;
- hovered_segment_index = -1;
- hovered_segment_point = Vector2();
- Transform2D xform;
- xform.set_origin(base_control->get_size() / 2 + panning);
- xform.set_scale(Vector2(editor_zoom_widget->get_zoom(), editor_zoom_widget->get_zoom()));
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (drag_type == DRAG_TYPE_DRAG_POINT) {
- ERR_FAIL_INDEX(drag_polygon_index, (int)polygons.size());
- ERR_FAIL_INDEX(drag_point_index, polygons[drag_polygon_index].size());
- Point2 point = xform.affine_inverse().xform(mm->get_position());
- float distance = grab_threshold * 2.0;
- _snap_to_tile_shape(point, distance, grab_threshold / editor_zoom_widget->get_zoom());
- if (button_pixel_snap->is_pressed()) {
- _snap_to_half_pixel(point);
- }
- polygons[drag_polygon_index].write[drag_point_index] = point;
- } else if (drag_type == DRAG_TYPE_PAN) {
- panning += mm->get_position() - drag_last_pos;
- drag_last_pos = mm->get_position();
- button_center_view->set_disabled(panning.is_zero_approx());
- } else {
- // Update hovered point.
- _grab_polygon_point(mm->get_position(), xform, hovered_polygon_index, hovered_point_index);
- // If we have no hovered point, check if we hover a segment.
- if (hovered_point_index == -1) {
- _grab_polygon_segment_point(mm->get_position(), xform, hovered_polygon_index, hovered_segment_index, hovered_segment_point);
- }
- }
- }
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_ctrl_pressed()) {
- editor_zoom_widget->set_zoom_by_increments(1);
- _zoom_changed();
- accept_event();
- } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_ctrl_pressed()) {
- editor_zoom_widget->set_zoom_by_increments(-1);
- _zoom_changed();
- accept_event();
- } else if (mb->get_button_index() == MouseButton::LEFT) {
- if (mb->is_pressed()) {
- if (tools_button_group->get_pressed_button() != button_create) {
- in_creation_polygon.clear();
- }
- if (tools_button_group->get_pressed_button() == button_create) {
- // Create points.
- if (in_creation_polygon.size() >= 3 && mb->get_position().distance_to(xform.xform(in_creation_polygon[0])) < grab_threshold) {
- // Closes and create polygon.
- if (!multiple_polygon_mode) {
- clear_polygons();
- }
- int added = add_polygon(in_creation_polygon);
- in_creation_polygon.clear();
- button_edit->set_pressed(true);
- undo_redo->create_action(TTR("Edit Polygons"));
- if (!multiple_polygon_mode) {
- undo_redo->add_do_method(this, "clear_polygons");
- }
- undo_redo->add_do_method(this, "add_polygon", in_creation_polygon);
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(this, "remove_polygon", added);
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->commit_action(false);
- emit_signal(SNAME("polygons_changed"));
- } else {
- // Create a new point.
- drag_type = DRAG_TYPE_CREATE_POINT;
- }
- } else if (tools_button_group->get_pressed_button() == button_edit) {
- // Edit points.
- int closest_polygon;
- int closest_point;
- _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
- if (closest_polygon >= 0) {
- drag_type = DRAG_TYPE_DRAG_POINT;
- drag_polygon_index = closest_polygon;
- drag_point_index = closest_point;
- drag_old_polygon = polygons[drag_polygon_index];
- } else {
- // Create a point.
- Vector2 point_to_create;
- _grab_polygon_segment_point(mb->get_position(), xform, closest_polygon, closest_point, point_to_create);
- if (closest_polygon >= 0) {
- polygons[closest_polygon].insert(closest_point + 1, point_to_create);
- drag_type = DRAG_TYPE_DRAG_POINT;
- drag_polygon_index = closest_polygon;
- drag_point_index = closest_point + 1;
- drag_old_polygon = polygons[closest_polygon];
- }
- }
- } else if (tools_button_group->get_pressed_button() == button_delete) {
- // Remove point.
- int closest_polygon;
- int closest_point;
- _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
- if (closest_polygon >= 0) {
- PackedVector2Array old_polygon = polygons[closest_polygon];
- polygons[closest_polygon].remove_at(closest_point);
- undo_redo->create_action(TTR("Edit Polygons"));
- if (polygons[closest_polygon].size() < 3) {
- remove_polygon(closest_polygon);
- undo_redo->add_do_method(this, "remove_polygon", closest_polygon);
- undo_redo->add_undo_method(this, "add_polygon", old_polygon, closest_polygon);
- } else {
- undo_redo->add_do_method(this, "set_polygon", closest_polygon, polygons[closest_polygon]);
- undo_redo->add_undo_method(this, "set_polygon", closest_polygon, old_polygon);
- }
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->commit_action(false);
- emit_signal(SNAME("polygons_changed"));
- }
- }
- } else {
- if (drag_type == DRAG_TYPE_DRAG_POINT) {
- undo_redo->create_action(TTR("Edit Polygons"));
- undo_redo->add_do_method(this, "set_polygon", drag_polygon_index, polygons[drag_polygon_index]);
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(this, "set_polygon", drag_polygon_index, drag_old_polygon);
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->commit_action(false);
- emit_signal(SNAME("polygons_changed"));
- } else if (drag_type == DRAG_TYPE_CREATE_POINT) {
- Point2 point = xform.affine_inverse().xform(mb->get_position());
- float distance = grab_threshold * 2;
- _snap_to_tile_shape(point, distance, grab_threshold / editor_zoom_widget->get_zoom());
- if (button_pixel_snap->is_pressed()) {
- _snap_to_half_pixel(point);
- }
- in_creation_polygon.push_back(point);
- }
- drag_type = DRAG_TYPE_NONE;
- drag_point_index = -1;
- }
- } else if (mb->get_button_index() == MouseButton::RIGHT) {
- if (mb->is_pressed()) {
- if (tools_button_group->get_pressed_button() == button_edit) {
- // Remove point or pan.
- int closest_polygon;
- int closest_point;
- _grab_polygon_point(mb->get_position(), xform, closest_polygon, closest_point);
- if (closest_polygon >= 0) {
- PackedVector2Array old_polygon = polygons[closest_polygon];
- polygons[closest_polygon].remove_at(closest_point);
- undo_redo->create_action(TTR("Edit Polygons"));
- if (polygons[closest_polygon].size() < 3) {
- remove_polygon(closest_polygon);
- undo_redo->add_do_method(this, "remove_polygon", closest_polygon);
- undo_redo->add_undo_method(this, "add_polygon", old_polygon, closest_polygon);
- } else {
- undo_redo->add_do_method(this, "set_polygon", closest_polygon, polygons[closest_polygon]);
- undo_redo->add_undo_method(this, "set_polygon", closest_polygon, old_polygon);
- }
- undo_redo->add_do_method(base_control, "queue_redraw");
- undo_redo->add_undo_method(base_control, "queue_redraw");
- undo_redo->commit_action(false);
- emit_signal(SNAME("polygons_changed"));
- } else {
- drag_type = DRAG_TYPE_PAN;
- drag_last_pos = mb->get_position();
- }
- } else {
- drag_type = DRAG_TYPE_PAN;
- drag_last_pos = mb->get_position();
- }
- } else {
- drag_type = DRAG_TYPE_NONE;
- }
- } else if (mb->get_button_index() == MouseButton::MIDDLE) {
- if (mb->is_pressed()) {
- drag_type = DRAG_TYPE_PAN;
- drag_last_pos = mb->get_position();
- } else {
- drag_type = DRAG_TYPE_NONE;
- }
- }
- }
- base_control->queue_redraw();
- }
- void GenericTilePolygonEditor::set_use_undo_redo(bool p_use_undo_redo) {
- use_undo_redo = p_use_undo_redo;
- }
- void GenericTilePolygonEditor::set_tile_set(Ref<TileSet> p_tile_set) {
- ERR_FAIL_COND(!p_tile_set.is_valid());
- if (tile_set == p_tile_set) {
- return;
- }
- // Set the default tile shape
- clear_polygons();
- if (p_tile_set.is_valid()) {
- Vector<Vector2> polygon = p_tile_set->get_tile_shape_polygon();
- for (int i = 0; i < polygon.size(); i++) {
- polygon.write[i] = polygon[i] * p_tile_set->get_tile_size();
- }
- add_polygon(polygon);
- }
- tile_set = p_tile_set;
- // Set the default zoom value.
- int default_control_y_size = 200 * EDSCALE;
- Vector2 zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
- while (zoomed_tile.y < default_control_y_size) {
- editor_zoom_widget->set_zoom_by_increments(6, false);
- zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
- }
- while (zoomed_tile.y > default_control_y_size) {
- editor_zoom_widget->set_zoom_by_increments(-6, false);
- zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
- }
- editor_zoom_widget->set_zoom_by_increments(-6, false);
- _zoom_changed();
- }
- 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) {
- background_texture = p_texture;
- background_region = p_region;
- background_offset = p_offset;
- background_h_flip = p_flip_h;
- background_v_flip = p_flip_v;
- background_transpose = p_transpose;
- background_modulate = p_modulate;
- base_control->queue_redraw();
- }
- int GenericTilePolygonEditor::get_polygon_count() {
- return polygons.size();
- }
- int GenericTilePolygonEditor::add_polygon(Vector<Point2> p_polygon, int p_index) {
- ERR_FAIL_COND_V(p_polygon.size() < 3, -1);
- ERR_FAIL_COND_V(!multiple_polygon_mode && polygons.size() >= 1, -1);
- if (p_index < 0) {
- polygons.push_back(p_polygon);
- base_control->queue_redraw();
- button_edit->set_pressed(true);
- return polygons.size() - 1;
- } else {
- polygons.insert(p_index, p_polygon);
- button_edit->set_pressed(true);
- base_control->queue_redraw();
- return p_index;
- }
- }
- void GenericTilePolygonEditor::remove_polygon(int p_index) {
- ERR_FAIL_INDEX(p_index, (int)polygons.size());
- polygons.remove_at(p_index);
- if (polygons.size() == 0) {
- button_create->set_pressed(true);
- }
- base_control->queue_redraw();
- }
- void GenericTilePolygonEditor::clear_polygons() {
- polygons.clear();
- base_control->queue_redraw();
- }
- void GenericTilePolygonEditor::set_polygon(int p_polygon_index, Vector<Point2> p_polygon) {
- ERR_FAIL_INDEX(p_polygon_index, (int)polygons.size());
- ERR_FAIL_COND(p_polygon.size() < 3);
- polygons[p_polygon_index] = p_polygon;
- button_edit->set_pressed(true);
- base_control->queue_redraw();
- }
- Vector<Point2> GenericTilePolygonEditor::get_polygon(int p_polygon_index) {
- ERR_FAIL_INDEX_V(p_polygon_index, (int)polygons.size(), Vector<Point2>());
- return polygons[p_polygon_index];
- }
- void GenericTilePolygonEditor::set_polygons_color(Color p_color) {
- polygon_color = p_color;
- base_control->queue_redraw();
- }
- void GenericTilePolygonEditor::set_multiple_polygon_mode(bool p_multiple_polygon_mode) {
- multiple_polygon_mode = p_multiple_polygon_mode;
- }
- void GenericTilePolygonEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE:
- case NOTIFICATION_THEME_CHANGED: {
- button_create->set_icon(get_theme_icon(SNAME("CurveCreate"), SNAME("EditorIcons")));
- button_edit->set_icon(get_theme_icon(SNAME("CurveEdit"), SNAME("EditorIcons")));
- button_delete->set_icon(get_theme_icon(SNAME("CurveDelete"), SNAME("EditorIcons")));
- button_center_view->set_icon(get_theme_icon(SNAME("CenterView"), SNAME("EditorIcons")));
- button_pixel_snap->set_icon(get_theme_icon(SNAME("Snap"), SNAME("EditorIcons")));
- button_advanced_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
- PopupMenu *p = button_advanced_menu->get_popup();
- p->set_item_icon(p->get_item_index(ROTATE_RIGHT), get_theme_icon(SNAME("RotateRight"), SNAME("EditorIcons")));
- p->set_item_icon(p->get_item_index(ROTATE_LEFT), get_theme_icon(SNAME("RotateLeft"), SNAME("EditorIcons")));
- p->set_item_icon(p->get_item_index(FLIP_HORIZONTALLY), get_theme_icon(SNAME("MirrorX"), SNAME("EditorIcons")));
- p->set_item_icon(p->get_item_index(FLIP_VERTICALLY), get_theme_icon(SNAME("MirrorY"), SNAME("EditorIcons")));
- } break;
- }
- }
- void GenericTilePolygonEditor::_bind_methods() {
- ClassDB::bind_method(D_METHOD("get_polygon_count"), &GenericTilePolygonEditor::get_polygon_count);
- ClassDB::bind_method(D_METHOD("add_polygon", "polygon", "index"), &GenericTilePolygonEditor::add_polygon, DEFVAL(-1));
- ClassDB::bind_method(D_METHOD("remove_polygon", "index"), &GenericTilePolygonEditor::remove_polygon);
- ClassDB::bind_method(D_METHOD("clear_polygons"), &GenericTilePolygonEditor::clear_polygons);
- ClassDB::bind_method(D_METHOD("set_polygon", "index", "polygon"), &GenericTilePolygonEditor::set_polygon);
- ClassDB::bind_method(D_METHOD("get_polygon", "index"), &GenericTilePolygonEditor::set_polygon);
- ADD_SIGNAL(MethodInfo("polygons_changed"));
- }
- GenericTilePolygonEditor::GenericTilePolygonEditor() {
- editor_undo_redo = EditorNode::get_undo_redo();
- toolbar = memnew(HBoxContainer);
- add_child(toolbar);
- tools_button_group.instantiate();
- button_create = memnew(Button);
- button_create->set_flat(true);
- button_create->set_toggle_mode(true);
- button_create->set_button_group(tools_button_group);
- button_create->set_pressed(true);
- button_create->set_tooltip_text(TTR("Add polygon tool"));
- toolbar->add_child(button_create);
- button_edit = memnew(Button);
- button_edit->set_flat(true);
- button_edit->set_toggle_mode(true);
- button_edit->set_button_group(tools_button_group);
- button_edit->set_tooltip_text(TTR("Edit points tool"));
- toolbar->add_child(button_edit);
- button_delete = memnew(Button);
- button_delete->set_flat(true);
- button_delete->set_toggle_mode(true);
- button_delete->set_button_group(tools_button_group);
- button_delete->set_tooltip_text(TTR("Delete points tool"));
- toolbar->add_child(button_delete);
- button_advanced_menu = memnew(MenuButton);
- button_advanced_menu->set_flat(true);
- button_advanced_menu->set_toggle_mode(true);
- button_advanced_menu->get_popup()->add_item(TTR("Reset to default tile shape"), RESET_TO_DEFAULT_TILE, Key::F);
- button_advanced_menu->get_popup()->add_item(TTR("Clear"), CLEAR_TILE, Key::C);
- button_advanced_menu->get_popup()->add_separator();
- button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("RotateRight"), SNAME("EditorIcons")), TTR("Rotate Right"), ROTATE_RIGHT, Key::R);
- button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("RotateLeft"), SNAME("EditorIcons")), TTR("Rotate Left"), ROTATE_LEFT, Key::E);
- button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("MirrorX"), SNAME("EditorIcons")), TTR("Flip Horizontally"), FLIP_HORIZONTALLY, Key::H);
- button_advanced_menu->get_popup()->add_icon_item(get_theme_icon(SNAME("MirrorY"), SNAME("EditorIcons")), TTR("Flip Vertically"), FLIP_VERTICALLY, Key::V);
- button_advanced_menu->get_popup()->connect("id_pressed", callable_mp(this, &GenericTilePolygonEditor::_advanced_menu_item_pressed));
- button_advanced_menu->set_focus_mode(FOCUS_ALL);
- toolbar->add_child(button_advanced_menu);
- toolbar->add_child(memnew(VSeparator));
- button_pixel_snap = memnew(Button);
- button_pixel_snap->set_flat(true);
- button_pixel_snap->set_toggle_mode(true);
- button_pixel_snap->set_pressed(true);
- button_pixel_snap->set_tooltip_text(TTR("Snap to half-pixel"));
- toolbar->add_child(button_pixel_snap);
- Control *root = memnew(Control);
- root->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- root->set_custom_minimum_size(Size2(0, 200 * EDSCALE));
- root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
- add_child(root);
- panel = memnew(Panel);
- panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
- panel->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
- root->add_child(panel);
- base_control = memnew(Control);
- base_control->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
- base_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
- base_control->connect("draw", callable_mp(this, &GenericTilePolygonEditor::_base_control_draw));
- base_control->connect("gui_input", callable_mp(this, &GenericTilePolygonEditor::_base_control_gui_input));
- base_control->set_clip_contents(true);
- base_control->set_focus_mode(Control::FOCUS_CLICK);
- root->add_child(base_control);
- editor_zoom_widget = memnew(EditorZoomWidget);
- editor_zoom_widget->set_position(Vector2(5, 5));
- editor_zoom_widget->connect("zoom_changed", callable_mp(this, &GenericTilePolygonEditor::_zoom_changed).unbind(1));
- editor_zoom_widget->set_shortcut_context(this);
- root->add_child(editor_zoom_widget);
- button_center_view = memnew(Button);
- button_center_view->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("CenterView"), SNAME("EditorIcons")));
- button_center_view->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT, Control::PRESET_MODE_MINSIZE, 5);
- button_center_view->connect("pressed", callable_mp(this, &GenericTilePolygonEditor::_center_view));
- button_center_view->set_flat(true);
- button_center_view->set_disabled(true);
- root->add_child(button_center_view);
- }
- void TileDataDefaultEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
- ERR_FAIL_COND(!dummy_object);
- dummy_object->set(p_property, p_value);
- }
- Variant TileDataDefaultEditor::_get_painted_value() {
- ERR_FAIL_COND_V(!dummy_object, Variant());
- return dummy_object->get(property);
- }
- void TileDataDefaultEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Variant value = tile_data->get(property);
- dummy_object->set(property, value);
- if (property_editor) {
- property_editor->update_property();
- }
- }
- void TileDataDefaultEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- tile_data->set(property, p_value);
- }
- Variant TileDataDefaultEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND_V(!tile_data, Variant());
- return tile_data->get(property);
- }
- void TileDataDefaultEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
- for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
- Vector2i coords = E.key.get_atlas_coords();
- 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);
- 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);
- }
- }
- void TileDataDefaultEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
- if (drag_type == DRAG_TYPE_PAINT_RECT) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- p_canvas_item->draw_set_transform_matrix(p_transform);
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- 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())));
- rect = rect.abs();
- RBSet<TileMapCell> edited;
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- edited.insert(cell);
- }
- }
- }
- for (const TileMapCell &E : edited) {
- Vector2i coords = E.get_atlas_coords();
- p_canvas_item->draw_rect(p_tile_set_atlas_source->get_tile_texture_region(coords), selection_color, false);
- }
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- }
- };
- void TileDataDefaultEditor::forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform){
- };
- void TileDataDefaultEditor::forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (drag_type == DRAG_TYPE_PAINT) {
- 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()));
- for (int i = 0; i < line.size(); i++) {
- Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- if (!drag_modified.has(cell)) {
- drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
- }
- _set_value(p_tile_set_atlas_source, coords, 0, drag_painted_value);
- }
- }
- drag_last_pos = mm->get_position();
- }
- }
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::LEFT) {
- if (mb->is_pressed()) {
- if (picker_button->is_pressed()) {
- Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- _set_painted_value(p_tile_set_atlas_source, coords, 0);
- picker_button->set_pressed(false);
- }
- } else if (mb->is_ctrl_pressed()) {
- drag_type = DRAG_TYPE_PAINT_RECT;
- drag_modified.clear();
- drag_painted_value = _get_painted_value();
- drag_start_pos = mb->get_position();
- } else {
- drag_type = DRAG_TYPE_PAINT;
- drag_modified.clear();
- drag_painted_value = _get_painted_value();
- Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
- _set_value(p_tile_set_atlas_source, coords, 0, drag_painted_value);
- }
- drag_last_pos = mb->get_position();
- }
- } else {
- if (drag_type == DRAG_TYPE_PAINT_RECT) {
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
- rect = rect.abs();
- drag_modified.clear();
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, 0);
- }
- }
- }
- undo_redo->create_action(TTR("Painting Tiles Property"));
- _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
- undo_redo->commit_action(true);
- drag_type = DRAG_TYPE_NONE;
- } else if (drag_type == DRAG_TYPE_PAINT) {
- undo_redo->create_action(TTR("Painting Tiles Property"));
- _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- }
- }
- }
- }
- }
- void TileDataDefaultEditor::forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (drag_type == DRAG_TYPE_PAINT) {
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- if (!drag_modified.has(cell)) {
- drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, alternative_tile);
- }
- _set_value(p_tile_set_atlas_source, coords, alternative_tile, drag_painted_value);
- }
- drag_last_pos = mm->get_position();
- }
- }
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::LEFT) {
- if (mb->is_pressed()) {
- if (picker_button->is_pressed()) {
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- _set_painted_value(p_tile_set_atlas_source, coords, alternative_tile);
- picker_button->set_pressed(false);
- }
- } else {
- drag_type = DRAG_TYPE_PAINT;
- drag_modified.clear();
- drag_painted_value = _get_painted_value();
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- drag_modified[cell] = _get_value(p_tile_set_atlas_source, coords, alternative_tile);
- _set_value(p_tile_set_atlas_source, coords, alternative_tile, drag_painted_value);
- }
- drag_last_pos = mb->get_position();
- }
- } else {
- undo_redo->create_action(TTR("Painting Tiles Property"));
- _setup_undo_redo_action(p_tile_set_atlas_source, drag_modified, drag_painted_value);
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- }
- }
- }
- }
- void TileDataDefaultEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- bool valid;
- Variant value = tile_data->get(property, &valid);
- if (!valid) {
- return;
- }
- if (value.get_type() == Variant::BOOL) {
- Ref<Texture2D> texture = (bool)value ? tile_bool_checked : tile_bool_unchecked;
- int size = MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 3;
- Rect2 rect = p_transform.xform(Rect2(Vector2(-size / 2, -size / 2), Vector2(size, size)));
- p_canvas_item->draw_texture_rect(texture, rect);
- } else if (value.get_type() == Variant::COLOR) {
- int size = MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 3;
- Rect2 rect = p_transform.xform(Rect2(Vector2(-size / 2, -size / 2), Vector2(size, size)));
- p_canvas_item->draw_rect(rect, value);
- } else {
- Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
- int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
- String text;
- switch (value.get_type()) {
- case Variant::INT:
- text = vformat("%d", value);
- break;
- case Variant::FLOAT:
- text = vformat("%.2f", value);
- break;
- case Variant::STRING:
- case Variant::STRING_NAME:
- text = value;
- break;
- default:
- return;
- break;
- }
- Color color = Color(1, 1, 1);
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- selection_color.set_v(0.9);
- color = selection_color;
- } else if (is_visible_in_tree()) {
- Variant painted_value = _get_painted_value();
- 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;
- if (equal) {
- color = Color(0.7, 0.7, 0.7);
- }
- }
- Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- 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));
- 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);
- }
- }
- void TileDataDefaultEditor::setup_property_editor(Variant::Type p_type, String p_property, String p_label, Variant p_default_value) {
- ERR_FAIL_COND_MSG(!property.is_empty(), "Cannot setup TileDataDefaultEditor twice");
- property = p_property;
- // Update everything.
- if (property_editor) {
- property_editor->queue_delete();
- }
- // Update the dummy object.
- dummy_object->add_dummy_property(p_property);
- // Get the default value for the type.
- if (p_default_value == Variant()) {
- Callable::CallError error;
- Variant painted_value;
- Variant::construct(p_type, painted_value, nullptr, 0, error);
- dummy_object->set(p_property, painted_value);
- } else {
- dummy_object->set(p_property, p_default_value);
- }
- // Create and setup the property editor.
- property_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, p_type, p_property, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
- property_editor->set_object_and_property(dummy_object, p_property);
- if (p_label.is_empty()) {
- property_editor->set_label(EditorPropertyNameProcessor::get_singleton()->process_name(p_property, EditorPropertyNameProcessor::get_default_inspector_style()));
- } else {
- property_editor->set_label(p_label);
- }
- property_editor->connect("property_changed", callable_mp(this, &TileDataDefaultEditor::_property_value_changed).unbind(1));
- property_editor->set_tooltip_text(p_property);
- property_editor->update_property();
- add_child(property_editor);
- }
- void TileDataDefaultEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE:
- case NOTIFICATION_THEME_CHANGED: {
- picker_button->set_icon(get_theme_icon(SNAME("ColorPick"), SNAME("EditorIcons")));
- tile_bool_checked = get_theme_icon(SNAME("TileChecked"), SNAME("EditorIcons"));
- tile_bool_unchecked = get_theme_icon(SNAME("TileUnchecked"), SNAME("EditorIcons"));
- } break;
- }
- }
- TileDataDefaultEditor::TileDataDefaultEditor() {
- undo_redo = EditorNode::get_undo_redo();
- label = memnew(Label);
- label->set_text(TTR("Painting:"));
- label->set_theme_type_variation("HeaderSmall");
- add_child(label);
- toolbar->add_child(memnew(VSeparator));
- picker_button = memnew(Button);
- picker_button->set_flat(true);
- picker_button->set_toggle_mode(true);
- picker_button->set_shortcut(ED_SHORTCUT("tiles_editor/picker", "Picker", Key::P));
- toolbar->add_child(picker_button);
- }
- TileDataDefaultEditor::~TileDataDefaultEditor() {
- toolbar->queue_delete();
- memdelete(dummy_object);
- }
- void TileDataTextureOffsetEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- Vector2i tile_set_tile_size = tile_set->get_tile_size();
- Color color = Color(1.0, 0.0, 0.0);
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- color = selection_color;
- }
- Transform2D tile_xform;
- tile_xform.set_scale(tile_set_tile_size);
- tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, color);
- }
- void TileDataPositionEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- bool valid;
- Variant value = tile_data->get(property, &valid);
- if (!valid) {
- return;
- }
- ERR_FAIL_COND(value.get_type() != Variant::VECTOR2I && value.get_type() != Variant::VECTOR2);
- Color color = Color(1.0, 1.0, 1.0);
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- color = selection_color;
- }
- Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
- p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(value)) - position_icon->get_size() / 2, color);
- }
- void TileDataYSortEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- Color color = Color(1.0, 1.0, 1.0);
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- color = selection_color;
- }
- Ref<Texture2D> position_icon = TileSetEditor::get_singleton()->get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
- p_canvas_item->draw_texture(position_icon, p_transform.xform(Vector2(0, tile_data->get_y_sort_origin())) - position_icon->get_size() / 2, color);
- }
- void TileDataOcclusionShapeEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- Color color = grid_color.darkened(0.2);
- if (p_selected) {
- color = selection_color.darkened(0.2);
- }
- color.a *= 0.5;
- Vector<Color> debug_occlusion_color;
- debug_occlusion_color.push_back(color);
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
- Ref<OccluderPolygon2D> occluder = tile_data->get_occluder(occlusion_layer);
- if (occluder.is_valid() && occluder->get_polygon().size() >= 3) {
- p_canvas_item->draw_polygon(Variant(occluder->get_polygon()), debug_occlusion_color);
- }
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
- }
- Variant TileDataOcclusionShapeEditor::_get_painted_value() {
- Ref<OccluderPolygon2D> occluder_polygon;
- occluder_polygon.instantiate();
- if (polygon_editor->get_polygon_count() >= 1) {
- occluder_polygon->set_polygon(polygon_editor->get_polygon(0));
- }
- return occluder_polygon;
- }
- void TileDataOcclusionShapeEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Ref<OccluderPolygon2D> occluder_polygon = tile_data->get_occluder(occlusion_layer);
- polygon_editor->clear_polygons();
- if (occluder_polygon.is_valid()) {
- polygon_editor->add_polygon(occluder_polygon->get_polygon());
- }
- 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());
- }
- void TileDataOcclusionShapeEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Ref<OccluderPolygon2D> occluder_polygon = p_value;
- tile_data->set_occluder(occlusion_layer, occluder_polygon);
- 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());
- }
- Variant TileDataOcclusionShapeEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND_V(!tile_data, Variant());
- return tile_data->get_occluder(occlusion_layer);
- }
- void TileDataOcclusionShapeEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
- for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
- Vector2i coords = E.key.get_atlas_coords();
- 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);
- 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);
- }
- }
- void TileDataOcclusionShapeEditor::_tile_set_changed() {
- polygon_editor->set_tile_set(tile_set);
- }
- void TileDataOcclusionShapeEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE: {
- polygon_editor->set_polygons_color(get_tree()->get_debug_collisions_color());
- } break;
- }
- }
- TileDataOcclusionShapeEditor::TileDataOcclusionShapeEditor() {
- undo_redo = EditorNode::get_undo_redo();
- polygon_editor = memnew(GenericTilePolygonEditor);
- add_child(polygon_editor);
- }
- void TileDataCollisionEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
- dummy_object->set(p_property, p_value);
- }
- void TileDataCollisionEditor::_property_selected(StringName p_path, int p_focusable) {
- // Deselect all other properties
- for (KeyValue<StringName, EditorProperty *> &editor : property_editors) {
- if (editor.key != p_path) {
- editor.value->deselect();
- }
- }
- }
- void TileDataCollisionEditor::_polygons_changed() {
- // Update the dummy object properties and their editors.
- for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
- StringName one_way_property = vformat("polygon_%d_one_way", i);
- StringName one_way_margin_property = vformat("polygon_%d_one_way_margin", i);
- if (!dummy_object->has_dummy_property(one_way_property)) {
- dummy_object->add_dummy_property(one_way_property);
- dummy_object->set(one_way_property, false);
- }
- if (!dummy_object->has_dummy_property(one_way_margin_property)) {
- dummy_object->add_dummy_property(one_way_margin_property);
- dummy_object->set(one_way_margin_property, 1.0);
- }
- if (!property_editors.has(one_way_property)) {
- EditorProperty *one_way_property_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::BOOL, one_way_property, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
- one_way_property_editor->set_object_and_property(dummy_object, one_way_property);
- one_way_property_editor->set_label(one_way_property);
- one_way_property_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
- one_way_property_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
- one_way_property_editor->set_tooltip_text(one_way_property_editor->get_edited_property());
- one_way_property_editor->update_property();
- add_child(one_way_property_editor);
- property_editors[one_way_property] = one_way_property_editor;
- }
- if (!property_editors.has(one_way_margin_property)) {
- 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);
- one_way_margin_property_editor->set_object_and_property(dummy_object, one_way_margin_property);
- one_way_margin_property_editor->set_label(one_way_margin_property);
- one_way_margin_property_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
- one_way_margin_property_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
- one_way_margin_property_editor->set_tooltip_text(one_way_margin_property_editor->get_edited_property());
- one_way_margin_property_editor->update_property();
- add_child(one_way_margin_property_editor);
- property_editors[one_way_margin_property] = one_way_margin_property_editor;
- }
- }
- // Remove unneeded properties and their editors.
- for (int i = polygon_editor->get_polygon_count(); dummy_object->has_dummy_property(vformat("polygon_%d_one_way", i)); i++) {
- dummy_object->remove_dummy_property(vformat("polygon_%d_one_way", i));
- }
- for (int i = polygon_editor->get_polygon_count(); dummy_object->has_dummy_property(vformat("polygon_%d_one_way_margin", i)); i++) {
- dummy_object->remove_dummy_property(vformat("polygon_%d_one_way_margin", i));
- }
- for (int i = polygon_editor->get_polygon_count(); property_editors.has(vformat("polygon_%d_one_way", i)); i++) {
- property_editors[vformat("polygon_%d_one_way", i)]->queue_delete();
- property_editors.erase(vformat("polygon_%d_one_way", i));
- }
- for (int i = polygon_editor->get_polygon_count(); property_editors.has(vformat("polygon_%d_one_way_margin", i)); i++) {
- property_editors[vformat("polygon_%d_one_way_margin", i)]->queue_delete();
- property_editors.erase(vformat("polygon_%d_one_way_margin", i));
- }
- }
- Variant TileDataCollisionEditor::_get_painted_value() {
- Dictionary dict;
- dict["linear_velocity"] = dummy_object->get("linear_velocity");
- dict["angular_velocity"] = dummy_object->get("angular_velocity");
- Array array;
- for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
- ERR_FAIL_COND_V(polygon_editor->get_polygon(i).size() < 3, Variant());
- Dictionary polygon_dict;
- polygon_dict["points"] = polygon_editor->get_polygon(i);
- polygon_dict["one_way"] = dummy_object->get(vformat("polygon_%d_one_way", i));
- polygon_dict["one_way_margin"] = dummy_object->get(vformat("polygon_%d_one_way_margin", i));
- array.push_back(polygon_dict);
- }
- dict["polygons"] = array;
- return dict;
- }
- void TileDataCollisionEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- polygon_editor->clear_polygons();
- for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
- Vector<Vector2> polygon = tile_data->get_collision_polygon_points(physics_layer, i);
- if (polygon.size() >= 3) {
- polygon_editor->add_polygon(polygon);
- }
- }
- _polygons_changed();
- dummy_object->set("linear_velocity", tile_data->get_constant_linear_velocity(physics_layer));
- dummy_object->set("angular_velocity", tile_data->get_constant_angular_velocity(physics_layer));
- for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
- dummy_object->set(vformat("polygon_%d_one_way", i), tile_data->is_collision_polygon_one_way(physics_layer, i));
- dummy_object->set(vformat("polygon_%d_one_way_margin", i), tile_data->get_collision_polygon_one_way_margin(physics_layer, i));
- }
- for (const KeyValue<StringName, EditorProperty *> &E : property_editors) {
- E.value->update_property();
- }
- 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());
- }
- void TileDataCollisionEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Dictionary dict = p_value;
- tile_data->set_constant_linear_velocity(physics_layer, dict["linear_velocity"]);
- tile_data->set_constant_angular_velocity(physics_layer, dict["angular_velocity"]);
- Array array = dict["polygons"];
- tile_data->set_collision_polygons_count(physics_layer, array.size());
- for (int i = 0; i < array.size(); i++) {
- Dictionary polygon_dict = array[i];
- tile_data->set_collision_polygon_points(physics_layer, i, polygon_dict["points"]);
- tile_data->set_collision_polygon_one_way(physics_layer, i, polygon_dict["one_way"]);
- tile_data->set_collision_polygon_one_way_margin(physics_layer, i, polygon_dict["one_way_margin"]);
- }
- 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());
- }
- Variant TileDataCollisionEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND_V(!tile_data, Variant());
- Dictionary dict;
- dict["linear_velocity"] = tile_data->get_constant_linear_velocity(physics_layer);
- dict["angular_velocity"] = tile_data->get_constant_angular_velocity(physics_layer);
- Array array;
- for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
- Dictionary polygon_dict;
- polygon_dict["points"] = tile_data->get_collision_polygon_points(physics_layer, i);
- polygon_dict["one_way"] = tile_data->is_collision_polygon_one_way(physics_layer, i);
- polygon_dict["one_way_margin"] = tile_data->get_collision_polygon_one_way_margin(physics_layer, i);
- array.push_back(polygon_dict);
- }
- dict["polygons"] = array;
- return dict;
- }
- void TileDataCollisionEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
- Array new_array = p_new_value;
- for (KeyValue<TileMapCell, Variant> &E : p_previous_values) {
- Array old_array = E.value;
- Vector2i coords = E.key.get_atlas_coords();
- 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());
- for (int i = 0; i < old_array.size(); i++) {
- Dictionary dict = old_array[i];
- 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"]);
- 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"]);
- 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"]);
- }
- 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());
- for (int i = 0; i < new_array.size(); i++) {
- Dictionary dict = new_array[i];
- 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"]);
- 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"]);
- 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"]);
- }
- }
- }
- void TileDataCollisionEditor::_tile_set_changed() {
- polygon_editor->set_tile_set(tile_set);
- _polygons_changed();
- }
- void TileDataCollisionEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE: {
- polygon_editor->set_polygons_color(get_tree()->get_debug_collisions_color());
- } break;
- }
- }
- TileDataCollisionEditor::TileDataCollisionEditor() {
- undo_redo = EditorNode::get_undo_redo();
- polygon_editor = memnew(GenericTilePolygonEditor);
- polygon_editor->set_multiple_polygon_mode(true);
- polygon_editor->connect("polygons_changed", callable_mp(this, &TileDataCollisionEditor::_polygons_changed));
- add_child(polygon_editor);
- dummy_object->add_dummy_property("linear_velocity");
- dummy_object->set("linear_velocity", Vector2());
- dummy_object->add_dummy_property("angular_velocity");
- dummy_object->set("angular_velocity", 0.0);
- EditorProperty *linear_velocity_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::VECTOR2, "linear_velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
- linear_velocity_editor->set_object_and_property(dummy_object, "linear_velocity");
- linear_velocity_editor->set_label("linear_velocity");
- linear_velocity_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
- linear_velocity_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
- linear_velocity_editor->set_tooltip_text(linear_velocity_editor->get_edited_property());
- linear_velocity_editor->update_property();
- add_child(linear_velocity_editor);
- property_editors["linear_velocity"] = linear_velocity_editor;
- EditorProperty *angular_velocity_editor = EditorInspectorDefaultPlugin::get_editor_for_property(dummy_object, Variant::FLOAT, "angular_velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT);
- angular_velocity_editor->set_object_and_property(dummy_object, "angular_velocity");
- angular_velocity_editor->set_label("angular_velocity");
- angular_velocity_editor->connect("property_changed", callable_mp(this, &TileDataCollisionEditor::_property_value_changed).unbind(1));
- angular_velocity_editor->connect("selected", callable_mp(this, &TileDataCollisionEditor::_property_selected));
- angular_velocity_editor->set_tooltip_text(angular_velocity_editor->get_edited_property());
- angular_velocity_editor->update_property();
- add_child(angular_velocity_editor);
- property_editors["angular_velocity"] = angular_velocity_editor;
- _polygons_changed();
- }
- TileDataCollisionEditor::~TileDataCollisionEditor() {
- memdelete(dummy_object);
- }
- void TileDataCollisionEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- // Draw all shapes.
- Vector<Color> color;
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- selection_color.a = 0.7;
- color.push_back(selection_color);
- } else {
- Color debug_collision_color = p_canvas_item->get_tree()->get_debug_collisions_color();
- color.push_back(debug_collision_color);
- }
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
- for (int i = 0; i < tile_data->get_collision_polygons_count(physics_layer); i++) {
- Vector<Vector2> polygon = tile_data->get_collision_polygon_points(physics_layer, i);
- if (polygon.size() >= 3) {
- p_canvas_item->draw_polygon(polygon, color);
- }
- }
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
- }
- void TileDataTerrainsEditor::_update_terrain_selector() {
- ERR_FAIL_COND(!tile_set.is_valid());
- // Update the terrain set selector.
- Vector<String> options;
- options.push_back(String(TTR("No terrains")) + String(":-1"));
- for (int i = 0; i < tile_set->get_terrain_sets_count(); i++) {
- options.push_back(vformat("Terrain Set %d", i));
- }
- terrain_set_property_editor->setup(options);
- terrain_set_property_editor->update_property();
- // Update the terrain selector.
- int terrain_set = int(dummy_object->get("terrain_set"));
- if (terrain_set == -1) {
- terrain_property_editor->hide();
- } else {
- options.clear();
- Vector<Vector<Ref<Texture2D>>> icons = tile_set->generate_terrains_icons(Size2(16, 16) * EDSCALE);
- options.push_back(String(TTR("No terrain")) + String(":-1"));
- for (int i = 0; i < tile_set->get_terrains_count(terrain_set); i++) {
- String name = tile_set->get_terrain_name(terrain_set, i);
- if (name.is_empty()) {
- options.push_back(vformat("Terrain %d", i));
- } else {
- options.push_back(name);
- }
- }
- terrain_property_editor->setup(options);
- terrain_property_editor->update_property();
- // Kind of a hack to set icons.
- // We could provide a way to modify that in the EditorProperty.
- OptionButton *option_button = Object::cast_to<OptionButton>(terrain_property_editor->get_child(0));
- for (int terrain = 0; terrain < tile_set->get_terrains_count(terrain_set); terrain++) {
- option_button->set_item_icon(terrain + 1, icons[terrain_set][terrain]);
- }
- terrain_property_editor->show();
- }
- }
- void TileDataTerrainsEditor::_property_value_changed(StringName p_property, Variant p_value, StringName p_field) {
- Variant old_value = dummy_object->get(p_property);
- dummy_object->set(p_property, p_value);
- if (p_property == "terrain_set") {
- if (p_value != old_value) {
- dummy_object->set("terrain", -1);
- }
- _update_terrain_selector();
- }
- emit_signal(SNAME("needs_redraw"));
- }
- void TileDataTerrainsEditor::_tile_set_changed() {
- ERR_FAIL_COND(!tile_set.is_valid());
- // Fix if wrong values are selected.
- int terrain_set = int(dummy_object->get("terrain_set"));
- if (terrain_set >= tile_set->get_terrain_sets_count()) {
- terrain_set = -1;
- dummy_object->set("terrain_set", -1);
- }
- if (terrain_set >= 0) {
- if (int(dummy_object->get("terrain")) >= tile_set->get_terrains_count(terrain_set)) {
- dummy_object->set("terrain", -1);
- }
- }
- _update_terrain_selector();
- }
- void TileDataTerrainsEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
- ERR_FAIL_COND(!tile_set.is_valid());
- // Draw the hovered terrain bit, or the whole tile if it has the wrong terrain set.
- Vector2i hovered_coords = TileSetSource::INVALID_ATLAS_COORDS;
- if (drag_type == DRAG_TYPE_NONE) {
- Vector2i mouse_pos = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
- hovered_coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_pos);
- hovered_coords = p_tile_set_atlas_source->get_tile_at_coords(hovered_coords);
- if (hovered_coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(hovered_coords, 0);
- int terrain_set = tile_data->get_terrain_set();
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(hovered_coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(hovered_coords, 0);
- if (terrain_set >= 0 && terrain_set == int(dummy_object->get("terrain_set"))) {
- // Draw hovered bit.
- Transform2D xform;
- xform.set_origin(position);
- Vector<Color> color;
- color.push_back(Color(1.0, 1.0, 1.0, 0.5));
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
- p_canvas_item->draw_set_transform_matrix(p_transform * xform);
- p_canvas_item->draw_polygon(polygon, color);
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
- p_canvas_item->draw_set_transform_matrix(p_transform * xform);
- p_canvas_item->draw_polygon(polygon, color);
- }
- }
- }
- } else {
- // Draw hovered tile.
- Transform2D tile_xform;
- tile_xform.set_origin(position);
- tile_xform.set_scale(tile_set->get_tile_size());
- tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
- }
- }
- }
- // Dim terrains with wrong terrain set.
- Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
- int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
- for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) {
- Vector2i coords = p_tile_set_atlas_source->get_tile_id(i);
- if (coords != hovered_coords) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- if (tile_data->get_terrain_set() != int(dummy_object->get("terrain_set"))) {
- // Dimming
- p_canvas_item->draw_set_transform_matrix(p_transform);
- Rect2i rect = p_tile_set_atlas_source->get_tile_texture_region(coords);
- p_canvas_item->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.3));
- // Text
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Color color = Color(1, 1, 1);
- String text;
- if (tile_data->get_terrain_set() >= 0) {
- text = vformat("%d", tile_data->get_terrain_set());
- } else {
- text = "-";
- }
- Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- 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));
- 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);
- }
- }
- }
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET_RECT) {
- // Draw selection rectangle.
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- p_canvas_item->draw_set_transform_matrix(p_transform);
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- 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())));
- rect = rect.abs();
- RBSet<TileMapCell> edited;
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- edited.insert(cell);
- }
- }
- }
- for (const TileMapCell &E : edited) {
- Vector2i coords = E.get_atlas_coords();
- p_canvas_item->draw_rect(p_tile_set_atlas_source->get_tile_texture_region(coords), selection_color, false);
- }
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS_RECT) {
- // Highlight selected peering bits.
- Dictionary painted = Dictionary(drag_painted_value);
- int terrain_set = int(painted["terrain_set"]);
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- 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())));
- rect = rect.abs();
- RBSet<TileMapCell> edited;
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- if (tile_data->get_terrain_set() == terrain_set) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- edited.insert(cell);
- }
- }
- }
- }
- Vector2 end = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
- Vector<Point2> mouse_pos_rect_polygon;
- mouse_pos_rect_polygon.push_back(drag_start_pos);
- mouse_pos_rect_polygon.push_back(Vector2(end.x, drag_start_pos.y));
- mouse_pos_rect_polygon.push_back(end);
- mouse_pos_rect_polygon.push_back(Vector2(drag_start_pos.x, end.y));
- Vector<Color> color;
- color.push_back(Color(1.0, 1.0, 1.0, 0.5));
- p_canvas_item->draw_set_transform_matrix(p_transform);
- for (const TileMapCell &E : edited) {
- Vector2i coords = E.get_atlas_coords();
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- for (int j = 0; j < polygon.size(); j++) {
- polygon.write[j] += position;
- }
- if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
- // Draw terrain.
- p_canvas_item->draw_polygon(polygon, color);
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- for (int j = 0; j < polygon.size(); j++) {
- polygon.write[j] += position;
- }
- if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
- // Draw bit.
- p_canvas_item->draw_polygon(polygon, color);
- }
- }
- }
- }
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- }
- }
- void TileDataTerrainsEditor::forward_draw_over_alternatives(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, CanvasItem *p_canvas_item, Transform2D p_transform) {
- ERR_FAIL_COND(!tile_set.is_valid());
- // Draw the hovered terrain bit, or the whole tile if it has the wrong terrain set.
- Vector2i hovered_coords = TileSetSource::INVALID_ATLAS_COORDS;
- int hovered_alternative = TileSetSource::INVALID_TILE_ALTERNATIVE;
- if (drag_type == DRAG_TYPE_NONE) {
- Vector2i mouse_pos = p_transform.affine_inverse().xform(p_canvas_item->get_local_mouse_position());
- Vector3i hovered = p_tile_atlas_view->get_alternative_tile_at_pos(mouse_pos);
- hovered_coords = Vector2i(hovered.x, hovered.y);
- hovered_alternative = hovered.z;
- if (hovered_coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(hovered_coords, hovered_alternative);
- int terrain_set = tile_data->get_terrain_set();
- Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(hovered_coords, hovered_alternative);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(hovered_coords, hovered_alternative);
- if (terrain_set == int(dummy_object->get("terrain_set"))) {
- // Draw hovered bit.
- Transform2D xform;
- xform.set_origin(position);
- Vector<Color> color;
- color.push_back(Color(1.0, 1.0, 1.0, 0.5));
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
- p_canvas_item->draw_set_transform_matrix(p_transform * xform);
- p_canvas_item->draw_polygon(polygon, color);
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(xform.affine_inverse().xform(mouse_pos), polygon)) {
- p_canvas_item->draw_set_transform_matrix(p_transform * xform);
- p_canvas_item->draw_polygon(polygon, color);
- }
- }
- }
- } else {
- // Draw hovered tile.
- Transform2D tile_xform;
- tile_xform.set_origin(position);
- tile_xform.set_scale(tile_set->get_tile_size());
- tile_set->draw_tile_shape(p_canvas_item, p_transform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
- }
- }
- }
- // Dim terrains with wrong terrain set.
- Ref<Font> font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
- int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
- for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) {
- Vector2i coords = p_tile_set_atlas_source->get_tile_id(i);
- for (int j = 1; j < p_tile_set_atlas_source->get_alternative_tiles_count(coords); j++) {
- int alternative_tile = p_tile_set_atlas_source->get_alternative_tile_id(coords, j);
- if (coords != hovered_coords || alternative_tile != hovered_alternative) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
- if (tile_data->get_terrain_set() != int(dummy_object->get("terrain_set"))) {
- // Dimming
- p_canvas_item->draw_set_transform_matrix(p_transform);
- Rect2i rect = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
- p_canvas_item->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.3));
- // Text
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Color color = Color(1, 1, 1);
- String text;
- if (tile_data->get_terrain_set() >= 0) {
- text = vformat("%d", tile_data->get_terrain_set());
- } else {
- text = "-";
- }
- Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- 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));
- 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);
- }
- }
- }
- }
- p_canvas_item->draw_set_transform_matrix(Transform2D());
- }
- void TileDataTerrainsEditor::forward_painting_atlas_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
- 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()));
- for (int i = 0; i < line.size(); i++) {
- Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- int terrain_set = drag_painted_value;
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- // Save the old terrain_set and terrains bits.
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- if (!drag_modified.has(cell)) {
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- }
- // Set the terrain_set.
- tile_data->set_terrain_set(terrain_set);
- }
- }
- drag_last_pos = mm->get_position();
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
- int terrain_set = Dictionary(drag_painted_value)["terrain_set"];
- int terrain = Dictionary(drag_painted_value)["terrain"];
- 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()));
- for (int i = 0; i < line.size(); i++) {
- Vector2i coords = p_tile_set_atlas_source->get_tile_at_coords(line[i]);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- if (tile_data->get_terrain_set() == terrain_set) {
- // Save the old terrain_set and terrains bits.
- if (!drag_modified.has(cell)) {
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- }
- // Set the terrains bits.
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(tile_data->get_terrain_set());
- if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
- tile_data->set_terrain(terrain);
- }
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- if (tile_data->is_valid_terrain_peering_bit(bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(tile_data->get_terrain_set(), bit);
- if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
- tile_data->set_terrain_peering_bit(bit, terrain);
- }
- }
- }
- }
- }
- }
- drag_last_pos = mm->get_position();
- accept_event();
- }
- }
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
- if (mb->is_pressed()) {
- if (mb->get_button_index() == MouseButton::LEFT && picker_button->is_pressed()) {
- Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- int terrain_set = tile_data->get_terrain_set();
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- dummy_object->set("terrain_set", terrain_set);
- dummy_object->set("terrain", -1);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- dummy_object->set("terrain", tile_data->get_terrain());
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- dummy_object->set("terrain", tile_data->get_terrain_peering_bit(bit));
- }
- }
- }
- terrain_set_property_editor->update_property();
- _update_terrain_selector();
- picker_button->set_pressed(false);
- accept_event();
- }
- } else {
- Vector2i coords = p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position());
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- TileData *tile_data = nullptr;
- if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
- tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- }
- int terrain_set = int(dummy_object->get("terrain_set"));
- int terrain = int(dummy_object->get("terrain"));
- if (terrain_set == -1 || !tile_data || tile_data->get_terrain_set() != terrain_set) {
- // Paint terrain sets.
- if (mb->get_button_index() == MouseButton::RIGHT) {
- terrain_set = -1;
- }
- if (mb->is_ctrl_pressed()) {
- // Paint terrain set with rect.
- drag_type = DRAG_TYPE_PAINT_TERRAIN_SET_RECT;
- drag_modified.clear();
- drag_painted_value = terrain_set;
- drag_start_pos = mb->get_position();
- } else {
- // Paint terrain set.
- drag_type = DRAG_TYPE_PAINT_TERRAIN_SET;
- drag_modified.clear();
- drag_painted_value = terrain_set;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- // Save the old terrain_set and terrains bits.
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- // Set the terrain_set.
- tile_data->set_terrain_set(terrain_set);
- }
- drag_last_pos = mb->get_position();
- }
- accept_event();
- } else if (tile_data->get_terrain_set() == terrain_set) {
- // Paint terrain bits.
- if (mb->get_button_index() == MouseButton::RIGHT) {
- terrain = -1;
- }
- if (mb->is_ctrl_pressed()) {
- // Paint terrain bits with rect.
- drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS_RECT;
- drag_modified.clear();
- Dictionary painted_dict;
- painted_dict["terrain_set"] = terrain_set;
- painted_dict["terrain"] = terrain;
- drag_painted_value = painted_dict;
- drag_start_pos = mb->get_position();
- } else {
- // Paint terrain bits.
- drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS;
- drag_modified.clear();
- Dictionary painted_dict;
- painted_dict["terrain_set"] = terrain_set;
- painted_dict["terrain"] = terrain;
- drag_painted_value = painted_dict;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- // Save the old terrain_set and terrains bits.
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- // Set the terrain bit.
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- tile_data->set_terrain(terrain);
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- tile_data->set_terrain_peering_bit(bit, terrain);
- }
- }
- }
- }
- drag_last_pos = mb->get_position();
- }
- accept_event();
- }
- }
- } else {
- if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET_RECT) {
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
- rect = rect.abs();
- RBSet<TileMapCell> edited;
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- edited.insert(cell);
- }
- }
- }
- undo_redo->create_action(TTR("Painting Terrain Set"));
- for (const TileMapCell &E : edited) {
- Vector2i coords = E.get_atlas_coords();
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- 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);
- 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());
- if (tile_data->get_terrain_set() >= 0) {
- 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());
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_data->is_valid_terrain_peering_bit(bit)) {
- 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));
- }
- }
- }
- }
- undo_redo->commit_action(true);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
- undo_redo->create_action(TTR("Painting Terrain Set"));
- for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
- Dictionary dict = E.value;
- Vector2i coords = E.key.get_atlas_coords();
- 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);
- 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"]);
- if (int(dict["terrain_set"]) >= 0) {
- 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"]);
- Array array = dict["terrain_peering_bits"];
- for (int i = 0; i < array.size(); i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
- 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]);
- }
- }
- }
- }
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
- Dictionary painted = Dictionary(drag_painted_value);
- int terrain_set = int(painted["terrain_set"]);
- int terrain = int(painted["terrain"]);
- undo_redo->create_action(TTR("Painting Terrain"));
- for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
- Dictionary dict = E.value;
- Vector2i coords = E.key.get_atlas_coords();
- undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), terrain);
- 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"]);
- Array array = dict["terrain_peering_bits"];
- for (int i = 0; i < array.size(); i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- 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);
- }
- if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
- 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]);
- }
- }
- }
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS_RECT) {
- Dictionary painted = Dictionary(drag_painted_value);
- int terrain_set = int(painted["terrain_set"]);
- int terrain = int(painted["terrain"]);
- Rect2i rect;
- rect.set_position(p_tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_pos));
- rect.set_end(p_tile_atlas_view->get_atlas_tile_coords_at_pos(mb->get_position()));
- rect = rect.abs();
- RBSet<TileMapCell> edited;
- for (int x = rect.get_position().x; x <= rect.get_end().x; x++) {
- for (int y = rect.get_position().y; y <= rect.get_end().y; y++) {
- Vector2i coords = Vector2i(x, y);
- coords = p_tile_set_atlas_source->get_tile_at_coords(coords);
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- if (tile_data->get_terrain_set() == terrain_set) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = 0;
- edited.insert(cell);
- }
- }
- }
- }
- Vector<Point2> mouse_pos_rect_polygon;
- mouse_pos_rect_polygon.push_back(drag_start_pos);
- mouse_pos_rect_polygon.push_back(Vector2(mb->get_position().x, drag_start_pos.y));
- mouse_pos_rect_polygon.push_back(mb->get_position());
- mouse_pos_rect_polygon.push_back(Vector2(drag_start_pos.x, mb->get_position().y));
- undo_redo->create_action(TTR("Painting Terrain"));
- for (const TileMapCell &E : edited) {
- Vector2i coords = E.get_atlas_coords();
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, 0);
- Rect2i texture_region = p_tile_set_atlas_source->get_tile_texture_region(coords);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- for (int j = 0; j < polygon.size(); j++) {
- polygon.write[j] += position;
- }
- if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
- // Draw terrain.
- undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.alternative_tile), terrain);
- 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());
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- for (int j = 0; j < polygon.size(); j++) {
- polygon.write[j] += position;
- }
- if (!Geometry2D::intersect_polygons(polygon, mouse_pos_rect_polygon).is_empty()) {
- // Draw bit.
- 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);
- 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));
- }
- }
- }
- }
- undo_redo->commit_action(true);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- }
- }
- }
- }
- }
- void TileDataTerrainsEditor::forward_painting_alternatives_gui_input(TileAtlasView *p_tile_atlas_view, TileSetAtlasSource *p_tile_set_atlas_source, const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
- if (!drag_modified.has(cell)) {
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- }
- tile_data->set_terrain_set(drag_painted_value);
- }
- drag_last_pos = mm->get_position();
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
- Dictionary painted = Dictionary(drag_painted_value);
- int terrain_set = int(painted["terrain_set"]);
- int terrain = int(painted["terrain"]);
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mm->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- // Save the old terrain_set and terrains bits.
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
- if (tile_data->get_terrain_set() == terrain_set) {
- if (!drag_modified.has(cell)) {
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- }
- // Set the terrains bits.
- Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(tile_data->get_terrain_set());
- if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
- tile_data->set_terrain(terrain);
- }
- for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
- if (tile_data->is_valid_terrain_peering_bit(bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(tile_data->get_terrain_set(), bit);
- if (Geometry2D::is_segment_intersecting_polygon(mm->get_position() - position, drag_last_pos - position, polygon)) {
- tile_data->set_terrain_peering_bit(bit, terrain);
- }
- }
- }
- }
- }
- drag_last_pos = mm->get_position();
- accept_event();
- }
- }
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
- if (mb->is_pressed()) {
- if (mb->get_button_index() == MouseButton::LEFT && picker_button->is_pressed()) {
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
- int terrain_set = tile_data->get_terrain_set();
- Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
- dummy_object->set("terrain_set", terrain_set);
- dummy_object->set("terrain", -1);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- dummy_object->set("terrain", tile_data->get_terrain());
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- dummy_object->set("terrain", tile_data->get_terrain_peering_bit(bit));
- }
- }
- }
- terrain_set_property_editor->update_property();
- _update_terrain_selector();
- picker_button->set_pressed(false);
- accept_event();
- }
- } else {
- int terrain_set = int(dummy_object->get("terrain_set"));
- int terrain = int(dummy_object->get("terrain"));
- Vector3i tile = p_tile_atlas_view->get_alternative_tile_at_pos(mb->get_position());
- Vector2i coords = Vector2i(tile.x, tile.y);
- int alternative_tile = tile.z;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(coords, alternative_tile);
- if (terrain_set == -1 || !tile_data || tile_data->get_terrain_set() != terrain_set) {
- // Paint terrain sets.
- if (mb->get_button_index() == MouseButton::RIGHT) {
- terrain_set = -1;
- }
- drag_type = DRAG_TYPE_PAINT_TERRAIN_SET;
- drag_modified.clear();
- drag_painted_value = int(dummy_object->get("terrain_set"));
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- Array array;
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- tile_data->set_terrain_set(drag_painted_value);
- }
- drag_last_pos = mb->get_position();
- accept_event();
- } else if (tile_data->get_terrain_set() == terrain_set) {
- // Paint terrain bits.
- if (mb->get_button_index() == MouseButton::RIGHT) {
- terrain = -1;
- }
- // Paint terrain bits.
- drag_type = DRAG_TYPE_PAINT_TERRAIN_BITS;
- drag_modified.clear();
- Dictionary painted_dict;
- painted_dict["terrain_set"] = terrain_set;
- painted_dict["terrain"] = terrain;
- drag_painted_value = painted_dict;
- if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
- TileMapCell cell;
- cell.source_id = 0;
- cell.set_atlas_coords(coords);
- cell.alternative_tile = alternative_tile;
- // Save the old terrain_set and terrains bits.
- Dictionary dict;
- dict["terrain_set"] = tile_data->get_terrain_set();
- dict["terrain"] = tile_data->get_terrain();
- Array array;
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- array.push_back(tile_data->is_valid_terrain_peering_bit(bit) ? tile_data->get_terrain_peering_bit(bit) : -1);
- }
- dict["terrain_peering_bits"] = array;
- drag_modified[cell] = dict;
- // Set the terrain bit.
- Rect2i texture_region = p_tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
- Vector2i position = texture_region.get_center() + p_tile_set_atlas_source->get_tile_effective_texture_offset(coords, alternative_tile);
- Vector<Vector2> polygon = tile_set->get_terrain_polygon(terrain_set);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- tile_data->set_terrain(terrain);
- }
- for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- polygon = tile_set->get_terrain_peering_bit_polygon(terrain_set, bit);
- if (Geometry2D::is_point_in_polygon(mb->get_position() - position, polygon)) {
- tile_data->set_terrain_peering_bit(bit, terrain);
- }
- }
- }
- }
- drag_last_pos = mb->get_position();
- accept_event();
- }
- }
- }
- } else {
- if (drag_type == DRAG_TYPE_PAINT_TERRAIN_SET) {
- undo_redo->create_action(TTR("Painting Tiles Property"));
- for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
- Dictionary dict = E.value;
- Vector2i coords = E.key.get_atlas_coords();
- 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);
- 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"]);
- if (int(dict["terrain_set"]) >= 0) {
- 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"]);
- Array array = dict["terrain_peering_bits"];
- for (int i = 0; i < array.size(); i++) {
- 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]);
- }
- }
- }
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- } else if (drag_type == DRAG_TYPE_PAINT_TERRAIN_BITS) {
- Dictionary painted = Dictionary(drag_painted_value);
- int terrain_set = int(painted["terrain_set"]);
- int terrain = int(painted["terrain"]);
- undo_redo->create_action(TTR("Painting Terrain"));
- for (KeyValue<TileMapCell, Variant> &E : drag_modified) {
- Dictionary dict = E.value;
- Vector2i coords = E.key.get_atlas_coords();
- undo_redo->add_do_property(p_tile_set_atlas_source, vformat("%d:%d/%d/terrain", coords.x, coords.y, E.key.alternative_tile), terrain);
- 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"]);
- Array array = dict["terrain_peering_bits"];
- for (int i = 0; i < array.size(); i++) {
- TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
- if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
- 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);
- }
- if (tile_set->is_valid_terrain_peering_bit(dict["terrain_set"], bit)) {
- 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]);
- }
- }
- }
- undo_redo->commit_action(false);
- drag_type = DRAG_TYPE_NONE;
- accept_event();
- }
- }
- }
- }
- }
- void TileDataTerrainsEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- tile_set->draw_terrains(p_canvas_item, p_transform, tile_data);
- }
- void TileDataTerrainsEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE:
- case NOTIFICATION_THEME_CHANGED: {
- picker_button->set_icon(get_theme_icon(SNAME("ColorPick"), SNAME("EditorIcons")));
- } break;
- }
- }
- TileDataTerrainsEditor::TileDataTerrainsEditor() {
- undo_redo = EditorNode::get_undo_redo();
- label = memnew(Label);
- label->set_text(TTR("Painting:"));
- label->set_theme_type_variation("HeaderSmall");
- add_child(label);
- // Toolbar
- toolbar->add_child(memnew(VSeparator));
- picker_button = memnew(Button);
- picker_button->set_flat(true);
- picker_button->set_toggle_mode(true);
- picker_button->set_shortcut(ED_SHORTCUT("tiles_editor/picker", "Picker", Key::P));
- toolbar->add_child(picker_button);
- // Setup
- dummy_object->add_dummy_property("terrain_set");
- dummy_object->set("terrain_set", -1);
- dummy_object->add_dummy_property("terrain");
- dummy_object->set("terrain", -1);
- // Get the default value for the type.
- terrain_set_property_editor = memnew(EditorPropertyEnum);
- terrain_set_property_editor->set_object_and_property(dummy_object, "terrain_set");
- terrain_set_property_editor->set_label("Terrain Set");
- terrain_set_property_editor->connect("property_changed", callable_mp(this, &TileDataTerrainsEditor::_property_value_changed).unbind(1));
- terrain_set_property_editor->set_tooltip_text(terrain_set_property_editor->get_edited_property());
- add_child(terrain_set_property_editor);
- terrain_property_editor = memnew(EditorPropertyEnum);
- terrain_property_editor->set_object_and_property(dummy_object, "terrain");
- terrain_property_editor->set_label("Terrain");
- terrain_property_editor->connect("property_changed", callable_mp(this, &TileDataTerrainsEditor::_property_value_changed).unbind(1));
- add_child(terrain_property_editor);
- }
- TileDataTerrainsEditor::~TileDataTerrainsEditor() {
- toolbar->queue_delete();
- memdelete(dummy_object);
- }
- Variant TileDataNavigationEditor::_get_painted_value() {
- Ref<NavigationPolygon> nav_polygon;
- nav_polygon.instantiate();
- for (int i = 0; i < polygon_editor->get_polygon_count(); i++) {
- Vector<Vector2> polygon = polygon_editor->get_polygon(i);
- nav_polygon->add_outline(polygon);
- }
- nav_polygon->make_polygons_from_outlines();
- return nav_polygon;
- }
- void TileDataNavigationEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Ref<NavigationPolygon> nav_polygon = tile_data->get_navigation_polygon(navigation_layer);
- polygon_editor->clear_polygons();
- if (nav_polygon.is_valid()) {
- for (int i = 0; i < nav_polygon->get_outline_count(); i++) {
- polygon_editor->add_polygon(nav_polygon->get_outline(i));
- }
- }
- 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());
- }
- void TileDataNavigationEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, Variant p_value) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND(!tile_data);
- Ref<NavigationPolygon> nav_polygon = p_value;
- tile_data->set_navigation_polygon(navigation_layer, nav_polygon);
- 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());
- }
- Variant TileDataNavigationEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
- TileData *tile_data = p_tile_set_atlas_source->get_tile_data(p_coords, p_alternative_tile);
- ERR_FAIL_COND_V(!tile_data, Variant());
- return tile_data->get_navigation_polygon(navigation_layer);
- }
- void TileDataNavigationEditor::_setup_undo_redo_action(TileSetAtlasSource *p_tile_set_atlas_source, HashMap<TileMapCell, Variant, TileMapCell> p_previous_values, Variant p_new_value) {
- for (const KeyValue<TileMapCell, Variant> &E : p_previous_values) {
- Vector2i coords = E.key.get_atlas_coords();
- 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);
- 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);
- }
- }
- void TileDataNavigationEditor::_tile_set_changed() {
- polygon_editor->set_tile_set(tile_set);
- }
- void TileDataNavigationEditor::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE: {
- #ifdef DEBUG_ENABLED
- polygon_editor->set_polygons_color(NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color());
- #endif // DEBUG_ENABLED
- } break;
- }
- }
- TileDataNavigationEditor::TileDataNavigationEditor() {
- undo_redo = EditorNode::get_undo_redo();
- polygon_editor = memnew(GenericTilePolygonEditor);
- polygon_editor->set_multiple_polygon_mode(true);
- add_child(polygon_editor);
- }
- void TileDataNavigationEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2D p_transform, TileMapCell p_cell, bool p_selected) {
- TileData *tile_data = _get_tile_data(p_cell);
- ERR_FAIL_COND(!tile_data);
- // Draw all shapes.
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
- Ref<NavigationPolygon> nav_polygon = tile_data->get_navigation_polygon(navigation_layer);
- if (nav_polygon.is_valid()) {
- Vector<Vector2> verts = nav_polygon->get_vertices();
- if (verts.size() < 3) {
- return;
- }
- Color color = Color(0.5, 1.0, 1.0, 1.0);
- #ifdef DEBUG_ENABLED
- color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
- #endif // DEBUG_ENABLED
- if (p_selected) {
- Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
- 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);
- selection_color.a = 0.7;
- color = selection_color;
- }
- RandomPCG rand;
- for (int i = 0; i < nav_polygon->get_polygon_count(); i++) {
- // An array of vertices for this polygon.
- Vector<int> polygon = nav_polygon->get_polygon(i);
- Vector<Vector2> vertices;
- vertices.resize(polygon.size());
- for (int j = 0; j < polygon.size(); j++) {
- ERR_FAIL_INDEX(polygon[j], verts.size());
- vertices.write[j] = verts[polygon[j]];
- }
- // Generate the polygon color, slightly randomly modified from the settings one.
- Color random_variation_color;
- 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);
- random_variation_color.a = color.a;
- Vector<Color> colors;
- colors.push_back(random_variation_color);
- RenderingServer::get_singleton()->canvas_item_add_polygon(p_canvas_item->get_canvas_item(), vertices, colors);
- }
- }
- RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
- }
|