Line data Source code
1 : import { SetTransformCommand } from '#/commands/setTransform'
2 : import { filterTopmost, findNode } from '#/document'
3 : import { CommandGroup, Dirty } from '#/history'
4 : import type { Command } from '#/history'
5 : import type { SceneNode, Transform } from '#/node'
6 : import type { Point2D as Vec2 } from '@comfyorg/shared-frontend-utils/geometry'
7 : import { getNodeKind } from '#/nodeKind'
8 : import { defaultControl } from '#/tool'
9 : import type { Overlay, Tool, ToolContext, ToolControl, ToolDef } from '#/tool'
10 : import {
11 : PICK_OPACITY_THRESHOLD,
12 : layerOpacityAt,
13 : pickLayerAt
14 : } from '#/editor/pickOps'
15 : import { addTransformBox } from './overlayBox'
16 : import { applyMove } from './transformMath'
17 :
18 : interface MoveTarget {
19 : node: SceneNode
20 : before: Transform
21 : }
22 :
23 : type Session =
24 : | { mode: 'idle' }
25 : | { mode: 'move'; start: Vec2; targets: MoveTarget[] }
26 :
27 0 : function nodeBounds(node: SceneNode): Transform {
28 0 : if (node.kind !== 'group') return node.transform
29 0 : const b = getNodeKind('group').bbox(node)
30 0 : return { x: b.x, y: b.y, w: b.w, h: b.h, rotation: 0 }
31 : }
32 :
33 0 : function moveLeaves(node: SceneNode): SceneNode[] {
34 0 : if (node.locks.position) return []
35 0 : if (node.kind !== 'group') return [node]
36 0 : return node.children.flatMap((c) => moveLeaves(c))
37 : }
38 :
39 2 : class SelectTool implements Tool {
40 : readonly control: ToolControl
41 : private session: Session = { mode: 'idle' }
42 :
43 : constructor(
44 2 : readonly id: string,
45 2 : private readonly ctx: ToolContext
46 : ) {
47 2 : this.control = {
48 : ...defaultControl(),
49 : cursor: 'default',
50 : abortMask: Dirty.STRUCTURE
51 : }
52 : }
53 :
54 : private selectedNodes(): SceneNode[] {
55 0 : const root = this.ctx.document().root
56 0 : return filterTopmost(root, this.ctx.selectedNodeIds())
57 : .map((id) => findNode(root, id)?.node)
58 : .filter((n): n is SceneNode => !!n)
59 : }
60 :
61 : private startMove(nodes: SceneNode[], pt: Vec2): void {
62 0 : const targets = nodes
63 : .flatMap((n) => moveLeaves(n))
64 : .map((node) => ({ node, before: { ...node.transform } }))
65 0 : this.session = targets.length
66 : ? { mode: 'move', start: pt, targets }
67 : : { mode: 'idle' }
68 : }
69 :
70 : onButtonPress(e: PointerEvent, pt: Vec2): void {
71 0 : if (e.shiftKey || e.ctrlKey || e.metaKey) {
72 0 : const picked = this.pick(pt)
73 0 : if (picked) {
74 0 : const sel = this.ctx.selectedNodeIds()
75 0 : const at = sel.indexOf(picked.id)
76 0 : if (at >= 0) sel.splice(at, 1)
77 0 : else sel.push(picked.id)
78 0 : this.ctx.setSelectedNodes(sel)
79 : }
80 0 : this.session = { mode: 'idle' }
81 0 : return
82 : }
83 0 : const selMask = this.ctx.selection.currentMask()
84 0 : if (selMask) {
85 0 : const mx = Math.floor(pt.x)
86 0 : const my = Math.floor(pt.y)
87 : const insideSel =
88 : mx >= 0 &&
89 : my >= 0 &&
90 : mx < selMask.width &&
91 : my < selMask.height &&
92 : selMask.data[my * selMask.width + mx] >= 0.5
93 0 : if (insideSel && this.ctx.floatSelection()) {
94 0 : this.session = { mode: 'idle' }
95 0 : return
96 : }
97 : }
98 0 : const selected = this.selectedNodes()
99 0 : if (
100 : selected.some(
101 : (n) => layerOpacityAt(n, pt, this.ctx.content) > PICK_OPACITY_THRESHOLD
102 : )
103 : ) {
104 0 : this.startMove(selected, pt)
105 0 : return
106 : }
107 0 : const picked = this.pick(pt)
108 0 : if (picked) {
109 0 : this.ctx.setActiveNode(picked.id)
110 0 : this.startMove([picked], pt)
111 : } else {
112 0 : this.ctx.setActiveNode(null)
113 0 : this.session = { mode: 'idle' }
114 : }
115 : }
116 :
117 : onMotion(_e: PointerEvent, pt: Vec2): void {
118 0 : const s = this.session
119 0 : if (s.mode !== 'move') return
120 0 : for (const t of s.targets) {
121 0 : t.node.transform = applyMove(t.before, pt.x - s.start.x, pt.y - s.start.y)
122 : }
123 0 : this.ctx.requestRender()
124 : }
125 :
126 : private commitTransform(node: SceneNode, before: Transform): Command[] {
127 0 : const after = { ...node.transform }
128 0 : if (before.x === after.x && before.y === after.y) return []
129 0 : const cmds: Command[] = [
130 : new SetTransformCommand('move', node, before, after)
131 : ]
132 0 : const extra =
133 : getNodeKind(node.kind).onTransformCommitted?.(node, before, {
134 : content: this.ctx.content
135 : }) ?? null
136 0 : if (extra) cmds.push(extra)
137 0 : return cmds
138 : }
139 :
140 : onButtonRelease(): void {
141 0 : const s = this.session
142 0 : if (s.mode === 'move') {
143 0 : const cmds = s.targets.flatMap((t) =>
144 : this.commitTransform(t.node, t.before)
145 : )
146 0 : if (cmds.length === 1) {
147 0 : this.ctx.history.push(cmds[0])
148 0 : this.ctx.requestRender()
149 0 : } else if (cmds.length > 1) {
150 0 : const group = new CommandGroup('move')
151 0 : group.children.push(...cmds)
152 0 : this.ctx.history.push(group)
153 0 : this.ctx.requestRender()
154 : }
155 : }
156 0 : this.session = { mode: 'idle' }
157 : }
158 :
159 : onHover(): void {}
160 :
161 : cursorFor(pt: Vec2): string {
162 0 : for (const n of this.selectedNodes()) {
163 0 : if (layerOpacityAt(n, pt, this.ctx.content) > PICK_OPACITY_THRESHOLD) {
164 0 : return n.locks.position ? 'not-allowed' : 'move'
165 : }
166 : }
167 0 : return 'default'
168 : }
169 :
170 : drawOverlay(overlay: Overlay): void {
171 0 : for (const n of this.selectedNodes()) {
172 0 : const box = nodeBounds(n)
173 0 : if (box.w <= 0 || box.h <= 0) continue
174 0 : addTransformBox(overlay, box, false)
175 : }
176 : }
177 :
178 : private pick(pt: Vec2): SceneNode | null {
179 0 : return pickLayerAt(this.ctx.document().root.children, pt, this.ctx.content)
180 : }
181 : }
182 :
183 2 : export function makeSelectToolDef(): ToolDef {
184 2 : return { id: 'select', create: (ctx) => new SelectTool('select', ctx) }
185 : }
|