-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwidget-v1.lua
3189 lines (2641 loc) · 104 KB
/
widget-v1.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--****************************************************************************************
--
-- ====================================================================
-- Corona SDK Widget Module
-- ====================================================================
--
-- File: widget.lua
--
-- 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.
--
-- 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 modname = ...
local widget = {}
package.loaded[modname] = widget
widget.version = "0.7"
-- cached locals
local mAbs = math.abs
local mFloor = math.floor
-- defaults
local scrollFriction = 0.935
local pickerFriction = 0.88
-- modify factory function to ensure widgets are properly cleaned on group removal
local cached_displayNewGroup = display.newGroup
function display.newGroup()
local g = cached_displayNewGroup()
-- function to find/remove widgets within group
local function removeWidgets( group )
if group.numChildren then
for i=group.numChildren,1,-1 do
if group[i]._isWidget then
group[i]:removeSelf()
elseif not group[i]._isWidget and group[i].numChildren then
-- nested group (that is not a widget)
removeWidgets( group[i] )
end
end
end
end
-- store reference to original removeSelf method
local cached_removeSelf = g.removeSelf
-- subclass removeSelf method
function g:removeSelf()
removeWidgets( self ) -- remove widgets first
-- continue removing group as usual
if self.parent and self.parent.remove then
self.parent:remove( self )
end
end
return g
end
-- set current theme from external .lua module
function widget.setTheme( themeModule )
widget.theme = require( themeModule ) -- should return table w/ theme data
end
-- add 'setText()' method to display.newText (to be consistent with display.newEmbossed text)
local cached_newText = display.newText
function display.newText( ... )
local text = cached_newText( ... )
function text:setText( newString )
self.text = newString
end
return text
end
-- creates very sharp text for high resolution/high density displays
function widget.retinaText( ... )
text = display.newText( ... );
return text
end; display.newRetinaText = display.newText --widget.retinaText
-- creates sharp (retina) text with an embossed/inset effect
function widget.embossedText( ... )
local arg = { ... }
-- parse arguments
local parentG, w, h
local argOffset = 0
-- determine if a parentGroup was specified
if arg[1] and type(arg[1]) == "table" then
parentG = arg[1]; argOffset = 1
end
local string = arg[1+argOffset] or ""
local x = arg[2+argOffset] or 0
local y = arg[3+argOffset] or 0
local w, h = 0, 0
local newOffset = 3+argOffset
if type(arg[4+argOffset]) == "number" then w = arg[4+argOffset]; newOffset=newOffset+1; end
if w and #arg >= 7+argOffset then h = arg[5+argOffset]; newOffset=newOffset+1; end
local font = arg[1+newOffset] or native.systemFont
local size = arg[2+newOffset] or 12
local color = { 0, 0, 0, 255 }
---------------------------------------------
local r, g, b, a = color[1], color[2], color[3], color[4]
local textBrightness = ( r + g + b ) / 3
local highlight = display.newText( string, 0.5, 1, w, h, font, size )
if ( textBrightness > 127) then
highlight:setTextColor( 255, 255, 255, 20 )
else
highlight:setTextColor( 255, 255, 255, 140 )
end
local shadow = display.newText( string, -0.5, -1, w, h, font, size )
if ( textBrightness > 127) then
shadow:setTextColor( 0, 0, 0, 128 )
else
shadow:setTextColor( 0, 0, 0, 20 )
end
local label = display.newText( string, 0, 0, w, h, font, size )
label:setTextColor( r, g, b, a )
-- create display group, insert all embossed text elements, and position it
local text = display.newGroup()
text:insert( highlight ); text.highlight = highlight
text:insert( shadow ); text.shadow = shadow
text:insert( label ); text.label = label
text.x, text.y = x, y
text:setReferencePoint( display.CenterReferencePoint )
-- setTextColor method
function text:setTextColor( ... )
local r, g, b, a; local arg = { ... }
if #arg == 4 then
r = arg[1]; g = arg[2]; b = arg[3]; a = arg[4]
elseif #arg == 3 then
r = arg[1]; g = arg[2]; b = arg[3]; a = 255
elseif #arg == 2 then
r = arg[1]; g = r; b = r; a = arg[2]
elseif #arg == 1 then
if type(arg[1]) == "number" then
r = arg[1]; g = r; b = r; a = 255
end
end
local textBrightness = ( r + g + b ) / 3
if ( textBrightness > 127) then
self.highlight:setTextColor( 255, 255, 255, 20 )
self.shadow:setTextColor( 0, 0, 0, 128 )
else
self.highlight:setTextColor( 255, 255, 255, 140 )
self.shadow:setTextColor( 0, 0, 0, 20 )
end
self.label:setTextColor( r, g, b, a )
end
-- setText method
function text:setText( newString )
local newString = newString or self.text
self.highlight.text = newString
self.shadow.text = newString
self.label.text = newString
self.text = newString
end
-- setSize method
function text:setSize ( newSize )
local newSize = newSize or size
self.highlight.size = newSize
self.shadow.size = newSize
self.label.size = newSize
self.size = newSize
end
if parentG then parentG:insert( text ) end
text.text = string
return text
end; display.newEmbossedText = widget.embossedText
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--
-- button widget
--
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
function widget.newButton( options )
local function onButtonTouch( self, event ) -- self == button
local result = true
local phase = event.phase
event.name = "buttonEvent"
event.target = self
if phase == "began" then
display.getCurrentStage():setFocus( self, event.id )
self.isFocus = true
event.phase = "press"
if self.onEvent then
result = self.onEvent( event )
elseif self.onPress then
result = self.onPress( event )
end
self.default.isVisible = false
self.over.isVisible = true
local r, g, b, a = self.label.color.over[1] or 0, self.label.color.over[2] or self.label.color.over[1], self.label.color.over[3] or self.label.color.over[1], self.label.color.over[4] or 255
self.label:setTextColor( r, g, b, a )
elseif self.isFocus then
local bounds = self.contentBounds
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 phase == "moved" then
if not isWithinBounds then
self.default.isVisible = true
self.over.isVisible = false
local r, g, b, a = self.label.color.default[1] or 0, self.label.color.default[2] or self.label.color.default[1], self.label.color.default[3] or self.label.color.default[1], self.label.color.default[4] or 255
self.label:setTextColor( r, g, b, a )
else
self.default.isVisible = false
self.over.isVisible = true
local r, g, b, a = self.label.color.over[1] or 0, self.label.color.over[2] or self.label.color.over[1], self.label.color.over[3] or self.label.color.over[1], self.label.color.over[4] or 255
self.label:setTextColor( r, g, b, a )
end
if self.onEvent then
result = self.onEvent( event )
elseif self.onDrag then
result = self.onDrag( event )
end
elseif phase == "ended" or phase == "cancelled" then
if self.default and self.over then
self.default.isVisible = true
self.over.isVisible = false
local r, g, b, a = self.label.color.default[1] or 0, self.label.color.default[2] or self.label.color.default[1], self.label.color.default[3] or self.label.color.default[1], self.label.color.default[4] or 255
self.label:setTextColor( r, g, b, a )
end
-- trigger appropriate event listener if released within bounds of button
if isWithinBounds then
event.phase = "release"
if self.onEvent then
result = self.onEvent( event )
elseif self.onRelease then
result = self.onRelease( event )
end
end
-- remove focus from button
display.getCurrentStage():setFocus( self, nil )
self.isFocus = false
end
end
return result
end
local function setLabel( self, newLabel ) -- self == button
if not newLabel then return; end
if self.label.setText then
self.label:setText( newLabel )
else
self.label.text = newLabel
end
-- re-center label on button
self.label:setReferencePoint( display.CenterReferencePoint )
self.label.x = (self.contentWidth*0.5) + self.label.xOffset
self.label.y = (self.contentHeight*0.5) + self.label.yOffset
end
local function getLabel( self )
return self.label.text
end
local function removeSelf( self ) -- self == button
-- check to see if there is a clean method; if so, call it
if self.clean then self:clean(); end
-- remove all children of default and over
if self.default and self.default.numChildren then
for i=self.default.numChildren,1,-1 do
display.remove( self.default.numChildren[i] )
end
display.remove( self.default )
end
if self.over and self.over.numChildren then
for i=self.over.numChildren,1,-1 do
display.remove( self.over.numChildren[i] )
end
display.remove( self.over )
end
-- remove label
display.remove( self.label )
-- remove button group
self:cached_removeSelf()
end
local function createButton( options, theme )
local defaultBtnWidth = 124
local defaultBtnHeight = 42
local options = options or {}
local theme = theme or {}
local id = options.id or "widget_button"
local left = options.left or 0
local top = options.top or 0
local xOffset = options.xOffset or theme.xOffset or 0 -- offsets x value of the label text
local yOffset = options.yOffset or options.offset or theme.yOffset or theme.offset or 0 -- offsets y value of the label text
local label = options.label or ""
local font = options.font or theme.font or native.systemFont
local fontSize = options.fontSize or theme.fontSize or 14
local emboss = options.emboss or theme.emboss
local textFunction = display.newText; if emboss then textFunction = widget.embossedText; end
local labelColor = options.labelColor or theme.labelColor or { default={ 0 }, over={ 255 } }
local onPress = options.onPress
local onRelease = options.onRelease
local onDrag = options.onDrag
local onEvent = options.onEvent
local default = options.default or theme.default
local defaultColor = options.defaultColor or theme.defaultColor
local over = options.over or theme.over
local overColor = options.overColor or theme.overColor
local strokeColor = options.strokeColor or theme.strokeColor
local strokeWidth = options.strokeWidth or theme.strokeWidth
local cornerRadius = options.cornerRadius or theme.cornerRadius
local width = options.width or theme.width
local height = options.height or theme.height
local sheet = options.sheet or theme.sheet
local defaultIndex = options.defaultIndex or theme.defaultIndex
local overIndex = options.overIndex or theme.overIndex
local baseDir = options.baseDir or theme.baseDir or system.ResourceDirectory
local button = display.newGroup()
if default or sheet then
-- user-provided image for default and over state
if sheet and defaultIndex and width and height then
-- image sheet option
button.default = display.newImageRect( button, sheet, defaultIndex, width, height )
button.default:setReferencePoint( display.TopLeftReferencePoint )
button.default.x, button.default.y = 0, 0
local over = overIndex or defaultIndex
button.over = display.newImageRect( button, sheet, over, width, height )
button.over:setReferencePoint( display.TopLeftReferencePoint )
button.over.x, button.over.y = 0, 0
elseif width and height then
-- display.newImageRect() option (non)
button.default = display.newImageRect( button, default, baseDir, width, height )
button.default:setReferencePoint( display.TopLeftReferencePoint )
button.default.x, button.default.y = 0, 0
local over = over or default
button.over = display.newImageRect( button, over, baseDir, width, height )
button.over:setReferencePoint( display.TopLeftReferencePoint )
button.over.x, button.over.y = 0, 0
else
-- display.newImage() option
button.default = display.newImage( button, default, baseDir, true )
button.default:setReferencePoint( display.TopLeftReferencePoint )
button.default.x, button.default.y = 0, 0
local over = over or default
button.over = display.newImage( button, over, baseDir, true )
button.over:setReferencePoint( display.TopLeftReferencePoint )
button.over.x, button.over.y = 0, 0
width, height = button.default.contentWidth, button.default.contentHeight
end
if defaultColor then
if defaultColor[1] then
button.default:setFillColor( defaultColor[1], defaultColor[2] or defaultColor[1], defaultColor[3] or defaultColor[1], defaultColor[4] or 255 )
end
end
if overColor then
if overColor[1] then
button.over:setFillColor( overColor[1], overColor[2] or overColor[1], overColor[3] or overColor[1], overColor[4] or 255 )
end
end
else
-- no images; construct button using newRoundedRect
if not width then width = defaultBtnWidth; end
if not height then height = defaultBtnHeight; end
if not cornerRadius then cornerRadius = 8; end
button.default = display.newRoundedRect( button, 0, 0, width, height, cornerRadius )
button.over = display.newRoundedRect( button, 0, 0, width, height, cornerRadius )
if defaultColor and defaultColor[1] then
button.default:setFillColor( defaultColor[1], defaultColor[2] or defaultColor[1], defaultColor[3] or defaultColor[1], defaultColor[4] or 255 )
else
button.default:setFillColor( 255 )
end
if overColor and overColor[1] then
button.over:setFillColor( overColor[1], overColor[2] or overColor[1], overColor[3] or overColor[1], overColor[4] or 255 )
else
button.over:setFillColor( 128 )
end
if strokeColor and strokeColor[1] then
button.default:setStrokeColor( strokeColor[1], strokeColor[2] or strokeColor[1], strokeColor[3] or strokeColor[1], strokeColor[4] or 255 )
button.over:setStrokeColor( strokeColor[1], strokeColor[2] or strokeColor[1], strokeColor[3] or strokeColor[1], strokeColor[4] or 255 )
else
button.default:setStrokeColor( 0 )
button.over:setStrokeColor( 0 )
end
if not strokeWidth then
button.default.strokeWidth = 1
button.over.strokeWidth = 1
else
button.default.strokeWidth = strokeWidth
button.over.strokeWidth = strokeWidth
end
end
button.over.isVisible = false -- hide "down/over" state of button
-- create the label
if not labelColor then labelColor = {}; end
if not labelColor.default then labelColor.default = { 0 }; end
if not labelColor.over then labelColor.over = { 255 }; end
local r, g, b, a = labelColor.default[1] or 0, labelColor.default[2] or labelColor.default[1], labelColor.default[3] or labelColor.default[1], labelColor.default[4] or 255
button.label = textFunction( button, label, 0, 0, font, fontSize )
button.label:setTextColor( r, g, b, a )
button.label:setReferencePoint( display.CenterReferencePoint )
button.label.x = (button.contentWidth * 0.5) + xOffset
button.label.y = (button.contentHeight * 0.5) + yOffset
button.label.color = labelColor
button.label.xOffset = xOffset
button.label.yOffset = yOffset
-- set properties and methods
button._isWidget = true
button.id = id
button.onPress = onPress
button.onDrag = onDrag
button.onRelease = onRelease
button.onEvent = onEvent
button.touch = onButtonTouch; button:addEventListener( "touch", button )
button.cached_removeSelf = button.removeSelf
button.removeSelf = removeSelf
button.setLabel = setLabel
button.getLabel = getLabel
-- position the button
button:setReferencePoint( display.TopLeftReferencePoint )
button.x, button.y = left, top
button:setReferencePoint( display.CenterReferencePoint )
return button
end
-- this widget supports visual customization via themes
local themeOptions
if widget.theme then
local buttonTheme = widget.theme.button
if buttonTheme then
if options and options.style then -- style parameter optionally set by user
-- for themes that support various "styles" per widget
local style = buttonTheme[options.style]
if style then themeOptions = style; end
else
-- if no style parameter set, use default style specified by theme
themeOptions = buttonTheme
end
end
end
return createButton( options, themeOptions )
end
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--
-- slider widget
--
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
function widget.newSlider( options )
-- set slider value from 0 to 100
local function setSliderValue( self, value ) -- self == slider
-- make sure value is not less than 0 or greater than 100
if value < 0 then
value = 0
elseif value > 100 then
value = 100
else
value = mFloor( value ) -- round to the nearest whole number
end
local width = self.max - self.min
-- calculate percentage based on slidable width
local percent = value / 100
-- move handle to new position
local x = (width * percent) + self.min
self.handle.x = x
-- stretch fill image from left side to handle
local fillScaleX = (self.handle.x - self.min) / self.fillWidth
if fillScaleX <= 0 then fillScaleX = 0.1; end
self.fill.xScale = fillScaleX
-- update reference to value
self.value = value
end
-- dispatch slider event
local function dispatchSliderEvent( self ) -- self == slider
if self.listener then
local e = {}
e.name = "sliderEvent"
e.type = "sliderMoved"
e.target = self
e.value = self.value
self.listener( e )
end
end
-- slider touch event
local function onSliderTouch( self, event ) -- self == slider
if event.phase == "began" then
display.getCurrentStage():setFocus( self )
self.isFocus = true
self:setReferencePoint( display.CenterReferencePoint )
local sliderX = (self.contentBounds.xMin + (self.contentWidth*0.5))
local x = event.x - sliderX
local width = self.max - self.min
local percent = mFloor(((( (width*0.5) + x) * 100) / width))
self:setValue( percent )
dispatchSliderEvent( self )
elseif self.isFocus then
local isWithinBounds = self.min <= event.x and self.max >= event.x
if event.phase == "moved" then
local sliderX = (self.contentBounds.xMin + (self.contentWidth*0.5))
local x = event.x - sliderX
local width = self.max - self.min
local percent = mFloor(((( (width*0.5) + x) * 100) / width))
self:setValue( percent )
dispatchSliderEvent( self )
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end
-- removeSelf() method for slider widget
local function removeSelf( self )
if self.clean and type(self.clean) == "function" then self:clean(); end
if self.fill then self.fill:removeSelf(); self.fill = nil; end
if self.handle then self.handle:removeSelf(); self.handle = nil; end
self.fillWidth = nil
self.value = nil
self:cached_removeSelf()
end
local function createSlider( options, theme )
local options = options or {}
local theme = theme or {}
local id = options.id or "widget_slider"
local left = options.left or 0
local top = options.top or 0
local width = options.width or theme.width or 200
local height = options.height or theme.height or 10
local background = options.background or theme.background
local handleImage = options.handle or theme.handle
local handleWidth = options.handleWidth or theme.handleWidth
local handleHeight = options.handleHeight or theme.handleHeight
local leftImage = options.leftImage or theme.leftImage
local leftWidth = options.leftWidth or theme.leftWidth or 16
local fillImage = options.fillImage or theme.fillImage
local fillWidth = options.fillWidth or theme.fillWidth or 2
local cornerRadius = options.cornerRadius or theme.cornerRadius or 5
local value = options.value or 50
local listener = options.listener or options.callback
local baseDir = options.baseDir or theme.baseDir or system.ResourceDirectory
local fillColor = options.fillColor or theme.fillColor or {}
fillColor[1] = fillColor[1] or 0
fillColor[2] = fillColor[2] or 100
fillColor[3] = fillColor[3] or 230
fillColor[4] = fillColor[4] or 255
local handleColor = options.handleColor or theme.handleColor or {}
handleColor[1] = handleColor[1] or 189
handleColor[2] = handleColor[2] or 189
handleColor[3] = handleColor[3] or 189
handleColor[4] = handleColor[4] or 255
local handleStroke = options.handleStroke or theme.handleStroke or {}
handleStroke[1] = handleStroke[1] or 143
handleStroke[2] = handleStroke[2] or 143
handleStroke[3] = handleStroke[3] or 143
handleStroke[4] = handleStroke[4] or 255
local bgFill = options.bgFill or theme.bgFill or {}
bgFill[1] = bgFill[1] or 225
bgFill[2] = bgFill[2] or 225
bgFill[3] = bgFill[3] or 225
bgFill[4] = bgFill[4] or 255
local bgStroke = options.bgStroke or theme.bgStroke or {}
bgStroke[1] = bgStroke[1] or 102
bgStroke[2] = bgStroke[2] or 102
bgStroke[3] = bgStroke[3] or 102
bgStroke[4] = bgStroke[4] or 255
-- construct slider widget based on provided parameters (or defaults)
local slider = display.newGroup()
local bg, leftSide, fill, handle
if not background and not fillImage then
bg = display.newRoundedRect( slider, 0, 0, width, height, cornerRadius )
bg.strokeWidth = 1
bg:setStrokeColor( bgStroke[1], bgStroke[2], bgStroke[3], bgStroke[4] )
bg:setFillColor( bgFill[1], bgFill[2], bgFill[3], bgFill[4] )
leftSide = display.newRoundedRect( slider, 0, 0, leftWidth, height, cornerRadius )
leftSide:setReferencePoint( display.CenterReferencePoint )
leftSide:setFillColor( fillColor[1], fillColor[2], fillColor[3], fillColor[4] )
fill = display.newRect( slider, leftWidth*0.5, 0, fillWidth, height )
fill:setReferencePoint( display.CenterLeftReferencePoint )
fill:setFillColor( fillColor[1], fillColor[2], fillColor[3], fillColor[4] )
elseif background and fillImage then
bg = display.newImageRect( slider, background, baseDir, width, height )
bg:setReferencePoint( display.TopLeftReferencePoint )
bg.x, bg.y = 0, 0
fill = display.newImageRect( slider, fillImage, baseDir, fillWidth, height )
fill:setReferencePoint( display.CenterLeftReferencePoint )
fill.x, fill.y = leftWidth, height * 0.5
else
if background and not fillImage then
print( "WARNING: You must also specify a fillImage when using a custom background with the slider widget." )
return
elseif fillImage and not background then
print( "WARNING: You must specify a custom background when using a custom fillImage with the slider widget." )
return
end
end
slider.fill = fill
slider.fillWidth = fillWidth
if not handleImage or not handleWidth or not handleHeight then
handle = display.newCircle( slider, width*0.5, height*0.5, height )
handle:setReferencePoint( display.CenterReferencePoint )
handle:setFillColor( handleColor[1], handleColor[2], handleColor[3], handleColor[4] )
handle.strokeWidth = 1
handle:setStrokeColor( handleStroke[1], handleStroke[2], handleStroke[3], handleStroke[4] )
else
handle = display.newImageRect( slider, handleImage, handleWidth, handleHeight )
handle:setReferencePoint( display.CenterReferencePoint )
handle.x, handle.y = width*0.5, height*0.5
end
slider.handle = handle
-- properties and methods
slider._isWidget = true
slider.id = id
slider.min = leftWidth*0.5
slider.max = width - (leftWidth * 0.5)
slider.setValue = setSliderValue
slider.touch = onSliderTouch
slider:addEventListener( "touch", slider )
slider.listener = listener
slider.cached_removeSelf = slider.removeSelf
slider.removeSelf = removeSelf
local fillScaleX = (handle.x - slider.min) / fillWidth
fill.xScale = fillScaleX
-- position the widget and set reference point to center
slider.x, slider.y = left, top
slider:setReferencePoint( display.CenterReferencePoint )
-- set initial value
slider:setValue( value )
return slider
end
-- this widget supports visual customization via themes
local themeOptions
if widget.theme then
local sliderTheme = widget.theme.slider
if sliderTheme then
if options and options.style then -- style parameter optionally set by user
-- for themes that support various "styles" per widget
local style = sliderTheme[options.style]
if style then themeOptions = style; end
else
-- if no style parameter set, use default style specified by theme
themeOptions = sliderTheme
end
end
end
return createSlider( options, themeOptions )
end
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--
-- pickerWheel widget
--
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--Function to handle the soft-landing of the picker wheel
local function pickerSoftLand( self )
local target = self.parent
--Variables that equal the ones used in picker.getValues
local height = self.height
local selectionHeight = self.selectionHeight
local top = self.parent.parent.parent.y --Get the actual pickers groups y position to use as the top position
local selectionTop = target.topPadding
--Index to scroll to
local index = nil
--Get row using same system at picker.getValues uses
if target:getRowAtCoordinate( top + selectionTop + ( selectionHeight * 0.5 ) ) ~= nil then
index = target:getRowAtCoordinate( top + selectionTop + ( selectionHeight * 0.5 ) ).index
end
--If there is an index, scroll to it to give the impression of soft landing
if index ~= nil then
target:scrollToIndex( index, 400 )
end
end
function widget.newPickerWheel( options )
-- get selection values of pickerWheel columns (returns table)
local function getValues( self ) -- self == pickerWheel
local columnValues = {}
local columns = self.columns
local top = self.y
local selectionTop = self.selectionTop or 255
local selectionHeight = self.selectionHeight or 46
--print( selectionTop)
for i=1,columns.numChildren do
local col = columns[i]
local realSelectionY = top + selectionTop + (selectionHeight*0.5)
local row = col:getRowAtCoordinate( realSelectionY )
if row and row.value and row.index then
columnValues[i] = {}
columnValues[i].value = row.value
columnValues[i].index = row.index
end
end
return columnValues
end
-- creates new pickerWheel column
local function newPickerColumn( pickerWheel, parentGroup, columnData, params )
local column = widget.newTableView( params )
-- create individual 'rows' for the column
for i=1,#columnData do
local labelX = 14
local ref = display.CenterLeftReferencePoint
if columnData.alignment and columnData.alignment ~= "left" then
if columnData.alignment == "center" then
labelX = params.width * 0.5
ref = display.CenterReferencePoint
elseif columnData.alignment == "right" then
labelX = params.width - 14
ref = display.CenterRightReferencePoint
end
end
local function renderRow( event )
local row = event.row
local view = event.view
local fc = params.fontColor
local label = display.newText( columnData[i], 0, 0, params.font, params.fontSize )
label:setTextColor( fc[1], fc[2], fc[3], fc[4] )
label:setReferencePoint( ref )
label.x = labelX
label.y = row.height * 0.5
row.value = columnData[i]
view:insert( label )
end
column:insertRow{
onRender = renderRow,
width = params.width,
height = params.rowHeight or 32,
rowColor = params.bgColor or { 255, 255, 255, 255 },
lineColor = params.bgColor or { 255, 255, 255, 255 },
skipRender = true,
}
end
parentGroup:insert( column )
return column
end
-- subclassed removeSelf method for pickerWheel
local function removeSelf( self ) -- self == pickerWheel
-- check to see if there is a clean method; if so, call it
if self.clean then self:clean(); end
-- remove mask if it exists
if self.mask then
self.columns:setMask( nil )
self.mask = nil
end
-- remove each column one by one
for i=self.columns.numChildren,1,-1 do
self.columns[i]:removeSelf()
end
self.columns = nil
-- remove pickerWheel widget
self:cached_removeSelf()
end
local function createPickerWheel( options, themeOptions )
local options = options or {}
local theme = themeOptions or {}
-- parse parameters (options) or set defaults (or theme defaults)
local id = options.id or "widget_pickerWheel"
local left = options.left or 0
local top = options.top or 0
local width = options.width or theme.width or 296
local height = options.height or theme.height or 222
local bgWidth = options.bgWidth or options.totalWidth or theme.bgWidth or theme.totalWidth or display.contentWidth
local selectionTop = options.selectionTop or theme.selectionTop or 90
local selectionHeight = options.selectionHeight or theme.selectionHeight or 46
local font = options.font or theme.font or system.nativeFontBold
local fontSize = options.fontSize or theme.fontSize or 22
local fontColor = options.fontColor or theme.fontColor or {}
fontColor[1] = fontColor[1] or 0
fontColor[2] = fontColor[2] or fontColor[1]
fontColor[3] = fontColor[3] or fontColor[1]
fontColor[4] = fontColor[4] or 255
local columnColor = options.columnColor or theme.columnColor or {}
columnColor[1] = columnColor[1] or 255
columnColor[2] = columnColor[2] or columnColor[1]
columnColor[3] = columnColor[3] or columnColor[1]
columnColor[4] = columnColor[4] or 255
local columns = options.columns or { { "One", "Two", "Three", "Four", "Five" } }
local maskFile = options.maskFile or theme.maskFile
local bgImage = options.bgImage or options.background or theme.bgImage or theme.background
local bgImageWidth = options.bgImageWidth or options.backgroundWidth or theme.bgImageWidth or theme.backgroundWidth
local bgImageHeight = options.bgImageHeight or options.backgroundHeight or theme.bgImageHeight or theme.backgroundHeight or height
local overlayImage = options.overlayImage or options.glassFile or theme.overlayImage or theme.glassFile
local overlayWidth = options.overlayWidth or options.glassWidth or theme.overlayWidth or theme.glassWidth
local overlayHeight = options.overlayHeight or options.glassHeight or theme.overlayHeight or theme.glassHeight
local separator = options.separator or theme.separator
local separatorWidth = options.separatorWidth or theme.separatorWidth
local separatorHeight = options.separatorHeight or theme.separatorHeight
local baseDir = options.baseDir or theme.baseDir or system.ResourceDirectory
local pickerWheel = display.newGroup()
local columnGroup = display.newGroup() -- will hold all column groups (tableViews)
-- create background image
if bgImage then
local bg = display.newImageRect( pickerWheel, bgImage, baseDir, bgImageWidth, bgImageHeight )
bg:setReferencePoint( display.TopLeftReferencePoint )
bg.x, bg.y = 0, 0
bg.xScale = bgWidth / bg.contentWidth
local function disableTouchLeak() return true; end
bg.touch = disableTouchLeak
bg:addEventListener( "touch", bg )
end
-- insert the columns group into the pickerWheel widget group
pickerWheel:insert( columnGroup )
columnGroup.x = (bgWidth * 0.5) - width * 0.5
columnGroup.y = 0
local currentX = 0 -- variable that used for x-location of each column
-- create all columns
for i=1,#columns do
local col = columns[i]
-- set up tableView options (each column is a tableView widget)
local params = {}
-- tableView specific parameters
params.id = "pickerColumn_" .. i
params.renderThresh = (height - selectionTop) + selectionHeight
params.left = 0
params.top = 0
params.topPadding = selectionTop
params.bottomPadding = height - (selectionTop+selectionHeight)
params.width = col.width or width/#columns
params.height = height
params.bgColor = columnColor
params.friction = pickerFriction
params.keepRowsPastTopVisible = true
params.hideScrollBar = true
--Used for controlling the pickers softlanding
params.selectionHeight = selectionHeight
params.isPicker = true
params.pickerTop = top
-- if last column, ensure width fills remaining space
if i == #columns then params.width = width - currentX; end
-- picker-specific parameters
params.rowHeight = selectionHeight
params.font = font