From 47532b85120d6edd406954f9220d3318c35f9da1 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 17 Sep 2023 20:52:12 +0000 Subject: [PATCH] Snapping to grid. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4465 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/gg/script.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/apps/gg/script.js b/apps/gg/script.js index 62e82fb0..0a60f45b 100644 --- a/apps/gg/script.js +++ b/apps/gg/script.js @@ -18,8 +18,11 @@ const k_store = { '🦞': 15, '🛶': 10, '🏠': 10, + '⛰': 10, }; +const k_marker_snap = {x: 5, y: 1}; + class GgAppElement extends LitElement { static get properties() { return { @@ -54,6 +57,7 @@ class GgAppElement extends LitElement { this.load().catch(function(e) { console.log('load error', e); }); + this.to_build = '🏠'; } async load() { @@ -363,6 +367,22 @@ class GgAppElement extends LitElement { .openOn(this.leaflet); } + snap_to_grid(latlng, fudge) { + let position = this.leaflet.options.crs.latLngToPoint(latlng, this.leaflet.getZoom()); + position.x = Math.round(position.x / 16) * 16 + (fudge?.x ?? 0); + position.y = Math.round(position.y / 16) * 16 + (fudge?.y ?? 0); + position = this.leaflet.options.crs.pointToLatLng(position, this.leaflet.getZoom()); + return position; + } + + on_marker_move(event) { + if (!this.no_snap && this.marker) { + this.no_snap = true; + this.marker.setLatLng(this.snap_to_grid(this.marker.getLatLng(), k_marker_snap)); + this.no_snap = false; + } + } + on_mouse_down(event) { if (this.marker) { this.marker.remove(); @@ -370,8 +390,9 @@ class GgAppElement extends LitElement { } if (this.to_build) { - this.marker = L.marker(event.latlng, {icon: L.divIcon({className: 'build-icon'}), draggable: true}).addTo(this.leaflet); + this.marker = L.marker(this.snap_to_grid(event.latlng, k_marker_snap), {icon: L.divIcon({className: 'build-icon'}), draggable: true}).addTo(this.leaflet); this.marker.on({click: this.on_marker_click.bind(this)}); + this.marker.on({drag: this.on_marker_move.bind(this)}); } } @@ -409,23 +430,25 @@ class GgAppElement extends LitElement { for (let activity of self.loaded_activities) { self.draw_activity_to_tile(image_data, mini.width, mini.height, ul, lr, activity); } + context.textAlign = 'left'; + context.textBaseline = 'top'; for (let x = 0; x < mini.width; x++) { for (let y = 0; y < mini.height; y++) { let start = (y * mini.width + x) * 4; let pixel = self.color_to_emoji(image_data.data.slice(start, start + 4)); if (pixel) { - context.fillText(pixel, x * size.x / mini.width, y * size.y / mini.height + 10); + context.fillText(pixel, x * size.x / mini.width, y * size.y / mini.height); } } } for (let placed of self.placed_emojis) { - let position = self.leaflet.options.crs.latLngToPoint(placed.position, coords.z); + let position = self.leaflet.options.crs.latLngToPoint(self.snap_to_grid(placed.position), coords.z); let tile_x = Math.floor(position.x / size.x); let tile_y = Math.floor(position.y / size.y); position.x = position.x - tile_x * size.x; position.y = position.y - tile_y * size.y; if (tile_x == coords.x && tile_y == coords.y) { - context.fillText(placed.emoji, position.x, position.y + 10); + context.fillText(placed.emoji, position.x, position.y); } } return tile;