Snapping to grid.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4465 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-09-17 20:52:12 +00:00
parent 3c4959433a
commit 47532b8512

View File

@ -18,8 +18,11 @@ const k_store = {
'🦞': 15, '🦞': 15,
'🛶': 10, '🛶': 10,
'🏠': 10, '🏠': 10,
'⛰': 10,
}; };
const k_marker_snap = {x: 5, y: 1};
class GgAppElement extends LitElement { class GgAppElement extends LitElement {
static get properties() { static get properties() {
return { return {
@ -54,6 +57,7 @@ class GgAppElement extends LitElement {
this.load().catch(function(e) { this.load().catch(function(e) {
console.log('load error', e); console.log('load error', e);
}); });
this.to_build = '🏠';
} }
async load() { async load() {
@ -363,6 +367,22 @@ class GgAppElement extends LitElement {
.openOn(this.leaflet); .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) { on_mouse_down(event) {
if (this.marker) { if (this.marker) {
this.marker.remove(); this.marker.remove();
@ -370,8 +390,9 @@ class GgAppElement extends LitElement {
} }
if (this.to_build) { 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({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) { for (let activity of self.loaded_activities) {
self.draw_activity_to_tile(image_data, mini.width, mini.height, ul, lr, activity); 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 x = 0; x < mini.width; x++) {
for (let y = 0; y < mini.height; y++) { for (let y = 0; y < mini.height; y++) {
let start = (y * mini.width + x) * 4; let start = (y * mini.width + x) * 4;
let pixel = self.color_to_emoji(image_data.data.slice(start, start + 4)); let pixel = self.color_to_emoji(image_data.data.slice(start, start + 4));
if (pixel) { 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) { 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_x = Math.floor(position.x / size.x);
let tile_y = Math.floor(position.y / size.y); let tile_y = Math.floor(position.y / size.y);
position.x = position.x - tile_x * size.x; position.x = position.x - tile_x * size.x;
position.y = position.y - tile_y * size.y; position.y = position.y - tile_y * size.y;
if (tile_x == coords.x && tile_y == coords.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; return tile;