forked from coronalabs/framework-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picker.lua
executable file
·252 lines (224 loc) · 7.65 KB
/
picker.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
-- Abstract: widget.newPickerWheel() unit test
-- Code is MIT licensed; see https://www.coronalabs.com/links/code/license
---------------------------------------------------------------------------------------
local widget = require( "widget" )
local composer = require( "composer" )
local scene = composer.newScene()
local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
function scene:create( event )
local group = self.view
local xAnchor, yAnchor
if not isGraphicsV1 then
xAnchor = display.contentCenterX
yAnchor = display.contentCenterY
else
xAnchor = 0
yAnchor = 0
end
local fontColor = 0
local background = display.newRect( xAnchor, yAnchor, display.actualContentWidth, display.actualContentHeight )
if widget.USE_IOS_THEME then
if isGraphicsV1 then background:setFillColor( 197, 204, 212, 255 )
else background:setFillColor( 197/255, 204/255, 212/255, 1 ) end
elseif widget.USE_ANDROID_HOLO_LIGHT_THEME then
if isGraphicsV1 then background:setFillColor( 255, 255, 255, 255 )
else background:setFillColor( 1, 1, 1, 1 ) end
elseif widget.USE_ANDROID_HOLO_DARK_THEME then
if isGraphicsV1 then background:setFillColor( 34, 34, 34, 255 )
else background:setFillColor( 34/255, 34/255, 34/255, 1 ) end
fontColor = 0.5
else
if isGraphicsV1 then background:setFillColor( 255, 255, 255, 255 )
else background:setFillColor( 1, 1, 1, 1 ) end
end
group:insert( background )
local backButtonPosition = 5
local backButtonSize = 34
-- Button to return to unit test listing
local returnToListing = widget.newButton{
id = "returnToListing",
left = 60,
top = backButtonPosition,
label = "Exit",
width = 200, height = backButtonSize,
onRelease = function() composer.gotoScene( "unitTestListing" ) end;
}
returnToListing.x = display.contentCenterX
group:insert( returnToListing )
----------------------------------------------------------------------------------------------------------------
-- START OF UNIT TEST
----------------------------------------------------------------------------------------------------------------
-- Set up the Picker Wheel's columns
local columnData =
{
{
align = "left",
width = 124,
labelPadding = 20, --NEW (default is 6)
startIndex = 2,
labels = { "Hoodie", "Short Sleeve", "Long Sleeve", "Sweatshirt" }
},
{
align = "left",
width = 96,
labelPadding = 10,
startIndex = 1,
labels = { "Dark Grey", "White", "Black", "Orange" }
},
{
align = "left",
width = 60,
labelPadding = 10,
startIndex = 3,
labels = { "S", "M", "L", "XL", "XXL" }
},
}
-- Function to be called when user selects an option
local function valueSelectedFixed( event )
print( "valueSelectedFixed() function called" )
print( "-------------------------------" )
print( "Column: " .. event["column"] )
print( "Row: " .. event["row"] )
end
local function valueSelectedResizable( event )
print( "valueSelectedResizable() function called" )
print( "-------------------------------" )
print( "Column: " .. event["column"] )
print( "Row: " .. event["row"] )
end
-- Fixed-size picker wheel
local options = {
frames =
{
{ x=0, y=0, width=320, height=222 },
{ x=328, y=0, width=320, height=222 },
{ x=656, y=0, width=12, height=222 }
},
sheetContentWidth = 668,
sheetContentHeight = 222
}
local pickerWheelSheetFixed = graphics.newImageSheet( "unitTestAssets/pickerwheel-fixed.png", options )
local pickerWheel = widget.newPickerWheel(
{
x = display.contentCenterX,
top = 0,
fontSize = 18,
columns = columnData,
onValueSelected = valueSelectedFixed, --NEW
sheet = pickerWheelSheetFixed,
overlayFrame = 1,
backgroundFrame = 2,
separatorFrame = 3,
listener = testF
})
group:insert( pickerWheel )
--[[local testRectA1 = display.newRect( group,display.contentCenterX,pickerWheel.y,400,40 )
testRectA1:setFillColor(1,0,0.2,0.3)
local testRectAT1 = display.newRect( group,display.contentCenterX,pickerWheel.y-(40*2),400,40 )
testRectAT1:setFillColor(1,0.2,0,0.2)
local testRectAT2 = display.newRect( group,display.contentCenterX,pickerWheel.y-40,400,40 )
testRectAT2:setFillColor(1,0.6,0,0.2)
local testRectAT3 = display.newRect( group,display.contentCenterX,pickerWheel.y+(40*2),400,40 )
testRectAT3:setFillColor(1,0.2,0,0.2)
local testRectAT4 = display.newRect( group,display.contentCenterX,pickerWheel.y+40,400,40 )
testRectAT4:setFillColor(1,0.6,0,0.2)--]]
local getValuesButtonA = widget.newButton(
{
id = "getValues",
top = pickerWheel.contentBounds.yMax+2,
label = "print() values",
height = backButtonSize,
onRelease = function()
local values = pickerWheel:getValues()
for i = 1, #values do
print( "Column", i, "value is:", values[i].value )
print( "Column", i, "index is:", values[i].index )
end
end
})
getValuesButtonA.x = display.contentCenterX
group:insert( getValuesButtonA )
-- Resizable picker wheel
local rowHeight = 32
local options2 = {
frames =
{
{ x=0, y=0, width=20, height=20 }, --topLeft
{ x=20, y=0, width=120, height=20 }, --topMiddle
{ x=140, y=0, width=20, height=20 }, --topRight
{ x=0, y=20, width=20, height=120 }, --middleLeft
{ x=140, y=20, width=20, height=120 }, --middleRight (adjust x later!)
{ x=0, y=140, width=20, height=20 }, --bottomLeft (adjust y later!)
{ x=20, y=140, width=120, height=20 }, --bottomMiddle (adjust y later!)
{ x=140, y=140, width=20, height=20 }, --bottomRight (adjust x/y later!)
{ x=180, y=0, width=32, height=80 }, --topFade
{ x=224, y=0, width=32, height=80 }, --bottomFade
{ x=276, y=0, width=32, height=20 }, --middleSpanTop
{ x=276, y=60, width=32, height=20 }, --middleSpanBottom
{ x=276, y=100, width=12, height=32 } --separator
},
sheetContentWidth = 312,
sheetContentHeight = 160
}
local pickerWheelSheetResizable = graphics.newImageSheet( "unitTestAssets/pickerwheel-resizable.png", options2 )
local resizablePickerWheel = widget.newPickerWheel
{
x = display.contentCenterX,
top = pickerWheel.contentBounds.yMax+40,
columns = columnData,
fontSize = 14,
style = "resizable",
width = 280,
rowHeight = rowHeight,
onValueSelected = valueSelectedResizable,
sheet = pickerWheelSheetResizable,
--borderPadding = 28,
topLeftFrame = 1,
topMiddleFrame = 2,
topRightFrame = 3,
middleLeftFrame = 4,
middleRightFrame = 5,
bottomLeftFrame = 6,
bottomMiddleFrame = 7,
bottomRightFrame = 8,
topFadeFrame = 9,
bottomFadeFrame = 10,
middleSpanTopFrame = 11,
middleSpanBottomFrame = 12,
--backgroundFrame = 11,
separatorFrame = 13,
middleSpanOffset = 4
}
group:insert( resizablePickerWheel )
local getValuesButtonB = widget.newButton(
{
id = "getValues",
top = resizablePickerWheel.contentBounds.yMax+2,
label = "print() values",
height = backButtonSize,
onRelease = function()
local values = resizablePickerWheel:getValues()
for i = 1, #values do
print( "Column", i, "value is:", values[i].value )
print( "Column", i, "index is:", values[i].index )
end
end
})
getValuesButtonB.x = display.contentCenterX
group:insert( getValuesButtonB )
--timer.performWithDelay( 2000, function() resizablePickerWheel:selectValue( 1, 3 ); end )
--timer.performWithDelay( 4000, function() resizablePickerWheel:selectValue( 1, 4 ); end )
--timer.performWithDelay( 6000, function() resizablePickerWheel:selectValue( 1, 1 ); end )
end
function scene:hide( event )
if ( "did" == event.phase ) then
--Cancel test timer if active
if testTimer ~= nil then
timer.cancel( testTimer )
testTimer = nil
end
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "hide", scene )
return scene