use matrix interface on admin

This commit is contained in:
waltem01 2024-02-16 09:27:19 +01:00
parent 846778b969
commit 0554531b12

View File

@ -1,7 +1,13 @@
<script lang="ts"> <script lang="ts">
import {
PUBLIC_LED_CHAIN,
PUBLIC_LED_HEIGHT,
PUBLIC_LED_PARALLEL,
PUBLIC_LED_WIDTH
} from '$env/static/public';
import { rgbToHex, type Color, getContrastColor } from '$lib/client/color'; import { rgbToHex, type Color, getContrastColor } from '$lib/client/color';
import { detectGamepad, gamepadButtonPress } from '$lib/client/gamepad'; import { detectGamepad, gamepadButtonPress } from '$lib/client/gamepad';
import { initializeMatrix } from '$lib/client/matrix'; import { initializeMatrix, type Matrix } from '$lib/client/matrix';
import { toRadians } from '$lib/client/miscellaneous'; import { toRadians } from '$lib/client/miscellaneous';
import type { APIResponse } from '$lib/interfaces'; import type { APIResponse } from '$lib/interfaces';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
@ -81,7 +87,8 @@
const ny = Math.round(Math.sin(toRadians(a)) * r + y); const ny = Math.round(Math.sin(toRadians(a)) * r + y);
// If within bounds, draw pixel with color // If within bounds, draw pixel with color
if (nx >= 0 && nx < 192 && ny >= 0 && ny < 192) matrix[ny][nx].color = hex; if (nx >= 0 && nx < 192 && ny >= 0 && ny < 192 && matrix.grid)
matrix.grid[ny][nx].color = hex;
} }
} }
@ -96,7 +103,8 @@
// Convert stored color data to hex // Convert stored color data to hex
const hex = rgbToHex(pixelColor); const hex = rgbToHex(pixelColor);
// Loop through every position in rectangle to set color // Loop through every position in rectangle to set color
for (let j = y; j < y + h; j++) for (let i = x; i < x + w; i++) matrix[j][i].color = hex; if (!matrix.grid) return;
for (let j = y; j < y + h; j++) for (let i = x; i < x + w; i++) matrix.grid[j][i].color = hex;
} }
// Draw pixel onto matrix // Draw pixel onto matrix
@ -106,12 +114,12 @@
const y = parseInt((formData.get('y') as string) ?? '191'); const y = parseInt((formData.get('y') as string) ?? '191');
// Set singular coodinate to given color // Set singular coodinate to given color
matrix[y][x].color = rgbToHex(pixelColor); if (matrix.grid) matrix.grid[y][x].color = rgbToHex(pixelColor);
} }
// Clear simulated matrix // Clear simulated matrix
function clearMatrix() { function clearMatrix() {
matrix.forEach((y) => y.forEach((x) => (x.color = '000000'))); matrix.grid?.forEach((y) => y.forEach((x) => (x.color = '000000')));
} }
function colorSelect(formData: FormData) { function colorSelect(formData: FormData) {
@ -135,9 +143,9 @@
// Convert stored color data to hex // Convert stored color data to hex
const next = rgbToHex(pixelColor); const next = rgbToHex(pixelColor);
// Ignore if color stays the same to reduce API calls // Ignore if color stays the same to reduce API calls
if (matrix[y][x].color === next) return; if (matrix.grid?.[y][x].color === next) return;
// Set pixel to color // Set pixel to color
matrix[y][x].color = next; matrix.grid![y][x].color = next;
// Create new formdata // Create new formdata
const fdata = new FormData(); const fdata = new FormData();
@ -184,7 +192,7 @@
padding = 0.1875 * scaling; padding = 0.1875 * scaling;
// Reinitialize matrix // Reinitialize matrix
matrix = initializeMatrix(scaling); matrix.grid = initializeMatrix(scaling);
} }
// System variables // System variables
@ -192,7 +200,7 @@
padding = 0.5625; padding = 0.5625;
let mousePressed = false; let mousePressed = false;
let gamepad: Gamepad | null = null; let gamepad: Gamepad | null = null;
let matrix = initializeMatrix(scaling); let matrix: Matrix;
const gameCoords = { x: 0, y: 0 }; const gameCoords = { x: 0, y: 0 };
const pixelColor = { r: 255, g: 255, b: 255 } as Color; const pixelColor = { r: 255, g: 255, b: 255 } as Color;
@ -223,6 +231,12 @@
// Remove only if it's the same gameapd // Remove only if it's the same gameapd
if (e.gamepad.id === gamepad?.id) gamepad = null; if (e.gamepad.id === gamepad?.id) gamepad = null;
}); });
matrix = {
width: +PUBLIC_LED_WIDTH * +PUBLIC_LED_CHAIN,
height: +PUBLIC_LED_HEIGHT * +PUBLIC_LED_PARALLEL,
grid: initializeMatrix(scaling)
};
}); });
</script> </script>
@ -561,7 +575,7 @@
<div class="flex justify-center"> <div class="flex justify-center">
<!-- Color display --> <!-- Color display -->
<table id="main" class="mt-5 border-2 border-black"> <table id="main" class="mt-5 border-2 border-black">
{#each matrix as row} {#each matrix?.grid ?? [] as row}
<tr> <tr>
{#each row as cell} {#each row as cell}
<td style="padding: {padding}rem; background-color: #{cell.color}" /> <td style="padding: {padding}rem; background-color: #{cell.color}" />
@ -578,7 +592,7 @@
on:mousedown|preventDefault={() => (mousePressed = true)} on:mousedown|preventDefault={() => (mousePressed = true)}
on:mouseup|preventDefault={() => (mousePressed = false)} on:mouseup|preventDefault={() => (mousePressed = false)}
> >
{#each matrix as row} {#each matrix?.grid ?? [] as row}
<tr data-y={row[0].y}> <tr data-y={row[0].y}>
{#each row as cell} {#each row as cell}
<td <td