-
Notifications
You must be signed in to change notification settings - Fork 31
/
slider.lua
145 lines (119 loc) · 4.25 KB
/
slider.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
-- Abstract: widget.newSlider() 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" ) )
--Forward reference for test function timer
local testTimer = nil
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.contentWidth, display.contentHeight )
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
local fontUsed = native.systemFont
-- Button to return to unit test listing
local returnToListing = widget.newButton{
id = "returnToListing",
left = display.contentWidth * 0.5,
top = backButtonPosition,
label = "Exit",
width = 200, height = backButtonSize,
cornerRadius = 8,
onRelease = function() composer.gotoScene( "unitTestListing" ) end;
}
returnToListing.x = display.contentCenterX
group:insert( returnToListing )
----------------------------------------------------------------------------------------------------------------
-- START OF UNIT TEST
----------------------------------------------------------------------------------------------------------------
--Toggle these defines to execute tests. NOTE: It is recommended to only enable one of these tests at a time
local TEST_SET_VALUE = false
--Create some text to show the sliders output
local sliderResult = display.newText( "Slider at 50%", 0, 0, fontUsed, 18 )
sliderResult:setFillColor( fontColor )
if isGraphicsV1 then
sliderResult:setReferencePoint( display.CenterReferencePoint )
end
sliderResult.x = 160
sliderResult.y = 250
group:insert( sliderResult )
-- Slider listener function
local function sliderListener( event )
--print( "phase is:", event.phase )
sliderResult.text = "Slider at " .. event.value .. "%"
end
-- Create a horizontal slider
local sliderHorizontal = widget.newSlider
{
width = 200,
left = 80,
top = 300,
value = 50,
listener = sliderListener,
}
sliderHorizontal.x = display.contentCenterX
group:insert( sliderHorizontal )
-- Create a vertical slider
local sliderVertical = widget.newSlider
{
height = 150,
top = 130,
left = 50,
value = 80,
orientation = "vertical",
listener = sliderListener,
}
group:insert( sliderVertical )
----------------------------------------------------------------------------------------------------------------
-- TESTS
----------------------------------------------------------------------------------------------------------------
--Test setValue()
if TEST_SET_VALUE then
testTimer = timer.performWithDelay( 1000, function()
sliderHorizontal:setValue( 0 )
sliderVertical:setValue( 0 )
sliderResult.text = "Slider at " .. sliderHorizontal.value .. "%"
--print( "Vertical slider at " .. sliderVertical.value .. "%" )
--print( "Horizontal slider at " .. sliderHorizontal.value .. "%" )
end, 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
composer.removeHidden( false )
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "hide", scene )
return scene