-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugMenu.lua
245 lines (216 loc) · 5.66 KB
/
debugMenu.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
-----------------------------------------------------------------------------------------
--
-- debugMenu.lua
--
-- The debug menu view for the Mars App.
-----------------------------------------------------------------------------------------
-- Get local reference to the game globals
local game = globalGame
-- Corona modules needed
local widget = require( "widget" )
-- Create the act object
local act = game.newAct()
-- List of activities that can be run directly from the debug menu
local debugActs = {
"scottAct",
"marsLanding",
"startScreen",
"mainAct",
"thrustNav",
"doorLock",
"hack",
"circuit",
"wireCut",
"greenhouse",
"drillScan",
"drill",
"anotherRover",
"rover",
"rover2",
"shipLanding",
"stasis",
"spaceWalk",
"blankAct",
"sampleAct",
"layoutTool",
"about",
}
-- File local variables
local ss = game.saveState
local res = ss.resources
local waterEdit
local foodEdit
local energyEdit
local stateEdit
local xLabel = act.xCenter
local xEdit = act.xMax - 60
local dyLine = act.dyTitleBar
local yEditFirst = act.yMin + dyLine * 1.5
-- Hide the keyboard
local function hideKeyboard()
native.setKeyboardFocus( nil )
end
-- Draw a row in the tableView
local function onRowRender( event )
-- Get row info
local row = event.row
local dxRow = row.contentWidth
local dyRow = row.contentHeight
-- Display the text
local rowTitle = display.newText( row, debugActs[row.index], 0, 0, native.systemFontBold, 18 )
rowTitle:setFillColor( 0 ) -- black text
rowTitle.anchorX = 0 -- left aligned
rowTitle.x = 15
rowTitle.y = dyRow * 0.5 -- vertically centered
end
-- Handle touch on a row
function onRowTouch( event )
hideKeyboard()
if event.phase == "tap" or event.phase == "release" then
-- Run the selected activity module on the main tab
game.gotoTab( "mainAct", false )
game.gotoAct( debugActs[event.target.index] )
end
end
-- Handle press of the back button
local function onBackButton()
game.gotoScene( "menu", { effect = "slideRight", time = 200 } )
end
-- Create a UI label in the act
local function newLabel( text, x, y )
local label = display.newText( act.group, text, x, y, native.systemFont, 18 )
label.anchorX = 0
label:setFillColor( 0 )
return label
end
-- Create a new textEdit in the act
local function newNumberEdit( x, y, value, listener )
local edit = native.newTextField( x, y, 70, 30 )
edit.inputType = "number"
edit.text = tostring( value )
edit:addEventListener( "userInput", listener )
act.group:insert( edit )
return edit
end
-- Create a new on/off switch in the act
local function newSwitch( x, y, listener )
local switch = widget.newSwitch{ x = x, y = y, onRelease = listener }
act.group:insert( switch )
return switch
end
-- Init the act
function act:init()
-- Background and title bar for the view
local bg = act:grayBackground()
bg:addEventListener( "touch", hideKeyboard )
local tb = act:makeTitleBar( "Debug Menu", onBackButton )
tb:addEventListener( "touch", hideKeyboard )
-- Position for controls and labels
local y = act.yMin + act.dyTitleBar * 1.5
-- Resource edit labels
y = yEditFirst
newLabel( "Water", xLabel, y )
y = y + dyLine
newLabel( "Food", xLabel, y )
y = y + dyLine
newLabel( "Energy", xLabel, y )
-- Ship state edit label
y = y + dyLine * 1.5
newLabel( "State", xLabel, y )
-- Land button
y = y + dyLine
local btn = widget.newButton{
x = act.xMin + act.width * 0.6,
y = y,
width = 50,
height = 30,
shape = "rect",
fillColor = { default = { 1, 1, 1 }, over = { 1, 0, 0 } },
label = "Land",
--labelAlign = "left",
onRelease =
function ()
game.landShip()
end
}
act.group:insert( btn )
-- Cheat mode switch and label
y = y + dyLine * 1.5
newLabel( "Cheat", xLabel , y )
newSwitch( act.xMax - 45, y,
function ( event )
game.cheatMode = event.target.isOn
end )
-- All Gems mode switch and label
y = y + dyLine
newLabel( "All Gems", xLabel , y )
newSwitch( act.xMax - 45, y,
function ( event )
game.allGems = event.target.isOn
end )
-- Create the tableView widget to list the debug activities
local tableView = widget.newTableView
{
left = act.xMin,
top = act.yMin + act.dyTitleBar,
height = act.height - act.dyTitleBar,
width = act.width * 0.45,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
act.group:insert( tableView )
-- Insert the rows
for i = 1, #debugActs do
tableView:insertRow{}
end
end
-- Prepare the act
function act:prepare()
-- Create the resource text edits
local y = yEditFirst
waterEdit = newNumberEdit( xEdit, y, res.h2o,
function ( event )
res.h2o = tonumber( event.target.text ) or 0
end )
y = y + dyLine
foodEdit = newNumberEdit( xEdit, y, res.food,
function ( event )
res.food = tonumber( event.target.text ) or 0
end )
y = y + dyLine
energyEdit = newNumberEdit( xEdit, y, res.kWh,
function ( event )
res.kWh = tonumber( event.target.text ) or 0
end )
y = y + dyLine * 1.5
stateEdit = newNumberEdit( xEdit, y, ss.shipState,
function ( event )
if event.phase == "ended" or event.phase == "submitted" then
game.setShipState( tonumber( event.target.text ) or ss.shipState )
end
end )
end
-- Stop the act
function act:stop()
-- Destroy the resource text edits
hideKeyboard()
if waterEdit then
waterEdit:removeSelf()
waterEdit = nil
end
if foodEdit then
foodEdit:removeSelf()
foodEdit = nil
end
if energyEdit then
energyEdit:removeSelf()
energyEdit = nil
end
if stateEdit then
stateEdit:removeSelf()
stateEdit = nil
end
end
------------------------- End of Activity --------------------------------
-- Corona needs the scene object returned from the act file
return act.scene