-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.lua
230 lines (182 loc) · 6.45 KB
/
ui.lua
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
226
227
228
229
230
-- ui.lua (currently includes Button class with labels, font selection and optional event model)
-- Version 1.1
module(..., package.seeall)
-----------------
-- Helper function for newButton utility function below
local function newButtonHandler( self, event )
local result = true
local default = self[1]
local over = self[2]
-- General "onEvent" function overrides onPress and onRelease, if present
local onEvent = self._onEvent
local onPress = self._onPress
local onRelease = self._onRelease
local buttonEvent = {}
if (self._id) then
buttonEvent.id = self._id
end
local phase = event.phase
if "began" == phase then
if over then
default.isVisible = false
over.isVisible = true
end
if onEvent then
buttonEvent.phase = "press"
result = onEvent( buttonEvent )
elseif onPress then
result = onPress( event )
end
-- Subsequent touch events will target button even if they are outside the stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true
elseif self.isFocus then
local bounds = self.stageBounds
local x,y = event.x,event.y
local isWithinBounds =
bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y
if "moved" == phase then
if over then
-- The rollover image should only be visible while the finger is within button's stageBounds
default.isVisible = not isWithinBounds
over.isVisible = isWithinBounds
end
elseif "ended" == phase or "cancelled" == phase then
if over then
default.isVisible = true
over.isVisible = false
end
-- Only consider this a "click" if the user lifts their finger inside button's stageBounds
if isWithinBounds then
if onEvent then
buttonEvent.phase = "release"
result = onEvent( buttonEvent )
elseif onRelease then
result = onRelease( event )
end
end
-- Allow touch events to be sent normally to the objects they "hit"
display.getCurrentStage():setFocus( nil )
self.isFocus = false
end
end
return result
end
---------------
-- Button class
function newButton( params )
local button, default, over, size, font, textColor, offset
if params.default then
button = display.newGroup()
default = display.newImage( params.default )
button:insert( default, true )
end
if params.over then
over = display.newImage( params.over )
over.isVisible = false
button:insert( over, true )
end
if params.text then
if ( params.size and type(params.size) == "number" ) then size=params.size else size=20 end
if ( params.font ) then font=params.font else font=native.systemFontBold end
if ( params.textColor ) then textColor=params.textColor else textColor={ 255, 255, 255, 255 } end
-- Optional vertical correction for fonts with unusual baselines (I'm looking at you, Zapfino)
if ( params.offset and type(params.offset) == "number" ) then offset=params.offset else offset = 0 end
if ( params.emboss ) then
-- Make the label text look "embossed" (also adjusts effect for textColor brightness)
local textBrightness = ( textColor[1] + textColor[2] + textColor[3] ) / 3
local labelHighlight = display.newText( params.text, 0, 0, font, size )
if ( textBrightness > 127) then
labelHighlight:setTextColor( 255, 255, 255, 20 )
else
labelHighlight:setTextColor( 255, 255, 255, 140 )
end
button:insert( labelHighlight, true )
labelHighlight.x = labelHighlight.x + 1.5; labelHighlight.y = labelHighlight.y + 1.5 + offset
local labelShadow = display.newText( params.text, 0, 0, font, size )
if ( textBrightness > 127) then
labelShadow:setTextColor( 0, 0, 0, 128 )
else
labelShadow:setTextColor( 0, 0, 0, 20 )
end
button:insert( labelShadow, true )
labelShadow.x = labelShadow.x - 1; labelShadow.y = labelShadow.y - 1 + offset
end
local labelText = display.newText( params.text, 0, 0, font, size )
labelText:setTextColor( textColor[1], textColor[2], textColor[3], textColor[4] )
button:insert( labelText, true )
labelText.y = labelText.y + offset
end
if ( params.onPress and ( type(params.onPress) == "function" ) ) then
button._onPress = params.onPress
end
if ( params.onRelease and ( type(params.onRelease) == "function" ) ) then
button._onRelease = params.onRelease
end
if (params.onEvent and ( type(params.onEvent) == "function" ) ) then
button._onEvent = params.onEvent
end
-- Set button as a table listener by setting a table method and adding the button as its own table listener for "touch" events
button.touch = newButtonHandler
button:addEventListener( "touch", button )
if params.x then
button.x = params.x
end
if params.y then
button.y = params.y
end
if params.id then
button._id = params.id
end
return button
end
--------------
-- Label class
function newLabel( params )
local labelText
local size, font, textColor, align
if ( params.bounds ) then
local bounds = params.bounds
local left = bounds[1]
local top = bounds[2]
local width = bounds[3]
local height = bounds[4]
if ( params.size and type(params.size) == "number" ) then size=params.size else size=20 end
if ( params.font ) then font=params.font else font=native.systemFontBold end
if ( params.textColor ) then textColor=params.textColor else textColor={ 255, 255, 255, 255 } end
if ( params.offset and type(params.offset) == "number" ) then offset=params.offset else offset = 0 end
if ( params.align ) then align = params.align else align = "center" end
if ( params.text ) then
labelText = display.newText( params.text, 0, 0, font, size )
labelText:setTextColor( textColor[1], textColor[2], textColor[3], textColor[4] )
if ( align == "left" ) then
labelText.x = left + labelText.stageWidth * 0.5
elseif ( align == "right" ) then
labelText.x = (left + width) - labelText.stageWidth * 0.5
else
labelText.x = ((2 * left) + width) * 0.5
end
end
labelText.y = top + labelText.stageHeight * 0.5
-- Public methods
function labelText:setText( newText )
if ( newText ) then
self.text = newText
if ( "left" == align ) then
self.x = left + self.stageWidth / 2
elseif ( "right" == align ) then
self.x = (left + width) - self.stageWidth / 2
else
self.x = ((2 * left) + width) / 2
end
end
end
function labelText:setTextColor( textColor )
if ( textColor and type(textColor) == "table" ) then
self:setTextColor( textColor[1], textColor[2], textColor[3], textColor[4] )
end
end
end
-- Return instance
return labelText
end