Skip to content

Commit 5380894

Browse files
committed
♻️ 优化代码结构
1 parent dc96b88 commit 5380894

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

app/src/core/service/controlService/controller/concrete/ControllerEntityCreate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Vector } from "../../../../dataStruct/Vector";
22
import { Renderer } from "../../../../render/canvas2d/renderer";
3-
import { Stage } from "../../../../stage/Stage";
3+
import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
44
import { SectionMethods } from "../../../../stage/stageManager/basicMethods/SectionMethods";
55
import { StageNodeAdder } from "../../../../stage/stageManager/concreteMethods/stageNodeAdder";
66
import { StageManager } from "../../../../stage/stageManager/StageManager";
@@ -19,7 +19,7 @@ ControllerEntityCreate.mouseDoubleClick = (event: MouseEvent) => {
1919
if (!(event.button === 0)) {
2020
return;
2121
}
22-
if (Stage.drawingMachine.isUsing) {
22+
if (Stage.leftMouseMode === LeftMouseModeEnum.draw) {
2323
// 绘制模式不能使用创建节点
2424
return;
2525
}

app/src/core/service/controlService/controller/concrete/ControllerPenStrokeDrawing.tsx

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@ class ControllerDrawingClass extends ControllerClass {
1818

1919
public currentStroke: PenStrokeSegment[] = [];
2020

21-
public get isUsing() {
22-
return this._isUsing;
23-
}
24-
public shutDown() {
25-
this._isUsing = false;
26-
this.currentStroke = [];
27-
// 鼠标提示
28-
Controller.setCursorNameHook(CursorNameEnum.Default);
29-
}
30-
public open() {
31-
this._isUsing = true;
32-
// 鼠标提示
33-
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
34-
}
35-
3621
private autoFillPenStrokeColorEnable = false;
3722
private autoFillPenStrokeColor: Color = Color.Transparent;
3823

@@ -60,70 +45,68 @@ class ControllerDrawingClass extends ControllerClass {
6045
private recordLocation: Vector[] = [];
6146

6247
public mousedown: (event: MouseEvent) => void = (event: MouseEvent) => {
63-
if (!this._isUsing) return;
64-
if (!(event.button === 0)) {
48+
if (Stage.leftMouseMode !== LeftMouseModeEnum.draw) {
49+
return;
50+
}
51+
if (!(event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw)) {
6552
return;
6653
}
54+
this._isUsing = true;
6755
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
68-
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
69-
this.recordLocation.push(pressWorldLocation.clone());
56+
this.recordLocation.push(pressWorldLocation.clone());
7057

71-
this.lastMoveLocation = pressWorldLocation.clone();
58+
this.lastMoveLocation = pressWorldLocation.clone();
7259

73-
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
74-
}
60+
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
7561
};
7662

7763
public mousemove = (event: MouseEvent) => {
7864
if (!this._isUsing) return;
79-
if (!Controller.isMouseDown[0]) {
65+
if (!Controller.isMouseDown[0] && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
8066
return;
8167
}
8268
const worldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
83-
if (Controller.isMouseDown[0] && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
84-
// 检测:如果移动距离不超过10,则不记录
85-
if (worldLocation.distance(this.lastMoveLocation) < 5) {
86-
return;
87-
}
88-
this.recordLocation.push(worldLocation.clone());
89-
90-
// 记录笔刷
91-
this.currentStroke.push(new PenStrokeSegment(this.lastMoveLocation, worldLocation, this.currentStrokeWidth));
92-
this.lastMoveLocation = worldLocation.clone();
69+
// 检测:如果移动距离不超过10,则不记录
70+
if (worldLocation.distance(this.lastMoveLocation) < 5) {
71+
return;
9372
}
73+
this.recordLocation.push(worldLocation.clone());
74+
75+
// 记录笔刷
76+
this.currentStroke.push(new PenStrokeSegment(this.lastMoveLocation, worldLocation, this.currentStrokeWidth));
77+
this.lastMoveLocation = worldLocation.clone();
9478
};
9579

9680
public mouseup = (event: MouseEvent) => {
9781
if (!this._isUsing) return;
98-
if (!(event.button === 0)) {
82+
if (!(event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw)) {
9983
return;
10084
}
101-
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
102-
const releaseWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
103-
this.recordLocation.push(releaseWorldLocation.clone());
104-
105-
// 生成笔触
106-
const strokeStringList: string[] = [];
107-
for (const location of this.recordLocation) {
108-
strokeStringList.push(`${Math.round(location.x)},${Math.round(location.y)},${this.currentStrokeWidth}`);
109-
}
110-
const contentString = strokeStringList.join("~");
111-
112-
const stroke = new PenStroke({
113-
type: "core:pen_stroke",
114-
content: contentString,
115-
color: this.getCurrentStrokeColor().toArray(),
116-
uuid: v4(),
117-
location: [0, 0],
118-
details: "",
119-
});
120-
stroke.setColor(this.getCurrentStrokeColor());
121-
StageManager.addPenStroke(stroke);
122-
this.recordLocation = [];
123-
this.currentStroke = [];
124-
125-
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
85+
const releaseWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
86+
this.recordLocation.push(releaseWorldLocation.clone());
87+
88+
// 生成笔触
89+
const strokeStringList: string[] = [];
90+
for (const location of this.recordLocation) {
91+
strokeStringList.push(`${Math.round(location.x)},${Math.round(location.y)},${this.currentStrokeWidth}`);
12692
}
93+
const contentString = strokeStringList.join("~");
94+
95+
const stroke = new PenStroke({
96+
type: "core:pen_stroke",
97+
content: contentString,
98+
color: this.getCurrentStrokeColor().toArray(),
99+
uuid: v4(),
100+
location: [0, 0],
101+
details: "",
102+
});
103+
stroke.setColor(this.getCurrentStrokeColor());
104+
StageManager.addPenStroke(stroke);
105+
this.recordLocation = [];
106+
this.currentStroke = [];
107+
108+
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
109+
this._isUsing = false;
127110
};
128111

129112
public getCurrentStrokeColor() {

app/src/pages/_toolbar.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ export default function Toolbar({ className = "" }: { className?: string }) {
219219
description="左键:框选和移动模式"
220220
icon={<MousePointer />}
221221
handleFunction={() => {
222-
Stage.drawingMachine.shutDown();
223222
Stage.leftMouseMode = LeftMouseModeEnum.selectAndMove;
224223
setIsSelecting(true);
225224
setIsDrawing(false);
@@ -231,7 +230,6 @@ export default function Toolbar({ className = "" }: { className?: string }) {
231230
description="左键:涂鸦模式"
232231
icon={<Pencil className="rotate-90" />}
233232
handleFunction={() => {
234-
Stage.drawingMachine.open();
235233
Stage.leftMouseMode = LeftMouseModeEnum.draw;
236234
setIsSelecting(false);
237235
setIsDrawing(true);
@@ -243,7 +241,6 @@ export default function Toolbar({ className = "" }: { className?: string }) {
243241
description="左键:连接与斩断"
244242
icon={<Slash className="rotate-90" />}
245243
handleFunction={() => {
246-
Stage.drawingMachine.open();
247244
Stage.leftMouseMode = LeftMouseModeEnum.connectAndCut;
248245
setIsSelecting(false);
249246
setIsDrawing(false);

0 commit comments

Comments
 (0)