Debian Codegraph @

CorsixTH/Lua/dialogs/fullscreen/bank_manager.lua

raw
1 --[[ Copyright (c) 2009 Edvin "Lego3" Linge
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Bank manager (for loans / insurance companies) and bank statement fullscreen windows.
22 class "UIBankManager" (UIFullscreen)
23
24 ---@type UIBankManager
25 local UIBankManager = _G["UIBankManager"]
26
27 function UIBankManager:UIBankManager(ui)
28 self:UIFullscreen(ui)
29 local gfx = ui.app.gfx
30 self.world = ui.app.world
31 if not pcall(function()
32 self.background = gfx:loadRaw("Bank01V", 640, 480, "QData", "QData", "Bank01V.pal", true)
33 self.stat_background = gfx:loadRaw("Stat01V", 640, 480, "QData", "QData", "Stat01V.pal", true)
34 local palette = gfx:getPalette("Bank01V.pal")
35 self.panel_sprites = gfx:loadSpriteTable("QData", "Bank02V", true, palette)
36 self.font = gfx:loadFontAndSpriteTable("QData", "Font36V", false, palette, { apply_ui_scale = true })
37
38 -- The statistics font
39 palette = gfx:getPalette("Stat01V.pal")
40 self.stat_font = gfx:loadFontAndSpriteTable("QData", "Font37V", false, palette, { apply_ui_scale = true })
41 end) then
42 ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics}))
43 self:close()
44 return
45 end
46 -- counters for the animations of the bank manager
47 self.counter = 0
48 self.browsclk = 0
49 self.smilesclk = 0
50 self.eyesclk = 0
51
52 -- sprites for the animation
53 self.smiles = self:addPanel(12, 303, 199)
54 self.eyesblink = self:addPanel(7, 298, 173)
55 self.browslift = self:addPanel(9, 296, 165)
56
57 -- Button so that the user can click in the middle and get the statistics page and
58 -- vice versa
59 self.stat_button = self:addPanel(0, 230, 100)
60 :makeButton(0, 0, 160, 300, 0, self.showStatistics)
61 self.return_from_stat_button = self:addPanel(0, 0, 0)
62 :makeButton(0, 0, 640, 440, 0, self.hideStatistics)
63 self.return_from_stat_button.enabled = false
64
65 -- Buttons
66 -- The close button needs to be movable
67 self.close_panel = self:addPanel(0, 607, 448)
68 self.close_button = self.close_panel:makeButton(0, 0, 26, 26, 4, self.close):setTooltip(_S.tooltip.bank_manager.close)
69
70 self:addPanel(0, 250, 390):makeButton(0, 0, 200, 50, 0, self.openTownMap):setTooltip(_S.tooltip.toolbar.town_map)
71 self:addPanel(0, 192, 265):makeButton(0, 0, 21, 21, 6, self.increaseLoan):setTooltip(_S.tooltip.bank_manager.borrow_5000)
72 self:addPanel(0, 50, 265):makeButton(0, 0, 21, 21, 5, self.decreaseLoan):setTooltip(_S.tooltip.bank_manager.repay_5000)
73
74 self.graph_buttons = {
75 self:addPanel(0, 547, 157):makeButton(0, 0, 42, 23, 3, self.showGraph1):setTooltip(_S.tooltip.bank_manager.show_graph:format(self.ui.hospital.insurance[1])),
76 self:addPanel(0, 547, 217):makeButton(0, 0, 42, 23, 3, self.showGraph2):setTooltip(_S.tooltip.bank_manager.show_graph:format(self.ui.hospital.insurance[2])),
77 self:addPanel(0, 547, 277):makeButton(0, 0, 42, 23, 3, self.showGraph3):setTooltip(_S.tooltip.bank_manager.show_graph:format(self.ui.hospital.insurance[3]))
78 }
79
80 self.graph = self:addPanel(1, 417, 150)
81
82 self.graph.visible = false
83 self.graph.enabled = false
84 self.return_from_graph_button = self:addPanel(0, 547, 277)
85 self.return_from_graph_button:makeButton(0, 0, 42, 23, 2, self.returnFromGraph):setTooltip(_S.tooltip.bank_manager.graph_return)
86 self.return_from_graph_button.visible = false
87 self.return_from_graph_button.enabled = false
88
89 self:makeTooltip(_S.tooltip.bank_manager.hospital_value, 60, 105, 203, 157)
90 self:makeTooltip(_S.tooltip.bank_manager.balance, 60, 170, 203, 222)
91 self:makeTooltip(_S.tooltip.bank_manager.current_loan, 60, 235, 203, 287)
92 self:makeTooltip(_S.tooltip.bank_manager.interest_payment, 60, 300, 203, 352)
93
94 local --[[persistable:insurance_tooltip_template]] function insurance_tooltip(i)
95 return --[[persistable:insurance_tooltip]] function()
96 if not self.graph.visible then
97 return _S.tooltip.bank_manager.insurance_owed:format(self.ui.hospital.insurance[i])
98 end
99 end
100 end
101
102 self:makeDynamicTooltip(insurance_tooltip(1), 430, 128, 589, 180)
103 self:makeDynamicTooltip(insurance_tooltip(2), 430, 188, 589, 240)
104 self:makeDynamicTooltip(insurance_tooltip(3), 430, 248, 589, 300)
105
106 self:makeTooltip(_S.tooltip.bank_manager.inflation_rate, 430, 308, 589, 331)
107 self:makeTooltip(_S.tooltip.bank_manager.interest_rate, 430, 337, 589, 360)
108 -- TODO: Add the graphs
109 end
110
111 function UIBankManager:afterLoad(old, new)
112 if old < 36 then
113 -- adds the new variables for bank manager animation
114 self.browsclk = 0
115 self.smilesclk = 0
116 self.eyesclk = 0
117 self.counter = 0
118 self.smiles = self:addPanel(12, 303, 199)
119 self.eyesblink = self:addPanel(7, 298, 173)
120 self.browslift = self:addPanel(9, 296, 165)
121 end
122 if old < 236 then
123 local gfx = TheApp.gfx
124 self.background = gfx:loadRaw("Bank01V", 640, 480, "QData", "QData", "Bank01V.pal", true)
125 self.stat_background = gfx:loadRaw("Stat01V", 640, 480, "QData", "QData", "Stat01V.pal", true)
126 local palette = gfx:getPalette("Bank01V.pal")
127 self.panel_sprites = gfx:loadSpriteTable("QData", "Bank02V", true, palette)
128 self.font = gfx:loadFontAndSpriteTable("QData", "Font36V", false, palette, { apply_ui_scale = true })
129
130 palette = gfx:getPalette("Stat01V.pal")
131 self.stat_font = gfx:loadFontAndSpriteTable("QData", "Font37V", false, palette, { apply_ui_scale = true })
132 end
133 UIFullscreen.afterLoad(self, old, new)
134 end
135
136 --! Sum the values in the provided array.
137 --!param t Array to sum.
138 --!return (int) The sum of all values from the t array.
139 local function sum(t)
140 local total = 0
141 for _, entry in ipairs(t) do
142 total = total + entry
143 end
144 return total
145 end
146
147 --! Animation function.
148 function UIBankManager:onTick()
149 self.counter = self.counter + 1
150 -- animate the eyes to blink
151 local function animateEyes()
152 self.eyesclk = self.eyesclk + 1
153 if self.eyesclk > 2 then
154 self.eyesclk = 0
155 self.eyesblink.sprite_index = self.eyesblink.sprite_index + 1
156 if self.eyesblink.sprite_index > 8 then
157 self.eyesblink.sprite_index = 7
158 end
159 end
160 end
161 -- animate the eyebrows to raise and lower
162 local function animateBrows()
163 self.browsclk = self.browsclk + 1
164 if self.browsclk > 3 then
165 self.browsclk = 0
166 self.browslift.sprite_index = self.browslift.sprite_index + 1
167 if self.browslift.sprite_index > 11 then
168 self.browslift.sprite_index = 9
169 end
170 end
171 end
172 -- animate the smile to frown and back again
173 local function animateSmile()
174 self.smilesclk = self.smilesclk + 1
175 if self.smilesclk > 3 then
176 self.smilesclk = 0
177 self.smiles.sprite_index = self.smiles.sprite_index + 1
178 if self.smiles.sprite_index > 15 then
179 self.smiles.sprite_index = 12
180 end
181 end
182 end
183 -- counters to determine when to start and stop the animations
184 -- two blinks
185 if self.counter >= 24 and self.counter < 36 then
186 animateEyes()
187 -- one blink
188 elseif self.counter >= 49 and self.counter < 55 then
189 animateEyes()
190 -- one blink
191 elseif self.counter >= 70 and self.counter < 76 then
192 animateEyes()
193 -- up and down once
194 elseif self.counter >= 88 and self.counter < 100 then
195 animateBrows()
196 -- smile
197 elseif self.counter >= 132 and self.counter < 140 then
198 animateSmile()
199 -- two blinks
200 elseif self.counter >= 164 and self.counter < 176 then
201 animateEyes()
202 -- one blink
203 elseif self.counter >= 189 and self.counter < 195 then
204 animateEyes()
205 -- one blink
206 elseif self.counter >= 219 and self.counter < 225 then
207 animateEyes()
208 -- brows up and down once
209 elseif self.counter >= 248 and self.counter < 260 then
210 animateBrows()
211 --smiles
212 elseif self.counter >= 272 and self.counter < 280 then
213 animateSmile()
214 -- brows up and down twice
215 elseif self.counter >= 298 and self.counter < 322 then
216 animateBrows()
217 -- two blinks
218 elseif self.counter >= 340 and self.counter < 352 then
219 animateEyes()
220 end
221 -- reset the animation counter
222 if self.counter > 420 then
223 self.counter = 0
224 end
225 end
226
227 function UIBankManager:draw(canvas, x, y)
228 local hospital = self.ui.hospital
229 local s = TheApp.config.ui_scale
230
231 -- Either draw the statistics page or the normal bank page
232 if self.showingStatistics then
233 local font = self.stat_font
234 canvas:scale(s, "bitmap")
235 self.stat_background:draw(canvas, self.x * s + x, self.y * s + y)
236 canvas:scale(1, "bitmap")
237 UIFullscreen.draw(self, canvas, x, y)
238 x, y = self.x * s + x, self.y * s + y
239
240 -- Titles
241 font:draw(canvas, _S.bank_manager.statistics_page.date, x + 44 * s, y + 37 * s, 65 * s, 0)
242 font:draw(canvas, _S.bank_manager.statistics_page.details, x + 125 * s, y + 40 * s, 230 * s, 0)
243 font:draw(canvas, _S.bank_manager.statistics_page.money_out, x + 373 * s, y + 42 * s, 70 * s, 0)
244 font:draw(canvas, _S.bank_manager.statistics_page.money_in, x + 449 * s, y + 41 * s, 70 * s, 0)
245 font:draw(canvas, _S.bank_manager.statistics_page.balance, x + 525 * s, y + 40 * s, 70 * s, 0)
246
247 --[[
248 Lua < 5.3 stored integer money amount in floating point values.
249 Lua 5.3 introduced an integer representation, and started printing
250 integer floating point numbers with a trailing .0 .
251
252 As a result, CorsixTH games converted from earlier Lua versions print a
253 trailing .0 in the bank manager window. The "math.floor" around all
254 numbers printed below avoids that problem by converting to integer
255 representation for Lua 5.3, and doing nothing in Lua < 5.3.
256 Note: hospital.balance is a special case and needs string conversion to
257 persist as an integer
258 --]]
259
260 -- Each transaction
261 -- A for loop going backwards
262 for no = 1, #hospital.transactions do
263 local values = hospital.transactions[#hospital.transactions - no + 1]
264 local current_y = no * 15 * s + y + 60 * s
265 font:draw(canvas, _S.date_format.daymonth:format(values.day, values.month), x + 48 * s, current_y)
266 font:draw(canvas, values.desc, x + 129 * s, current_y)
267 if values.spend then
268 font:draw(canvas, "$ " .. math.floor(values.spend), x + 377 * s, current_y)
269 else
270 font:draw(canvas, "$ " .. math.floor(values.receive), x + 453 * s, current_y)
271 end
272 font:draw(canvas, "$ " .. math.floor(values.balance), x + 529 * s, current_y)
273 end
274
275 -- Summary
276 font:draw(canvas, _S.bank_manager.statistics_page.current_balance, x + 373 * s, y + 420 * s, 140 * s, 0)
277 font:draw(canvas, string.format("$%.0f", hospital.balance), x + 526 * s, y + 421 * s, 70 * s, 0)
278 else
279 local font = self.font
280 canvas:scale(s, "bitmap")
281 self.background:draw(canvas, self.x * s + x, self.y * s + y)
282 canvas:scale(1, "bitmap")
283 UIFullscreen.draw(self, canvas, x, y)
284 x, y = self.x * s + x, self.y * s + y
285
286 -- The left side
287 font:draw(canvas, _S.bank_manager.hospital_value, x + 60 * s, y + 109 * s, 143 * s, 0)
288 font:draw(canvas, "$ " .. math.floor(hospital.value), x + 60 * s, y + 139 * s, 143 * s, 0)
289 font:draw(canvas, _S.bank_manager.balance, x + 60 * s, y + 174 * s, 143 * s, 0)
290 font:draw(canvas, "$ " .. math.floor(hospital.balance), x + 60 * s, y + 204 * s, 143 * s, 0)
291 font:draw(canvas, _S.bank_manager.current_loan, x + 60 * s, y + 239 * s, 143 * s, 0)
292 font:draw(canvas, "$ " .. math.floor(hospital.loan), x + 60 * s, y + 269 * s, 143 * s, 0)
293 font:draw(canvas, _S.bank_manager.interest_payment, x + 60 * s, y + 305 * s, 143 * s, 0)
294 local interest = math.floor(hospital.loan * hospital.interest_rate / 12)
295 font:draw(canvas, "$ " .. interest, x + 60 * s, y + 334 * s, 143 * s, 0)
296
297 -- The right side
298 font:draw(canvas, _S.bank_manager.insurance_owed, x + 430 * s, y + 102 * s, 158 * s, 0)
299 if self.graph.visible then
300 font:draw(canvas, hospital.insurance[self.chosen_insurance], x + 430 * s, y + 132 * s, 158 * s, 0)
301 else
302 font:draw(canvas, hospital.insurance[1], x + 430 * s, y + 132 * s, 158 * s, 0)
303 font:draw(canvas, "$ ".. math.floor(sum(hospital.insurance_balance[1])), x + 430 * s, y + 162 * s, 100 * s, 0)
304 font:draw(canvas, hospital.insurance[2], x + 430 * s, y + 192 * s, 158 * s, 0)
305 font:draw(canvas, "$ ".. math.floor(sum(hospital.insurance_balance[2])), x + 430 * s, y + 222 * s, 100 * s, 0)
306 font:draw(canvas, hospital.insurance[3], x + 430 * s, y + 252 * s, 158 * s, 0)
307 font:draw(canvas, "$ ".. math.floor(sum(hospital.insurance_balance[3])), x + 430 * s, y + 282 * s, 100 * s, 0)
308 end
309 font:draw(canvas, _S.bank_manager.inflation_rate, x + 430 * s, y + 312 * s, 100 * s, 0)
310 font:draw(canvas, hospital.inflation_rate * 100 .. " %", x + 550 * s, y + 313 * s, 38 * s, 0)
311 font:draw(canvas, _S.bank_manager.interest_rate, x + 430 * s, y + 342 * s, 100 * s, 0)
312 font:draw(canvas, hospital.interest_rate * 100 .. " %", x + 550 * s, y + 342 * s, 38 * s, 0)
313 end
314 end
315
316 function UIBankManager:onMouseMove(x, y, dx, dy)
317 local ui = self.ui
318 local s = TheApp.config.ui_scale
319
320 if x > 0 and x < 640 * s and y > 0 and y < 480 * s then
321 if self.showingStatistics then
322 ui:setCursor(ui.app.gfx:loadMainCursor("banksummary")) -- Set pie chart cursor
323 else
324 ui:setCursor(ui.app.gfx:loadMainCursor("bank")) -- Set dollar cursor
325 end
326 else
327 ui:setCursor(ui.default_cursor) -- Return to default cursor
328 end
329 end
330
331 function UIBankManager:close()
332 local ui = self.ui
333 ui:setCursor(ui.default_cursor) -- Return to default cursor
334 UIFullscreen.close(self)
335 self.ui:getWindow(UIBottomPanel):updateButtonStates()
336 end
337
338 function UIBankManager:showStatistics(keep_cursor)
339 if self.closed then
340 return
341 end
342 self.ui:playSound("selectx.wav")
343 -- close any open graphs
344 if self.graph.visible then
345 self:returnFromGraph()
346 end
347 self.showingStatistics = true
348 self.return_from_stat_button.enabled = true
349 self.stat_button.enabled = false
350 -- hides the animated parts of the bank manager when viewing the statement
351 self.smiles.visible = false
352 self.eyesblink.visible = false
353 self.browslift.visible = false
354 -- The close button has been slightly moved.
355 local panel = self.close_panel
356 panel.x = panel.x - 6
357 panel.y = panel.y - 6
358 local btn = self.close_button
359 btn.x = btn.x - 6
360 btn.y = btn.y - 6
361 -- Change tooltip to say that the statement screen is closed.
362 btn:setTooltip(_S.tooltip.statement.close)
363 -- Set pie chart cursor, unless coming here from right click on the dollar sign.
364 if not keep_cursor then
365 self.ui:setCursor(self.ui.app.gfx:loadMainCursor("banksummary"))
366 end
367 end
368
369 function UIBankManager:hideStatistics()
370 self.ui:playSound("selectx.wav")
371 self.showingStatistics = false
372 self.return_from_stat_button.enabled = false
373 self.stat_button.enabled = true
374 -- shows the animated parts of the bank manager when viewing the main screen
375 self.smiles.visible = true
376 self.eyesblink.visible = true
377 self.browslift.visible = true
378 -- resets the animation counter if the screen is switched to the statement and back
379 self.counter = -1
380 -- return the close button again
381 local panel = self.close_panel
382 panel.x = panel.x + 6
383 panel.y = panel.y + 6
384 local btn = self.close_button
385 btn.x = btn.x + 6
386 btn.y = btn.y + 6
387 -- Change the tooltip back
388 btn:setTooltip(_S.tooltip.bank_manager.close)
389 -- Set dollar cursor
390 self.ui:setCursor(self.ui.app.gfx:loadMainCursor("bank"))
391 end
392
393 function UIBankManager:showGraph()
394 self.ui:playSound("selectx.wav")
395 self.graph:setTooltip(_S.tooltip.bank_manager.graph:format(self.ui.hospital.insurance[self.chosen_insurance]))
396 self.graph.visible = true
397 self.return_from_graph_button.visible = true
398 self.return_from_graph_button.enabled = true
399
400 for i = 1, 3 do
401 self.graph_buttons[i].visible = false
402 self.graph_buttons[i].enabled = false
403 end
404 end
405
406 function UIBankManager:showGraph1()
407 self.chosen_insurance = 1
408 self:showGraph()
409 end
410
411 function UIBankManager:showGraph2()
412 self.chosen_insurance = 2
413 self:showGraph()
414 end
415
416 function UIBankManager:showGraph3()
417 self.chosen_insurance = 3
418 self:showGraph()
419 end
420
421 function UIBankManager:returnFromGraph()
422 self.ui:playSound("selectx.wav")
423 self.graph.visible = false
424 self.return_from_graph_button.visible = false
425 self.return_from_graph_button.enabled = false
426 for i = 1, 3 do
427 self.graph_buttons[i].enabled = true
428 self.graph_buttons[i].visible = true
429 end
430 end
431
432 function UIBankManager:increaseLoan()
433 local hospital = self.ui.hospital
434 local max_loan = not self.world.free_build_mode and (math.floor((hospital.value * 0.33) / 5000) * 5000) + 10000 or 0
435 if hospital.loan + 5000 <= max_loan then
436 local amount = self.ui.app.key_modifiers.ctrl and max_loan - hospital.loan or 5000
437 hospital.loan = hospital.loan + amount
438 hospital:receiveMoney(amount, _S.transactions.bank_loan)
439 self.ui:playSound("selectx.wav")
440 else
441 self.ui:playSound("Wrong2.wav")
442 end
443 end
444
445 function UIBankManager:decreaseLoan()
446 local hospital = self.ui.hospital
447 local amount = 5000
448 if self.ui.app.key_modifiers.ctrl then
449 -- Repay as much as possible in increments of 5000
450 if hospital.balance > 5000 then
451 amount = math.min(hospital.loan, math.floor(hospital.balance / 5000) * 5000)
452 end
453 end
454 if hospital.loan > 0 and hospital.balance >= amount then
455 hospital.loan = hospital.loan - amount
456 hospital:spendMoney(amount, _S.transactions.loan_repayment)
457 self.ui:playSound("selectx.wav")
458 else
459 self.ui:playSound("Wrong2.wav")
460 end
461 end
462
463 function UIBankManager:openTownMap()
464 local dlg = UITownMap(self.ui)
465 self.ui:addWindow(dlg)
466 end