silphius/app/react/components/devtools/tiles.jsx

231 lines
6.6 KiB
React
Raw Normal View History

2024-07-09 16:00:05 -05:00
import {useEffect, useRef, useState} from 'react';
2024-07-08 15:49:23 -05:00
2024-07-20 04:32:33 -05:00
import {useClient} from '@/react/context/client.js';
import {useEcs} from '@/react/context/ecs.js';
2024-07-08 17:31:07 -05:00
import useRect from '@/util/react-hooks/use-rect.js';
2024-07-08 15:49:23 -05:00
import styles from './tiles.module.css';
2024-07-09 16:00:05 -05:00
export default function Tiles({eventsChannel}) {
const client = useClient();
2024-07-08 17:31:07 -05:00
const wrapperRef = useRef();
const imageRef = useRef();
const imageRect = useRect(imageRef);
2024-07-10 14:12:48 -05:00
const [indices, setIndices] = useState([]);
2024-07-08 17:31:07 -05:00
const [selection, setSelection] = useState({x: 0, y: 0, w: 1, h: 1});
2024-07-08 15:49:23 -05:00
const [moveStart, setMoveStart] = useState();
2024-07-09 16:00:05 -05:00
const [layer, setLayer] = useState(0);
const [brush, setBrush] = useState(0);
const [stamp, setStamp] = useState([]);
2024-07-08 15:49:23 -05:00
const [ecs] = useEcs();
2024-07-09 16:00:05 -05:00
useEffect(() => {
if (!ecs) {
return false;
}
const master = ecs.get(1);
if (!master) {
return false;
}
const {TileLayers} = master;
const {area, tileSize} = TileLayers.layer(0);
function onClick({x, y}) {
const at = {
x: Math.floor(x / tileSize.x),
y: Math.floor(y / tileSize.y),
};
if (at.x < 0 || at.y < 0 || at.x >= area.x || at.y >= area.y) {
return;
}
client.send({
type: 'AdminAction',
2024-07-10 14:12:48 -05:00
payload: {
type: 'paint',
value: {
brush,
layer,
stamp: {
at,
data: stamp,
},
},
},
2024-07-09 16:00:05 -05:00
});
}
eventsChannel.addListener('click', onClick);
return () => {
eventsChannel.removeListener('click', onClick);
};
});
2024-07-08 15:49:23 -05:00
if (!ecs) {
return false;
}
const master = ecs.get(1);
if (!master) {
return false;
}
const {TileLayers} = master;
const {sourceJson, tileSize} = TileLayers.layer(0);
const {w, h} = sourceJson.meta.size;
2024-07-08 17:31:07 -05:00
const factor = (imageRect?.width ?? 1) / w;
2024-07-10 14:12:48 -05:00
const wt = w / tileSize.x;
2024-07-08 15:49:23 -05:00
return (
<div className={styles.tiles}>
<form>
<div className={styles.paintBar}>
<div className={styles.layer}>
<label>
2024-07-08 22:14:21 -05:00
<span>Layer</span>
2024-07-08 15:49:23 -05:00
<select
onChange={(event) => {
setLayer(event.target.value)
}}
value={layer}
>
{
Object.keys(TileLayers.layers)
.map((layer, i) => (
<option
key={i}
value={i}
>
{i}
</option>
))
}
</select>
</label>
</div>
<div className={styles.brush}>
<label>
2024-07-08 22:14:21 -05:00
<span>Brush</span>
2024-07-08 15:49:23 -05:00
<select
onChange={(event) => {
2024-07-08 17:31:07 -05:00
setBrush(event.target.value)
2024-07-08 15:49:23 -05:00
}}
value={brush}
>
<option>Paint</option>
</select>
</label>
</div>
2024-07-08 17:31:07 -05:00
</div>
2024-07-08 15:49:23 -05:00
</form>
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
<div
onMouseDown={(event) => {
2024-07-08 17:31:07 -05:00
const wrapperRect = wrapperRef.current.getBoundingClientRect();
const {left, top} = wrapperRect;
2024-07-08 22:14:21 -05:00
const c = {
x: event.clientX,
y: event.clientY + wrapperRef.current.scrollTop,
};
2024-07-08 15:49:23 -05:00
if (
2024-07-08 22:14:21 -05:00
c.x - left >= (w * factor)
|| c.y - top >= (h * factor)
2024-07-08 15:49:23 -05:00
) {
return;
}
2024-07-08 22:14:21 -05:00
const x = Math.floor((c.x - left) / (tileSize.x * factor));
const y = Math.floor((c.y - top) / (tileSize.y * factor));
2024-07-08 15:49:23 -05:00
setMoveStart({x, y});
setSelection({x, y, w: 1, h: 1});
2024-07-10 14:38:19 -05:00
setStamp([[y * wt + x]]);
2024-07-10 14:12:48 -05:00
setIndices([y * wt + x]);
2024-07-08 15:49:23 -05:00
}}
onMouseMove={(event) => {
2024-07-10 14:12:48 -05:00
if (0 === event.buttons) {
setMoveStart();
return;
}
2024-07-08 15:49:23 -05:00
if (!moveStart) {
return;
}
const {x: sx, y: sy} = moveStart;
2024-07-08 17:31:07 -05:00
const wrapperRect = wrapperRef.current.getBoundingClientRect();
const {left, top} = wrapperRect;
2024-07-08 22:14:21 -05:00
const c = {
x: event.clientX,
y: event.clientY + wrapperRef.current.scrollTop,
};
2024-07-08 15:49:23 -05:00
const x = Math.floor(
2024-07-08 22:14:21 -05:00
Math.max(0, Math.min((w * factor) - 1, (c.x - left)) / (tileSize.x * factor)),
2024-07-08 15:49:23 -05:00
);
const y = Math.floor(
2024-07-08 22:14:21 -05:00
Math.max(0, Math.min((h * factor) - 1, (c.y - top)) / (tileSize.y * factor)),
2024-07-08 15:49:23 -05:00
);
const mx = Math.min(sx, x);
const my = Math.min(sy, y);
2024-07-10 14:12:48 -05:00
const sw = Math.abs(sx - x) + 1;
const sh = Math.abs(sy - y) + 1;
const newSelection = {
x: mx,
y: my,
w: sw,
h: sh,
};
if (
selection.x === newSelection.x
&& selection.y === newSelection.y
&& selection.w === newSelection.w
&& selection.h === newSelection.h
) {
return;
}
setSelection(newSelection);
2024-07-08 15:49:23 -05:00
const stamp = [];
for (let iy = 0; iy < sh; ++iy) {
const row = [];
for (let ix = 0; ix < sw; ++ix) {
2024-07-10 14:38:19 -05:00
row.push((my + iy) * wt + mx + ix);
2024-07-08 15:49:23 -05:00
}
stamp.push(row);
}
setStamp(stamp);
2024-07-10 14:12:48 -05:00
const indices = [];
2024-07-10 14:38:19 -05:00
for (let sy = 0; sy < newSelection.h; ++sy) {
for (let sx = 0; sx < newSelection.w; ++sx) {
indices.push(((newSelection.y + sy) * wt) + (newSelection.x + sx));
2024-07-10 14:12:48 -05:00
}
}
setIndices(indices);
2024-07-08 15:49:23 -05:00
}}
className={styles.selectionWrapper}
2024-07-08 17:31:07 -05:00
ref={wrapperRef}
2024-07-08 15:49:23 -05:00
>
<div
className={styles.selection}
style={{
2024-07-08 17:31:07 -05:00
top: selection.y * tileSize.y * factor,
left: selection.x * tileSize.x * factor,
height: selection.h * tileSize.x * factor,
width: selection.w * tileSize.y * factor,
2024-07-08 15:49:23 -05:00
}}
/>
<img
alt="tileset"
2024-07-08 17:31:07 -05:00
ref={imageRef}
2024-07-08 15:49:23 -05:00
src={TileLayers.layer(0).source.replace('.json', '.png')}
/>
</div>
2024-07-10 14:12:48 -05:00
<div
className={styles.status}
>
<code className={styles.selectionStatus}>
Sel:
{' {'}
{selection.x}{', '}
{selection.y}{', '}
{selection.w}{', '}
{selection.h}
{'}'}
</code>
<code>
Idx:
{' ['}
{indices.join(', ')}
{']'}
</code>
</div>
2024-07-08 15:49:23 -05:00
</div>
);
}