Debian Codegraph @

CorsixTH/Lua/dialogs/queue_dialog.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 local TH = require("TH")
22
23 --! Room / door / reception desk queue visualisation dialog.
24 class "UIQueue" (Window)
25
26 ---@type UIQueue
27 local UIQueue = _G["UIQueue"]
28
29 function UIQueue:UIQueue(ui, queue)
30 self:Window()
31
32 local app = ui.app
33 self.esc_closes = true
34 self.ui = ui
35 self.modal_class = "main"
36 self.width = 604
37 self.height = 122
38 self:setDefaultPosition(0.5, 0.5)
39 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req06V", true)
40 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
41
42 self.queue = queue
43
44 self:addPanel(364, 0, 0) -- Right extremity of the panel
45 for x = 21, 83, 4 do
46 self:addPanel(365, x, 0)
47 end
48 self:addPanel(366, 85, 0)
49 for x = 223, 531, 7 do
50 self:addPanel(367, x, 0)
51 end
52 self:addPanel(368, 529, 0) -- Left extremity of the panel
53 self:addPanel(369, 97, self.height - 33):makeButton(0, 0, 17, 17, 370, self.decreaseMaxSize):setTooltip(_S.tooltip.queue_window.dec_queue_size)
54 self:addPanel(371, 144, self.height - 33):makeButton(0, 0, 17, 17, 372, self.increaseMaxSize):setTooltip(_S.tooltip.queue_window.inc_queue_size)
55 self:addPanel(373, self.width - 42, 17):makeButton(0, 0, 24, 24, 374, self.close):setTooltip(_S.tooltip.queue_window.close)
56
57 self:makeTooltip(_S.tooltip.queue_window.num_in_queue, 15, 15, 163, 36)
58 self:makeTooltip(_S.tooltip.queue_window.num_expected, 15, 39, 163, 60)
59 self:makeTooltip(_S.tooltip.queue_window.num_entered, 15, 62, 163, 83)
60 self:makeTooltip(_S.tooltip.queue_window.max_queue_size, 15, 87, 163, 108)
61
62 self:makeTooltip(_S.tooltip.queue_window.front_of_queue, 168, 25, 213, 105)
63 self:makeTooltip(_S.tooltip.queue_window.end_of_queue, 543, 51, 586, 105)
64 self:makeTooltip(_S.tooltip.queue_window.patient .. " " .. _S.misc.not_yet_implemented, 218, 15, 537, 107)
65 end
66
67 function UIQueue:decreaseMaxSize()
68 local amount = 1
69 if self.ui.app.key_modifiers.ctrl then
70 amount = amount * 10
71 elseif self.ui.app.key_modifiers.shift then
72 amount = amount * 5
73 end
74 self.queue:decreaseMaxSize(amount)
75 end
76
77 function UIQueue:increaseMaxSize()
78 local amount = 1
79 if self.ui.app.key_modifiers.ctrl then
80 amount = amount * 10
81 elseif self.ui.app.key_modifiers.shift then
82 amount = amount * 5
83 end
84 self.queue:increaseMaxSize(amount)
85 end
86
87 function UIQueue:draw(canvas, x, y)
88 Window.draw(self, canvas, x, y)
89 local s = TheApp.config.ui_scale
90 x, y = self.x * s + x, self.y * s + y
91
92 local font = self.white_font
93 local queue = self.queue
94 local num_patients = queue:reportedSize()
95
96 font:draw(canvas, _S.queue_window.num_in_queue, x + 22 * s, y + 22 * s)
97 font:draw(canvas, num_patients, x + 140 * s, y + 22 * s)
98
99 font:draw(canvas, _S.queue_window.num_expected, x + 22 * s, y + 45 * s)
100 font:draw(canvas, queue:expectedSize(), x + 140 * s, y + 45 * s)
101
102 font:draw(canvas, _S.queue_window.num_entered, x + 22 * s, y + 68 * s)
103 font:draw(canvas, queue.visitor_count, x + 140 * s, y + 68 * s)
104
105 font:draw(canvas, _S.queue_window.max_queue_size, x + 22 * s, y + 93 * s)
106 font:draw(canvas, queue.max_size, x + 119 * s, y + 93 * s)
107
108 self:drawPatients(canvas, x, y)
109
110 -- Draw dragged patient in the cursor location
111 if self.dragged then
112 self:drawPatient(canvas, self.dragged.x, self.dragged.y, self.dragged.patient)
113 end
114 end
115
116 local function isInsideQueueBoundingBox(x, y)
117 local s = TheApp.config.ui_scale
118 local x_min = 219 * s
119 local x_max = 534 * s
120 local y_min = 15 * s
121 local y_max = 105 * s
122 return not (x < x_min or x > x_max or y < y_min or y > y_max)
123 end
124
125 function UIQueue:onMouseDown(button, x, y)
126 -- Allow normal window operations if the mouse is outside the listing of patients
127 if not isInsideQueueBoundingBox(x, y) then
128 return Window.onMouseDown(self, button, x, y)
129 end
130 local s = TheApp.config.ui_scale
131 local x_min = 219 * s
132 self.hovered = self:getHoveredPatient(x - x_min)
133 -- Select patient to drag - if left clicking.
134 if button == "left" then
135 self.dragged = self.hovered
136 if self.dragged then
137 self.dragged.x = x + self.x * s
138 self.dragged.y = y + self.y * s
139 end
140 elseif button == "right" and self.hovered then
141 -- Otherwise bring up the choice screen.
142 self.just_added = true
143 local px = self.x + math.floor(x / s)
144 local py = self.y + math.floor(y / s)
145 self.ui:addWindow(UIQueuePopup(self.ui, px, py, self.hovered.patient))
146 end
147 end
148
149 function UIQueue:onMouseUp(button, x, y)
150 if self.just_added then
151 self.just_added = false
152 else
153 -- Always remove any leftover popup windows
154 local window = self.ui:getWindow(UIQueuePopup)
155 if window then
156 window:close()
157 end
158 end
159 if button == "left" then
160 local s = TheApp.config.ui_scale
161 local queue = self.queue
162 local num_patients = queue:reportedSize()
163 local width = 276 * s
164 self.ui:setCursor(self.ui.default_cursor) -- reset cursor
165
166 if not self.dragged then
167 return Window.onMouseUp(self, button, x, y)
168 end
169
170 -- Check whether the dragged patient is still in the queue
171 local index = -1
172 for i = 1, num_patients do
173 if self.dragged.patient == queue:reportedHumanoid(i) then
174 index = i
175 break
176 end
177 end
178
179 if index == -1 then
180 self.dragged = nil
181 return
182 end
183 if x > 170 * s and x < 210 * s and y > 25 * s and y < 105 * s then -- Inside door bounding box
184 queue:movePatient(index, 'front') -- move to front
185 elseif x > 542 * s and x < 585 * s and y > 50 * s and y < 105 * s then -- Inside exit sign bounding box
186 queue:movePatient(index, 'back') -- move to back
187 elseif isInsideQueueBoundingBox(x, y) then
188 local dx = 1
189 if num_patients ~= 1 then
190 dx = math.floor(width / (num_patients - 1))
191 end
192 queue:movePatient(index, math.floor((x - 220 * s) / dx)) -- move to dropped position
193 self:onMouseMove(x, y, 0, 0)
194 end
195
196 -- Try to drop to another room
197 local room
198 local wx, wy = self.ui:ScreenToWorld(x + self.x * s, y + self.y * s)
199 wx = math.floor(wx)
200 wy = math.floor(wy)
201 if wx > 0 and wy > 0 and wx < self.ui.app.map.width and wy < self.ui.app.map.height then
202 room = self.ui.app.world:getRoom(wx, wy)
203 end
204
205 -- The new room must be of the same class as the current one and active
206 local this_room = self.dragged.patient.next_room_to_visit
207 if this_room and room and room ~= this_room and
208 room.room_info.id == this_room.room_info.id and room.is_active then
209 -- Move to another room
210 local patient = self.dragged.patient
211 patient:setNextAction(room:createEnterAction(patient))
212 patient.next_room_to_visit = room
213 patient:setDynamicInfoText(_S.dynamic_info.patient.actions.on_my_way_to:format(room.room_info.name))
214 room.door:updateDynamicInfo()
215 -- call staff to room if required
216 if not room:testStaffCriteria(room:getRequiredStaffCriteria()) then
217 patient.world.dispatcher:callForStaff(room)
218 end
219 end
220 end
221 self.dragged = nil
222 end
223
224 function UIQueue:onMouseMove(x, y, dx, dy)
225 local s = TheApp.config.ui_scale
226 local x_min = 219 * s
227 if self.dragged then
228 self.dragged.x = x + self.x * s
229 self.dragged.y = y + self.y * s
230
231 -- Change cursor when outside queue dialog
232 if x > 0 and x < 605 * s and y > 0 and y < 120 * s then
233 self.ui:setCursor(self.ui.default_cursor)
234 else
235 self.ui:setCursor(self.ui.app.gfx:loadMainCursor("queue_drag"))
236 end
237 end
238 if not isInsideQueueBoundingBox(x, y) then
239 self.hovered = nil
240 Window:onMouseMove(x, y, dx, dy)
241 return
242 end
243
244 -- Update hovered patient
245 self.hovered = self:getHoveredPatient(x - x_min)
246 Window:onMouseMove(x, y, dx, dy)
247 end
248
249 function UIQueue:close()
250 -- Always remove any leftover popup windows
251 local window = self.ui:getWindow(UIQueuePopup)
252 if window then
253 window:close()
254 end
255 Window.close(self)
256 end
257
258 function UIQueue:getHoveredPatient(x)
259 local queue = self.queue
260 local num_patients = queue:reportedSize()
261 local s = TheApp.config.ui_scale
262 local width = 276 * s
263 local gap = 10 * s
264 x = x - 15 * s -- sprite offset
265
266 local dx = 0
267 if num_patients ~= 1 then
268 dx = math.floor(width / (num_patients - 1))
269 end
270
271 local offset = 0
272 local closest = nil
273
274 -- Find the closest patient to the given x-coordinate
275 for index = 1, num_patients do
276 local patient = queue:reportedHumanoid(index)
277 local patient_x = (index - 1) * dx + offset
278 local diff = math.abs(patient_x - x)
279
280 -- Take into account the gap between the hovered patient and other patients
281 if self.hovered and patient == self.hovered.patient then
282 offset = gap * 2
283 diff = diff + gap
284 end
285
286 if not closest or diff < closest.diff then
287 closest = {patient = patient, diff = diff, x = x}
288 end
289 end
290
291 -- The closest patient must be close enough (i.e. almost over the patient sprite)
292 if not closest or closest.diff > 25 * s then
293 return nil
294 end
295
296 return {patient = closest.patient, x = closest.x}
297 end
298
299 function UIQueue:drawPatients(canvas, x, y)
300 local s = TheApp.config.ui_scale
301 local queue = self.queue
302 local num_patients = queue:reportedSize()
303 local width = 276 * s
304 local gap = 10 * s
305 local dx = 0
306
307 if not self.hovered then
308 if num_patients ~= 1 then
309 dx = math.floor(width / (num_patients - 1))
310 end
311
312 for index = 1, num_patients do
313 local patient = queue:reportedHumanoid(index)
314 self:drawPatient(canvas, x + 239 * s + dx * (index - 1), y + 75 * s, patient)
315 end
316 else
317 if num_patients ~= 1 then
318 dx = math.floor((width - 2 * gap) / (num_patients - 1))
319 end
320
321 x = x + 239 * s
322 y = y + 75 * s
323 for index = 1, num_patients do
324 local patient = queue:reportedHumanoid(index)
325 if patient == self.hovered.patient then
326 x = x + gap
327 self:drawPatient(canvas, x, y - 10 * s, patient)
328 x = x + gap + dx
329 else
330 self:drawPatient(canvas, x, y, patient)
331 x = x + dx
332 end
333 end
334 end
335 end
336
337 function UIQueue:drawPatient(canvas, x, y, patient)
338 local anim = TH.animation()
339 local idle_anim = patient.getIdleAnimation(patient.humanoid_class)
340 anim:setAnimation(self.ui.app.world.anims, idle_anim, 1) -- flag 1 is for having patients in west position (looking the door in the dialog)
341 for layer, id in pairs(patient.layers) do
342 anim:setLayer(layer, id)
343 end
344 local s = TheApp.config.ui_scale
345 canvas:scale(s)
346 anim:draw(canvas, math.floor(x / s), math.floor(y / s))
347 -- Also draw the mood of the patient, if any.
348 local mood = patient:getCurrentMood()
349 if mood then
350 mood:draw(canvas, math.floor(x / s), math.floor(y / s) + 24)
351 end
352 canvas:scale(1)
353 end
354
355 function UIQueue:afterLoad(old, new)
356 if old < 236 then
357 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
358 end
359 Window.afterLoad(self, old, new)
360 end
361
362 class "UIQueuePopup" (Window)
363
364 ---@type UIQueuePopup
365 local UIQueuePopup = _G["UIQueuePopup"]
366
367 function UIQueuePopup:UIQueuePopup(ui, x, y, patient)
368 self:Window()
369 self.esc_closes = true
370 self.ui = ui
371 self.patient = patient
372 self.width = 188
373 self.height = 68
374 local app = ui.app
375 self.modal_class = "popup"
376 self:setDefaultPosition(x, y)
377
378 -- Background sprites
379 self:addPanel(375, 0, 0)
380
381 -- Buttons
382 self:addPanel(0, 12, 12):makeButton(0, 0, 81, 54, 378, self.sendToReception)
383 self:addPanel(0, 95, 12):makeButton(0, 0, 81, 54, 379, self.sendHome)
384 self:addPanel(377, 0, 58)
385
386 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req06V", true)
387 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
388 end
389
390 function UIQueuePopup:draw(canvas, x, y)
391 Window.draw(self, canvas, x, y)
392 -- TODO: Same as above.
393 -- x, y = self.x + x, self.y + y
394 --[[for i, hospital in ipairs(self.ui.app.world.hospitals) do
395 self.white_font:draw(canvas, hospital.name:upper() , x + 74, y + 78 + (i-1)*34, 92, 0)
396 end]]
397 end
398
399 function UIQueuePopup:sendToReception()
400 self.patient:setNextAction(SeekReceptionAction())
401 self:close()
402 end
403
404 function UIQueuePopup:sendHome()
405 self.patient:goHome("kicked")
406 self:close()
407 end
408
409 function UIQueuePopup:afterLoad(old, new)
410 if old < 236 then
411 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
412 end
413 Window.afterLoad(self, old, new)
414 end