-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorMenu.lua
205 lines (157 loc) · 5.66 KB
/
ColorMenu.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
-- ColorMenu.lua
local composer = require('composer')
local scene = composer.newScene()
local const = require 'constants'
local globalData = require 'globalData'
local Dim = require 'Dim'
local Tappy = require 'Tappy'
local Ivory = require 'Ivory'
local Util = require 'Util'
local genericMore
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via 'composer.removeScene()'
-- -----------------------------------------------------------------------------------
local function backTouch(event)
local grp = event.target
if event.phase == 'began' then
-- trace('touch began', event.x, event.y)
elseif event.phase == 'moved' then
-- trace('touch moved, start', event.xStart, event.yStart, 'now', event.x, event.y)
grp.x = event.x - event.xStart
grp.y = event.y - event.yStart
elseif event.phase == 'ended' then
-- trace('touch ended', event.x, event.y)
transition.moveTo(grp, {x = 0, y = 0, transition = easing.outQuad })
if genericMore then
genericMore:removeSelf()
genericMore = nil
end
elseif event.phase == 'cancelled' then
-- trace('touch cancelled', event.x, event.yet)
transition.moveTo(grp, {x = 0, y = 0, transition = easing.outQuad })
end
return true
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create(event)
trace('ColorMenu scene:create')
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
-- don't build the scene here
-- because we want it to reload if the palette has changed
end
-- show()
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
trace('ColorMenu scene:show', event.phase)
if phase == 'will' then
-- Code here runs when the scene is still off screen (but is about to come on screen)
Util.setBackground(sceneGroup)
local dim = Dim.new(9, 9) -- pretend we are on a 9x9 grid
globalData.dim = dim -- used by Tile
local function _titleRow(y, s)
local titleGroup = display.newGroup()
titleGroup.x = display.contentCenterX - (string.len(s) * dim.halfQ)
titleGroup.y = y
sceneGroup:insert(titleGroup)
local x = dim.halfQ
for i=1, string.len(s) do
Ivory.new({
parent = titleGroup,
x = x,
y = 0,
text = string.sub(s, i, i),
})
x = x + dim.Q
end
end
local function _tappyRow(y, paletteName, color)
local tappyGroup = display.newGroup()
tappyGroup.x = display.contentCenterX - (string.len(paletteName) * dim.halfQ)
tappyGroup.y = y
sceneGroup:insert(tappyGroup)
-- the first tile is dim.halfQ over to the right
local x = dim.halfQ
for i=1, string.len(paletteName) do
Tappy.new({
parent = tappyGroup,
x = x,
y = 0,
text = string.sub(paletteName, i, i),
color = color,
command = function()
Util.sound('ui')
globalData:setPalette(paletteName)
globalData:saveSettings()
composer.gotoScene('ModeMenu', {effect='slideRight'})
end,
-- no description
})
x = x + dim.Q
end
end
local y = dim.topInset + dim.Q
_titleRow(y, 'CHOOSE')
y = y + dim.Q
_titleRow(y, ' YOUR ')
y = y + dim.Q
_titleRow(y, 'COLORS')
y = y + dim.Q + dim.Q + dim.halfQ
for k,_ in pairs(const.PALETTE) do
_tappyRow(y, k, const.PALETTE[k].tappy)
y = y + dim.Q + dim.Q
end
if y > display.contentHeight then genericMore = Util.genericMore(sceneGroup, 'right') end
sceneGroup:addEventListener('touch', backTouch)
if not Runtime:addEventListener('key', scene) then
trace('ERROR: could not addEventListener key in ColorMenu scene:show')
end
elseif phase == 'did' then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
trace('ColorMenu scene:hide', event.phase)
if phase == 'will' then
-- Code here runs when the scene is on screen (but is about to go off screen)
sceneGroup:removeEventListener('touch', backTouch)
if not Runtime:removeEventListener('key', scene) then
trace('ERROR: could not removeEventListener key in ColorMenu scene:hide')
end
elseif phase == 'did' then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy(event)
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
trace('ColorMenu scene:destroy')
composer.removeScene('ColorMenu')
end
function scene:key(event)
local phase = event.phase
if phase == 'up' then
if event.keyName == 'back' or event.keyName == 'deleteBack' then
composer.gotoScene('ModeMenu', {effect='slideRight'})
return true -- override the key
end
end
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener('create', scene)
scene:addEventListener('show', scene)
scene:addEventListener('hide', scene)
scene:addEventListener('destroy', scene)
-- -----------------------------------------------------------------------------------
return scene