-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.lua
More file actions
300 lines (254 loc) · 9.94 KB
/
settings.lua
File metadata and controls
300 lines (254 loc) · 9.94 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
-----------------------------------------------------------------------------------------
--
-- settings.lua
--
-- Authors: Daniel Burris and Jairo Arreola
-----------------------------------------------------------------------------------------
local composer = require("composer")
local scene = composer.newScene()
local widget = require("widget")
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
-- returnButtonEvent()
-- input: none
-- output: none
--
-- This function just switches from the settings scene to the menu scene
-- It only allows you to return to the menu if you have legal values for
-- minValue and maxValue. It will throw an alert if you try to return
-- with illegal values.
local function returnButtonEvent(event)
if ("ended" == event.phase and minValue<=maxValue) then
composer.gotoScene("menu")
elseif("ended" == event.phase and minValue>maxValue) then
native.showAlert("Error", "Min interval cannot be less then max interval")
end
end
-- radioButtonPress()
-- input: none
-- output: none
--
-- This function is linked to the radio buttons and will reset the interval options
-- to default values if the relevant radio button is checked, if not it will allow
-- the user to change the values of the interval switching
local function radioButtonPress(event)
local switch = event.target
-- This says if the "Default Values"(id = 1) radio button is checked, the interval
-- values will be reset to default and the sliders / text will be adjusted accordingly
if (switch.id == "1") then
minValue = 500
minSlider:setValue(0)
minText.text = "Min Interval : "..minValue
maxValue = 5000
maxSlider:setValue(100)
maxText.text = "Max Interval : "..maxValue
end
end
-- minValueListener()
-- input: none
-- output: none
--
-- This function is linked to the minimum interval slider and will update the
-- minValue variable as well as the text displaying the value of minValue
local function minValueListener(event)
minValue = (5000 * (event.value)/100) + 500 -- lowest value is 500, max is 5500
minText.text = "Min Interval : "..minValue
end
-- maxValueListener()
-- input: none
-- output: none
--
-- This function is linked to the maximum interval slider and will update the
-- maxValue variable as well as the text displaying the value of maxValue
local function maxValueListener(event)
maxValue = (5000 * (event.value)/100) + 500 -- lowest value is 500, max is 5500
maxText.text = "Max Interval : "..maxValue
end
-- settingsUpdater()
-- input: none
-- output: none
--
-- This function is associated with an enterFrame event and makes sure the user cannot
-- alter the default values of the game. It also warns the user of illegal values
-- when minValue is greater than maxValue.
local function settingsUpdater(event)
-- If "Default Values" radio button is checked
if (radioButton1.isOn) then
minValue = 500
minSlider:setValue(0) -- adjusting slider
minText.text = "Min Interval : "..minValue
maxValue = 5000
maxSlider:setValue(90) -- adjusting slider, max value is 5500, not 5000
maxText.text = "Max Interval : "..maxValue
end
-- Actively checking for illegal values
if (minValue > maxValue) then
errText.text = "Warning: Min interval cannot\n exceed Max interval"
else
errText.text = ""
end
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
-- input: none
-- output: none
--
-- This function creates all the objects that will be used in the scene and adds
-- them to the scene group.
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
-- Defining options for radio button sheet
local options = {
width = 100,
height = 100,
numFrames = 2,
sheetContentWidth = 200,
sheetContentHeight = 100
}
-- Text to be displayed if invalid values selected
errText = display.newText("Warning: Min interval cannot\n exceed Max interval", display.contentCenterX, display.contentCenterY+100, native.systemFont, 16)
errText:setTextColor(139,0,0) -- red text
-- Text that updates when maxValue or minValue is altered, lets the user know what values are currently selected
maxText = display.newText("Max Interval: "..maxValue, 100, 150, native.sytemFont, 16)
minText = display.newText("Min Interval: "..minValue, 100, 100, native.sytemFont, 16)
-- Game Background
local menuBG = display.newImage("images/menuBG.jpg")
menuBG.width = display.contentWidth
menuBG.height = display.pixelWidth
menuBG:setFillColor(1,1,1,0.5) -- adding transparency
-- Static Text acting as labels for the user
local text1 = display.newText("Default Values", display.contentCenterX-display.contentWidth*0.25, 0, native.systemFont, 16)
local text2 = display.newText("Custom Values", display.contentCenterX+display.contentWidth*0.25, 0, native.systemFont, 16)
-- Radio buttons are bound to radio buttons sheets so that they are linked together and
-- only one can be selected at a time in a radio button sheet
local radioButtonSheet = graphics.newImageSheet("images/checkboxSheet.png", options)
radioGroup = display.newGroup()
-- Creating a radio button widget, this radio button is the "Default Values" radio button
radioButton1 = widget.newSwitch(
{
x = display.contentCenterX-display.contentWidth*0.25,
y = 40,
style = "radio",
id = "1",
width = 50,
height = 50,
initialSwitchState = true,
onPress = radioButtonPress,
sheet = radioButtonSheet,
frameOff = 1,
frameOn = 2
}
)
-- Creating a radio button widget, this radio button is the "Custom Values" radio button
radioButton2 = widget.newSwitch(
{
x = display.contentCenterX+display.contentWidth*0.25,
y = 40,
style = "radio",
id = "2",
width = 50,
height = 50,
onPress = radioButtonPress,
sheet = radioButtonSheet,
frameOff = 1,
frameOn = 2
} )
-- Adding the radio buttons to the radio button sheet
radioGroup:insert( radioButton2 )
radioGroup:insert( radioButton1 )
-- Creating a slider widget, this slider widget corresponds to the minimum interval variable
minSlider = widget.newSlider(
{
top = 200,
left = 50,
width = 200,
value = 10,
listener = minValueListener
})
-- Creating a slider widget, this slider widget corresponds to the maximum interval variable
maxSlider = widget.newSlider(
{
top = 250,
left = 50,
width = 200,
value = 10,
listener = maxValueListener
})
-- Creating a button widget, this button returns us to the menu
local returnButton = widget.newButton({
id = "returnButton",
label = "Return",
width = 100,
height = 20,
fontSize = 10,
defaultFile = "images/button.png",
onEvent = returnButtonEvent
} )
-- Positioning all objects on the scene
returnButton.x = display.contentCenterX
returnButton.y = display.contentCenterY+(display.contentCenterY/1.5)
menuBG.x = display.contentCenterX
menuBG.y = display.contentCenterY
-- Adding all objects to the scene group
sceneGroup:insert( menuBG )
sceneGroup:insert(returnButton)
sceneGroup:insert(minSlider)
sceneGroup:insert(maxSlider)
sceneGroup:insert(text1)
sceneGroup:insert(text2)
sceneGroup:insert(minText)
sceneGroup:insert(maxText)
sceneGroup:insert(radioGroup)
end
-- show()
-- input: none
-- output: none
--
-- This function calls an enterFrame event listener for settingsUpdater function
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
Runtime:addEventListener("enterFrame", settingsUpdater)
end
end
-- hide()
-- input: none
-- output: none
--
-- This function does nothing for us, but is still part of Corona SDK scene creation requirements
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
-- input: none
-- output: none
--
-- This function does nothing for us, but is still part of Corona SDK scene creation requirements
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene