-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.ms
169 lines (146 loc) · 4.49 KB
/
buttons.ms
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
// This module handles all the buttons in the main UI.
import "importUtil"
ensureImport "events"
ensureImport "resources"
if not globals.hasIndex("font") then
ensureImport "bmfFonts"
bigFont = bmfFonts.Font.load("/sys/fonts/minimicro-pro-16.bmf")
font = bmfFonts.Font.load("/sys/fonts/minimicro-pro-12.bmf")
end if
buttonColor = "#DA00FF"
scratchDisp = new PixelDisplay
buttons = []
buttonImage = function(width=48, icon=null, text=null, roundLeft=true, roundRight=true)
src = resources.pics.button
h = src.height
scratchDisp.clear color.black, width, h
if roundLeft then
scratchDisp.drawImage src, 0, 0, 8, h, 0, 0, 8, h, buttonColor
else
scratchDisp.drawImage src, 0, 0, 8, h, 8, 0, 8, h, buttonColor
end if
scratchDisp.drawImage src, 8, 0, width-16, h, 8, 0, src.width-16, h, buttonColor
if roundRight then
scratchDisp.drawImage src, width-8, 0, 8, h, src.width-8, 0, 8, h, buttonColor
else
scratchDisp.drawImage src, width-8, 0, 8, h, src.width-16, 0, 8, h, buttonColor
end if
if icon then
scratchDisp.drawImage icon, width/2 - icon.width/2, h/2 - icon.height/2
end if
if text then
oldGfx = gfx; globals.gfx = scratchDisp
bigFont.printCentered text, width/2, 5, 1, color.black
globals.gfx = oldGfx
end if
return scratchDisp.getImage(0, 0, width, h)
end function
clearAll = function
for btn in buttons
btn.stop
end for
outer.buttons = []
end function
Button = new events.EventSprite
Button.color = "#F0F0F0"
Button.disabledColor = "#E0E0E0AA"
Button.action = null
Button.highlightColor = color.white
Button.pressedColor = "#AAAAAA"
Button.state = "NORMAL"
Button.onMouseOver = function
if self.state == "DISABLED" then return
self.enterState "HOVER"
end function
hoveredButton = null
Button.disable = function
self.enterState "DISABLED"
end function
Button.enable = function(enableIt = true)
if not enableIt then return self.disable
if self.contains(mouse) then
self.enterState "HOVER"
else
self.enterState "NORMAL"
end if
end function
Button.isEnabled = function; return self.state != "DISABLED"; end function
Button.onEnterState = function(newState)
if newState == "HOVER" then
self.tint = self.highlightColor
if hoveredButton and hoveredButton != self then
hoveredButton.enterState "NORMAL"
end if
outer.hoveredButton = self
self.ignoreClicks = false
else if newState == "NORMAL" then
self.tint = self.color
self.ignoreClicks = false
else if newState == "DISABLED" then
self.tint = self.disabledColor
self.ignoreClicks = true
end if
end function
Button.onClick = function
if self.state == "DISABLED" then return
pressed = true
self.tint = self.pressedColor
while mouse.button
inBounds = self.contains(mouse)
if pressed and not inBounds then
self.tint = self.color
pressed = false
else if inBounds and not pressed then
self.tint = self.pressedColor
pressed = true
end if
yield
end while
if pressed then
self.action
self.tint = self.highlightColor
end if
end function
Button.update = function
// In Mini Micro 1.2, the events module does not support an onMouseExit
// event. So we just check for mouse-exit here in update instead.
if self.state == "HOVER" and not self.contains(mouse) then
self.enterState "NORMAL"
end if
end function
addTextOrIconButton = function(left, bottom, width, icon, text, name, roundLeft=true, roundRight=true)
btn = new Button
btn.image = buttonImage(width, icon, text, roundLeft, roundRight)
btn.name = name
btn.x = left + width/2
btn.y = bottom + btn.image.height/2
btn.tint = btn.color
// Note: due to a flaw in eventLoop.update (in Mini Micro 1.2 and older),
// onMouseOver does not fire if it is inherited from a superclass. We
// work around that thusly:
btn.onMouseOver = @Button.onMouseOver
btn.start
buttons.push btn
return btn
end function
addButton = function(left, bottom, width, iconOrText, name, roundLeft=true, roundRight=true)
icon = null; text = null
if iconOrText isa string then text = iconOrText else icon = iconOrText
return addTextOrIconButton(left, bottom, width, icon, text, name, roundLeft, roundRight)
end function
runTest = function
clearAll
x = 400
for i in range(0, 3)
w = 40 + 4 * (i==0 or i==3)
b = addButton(x, 300, w, resources.buttonIcons[i], resources.buttonNames[i],
i==0, i==3)
b.action = function; print "Clicked " + self.name; end function
x += w + 1
end for
buttons[2].disable
events.eventLoop.onKey["escape"] = function; events.eventLoop.running = false; end function
events.eventLoop.run
clearAll
end function
if locals == globals then runTest