|
1 | | -import { Color, Vector } from "@graphif/data-structures"; |
2 | | -import { Rectangle } from "@graphif/shapes"; |
3 | | -import { Serialized } from "@/types/node"; |
4 | 1 | import { Project } from "@/core/Project"; |
5 | | -import { SvgRenderer } from "@/core/render/canvas2d/basicRenderer/svgRenderer"; |
6 | 2 | import { ConnectableEntity } from "@/core/stage/stageObject/abstract/ConnectableEntity"; |
7 | 3 | import { CollisionBox } from "@/core/stage/stageObject/collisionBox/collisionBox"; |
| 4 | +import { Color, Vector } from "@graphif/data-structures"; |
| 5 | +import { passExtraAtArg1, passObject, serializable } from "@graphif/serializer"; |
| 6 | +import { Rectangle } from "@graphif/shapes"; |
8 | 7 |
|
9 | 8 | /** |
10 | 9 | * Svg 节点 |
11 | 10 | */ |
| 11 | +@passExtraAtArg1 |
| 12 | +@passObject |
12 | 13 | export class SvgNode extends ConnectableEntity { |
| 14 | + @serializable |
13 | 15 | color: Color = Color.Transparent; |
| 16 | + @serializable |
14 | 17 | uuid: string; |
| 18 | + @serializable |
15 | 19 | details: string; |
16 | | - scaleNumber: number; |
17 | | - public collisionBox: CollisionBox; |
| 20 | + @serializable |
| 21 | + scale: number; |
| 22 | + @serializable |
| 23 | + collisionBox: CollisionBox; |
| 24 | + @serializable |
18 | 25 | content: string; |
19 | | - location: Vector; |
20 | | - originSize: Vector; |
21 | | - state: "loading" | "loaded" | "error" = "loading"; |
| 26 | + state: "loading" | "success" = "loading"; |
22 | 27 | isHiddenBySectionCollapse: boolean = false; |
23 | 28 |
|
| 29 | + originalSize: Vector = Vector.getZero(); |
| 30 | + |
24 | 31 | constructor( |
25 | 32 | protected readonly project: Project, |
26 | 33 | { |
27 | | - uuid, |
| 34 | + uuid = crypto.randomUUID(), |
28 | 35 | details = "", |
29 | 36 | content = "", |
30 | | - location = [0, 0], |
| 37 | + collisionBox = new CollisionBox([new Rectangle(Vector.getZero(), Vector.getZero())]), |
31 | 38 | scale = 1, |
32 | | - color = [0, 0, 0, 0], |
33 | | - }: Partial<Serialized.SvgNode> & { uuid: string }, |
| 39 | + color = Color.Transparent, |
| 40 | + }, |
34 | 41 | ) { |
35 | 42 | super(); |
36 | 43 | this.uuid = uuid; |
37 | 44 | this.details = details; |
38 | | - this.scaleNumber = scale; |
| 45 | + this.scale = scale; |
39 | 46 | this.content = content; |
40 | | - this.location = new Vector(...location); |
41 | | - this.color = new Color(...color); |
42 | | - |
43 | | - this.originSize = new Vector(100, 100); |
44 | | - // 解析svg尺寸 |
45 | | - SvgRenderer.getSvgOriginalSize(content) |
46 | | - .then((size) => { |
47 | | - this.originSize = size; |
48 | | - this.collisionBox = new CollisionBox([ |
49 | | - new Rectangle(new Vector(...location), this.originSize.multiply(this.scaleNumber)), |
50 | | - ]); |
51 | | - this.state = "loaded"; |
52 | | - }) |
53 | | - .catch((error) => { |
54 | | - this.state = "error"; |
55 | | - console.error(error); |
56 | | - }); |
57 | | - |
58 | | - this.collisionBox = new CollisionBox([ |
59 | | - new Rectangle(new Vector(...location), this.originSize.multiply(this.scaleNumber)), |
60 | | - ]); |
| 47 | + this.collisionBox = collisionBox; |
| 48 | + this.color = color; |
| 49 | + // 获取SVG原始大小 |
| 50 | + this.project.svgRenderer.getSvgOriginalSize(content).then((size) => { |
| 51 | + this.originalSize = size; |
| 52 | + this.collisionBox = new CollisionBox([ |
| 53 | + new Rectangle(this.collisionBox.getRectangle().location, this.originalSize.multiply(this.scale)), |
| 54 | + ]); |
| 55 | + this.state = "success"; |
| 56 | + }); |
61 | 57 | } |
62 | 58 |
|
63 | 59 | public get geometryCenter(): Vector { |
64 | 60 | return this.collisionBox.getRectangle().center; |
65 | 61 | } |
66 | 62 |
|
67 | 63 | public scaleUpdate(scaleDiff: number) { |
68 | | - this.scaleNumber += scaleDiff; |
69 | | - if (this.scaleNumber < 0.1) { |
70 | | - this.scaleNumber = 0.1; |
| 64 | + this.scale += scaleDiff; |
| 65 | + if (this.scale < 0.1) { |
| 66 | + this.scale = 0.1; |
71 | 67 | } |
72 | | - if (this.scaleNumber > 10) { |
73 | | - this.scaleNumber = 10; |
| 68 | + if (this.scale > 10) { |
| 69 | + this.scale = 10; |
74 | 70 | } |
75 | 71 |
|
76 | | - this.collisionBox = new CollisionBox([new Rectangle(this.location, this.originSize.multiply(this.scaleNumber))]); |
| 72 | + this.collisionBox = new CollisionBox([ |
| 73 | + new Rectangle(this.collisionBox.getRectangle().location, this.originalSize.multiply(this.scale)), |
| 74 | + ]); |
77 | 75 | } |
78 | 76 |
|
79 | 77 | move(delta: Vector): void { |
80 | 78 | const newRectangle = this.collisionBox.getRectangle().clone(); |
81 | 79 | newRectangle.location = newRectangle.location.add(delta); |
82 | 80 | this.collisionBox.shapes[0] = newRectangle; |
83 | | - this.location = newRectangle.location.clone(); |
84 | 81 | this.updateFatherSectionByMove(); |
85 | 82 | } |
86 | 83 |
|
87 | 84 | moveTo(location: Vector): void { |
88 | 85 | const newRectangle = this.collisionBox.getRectangle().clone(); |
89 | 86 | newRectangle.location = location.clone(); |
90 | 87 | this.collisionBox.shapes[0] = newRectangle; |
91 | | - this.location = newRectangle.location.clone(); |
92 | 88 | this.updateFatherSectionByMove(); |
93 | 89 | } |
94 | 90 |
|
|
0 commit comments