Line data Source code
1 : import type { NodeTexture } from '#/compositor'
2 : import { generateId } from '#/id'
3 : import { defaultMode } from '#/mode'
4 : import type { NodeKind } from '#/nodeKind'
5 : import type { RasterData, Transform } from '#/node'
6 : import type {
7 : CompactRect as Rect,
8 : Point2D as Vec2
9 : } from '@comfyorg/shared-frontend-utils/geometry'
10 :
11 0 : function defaultTransform(w: number, h: number): Transform {
12 0 : return { x: 0, y: 0, w, h, rotation: 0 }
13 : }
14 :
15 2 : export const rasterKind: NodeKind<RasterData> = {
16 : kind: 'raster',
17 :
18 : create(init: Partial<RasterData> = {}): RasterData {
19 4 : const nw = init.naturalWidth ?? 512
20 4 : const nh = init.naturalHeight ?? 512
21 4 : return {
22 : kind: 'raster',
23 : id: init.id ?? generateId('layer'),
24 : name: init.name ?? 'Layer',
25 : visible: init.visible ?? true,
26 : opacity: init.opacity ?? 1,
27 : mode: init.mode ?? defaultMode('normal'),
28 : transform: init.transform ?? defaultTransform(nw, nh),
29 : locks: init.locks ?? {
30 : content: false,
31 : position: false,
32 : visibility: false
33 : },
34 : contentId: init.contentId ?? '',
35 : url: init.url,
36 : naturalWidth: nw,
37 : naturalHeight: nh,
38 : lockAlpha: init.lockAlpha ?? false,
39 : mask: init.mask
40 : }
41 : },
42 :
43 : contentIds(node: RasterData): string[] {
44 22 : const ids = [node.contentId]
45 22 : if (node.mask) ids.push(node.mask.contentId)
46 22 : return ids.filter(Boolean)
47 : },
48 :
49 : renderNode(node: RasterData, ctx): NodeTexture | null {
50 24 : const entry = ctx.content.get(node.contentId)
51 24 : if (!entry) return null
52 24 : return ctx.placed(
53 : `content:${node.id}`,
54 : node.contentId,
55 : entry.canvas,
56 : node.transform
57 : )
58 : },
59 :
60 : bbox(node: RasterData): Rect {
61 0 : return {
62 : x: node.transform.x,
63 : y: node.transform.y,
64 : w: node.transform.w,
65 : h: node.transform.h
66 : }
67 : },
68 :
69 : hitTest(node: RasterData, pt: Vec2): boolean {
70 0 : const b = this.bbox(node)
71 0 : return pt.x >= b.x && pt.x <= b.x + b.w && pt.y >= b.y && pt.y <= b.y + b.h
72 : }
73 : }
|