From 587d8fe65d61b8f7217b4e8850b44e43159c16fb Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 21 Jan 2024 14:48:35 +0100 Subject: Save WIP --- src/lib/animations/blink.js | 6 -- src/lib/components/background.svelte | 56 +++++++------ src/lib/components/hud.svelte | 35 ++++---- src/lib/components/minimap.svelte | 91 ++++++++++---------- src/lib/components/minimap_sector.svelte | 22 +++++ src/lib/components/palette.svelte | 33 ++++---- src/lib/components/sensor.svelte | 49 ++++++----- src/lib/components/widget.svelte | 139 +++++++++++++------------------ src/lib/math.js | 4 +- src/lib/plugins/square.js | 0 src/lib/stores/canvas.js | 30 +++---- src/lib/stores/widgets.js | 85 ++++++++++--------- 12 files changed, 278 insertions(+), 272 deletions(-) create mode 100644 src/lib/components/minimap_sector.svelte create mode 100644 src/lib/plugins/square.js (limited to 'src/lib') diff --git a/src/lib/animations/blink.js b/src/lib/animations/blink.js index c12e031..eca3917 100644 --- a/src/lib/animations/blink.js +++ b/src/lib/animations/blink.js @@ -9,14 +9,8 @@ export const blink = function blink(node, params) { duration: params.duration || 400, easing: params.easing || sineInOut, css: (t) => { - const halfWidth = originalWidth / 2; - const halfHeight = originalHeight / 2; const width = Math.round(t <= 0.2 ? (originalWidth * t) / 0.2 : originalWidth); - const marginY = Math.round(t <= 0.2 ? halfHeight * (1 - t / 0.2) : 0); const height = Math.round(t > 0.2 ? ((t - 0.2) / 0.8) * originalHeight : 0); - const marginX = Math.round(t > 0.2 ? (1 - (t - 0.2) / 0.8) * halfWidth : halfWidth); - - console.log(width, height); return `width: ${width}px; height: ${height}px;`; } diff --git a/src/lib/components/background.svelte b/src/lib/components/background.svelte index 1aa82fa..20b9e37 100644 --- a/src/lib/components/background.svelte +++ b/src/lib/components/background.svelte @@ -1,33 +1,39 @@ -
+
diff --git a/src/lib/components/hud.svelte b/src/lib/components/hud.svelte index ffea4de..a115c91 100644 --- a/src/lib/components/hud.svelte +++ b/src/lib/components/hud.svelte @@ -1,29 +1,26 @@
- { formatCoordinate($canvas.x) } × { formatCoordinate($canvas.y) } + {formatCoordinate($canvas.x)} × {formatCoordinate($canvas.y)}
diff --git a/src/lib/components/minimap.svelte b/src/lib/components/minimap.svelte index cfee685..0f823f4 100644 --- a/src/lib/components/minimap.svelte +++ b/src/lib/components/minimap.svelte @@ -1,54 +1,55 @@
- - - - - - - - - - - - - - - - -
+ + {#each sectors as _, y} + + {#each sectors[y] as _, x} + + {/each} + + {/each} +
- diff --git a/src/lib/components/minimap_sector.svelte b/src/lib/components/minimap_sector.svelte new file mode 100644 index 0000000..2e14043 --- /dev/null +++ b/src/lib/components/minimap_sector.svelte @@ -0,0 +1,22 @@ + + + + + diff --git a/src/lib/components/palette.svelte b/src/lib/components/palette.svelte index 94ba3db..4b7bcca 100644 --- a/src/lib/components/palette.svelte +++ b/src/lib/components/palette.svelte @@ -3,23 +3,24 @@
-

- palette palette palette palette palette palette
- palette palette palette palette palette palette
- palette palette palette palette palette palette
- palette palette palette palette palette palette
- palette palette palette palette palette palette
- palette palette palette palette palette palette
-

+

+ palette palette palette palette palette palette
+ palette palette palette palette palette palette
+ palette palette palette palette palette palette
+ palette palette palette palette palette palette
+ palette palette palette palette palette palette
+ palette palette palette palette palette palette
+

diff --git a/src/lib/components/sensor.svelte b/src/lib/components/sensor.svelte index b6d38d8..9458705 100644 --- a/src/lib/components/sensor.svelte +++ b/src/lib/components/sensor.svelte @@ -1,34 +1,33 @@ diff --git a/src/lib/components/widget.svelte b/src/lib/components/widget.svelte index 907a016..87a3ee5 100644 --- a/src/lib/components/widget.svelte +++ b/src/lib/components/widget.svelte @@ -1,89 +1,66 @@ -
- {widget.type} + {widget.type}
diff --git a/src/lib/math.js b/src/lib/math.js index c745cdc..9b45cf0 100644 --- a/src/lib/math.js +++ b/src/lib/math.js @@ -3,6 +3,6 @@ * @param {number} dividend the number to divide * @param {number} divisor the divisor to calculate the remainder */ -export const modulo = function modulo (dividend, divisor) { - return ((dividend % divisor) + divisor) % divisor +export const modulo = function modulo(dividend, divisor) { + return ((dividend % divisor) + divisor) % divisor; }; diff --git a/src/lib/plugins/square.js b/src/lib/plugins/square.js new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/stores/canvas.js b/src/lib/stores/canvas.js index 2a5e89c..ed0a5d7 100644 --- a/src/lib/stores/canvas.js +++ b/src/lib/stores/canvas.js @@ -1,24 +1,24 @@ import { browser } from '$app/environment'; import { derived, writable } from 'svelte/store'; -export const canvas = writable({x: 0, y: 0}); +export const canvas = writable({ x: 0, y: 0 }); export const topLeft = derived(canvas, ($canvas) => { - if (browser) { - return { - x: $canvas.x - window.screen.width/2, - y: $canvas.y - window.screen.height/2 - }; - } - return {x: 0, y: 0} + if (browser) { + return { + x: $canvas.x - window.screen.width / 2, + y: $canvas.y - window.screen.height / 2 + }; + } + return { x: 0, y: 0 }; }); export const bottomRight = derived(canvas, ($canvas) => { - if (browser) { - return { - x: $canvas.x + window.screen.width/2, - y: $canvas.y + window.screen.height/2 - }; - } - return {x: 0, y: 0} + if (browser) { + return { + x: $canvas.x + window.screen.width / 2, + y: $canvas.y + window.screen.height / 2 + }; + } + return { x: 0, y: 0 }; }); diff --git a/src/lib/stores/widgets.js b/src/lib/stores/widgets.js index 9bdb649..372cad1 100644 --- a/src/lib/stores/widgets.js +++ b/src/lib/stores/widgets.js @@ -1,55 +1,64 @@ -import { derived, writable } from 'svelte/store'; +import { derived, readable, writable } from 'svelte/store'; import { createClient } from '@supabase/supabase-js'; import { supabase } from '$lib/config'; import { maxSize } from '$lib/config'; -const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/ +const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/; const client = createClient(supabase.url, supabase.key); -export const sensor = writable({left: 0, top: 0, right: 0, bottom: 0}); +export const sensor = writable({ left: 0, top: 0, right: 0, bottom: 0 }); -const getBoxes = function getBoxes ({left, top, right, bottom}) { - const results = [ - `box.ov."((${left},${top}),(${right},${bottom}))"`, - `box.ov."((${left+maxSize},${top+maxSize}),(${right+maxSize},${bottom+maxSize}))"`, - `box.ov."((${left+maxSize},${top}),(${right+maxSize},${bottom}))"`, - `box.ov."((${left},${top+maxSize}),(${right},${bottom+maxSize}))"`, - `box.ov."((${left-maxSize},${top-maxSize}),(${right-maxSize},${bottom-maxSize}))"`, - `box.ov."((${left-maxSize},${top}),(${right-maxSize},${bottom}))"`, - `box.ov."((${left},${top-maxSize}),(${right},${bottom-maxSize}))"` - ]; +const getBoxes = function getBoxes({ left, top, right, bottom }) { + const results = [ + `box.ov."((${left},${top}),(${right},${bottom}))"`, + `box.ov."((${left + maxSize},${top + maxSize}),(${right + maxSize},${bottom + maxSize}))"`, + `box.ov."((${left + maxSize},${top}),(${right + maxSize},${bottom}))"`, + `box.ov."((${left},${top + maxSize}),(${right},${bottom + maxSize}))"`, + `box.ov."((${left - maxSize},${top - maxSize}),(${right - maxSize},${bottom - maxSize}))"`, + `box.ov."((${left - maxSize},${top}),(${right - maxSize},${bottom}))"`, + `box.ov."((${left},${top - maxSize}),(${right},${bottom - maxSize}))"` + ]; - return results + return results; }; const serialize = function serialize(widget) { + const boxComponents = widget.box.match(boxParser).slice(1, 5).map(Number); + const box = { + left: Math.min(boxComponents[0], boxComponents[2]), + right: Math.max(boxComponents[0], boxComponents[2]), + top: Math.min(boxComponents[1], boxComponents[3]), + bottom: Math.max(boxComponents[1], boxComponents[3]) + }; - const boxComponents = widget.box - .match(boxParser) - .slice(1,5) - .map(Number); - const box = { - left: Math.min(boxComponents[0], boxComponents[2]), - right: Math.max(boxComponents[0], boxComponents[2]), - top: Math.min(boxComponents[1], boxComponents[3]), - bottom: Math.max(boxComponents[1], boxComponents[3]) - }; - - return {...widget, box } + return { ...widget, box }; }; let ac = null; export const widgets = derived(sensor, async function ($sensor, set) { - - const boxes = getBoxes($sensor); - ac && ac.abort() - ac = new AbortController(); - const { data } = await client - .from('widgets') - .select() - .or(boxes.join(',')) - .abortSignal(ac.signal) - if (data) { - return set(data.map(serialize)); - } + const boxes = getBoxes($sensor); + ac && ac.abort(); + ac = new AbortController(); + const { data } = await client.from('widgets').select().or(boxes.join(',')).abortSignal(ac.signal); + if (data) { + return set(data.map(serialize)); + } }); + +export const countElements = function countElements(left, top, right, bottom) { + let countAc = null; + return readable(0, (set) => { + (async function () { + countAc && countAc.abort(); + countAc = new AbortController(); + const { data } = await client + .from('widgets') + .select('*', { head: true, count: 'estimated' }) + .or(`box.ov."((${left},${top}),(${right},${bottom}))"`) + .abortSignal(countAc.signal); + if (data) { + return set(data); + } + })(); + }); +}; -- cgit