-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeanButton.spin2
132 lines (105 loc) · 3.94 KB
/
LeanButton.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
{
LeanButton
A button that does not have a visible representation
To be used over images or parts of images
}
CON
DEBOUNCE_TIMER = 500
#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
long value 'Virtual value of the widget
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 invert_border 'Boolean flag to invert border colors
'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 active_value
byte inactive_value
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, x, y, tw, th
''This method draws the graphical representation of this widget type
if enabled AND (border_width <> 0)
'BORDER
util.draw_border(xtop,ytop,xbot,ybot,ol_color,border_width,invert_border)
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
if event_handler <> 0
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
event_handler(id) 'call the event handler and pass along id of button
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
ol_color := ol
border_width := 0
invert_border := false
active_value := true
configured := 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
''+--------------------------------------------------------------------+
'' 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) 'Calls a passed event handler
''+--------------------------------------------------------------------+
'' OTHER METHODS
''+--------------------------------------------------------------------+
PUB set_border_width(bw)
border_width := bw
PUB set_mode(m)
mode := m
PUB set_value(v)
value := v