4.3 场景工具模式切换
场景工具同一时刻只有一个活跃:选择、移动、旋转、缩放手柄,或扩展注册的自定义工具。工具可绑定快捷键,快捷键必须唯一;临时工具用栈压入,松手弹回原工具,例如按住 Alt 临时取色。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| Gizmo 与变换 | 《视图与子编辑器》 第 4 章 | 卷三实现移动/旋转/缩放手柄;本节管理工具模式与切换 |
| 命令系统 | 《架构内核》 | 内核处理命令;本节把快捷键映射到工具模式 |
| 全卷脉络 | 本卷目录 | 本节收口自定义 Gizmo 章 |
从零实现:场景工具模式切换
注册表保存工具声明与快捷键映射,两处都查重:重名工具拒绝,重复快捷键拒绝。模式机维护当前模式与瞬态栈;activate 先校验工具是否注册,再清空瞬态栈——显式切换意味着放弃此前的临时工具。pushTransient 只接受标记为瞬态的工具,pop 恢复栈顶。
对标视口扩展接口
Unity 的 ToolManager 提供设置和恢复活动工具的接口。helio 额外用栈表示临时工具的进入和退出。快捷键冲突由示例注册表检查,真实焦点和事件路由由视口宿主管理。 Unity ToolManager
可运行产物:6 条用例
下面是 src/tool-mode.ts,层级 L1,完整可编译。运行命令 node --test test/04-3-scene-tool-mode-switch.test.ts:
// file: src/tool-mode.ts
// tier: L1
// verify: node --test test/04-3-scene-tool-mode-switch.test.ts
//
// 卷四(第四章):场景工具模式切换。
// 场景工具同一时刻只有一个活跃:选择、移动、旋转、缩放手柄,或扩展注册
// 的自定义工具。工具可绑定快捷键,快捷键必须唯一;临时工具(如画刷按住
// Alt 时的取色)用栈压入,松手弹回原工具。
export interface ToolDecl {
mode: string;
shortcut: string;
transient: boolean;
}
export class ToolRegistry {
#tools = new Map<string, ToolDecl>();
#shortcuts = new Map<string, string>();
register(tool: ToolDecl): void {
if (this.#tools.has(tool.mode)) throw new Error(`工具 ${tool.mode} 已注册`);
const owner = this.#shortcuts.get(tool.shortcut);
if (owner) throw new Error(`快捷键 ${tool.shortcut} 已被 ${owner} 占用`);
this.#tools.set(tool.mode, { ...tool });
this.#shortcuts.set(tool.shortcut, tool.mode);
}
has(mode: string): boolean {
return this.#tools.has(mode);
}
isTransient(mode: string): boolean {
return this.#tools.get(mode)?.transient === true;
}
modeOfShortcut(shortcut: string): string | undefined {
return this.#shortcuts.get(shortcut);
}
count(): number {
return this.#tools.size;
}
}
export class ToolModeMachine {
#registry: ToolRegistry;
#current = 'select';
#stack: string[] = [];
constructor(registry: ToolRegistry) {
this.#registry = registry;
}
current(): string {
return this.#current;
}
activate(mode: string): void {
if (mode !== 'select' && !this.#registry.has(mode)) {
throw new Error(`未注册的工具 ${mode}`);
}
this.#stack = [];
this.#current = mode;
}
/** 压入瞬态工具,例如按住 Alt 临时取色。 */
pushTransient(mode: string): void {
if (!this.#registry.isTransient(mode)) {
throw new Error(`工具 ${mode} 不是瞬态工具`);
}
this.#stack.push(this.#current);
this.#current = mode;
}
/** 弹出瞬态工具,恢复到压栈前的工具。 */
pop(): string {
const previous = this.#stack.pop();
if (previous !== undefined) this.#current = previous;
return this.#current;
}
stackDepth(): number {
return this.#stack.length;
}
}
瞬态栈的意义是「按住期间临时改变语义,松手立即恢复」,而不是切换到另一个正式模式;因此 activate 会清空栈,避免从瞬态恢复到一个早已过期的模式。pop 在栈空时返回当前模式,调用方不需要判断是否还有截图。
下面是 test/04-3-scene-tool-mode-switch.test.ts,层级 L1:
// file: test/04-3-scene-tool-mode-switch.test.ts
// tier: L1
// verify: node --test test/04-3-scene-tool-mode-switch.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ToolRegistry, ToolModeMachine } from '../src/tool-mode.ts';
function makeRegistry() {
const r = new ToolRegistry();
r.register({ mode: 'move', shortcut: 'w', transient: false });
r.register({ mode: 'rotate', shortcut: 'e', transient: false });
r.register({ mode: 'eyedropper', shortcut: 'alt', transient: true });
return r;
}
test('默认工具是选择', () => {
const m = new ToolModeMachine(makeRegistry());
assert.equal(m.current(), 'select');
});
test('切换到已注册工具', () => {
const m = new ToolModeMachine(makeRegistry());
m.activate('move');
assert.equal(m.current(), 'move');
m.activate('select');
assert.equal(m.current(), 'select');
});
test('未注册工具无法激活', () => {
const m = new ToolModeMachine(makeRegistry());
assert.throws(() => m.activate('sculpt'), /未注册的工具/);
assert.equal(m.current(), 'select');
});
test('快捷键必须唯一', () => {
const r = makeRegistry();
assert.throws(
() => r.register({ mode: 'pan', shortcut: 'w', transient: false }),
/快捷键 w 已被 move 占用/,
);
assert.equal(r.modeOfShortcut('w'), 'move');
assert.equal(r.count(), 3);
});
test('瞬态工具压栈后弹回原工具', () => {
const m = new ToolModeMachine(makeRegistry());
m.activate('rotate');
m.pushTransient('eyedropper');
assert.equal(m.current(), 'eyedropper');
assert.equal(m.stackDepth(), 1);
assert.equal(m.pop(), 'rotate');
assert.equal(m.stackDepth(), 0);
});
test('非瞬态工具不能压栈,激活清空栈', () => {
const m = new ToolModeMachine(makeRegistry());
assert.throws(() => m.pushTransient('move'), /不是瞬态工具/);
m.activate('move');
m.pushTransient('eyedropper');
m.activate('rotate');
assert.equal(m.stackDepth(), 0);
assert.equal(m.current(), 'rotate');
});
本机实测(Node v26.5.0,node --test test/04-3-scene-tool-mode-switch.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉默认模式与切换;第三条钉未注册工具;第四条钉快捷键唯一;后两条钉瞬态栈与激活清栈。
小结
- 同一时刻只有一个活跃工具,模式机保证这一点。
- 快捷键是全局资源,注册时去重。
- 瞬态工具用栈压入/弹出,适合按住 Alt 的临时操作。
- 显式激活会清空瞬态栈,避免恢复到过期状态。
阅读导航:上一节:4.2 视口叠加与工具 · 下一节:5.1 API 设计与上下文 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。