Skip to content

Commit 005028e

Browse files
committed
✨ 自定义三种左键功能
1 parent 6c577db commit 005028e

File tree

10 files changed

+149
-70
lines changed

10 files changed

+149
-70
lines changed

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

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

496496
if (Stage.drawingMachine.isUsing) {
497-
if (Stage.drawingMachine.isAdjusting) {
497+
if (Stage.drawingControlMachine.isAdjusting) {
498498
ShapeRenderer.renderCircle(
499-
transformWorld2View(Stage.drawingMachine.startAdjustWidthLocation),
499+
transformWorld2View(Stage.drawingControlMachine.startAdjustWidthLocation),
500500
(Stage.drawingMachine.currentStrokeWidth / 2) * Camera.currentScale,
501501
currentStrokeColor.a === 0 ? StageStyleManager.currentStyle.StageObjectBorder : currentStrokeColor,
502502
Color.Transparent,

app/src/core/service/controlService/controller/Controller.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { ControllerSectionEdit } from "./concrete/ControllerSectionEdit";
2121
// ...
2222
import { CursorNameEnum } from "../../../../types/cursors";
2323
import { controllerChildCamera } from "./concrete/ControllerChildCamera";
24-
import { controllerPenStroke } from "./concrete/ControllerPenStrokeControl";
2524
import { ControllerEntityResize } from "./concrete/ControllerEntityResize";
25+
import { controllerPenStrokeControl } from "./concrete/ControllerPenStrokeControl";
2626

2727
/**
2828
* 控制器,控制鼠标、键盘事件
@@ -151,7 +151,7 @@ export namespace Controller {
151151
ControllerLayerMoving.init();
152152
ControllerImageScale.init();
153153
controllerChildCamera.init();
154-
controllerPenStroke.init();
154+
controllerPenStrokeControl.init();
155155
ControllerEntityResize.init();
156156
//
157157
}
@@ -330,7 +330,7 @@ export namespace Controller {
330330
ControllerLayerMoving.destroy();
331331
ControllerImageScale.destroy();
332332
controllerChildCamera.destroy();
333-
controllerPenStroke.destroy();
333+
controllerPenStrokeControl.destroy();
334334
ControllerEntityResize.destroy();
335335
}
336336
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Line } from "../../../../dataStruct/shape/Line";
55
import { Vector } from "../../../../dataStruct/Vector";
66
import { EdgeRenderer } from "../../../../render/canvas2d/entityRenderer/edge/EdgeRenderer";
77
import { Renderer } from "../../../../render/canvas2d/renderer";
8-
import { Stage } from "../../../../stage/Stage";
8+
import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
99
import { GraphMethods } from "../../../../stage/stageManager/basicMethods/GraphMethods";
1010
import { StageManager } from "../../../../stage/stageManager/StageManager";
1111
import { Association } from "../../../../stage/stageObject/abstract/Association";
@@ -45,12 +45,22 @@ class CuttingControllerClass extends ControllerClass {
4545
private cuttingStartLocation = Vector.getZero();
4646

4747
public mousedown: (event: MouseEvent) => void = (event: MouseEvent) => {
48-
if (!(event.button == 2 || event.button == 3)) {
48+
if (!(event.button == 2 || event.button == 0)) {
4949
return;
5050
}
51-
if (Stage.mouseRightDragBackground !== "cut") {
51+
// 左键按下的
52+
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.connectAndCut) {
53+
this.mouseDownEvent(event);
5254
return;
5355
}
56+
// 右键按下的
57+
if (event.button === 2 && Stage.mouseRightDragBackground === "cut") {
58+
this.mouseDownEvent(event);
59+
return;
60+
}
61+
};
62+
63+
private mouseDownEvent(event: MouseEvent) {
5464
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
5565
this.lastMoveLocation = pressWorldLocation.clone();
5666

@@ -69,7 +79,7 @@ class CuttingControllerClass extends ControllerClass {
6979
} else {
7080
this.isUsing = false;
7181
}
72-
};
82+
}
7383

7484
public mousemove: (event: MouseEvent) => void = (event: MouseEvent) => {
7585
if (!this.isUsing) {
@@ -162,7 +172,7 @@ class CuttingControllerClass extends ControllerClass {
162172
}
163173

164174
public mouseup: (event: MouseEvent) => void = (event: MouseEvent) => {
165-
if (!(event.button == 2 || event.button == 3)) {
175+
if (!(event.button == 2 || event.button == 0)) {
166176
return;
167177
}
168178
if (!this.isUsing) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Rectangle } from "../../../../dataStruct/shape/Rectangle";
22
import { Vector } from "../../../../dataStruct/Vector";
33
import { Renderer } from "../../../../render/canvas2d/renderer";
4-
import { Stage } from "../../../../stage/Stage";
4+
import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
55
import { StageAutoAlignManager } from "../../../../stage/stageManager/concreteMethods/StageAutoAlignManager";
66
import { StageEntityMoveManager } from "../../../../stage/stageManager/concreteMethods/StageEntityMoveManager";
77
import { StageHistoryManager } from "../../../../stage/stageManager/StageHistoryManager";
@@ -24,8 +24,7 @@ class ControllerEntityClickSelectAndMoveClass extends ControllerClass {
2424
if (event.button !== 0) {
2525
return;
2626
}
27-
if (Stage.drawingMachine.isUsing) {
28-
// 涂鸦的时候用不了拖动
27+
if (Stage.leftMouseMode !== LeftMouseModeEnum.selectAndMove) {
2928
return;
3029
}
3130
this.mouseDownViewLocation = new Vector(event.clientX, event.clientY);
@@ -86,6 +85,9 @@ class ControllerEntityClickSelectAndMoveClass extends ControllerClass {
8685
if (Stage.selectMachine.isUsing || Stage.cuttingMachine.isUsing || Controller.pressingKeySet.has("alt")) {
8786
return;
8887
}
88+
if (Stage.leftMouseMode !== LeftMouseModeEnum.selectAndMove) {
89+
return;
90+
}
8991
if (!this.isMovingEntity) {
9092
return;
9193
}
@@ -116,6 +118,9 @@ class ControllerEntityClickSelectAndMoveClass extends ControllerClass {
116118
if (event.button !== 0) {
117119
return;
118120
}
121+
if (Stage.leftMouseMode !== LeftMouseModeEnum.selectAndMove) {
122+
return;
123+
}
119124

120125
const mouseUpViewLocation = new Vector(event.clientX, event.clientY);
121126
const diffLocation = mouseUpViewLocation.subtract(this.mouseDownViewLocation);

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ProgressNumber } from "../../../../dataStruct/ProgressNumber";
44
import { Vector } from "../../../../dataStruct/Vector";
55
import { EdgeRenderer } from "../../../../render/canvas2d/entityRenderer/edge/EdgeRenderer";
66
import { Renderer } from "../../../../render/canvas2d/renderer";
7-
import { Stage } from "../../../../stage/Stage";
7+
import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
88
import { StageManager } from "../../../../stage/stageManager/StageManager";
99
import { ConnectableEntity } from "../../../../stage/stageObject/abstract/ConnectableEntity";
1010
import { ConnectPoint } from "../../../../stage/stageObject/entity/ConnectPoint";
@@ -73,13 +73,24 @@ class ControllerNodeConnectionClass extends ControllerClass {
7373
}
7474

7575
public mousedown: (event: MouseEvent) => void = (event) => {
76+
if (!(event.button == 2 || event.button == 0)) {
77+
return;
78+
}
79+
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.connectAndCut) {
80+
this.onMouseDown(event);
81+
}
82+
if (event.button === 2) {
83+
this.onMouseDown(event);
84+
}
85+
};
86+
87+
private onMouseDown(event: MouseEvent) {
7688
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
77-
if (event.button === 0) {
89+
90+
if (event.button === 0 && Stage.leftMouseMode !== LeftMouseModeEnum.connectAndCut) {
91+
// 在非左键模式下点击左键
7892
this.onLeftMouseDown(pressWorldLocation);
7993
}
80-
if (!(event.button == 2 || event.button == 3)) {
81-
return;
82-
}
8394

8495
this._lastRightMousePressLocation = pressWorldLocation.clone();
8596

@@ -141,7 +152,7 @@ class ControllerNodeConnectionClass extends ControllerClass {
141152
SoundService.play.connectLineStart();
142153
this._isUsing = true;
143154
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
144-
};
155+
}
145156

146157
/**
147158
* 在mousemove的过程中,是否鼠标悬浮在了目标节点上
@@ -152,9 +163,18 @@ class ControllerNodeConnectionClass extends ControllerClass {
152163
if (Stage.selectMachine.isUsing || Stage.cuttingMachine.isUsing) {
153164
return;
154165
}
155-
if (!(Controller.isMouseDown[2] || Controller.isMouseDown[3])) {
166+
if (!this._isUsing) {
156167
return;
157168
}
169+
if (Controller.isMouseDown[0] && Stage.leftMouseMode === LeftMouseModeEnum.connectAndCut) {
170+
this.mouseMove(event);
171+
}
172+
if (Controller.isMouseDown[2]) {
173+
this.mouseMove(event);
174+
}
175+
};
176+
177+
private mouseMove(event: MouseEvent) {
158178
const worldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
159179
// 连接线
160180
let isFindConnectToNode = false;
@@ -176,15 +196,23 @@ class ControllerNodeConnectionClass extends ControllerClass {
176196
}
177197
// 由于连接线要被渲染器绘制,所以需要更新总控制里的lastMoveLocation
178198
Controller.lastMoveLocation = worldLocation.clone();
179-
};
199+
}
180200

181201
public mouseup: (event: MouseEvent) => void = (event) => {
182-
if (!(event.button == 2 || event.button == 3)) {
202+
if (!(event.button == 2 || event.button == 0)) {
183203
return;
184204
}
185205
if (!this.isConnecting()) {
186206
return;
187207
}
208+
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.connectAndCut) {
209+
this.mouseUp(event);
210+
} else if (event.button === 2) {
211+
this.mouseUp(event);
212+
}
213+
};
214+
215+
private mouseUp(event: MouseEvent) {
188216
const releaseWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
189217
const releaseTargetEntity = StageManager.findConnectableEntityByLocation(releaseWorldLocation);
190218
// 结束连线
@@ -219,7 +247,7 @@ class ControllerNodeConnectionClass extends ControllerClass {
219247
}
220248
this.clear();
221249
Controller.setCursorNameHook(CursorNameEnum.Default);
222-
};
250+
}
223251

224252
/**
225253
* 一种更快捷的连接方法: 节点在选中状态下右键其它节点直接连接,不必拖动
Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
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 { StageManager } from "../../../../stage/stageManager/StageManager";
5+
import { Controller } from "../Controller";
56
import { ControllerClass } from "../ControllerClass";
67

78
/**
89
* 所有和笔迹控制特定的逻辑都在这里
910
*/
1011
export class ControllerPenStrokeControl extends ControllerClass {
11-
// 检查鼠标是否悬浮在笔迹上
12-
public mousemove: (event: MouseEvent) => void = (event) => {
13-
if (Stage.drawingMachine.isUsing) {
12+
public isAdjusting = false;
13+
/**
14+
* Alt键右键按下时的位置
15+
*/
16+
public startAdjustWidthLocation: Vector = Vector.getZero();
17+
18+
public mousedown: (event: MouseEvent) => void = (event) => {
19+
if (!(event.button === 2 && Stage.leftMouseMode === LeftMouseModeEnum.draw)) {
1420
return;
1521
}
16-
const location = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
17-
for (const node of StageManager.getPenStrokes()) {
18-
node.isMouseHover = false;
19-
if (node.collisionBox.isContainsPoint(location)) {
20-
node.isMouseHover = true;
22+
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
23+
if (event.button === 2 && Controller.pressingKeySet.has("alt")) {
24+
// 右键按下时,开始调整笔刷粗细
25+
this.startAdjustWidthLocation = pressWorldLocation.clone();
26+
this.isAdjusting = true;
27+
}
28+
};
29+
30+
public mousemove: (event: MouseEvent) => void = (event) => {
31+
if (Stage.leftMouseMode === LeftMouseModeEnum.selectAndMove) {
32+
// 检查鼠标是否悬浮在笔迹上
33+
const location = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
34+
for (const node of StageManager.getPenStrokes()) {
35+
node.isMouseHover = false;
36+
if (node.collisionBox.isContainsPoint(location)) {
37+
node.isMouseHover = true;
38+
}
2139
}
2240
}
41+
if (Stage.leftMouseMode === LeftMouseModeEnum.draw) {
42+
if (Controller.pressingKeySet.has("alt") && Controller.isMouseDown[2]) {
43+
this.onMouseMoveWhenAdjusting(event);
44+
return;
45+
}
46+
}
47+
};
48+
49+
private onMouseMoveWhenAdjusting = (event: MouseEvent) => {
50+
// 更改宽度,检测鼠标上下移动的距离(模仿PS的笔刷粗细调整)
51+
const worldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
52+
const delta = this.startAdjustWidthLocation.distance(worldLocation);
53+
// Stage.effectMachine.addEffect(LineEffect.default(this.startAdjustWidthLocation, worldLocation.clone()));
54+
if (delta > 1) {
55+
Stage.drawingMachine.currentStrokeWidth = Math.min(Math.round(delta * 2), 1000);
56+
}
2357
};
2458
}
2559

26-
export const controllerPenStroke = new ControllerPenStrokeControl();
60+
export const controllerPenStrokeControl = new ControllerPenStrokeControl();

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

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PenStroke, PenStrokeSegment } from "../../../../stage/stageObject/entit
88
import { Settings } from "../../../Settings";
99
import { Controller } from "../Controller";
1010
import { ControllerClass } from "../ControllerClass";
11+
import { LeftMouseModeEnum, Stage } from "../../../../stage/Stage";
1112
/**
1213
* 涂鸦功能
1314
*/
@@ -17,8 +18,6 @@ class ControllerDrawingClass extends ControllerClass {
1718

1819
public currentStroke: PenStrokeSegment[] = [];
1920

20-
public isAdjusting = false;
21-
2221
public get isUsing() {
2322
return this._isUsing;
2423
}
@@ -41,10 +40,6 @@ class ControllerDrawingClass extends ControllerClass {
4140
* 当前画笔的粗度
4241
*/
4342
public currentStrokeWidth = 5;
44-
/**
45-
* Alt键右键按下时的位置
46-
*/
47-
public startAdjustWidthLocation: Vector = Vector.getZero();
4843

4944
/**
5045
* 初始化函数
@@ -66,34 +61,26 @@ class ControllerDrawingClass extends ControllerClass {
6661

6762
public mousedown: (event: MouseEvent) => void = (event: MouseEvent) => {
6863
if (!this._isUsing) return;
69-
if (!(event.button === 0 || event.button === 2)) {
64+
if (!(event.button === 0)) {
7065
return;
7166
}
7267
const pressWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
73-
if (event.button === 0) {
68+
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
7469
this.recordLocation.push(pressWorldLocation.clone());
7570

7671
this.lastMoveLocation = pressWorldLocation.clone();
7772

7873
Controller.setCursorNameHook(CursorNameEnum.Crosshair);
79-
} else if (event.button === 2 && Controller.pressingKeySet.has("alt")) {
80-
// 右键按下时,开始调整笔刷粗细
81-
this.startAdjustWidthLocation = pressWorldLocation.clone();
82-
this.isAdjusting = true;
8374
}
8475
};
8576

8677
public mousemove = (event: MouseEvent) => {
8778
if (!this._isUsing) return;
88-
if (!(Controller.isMouseDown[0] || Controller.isMouseDown[2])) {
79+
if (!Controller.isMouseDown[0]) {
8980
return;
9081
}
9182
const worldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
92-
if (Controller.pressingKeySet.has("alt") && Controller.isMouseDown[2]) {
93-
this.onMouseMoveWhenAdjusting(event);
94-
return;
95-
}
96-
if (Controller.isMouseDown[0]) {
83+
if (Controller.isMouseDown[0] && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
9784
// 检测:如果移动距离不超过10,则不记录
9885
if (worldLocation.distance(this.lastMoveLocation) < 5) {
9986
return;
@@ -106,25 +93,12 @@ class ControllerDrawingClass extends ControllerClass {
10693
}
10794
};
10895

109-
private onMouseMoveWhenAdjusting = (event: MouseEvent) => {
110-
// 更改宽度,检测鼠标上下移动的距离(模仿PS的笔刷粗细调整)
111-
const worldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
112-
const delta = this.startAdjustWidthLocation.distance(worldLocation);
113-
// Stage.effectMachine.addEffect(LineEffect.default(this.startAdjustWidthLocation, worldLocation.clone()));
114-
if (delta > 1) {
115-
this.currentStrokeWidth = Math.min(Math.round(delta * 2), 1000);
116-
}
117-
};
118-
11996
public mouseup = (event: MouseEvent) => {
12097
if (!this._isUsing) return;
121-
if (!(event.button === 0 || event.button === 2)) {
98+
if (!(event.button === 0)) {
12299
return;
123100
}
124-
if (event.button === 2) {
125-
this.isAdjusting = false;
126-
}
127-
if (event.button === 0) {
101+
if (event.button === 0 && Stage.leftMouseMode === LeftMouseModeEnum.draw) {
128102
const releaseWorldLocation = Renderer.transformView2World(new Vector(event.clientX, event.clientY));
129103
this.recordLocation.push(releaseWorldLocation.clone());
130104

0 commit comments

Comments
 (0)