Debian Codegraph @

CorsixTH/Lua/dialogs/fullscreen/hospital_policy.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 --! Hospital policy fullscreen window (set staff tiredness and patient cure thresholds, etc.).
22 class "UIPolicy" (UIFullscreen)
23
24 ---@type UIPolicy
25 local UIPolicy = _G["UIPolicy"]
26
27 function UIPolicy:UIPolicy(ui)
28 self:UIFullscreen(ui)
29 local gfx = ui.app.gfx
30 if not pcall(function()
31 self.background = gfx:loadRaw("Pol01V", 640, 480, "QData", "QData", "Pol01V.pal", true)
32 local palette = gfx:getPalette("Pol01V.pal")
33 self.panel_sprites = gfx:loadSpriteTable("QData", "Pol02V", true, palette)
34 self.label_font = gfx:loadFontAndSpriteTable("QData", "Font74V", false, palette, { apply_ui_scale = true })
35 self.text_font = gfx:loadFontAndSpriteTable("QData", "Font105V", false, palette, { apply_ui_scale = true })
36 end) then
37 ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics}))
38 self:close()
39 return
40 end
41
42 local hosp = ui.hospital
43 self.hospital = hosp
44
45 local --[[persistable:hospital_policy_allow_staff]] function allowStaff(name)
46 if name == "Allow" then
47 if self.prohibit_button.toggled then -- Changing setting from prohibit to allow
48 hosp.policies["staff_allowed_to_move"] = true
49 self.prohibit_button:toggle()
50 else -- Already allowed, toggle again
51 self.allow_button:toggle()
52 end
53 else -- Clicking the prohibit button
54 if self.allow_button.toggled then -- Changing setting from allow to prohibit
55 hosp.policies["staff_allowed_to_move"] = false
56 self.allow_button:toggle()
57 else -- Already prohibited, toggle again
58 self.prohibit_button:toggle()
59 end
60 end
61 end
62
63 -- Buttons
64 self:addPanel(0, 607, 447):makeButton(0, 0, 26, 26, 6, self.close)
65 :setTooltip(_S.tooltip.policy.close)
66
67 self.allow_button = self:addPanel(0, 348, 379)
68 :makeToggleButton(0, 0, 48, 17, 4, allowStaff, "Allow")
69 :setTooltip(_S.tooltip.policy.staff_leave)
70 :setSound("selectx.wav") -- Allow staff to move
71
72 self.prohibit_button = self:addPanel(0, 395, 379)
73 :makeToggleButton(0, 0, 48, 17, 5, allowStaff, "Prohibit")
74 :setTooltip(_S.tooltip.policy.staff_stay)
75 :setSound("selectx.wav") -- Prohibit staff to move
76
77 if self.hospital.policies["staff_allowed_to_move"] then
78 self.allow_button:toggle()
79 else
80 self.prohibit_button:toggle()
81 end
82
83 -- Slider positions
84 local guess = 129 + math.floor(hosp.policies["guess_cure"] * 299)
85 local home = 129 + math.floor(hosp.policies["send_home"] * 299)
86 local stop = 124 + math.floor((hosp.policies["stop_procedure"] - 1) * 299)
87 local staffroom = 149 + math.floor(hosp.policies["goto_staffroom"] * 250)
88
89 -- Sliders
90 self.sliders = {}
91 self.sliders["guess_cure"] = self:addPanel(2, guess, 119, 82, 44)
92 self.sliders["send_home"] = self:addPanel(1, home, 135, 82, 28)
93 self.sliders["stop_procedure"] = self:addPanel(3, stop, 210, 92, 28)
94 self.sliders["goto_staffroom"] = self:addPanel(3, staffroom, 285, 92, 28)
95
96 self.sliders["guess_cure"].min_x = home
97 self.sliders["guess_cure"].total_min_x = 129 -- Needed to get the correct value set when windows is closed.
98 self.sliders["guess_cure"].max_x = 428
99 self.sliders["send_home"].min_x = 129
100 self.sliders["send_home"].max_x = guess
101 self.sliders["send_home"].total_max_x = 428
102 self.sliders["stop_procedure"].min_x = 124
103 self.sliders["stop_procedure"].max_x = 423
104 self.sliders["stop_procedure"].addition = true -- This value goes from 1 to 2.
105 self.sliders["goto_staffroom"].min_x = 149
106 self.sliders["goto_staffroom"].max_x = 399
107
108 -- Z order of the sliders.
109 -- The small 'send_home' slider must be tested first in 'self:panelHit',
110 -- to ensure it can always be moved, even if the big 'guess_cure' is at the
111 -- same position.
112 self.sliders_z = {self.sliders["send_home"], self.sliders["guess_cure"],
113 self.sliders["stop_procedure"], self.sliders["goto_staffroom"]}
114
115 -- Tooltips for slider bars
116 self:makeTooltip(_S.tooltip.policy.diag_procedure, 161, 119, 479, 174)
117 self:makeTooltip(_S.tooltip.policy.diag_termination, 161, 210, 479, 249)
118 self:makeTooltip(_S.tooltip.policy.staff_rest, 161, 285, 479, 324)
119 end
120
121 function UIPolicy:draw(canvas, x, y)
122 local s = TheApp.config.ui_scale
123 canvas:scale(s, "bitmap")
124 self.background:draw(canvas, self.x * s + x, self.y * s + y)
125 canvas:scale(1, "bitmap")
126 UIFullscreen.draw(self, canvas, x, y)
127 x, y = self.x * s + x, self.y * s + y
128
129 local text = self.text_font
130 local label = self.label_font
131
132 -- Labels on the panels. Labels are always capitalised.
133 local added_x = self.sliders["send_home"].x * s
134 local added_y = (self.sliders["send_home"].y + 2) * s
135 label:draw(canvas, _S.policy.sliders.send_home:upper(), x + added_x, y + added_y, 82 * s, 0)
136
137 added_x = self.sliders["guess_cure"].x * s
138 added_y = (self.sliders["guess_cure"].y + 2) * s
139 label:draw(canvas, _S.policy.sliders.guess:upper(), x + added_x, y + added_y, 82 * s, 0)
140
141 added_x = self.sliders["stop_procedure"].x * s
142 added_y = (self.sliders["stop_procedure"].y + 2) * s
143 label:draw(canvas, _S.policy.sliders.stop:upper(), x + added_x, y + added_y, 92 * s, 0)
144
145 added_x = self.sliders["goto_staffroom"].x * s
146 added_y = (self.sliders["goto_staffroom"].y + 2) * s
147 label:draw(canvas, _S.policy.sliders.staff_room:upper(), x + added_x, y + added_y, 92 * s, 0)
148
149 -- All other text
150 text:draw(canvas, _S.policy.header, x + 160 * s, y + 78 * s, 300 * s, 0)
151 text:draw(canvas, _S.policy.diag_procedure, x + 161 * s, y + 100 * s)
152 text:draw(canvas, _S.policy.diag_termination, x + 161 * s, y + 181 * s)
153 text:draw(canvas, _S.policy.staff_rest, x + 161 * s, y + 262 * s)
154 text:draw(canvas, _S.policy.staff_leave_rooms, x + 161 * s, y + 374 * s)
155 end
156
157 function UIPolicy:onMouseMove(x, y, dx, dy)
158 local repaint = UIFullscreen.onMouseMove(self, x, y, dx, dy)
159 if self.moving_panel then -- A slider is being moved.
160 local s = TheApp.config.ui_scale
161 local p = self.moving_panel
162 self.moved_x = self.moved_x + dx / s
163 local new_x = math.floor(self.moved_x + self.down_x - self.moving_panel.w / 2 - self.offset)
164 if new_x > p.min_x then
165 if new_x < p.max_x then
166 self.moving_panel.x = new_x
167 self.position_x = new_x
168 else
169 self.moving_panel.x = self.moving_panel.max_x
170 self.position_x = self.moving_panel.max_x
171 end
172 else
173 self.moving_panel.x = self.moving_panel.min_x
174 self.position_x = self.moving_panel.min_x
175 end
176 repaint = true
177 end
178 return repaint
179 end
180
181 function UIPolicy:onMouseDown(code, x, y)
182 if code == "left" then
183 self.moving_panel = self:panelHit(x, y)
184 if self.moving_panel then
185 self.down_x = x
186 self.offset = x - (self.moving_panel.x + self.moving_panel.w / 2)
187 self.moved_x = 0
188 self.position_x = self.moving_panel.x
189 return true
190 end
191 end
192 return UIFullscreen.onMouseDown(self, code, x, y)
193 end
194
195 function UIPolicy:onMouseUp(code, x, y)
196 if self.moving_panel then
197 if self.moving_panel == self.sliders["guess_cure"] then
198 self.sliders["send_home"].max_x = self.position_x
199 elseif self.moving_panel == self.sliders["send_home"] then
200 self.sliders["guess_cure"].min_x = self.position_x
201 end
202 end
203 self.moving_panel = nil
204 return UIFullscreen.onMouseUp(self, code, x, y)
205 end
206
207 --! Detect which slider is clicked by the mouse.
208 --!param x (int) X position of the mouse.
209 --!param y (int) Y position of the mouse.
210 --!return Slider that was detected at the given position, or nil
211 function UIPolicy:panelHit(x, y)
212 local s = TheApp.config.ui_scale
213 x = x / s
214 y = y / s
215 for _, panel in ipairs(self.sliders_z) do
216 if x > panel.x and y > panel.y and x < panel.x + panel.w and y < panel.y + panel.h then
217 return panel
218 end
219 end
220 return nil
221 end
222
223 --! Close the window.
224 function UIPolicy:close()
225 -- Save new slider positions in the hospital policies again for the next use.
226 for key, s in pairs(self.sliders or {}) do
227 local divider = (s.total_max_x or s.max_x) - (s.total_min_x or s.min_x)
228 local number = (s.addition and 1 or 0)
229 self.hospital.policies[key] = number + (s.x - (s.total_min_x or s.min_x))/divider
230 end
231 UIFullscreen.close(self)
232 self.ui:getWindow(UIBottomPanel):updateButtonStates()
233 end
234
235 function UIPolicy:afterLoad(old, new)
236 if old < 116 then -- Ensure panelHit tests the sliders in the right order.
237 self.sliders_z = {self.sliders["send_home"], self.sliders["guess_cure"],
238 self.sliders["stop_procedure"], self.sliders["goto_staffroom"]}
239 end
240 if old < 236 then
241 local gfx = TheApp.gfx
242
243 self.background = gfx:loadRaw("Pol01V", 640, 480, "QData", "QData", "Pol01V.pal", true)
244 local palette = gfx:getPalette("Pol01V.pal")
245 self.panel_sprites = gfx:loadSpriteTable("QData", "Pol02V", true, palette)
246 self.label_font = gfx:loadFontAndSpriteTable("QData", "Font74V", false, palette, { apply_ui_scale = true })
247 self.text_font = gfx:loadFontAndSpriteTable("QData", "Font105V", false, palette, { apply_ui_scale = true })
248 end
249
250 UIFullscreen.afterLoad(self, old, new)
251 end