-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget_Template.spin2
170 lines (140 loc) · 5.04 KB
/
Widget_Template.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
{
Widget Object - Template
}
CON
DEBOUNCE_TIMER = 500
MAX_CHARACTERS = 8
'Text align constants
#0, LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN
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
'Characters variables
byte contents[MAX_CHARACTERS+1] 'Should be 1 more than the displayed characters to account for the terminating zero
long string_pointer 'Hub address of string memory (addresses are LONG but memory access in hub is byte)
byte font_size
byte padding
byte align '0 = LEFT, 1 = CENTER, 2 = RIGHT
byte compression 'compression factor for the text
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
'FILL
graphics.fill_rectangle(xtop+border_width,ytop+border_width,xbot-border_width,ybot-border_width,bg_color)
'BORDER
util.draw_border(drawing_method_pointer,xtop,ytop,xbot,ybot,ol_color,border_width,invert_border)
'TEXT
w := (xbot - xtop) - 2*border_width
h := (ybot - ytop) - 2*border_width
tw := util.get_characters(string_pointer) * (font_size==1) ? 8 : 12
th := font_size*8
if align == LEFT_ALIGN
x := xtop + border_width + padding
if align == CENTER_ALIGN
x := xtop + border_width + (w - tw)/2
if align == RIGHT_ALIGN
x := xbot - border_width - tw - padding
y := ytop + border_width + (h - th)/2
util.draw_text(drawing_method_pointer,x,y,string_pointer,font_size,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
if event_handler <> 0
if (getms()-last_time) >= DEBOUNCE_TIMER 'debounce the event
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
bg_color := bg
fg_color := fg
ol_color := ol
border_width := 1
invert_border := false
font_size := 2
padding := 2
align := CENTER_ALIGN
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
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
''+--------------------------------------------------------------------+
'' 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_contents(addr)|c,i
'Set new contents
repeat i from 0 to MAX_CHARACTERS-1 'Reserve last character place for the terminating 0
c := BYTE[addr][i]
if c <> 0
contents[i] := c
else
quit
contents[i+1] := 0
string_pointer := @contents