-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.fs
209 lines (175 loc) · 3.86 KB
/
button.fs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
\ RilouwShell 0.1.0
\ Copyright (c) 2019 Jerome Martin
\ Released under the terms of the GNU GPL version 3
\ http://rilouw.eu/project/rilouwshell
create button-tileset tileset% %allot drop
0 constant BUTTON-BORDER-LEFT
1 constant BUTTON-BORDER-RIGHT
0 value DRAW-CLICKED
0 value DRAW-INVERTED
: clicked-tile? DRAW-CLICKED if 2 + then ;
: inverted-tile? DRAW-INVERTED if 4 + then ;
sprite
l: ..OO..........OO........
l: .OOOO........OOOO.......
l: OOOOOO..OO..OOBBOO..OO..
l: OOOOOO.OOOO.OBBBBO.OOOO.
l: OOOOOOOOOOOOOBBBBOOOBBOO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OOOOOOOOOOOOOBBBBOOBBBBO
l: OBOOBOOOOOOOOOBBOOOBBBBO
l: OBBBBOOOOOOOOOOOOOOBBBBO
l: OBBBBOOOOOOOOOOOOOOOBBOO
l: .OBBO..OOOO..OOOO..OOOO.
l: ..OO....OO....OO....OO..
end-sprite BUTTON-SPRITE
struct
cell% field button-x
cell% field button-y
cell% field button-w
ptr% field button-text
cell% field button-primary
cell% field button-enabled
cell% field button-event
ptr% field button-surface-normal
ptr% field button-surface-clicked
end-struct button%
: new-button-surface ( button -- surface )
button-w @ tile/pos 2 +
TILE 4 +
new-surface
;
: init-buttons ( -- )
BUTTON-SPRITE load-sprite
button-tileset tileset-surface !
8 button-tileset tileset-w !
3 button-tileset tileset-tile-w !
20 button-tileset tileset-tile-h !
;
: select-button-surface ( button -- surface )
DRAW-CLICKED if
button-surface-clicked @
else
button-surface-normal @
then
;
: draw-button-borders ( button -- )
>r
r@ select-button-surface
dup
0 0
button-tileset
BUTTON-BORDER-LEFT
inverted-tile? clicked-tile? draw-tileset
( surface )
r> button-w @ tile/pos 1-
0
button-tileset
BUTTON-BORDER-RIGHT
inverted-tile? clicked-tile? draw-tileset
;
: draw-button-lines ( button -- )
>r
r@ select-button-surface
\ higher line or rect
dup
3 DRAW-CLICKED if 2 else 0 then
r@ button-w @ tile/pos 4 -
r@ button-primary @ if
TILE 1+ Orange draw-rect
else
Orange draw-hline
then
\ lower-line or rect
( surface )
DRAW-CLICKED
r@ button-primary @
or if
3 TILE 3 +
r> button-w @ tile/pos 4 -
Orange draw-hline
else
3 TILE 1+
r> button-w @ tile/pos 4 -
3
Orange draw-rect
then
;
: draw-button-text ( button -- )
>r
r@ button-primary @ invert-font
r@ select-button-surface
r> button-text @
9
DRAW-CLICKED if 5 else 3 then
write-at
;
: init-button ( button -- )
>r
\ init surfaces
r@ new-button-surface
r@ button-surface-normal !
r@ new-button-surface
r@ button-surface-clicked !
r@ button-primary @ 0= to DRAW-INVERTED
\ normal button
false to DRAW-CLICKED
r@ draw-button-borders
r@ draw-button-lines
r@ draw-button-text
\ clicked button
true to DRAW-CLICKED
r@ draw-button-borders
r@ draw-button-lines
r> draw-button-text
;
: no-surface? ( button -- b )
button-surface-normal @ 0=
;
: get-button-surface ( button -- surface )
dup no-surface? if
dup init-button
then
dup clicked? if
button-surface-clicked @
else
button-surface-normal @
then
;
: (render-button) ( surface button -- )
>r
r@ get-button-surface
swap
r@ button-x @ tile/pos 1-
r> button-y @ tile/pos 1-
blit
;
' (render-button) is render-button
: (get-button-rect) ( button -- x y w h )
>r
r@ button-x @ tile/pos
r@ button-y @ tile/pos
r> button-w @ tile/pos
TILE
;
' (get-button-rect) is get-button-rect
: button, ( #element x y w text primary enabled event -- element type #element )
button% %allot >r
r@ button-event !
r@ button-enabled !
r@ button-primary !
r@ button-text !
r@ button-w !
r@ button-y !
r@ button-x !
1+ \ increment #element
r> TYPE-BUTTON rot
;