-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile-debug.html
More file actions
236 lines (225 loc) · 11.1 KB
/
Copy pathtile-debug.html
File metadata and controls
236 lines (225 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!doctype html>
<html><head><meta charset="utf-8"><title>Tile picker</title>
<style>
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 400; font-display: swap; src: url('assets/fonts/Inter-Regular.woff2') format('woff2'); }
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 600; font-display: swap; src: url('assets/fonts/Inter-SemiBold.woff2') format('woff2'); }
body { background: #1a1612; color: #e8dcc8; font-family: 'Inter', system-ui, -apple-system, sans-serif; margin: 0; }
#app { display: grid; grid-template-columns: 1fr 320px; height: 100vh; }
#sheetWrap { overflow: auto; padding: 12px; background: #0d0a08; }
canvas { image-rendering: pixelated; display: block; cursor: crosshair; }
#side { padding: 14px 18px; background: #1a1612; border-left: 1px solid #3a3024; overflow-y: auto; }
h2 { color: #ffd86b; margin: 0 0 8px 0; font-size: 15px; letter-spacing: 1px; }
#coord { font-size: 18px; color: #ffd86b; margin-bottom: 12px; }
#picks { font-size: 14px; line-height: 1.6; }
#picks div { padding: 4px 0; border-bottom: 1px solid #2a221a; }
button { background: rgba(255,216,107,0.12); color: #ffd86b; border: 1px solid rgba(255,216,107,0.3); padding: 4px 10px; cursor: pointer; font-family: inherit; font-size: 13px; margin-right: 6px; }
.controls { margin: 8px 0 12px 0; }
input { background: #0d0a08; color: #e8dcc8; border: 1px solid #3a3024; padding: 4px 8px; font-family: inherit; font-size: 13px; width: 140px; }
pre { background: #0d0a08; border: 1px solid #3a3024; padding: 8px; font-size: 12px; max-height: 240px; overflow: auto; white-space: pre-wrap; }
.hint { color: #8a7e6c; font-size: 12px; line-height: 1.4; margin-bottom: 10px; }
</style></head>
<body>
<div id="app">
<div id="sheetWrap"><canvas id="c"></canvas></div>
<div id="side">
<h2>Tile picker</h2>
<div class="hint">Hover to inspect a tile. Click to copy <code>[col, row]</code>. Tag a clicked tile by typing a label and clicking <b>Assign</b>; assignments accumulate below and you can copy the YAML snippet at the bottom.</div>
<div id="coord">—</div>
<div class="controls">
<input id="label" placeholder="label (e.g. floor)">
<button id="assign">Assign</button>
<button id="clear">Clear</button>
</div>
<h2>Assignments</h2>
<div id="picks"></div>
<h2>Paint preview</h2>
<div class="hint">Click a tile in the sheet to arm it as the brush, then click cells in the grid below to paint. Right-click a cell to erase. Cells are <code>4×</code> the source tile size.</div>
<canvas id="paint" style="margin: 6px 0;"></canvas>
<div class="controls">
<button id="clearPaint">Clear paint</button>
<button id="rotL">↺</button>
<button id="rotR">↻</button>
<button id="copyPaint">Copy ↗</button>
<span class="hint" id="brush">brush: none</span>
</div>
<pre id="paintOut"></pre>
<div class="hint">Press <kbd>R</kbd> to rotate the brush 90° (or use ↻). Each painted cell remembers its rotation.</div>
<h2>YAML</h2>
<pre id="yaml"></pre>
<div class="hint">Adjust zoom with <kbd>+</kbd>/<kbd>-</kbd> or via URL <code>?s=N</code> (default 6). Paint grid size <code>?paint=N</code> (default 4).</div>
</div>
</div>
<script>
const params = new URLSearchParams(location.search);
let SCALE = +params.get('s') || 6;
const TILE = 16;
const img = new Image();
img.src = 'assets/tiles.png';
const c = document.getElementById('c');
const ctx = c.getContext('2d');
let cols = 0, rows = 0, last = null;
const picks = []; // {label, col, row}
// Paint grid state.
const PAINT_N = +params.get('paint') || 4;
const PAINT_TILE_SCALE = 4; // each painted cell shown 4× the 16-px source
const paintGrid = Array.from({ length: PAINT_N }, () => Array(PAINT_N).fill(null));
let brush = null; // {col, row, rot} — rot is 0|1|2|3 (× 90° clockwise)
const paintCanvas = document.getElementById('paint');
const paintCtx = paintCanvas.getContext('2d');
paintCanvas.width = PAINT_N * TILE * PAINT_TILE_SCALE;
paintCanvas.height = PAINT_N * TILE * PAINT_TILE_SCALE;
paintCtx.imageSmoothingEnabled = false;
function drawRotated(ctx, src, sx, sy, sw, sh, dx, dy, dw, dh, rot) {
if (!rot) { ctx.drawImage(src, sx, sy, sw, sh, dx, dy, dw, dh); return; }
ctx.save();
ctx.translate(dx + dw / 2, dy + dh / 2);
ctx.rotate(rot * Math.PI / 2);
ctx.drawImage(src, sx, sy, sw, sh, -dw / 2, -dh / 2, dw, dh);
ctx.restore();
}
function renderPaint() {
const px = TILE * PAINT_TILE_SCALE;
paintCtx.fillStyle = '#0d0a08';
paintCtx.fillRect(0, 0, paintCanvas.width, paintCanvas.height);
for (let y = 0; y < PAINT_N; y++) for (let x = 0; x < PAINT_N; x++) {
const t = paintGrid[y][x];
if (t) drawRotated(paintCtx, img, t.col * TILE, t.row * TILE, TILE, TILE, x * px, y * px, px, px, t.rot ?? 0);
}
paintCtx.strokeStyle = 'rgba(255,255,255,0.18)';
paintCtx.lineWidth = 1;
for (let i = 0; i <= PAINT_N; i++) {
paintCtx.beginPath(); paintCtx.moveTo(i * px, 0); paintCtx.lineTo(i * px, paintCanvas.height); paintCtx.stroke();
paintCtx.beginPath(); paintCtx.moveTo(0, i * px); paintCtx.lineTo(paintCanvas.width, i * px); paintCtx.stroke();
}
}
function setBrushLabel() {
const el = document.getElementById('brush');
if (!brush) { el.textContent = 'brush: none'; return; }
el.textContent = `brush: (${brush.col}, ${brush.row}) rot=${(brush.rot ?? 0) * 90}°`;
}
function paintAsText() {
// Format each cell as `c,r,rot°` (or `.` if empty), padded to a uniform
// width so the grid is readable. Easy for me to paste back to Claude.
const cell = (t) => t ? `${t.col},${t.row},${(t.rot ?? 0) * 90}` : '.';
const cells = paintGrid.map(row => row.map(cell));
const width = Math.max(1, ...cells.flat().map(s => s.length));
return cells.map(row => row.map(s => s.padEnd(width, ' ')).join(' ')).join('\n');
}
function refreshPaintOut() { document.getElementById('paintOut').textContent = paintAsText(); }
paintCanvas.addEventListener('contextmenu', e => e.preventDefault());
paintCanvas.addEventListener('mousedown', e => {
const rect = paintCanvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / (TILE * PAINT_TILE_SCALE));
const y = Math.floor((e.clientY - rect.top) / (TILE * PAINT_TILE_SCALE));
if (x < 0 || y < 0 || x >= PAINT_N || y >= PAINT_N) return;
if (e.button === 2) {
// Right-click rotates the painted cell's tile by +90°. If the cell is
// empty, rotate the brush instead so subsequent paints use the new angle.
if (paintGrid[y][x]) paintGrid[y][x] = { ...paintGrid[y][x], rot: (((paintGrid[y][x].rot ?? 0) + 1) & 3) };
else if (brush) { brush.rot = ((brush.rot ?? 0) + 1) & 3; setBrushLabel(); }
} else if (brush) {
paintGrid[y][x] = { ...brush };
}
renderPaint();
refreshPaintOut();
});
document.getElementById('clearPaint').onclick = () => {
for (let y = 0; y < PAINT_N; y++) paintGrid[y].fill(null);
renderPaint();
refreshPaintOut();
};
document.getElementById('copyPaint').onclick = async () => {
try { await navigator.clipboard.writeText(paintAsText()); document.getElementById('copyPaint').textContent = 'Copied'; setTimeout(() => document.getElementById('copyPaint').textContent = 'Copy ↗', 900); } catch {}
};
function rotateBrush(delta) {
if (!brush) return;
brush.rot = (((brush.rot ?? 0) + delta) & 3);
setBrushLabel();
}
document.getElementById('rotL').onclick = () => rotateBrush(-1);
document.getElementById('rotR').onclick = () => rotateBrush(1);
function render() {
const sCols = +params.get('cols');
const sRows = +params.get('rows');
cols = Math.min(sCols || (img.naturalWidth / TILE), img.naturalWidth / TILE);
rows = Math.min(sRows || (img.naturalHeight / TILE), img.naturalHeight / TILE);
c.width = cols * TILE * SCALE;
c.height = rows * TILE * SCALE;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(img, 0, 0, cols * TILE, rows * TILE, 0, 0, c.width, c.height);
// grid
ctx.strokeStyle = 'rgba(255,255,255,0.18)';
ctx.lineWidth = 1;
for (let i = 0; i <= cols; i++) { ctx.beginPath(); ctx.moveTo(i * TILE * SCALE, 0); ctx.lineTo(i * TILE * SCALE, c.height); ctx.stroke(); }
for (let j = 0; j <= rows; j++) { ctx.beginPath(); ctx.moveTo(0, j * TILE * SCALE); ctx.lineTo(c.width, j * TILE * SCALE); ctx.stroke(); }
// labels every cell (top-left corner)
ctx.font = `bold ${Math.max(9, Math.floor(SCALE * 1.6))}px monospace`;
for (let r = 0; r < rows; r++) for (let q = 0; q < cols; q++) {
const x = q * TILE * SCALE, y = r * TILE * SCALE;
// backdrop for readability
ctx.fillStyle = 'rgba(0,0,0,0.55)';
ctx.fillRect(x + 1, y + 1, SCALE * 4.5, SCALE * 1.8);
ctx.fillStyle = '#ffe88a';
ctx.fillText(`${q},${r}`, x + 2, y + SCALE * 1.6);
}
// highlight currently picked tile
if (last) {
ctx.strokeStyle = '#ffd86b';
ctx.lineWidth = 3;
ctx.strokeRect(last.col * TILE * SCALE + 1, last.row * TILE * SCALE + 1, TILE * SCALE - 2, TILE * SCALE - 2);
}
}
img.onload = () => { render(); renderPaint(); refreshPaintOut(); };
c.addEventListener('mousemove', e => {
const rect = c.getBoundingClientRect();
const px = e.clientX - rect.left, py = e.clientY - rect.top;
const col = Math.floor(px / (TILE * SCALE));
const row = Math.floor(py / (TILE * SCALE));
document.getElementById('coord').textContent = `(${col}, ${row})`;
});
c.addEventListener('click', async e => {
const rect = c.getBoundingClientRect();
const px = e.clientX - rect.left, py = e.clientY - rect.top;
const col = Math.floor(px / (TILE * SCALE));
const row = Math.floor(py / (TILE * SCALE));
last = { col, row };
brush = { col, row, rot: brush?.rot ?? 0 };
setBrushLabel();
document.getElementById('coord').textContent = `selected (${col}, ${row})`;
try { await navigator.clipboard.writeText(`[${col}, ${row}]`); } catch {}
render();
});
document.getElementById('assign').onclick = () => {
const label = document.getElementById('label').value.trim();
if (!label || !last) return;
picks.push({ label, col: last.col, row: last.row });
document.getElementById('label').value = '';
refreshPicks();
};
document.getElementById('clear').onclick = () => { picks.length = 0; refreshPicks(); };
function refreshPicks() {
const grouped = {};
for (const p of picks) (grouped[p.label] ??= []).push([p.col, p.row]);
document.getElementById('picks').innerHTML = picks.map((p, i) =>
`<div>${p.label}: <span style="color:#ffd86b">[${p.col}, ${p.row}]</span> <button onclick="removePick(${i})">×</button></div>`
).join('') || '<div style="color:#8a7e6c">none yet</div>';
let yaml = '';
for (const [label, coords] of Object.entries(grouped)) {
if (coords.length === 1) yaml += `${label}: [${coords[0].join(', ')}]\n`;
else {
yaml += `${label}:\n sprite: [${coords[0].join(', ')}]\n variants:\n`;
for (const c of coords) yaml += ` - [${c.join(', ')}]\n`;
}
}
document.getElementById('yaml').textContent = yaml || '(empty)';
}
window.removePick = (i) => { picks.splice(i, 1); refreshPicks(); };
window.addEventListener('keydown', e => {
if (e.target?.tagName === 'INPUT') return;
if (e.key === '+' || e.key === '=') { SCALE++; render(); }
if (e.key === '-' && SCALE > 2) { SCALE--; render(); }
if (e.key === 'r' || e.key === 'R') rotateBrush(1);
});
refreshPicks();
</script>
</body></html>