From 0e991dd06f923d77bbb8e7fe958e5523840477f6 Mon Sep 17 00:00:00 2001 From: junFeng Date: Tue, 24 Nov 2026 18:08:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E5=A4=9A=E9=80=89=E6=97=8B=E8=BD=AC=E5=A4=8D=E5=88=B6=E5=8F=8A?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=93=8D=E4=BD=9C=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 306 ++++++++++++++++++++++++++++++++++++++++++------ style.css | 2 + 使用说明.md | 16 +++ 编辑器.html | 2 +- 4 files changed, 286 insertions(+), 40 deletions(-) diff --git a/app.js b/app.js index a579523..32c7928 100644 --- a/app.js +++ b/app.js @@ -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 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; - } +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; } - throw new Error('棋盘内没有可复制的空位'); + 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() { + 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 + ' 个对象,移动鼠标后单击合法位置放置;右键取消'); +} +// 副本仅存在于临时布局,单击合法位置后才一次性写入并生成撤销记录。 +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 () => { diff --git a/style.css b/style.css index ec06898..af67cc4 100644 --- a/style.css +++ b/style.css @@ -63,3 +63,5 @@ header { flex-wrap: wrap; } #mainline-error { font-size: 13px; color: #b42335; min-height: 18px; overflow-wrap: anywhere; } .mainline-footer { justify-content: flex-end; border-top: 1px solid #e3e7ed; padding-top: 12px; } @media (max-width: 600px) { dialog#mainline-dialog { padding: 14px; } #mainline-list { height: 240px; } .file-actions { flex-wrap: wrap; } } +#selection-help{position:absolute;right:16px;bottom:12px;max-width:calc(100% - 32px);padding:6px 9px;border:1px solid #dce1e6;border-radius:4px;background:rgba(255,255,255,.94);color:#64727c;font-size:11px;line-height:1.7;pointer-events:none;overflow-wrap:anywhere}#selection-help span{display:block} +#selection-help{white-space:pre-line} diff --git a/使用说明.md b/使用说明.md index 7f5fb52..6442ee0 100644 --- a/使用说明.md +++ b/使用说明.md @@ -13,6 +13,22 @@ 浏览器下载位置由浏览器设置决定。交付、换电脑或升级编辑器前,务必导出 JSON 备份;浏览器工作区不是文件备份。移动文件夹、切换浏览器、无痕模式或清理浏览器数据都可能导致本机记录不可用。不要同时打开多个窗口编辑同一份离线工作区。 +## 棋盘选择 + +再次点击对象库中当前高亮的创建图标,会取消高亮和创建预览,切换回选择模式;点击另一个图标则切换到对应对象的创建模式。 + +点击对象库中的军械或机关进入创建模式,初始朝向为向右。按 R 顺时针旋转创建预览,按 E 逆时针旋转,也可点击工具栏旋转按钮。点击棋盘会按当前预览朝向放置,连续放置保持该朝向;重新选择对象库条目时重置为向右。预览旋转不修改已放置对象,也不会产生草稿或撤销记录;不合法的位置仍不可放置。固定直立图标的图片外观不旋转,但对象的朝向及占格按配置变化。 + +选择工具下,单击对象选中,再次单击已选对象取消选择;按住 Ctrl 单击可逐个加入或移出选择(Mac 可使用 Command)。单击空白处或按 Esc 清空选择,Ctrl 单击空白处保留选择。 + +按住 Ctrl(Mac 为 Command)并用鼠标左键拖动可拉框选择,从空白或对象上起拖均可。框碰到对象的任一占格,即选中该对象;松手以本次框选结果替换原选择,空框清空选择。拖动过程中实时高亮,松开 Ctrl 不会中断已开始的框选;未拖动时仍按 Ctrl 单击处理。框选只改变选择,不修改关卡内容;局内预览也可框选,但仍不可编辑。 + +拖动已选对象可整组移动,普通移动越界或军械重叠时整组取消移动。按 R 顺时针旋转 90 度,按 E 逆时针旋转 90 度;工具栏旋转按钮为顺时针。单选保持原占格锚点并调整朝向,多选绕全部选中占格的包围盒中心整体旋转,移动路径同步旋转。选中对象始终显示在其他棋盘对象上方。旋转后出现半格、越界或军械重叠时显示红色临时选区,仍可用 R、E 或旋转按钮基于当前显示状态继续旋转,也可点击其中任一选中对象整组拖动并按格吸附;位置合法后应用,仍不合法则保留红色等待调整。拖动过程中按旋转键会结束本次拖动,并基于当前拖动位置旋转。除继续旋转、拖动外,执行其他操作会先取消临时调整并恢复原合法布局,再执行该操作。临时预览不会写入保存或草稿,连续非法旋转及拖动调整到合法位置作为一次修改撤销;占格形状不兼容等无法构造预览的错误仍直接取消。局内预览只读、保存处理中及未选择对象时不允许旋转。 + +单选或多选后点击复制按钮,或按 Ctrl+C(Mac 为 Command+C),会生成并选中整组临时副本。副本无需按住鼠标即可跟随移动,按格吸附并保持相对位置,移动路径同步平移;点击复制按钮时以整组左上角跟随鼠标,快捷键复制则保持鼠标与选区的位置关系。副本可继续按 R/E 旋转,红色表示当前不能放置;单击合法位置后才写入关卡,并保留对副本的选择。原对象始终不变,完成整组复制可一次撤销/重做。Esc 或切换其他操作会取消未放置的副本,不写入保存或草稿;复制过程中再次复制不会重复生成副本。输入框内 Ctrl+C 仍复制文字。 + +删除按钮或 Delete 删除全部选中对象,单选直接删除,多选必须再次确认;旋转、移动和删除均可撤销/重做。多选时右侧显示选中数量,单对象属性编辑仅在单选时启用。局内预览仍为只读,不允许复制、旋转、移动或删除。 + ## 对象附加配置 选中棋盘对象后,在右侧属性面板配置对应参数:移动速度、木栅栏初始升降、巡逻路径、炸弹后备倒计时、封印锁后备血量。数字框留空恢复默认;路径点使用列、行整数坐标,可添加或删除,至少保留两点,点击“应用路径”。待修改路径也会在保存、单关导出或整包导出时应用。 diff --git a/编辑器.html b/编辑器.html index 13734cd..ef0b67b 100644 --- a/编辑器.html +++ b/编辑器.html @@ -41,7 +41,7 @@
100%

主线关卡

源布局
-
+

布局校验

等待加载