-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathwidget_button.lua
More file actions
executable file
·1464 lines (1199 loc) · 44.6 KB
/
widget_button.lua
File metadata and controls
executable file
·1464 lines (1199 loc) · 44.6 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
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
-- 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.newButton",
}
-- Require needed widget files
local _widget = require( "widget" )
local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
local isByteColorRange = display.getDefault( "isByteColorRange" )
-- Function to handle touches on a widget button, function is common to all widget button creation types (ie image files, imagesheet, and 9 slice button creation)
local function manageButtonTouch( view, event )
local phase = event.phase
-- If the button isn't active, just return
if not view._isEnabled then
return
end
if "began" == phase then
-- Set the button to it's over image state
view:_setState( "over" )
-- Create the alpha fade if the theme has it
if view._hasAlphaFade then
if view._label then
transition.to( view._label, { time = 50, alpha = 0.5 } )
else
transition.to( view, { time = 50, alpha = 0.5 } )
end
end
-- If there is a onPress method ( and not a onEvent method )
if view._onPress and not view._onEvent then
view._onPress( event )
end
-- If the parent group still exists
if "table" == type( view.parent ) then
-- Set focus on the button
view._isFocus = true
display.getCurrentStage():setFocus( view, event.id )
end
elseif view._isFocus then
if "moved" == phase then
if not _widget._isWithinBounds( view.parent, event ) then
-- Set the button to it's default image state
view:_setState( "default" )
-- Create the alpha fade if the theme has it
if view._hasAlphaFade then
if view._label then
transition.to( view._label, { time = 50, alpha = 1.0 } )
else
transition.to( view, { time = 50, alpha = 1.0 } )
end
end
else
if view:_getState() ~= "over" then
-- Set the button to it's over image state
view:_setState( "over" )
-- Create the alpha fade if the theme has it
if view._hasAlphaFade then
if view._label then
transition.to( view._label, { time = 50, alpha = 0.5 } )
else
transition.to( view, { time = 50, alpha = 0.5 } )
end
end
end
end
elseif "ended" == phase or "cancelled" == phase then
if _widget._isWithinBounds( view.parent, event ) then
-- If there is a onRelease method ( and not a onEvent method )
if view._onRelease and not view._onEvent then
view._onRelease( event )
end
end
-- Set the button to it's default image state
view:_setState( "default" )
-- Create the alpha fade if the theme has it
if view._hasAlphaFade then
if view._label then
transition.to( view._label, { time = 50, alpha = 1.0 } )
else
transition.to( view, { time = 50, alpha = 1.0 } )
end
end
-- Remove focus from the button
view._isFocus = false
display.getCurrentStage():setFocus( nil )
end
end
-- If there is a onEvent method ( and not a onPress or onRelease method )
if view._onEvent and not view._onPress and not view._onRelease then
-- corrected: phase becomes cancelled if outside the bounds of the widget, not the view's.
if not _widget._isWithinBounds( view.parent, event ) and "ended" == phase then
event.phase = "cancelled"
end
view._onEvent( event )
end
end
------------------------------------------------------------------------
-- Text only button
------------------------------------------------------------------------
local function createUsingText( button, options )
-- Create a local reference to our options table
local opt = options
-- Forward references
local view
-- Create the label (either embossed or standard)
if opt.embossedLabel then
view = display.newEmbossedText( button, opt.label, 0, 0, opt.font, opt.fontSize )
else
view = display.newText( button, opt.label, 0, 0, opt.font, opt.fontSize )
end
-- Set the view's color
view:setFillColor( unpack( opt.labelColor.default ) )
view._labelColor = opt.labelColor
----------------------------------
-- Positioning
----------------------------------
-- The view
view.x = button.x + ( view.contentWidth * 0.5 )
view.y = button.y + ( view.contentHeight * 0.5 )
-------------------------------------------------------
-- Assign properties/objects to the view
-------------------------------------------------------
view._isEnabled = opt.isEnabled
view._pressedState = "default"
view._fontSize = opt.fontSize
view._labelColor = view._labelColor
view._hasAlphaFade = opt.hasAlphaFade
-- Methods
view._onPress = opt.onPress
view._onRelease = opt.onRelease
view._onEvent = opt.onEvent
-------------------------------------------------------
-- Assign properties/objects to the button
-------------------------------------------------------
-- Assign objects to the button
button._view = view
----------------------------------------------------------
-- PUBLIC METHODS
----------------------------------------------------------
-- Function to set the buttons text color
function button:setFillColor( ... )
self._view:setFillColor( ... )
end
-- Function to set the button's label
function button:setLabel( newLabel )
return self._view:_setLabel( newLabel )
end
-- Function to get the button's label
function button:getLabel()
return self._view:_getLabel()
end
-- Function to set a button as active
function button:setEnabled( isEnabled )
self._view._isEnabled = isEnabled
end
-- Touch listener for our button
function button:touch( event )
-- Set the target to the view's parent group (the button object)
event.target = self
-- Manage touch events on the button
manageButtonTouch( self._view, event )
return true
end
button:addEventListener( "touch", button )
----------------------------------------------------------
-- PRIVATE METHODS
----------------------------------------------------------
-- Function to set the button's label
function view:_setLabel( newLabel )
-- Update the label's text
if "function" == type( self.setText ) then
self:setText( newLabel )
else
self.text = newLabel
end
end
-- Function to get the button's label
function view:_getLabel()
return self._label.text
end
-- Function to set the buttons current state
function view:_setState( state )
local newState = state
if "over" == newState then
-- Set the label to it's over color
if "table" == type( self ) then
self:setFillColor( unpack( self._labelColor.over ) )
end
-- The pressedState is now "over"
self._pressedState = "over"
elseif "default" == newState then
-- Set the label back to it's default color
if "table" == type( self ) then
self:setFillColor( unpack( self._labelColor.default ) )
end
-- The pressedState is now "default"
self._pressedState = "default"
end
end
-- Function to get the buttons current state
function view:_getState()
return self._pressedState
end
-- Lose focus function
function button:_loseFocus()
self._view:_setState( "default" )
-- Create the alpha fade if the theme has it
if self._view._hasAlphaFade then
if self._view._label then
transition.to( self._view._label, { time = 50, alpha = 1.0 } )
else
transition.to( self._view, { time = 50, alpha = 1.0 } )
end
end
end
-- Finalize function
function button:_finalize()
end
return button
end
------------------------------------------------------------------------
-- Image Files Button
------------------------------------------------------------------------
-- Creates a new button from single png images
local function createUsingImageFiles( button, options )
-- Create a local reference to our options table
local opt = options
-- Forward references
local view, viewOver, viewLabel
-- Create the view
if opt.width and opt.height then
view = display.newImageRect( button, opt.defaultFile, opt.baseDir, opt.width, opt.height )
else
view = display.newImage( button, opt.defaultFile, opt.baseDir )
end
-- Create the view 'over' object
if opt.width and opt.height then
viewOver = display.newImageRect( button, opt.overFile, opt.baseDir, opt.width, opt.height )
else
viewOver = display.newImage( button, opt.overFile, opt.baseDir )
end
-- Create the label (either embossed or standard)
if opt.embossedLabel then
viewLabel = display.newEmbossedText( button, opt.label, 0, 0, opt.font, opt.fontSize )
else
viewLabel = display.newText( button, opt.label, 0, 0, opt.font, opt.fontSize )
end
----------------------------------
-- Positioning
----------------------------------
-- The view
view.x = button.x + ( view.contentWidth * 0.5 )
view.y = button.y + ( view.contentHeight * 0.5 )
viewOver.x = view.x
viewOver.y = view.y
viewOver.isVisible = false
-- Setup the label
viewLabel:setFillColor( unpack( opt.labelColor.default ) )
viewLabel._isLabel = true
viewLabel._labelColor = opt.labelColor
-- If there isn't an over color defined, fall back to default label color
if not viewLabel._labelColor.over then
viewLabel._labelColor.over = viewLabel._labelColor.default
end
-- Labels position
if "center" == opt.labelAlign then
viewLabel.x = view.x + opt.labelXOffset
elseif "left" == opt.labelAlign then
viewLabel.x = button.x + ( viewLabel.contentWidth * 0.5 ) + 10 + opt.labelXOffset
elseif "right" == opt.labelAlign then
viewLabel.x = view.x + ( view.contentWidth * 0.5 ) - ( viewLabel.contentWidth * 0.5 ) - 10 + opt.labelXOffset
end
-- Set the labels y position
viewLabel.y = button.y + ( view.contentHeight * 0.5 ) + opt.labelYOffset
-------------------------------------------------------
-- Assign properties/objects to the view
-------------------------------------------------------
view._isEnabled = opt.isEnabled
view._pressedState = "default"
view._fontSize = opt.fontSize
view._label = viewLabel
view._labelColor = viewLabel._labelColor
view._labelAlign = opt.labelAlign
view._labelXOffset = opt.labelXOffset
view._labelYOffset = opt.labelYOffset
view._over = viewOver
view._hasAlphaFade = opt.hasAlphaFade
-- Methods
view._onPress = opt.onPress
view._onRelease = opt.onRelease
view._onEvent = opt.onEvent
-------------------------------------------------------
-- Assign properties/objects to the button
-------------------------------------------------------
-- Assign objects to the button
button._view = view
----------------------------------------------------------
-- PUBLIC METHODS
----------------------------------------------------------
-- Function to set the buttons fill color
function button:setFillColor( ... )
self._view:setFillColor( ... )
self._view._over:setFillColor( ... )
end
-- Function to set the button's label
function button:setLabel( newLabel )
return self._view:_setLabel( newLabel )
end
-- Function to get the button's label
function button:getLabel()
return self._view:_getLabel()
end
-- Function to set a button as active
function button:setEnabled( isEnabled )
self._view._isEnabled = isEnabled
end
-- Touch listener for our button
function button:touch( event )
-- Set the target to the view's parent group (the button object)
event.target = self
-- Manage touch events on the button
manageButtonTouch( self._view, event )
return true
end
button:addEventListener( "touch", button )
----------------------------------------------------------
-- PRIVATE METHODS
----------------------------------------------------------
-- Function to set the button's label
function view:_setLabel( newLabel )
-- Update the label's text
if "function" == type( self._label.setText ) then
self._label:setText( newLabel )
else
self._label.text = newLabel
end
-- Labels position
if "center" == self._labelAlign then
self._label.x = self.x + self._labelXOffset
elseif "left" == self._labelAlign then
self._label.x = self.x - ( self.contentWidth * 0.5 ) + ( self._label.contentWidth * 0.5 ) + 10 + self._labelXOffset
elseif "right" == self._labelAlign then
self._label.x = self.x + ( self.contentWidth * 0.5 ) - ( self._label.contentWidth * 0.5 ) - 10 + self._labelXOffset
end
-- Update the label's y position
self._label.y = self._label.y
end
-- Function to get the button's label
function view:_getLabel()
return self._label.text
end
-- Function to set the buttons current state
function view:_setState( state )
local newState = state
if "over" == newState then
-- Set the button to the over image state
self.isVisible = false
self._over.isVisible = true
-- Set the label to it's over color
if "table" == type( self._label ) then
self._label:setFillColor( unpack( self._label._labelColor.over ) )
end
-- The pressedState is now "over"
self._pressedState = "over"
elseif "default" == newState then
-- Set the piece to the default image state
self.isVisible = true
self._over.isVisible = false
-- Set the label back to it's default color
if "table" == type( self._label ) then
self._label:setFillColor( unpack( self._label._labelColor.default ) )
end
-- The pressedState is now "default"
self._pressedState = "default"
end
end
-- Function to get the buttons current state
function view:_getState()
return self._pressedState
end
-- Lose focus function
function button:_loseFocus()
self._view:_setState( "default" )
-- Create the alpha fade if the theme has it
if self._view._hasAlphaFade then
if self._view._label then
transition.to( self._view._label, { time = 50, alpha = 1.0 } )
else
transition.to( self._view, { time = 50, alpha = 1.0 } )
end
end
end
-- Finalize function
function button:_finalize()
end
return button
end
------------------------------------------------------------------------
-- Image Sheet (2 Frame) Button
------------------------------------------------------------------------
-- Creates a new button from a sprite (imageSheet)
local function createUsingImageSheet( button, options )
-- Create a local reference to our options table
local opt = options
-- Animation options
local sheetOptions =
{
{
name = "default",
start = opt.defaultFrame,
count = 1,
},
{
name = "over",
start = opt.overFrame,
count = 1,
},
}
-- Forward references
local view, viewLabel
-- Create a reference to the imageSheet
local imageSheet = opt.sheet
-- Create the view
view = display.newSprite( button, imageSheet, sheetOptions )
-- Create the label (either embossed or standard)
if opt.embossedLabel then
viewLabel = display.newEmbossedText( button, opt.label, 0, 0, opt.font, opt.fontSize )
else
viewLabel = display.newText( button, opt.label, 0, 0, opt.font, opt.fontSize )
end
----------------------------------
-- Positioning
----------------------------------
-- The view
view.x = button.x + ( view.contentWidth * 0.5 )
view.y = button.y + ( view.contentHeight * 0.5 )
-- Setup the label
viewLabel:setFillColor( unpack( opt.labelColor.default ) )
viewLabel._isLabel = true
viewLabel._labelColor = opt.labelColor
-- If there isn't an over color defined, fall back to default label color
if not viewLabel._labelColor.over then
viewLabel._labelColor.over = viewLabel._labelColor.default
end
-- Labels position
if "center" == opt.labelAlign then
viewLabel.x = view.x + opt.labelXOffset
elseif "left" == opt.labelAlign then
viewLabel.x = view.x - ( view.contentWidth * 0.5 ) + ( viewLabel.contentWidth * 0.5 ) + 10 + opt.labelXOffset
elseif "right" == opt.labelAlign then
viewLabel.x = view.x + ( view.contentWidth * 0.5 ) - ( viewLabel.contentWidth * 0.5 ) - 10 + opt.labelXOffset
end
-- Set the labels y position
viewLabel.y = button.y + ( view.contentHeight * 0.5 ) + opt.labelYOffset
-------------------------------------------------------
-- Assign properties/objects to the view
-------------------------------------------------------
view._isEnabled = opt.isEnabled
view._pressedState = "default"
view._fontSize = opt.fontSize
view._label = viewLabel
view._labelColor = viewLabel._labelColor
view._labelAlign = opt.labelAlign
view._labelXOffset = opt.labelXOffset
view._labelYOffset = opt.labelYOffset
view._hasAlphaFade = opt.hasAlphaFade
-- Methods
view._onPress = opt.onPress
view._onRelease = opt.onRelease
view._onEvent = opt.onEvent
-------------------------------------------------------
-- Assign properties/objects to the button
-------------------------------------------------------
-- Assign objects to the button
button._imageSheet = imageSheet
button._view = view
----------------------------------------------------------
-- PUBLIC METHODS
----------------------------------------------------------
-- Function to set the buttons fill color
function button:setFillColor( ... )
self._view:setFillColor( ... )
end
-- Function to set the button's label
function button:setLabel( newLabel )
return self._view:_setLabel( newLabel )
end
-- Function to get the button's label
function button:getLabel()
return self._view:_getLabel()
end
-- Function to set a button as active
function button:setEnabled( isEnabled )
self._view._isEnabled = isEnabled
end
-- Touch listener for our button
function button:touch( event )
-- Set the target to the view's parent group (the button object)
event.target = self
-- Manage touch events on the button
manageButtonTouch( self._view, event )
return true
end
button:addEventListener( "touch", button )
----------------------------------------------------------
-- PRIVATE METHODS
----------------------------------------------------------
-- Function to set the button's label
function view:_setLabel( newLabel )
-- Update the label's text
if "function" == type( self._label.setText ) then
self._label:setText( newLabel )
else
self._label.text = newLabel
end
-- Labels position
if "center" == self._labelAlign then
self._label.x = view.x + self._labelXOffset
elseif "left" == self._labelAlign then
self._label.x = view.x - ( self.contentWidth * 0.5 ) + ( self._label.contentWidth * 0.5 ) + 10 + self._labelXOffset
elseif "right" == self._labelAlign then
self._label.x = self.x + ( self.contentWidth * 0.5 ) - ( self._label.contentWidth * 0.5 ) - 10 + self._labelXOffset
end
-- Update the label's y position
self._label.y = self._label.y
end
-- Function to get the button's label
function view:_getLabel()
return self._label.text
end
-- Function to set the buttons current state
function view:_setState( state )
local newState = state
if "over" == newState then
-- Set the button to the over image state
self:setSequence( "over" )
-- Set the label to it's over color
if "table" == type( self._label ) then
self._label:setFillColor( unpack( self._label._labelColor.over ) )
end
-- The pressedState is now "over"
self._pressedState = "over"
elseif "default" == newState then
-- Set the piece to the default image state
self:setSequence( "default" )
-- Set the label back to it's default color
if "table" == type( self._label ) then
self._label:setFillColor( unpack( self._label._labelColor.default ) )
end
-- The pressedState is now "default"
self._pressedState = "default"
end
end
-- Function to get the buttons current state
function view:_getState()
return self._pressedState
end
-- Lose focus function
function button:_loseFocus()
self._view:_setState( "default" )
-- Create the alpha fade if the theme has it
if self._view._hasAlphaFade then
if self._view._label then
transition.to( self._view._label, { time = 50, alpha = 1.0 } )
else
transition.to( self._view, { time = 50, alpha = 1.0 } )
end
end
end
-- Finalize function
function button:_finalize()
-- Set the ImageSheet to nil
self._imageSheet = nil
if imageSheet then imageSheet = nil; end
if ( opt and opt.sheet ) then opt.sheet = nil; end
end
return button
end
------------------------------------------------------------------------
-- Image Sheet (9 piece/slice) Button
------------------------------------------------------------------------
-- Creates a new button from a 9 piece sprite
local function createUsing9Slice( button, options )
-- Create a local reference to our options table
local opt = options
-- Forward references
local imageSheet, view, viewLabel
local viewTopLeft, viewMiddleLeft, viewBottomLeft
local viewTopMiddle, viewMiddle, viewBottomMiddle
local viewTopRight, viewMiddleRight, viewBottomRight
-- Create the imageSheet
if opt.sheet then
imageSheet = opt.sheet
else
local themeData = require( opt.themeData )
imageSheet = graphics.newImageSheet( opt.themeSheetFile, themeData:getSheet() )
end
-- The view is the button (group)
view = display.newGroup()
button:insert( view )
-- Imagesheet options
local sheetOptions =
{
-- Top Left
viewTopLeft =
{
{
name = "default",
start = opt.topLeftFrame,
count = 1,
},
{
name = "over",
start = opt.topLeftOverFrame,
count = 1,
}
},
-- Middle Left
viewMiddleLeft =
{
{
name = "default",
start = opt.middleLeftFrame,
count = 1,
},
{
name = "over",
start = opt.middleLeftOverFrame,
count = 1,
}
},
-- Bottom Left
viewBottomLeft =
{
{
name = "default",
start = opt.bottomLeftFrame,
count = 1,
},
{
name = "over",
start = opt.bottomLeftOverFrame,
count = 1,
}
},
-- Top Middle
viewTopMiddle =
{
{
name = "default",
start = opt.topMiddleFrame,
count = 1,
},
{
name = "over",
start = opt.topMiddleOverFrame,
count = 1,
}
},
-- Middle
viewMiddle =
{
{
name = "default",
start = opt.middleFrame,
count = 1,
},
{
name = "over",
start = opt.middleOverFrame,
count = 1,
}
},
-- Bottom Middle
viewBottomMiddle =
{
{
name = "default",
start = opt.bottomMiddleFrame,
count = 1,
},
{
name = "over",
start = opt.bottomMiddleOverFrame,
count = 1,
}
},
-- Top Right
viewTopRight =
{
{
name = "default",
start = opt.topRightFrame,
count = 1,
},
{
name = "over",
start = opt.topRightOverFrame,
count = 1,
}
},
-- Middle Right
viewMiddleRight =
{
{
name = "default",
start = opt.middleRightFrame,
count = 1,
},
{
name = "over",
start = opt.middleRightOverFrame,
count = 1,
}
},
-- Bottom Right
viewBottomRight =
{
{
name = "default",
start = opt.bottomRightFrame,
count = 1,
},
{
name = "over",
start = opt.bottomRightOverFrame,
count = 1,
}
},
}
-- Create the left portion of the button
viewTopLeft = display.newSprite( view, imageSheet, sheetOptions.viewTopLeft )
viewMiddleLeft = display.newSprite( view, imageSheet, sheetOptions.viewMiddleLeft )
viewBottomLeft = display.newSprite( view, imageSheet, sheetOptions.viewBottomLeft )
-- Create the right portion of the button
viewTopRight = display.newSprite( view, imageSheet, sheetOptions.viewTopRight )
viewMiddleRight = display.newSprite( view, imageSheet, sheetOptions.viewMiddleRight )
viewBottomRight = display.newSprite( view, imageSheet, sheetOptions.viewBottomRight )
-- Create the middle portion of the button
viewTopMiddle = display.newSprite( view, imageSheet, sheetOptions.viewTopMiddle )
viewMiddle = display.newSprite( view, imageSheet, sheetOptions.viewMiddle )
viewBottomMiddle = display.newSprite( view, imageSheet, sheetOptions.viewBottomMiddle )
-- Create the label (either embossed or standard)
if opt.embossedLabel then
viewLabel = display.newEmbossedText( view, opt.label, 0, 0, opt.font, opt.fontSize )
else
viewLabel = display.newText( view, opt.label, 0, 0, opt.font, opt.fontSize )
end
----------------------------------
-- Positioning
----------------------------------
-- Top
viewTopLeft.x = button.x + ( viewTopLeft.contentWidth * 0.5 )
viewTopLeft.y = button.y + ( viewTopLeft.contentHeight * 0.5 )
viewTopMiddle.width = opt.width - ( viewTopLeft.contentWidth + viewTopRight.contentWidth )
viewTopMiddle.x = viewTopLeft.x + ( viewTopLeft.contentWidth * 0.5 ) + ( viewTopMiddle.contentWidth * 0.5 )
viewTopMiddle.y = viewTopLeft.y
viewTopMiddle._width = viewTopMiddle.width
viewTopRight.x = viewTopMiddle.x + ( viewTopMiddle.contentWidth * 0.5 ) + ( viewTopRight.contentWidth * 0.5 )
viewTopRight.y = viewTopLeft.y
-- Middle
viewMiddleLeft.height = opt.height - ( viewTopLeft.contentHeight + viewTopRight.contentHeight )
viewMiddleLeft.x = viewTopLeft.x
viewMiddleLeft.y = viewTopLeft.y + ( viewMiddleLeft.contentHeight * 0.5 ) + ( viewBottomLeft.contentHeight * 0.5 )
viewMiddleLeft._height = viewMiddleLeft.height
viewMiddle.width = viewTopMiddle.width
viewMiddle.height = opt.height - ( viewTopLeft.contentHeight + ( viewTopRight.contentHeight ) )
viewMiddle.x = viewTopMiddle.x
viewMiddle.y = viewTopMiddle.y + ( viewTopMiddle.contentHeight * 0.5 ) + ( viewMiddle.contentHeight * 0.5 )
viewMiddle._width = viewMiddle.width
viewMiddle._height = viewMiddle.height
viewMiddleRight.height = opt.height - ( viewTopLeft.contentHeight + viewTopRight.contentHeight )
viewMiddleRight.x = viewTopRight.x
viewMiddleRight.y = viewTopRight.y + ( viewMiddleRight.contentHeight * 0.5 ) + ( viewBottomRight.contentHeight * 0.5 )
viewMiddleRight._height = viewMiddleRight.height
-- Bottom
viewBottomLeft.x = viewTopLeft.x
viewBottomLeft.y = viewMiddle.y + ( viewMiddle.contentHeight * 0.5 ) + ( viewBottomLeft.contentHeight * 0.5 )
viewBottomMiddle.width = viewTopMiddle.width
viewBottomMiddle.x = viewTopMiddle.x
viewBottomMiddle.y = viewMiddle.y + ( viewMiddle.contentHeight * 0.5 ) + ( viewBottomMiddle.contentHeight * 0.5 )
viewBottomMiddle._width = viewBottomMiddle.width
viewBottomRight.x = viewTopRight.x
viewBottomRight.y = viewMiddle.y + ( viewMiddle.contentHeight * 0.5 ) + ( viewBottomRight.contentHeight * 0.5 )
-- If the passed width is less than the topLeft & top right width then don't use the middle pieces
if opt.width <= ( viewTopLeft.contentWidth + viewTopRight.contentWidth ) then
-- Hide the middle slices
viewTopMiddle.isVisible = false
viewMiddle.isVisible = false
viewBottomMiddle.isVisible = false