-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c3bf19
commit 8cdce09
Showing
11 changed files
with
113 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,89 @@ | ||
import { Object, Point, TPointerEvent, TPointerEventInfo } from 'fabric'; | ||
|
||
import { App } from '../app'; | ||
import { Service } from '../types'; | ||
|
||
type IMouseEvent = TPointerEventInfo<TPointerEvent>; | ||
import { KonvaMouseEvent, Service } from '../types'; | ||
|
||
export class MouseService extends Service { | ||
private event?: IMouseEvent; | ||
private targets: Object[]; | ||
|
||
constructor(app: App) { | ||
super(app); | ||
this.targets = []; | ||
( | ||
['onMouseDown', 'onMouseUp', 'onMouseMove', 'onMouseDoubleClick', 'onMouseOver', 'onMouseOut'] as (keyof this)[] | ||
[ | ||
'onMouseDown', | ||
'onMouseUp', | ||
'onMouseMove', | ||
'onMouseDoubleClick', | ||
'onMouseOver', | ||
'onMouseOut', | ||
'onMouseClick', | ||
] as (keyof this)[] | ||
).forEach((method) => { | ||
method = method as keyof MouseService; | ||
this[method] = (this[method] as Function).bind(this); | ||
}); | ||
|
||
this.app.canvas.on('mouse:down', this.onMouseDown); | ||
this.app.canvas.on('mouse:up', this.onMouseUp); | ||
this.app.canvas.on('mouse:move', this.onMouseMove); | ||
this.app.canvas.on('mouse:dblclick', this.onMouseDoubleClick); | ||
this.app.canvas.on('mouse:over', this.onMouseOver); | ||
this.app.canvas.on('mouse:out', this.onMouseOut); | ||
this.app.stage.on<'mousedown'>('mousedown', this.onMouseDown); | ||
this.app.stage.on('mouseup', this.onMouseUp); | ||
this.app.stage.on('mousemove', this.onMouseMove); | ||
this.app.stage.on('mouseover', this.onMouseOver); | ||
this.app.stage.on('mouseout', this.onMouseOut); | ||
this.app.stage.on('dblclick', this.onMouseDoubleClick); | ||
this.app.stage.on('click', this.onMouseClick); | ||
} | ||
|
||
public get pointer(): Point { | ||
if (!this.event) { | ||
return new Point(0, 0); | ||
private onMouseDown(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseDown) { | ||
return; | ||
} | ||
return this.event.pointer; | ||
this.app.currentTool.onMouseDown({ event, app: this.app }); | ||
} | ||
|
||
public toGlobal(point: Point): Point { | ||
/** | ||
* 变换矩阵包含了画布的平移、缩放和旋转信息 | ||
* [ a, b, c, d, e, f] | ||
* | ||
* viewportX = canvasX * a + canvasY * c + e | ||
* viewportY = canvasX * b + canvasY * d + f | ||
*/ | ||
const transformMatrix = this.app.canvas.viewportTransform; | ||
if (!transformMatrix) { | ||
return point; | ||
private onMouseUp(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseUp) { | ||
return; | ||
} | ||
const x = point.x * transformMatrix[0] + point.y * transformMatrix[2] + transformMatrix[4]; | ||
const y = point.x * transformMatrix[1] + point.y * transformMatrix[3] + transformMatrix[5]; | ||
return new Point(x, y); | ||
this.app.currentTool.onMouseUp({ event, app: this.app }); | ||
} | ||
|
||
private onMouseDown(event: IMouseEvent): void { | ||
this.event = event; | ||
if (!this.app.currentTool) { | ||
private onMouseMove(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseMove) { | ||
return; | ||
} | ||
if (this.app.currentTool.drawable && event.target) { | ||
this.targets.push(event.target); | ||
event.target.set({ evented: false }); | ||
} | ||
if (typeof this.app.currentTool.onMouseDown === 'function') { | ||
this.app.currentTool.onMouseDown({ event, app: this.app }); | ||
} | ||
this.app.currentTool.onMouseMove({ event, app: this.app }); | ||
} | ||
|
||
private onMouseUp(event: IMouseEvent): void { | ||
this.event = event; | ||
if (!this.app.currentTool) { | ||
private onMouseOver(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseOver) { | ||
return; | ||
} | ||
this.targets.forEach((obj) => { | ||
obj.set({ evented: true }); | ||
}); | ||
this.targets = []; | ||
if (typeof this.app.currentTool.onMouseUp === 'function') { | ||
this.app.currentTool.onMouseUp({ event, app: this.app }); | ||
} | ||
this.app.currentTool.onMouseOver({ event, app: this.app }); | ||
} | ||
|
||
private onMouseMove(event: IMouseEvent): void { | ||
this.event = event; | ||
if (!this.app.currentTool) { | ||
private onMouseOut(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseOut) { | ||
return; | ||
} | ||
if (this.app.currentTool.drawable && event.target) { | ||
this.targets.push(event.target); | ||
event.target.set({ evented: false }); | ||
} | ||
if (typeof this.app.currentTool.onMouseMove === 'function') { | ||
this.app.currentTool.onMouseMove({ event, app: this.app }); | ||
} | ||
this.app.currentTool.onMouseOut({ event, app: this.app }); | ||
} | ||
|
||
private onMouseDoubleClick(event: IMouseEvent): void { | ||
this.event = event; | ||
if (!this.app.currentTool) { | ||
private onMouseDoubleClick(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseDoubleClick) { | ||
return; | ||
} | ||
if (typeof this.app.currentTool.onMouseDoubleClick === 'function') { | ||
this.app.currentTool.onMouseDoubleClick({ event, app: this.app }); | ||
} | ||
this.app.currentTool.onMouseDoubleClick({ event, app: this.app }); | ||
} | ||
|
||
private onMouseOver(event: IMouseEvent): void { | ||
this.event = event; | ||
} | ||
|
||
private onMouseOut(event: IMouseEvent): void { | ||
this.event = event; | ||
private onMouseClick(event: KonvaMouseEvent): void { | ||
if (!this.app.currentTool || !this.app.currentTool.onMouseClick) { | ||
return; | ||
} | ||
this.app.currentTool.onMouseClick({ event, app: this.app }); | ||
} | ||
|
||
public dispose(): void { | ||
this.app.canvas.off('mouse:down', this.onMouseDown); | ||
this.app.canvas.off('mouse:up', this.onMouseUp); | ||
this.app.canvas.off('mouse:move', this.onMouseMove); | ||
this.app.canvas.off('mouse:dblclick', this.onMouseDoubleClick); | ||
this.app.canvas.off('mouse:over', this.onMouseOver); | ||
this.app.canvas.off('mouse:out', this.onMouseOut); | ||
this.app.stage.off('mousedown', this.onMouseDown); | ||
this.app.stage.off('mouseup', this.onMouseUp); | ||
this.app.stage.off('mousemove', this.onMouseMove); | ||
this.app.stage.off('mouseover', this.onMouseOver); | ||
this.app.stage.off('mouseout', this.onMouseOut); | ||
this.app.stage.off('dblclick', this.onMouseDoubleClick); | ||
this.app.stage.off('click', this.onMouseClick); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.