Skip to content

Commit d187354

Browse files
committed
✨ 涂鸦增加直线绘制方式
1 parent 5380894 commit d187354

File tree

2 files changed

+95
-25
lines changed

2 files changed

+95
-25
lines changed

app/src/core/render/canvas2d/renderer.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,28 @@ export namespace Renderer {
494494
const currentStrokeColor = Stage.drawingMachine.getCurrentStrokeColor();
495495

496496
if (Stage.leftMouseMode === LeftMouseModeEnum.draw) {
497+
// 画鼠标绘制过程,还未抬起鼠标左键的 笔迹
498+
if (Stage.drawingMachine.currentStroke.length > 0) {
499+
if (Controller.pressingKeySet.has("shift")) {
500+
CurveRenderer.renderSolidLine(
501+
transformWorld2View(Stage.drawingMachine.currentStroke[0].startLocation),
502+
MouseLocation.vector(),
503+
currentStrokeColor.a === 0 ? StageStyleManager.currentStyle.StageObjectBorder : currentStrokeColor,
504+
Stage.drawingMachine.currentStrokeWidth * Camera.currentScale,
505+
);
506+
} else {
507+
for (const segment of Stage.drawingMachine.currentStroke) {
508+
CurveRenderer.renderSolidLine(
509+
transformWorld2View(segment.startLocation),
510+
transformWorld2View(segment.endLocation),
511+
currentStrokeColor.a === 0 ? StageStyleManager.currentStyle.StageObjectBorder : currentStrokeColor,
512+
Stage.drawingMachine.currentStrokeWidth * Camera.currentScale,
513+
);
514+
}
515+
}
516+
}
497517
if (Stage.drawingControlMachine.isAdjusting) {
518+
// 鼠标正在调整状态
498519
ShapeRenderer.renderCircle(
499520
transformWorld2View(Stage.drawingControlMachine.startAdjustWidthLocation),
500521
(Stage.drawingMachine.currentStrokeWidth / 2) * Camera.currentScale,
@@ -522,16 +543,28 @@ export namespace Renderer {
522543
0,
523544
);
524545
}
525-
}
526-
527-
// 画笔
528-
if (Stage.drawingMachine.currentStroke.length > 0) {
529-
for (const segment of Stage.drawingMachine.currentStroke) {
546+
// 如果按下shift键,说明正在画直线
547+
if (Controller.pressingKeySet.has("shift")) {
548+
// 画一个跟随鼠标的十字准星
549+
const crossSize = 100;
550+
const crossCenter = MouseLocation.vector();
551+
const crossLine1Start = crossCenter.add(new Vector(-crossSize, 0));
552+
const crossLine1End = crossCenter.add(new Vector(crossSize, 0));
553+
const crossLine2Start = crossCenter.add(new Vector(0, -crossSize));
554+
const crossLine2End = crossCenter.add(new Vector(0, crossSize));
555+
CurveRenderer.renderSolidLine(crossLine1Start, crossLine1End, StageStyleManager.currentStyle.Background, 2);
556+
CurveRenderer.renderSolidLine(crossLine2Start, crossLine2End, StageStyleManager.currentStyle.Background, 2);
530557
CurveRenderer.renderSolidLine(
531-
transformWorld2View(segment.startLocation),
532-
transformWorld2View(segment.endLocation),
533-
currentStrokeColor.a === 0 ? StageStyleManager.currentStyle.StageObjectBorder : currentStrokeColor,
534-
Stage.drawingMachine.currentStrokeWidth * Camera.currentScale,
558+
crossLine1Start,
559+
crossLine1End,
560+
StageStyleManager.currentStyle.effects.successShadow,
561+
0.5,
562+
);
563+
CurveRenderer.renderSolidLine(
564+
crossLine2Start,
565+
crossLine2End,
566+
StageStyleManager.currentStyle.effects.successShadow,
567+
0.5,
535568
);
536569
}
537570
}

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

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
1313
* 涂鸦功能
1414
*/
1515
class ControllerDrawingClass extends ControllerClass {
16-
// 一开始是禁用状态
1716
private _isUsing: boolean = false;
1817

18+
/** 在移动的过程中,记录这一笔画的笔迹 */
1919
public currentStroke: PenStrokeSegment[] = [];
2020

2121
private autoFillPenStrokeColorEnable = false;
2222
private autoFillPenStrokeColor: Color = Color.Transparent;
2323

24+
/**
25+
* 初始按下的起始点的位置
26+
*/
27+
public pressStartWordLocation = Vector.getZero();
28+
/** 当前是否是在绘制直线 */
29+
public isDrawingLine = false;
30+
2431
/**
2532
* 当前画笔的粗度
2633
*/
@@ -52,7 +59,12 @@ class ControllerDrawingClass extends ControllerClass {
5259
return;
5360
}
5461
this._isUsing = true;
62+
5563
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
64+
if (Controller.pressingKeySet.has("shift")) {
65+
this.isDrawingLine = true;
66+
}
67+
this.pressStartWordLocation = pressWorldLocation.clone();
5668
this.recordLocation.push(pressWorldLocation.clone());
5769

5870
this.lastMoveLocation = pressWorldLocation.clone();
@@ -86,27 +98,52 @@ class ControllerDrawingClass extends ControllerClass {
8698
this.recordLocation.push(releaseWorldLocation.clone());
8799

88100
// 生成笔触
89-
const strokeStringList: string[] = [];
90-
for (const location of this.recordLocation) {
91-
strokeStringList.push(`${Math.round(location.x)},${Math.round(location.y)},${this.currentStrokeWidth}`);
101+
if (Controller.pressingKeySet.has("shift")) {
102+
// 直线
103+
const startLocation = this.pressStartWordLocation;
104+
const endLocation = releaseWorldLocation;
105+
const strokeStringList: string[] = [
106+
`${Math.round(startLocation.x)},${Math.round(startLocation.y)},${this.currentStrokeWidth}`,
107+
`${Math.round(endLocation.x)},${Math.round(endLocation.y)},${this.currentStrokeWidth}`,
108+
];
109+
const contentString = strokeStringList.join("~");
110+
const stroke = new PenStroke({
111+
type: "core:pen_stroke",
112+
content: contentString,
113+
color: this.getCurrentStrokeColor().toArray(),
114+
uuid: v4(),
115+
location: [0, 0],
116+
details: "",
117+
});
118+
stroke.setColor(this.getCurrentStrokeColor());
119+
StageManager.addPenStroke(stroke);
120+
} else {
121+
// 普通笔迹
122+
const strokeStringList: string[] = [];
123+
for (const location of this.recordLocation) {
124+
strokeStringList.push(`${Math.round(location.x)},${Math.round(location.y)},${this.currentStrokeWidth}`);
125+
}
126+
const contentString = strokeStringList.join("~");
127+
128+
const stroke = new PenStroke({
129+
type: "core:pen_stroke",
130+
content: contentString,
131+
color: this.getCurrentStrokeColor().toArray(),
132+
uuid: v4(),
133+
location: [0, 0],
134+
details: "",
135+
});
136+
stroke.setColor(this.getCurrentStrokeColor());
137+
StageManager.addPenStroke(stroke);
92138
}
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);
139+
140+
// 清理
105141
this.recordLocation = [];
106142
this.currentStroke = [];
107143

108144
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
109145
this._isUsing = false;
146+
this.isDrawingLine = false;
110147
};
111148

112149
public getCurrentStrokeColor() {

0 commit comments

Comments
 (0)