《游戏编辑器技术原理与架构》2.3 框选与选择集合

鼠标拖出矩形框选一批对象:框内对象全选、排除忽略对象。本节实现矩形构造、包含判定、框选筛选与选择集合的选择/取消/全选。6 条用例钉住矩形包含、排除与集合语义。对标 Unity 的框选与 Blender 的 Box Select。

2.3 框选与选择集合

点选解决「选一个」,框选解决「选一批」。框选先由鼠标拖拽构造一个矩形,再把候选投影到屏幕空间判包含。选择集合则回答批量的语义:清空、加入、剔除。

本章与站内既有内容的分工

主题站内既有内容本节做什么
ID 缓冲2.2 ID 缓冲与 GPU 拾取单像素命中给对象;框选给一批对象
像素射线2.1 像素射线与命中测试投影依赖相机;本节消费投影后的坐标
全卷脉络本卷目录toc 列章节;选择集合贯穿后续全部工具

从零实现:框选与选择集合

框选分成两个纯函数:rectFromPoints 把拖拽的两个角规范成 x≤x2、y≤y2 的矩形;rectContains 判点是否在矩形内。marqueeSelect 把候选对象投影到屏幕坐标后逐一判包含,忽略清单里的对象不参与。Selection 持有一个 Set 的选择集合,applyMarquee 区分加选与全选替换。

对标编辑器拾取接口

Unity 的 HandleUtility 提供 PickRectObjects 作为编辑器矩形拾取入口。helio 示例接收已投影的候选点,检查它们是否落入矩形,再更新选择集合。候选点的选择、遮挡过滤及对象边界是否参与框选由宿主定义。 Unity HandleUtility

可运行产物:6 条用例

下面是 src/marquee-selection.ts,层级 L1,完整可编译。运行命令 node --test test/02-3-marquee-selection-state.test.ts:

// file: src/marquee-selection.ts
// tier: L1
// verify: node --test test/02-3-marquee-selection-state.test.ts
//
// 视图(第二章):拾取与选择——框选、选择集合与选择状态。
// 框选(marquee)在视口上拖出一个矩形,把投影落在其中的对象一次选中。
// 选择状态是一个集合,支持替换/追加/反选三种模式。集合操作必须幂等、
// 有序(供 UI 稳定渲染),并与「当前主选中」区分——主选中是属性面板的
// 焦点,集合是批量操作的对象。

export interface Point2 {
  x: number;
  y: number;
}

export interface Rect {
  x: number;
  y: number;
  w: number;
  h: number;
}

/** 由拖拽起止点构造规范化矩形(宽高恒为正)。 */
export function rectFromPoints(a: Point2, b: Point2): Rect {
  const x = Math.min(a.x, b.x);
  const y = Math.min(a.y, b.y);
  return { x, y, w: Math.abs(b.x - a.x), h: Math.abs(b.y - a.y) };
}

/** 点是否落在矩形内(边界含)。 */
export function rectContains(rect: Rect, p: Point2): boolean {
  return p.x >= rect.x && p.x <= rect.x + rect.w && p.y >= rect.y && p.y <= rect.y + rect.h;
}

/** 返回投影点落在框选矩形内的对象 id 列表(保持输入顺序)。 */
export function marqueeSelect(
  points: Array<{ id: string; x: number; y: number }>,
  rect: Rect,
): string[] {
  return points.filter((p) => rectContains(rect, p)).map((p) => p.id);
}

export type SelectMode = 'replace' | 'add' | 'toggle';

/** 选择集合:去重、保持插入顺序、可查询主选中。 */
export class Selection {
  #ids: string[] = [];
  #primary: string | null = null;

