-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.ts
79 lines (69 loc) · 1.74 KB
/
modifiers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type {
Container,
DisplayObject,
DisplayObjectEvents,
FederatedPointerEvent,
FederatedWheelEvent,
Text,
} from 'pixi.js'
import { getGameScale } from './core'
/**
* Make it possible to resize Text using the resize function
*/
export const handleResize = (textObject: Text): void => {
const ratio = getGameScale()
// * This will break typechecking
// @ts-expect-error
textObject.originalFontSize = textObject.style.fontSize
// @ts-expect-error
textObject.originalScale = { x: textObject.scale.x, y: textObject.scale.y }
textObject.style = {
...textObject.style,
// Pixis default font size is 26
fontSize: textObject.style.fontSize ?? 26 * ratio,
}
textObject.scale.set(
// @ts-expect-error
textObject.originalScale.x / ratio,
// @ts-expect-error
textObject.originalScale.y / ratio,
)
}
export const onHover = (
displayObject: DisplayObject,
options: { onOver?: () => void; onOut?: () => void },
) => {
const { onOver, onOut } = options
displayObject.interactive = true
displayObject.on('pointerover', () => {
if (onOver) {
onOver()
}
})
displayObject.on('pointerout', () => {
if (onOut) {
onOut()
}
})
}
const CLICK_EVENTS: Array<keyof DisplayObjectEvents> = ['click', 'tap']
export const onClick = (
displayObject: DisplayObject,
callback: (
event:
| DisplayObject
| Container<DisplayObject>
| FederatedPointerEvent
| FederatedWheelEvent
| undefined,
) => void,
): void => {
displayObject.interactive = true
displayObject.cursor = 'pointer'
CLICK_EVENTS.forEach((clickEvent) => {
// @ts-expect-error TODO: Figure type out here
displayObject.on(clickEvent, (event) => {
callback(event)
})
})
}