-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.spin2
225 lines (175 loc) · 6.08 KB
/
Button.spin2
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{
Button Object
Represents a button that is used as an input signal
The button object can be a Toggle button or a normal button.
When set as a toggle button it will alternate between it's
active and inactive values on consecutive touch events.
When a button claims an event, it calls the its event handler
method pointer and passes its ID and its value as parameters.
The button object debounces the event to prevent unwanted triggering
of the event handler.
A button object also has a contents buffer that stores the
text that is going to be drawn inside it.
}
CON
'DEBOUNCE_TIMER = 500
MAX_CHARACTERS = 8
#0, NORMAL_MODE, TOGGLE_MODE
VAR
'Attributes
long id 'Unique ID - (window index << 4)|(button index)
long bg_color 'background color
long fg_color 'foreground color
long ol_color 'outline = border
word xtop 'Top Left X (Pixel)
word ytop 'Top Left Y (Pixel)
word xbot 'Bottom Right X (Pixel)
word ybot 'Bottom Right Y (Pixel)
byte border_width 'Border width in pixels
'Method Pointers
long event_handler 'Default event handler method pointer
'Internal state variables
long last_time 'Stores touch event debouncer last time value
byte configured 'Boolean flag to identify configured widgets in the window's list
byte enabled 'Boolean flag to indicate widget can be drawn and can check for events
byte value 'Virtual value of the widget
byte active_value
byte inactive_value
'Button specific attributes
byte mode 'Mode: 0 = Normal (no debouncing, value = active_value while event is true)
'Mode: 1 = Toggle (debouncing, value toggles on successive events)
byte invert_border 'Boolean flag to invert border colors
LONG string_pointer 'Hub address of string memory (addresses are LONG but memory access in
'hub is byte
byte contents[MAX_CHARACTERS+1]
byte font_size
byte padding
byte compression
word debounce_timer
OBJ
color : "Colors"
util : "Utilities"
graphics : "Graphics"
PUB null()
''+--------------------------------------------------------------------+
'' WIDGET INTERFACE METHODS
''+--------------------------------------------------------------------+
PUB set_enable(e)
if e
enabled := true
else
enabled := false
PUB draw()| w, h, tw, th, x, y
''This method draws the graphical representation of this widget type
if enabled
'FILL
graphics.fill_rectangle(xtop+border_width,ytop+border_width,xbot-border_width,ybot-border_width,bg_color)
'BORDER
util.draw_border(xtop,ytop,xbot,ybot,ol_color,border_width,invert_border)
'TEXT
if string_pointer <> 0
w := (xbot - xtop) - 2*border_width
h := (ybot - ytop) - 2*border_width
tw := util.get_text_width(string_pointer, font_size, compression)
th := util.get_text_height(font_size)
x := xtop + border_width + (w - tw)/2
y := ytop + border_width + (h - th)/2
util.draw_text(x,y,string_pointer,0,font_size,compression,bg_color,fg_color)
PUB check_event(coord):is_mine | x, y
if configured AND enabled
x := coord.WORD[0]
y := coord.WORD[1]
if (x >= xtop AND x <= xbot) AND (y >= ytop AND y <= ybot)
is_mine := true
'debug("event at ", udec(id))
if (getms()-last_time) >= DEBOUNCE_TIMER 'debounce the event
if mode == TOGGLE_MODE 'Toggle or Event Mode
value := (value==inactive_value)?active_value:inactive_value
if event_handler <> 0
event_handler(id, value) 'call the event handler and pass along id of button and current value
last_time := getms() 'reset debounce timer
''+--------------------------------------------------------------------+
'' CONFIGURATION METHODS
''+--------------------------------------------------------------------+
PUB configure(i, xt, yt, w, h, bg, fg, ol)
id := i
xtop := xt
ytop := yt
xbot := w + xt 'xb
ybot := h + yt 'yb
bg_color := bg
fg_color := fg
ol_color := ol
border_width := 1
active_value := true
padding := 10
compression := 4
font_size := 2
string_pointer := @contents
debounce_timer := 500 'ms
configured := true
enabled := true
last_time := getms()
PUB is_configured():conf
conf := configured
PUB set_coordinates(xt, yt, xb, yb)
xtop := xt
ytop := yt
xbot := xb
ybot := yb
PUB get_coordinates() : coords
coords.BYTE[0] := xtop
coords.BYTE[1] := ytop
coords.BYTE[2] := xbot
coords.BYTE[3] := ybot
PUB set_colors(bg, fg, ol)
bg_color := bg
fg_color := fg
ol_color := ol
PUB get_colors(): bg, fg, ol
bg := bg_color
fg := fg_color
ol := ol_color
PUB set_font_size(s)
font_size := 1 #> s <# 2
PUB set_compression(v)
compression := v
''+--------------------------------------------------------------------+
'' EVENT HANDLING METHODS
''+--------------------------------------------------------------------+
PUB set_event_handler(Method)
event_handler := Method 'Sets the default event handler of this object
PUB call_event_handler(Method)
Method(id,value) 'Calls a passed event handler
''+--------------------------------------------------------------------+
'' OTHER METHODS
''+--------------------------------------------------------------------+
PUB set_values(a,i)
active_value := a
inactive_value := i
PUB set_value(v)
value := v
PUB get_value():v
v := value
PUB set_invert(i)
invert_border := i
PUB set_mode(m)
mode := m
PUB set_border_width(bw)
border_width := bw
PUB set_padding(p)
padding := p
PUB set_contents(addr)|c,i
'Set new contents
repeat i from 0 to util.get_characters(addr)-1 'Reserve last character place for the terminating 0
c := BYTE[addr][i]
if c <> 0
contents[i] := c
else
quit
contents[i] := 0
PUB update_contents(s)|c
set_contents(s)
draw()
PUB set_debounce(v)
debounce_timer := v