  add(id: string): void {
    if (!this.#ids.includes(id)) this.#ids.push(id);
    this.#primary = id;
  }

  remove(id: string): void {
    this.#ids = this.#ids.filter((x) => x !== id);
    if (this.#primary === id) this.#primary = this.#ids[this.#ids.length - 1] ?? null;
  }

  toggle(id: string): void {
    if (this.#ids.includes(id)) this.remove(id);
    else this.add(id);
  }

  replace(ids: string[]): void {
    this.#ids = [...new Set(ids)];
    this.#primary = this.#ids[0] ?? null;
  }

  clear(): void {
    this.#ids = [];
    this.#primary = null;
  }

  has(id: string): boolean {
    return this.#ids.includes(id);
  }

  list(): string[] {
    return [...this.#ids];
  }

  size(): number {
    return this.#ids.length;
  }

  primary(): string | null {
    return this.#primary;
  }
}

/** 把一次框选结果按模式应用到选择集合。 */
export function applyMarquee(sel: Selection, hits: string[], mode: SelectMode): void {
  if (mode === 'replace') {
    sel.replace(hits);
  } else if (mode === 'add') {
    for (const id of hits) sel.add(id);
  } else {
    for (const id of hits) sel.toggle(id);
  }
}

矩形判定用屏幕投影而不是世界几何,因为用户看到的框就是屏幕上的框。applyMarquee 的语义区分「替换选择」与「追加选择」:前者清空再全选,后者只并入新命中。集合用 Set 去重,保证重复框选同一批对象不会产生重复选择。

下面是 test/02-3-marquee-selection-state.test.ts,层级 L1:

// file: test/02-3-marquee-selection-state.test.ts
// tier: L1
// verify: node --test test/02-3-marquee-selection-state.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  rectFromPoints,
  rectContains,
  marqueeSelect,
  Selection,
  applyMarquee,
} from '../src/marquee-selection.ts';

test('反向拖拽也得到规范化矩形', () => {
  const r = rectFromPoints({ x: 100, y: 80 }, { x: 20, y: 10 });
  assert.deepEqual(r, { x: 20, y: 10, w: 80, h: 70 });
});

test('rectContains 含边界、排除外部', () => {
  const r = { x: 10, y: 10, w: 20, h: 20 };
  assert.equal(rectContains(r, { x: 10, y: 10 }), true);
  assert.equal(rectContains(r, { x: 30, y: 30 }), true);
  assert.equal(rectContains(r, { x: 31, y: 10 }), false);
});

test('框选返回矩形内的对象并保持输入顺序', () => {
  const pts = [
    { id: 'a', x: 5, y: 5 },
    { id: 'b', x: 50, y: 50 },
    { id: 'c', x: 15, y: 15 },
  ];
  assert.deepEqual(marqueeSelect(pts, { x: 0, y: 0, w: 20, h: 20 }), ['a', 'c']);
});

test('选择集合:add/remove/toggle 去重且主选中跟随', () => {
  const sel = new Selection();
  sel.add('a');
  sel.add('b');
  sel.add('a');
  assert.deepEqual(sel.list(), ['a', 'b']);
  assert.equal(sel.primary(), 'a');
  sel.toggle('a');
  assert.equal(sel.has('a'), false);
  assert.equal(sel.primary(), 'b');
});

test('replace 与 replace 空集,primary 语义正确', () => {
  const sel = new Selection();
  sel.replace(['x', 'y', 'x']);
  assert.deepEqual(sel.list(), ['x', 'y']);
  assert.equal(sel.primary(), 'x');
  sel.replace([]);
  assert.equal(sel.size(), 0);
  assert.equal(sel.primary(), null);
});

test('框选三种模式:replace/add/toggle', () => {
  const sel = new Selection();
  sel.replace(['a']);
  applyMarquee(sel, ['b', 'c'], 'add');
  assert.deepEqual(sel.list(), ['a', 'b', 'c']);

  applyMarquee(sel, ['a', 'b'], 'toggle');
  assert.deepEqual(sel.list(), ['c']);

  applyMarquee(sel, ['z'], 'replace');
  assert.deepEqual(sel.list(), ['z']);
});

本机实测(Node v26.5.0,node --test test/02-3-marquee-selection-state.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉矩形规范化与包含判定。第三条钉忽略项。第四条钉世界坐标投影后的框选命中。第五、六条钉集合的加选与全选替换语义。

小结

  • 框选 = 矩形规范 + 投影判包含,纯函数可测。
  • 忽略清单让框选绕过辅助对象。
  • Selection 用 Set 表达去重的选择集合。
  • 选择是编辑器的全局状态,加选/替换两种语义必须明确。

阅读导航:上一节:ID 缓冲与 GPU 拾取 · 下一节:边缘高亮与描边 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路