Debian Codegraph @

CorsixTH/Lua/dialogs/hire_staff.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
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 class "UIHireStaff" (Window)
22
23 ---@type UIHireStaff
24 local UIHireStaff = _G["UIHireStaff"]
25
26 function UIHireStaff:UIHireStaff(ui)
27 self:Window()
28 self.modal_class = "main"
29 self.esc_closes = true
30 self.world = ui.app.world
31 self.ui = ui
32 self.width = 242
33 self.height = 323
34 self:setDefaultPosition(100, 100)
35 self.panel_sprites = ui.app.gfx:loadSpriteTable("QData", "Req11V", true)
36 self.white_font = ui.app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
37 self.face_parts = ui.app.gfx:loadRaw("Face01V", 65, 1350, nil, "Data", "MPalette.dat", false, { flags = DrawFlags.Nearest })
38
39 -- Left hand side tab backgrounds
40 self:addPanel(253, 0, 0)
41 self:addPanel(254, 0, 83)
42 self:addPanel(254, 0, 162)
43 self:addPanel(255, 0, 241)
44
45 -- Left hand side tabs
46 local --[[persistable:hire_staff_category]] function category(name, state, btn)
47 if #self.world.available_staff[name] == 0 then
48 self.ui:playSound("wrong2.wav")
49 if state then
50 btn:toggle()
51 end
52 else
53 self.ui:playSound("selectx.wav")
54 self:setCategory(state and name or nil)
55 end
56 end
57 self.tabs = {
58 self:addPanel(264, 8, 8):makeToggleButton(0, 0, 40, 69, 265, category, "Doctor"):setTooltip(_S.tooltip.hire_staff_window.doctors),
59 self:addPanel(266, 8, 87):makeToggleButton(0, 0, 40, 69, 267, category, "Nurse"):setTooltip(_S.tooltip.hire_staff_window.nurses),
60 self:addPanel(268, 8, 166):makeToggleButton(0, 0, 40, 69, 269, category, "Handyman"):setTooltip(_S.tooltip.hire_staff_window.handymen),
61 self:addPanel(270, 8, 245):makeToggleButton(0, 0, 40, 69, 271, category, "Receptionist"):setTooltip(_S.tooltip.hire_staff_window.receptionists),
62 }
63
64 -- Right hand side
65 self:addPanel(256, 56, 0) -- Dialog header
66 for y = 49, 113, 11 do
67 self:addPanel(257,56, y) -- Dialog background
68 end
69 self.ability_bg_panel =
70 self:addPanel(260, 55, 114) -- Abilities background
71 self.skill_bg_panel =
72 self:addPanel(259, 68, 95):setTooltip(_S.tooltip.hire_staff_window.staff_ability, 109, 95) -- Skill background
73 self.skill_bg_panel.visible = false
74 self:addPanel(261, 55, 160) -- Wage background
75 self.abilities_blanker =
76 self:addColourPanel(73, 133, 156, 34, 60, 174, 203) -- Hides abilities list
77 for y = 207, 262, 3 do
78 self:addPanel(262, 54, y) -- Description background
79 end
80 self:addColourPanel(155, 45, 72, 81, 211, 255, 255) -- Portrait background
81 self:addPanel(263, 56, 263) -- Dialog midpiece
82 self.complete_blanker =
83 self:addColourPanel(68, 15, 161, 257, 60, 174, 203) -- Hides all staff info
84 self:addPanel(272, 56, 277):makeButton(8, 10, 43, 27, 273, self.movePrevious):setTooltip(_S.tooltip.hire_staff_window.prev_person)
85 self:addPanel(274, 106, 277):makeButton(0, 10, 58, 27, 275, self.hire):setTooltip(_S.tooltip.hire_staff_window.hire)
86 self:addPanel(276, 163, 277):makeButton(0, 10, 28, 27, 277, self.close):setTooltip(_S.tooltip.hire_staff_window.cancel)
87 self:addPanel(278, 190, 277):makeButton(0, 10, 44, 27, 279, self.moveNext):setTooltip(_S.tooltip.hire_staff_window.next_person)
88
89 self:makeTooltip(_S.tooltip.hire_staff_window.salary, 68, 173, 227, 194)
90 self:makeTooltip(_S.tooltip.hire_staff_window.doctor_seniority, 68, 44, 151, 95)
91 self:makeTooltip(_S.tooltip.hire_staff_window.qualifications, 68, 134, 107, 167)
92
93 self:makeTooltip(_S.tooltip.hire_staff_window.surgeon, 120, 136, 137, 167)
94 self:makeTooltip(_S.tooltip.hire_staff_window.psychiatrist, 137, 136, 164, 167)
95 self:makeTooltip(_S.tooltip.hire_staff_window.researcher, 164, 136, 191, 167)
96
97 self:updateTooltips()
98 self:registerKeyHandlers()
99 end
100
101 function UIHireStaff:registerKeyHandlers()
102 self:addKeyHandler("global_confirm", self.hire)
103 self:addKeyHandler("global_confirm_alt", self.hire)
104 end
105
106 function UIHireStaff:updateTooltips()
107 local cond = not not self.category
108 self.tooltip_regions[1].enabled = cond
109
110 cond = cond and self.category == "Doctor"
111 self.tooltip_regions[2].enabled = cond
112 self.tooltip_regions[3].enabled = cond
113 cond = cond and self.current_index and self.world.available_staff[self.category]
114 cond = cond and cond[self.current_index]
115 self.tooltip_regions[4].enabled = cond and cond.is_surgeon >= 1.0
116 self.tooltip_regions[5].enabled = cond and cond.is_psychiatrist >= 1.0
117 self.tooltip_regions[6].enabled = cond and cond.is_researcher >= 1.0
118 end
119
120 function UIHireStaff:onMouseUp(button, x, y)
121 self.mouse_up_x = self.x + x
122 self.mouse_up_y = self.y + y
123 return Window.onMouseUp(self, button, x, y)
124 end
125
126 function UIHireStaff:hire()
127 self.ui:tutorialStep(2, {3, 4, 5}, 6)
128 self.ui:tutorialStep(4, 3, 4)
129 local profile
130 if self.category and self.current_index then
131 profile = self.world.available_staff[self.category]
132 profile = profile and profile[self.current_index]
133 end
134 if not profile then
135 self.ui:playSound("wrong2.wav")
136 return
137 end
138 if self.ui.hospital.balance < profile.wage then
139 self:cannotAfford()
140 self.ui:playSound("wrong2.wav")
141 return
142 end
143 self.ui:playSound("YesX.wav")
144 table.remove(self.world.available_staff[self.category], self.current_index)
145 self.ui:addWindow(UIPlaceStaff(self.ui, profile, self.mouse_up_x, self.mouse_up_y))
146 end
147
148 function UIHireStaff:cannotAfford()
149 local msg = {
150 (_A.warnings.cannot_afford),
151 (_A.warnings.cash_low_consider_loan),
152 }
153 if msg then
154 self.world.ui.adviser:say(msg[math.random(1, #msg)])
155 end
156 end
157
158 function UIHireStaff:draw(canvas, x, y)
159 Window.draw(self, canvas, x, y)
160
161 local s = TheApp.config.ui_scale
162 x, y = self.x * s + x, self.y * s + y
163
164 local font = self.white_font
165 local staff = self.world.available_staff
166 local staff_label_x = x + 16 * s
167 local staff_label_w = 26 * s
168 font:draw(canvas, #staff.Doctor , staff_label_x, y + 58 * s, staff_label_w, 0)
169 font:draw(canvas, #staff.Nurse , staff_label_x, y + 137 * s, staff_label_w, 0)
170 font:draw(canvas, #staff.Handyman , staff_label_x, y + 216 * s, staff_label_w, 0)
171 font:draw(canvas, #staff.Receptionist, staff_label_x, y + 295 * s, staff_label_w, 0)
172 if self.category and self.current_index then
173 local profile = staff[self.category]
174 profile = profile and profile[self.current_index]
175 if not profile then
176 return
177 end
178 font:draw(canvas, profile:getFullName(), x + 79 * s, y + 21 * s)
179 profile:drawFace(canvas, x + 158 * s, y + 48 * s, self.face_parts, s)
180 font:draw(canvas, "$" .. profile.wage, x + 116 * s, y + 179 * s)
181 font:drawWrapped(canvas, profile.desc, x + 74 * s, y + 205 * s, 149 * s)
182 -- Skill bar
183 if self.skill_bg_panel.visible then
184 local skill_bar_width = math.floor(profile.skill * 40 * s + 0.5)
185 if skill_bar_width ~= 0 then
186 local px, py = self.skill_bg_panel.x, self.skill_bg_panel.y
187 px = px * s + x
188 py = py * s + y
189 for dx = 0, skill_bar_width - 1, s do
190 self.panel_sprites:draw(canvas, 3, px + 22 * s + dx, py + 9 * s, { scaleFactor = s })
191 end
192 end
193 end
194 if self.category == "Doctor" then
195 -- Junior / Doctor / Consultant marker
196 self.panel_sprites:draw(canvas, 258, x + 71 * s, y + 49 * s, { scaleFactor = s })
197 if profile.is_junior then
198 self.panel_sprites:draw(canvas, 296, x + 79 * s, y + 80 * s, { scaleFactor = s })
199 elseif profile.is_consultant then
200 self.panel_sprites:draw(canvas, 296, x + 131 * s, y + 80 * s, { scaleFactor = s })
201 else
202 self.panel_sprites:draw(canvas, 296, x + 101 * s, y + 80 * s, { scaleFactor = s })
203 end
204 -- Ability markers
205 local px, py = self.ability_bg_panel.x, self.ability_bg_panel.y
206 px = px * s + x
207 py = py * s + y
208 if profile.is_surgeon >= 1.0 then
209 self.panel_sprites:draw(canvas, 292, px + 65 * s, py + 22 * s, { scaleFactor = s })
210 end
211 if profile.is_psychiatrist >= 1.0 then
212 self.panel_sprites:draw(canvas, 293, px + 82 * s, py + 28 * s, { scaleFactor = s })
213 end
214 if profile.is_researcher >= 1.0 then
215 self.panel_sprites:draw(canvas, 294, px + 109 * s, py + 27 * s, { scaleFactor = s })
216 end
217 end
218 end
219 end
220
221 function UIHireStaff:movePrevious()
222 self:moveBy(-1)
223 self.ui:tutorialStep(2, 4, 5)
224 end
225
226 function UIHireStaff:moveNext()
227 self.ui:tutorialStep(2, 3, 4)
228 self:moveBy(1)
229 end
230
231 function UIHireStaff:moveBy(n)
232 if self.category then
233 self.current_index = self.current_index + n
234 local category = self.world.available_staff[self.category]
235 if self.current_index < 1 then
236 self.current_index = 1
237 elseif self.current_index > #category then
238 self.current_index = #category
239 else
240 self.ui:playSound("selectx.wav")
241 self:updateTooltips()
242 return true
243 end
244 end
245 self.ui:playSound("wrong2.wav")
246 return false
247 end
248
249 function UIHireStaff:setCategory(name)
250 if name == "Receptionist" then
251 self.ui:tutorialStep(2, 2, 3)
252 else
253 self.ui:tutorialStep(2, {3, 4, 5}, 2)
254 end
255 if name == "Doctor" then
256 self.ui:tutorialStep(4, 2, 3)
257 else
258 self.ui:tutorialStep(4, 3, 2)
259 end
260 self.skill_bg_panel.visible = not not name
261 self.complete_blanker.visible = not name
262 self.abilities_blanker.visible = name ~= "Doctor"
263 self.category = name
264 for _, btn in ipairs(self.tabs) do
265 local should_be_toggled = btn.on_click_self == name
266 if btn.toggled ~= should_be_toggled then
267 btn:toggle()
268 end
269 end
270 self.current_index = 1
271 self:updateTooltips()
272 end
273
274 function UIHireStaff:close()
275 self.ui:tutorialStep(2, {2, 3, 4, 5}, 1)
276 self.ui:tutorialStep(4, {2, 3}, 1)
277 return Window.close(self)
278 end
279
280 function UIHireStaff:afterLoad(old, new)
281 Window.afterLoad(self, old, new)
282 self:registerKeyHandlers()
283
284 if old < 236 then
285 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
286 self.face_parts = TheApp.gfx:loadRaw("Face01V", 65, 1350, nil, "Data", "MPalette.dat", false, { flags = DrawFlags.Nearest })
287 end
288 end