-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathwidget_pickerWheel.lua
More file actions
522 lines (424 loc) · 18.1 KB
/
widget_pickerWheel.lua
File metadata and controls
522 lines (424 loc) · 18.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
-- Copyright © 2013 Corona Labs Inc. All Rights Reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- * Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
-- * Neither the name of the company nor the names of its contributors
-- may be used to endorse or promote products derived from this software
-- without specific prior written permission.
-- * Redistributions in any form whatsoever must retain the following
-- acknowledgment visually in the program (e.g. the credits of the program):
-- 'This product includes software developed by Corona Labs Inc. (http://www.coronalabs.com).'
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL CORONA LABS INC. BE LIABLE FOR ANY
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
local M =
{
_options = {},
_widgetName = "widget.newPickerWheel",
}
-- Require needed widget files
local _widget = require( "widget" )
local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
local isByteColorRange = display.getDefault( "isByteColorRange" )
local labelColor = { 0.60 }
local defaultRowColor = { 1 }
local blackColor = { 0 }
local screenScaleFactor = display.contentWidth / 320; -- factor that adjust the pickerWheel to work with config.content.width > 320
if isByteColorRange then
_widget._convertColorToV1( labelColor )
_widget._convertColorToV1( defaultRowColor )
_widget._convertColorToV1( blackColor )
end
-- Creates a new pickerWheel
local function createPickerWheel( pickerWheel, options )
-- Create a local reference to our options table
local opt = options
-- Forward references
local imageSheet, view, viewBackground, viewOverlay, viewColumns
-- Create the imageSheet
if opt.sheet then
imageSheet = opt.sheet
else
local themeData = require( opt.themeData )
imageSheet = graphics.newImageSheet( opt.themeSheetFile, themeData:getSheet() )
end
-- Create the view
view = display.newGroup()
-- The view's background
viewOverlay = display.newImageRect( pickerWheel, imageSheet, opt.overlayFrame, opt.overlayFrameWidth, opt.overlayFrameHeight )
----------------------------------
-- Properties
----------------------------------
-- The table which holds our pickerWheel columns
viewColumns = {}
-------------------------------------------------------
-- Assign properties to the view
-------------------------------------------------------
-- Assign properties to our view
view._width = opt.overlayFrameWidth
view._height = opt.overlayFrameHeight
view._top = opt.top
view._yPosition = pickerWheel.y + ( view._height * 0.5 )
-- Assign objects to our view
view._overlay = viewOverlay
view._background = viewBackground
view._columns = viewColumns
view._didTap = false
-------------------------------------------------------
-- Assign properties/objects to the pickerWheel
-------------------------------------------------------
-- Assign objects to the pickerWheel
pickerWheel._imageSheet = imageSheet
pickerWheel._view = view
pickerWheel:insert( view )
-- Function to render the pickerWheels columns
local function _renderColumns( event )
local phase = event.phase
local row = event.row
local font = event.target._font or opt.font
local fontSize = event.target._fontSize
local alignment = event.target._align
-- Create the column's title text
local rowTitle = display.newText( row, row._label, 0, 0, font, fontSize )
rowTitle.y = row.contentHeight * 0.5
-- when the widget is outside the view so no column is rendered, then _values does not exist, so we check for it
if pickerWheel._view._columns[row.id]._values then
if row.index == pickerWheel._view._columns[row.id]._values.index then
if ( event.target._fontColorSelected and type( event.target._fontColorSelected ) == "table" ) then
rowTitle:setFillColor( unpack( event.target._fontColorSelected ) )
else
rowTitle:setFillColor( unpack( blackColor ) )
end
else
if ( event.target._fontColor and type( event.target._fontColor ) == "table" ) then
rowTitle:setFillColor( unpack( event.target._fontColor ) )
else
rowTitle:setFillColor( unpack( labelColor ) )
end
end
else
if ( event.target._fontColor and type( event.target._fontColor ) == "table" ) then
rowTitle:setFillColor( unpack( event.target._fontColor ) )
else
rowTitle:setFillColor( unpack( labelColor ) )
end
end
row.value = rowTitle.text
-- check if the text is greater than the actual column size
local availableWidth = viewOverlay.width - 28 * screenScaleFactor
local columnWidth = view._columns[ row.id ].width or availableWidth / #view._columns
local textWidth = rowTitle.contentWidth
if textWidth > columnWidth - 1 then
--cap the text
local pixelsPerChar = 23 * screenScaleFactor -- aproximate median value
local numChars = columnWidth / pixelsPerChar
row._label = row._label:sub(1, numChars)
rowTitle.text = row._label
end
-- Align the text as requested
if "center" == alignment then
local rowTitleX
if isGraphicsV1 then
rowTitleX = row.x
else
rowTitleX = row.x + columnWidth * 0.5
end
rowTitle.x = rowTitleX
elseif "left" == alignment then
local rowTitleX
if isGraphicsV1 then
rowTitleX = ( rowTitle.contentWidth * 0.5 ) + 6 * screenScaleFactor
else
rowTitleX = row.x + 6 * screenScaleFactor
rowTitle.anchorX = 0
end
rowTitle.x = rowTitleX
elseif "right" == alignment then
local rowTitleX
if isGraphicsV1 then
rowTitleX = row.x + ( row.contentWidth * 0.5 ) - ( rowTitle.contentWidth * 0.5 ) - 6 * screenScaleFactor
else
rowTitleX = row.x + columnWidth - 6 * screenScaleFactor
rowTitle.anchorX = 1
end
rowTitle.x = rowTitleX
end
end
-- Create a background to sit behind the pickerWheel
viewBackground = display.newImageRect( view, imageSheet, opt.backgroundFrame, opt.overlayFrameWidth, opt.overlayFrameHeight )
viewBackground.x = viewOverlay.x
viewBackground.y = viewOverlay.y
-- Function to create the column separator
function view:_createSeparator( x )
local separator = display.newImageRect( self, imageSheet, opt.separatorFrame, opt.separatorFrameWidth + 4, opt.backgroundFrameHeight )
separator.x = x
return separator
end
-- The available width for the whole pickerWheel (to fit columns)
local availableWidth = viewOverlay.width - 28 * screenScaleFactor
-- local method that handles scrolling to the tapped / touched index
local function didTapValue( event )
local phase = event.phase
local row = event.target
if "tap" == phase or "release" == phase then
view._columns[ row.id ]:scrollToIndex( row.index )
view._didTap = true
end
end
-- Create the pickerWheel Columns (which are tableView's)
local topPadding = 84
local bottomPadding = 96
if isGraphicsV1 then
topPadding = 90
bottomPadding = pickerWheel.contentHeight - 20 - pickerWheel.contentHeight * 0.5 -- 20 is half a row height
end
topPadding = topPadding * screenScaleFactor
bottomPadding = bottomPadding * screenScaleFactor
local initialX = 0
local initialPos = -140
if _widget.isHolo() then
initialPos = -140
--else
--initialPos = -144
end
initialPos = initialPos * screenScaleFactor
for i = 1, #opt.columnData do
if i > 1 then
initialPos = viewColumns[i-1].x + ( viewColumns[i-1]._view._width * 0.5 )
end
local tableViewWidth = opt.columnData[i].width or availableWidth / #opt.columnData
tableViewWidth = tableViewWidth * screenScaleFactor
viewColumns[i] = _widget.newTableView
{
left = initialPos,
top = -110 * screenScaleFactor,
width = tableViewWidth,
height = opt.overlayFrameHeight - 1,
topPadding = topPadding,
bottomPadding = bottomPadding,
noLines = true,
hideBackground = false,
hideScrollBar = true,
friction = 0.92,
rowColor = opt.columnColor,
backgroundColor = opt.backgroundColor or defaultRowColor,
onRowRender = _renderColumns,
--maskFile = opt.maskFile,
listener = nil,
onRowTouch = didTapValue
}
viewColumns[i]._view._isUsedInPickerWheel = true
-- Column properties
viewColumns[i]._align = opt.columnData[i].align or "center"
viewColumns[i]._fontSize = opt.fontSize
viewColumns[i]._font = opt.font
viewColumns[i]._fontColor = opt.fontColor
viewColumns[i]._fontColorSelected = opt.fontColorSelected
viewColumns[i]._view._fontColor = opt.fontColor
-- Set the volumns initial values
viewColumns[i]._values =
{
index = opt.columnData[i].startIndex,
value = opt.columnData[i].labels[opt.columnData[i].startIndex],
}
-- Create the columns row's
for j = 1, #opt.columnData[i].labels do
viewColumns[i]:insertRow
{
rowHeight = opt.rowHeight,
rowColor = {
default = opt.columnColor,
over = opt.columnColor,
},
label = opt.columnData[i].labels[j],
id = i
}
end
-- Insert the pickerWheel column into the view
view:insert( viewColumns[i] )
-- Scroll to the defined index -- TODO needs failsafe
viewColumns[i]:scrollToIndex( opt.columnData[i].startIndex, 0 )
end
-- Create the column separators
if ( opt.separatorFrame and opt.separatorFrameWidth and opt.separatorFrameHeight and not _widget.isHolo() ) then
for i = 1, #opt.columnData - 1 do
view:_createSeparator( viewColumns[i].x + viewColumns[i]._view._width * 0.5 )
end
end
-- Push the view's background to the front.
viewOverlay:toFront()
----------------------------------------------------------
-- PUBLIC METHODS
----------------------------------------------------------
-- Function to retrieve the column values
function pickerWheel:getValues()
return self._view:_getValues()
end
-- Function to scroll to a specific pickerWheel column row
function pickerWheel:scrollToIndex( ... )
local arg = { ... }
local column = nil
-- If the first arg is a number, set the column to that
if "number" == type( arg[1] ) then
column = arg[1]
-- We have retrieved the column, now set arg1 to arg2 (which is the index to scroll to) so scrollTo index gets called as expected
arg[1] = arg[2]
end
arg[4] = self._view:_getValues()
-- Scroll to the specified column index
return self._view._columns[column]:scrollToIndex( unpack( arg ) )
end
----------------------------------------------------------
-- PRIVATE METHODS
----------------------------------------------------------
-- Override scale function as pickerWheels don't support it
function pickerWheel:scale()
print( M._widgetName, "Does not support scaling" )
end
-- EnterFrame listener for our pickerWheel
function view:enterFrame( event )
local _pickerWheel = self.parent
-- Update the y position
-- this has to be calculated in content coordinates to abstract the widget being in a group
local xPos, yPos = _pickerWheel:localToContent( 0, 0 )
if isGraphicsV1 then
self._yPosition = yPos + self.y + ( self._height * 0.5 )
else
self._yPosition = yPos + ( self._height * 0.5 )
end
-- Manage the Picker Wheels columns
for i = 1, #self._columns do
if "ended" == self._columns[i]._view._phase and not self._columns[i]._view._updateRuntime then
if not self._didTap then
local calculatePosition = self._yPosition - self.parent.contentHeight * 0.5
self._columns[i]._values = self._columns[i]._view:_getRowAtPosition( calculatePosition )
else
self._columns[i]._values = self._columns[i]._view:_getRowAtIndex( self._columns[ i ]._view._lastRowIndex )
self._didTap = false
end
self._columns[i]._view._phase = "none"
-- update the actual values, by rerendering row
if nil ~= self._columns[i]._values then
if ( self._columns[i]._fontColorSelected and type( self._columns[i]._fontColorSelected ) == "table" ) then
self._columns[i]._view._rows[self._columns[i]._values.index]._view[ 2 ]:setFillColor( unpack( self._columns[i]._fontColorSelected ) )
else
self._columns[i]._view._rows[self._columns[i]._values.index]._view[ 2 ]:setFillColor( unpack( blackColor ) )
end
end
end
end
-- Constrain x/y scale values to 1.0
if _pickerWheel.xScale ~= 1.0 then
_pickerWheel.xScale = 1.0
print( M._widgetName, "Does not support scaling" )
end
if _pickerWheel.yScale ~= 1.0 then
_pickerWheel.yScale = 1.0
print( M._widgetName, "Does not support scaling" )
end
return true
end
Runtime:addEventListener( "enterFrame", view )
-- Function to retrieve the column values
function view:_getValues()
local values = {}
-- Loop through all the columns and retrieve the values
for i = 1, #self._columns do
values[i] = self._columns[i]._values
end
return values
end
-- Finalize function for the pickerWheel
function pickerWheel:_finalize()
-- Remove the event listener
Runtime:removeEventListener( "enterFrame", self._view )
-- Set the ImageSheet to nil
self._imageSheet = nil
if imageSheet then imageSheet = nil; end
end
return pickerWheel
end
-- Function to create a new pickerWheel object ( widget.newPickerWheel )
function M.new( options, theme )
local customOptions = options or {}
local themeOptions = theme or {}
-- Create a local reference to our options table
local opt = M._options
-- Check if the requirements for creating a widget has been met (throws an error if not)
_widget._checkRequirements( customOptions, themeOptions, M._widgetName )
-------------------------------------------------------
-- Properties
-------------------------------------------------------
-- Positioning & properties
opt.left = customOptions.left or 0
opt.top = customOptions.top or 0
opt.x = customOptions.x or nil
opt.y = customOptions.y or nil
if customOptions.x and customOptions.y then
opt.left = 0
opt.top = 0
end
opt.id = customOptions.id
opt.baseDir = customOptions.baseDir or system.ResourceDirectory
--opt.maskFile = customOptions.maskFile or themeOptions.maskFile
opt.font = customOptions.font or themeOptions.font or native.systemFontBold
opt.fontSize = customOptions.fontSize or (themeOptions.fontSize and themeOptions.fontSize * screenScaleFactor) or 22 * screenScaleFactor
opt.fontColor = customOptions.fontColor or themeOptions.fontColor or labelColor
opt.fontColorSelected = customOptions.fontColorSelected or themeOptions.fontColorSelected or blackColor
opt.columnColor = customOptions.columnColor or themeOptions.columnColor or defaultRowColor
opt.backgroundColor = customOptions.columnColor or themeOptions.columnColor or defaultRowColor
if _widget.isSeven() then
opt.font = customOptions.font or themeOptions.font or "HelveticaNeue-Medium"
opt.fontSize = customOptions.fontSize or (themeOptions.fontSize and themeOptions.fontSize * screenScaleFactor) or 20 * screenScaleFactor
end
-- Properties
opt.rowHeight = customOptions.rowHeight or 40 * screenScaleFactor
opt.columnData = customOptions.columns
-- Frames & images
opt.sheet = customOptions.sheet
opt.themeSheetFile = themeOptions.sheet
opt.themeData = themeOptions.data
opt.backgroundFrame = customOptions.backgroundFrame or _widget._getFrameIndex( themeOptions, themeOptions.backgroundFrame )
opt.backgroundFrameWidth = customOptions.backgroundFrameWidth or (themeOptions.backgroundFrameWidth and themeOptions.backgroundFrameWidth * screenScaleFactor)
opt.backgroundFrameHeight = customOptions.backgroundFrameHeight or (themeOptions.backgroundFrameHeight and themeOptions.backgroundFrameHeight * screenScaleFactor)
opt.overlayFrame = customOptions.overlayFrame or _widget._getFrameIndex( themeOptions, themeOptions.overlayFrame )
opt.overlayFrameWidth = customOptions.overlayFrameWidth or (themeOptions.overlayFrameWidth and themeOptions.overlayFrameWidth * screenScaleFactor)
opt.overlayFrameHeight = customOptions.overlayFrameHeight or (themeOptions.overlayFrameHeight and themeOptions.overlayFrameHeight * screenScaleFactor)
opt.separatorFrame = customOptions.separatorFrame or _widget._getFrameIndex( themeOptions, themeOptions.separatorFrame ) or _widget._getFrameIndex( themeOptions, themeOptions.seperatorFrame ) or nil
opt.separatorFrameWidth = customOptions.separatorFrameWidth or (themeOptions.separatorFrameWidth and themeOptions.separatorFrameWidth * screenScaleFactor) or (themeOptions.seperatorFrameWidth and themeOptions.seperatorFrameWidth * screenScaleFactor) or nil
opt.separatorFrameHeight = customOptions.separatorFrameHeight or (themeOptions.separatorFrameHeight and themeOptions.separatorFrameHeight * screenScaleFactor) or (themeOptions.seperatorFrameHeight and themeOptions.seperatorFrameHeight * screenScaleFactor) or nil
-------------------------------------------------------
-- Create the pickerWheel
-------------------------------------------------------
-- Create the pickerWheel object
local pickerWheel = _widget._new
{
left = opt.left,
top = opt.top,
id = opt.id or "widget_pickerWheel",
baseDir = opt.baseDir,
}
-- Create the pickerWheel
createPickerWheel( pickerWheel, opt )
if isGraphicsV1 then
pickerWheel:setReferencePoint( display.TopLeftReferencePoint )
end
local x, y = _widget._calculatePosition( pickerWheel, opt )
pickerWheel.x, pickerWheel.y = x, y
return pickerWheel
end
return M