|
|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
const $ = id => document.getElementById(id);
|
|
|
|
|
const clone = value => JSON.parse(JSON.stringify(value));
|
|
|
|
|
const icons = () => window.lucide.createIcons();
|
|
|
|
|
const state = { mode: 'mainline', level: null, revision: '', baseline: '', definitions: [], selected: null, material: null,
|
|
|
|
|
const state = { mode: 'mainline', level: null, revision: '', baseline: '', definitions: [], selected: null, selections: [], material: null, materialOrientation: 2,
|
|
|
|
|
kind: 'characters', view: 'source', history: [], future: [], result: null, sequence: 0,
|
|
|
|
|
zoom: 1, pan: { x: 0, y: 0 }, drag: null, hover: null, busy: false, draft: null };
|
|
|
|
|
const images = new Map();
|
|
|
|
@@ -10,7 +10,20 @@ const canvas = $('board'), ctx = canvas.getContext('2d');
|
|
|
|
|
let transform = null, validateTimer;
|
|
|
|
|
const layoutText = level => JSON.stringify([level.width, level.height, level.characters, level.aoes]);
|
|
|
|
|
const dirty = () => state.level && layoutText(state.level) !== state.baseline;
|
|
|
|
|
const selectedEntry = () => state.selected && state.level?.[state.selected.collection]?.[state.selected.index];
|
|
|
|
|
const sameSelection = (a, b) => a?.collection === b?.collection && a?.index === b?.index;
|
|
|
|
|
const selections = () => !state.selected ? [] : state.selections?.includes(state.selected) ? state.selections : [state.selected];
|
|
|
|
|
function setSelections(items) { state.selections = items; state.selected = items.at(-1) || null; }
|
|
|
|
|
// 非法旋转只保存在展示层,不进入保存、草稿和撤销历史。
|
|
|
|
|
function cancelRotationPreview() {
|
|
|
|
|
if (!state.rotationPreview) return;
|
|
|
|
|
state.rotationPreview = null; state.drag = null; render();
|
|
|
|
|
}
|
|
|
|
|
function cancelCopyPreview() {
|
|
|
|
|
if (!state.copyPreview) return;
|
|
|
|
|
const original = state.copyPreview.originalSelection;
|
|
|
|
|
state.copyPreview = null; state.drag = null; setSelections(original); render();
|
|
|
|
|
}
|
|
|
|
|
const selectedEntry = () => selections().length === 1 && state.level?.[state.selected.collection]?.[state.selected.index];
|
|
|
|
|
const definitionFor = entry => state.definitions.find(item => item.id === entry?.[0]);
|
|
|
|
|
const draftKey = () => 'level-editor:' + state.project + ':source:' + state.sourceLevel;
|
|
|
|
|
function message(text, error = false) { $('message').textContent = text; $('message').classList.toggle('error', error); }
|
|
|
|
@@ -74,7 +87,13 @@ function renderLibrary() {
|
|
|
|
|
const name = document.createElement('strong'); name.textContent = definition.name;
|
|
|
|
|
const id = document.createElement('small'); id.textContent = definition.id;
|
|
|
|
|
button.append(thumbnail, name, id);
|
|
|
|
|
button.onclick = () => { if (state.busy) return; state.material = definition; state.selected = null; state.view = 'source'; state.hover = null; renderLibrary(); render(); message('已选择:' + definition.name); };
|
|
|
|
|
button.onclick = () => {
|
|
|
|
|
if (state.busy) return;
|
|
|
|
|
const deselect = state.material?.id === definition.id && state.material.collection === definition.collection;
|
|
|
|
|
state.material = deselect ? null : definition; state.materialOrientation = 2;
|
|
|
|
|
state.selected = null; state.view = 'source'; state.hover = null;
|
|
|
|
|
renderLibrary(); render(); message(deselect ? '已切换为选择模式' : '已选择:' + definition.name);
|
|
|
|
|
};
|
|
|
|
|
$('materials').append(button);
|
|
|
|
|
});
|
|
|
|
|
icons();
|
|
|
|
@@ -83,6 +102,7 @@ function renderInspector() {
|
|
|
|
|
const entry = selectedEntry(), definition = definitionFor(entry);
|
|
|
|
|
$('properties').hidden = !entry;
|
|
|
|
|
$('selection-empty').hidden = Boolean(entry);
|
|
|
|
|
$('selection-empty').querySelector('span').textContent = state.copyPreview ? '待放置副本:' + selections().length + ' 个对象' : selections().length > 1 ? '已选中 ' + selections().length + ' 个对象' : '未选择对象';
|
|
|
|
|
document.querySelectorAll('#properties input,#properties select,#properties textarea,#properties button,.board-settings input').forEach(input => { input.disabled = state.view !== 'source'; });
|
|
|
|
|
$('selection-kind').textContent = entry ? (state.selected.collection === 'characters' ? '军械' : '机关') : '';
|
|
|
|
|
if (!entry || !definition) return;
|
|
|
|
@@ -114,6 +134,18 @@ function render() {
|
|
|
|
|
$('columns').value = level.width; $('rows').value = level.height;
|
|
|
|
|
$('undo').disabled = !state.history.length; $('redo').disabled = !state.future.length;
|
|
|
|
|
for (const id of ['rotate', 'duplicate', 'delete']) $(id).disabled = !selectedEntry() || state.view !== 'source';
|
|
|
|
|
$('delete').disabled = !selections().length || state.view !== 'source';
|
|
|
|
|
$('duplicate').disabled = !selections().length || state.view !== 'source';
|
|
|
|
|
$('rotate').disabled = (!selections().length && !state.material) || state.view !== 'source';
|
|
|
|
|
$('selection-help').hidden = state.view !== 'source' || (!state.material && selections().length < 2);
|
|
|
|
|
$('selection-help').textContent = state.material
|
|
|
|
|
? '单击空位放置 · R 顺时针旋转 · E 逆时针旋转\n可连续放置 · 红色表示位置不合法\nEsc / 选择工具 / 再次点击同一对象图标:退出创建'
|
|
|
|
|
: state.copyPreview
|
|
|
|
|
? (state.copyPreview.valid ? '副本位置合法:单击放置' : '副本位置不合法:请移动到空位') + ' · R / E 旋转 · 右键取消'
|
|
|
|
|
: state.rotationPreview
|
|
|
|
|
? '位置不合法:可继续 R / E 旋转或拖动红色选区按格吸附;合法后应用,其他操作取消临时调整。'
|
|
|
|
|
: 'R 顺时针旋转 · E 逆时针旋转 · 拖动整体移动\nCtrl+C 复制 · Ctrl 单击增减选择 / 拖动框选 · Delete 删除(需确认) · Esc 取消选择';
|
|
|
|
|
if (state.rotationPreview || state.copyPreview) $('selection-help').hidden = false;
|
|
|
|
|
$('select-tool').classList.toggle('active', !state.material);
|
|
|
|
|
document.querySelectorAll('[data-view]').forEach(button => button.classList.toggle('active', button.dataset.view === state.view));
|
|
|
|
|
$('view-state').textContent = state.view === 'source' ? (state.material ? '放置 · ' + state.material.name : '源布局') : '局内预览 · 只读';
|
|
|
|
@@ -229,9 +261,15 @@ function displayedEntities() {
|
|
|
|
|
entry: [unit.mechanismId, unit.col, unit.row, unit.sourceOrientation] }))];
|
|
|
|
|
}
|
|
|
|
|
const units = new Map((state.result?.board?.units || []).map(unit => [unit.id, unit]));
|
|
|
|
|
return ['characters', 'aoes'].flatMap(collection => state.level[collection].map((entry, index) => ({ collection, index, entry,
|
|
|
|
|
const layout = state.copyPreview?.candidate || state.rotationPreview?.candidate || state.level;
|
|
|
|
|
return ['characters', 'aoes'].flatMap(collection => layout[collection].map((entry, index) => ({ collection, index, entry,
|
|
|
|
|
unit: collection === 'characters' ? units.get('u-' + String(index + 1).padStart(6, '0')) : null })));
|
|
|
|
|
}
|
|
|
|
|
// 绘制和命中共用层级:选中对象置顶,不改变源数据的对象顺序。
|
|
|
|
|
function orderedEntities() {
|
|
|
|
|
const selected = new Set(selections().map(item => item.collection + ':' + item.index));
|
|
|
|
|
return displayedEntities().sort((a, b) => Number(selected.has(a.collection + ':' + a.index)) - Number(selected.has(b.collection + ':' + b.index)));
|
|
|
|
|
}
|
|
|
|
|
function bombLabel(definition, unit) {
|
|
|
|
|
if (definition.boardBehavior === 'collisionBomb') {
|
|
|
|
|
const count = unit?.collisionHp ?? definition.behaviorParams?.collisionHp;
|
|
|
|
@@ -319,7 +357,7 @@ function drawEntity(entry, selected = false, ghost = false, invalid = false, uni
|
|
|
|
|
if (definition.boardBehavior !== 'burrowingBlocker') ctx.fillText({ 1: '↓', 2: '→', 3: '↑', 4: '←' }[direction], col + footprint.width - .19, row + .2);
|
|
|
|
|
drawBombLabel(definition, unit, col, row, footprint);
|
|
|
|
|
if (selected) {
|
|
|
|
|
ctx.lineWidth = .045; ctx.strokeStyle = '#07858c'; ctx.strokeRect(col + .018, row + .018, footprint.width - .036, footprint.height - .036);
|
|
|
|
|
ctx.lineWidth = .045; ctx.strokeStyle = invalid ? '#d5544b' : '#07858c'; ctx.strokeRect(col + .018, row + .018, footprint.width - .036, footprint.height - .036);
|
|
|
|
|
}
|
|
|
|
|
ctx.restore();
|
|
|
|
|
}
|
|
|
|
@@ -363,20 +401,56 @@ function draw() {
|
|
|
|
|
ctx.fillStyle = '#8d9ba4'; ctx.font = '0.21px Consolas,monospace'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
|
|
|
|
for (let col = 0; col < columns; col++) ctx.fillText(col, col + .5, -.38);
|
|
|
|
|
for (let row = 0; row < rows; row++) ctx.fillText(row, -.38, row + .5);
|
|
|
|
|
const entities = displayedEntities();
|
|
|
|
|
const entities = orderedEntities();
|
|
|
|
|
const invalidEntries = new Set((state.result?.issues || []).filter(issue => issue.collection).map(issue => issue.collection + ':' + issue.entryIndex));
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
|
const selected = state.selected?.collection === entity.collection && state.selected?.index === entity.index;
|
|
|
|
|
const selected = selections().some(item => sameSelection(item, entity));
|
|
|
|
|
if (state.drag?.type === 'object' && selected && !preview && state.drag.moved) continue;
|
|
|
|
|
drawEntity(entity.entry, selected, false, invalidEntries.has(entity.collection + ':' + entity.index), entity.unit);
|
|
|
|
|
drawEntity(entity.entry, selected, false, Boolean(selected && (state.rotationPreview || state.copyPreview && !state.copyPreview.valid)) || invalidEntries.has(entity.collection + ':' + entity.index), entity.unit);
|
|
|
|
|
}
|
|
|
|
|
if (!preview && state.drag?.type === 'object' && state.drag.moved) drawEntity(state.drag.candidate, true, true, !state.drag.valid);
|
|
|
|
|
if (!preview && state.drag?.type === 'object' && state.drag.moved) for (const item of state.drag.items) drawEntity(state.drag.candidate[item.collection][item.index], true, true, !state.drag.valid);
|
|
|
|
|
if (!preview && state.material && state.hover) {
|
|
|
|
|
const entry = [state.material.id, state.hover.col, state.hover.row, 2];
|
|
|
|
|
const entry = [state.material.id, state.hover.col, state.hover.row, state.materialOrientation];
|
|
|
|
|
const candidate = clone(state.level); candidate[state.material.collection].push(entry);
|
|
|
|
|
drawEntity(entry, false, true, Boolean(localIssues(candidate).length));
|
|
|
|
|
}
|
|
|
|
|
ctx.restore();
|
|
|
|
|
if (state.drag?.type === 'marquee' && state.drag.moved) {
|
|
|
|
|
const drag = state.drag, bounds = canvas.getBoundingClientRect();
|
|
|
|
|
const left = Math.min(drag.startX, drag.endX) - bounds.left, top = Math.min(drag.startY, drag.endY) - bounds.top;
|
|
|
|
|
const w = Math.abs(drag.endX - drag.startX), h = Math.abs(drag.endY - drag.startY);
|
|
|
|
|
ctx.save(); ctx.fillStyle = 'rgba(7,133,140,.12)'; ctx.strokeStyle = '#07858c'; ctx.lineWidth = 1;
|
|
|
|
|
ctx.setLineDash([5, 3]); ctx.fillRect(left, top, w, h); ctx.strokeRect(left, top, w, h); ctx.restore();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 框选在屏幕坐标下求交,保证缩放、平移及旋转预览中的命中与可见框一致。
|
|
|
|
|
function marqueeItems(drag) {
|
|
|
|
|
const left = Math.min(drag.startX, drag.endX), right = Math.max(drag.startX, drag.endX);
|
|
|
|
|
const top = Math.min(drag.startY, drag.endY), bottom = Math.max(drag.startY, drag.endY);
|
|
|
|
|
const box = [[left, top], [right, top], [right, bottom], [left, bottom]];
|
|
|
|
|
const t = transform, rect = canvas.getBoundingClientRect(), cosine = Math.cos(t.angle), sine = Math.sin(t.angle);
|
|
|
|
|
const axes = [[1, 0], [0, 1], [cosine, sine], [-sine, cosine]];
|
|
|
|
|
return displayedEntities().filter(({ entry }) => {
|
|
|
|
|
const footprint = definitionFor(entry)?.footprintsByOrientation[entry[3]];
|
|
|
|
|
return footprint?.cells.some(([dx, dy]) => {
|
|
|
|
|
const col = entry[1] + dx, row = entry[2] + dy;
|
|
|
|
|
const cell = [[col, row], [col + 1, row], [col + 1, row + 1], [col, row + 1]].map(([x, y]) => {
|
|
|
|
|
const gx = x - t.columns / 2, gy = y - t.rows / 2;
|
|
|
|
|
return [rect.left + t.x + (gx * cosine - gy * sine) * t.scale,
|
|
|
|
|
rect.top + t.y + (gx * sine + gy * cosine) * t.scale];
|
|
|
|
|
});
|
|
|
|
|
return axes.every(([ax, ay]) => {
|
|
|
|
|
const a = cell.map(([x, y]) => x * ax + y * ay), b = box.map(([x, y]) => x * ax + y * ay);
|
|
|
|
|
return Math.min(...a) < Math.max(...b) - 1e-7 && Math.min(...b) < Math.max(...a) - 1e-7;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}).map(({ collection, index }) => ({ collection, index }));
|
|
|
|
|
}
|
|
|
|
|
function updateMarquee(event) {
|
|
|
|
|
const drag = state.drag;
|
|
|
|
|
drag.endX = event.clientX; drag.endY = event.clientY;
|
|
|
|
|
drag.moved ||= Math.hypot(drag.endX - drag.startX, drag.endY - drag.startY) >= 4;
|
|
|
|
|
if (drag.moved) { setSelections(marqueeItems(drag)); render(); }
|
|
|
|
|
}
|
|
|
|
|
function gridPoint(event) {
|
|
|
|
|
if (!transform) return null;
|
|
|
|
@@ -386,9 +460,10 @@ function gridPoint(event) {
|
|
|
|
|
row: Math.floor(-dx * Math.sin(t.angle) + dy * Math.cos(t.angle) + t.rows / 2) };
|
|
|
|
|
}
|
|
|
|
|
function hit(point) {
|
|
|
|
|
return displayedEntities().reverse().find(entity => {
|
|
|
|
|
const entities = orderedEntities().reverse();
|
|
|
|
|
return entities.find(entity => {
|
|
|
|
|
const entry = entity.entry, footprint = definitionFor(entry)?.footprintsByOrientation[entry[3]];
|
|
|
|
|
return footprint?.cells.some(([dx, dy]) => entry[1] + dx === point.col && entry[2] + dy === point.row);
|
|
|
|
|
return footprint?.cells.some(([dx, dy]) => point.col >= entry[1] + dx && point.col < entry[1] + dx + 1 && point.row >= entry[2] + dy && point.row < entry[2] + dy + 1);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
canvas.addEventListener('pointerdown', guarded(event => {
|
|
|
|
@@ -396,44 +471,88 @@ canvas.addEventListener('pointerdown', guarded(event => {
|
|
|
|
|
if (pendingExtra()) applyExtra();
|
|
|
|
|
const point = gridPoint(event); if (!point) return;
|
|
|
|
|
canvas.focus();
|
|
|
|
|
if (state.copyPreview && event.button === 0) {
|
|
|
|
|
updateCopyPreview(point);
|
|
|
|
|
if (!state.copyPreview.valid) { message('副本无法放置:对象越界或军械重叠,请继续移动', true); return; }
|
|
|
|
|
const before = clone(state.level), candidate = state.copyPreview.candidate;
|
|
|
|
|
state.copyPreview = null; state.level = candidate; changed(before); render(); message('已放置复制对象,尚未保存'); return;
|
|
|
|
|
}
|
|
|
|
|
if (state.material && state.view === 'source' && event.button === 0) {
|
|
|
|
|
const entry = [state.material.id, point.col, point.row, 2], candidate = clone(state.level);
|
|
|
|
|
const entry = [state.material.id, point.col, point.row, state.materialOrientation], candidate = clone(state.level);
|
|
|
|
|
candidate[state.material.collection].push(entry);
|
|
|
|
|
if (localIssues(candidate).length) { message('无法放置:对象越界或军械重叠', true); return; }
|
|
|
|
|
mutate(() => { state.level[state.material.collection].push(entry); }); return;
|
|
|
|
|
}
|
|
|
|
|
const entity = event.button === 0 ? hit(point) : null;
|
|
|
|
|
if (!state.material && event.button === 0 && (event.ctrlKey || event.metaKey)) {
|
|
|
|
|
state.drag = { type: 'marquee', startX: event.clientX, startY: event.clientY,
|
|
|
|
|
endX: event.clientX, endY: event.clientY, moved: false, beforeSelection: selections().slice(),
|
|
|
|
|
clicked: entity ? { collection: entity.collection, index: entity.index } : null };
|
|
|
|
|
canvas.setPointerCapture(event.pointerId); event.preventDefault(); return;
|
|
|
|
|
}
|
|
|
|
|
if (entity) {
|
|
|
|
|
state.selected = { collection: entity.collection, index: entity.index }; state.material = null;
|
|
|
|
|
if (state.view === 'source') state.drag = { type: 'object', start: point, before: clone(state.level), original: clone(entity.entry), moved: false };
|
|
|
|
|
const item = { collection: entity.collection, index: entity.index }, previous = selections();
|
|
|
|
|
const alreadySelected = previous.some(selected => sameSelection(selected, item));
|
|
|
|
|
state.material = null;
|
|
|
|
|
if (!alreadySelected) setSelections([item]);
|
|
|
|
|
// 反选延迟到抬起,保留拖动已选对象及整组移动的能力。
|
|
|
|
|
state.drag = { type: 'object', start: point, before: clone(state.rotationPreview?.candidate || state.level), items: selections().slice(),
|
|
|
|
|
moved: false, didMove: false, deselectOnClick: alreadySelected && !state.rotationPreview, readOnly: state.view !== 'source' };
|
|
|
|
|
render();
|
|
|
|
|
} else {
|
|
|
|
|
state.selected = null; state.drag = { type: 'pan', x: event.clientX, y: event.clientY, original: { ...state.pan } }; render();
|
|
|
|
|
if (!event.ctrlKey && !event.metaKey) setSelections([]);
|
|
|
|
|
state.drag = { type: 'pan', x: event.clientX, y: event.clientY, original: { ...state.pan } }; render();
|
|
|
|
|
}
|
|
|
|
|
canvas.setPointerCapture(event.pointerId);
|
|
|
|
|
}));
|
|
|
|
|
canvas.addEventListener('pointermove', event => {
|
|
|
|
|
const point = gridPoint(event); if (!point) return;
|
|
|
|
|
state.hover = point; $('coordinates').textContent = '列 ' + point.col + ' / 行 ' + point.row;
|
|
|
|
|
if (state.copyPreview) { updateCopyPreview(point); render(); return; }
|
|
|
|
|
const drag = state.drag;
|
|
|
|
|
if (drag?.type === 'object') {
|
|
|
|
|
const dx = point.col - drag.start.col, dy = point.row - drag.start.row;
|
|
|
|
|
drag.candidate = clone(drag.original); shiftEntry(drag.candidate, dx, dy); drag.moved = dx !== 0 || dy !== 0;
|
|
|
|
|
const candidate = clone(state.level); candidate[state.selected.collection][state.selected.index] = drag.candidate;
|
|
|
|
|
drag.valid = !localIssues(candidate).length;
|
|
|
|
|
if (drag?.type === 'marquee') {
|
|
|
|
|
updateMarquee(event); return;
|
|
|
|
|
} else if (drag?.type === 'object') {
|
|
|
|
|
if (drag.readOnly) return;
|
|
|
|
|
let dx = point.col - drag.start.col, dy = point.row - drag.start.row;
|
|
|
|
|
if (state.rotationPreview) {
|
|
|
|
|
const anchor = drag.before[drag.items[0].collection][drag.items[0].index];
|
|
|
|
|
dx = Math.round(anchor[1] + dx) - anchor[1]; dy = Math.round(anchor[2] + dy) - anchor[2];
|
|
|
|
|
}
|
|
|
|
|
drag.candidate = clone(drag.before); drag.moved = dx !== 0 || dy !== 0; drag.didMove ||= drag.moved;
|
|
|
|
|
for (const item of drag.items) shiftEntry(drag.candidate[item.collection][item.index], dx, dy);
|
|
|
|
|
drag.valid = !localIssues(drag.candidate).length;
|
|
|
|
|
} else if (drag?.type === 'pan') state.pan = { x: drag.original.x + event.clientX - drag.x, y: drag.original.y + event.clientY - drag.y };
|
|
|
|
|
draw();
|
|
|
|
|
});
|
|
|
|
|
canvas.addEventListener('pointerup', event => {
|
|
|
|
|
if (state.drag?.type === 'marquee' && Number.isFinite(event.clientX) && Number.isFinite(event.clientY)) updateMarquee(event);
|
|
|
|
|
const drag = state.drag; state.drag = null;
|
|
|
|
|
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
|
|
|
|
|
if (drag?.type === 'object' && drag.moved) {
|
|
|
|
|
if (drag.valid) { state.level[state.selected.collection][state.selected.index] = drag.candidate; changed(drag.before); }
|
|
|
|
|
else message('移动已取消:对象越界或军械重叠', true);
|
|
|
|
|
if (drag?.type === 'marquee') {
|
|
|
|
|
if (!drag.moved && drag.clicked) {
|
|
|
|
|
const existed = drag.beforeSelection.some(item => sameSelection(item, drag.clicked));
|
|
|
|
|
setSelections(existed ? drag.beforeSelection.filter(item => !sameSelection(item, drag.clicked)) : [...drag.beforeSelection, drag.clicked]);
|
|
|
|
|
}
|
|
|
|
|
render(); return;
|
|
|
|
|
}
|
|
|
|
|
if (drag?.type === 'object' && drag.moved) {
|
|
|
|
|
if (drag.valid) {
|
|
|
|
|
const before = state.rotationPreview ? clone(state.level) : drag.before;
|
|
|
|
|
state.rotationPreview = null; state.level = drag.candidate; changed(before);
|
|
|
|
|
} else if (state.rotationPreview) {
|
|
|
|
|
state.rotationPreview.candidate = drag.candidate; render(); message('位置仍不合法,请继续拖动红色选区', true);
|
|
|
|
|
} else message('移动已取消:对象越界或军械重叠', true);
|
|
|
|
|
}
|
|
|
|
|
if (drag?.type === 'object' && !drag.didMove && drag.deselectOnClick) { setSelections([]); render(); }
|
|
|
|
|
draw();
|
|
|
|
|
});
|
|
|
|
|
canvas.addEventListener('pointercancel', () => { state.drag = null; draw(); });
|
|
|
|
|
function cancelPointerDrag() {
|
|
|
|
|
if (state.drag?.type === 'marquee') setSelections(state.drag.beforeSelection);
|
|
|
|
|
state.drag = null; render();
|
|
|
|
|
}
|
|
|
|
|
canvas.addEventListener('pointercancel', cancelPointerDrag);
|
|
|
|
|
canvas.addEventListener('lostpointercapture', () => { if (state.drag) cancelPointerDrag(); });
|
|
|
|
|
canvas.addEventListener('pointerleave', () => { state.hover = null; draw(); });
|
|
|
|
|
canvas.addEventListener('contextmenu', event => event.preventDefault());
|
|
|
|
|
canvas.addEventListener('wheel', event => { event.preventDefault(); setZoom(state.zoom * (event.deltaY < 0 ? 1.1 : 1 / 1.1)); }, { passive: false });
|
|
|
|
@@ -444,24 +563,104 @@ function shiftEntry(entry, dx, dy) {
|
|
|
|
|
const clips = Array.isArray(entry[4]?.aiClips) ? entry[4].aiClips : [];
|
|
|
|
|
for (const clip of clips) for (const action of (Array.isArray(clip?.actions) ? clip.actions : [])) {
|
|
|
|
|
if (action.method === 'movePath' && Array.isArray(action.params)) action.params = action.params.map(point => {
|
|
|
|
|
const match = /^(-?\d+),(-?\d+)$/.exec(String(point));
|
|
|
|
|
const match = /^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$/.exec(String(point));
|
|
|
|
|
return match ? (Number(match[1]) + dx) + ',' + (Number(match[2]) + dy) : point;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function setZoom(value) { state.zoom = Math.max(.4, Math.min(3, value)); $('zoom-label').textContent = Math.round(state.zoom * 100) + '%'; draw(); }
|
|
|
|
|
function rotate() { const entry = selectedEntry(); if (entry) mutate(() => { entry[3] = ({ 1: 4, 4: 3, 3: 2, 2: 1 })[entry[3]]; }); }
|
|
|
|
|
function remove() { if (state.selected) mutate(() => { state.level[state.selected.collection].splice(state.selected.index, 1); state.selected = null; }); }
|
|
|
|
|
function rotate(clockwise = true) {
|
|
|
|
|
const items = selections();
|
|
|
|
|
if (state.busy || state.view !== 'source') return;
|
|
|
|
|
const turns = clockwise ? { 1: 4, 4: 3, 3: 2, 2: 1 } : { 1: 2, 2: 3, 3: 4, 4: 1 };
|
|
|
|
|
if (state.material) {
|
|
|
|
|
state.materialOrientation = turns[state.materialOrientation]; render(); return;
|
|
|
|
|
}
|
|
|
|
|
if (!items.length) return;
|
|
|
|
|
if (pendingExtra()) applyExtra();
|
|
|
|
|
const dragged = state.drag?.type === 'object' ? state.drag : null;
|
|
|
|
|
const candidate = clone(state.copyPreview?.candidate || (dragged ? (dragged.moved ? dragged.candidate : dragged.before) : state.rotationPreview?.candidate || state.level));
|
|
|
|
|
const footprints = items.map(item => {
|
|
|
|
|
const entry = candidate[item.collection][item.index], footprint = definitionFor(entry)?.footprintsByOrientation[entry[3]];
|
|
|
|
|
if (!footprint?.cells?.length) throw new Error('旋转已取消:对象占格无效');
|
|
|
|
|
return { item, entry, footprint };
|
|
|
|
|
});
|
|
|
|
|
// 多选以全部占格的包围盒中心旋转,格子按中心坐标变换,不取整或移动旋转中心。
|
|
|
|
|
const cells = footprints.flatMap(({ entry, footprint }) => footprint.cells.map(([x, y]) => [entry[1] + x, entry[2] + y]));
|
|
|
|
|
const cx = (Math.min(...cells.map(cell => cell[0])) + Math.max(...cells.map(cell => cell[0])) + 1) / 2;
|
|
|
|
|
const cy = (Math.min(...cells.map(cell => cell[1])) + Math.max(...cells.map(cell => cell[1])) + 1) / 2;
|
|
|
|
|
const rotateCell = (x, y) => {
|
|
|
|
|
const dx = x + .5 - cx, dy = y + .5 - cy;
|
|
|
|
|
const point = clockwise ? [cx - dy - .5, cy + dx - .5] : [cx + dy - .5, cy - dx - .5];
|
|
|
|
|
if (!point.every(Number.isFinite)) throw new Error('旋转已取消:旋转坐标无效');
|
|
|
|
|
return point;
|
|
|
|
|
};
|
|
|
|
|
for (const { entry, footprint } of footprints) {
|
|
|
|
|
const nextOrientation = turns[entry[3]], nextFootprint = definitionFor(entry)?.footprintsByOrientation[nextOrientation];
|
|
|
|
|
if (!nextFootprint?.cells?.length) throw new Error('旋转已取消:目标朝向占格无效');
|
|
|
|
|
if (items.length > 1) {
|
|
|
|
|
const rotated = footprint.cells.map(([x, y]) => rotateCell(entry[1] + x, entry[2] + y));
|
|
|
|
|
entry[1] = Math.min(...rotated.map(cell => cell[0])) - Math.min(...nextFootprint.cells.map(cell => cell[0]));
|
|
|
|
|
entry[2] = Math.min(...rotated.map(cell => cell[1])) - Math.min(...nextFootprint.cells.map(cell => cell[1]));
|
|
|
|
|
const occupied = new Set(rotated.map(cell => cell.join(',')));
|
|
|
|
|
if (occupied.size !== nextFootprint.cells.length || nextFootprint.cells.some(([x, y]) => !occupied.has((entry[1] + x) + ',' + (entry[2] + y)))) throw new Error('旋转已取消:目标朝向占格与旋转结果不一致');
|
|
|
|
|
for (const clip of (Array.isArray(entry[4]?.aiClips) ? entry[4].aiClips : [])) for (const action of (Array.isArray(clip?.actions) ? clip.actions : [])) {
|
|
|
|
|
if (action.method === 'movePath' && Array.isArray(action.params)) action.params = action.params.map(point => {
|
|
|
|
|
const match = /^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$/.exec(String(point));
|
|
|
|
|
if (!match) throw new Error('旋转已取消:移动路径坐标无效');
|
|
|
|
|
return rotateCell(Number(match[1]), Number(match[2])).join(',');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
entry[3] = nextOrientation;
|
|
|
|
|
}
|
|
|
|
|
const problems = localIssues(candidate);
|
|
|
|
|
state.drag = null;
|
|
|
|
|
if (state.copyPreview) {
|
|
|
|
|
Object.assign(state.copyPreview, { base: clone(candidate), candidate, valid: !problems.length,
|
|
|
|
|
start: state.copyPreview.point || state.copyPreview.start });
|
|
|
|
|
render(); return;
|
|
|
|
|
}
|
|
|
|
|
if (problems.length) {
|
|
|
|
|
state.rotationPreview = { candidate }; render();
|
|
|
|
|
message('位置不合法,可继续旋转或拖动红色选区;其他操作将取消临时调整', true); return;
|
|
|
|
|
}
|
|
|
|
|
state.rotationPreview = null;
|
|
|
|
|
mutate(() => { state.level = candidate; });
|
|
|
|
|
render(); message('旋转位置合法,已应用;尚未保存的修改请保存');
|
|
|
|
|
}
|
|
|
|
|
function remove() {
|
|
|
|
|
const items = selections();
|
|
|
|
|
if (!items.length || state.busy || state.drag || state.view !== 'source') return;
|
|
|
|
|
if (items.length > 1 && !confirm('将删除选中的 ' + items.length + ' 个对象,是否继续?')) return;
|
|
|
|
|
if (items.length) mutate(() => {
|
|
|
|
|
for (const collection of ['characters', 'aoes']) for (const item of items.filter(item => item.collection === collection).sort((a, b) => b.index - a.index)) state.level[collection].splice(item.index, 1);
|
|
|
|
|
setSelections([]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
function duplicate() {
|
|
|
|
|
const entry = selectedEntry(); if (!entry || state.view !== 'source') return;
|
|
|
|
|
for (let row = 0; row < state.level.height; row++) for (let col = 0; col < state.level.width; col++) {
|
|
|
|
|
const next = clone(entry); shiftEntry(next, col - entry[1], row - entry[2]);
|
|
|
|
|
const candidate = clone(state.level); candidate[state.selected.collection].push(next);
|
|
|
|
|
if (!localIssues(candidate).length) {
|
|
|
|
|
mutate(() => { state.level[state.selected.collection].push(next); state.selected.index = state.level[state.selected.collection].length - 1; }); return;
|
|
|
|
|
if (state.busy || state.view !== 'source' || state.copyPreview || !selections().length) return;
|
|
|
|
|
if (pendingExtra()) applyExtra();
|
|
|
|
|
cancelRotationPreview();
|
|
|
|
|
const originalSelection = selections().slice(), candidate = clone(state.level), items = [];
|
|
|
|
|
for (const item of originalSelection) {
|
|
|
|
|
const entry = clone(candidate[item.collection][item.index]);
|
|
|
|
|
items.push({ collection: item.collection, index: candidate[item.collection].length });
|
|
|
|
|
candidate[item.collection].push(entry);
|
|
|
|
|
}
|
|
|
|
|
const entries = items.map(item => candidate[item.collection][item.index]);
|
|
|
|
|
const start = state.hover ? { ...state.hover } : { col: Math.min(...entries.map(entry => entry[1])), row: Math.min(...entries.map(entry => entry[2])) };
|
|
|
|
|
state.copyPreview = { base: clone(candidate), candidate, items, start, originalSelection, valid: !localIssues(candidate).length };
|
|
|
|
|
state.drag = null; state.material = null; setSelections(items); render();
|
|
|
|
|
message('已复制 ' + items.length + ' 个对象,移动鼠标后单击合法位置放置;右键取消');
|
|
|
|
|
}
|
|
|
|
|
throw new Error('棋盘内没有可复制的空位');
|
|
|
|
|
// 副本仅存在于临时布局,单击合法位置后才一次性写入并生成撤销记录。
|
|
|
|
|
function updateCopyPreview(point) {
|
|
|
|
|
const preview = state.copyPreview;
|
|
|
|
|
if (!preview) return;
|
|
|
|
|
const candidate = clone(preview.base), dx = point.col - preview.start.col, dy = point.row - preview.start.row;
|
|
|
|
|
for (const item of preview.items) shiftEntry(candidate[item.collection][item.index], dx, dy);
|
|
|
|
|
Object.assign(preview, { candidate, point: { ...point }, valid: !localIssues(candidate).length });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function history(direction) {
|
|
|
|
@@ -479,7 +678,7 @@ $('level-number').onkeydown = guarded(event => { if (event.key === 'Enter') retu
|
|
|
|
|
$('save').onclick = guarded(save);
|
|
|
|
|
$('validate').onclick = guarded(async () => { if (pendingExtra()) applyExtra(); const result = await validate(); if (result) message(result.issues.length ? '校验发现问题' : '布局校验通过', Boolean(result.issues.length)); });
|
|
|
|
|
$('undo').onclick = () => history('undo'); $('redo').onclick = () => history('redo');
|
|
|
|
|
$('rotate').onclick = guarded(rotate); $('delete').onclick = guarded(remove); $('duplicate').onclick = guarded(duplicate);
|
|
|
|
|
$('rotate').onclick = guarded(() => rotate(true)); $('delete').onclick = guarded(remove); $('duplicate').onclick = guarded(duplicate);
|
|
|
|
|
$('zoom-in').onclick = () => setZoom(state.zoom * 1.15); $('zoom-out').onclick = () => setZoom(state.zoom / 1.15);
|
|
|
|
|
$('fit').onclick = () => { state.pan = { x: 0, y: 0 }; setZoom(1); };
|
|
|
|
|
$('select-tool').onclick = () => { state.material = null; renderLibrary(); render(); };
|
|
|
|
@@ -551,9 +750,12 @@ document.addEventListener('keydown', guarded(event => {
|
|
|
|
|
if (state.busy) return;
|
|
|
|
|
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') { event.preventDefault(); return save(); }
|
|
|
|
|
if (event.target.matches('input,textarea,select')) return;
|
|
|
|
|
if ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === 'c' && selections().length && state.view === 'source') {
|
|
|
|
|
event.preventDefault(); if (!event.repeat) duplicate(); return;
|
|
|
|
|
}
|
|
|
|
|
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'z') { event.preventDefault(); history(event.shiftKey ? 'redo' : 'undo'); }
|
|
|
|
|
if (event.key === 'Delete' || event.key === 'Backspace') { event.preventDefault(); remove(); }
|
|
|
|
|
if (event.key.toLowerCase() === 'r') rotate();
|
|
|
|
|
if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.repeat && ['r', 'e'].includes(event.key.toLowerCase())) { event.preventDefault(); rotate(event.key.toLowerCase() === 'r'); }
|
|
|
|
|
if (event.key === 'Escape') { state.material = null; state.drag = null; state.selected = null; renderLibrary(); render(); }
|
|
|
|
|
}));
|
|
|
|
|
// 切换选择或执行工具前提交参数;非法 JSON 留在原处,不静默丢弃。
|
|
|
|
@@ -564,6 +766,32 @@ function commitPendingExtra(event) {
|
|
|
|
|
}
|
|
|
|
|
document.addEventListener('pointerdown', commitPendingExtra, true);
|
|
|
|
|
document.addEventListener('click', commitPendingExtra, true);
|
|
|
|
|
function discardRotationForOtherAction(event) {
|
|
|
|
|
if (!state.rotationPreview) return;
|
|
|
|
|
if (event.type === 'keydown' && ['Control', 'Meta', 'Shift', 'Alt'].includes(event.key)) return;
|
|
|
|
|
if (event.type === 'keydown' && !event.ctrlKey && !event.metaKey && !event.altKey
|
|
|
|
|
&& ['r', 'e'].includes(event.key.toLowerCase()) && !event.target.matches('input,textarea,select')) return;
|
|
|
|
|
if (['pointerdown', 'click'].includes(event.type) && event.target.closest?.('#rotate')) return;
|
|
|
|
|
if (event.target === canvas) {
|
|
|
|
|
if (event.type === 'click') return;
|
|
|
|
|
const point = gridPoint(event), entity = point && hit(point);
|
|
|
|
|
if (event.type === 'pointerdown' && event.button === 0 && !event.ctrlKey && !event.metaKey && entity && selections().some(item => sameSelection(item, entity))) return;
|
|
|
|
|
}
|
|
|
|
|
cancelRotationPreview();
|
|
|
|
|
}
|
|
|
|
|
for (const type of ['pointerdown', 'click', 'change', 'input', 'wheel', 'contextmenu', 'keydown']) document.addEventListener(type, discardRotationForOtherAction, true);
|
|
|
|
|
function discardCopyForOtherAction(event) {
|
|
|
|
|
if (!state.copyPreview) return;
|
|
|
|
|
if (event.type === 'keydown') {
|
|
|
|
|
if (['Control', 'Meta', 'Shift', 'Alt'].includes(event.key)) return;
|
|
|
|
|
if (!event.target.matches('input,textarea,select') && ((!event.ctrlKey && !event.metaKey && !event.altKey && ['r', 'e'].includes(event.key.toLowerCase()))
|
|
|
|
|
|| ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === 'c'))) return;
|
|
|
|
|
}
|
|
|
|
|
if (['pointerdown', 'click'].includes(event.type) && event.target.closest?.('#rotate,#duplicate')) return;
|
|
|
|
|
if (event.target === canvas && (event.type === 'click' || event.type === 'pointerdown' && event.button === 0)) return;
|
|
|
|
|
cancelCopyPreview();
|
|
|
|
|
}
|
|
|
|
|
for (const type of ['pointerdown', 'click', 'change', 'input', 'wheel', 'contextmenu', 'keydown']) document.addEventListener(type, discardCopyForOtherAction, true);
|
|
|
|
|
window.addEventListener('beforeunload', event => { if (dirty() || pendingExtra()) { event.preventDefault(); event.returnValue = ''; } });
|
|
|
|
|
new ResizeObserver(draw).observe($('viewport'));
|
|
|
|
|
guarded(async () => {
|
|
|
|
|