-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppleScreen.lua
More file actions
241 lines (201 loc) · 7.17 KB
/
AppleScreen.lua
File metadata and controls
241 lines (201 loc) · 7.17 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
-- AppleScreen.lua
-- takes a schema and shows the screen, handles everything really
AppleScreen = class(Panel)
function AppleScreen:init(schema)
Panel.init(self,0,0)
self.topY = 0 -- larger topY means the user scrolled down
self.movableObjs = {}
self.taggedElems = {}
self.schema = schema
self:buildFromSchema()
end
function AppleScreen:rebuild()
self:removeAll()
self.movableObjs = {}
self.taggedElems = {}
local oldTopY = self.topY
self:buildFromSchema()
self:setTopY(oldTopY)
end
function AppleScreen:touched(t)
if t.state == BEGAN then
self.dontFwd = false
Panel.touched(self,t)
elseif t.state == MOVING then
local newY = self.topY + t.deltaY
self:setTopY(newY)
self.dontFwd = true
Panel.touched(self,t)
elseif t.state == ENDED then
if not self.dontFwd then
Panel.touched(self,t)
end
end
end
function AppleScreen:setTopY(newY)
if newY < 0 then newY = 0 end
if self.maxH < 0 then
if newY > -self.maxH+20 then newY = -self.maxH+20 end
else
newY = 0
end
local deltaY = newY - self.topY
self.topY = newY
if deltaY ~= 0 then
for _,obj in ipairs(self.movableObjs) do
obj:translate(0,deltaY)
if obj.setUnpressed and obj.pressed then obj:setUnpressed() end
end
end
end
function AppleScreen:buildFromSchema()
self.topY = 0
-- the background
local back = TextBanner("",0,0,WIDTH,HEIGHT,
{type="round",topColor=color(218,221,226),bottomColor=color(218,221,226)})
self:add(back)
-- create the title
local title = TextBanner(self.schema.title,0,HEIGHT-50,WIDTH,50,
{type="topRound",topColor=color(244,245,247),bottomColor=color(141,145,157),
text={fill=color(73,90,98)}})
self:add(title)
-- create the back button
if self.schema.backButton then
self.backBut = TextButton(self.schema.backButton.text,7,HEIGHT-40,74,30,
{type="back",topColor=color(140,140,150),bottomColor=color(78,85,96),
pressedTopColor=color(38,38,38),pressedBottomColor=color(38,38,38),
text={fill=color(224,224,225),fontSize=13,font="ArialRoundedMTBold"}})
self.backBut.onEnded = function(b,t)
TextButton.onEnded(b,t)
self.schema.backButton.callback()
end
self:add(self.backBut)
end
local currentH = title.y - 40
for idx,elem in ipairs(self.schema.elems) do
currentH = self:addElem(elem,currentH,p)
end
self.maxH = currentH
self:remove(title)
self:add(title)
if self.backBut then
self:remove(self.backBut)
self:add(self.backBut)
end
end
function AppleScreen:addElem(elem,currentH)
local type = elem.type
if type == "block" then
return self:addBlock(elem.elems,currentH)
elseif type == "text" then
return self:addText(elem,currentH)
elseif type == "blank" then
return self:addBlank(elem.amount,currentH)
elseif type == "MultiTextInput" then
return self:addMultiTextInput(elem,currentH)
elseif type=="selector" then
return self:addSelector(elem.elems,currentH)
end
assert(false,"unknown type: "..type)
end
function AppleScreen:addSelector(elems,currentH)
local numElems = #elems
if numElems == 0 then return currentH end
local elemH = 50
for idx,elem in ipairs(elems) do
local type = "square"
if idx == 1 then type = "topRound"
elseif idx == #elems then type = "bottomRound" end
if #elems == 1 then type = "round" end
currentH = currentH - elemH
local but = AppleSelector(elem.text,50,currentH,WIDTH-100,
{text={fontSize=21},type=type})
if elem.rightText then but:setRightText(elem.rightText) end
self:add(but)
table.insert(self.movableObjs,but)
if elem.tag then self.taggedElems[elem.tag] = but end
end
return currentH
end
function AppleScreen:addMultiTextInput(elem,currentH)
local multi = AppleMultiText(50,currentH,WIDTH-100,elem.lines)
multi:translate(0,-multi.textbox.h)
self:add(multi)
table.insert(self.movableObjs,multi)
if elem.tag then
self.taggedElems[elem.tag]=multi
end
return currentH - multi.textbox.h
end
-- text that you can't interact with
function AppleScreen:addText(elem,currentH)
local textElem = TextElem(elem.text,55,currentH,
{fill=color(59, 59, 61, 255)})
local h = select(2,textElem:textSize())
textElem:translate(0,-h)
--textElem:showHourGlass(true)
self:add(textElem)
table.insert(self.movableObjs,textElem)
if elem.tag then
self.taggedElems[elem.tag]=textElem
end
return currentH - h
end
-- just blank space
function AppleScreen:addBlank(amount,currentH)
return currentH - amount
end
-- a block of continuous elements
function AppleScreen:addBlock(elems,currentH)
local numElems = #elems
if numElems == 0 then return currentH end
local elemH = 50
for idx,elem in ipairs(elems) do
local type = "square"
if idx == 1 then type = "topRound"
elseif idx == #elems then type = "bottomRound" end
if #elems == 1 then type = "round" end
currentH = currentH - elemH
-- those things with a little arrow at the end
if elem.type == "SimpleArrow" then
local but = AppleSimpleArrow(elem.text,50,currentH,WIDTH-100,
{text={fontSize=21},type=type})
if elem.callback then
but.onEnded = function(b,t)
AppleSimpleArrow.onEnded(b,t)
elem.callback()
end
end
if elem.rightText then but:setRightText(elem.rightText) end
self:add(but)
table.insert(self.movableObjs,but)
if elem.tag then self.taggedElems[elem.tag] = but end
elseif elem.type == "TextInput" then
-- here's where you can enter some input
local box = AppleTextbox(elem.label,50,currentH,WIDTH-100,
{text={fontSize=21},type=type})
if elem.protected then
box.textbox.protected = true
end
if elem.startText then
box.textbox.text = elem.startText
end
if elem.keycallback then
box.textbox.keycallback = elem.keycallback
end
if elem.shadowText then
box.shadowText = elem.shadowText
if box.textbox.text == "" then
box.textbox.text = elem.shadowText
box.textbox.textIsShadow = true
box.textbox.fontProperties.font = "Arial-BoldItalicMT"
box.textbox.fontProperties.fill=color(129, 140, 140, 255)
end
end
self:add(box)
table.insert(self.movableObjs,box)
if elem.tag then self.taggedElems[elem.tag] = box end
end
end
return currentH
end