-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonElement.sj
94 lines (81 loc) · 2.66 KB
/
buttonElement.sj
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@heap
buttonElement #element #clickable(
text := ""
textColor := colors.black
font := style.getFont()
normalImage := valid(image(texture_fromPng("assets/buttonNormal.png"), margin := margin(2, 2, 2, 2)))
hotImage := valid(image(texture_fromPng("assets/buttonHot.png"), margin := margin(2, 2, 2, 2)))
pressedImage := valid(image(texture_fromPng("assets/buttonPressed.png"), margin := margin(2, 2, 2, 2)))
margin := margin(10, 10, 10, 10)
onClick := empty'heap ()void
_rect := rect()
_textRenderer := empty'textRenderer
_imageRenderer := empty'imageRenderer
_clickGesture := clickGesture()
getSize(maxSize : 'size) {
textSize : font.getTextSize(text) + margin
textSize.min(maxSize)
}
getRect() { _rect }
setRect(rect_ : 'rect) {
if _rect != rect_ {
_rect = rect_
_clickGesture.rect = rect_
_textRenderer = empty'textRenderer
_imageRenderer = empty'imageRenderer
}
void
}
render(scene : 'scene2d) {
if isEmpty(_imageRenderer) {
image : switch _clickGesture.state {
clickState.entered { hotImage }
clickState.pressed { pressedImage }
default { normalImage }
}
ifValid image {
_imageRenderer = valid(imageRenderer(
image : image
rect : _rect
))
}
}
if isEmpty(_textRenderer) {
innerRect : _rect.subtractMargin(margin)
textSize : font.getTextSize(text)
_textRenderer = valid(textRenderer(
text: text
point: point(innerRect.x + (innerRect.w - textSize.w) / 2, innerRect.y + (innerRect.h - textSize.h) / 2)
color: textColor
font: font))
}
_imageRenderer?.render(scene)
_textRenderer?.render(scene)
void
}
onClickGestureEnter(element : '#element) {
_imageRenderer = empty'imageRenderer
void
}
onClickGestureLeave(element : '#element) {
_imageRenderer = empty'imageRenderer
void
}
onClickGesturePress(element : '#element) {
_imageRenderer = empty'imageRenderer
void
}
onClickGestureClick(element : '#element) {
_imageRenderer = empty'imageRenderer
ifValid onClick {
onClick()
}
void
}
fireMouseEvent(mouseEvent : 'mouseEvent)'bool {
_clickGesture.fireMouseEvent(mouseEvent)
}
) {
_clickGesture = clickGesture(this as #element, this as #clickable)
this
}