| 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 |
--! Drug Casebook fullscreen window (view disease statistics and set prices). |
| 22 |
class "UICasebook" (UIFullscreen) |
| 23 |
|
| 24 |
---@type UICasebook |
| 25 |
local UICasebook = _G["UICasebook"] |
| 26 |
|
| 27 |
function UICasebook:UICasebook(ui, disease_selection) |
| 28 |
self:UIFullscreen(ui) |
| 29 |
local gfx = ui.app.gfx |
| 30 |
if not pcall(function() |
| 31 |
self.background = gfx:loadRaw("DrugN01V", 640, 480, "QData", "QData", "DrugN01V.pal") |
| 32 |
local palette = gfx:getPalette("DrugN01V.pal") |
| 33 |
self.panel_sprites = gfx:loadSpriteTable("QData", "DrugN02V", true, palette, { apply_ui_scale = true }) |
| 34 |
self.title_font = gfx:loadFontAndSpriteTable("QData", "Font25V", false, palette, { apply_ui_scale = true }) |
| 35 |
self.selected_title_font = gfx:loadFontAndSpriteTable("QData", "Font26V", false, palette, { apply_ui_scale = true }) |
| 36 |
self.drug_font = gfx:loadFontAndSpriteTable("QData", "Font24V", false, palette, { apply_ui_scale = true }) |
| 37 |
end) then |
| 38 |
ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics})) |
| 39 |
self:close() |
| 40 |
return |
| 41 |
end |
| 42 |
|
| 43 |
self.ui = ui |
| 44 |
self.hospital = ui.hospital |
| 45 |
self.casebook = self.hospital.disease_casebook |
| 46 |
self:updateDiseaseList() |
| 47 |
|
| 48 |
-- Buttons |
| 49 |
self:addPanel(0, 607, 449):makeButton(0, 0, 26, 26, 3, self.close):setTooltip(_S.tooltip.casebook.close) |
| 50 |
self:addPanel(0, 439, 29):makeRepeatButton(0, 0, 70, 46, 1, self.scrollUp):setTooltip(_S.tooltip.casebook.up) |
| 51 |
self:addPanel(0, 437, 394):makeRepeatButton(0, 0, 77, 53, 2, self.scrollDown):setTooltip(_S.tooltip.casebook.down) |
| 52 |
self:addPanel(0, 354, 133):makeRepeatButton(0, 0, 22, 22, 5, self.increasePay):setTooltip(_S.tooltip.casebook.increase) |
| 53 |
self:addPanel(0, 237, 133):makeRepeatButton(0, 0, 22, 22, 4, self.decreasePay):setTooltip(_S.tooltip.casebook.decrease) |
| 54 |
self:addPanel(0, 235, 400):makeButton(0, 0, 140, 20, 0, self.concentrateResearch) |
| 55 |
:setTooltip(_S.tooltip.casebook.research) |
| 56 |
|
| 57 |
self:registerKeyHandlers(); |
| 58 |
|
| 59 |
-- Icons representing cure effectiveness and other important information. |
| 60 |
self.machinery = self:addPanel(6, 306, 352):setTooltip(_S.tooltip.casebook.cure_type.machine) |
| 61 |
self.machinery.visible = false |
| 62 |
self.drug = self:addPanel(7, 306, 352):setDynamicTooltip(--[[persistable:casebook_drug_tooltip]] function() |
| 63 |
return _S.tooltip.casebook.cure_type.drug_percentage:format(self.casebook[self.selected_disease].cure_effectiveness) |
| 64 |
end) |
| 65 |
self.drug.visible = false |
| 66 |
self.surgery = self:addPanel(8, 306, 352):setTooltip(_S.tooltip.casebook.cure_type.surgery) |
| 67 |
self.surgery.visible = false |
| 68 |
self.unknown = self:addPanel(9, 306, 352):setTooltip(_S.tooltip.casebook.cure_type.unknown) |
| 69 |
self.unknown.visible = false |
| 70 |
self.psychiatry = self:addPanel(10, 306, 352):setTooltip(_S.tooltip.casebook.cure_type.psychiatrist) |
| 71 |
self.psychiatry.visible = false |
| 72 |
|
| 73 |
self.curable = self:addPanel(11, 335, 352):setTooltip(_S.tooltip.casebook.cure_requirement.possible) |
| 74 |
self.curable.visible = false |
| 75 |
self.not_curable = self:addPanel(12, 335, 352):setTooltip(_S.tooltip.casebook.cure_requirement.not_possible) -- TODO: split up in more specific requirements |
| 76 |
self.not_curable.visible = false |
| 77 |
|
| 78 |
self.percentage_counter = false -- Counter for displaying cure price percentage for a certain time before switching to price. |
| 79 |
|
| 80 |
self:makeTooltip(_S.tooltip.casebook.reputation, 249, 72, 362, 117) |
| 81 |
self:makeTooltip(_S.tooltip.casebook.treatment_charge, 249, 117, 362, 161) |
| 82 |
self:makeTooltip(_S.tooltip.casebook.earned_money, 247, 161, 362, 205) |
| 83 |
self:makeTooltip(_S.tooltip.casebook.cured, 247, 205, 362, 249) |
| 84 |
self:makeTooltip(_S.tooltip.casebook.deaths, 247, 249, 362, 293) |
| 85 |
self:makeTooltip(_S.tooltip.casebook.sent_home, 247, 293, 362, 337) |
| 86 |
|
| 87 |
if disease_selection then |
| 88 |
self:selectDisease(disease_selection) |
| 89 |
elseif self.ui.casebook_selected_disease then |
| 90 |
self:selectDisease(self.ui.casebook_selected_disease) |
| 91 |
else |
| 92 |
self.selected_index = #self.names_sorted |
| 93 |
self.selected_disease = self.names_sorted[self.selected_index] |
| 94 |
self:updateIcons() |
| 95 |
end |
| 96 |
end |
| 97 |
|
| 98 |
function UICasebook:registerKeyHandlers() |
| 99 |
-- Hotkeys |
| 100 |
self:addKeyHandler("ingame_scroll_up", self.scrollUp) |
| 101 |
self:addKeyHandler("ingame_scroll_down", self.scrollDown) |
| 102 |
self:addKeyHandler("ingame_scroll_left", self.decreasePay) |
| 103 |
self:addKeyHandler("ingame_scroll_right", self.increasePay) |
| 104 |
end |
| 105 |
|
| 106 |
function UICasebook:close() |
| 107 |
UIFullscreen.close(self) |
| 108 |
self.ui:getWindow(UIBottomPanel):updateButtonStates() |
| 109 |
end |
| 110 |
|
| 111 |
--! The diseases list has been changed, update the list. |
| 112 |
function UICasebook:updateDiseaseList() |
| 113 |
-- A sorted list of known diseases and pseudo diseases. |
| 114 |
-- Used to be able to list the diseases in, believe it or not, |
| 115 |
-- alphabetical order. |
| 116 |
self.names_sorted = {} |
| 117 |
for n, value in pairs(self.casebook) do |
| 118 |
if value.discovered then |
| 119 |
self.names_sorted[#self.names_sorted + 1] = n |
| 120 |
end |
| 121 |
end |
| 122 |
table.sort(self.names_sorted, function(d1, d2) |
| 123 |
local c1, c2 = self.casebook[d1], self.casebook[d2] |
| 124 |
if c1.pseudo ~= c2.pseudo then |
| 125 |
return c1.pseudo |
| 126 |
end |
| 127 |
return c1.disease.name:upper() < c2.disease.name:upper() |
| 128 |
end) |
| 129 |
|
| 130 |
if self.selected_disease then -- Re-select the current disease. |
| 131 |
self:selectDisease(self.selected_disease) |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
--! Select a disease by name. |
| 136 |
--!param disease (string) Name of the disease |
| 137 |
function UICasebook:selectDisease(disease) |
| 138 |
for i = 1, #self.names_sorted do |
| 139 |
if disease == self.names_sorted[i] then |
| 140 |
self.selected_index = i |
| 141 |
self.selected_disease = self.names_sorted[self.selected_index] |
| 142 |
break |
| 143 |
end |
| 144 |
end |
| 145 |
self:updateIcons() |
| 146 |
end |
| 147 |
|
| 148 |
--! Function that is called when a new entry is selected in some way |
| 149 |
--! It updates all icons etc. that react to what is selected |
| 150 |
function UICasebook:updateIcons() |
| 151 |
local disease = self.selected_disease |
| 152 |
local hosp = self.hospital |
| 153 |
|
| 154 |
local known = true |
| 155 |
-- Curable / not curable icons and their tooltip |
| 156 |
if self.casebook[disease].pseudo then |
| 157 |
self.curable.visible = false |
| 158 |
self.not_curable.visible = false |
| 159 |
else |
| 160 |
local req = hosp:checkDiseaseRequirements(disease) |
| 161 |
if not req then |
| 162 |
self.curable.visible = true |
| 163 |
self.not_curable.visible = false |
| 164 |
else |
| 165 |
self.curable.visible = false |
| 166 |
self.not_curable.visible = true |
| 167 |
|
| 168 |
-- Strings for the tooltip |
| 169 |
local research = false |
| 170 |
local build = false |
| 171 |
local staff = false |
| 172 |
-- Room requirements |
| 173 |
for _, room_id in ipairs(req.rooms) do |
| 174 |
-- Not researched yet? |
| 175 |
if not hosp:isRoomDiscovered(room_id) then |
| 176 |
known = false |
| 177 |
research = (research and (research .. ", ") or " (") .. TheApp.rooms[room_id].name |
| 178 |
end |
| 179 |
-- Researched, but not built. TODO: maybe make this an else clause to not oversize the tooltip that much |
| 180 |
build = (build and (build .. ", ") or " (") .. TheApp.rooms[room_id].name |
| 181 |
end |
| 182 |
research = research and (_S.tooltip.casebook.cure_requirement.research_machine .. research .. "). ") or "" |
| 183 |
build = build and (_S.tooltip.casebook.cure_requirement.build_room .. build .. "). ") or "" |
| 184 |
|
| 185 |
-- Staff requirements |
| 186 |
for sclass, amount in pairs(req.staff) do |
| 187 |
staff = (staff and (staff .. ", ") or " (") .. StaffProfile.translateStaffClass(sclass) .. ": " .. amount |
| 188 |
end |
| 189 |
staff = staff and (_S.tooltip.casebook.cure_requirement.hire_staff .. staff .. "). ") or "" |
| 190 |
|
| 191 |
self.not_curable:setTooltip(research .. build .. staff) |
| 192 |
end |
| 193 |
end |
| 194 |
|
| 195 |
self.unknown.visible = not known |
| 196 |
self.drug.visible = known and not not self.casebook[disease].drug |
| 197 |
self.machinery.visible = known and not not self.casebook[disease].machine and not self.casebook[disease].pseudo |
| 198 |
self.psychiatry.visible = known and not not self.casebook[disease].psychiatrist |
| 199 |
self.surgery.visible = known and not not self.casebook[disease].surgeon |
| 200 |
|
| 201 |
self.ui:updateTooltip() -- for the case that mouse is hovering over icon while player scrolls through list with keys |
| 202 |
self.percentage_counter = 50 |
| 203 |
self.ui.casebook_selected_disease = disease |
| 204 |
end |
| 205 |
|
| 206 |
function UICasebook:draw(canvas, x, y) |
| 207 |
local s = TheApp.config.ui_scale |
| 208 |
canvas:scale(s, "bitmap") |
| 209 |
self.background:draw(canvas, self.x * s + x, self.y * s + y) |
| 210 |
canvas:scale(1, "bitmap") |
| 211 |
UIFullscreen.draw(self, canvas, x, y) |
| 212 |
|
| 213 |
x, y = self.x * s + x, self.y * s + y |
| 214 |
local titles = self.title_font |
| 215 |
local book = self.casebook |
| 216 |
local disease = self.selected_disease |
| 217 |
local selected = self.selected_index |
| 218 |
|
| 219 |
-- All titles |
| 220 |
titles:draw(canvas, _S.casebook.reputation, x + 278 * s, y + 68 * s) |
| 221 |
titles:draw(canvas, _S.casebook.treatment_charge, x + 260 * s, y + 113 * s) |
| 222 |
titles:draw(canvas, _S.casebook.earned_money, x + 265 * s, y + 157 * s) |
| 223 |
titles:draw(canvas, _S.casebook.cured, x + 276 * s, y + 201 * s) |
| 224 |
titles:draw(canvas, _S.casebook.deaths, x + 279 * s, y + 245 * s) |
| 225 |
titles:draw(canvas, _S.casebook.sent_home, x + 270 * s, y + 289 * s) |
| 226 |
titles:draw(canvas, _S.casebook.cure, x + 255 * s, y + 354 * s) |
| 227 |
|
| 228 |
-- Specific disease information |
| 229 |
if self.hospital:canConcentrateResearch(disease) then |
| 230 |
if book[disease].concentrate_research then -- Concentrate research |
| 231 |
self.selected_title_font:draw(canvas, _S.casebook.research, x + 245 * s, y + 398 * s) |
| 232 |
else |
| 233 |
titles:draw(canvas, _S.casebook.research, x + 245 * s, y + 398 * s) |
| 234 |
end |
| 235 |
end |
| 236 |
local rep = book[disease].reputation or self.hospital.reputation |
| 237 |
if rep < self.hospital.reputation_min then |
| 238 |
rep = self.hospital.reputation_min |
| 239 |
elseif rep > self.hospital.reputation_max then |
| 240 |
rep = self.hospital.reputation_max |
| 241 |
end |
| 242 |
|
| 243 |
titles:draw(canvas, rep, x + 248 * s, y + 92 * s, 114 * s, 0) -- Reputation |
| 244 |
|
| 245 |
-- Treatment Charge is either displayed in percent, or normally |
| 246 |
local price_text = self.percentage_counter and ("%.0f%%"):format(book[disease].price * 100) or |
| 247 |
"$" .. self.hospital:getTreatmentPrice(disease) |
| 248 |
titles:draw(canvas, price_text, x + 262 * s, y + 137 * s, 90 * s, 0) -- Treatment Charge |
| 249 |
|
| 250 |
titles:draw(canvas, "$" .. book[disease].money_earned, x + 248 * s, y + 181 * s, 114 * s, 0) -- Money Earned |
| 251 |
titles:draw(canvas, book[disease].recoveries, x + 248 * s, y + 225 * s, 114 * s, 0) -- Recoveries |
| 252 |
titles:draw(canvas, book[disease].fatalities, x + 248 * s, y + 269 * s, 114 * s, 0) -- Fatalities |
| 253 |
titles:draw(canvas, book[disease].turned_away, x + 248 * s, y + 313 * s, 114 * s, 0) -- Turned away |
| 254 |
|
| 255 |
-- Cure percentage |
| 256 |
if self.drug.visible then |
| 257 |
self.drug_font:draw(canvas, book[disease].cure_effectiveness, x + 313 * s, y + 364 * s, 16 * s, 0) |
| 258 |
end |
| 259 |
-- Right-hand side list of diseases (and pseudo diseases) |
| 260 |
local index = 1 |
| 261 |
while selected - index > 0 and index <= 7 do |
| 262 |
titles:draw(canvas, book[self.names_sorted[selected - index]].disease.name:upper(), x + 409 * s, y + 203 * s - index * 18 * s) |
| 263 |
index = index + 1 |
| 264 |
end |
| 265 |
self.selected_title_font:draw(canvas, book[disease].disease.name:upper(), x + 409 * s, y + 227 * s) |
| 266 |
index = 1 |
| 267 |
while index + selected <= #self.names_sorted and index <= 7 do |
| 268 |
titles:draw(canvas, book[self.names_sorted[index + selected]].disease.name:upper(), x + 409 * s, y + 251 * s + index * 18 * s) |
| 269 |
index = index + 1 |
| 270 |
end |
| 271 |
end |
| 272 |
|
| 273 |
function UICasebook:scrollUp() |
| 274 |
if self.selected_index > 1 then |
| 275 |
if self.ui.app.key_modifiers.ctrl then |
| 276 |
self.selected_index = 1 |
| 277 |
else |
| 278 |
self.selected_index = self.selected_index - 1 |
| 279 |
end |
| 280 |
|
| 281 |
self.selected_disease = self.names_sorted[self.selected_index] |
| 282 |
self.ui:playSound("pagetur2.wav") |
| 283 |
else |
| 284 |
self.ui:playSound("Wrong2.wav") |
| 285 |
end |
| 286 |
self:updateIcons() |
| 287 |
end |
| 288 |
|
| 289 |
function UICasebook:scrollDown() |
| 290 |
if self.selected_index < #self.names_sorted then |
| 291 |
if self.ui.app.key_modifiers.ctrl then |
| 292 |
self.selected_index = #self.names_sorted |
| 293 |
else |
| 294 |
self.selected_index = self.selected_index + 1 |
| 295 |
end |
| 296 |
|
| 297 |
self.selected_disease = self.names_sorted[self.selected_index] |
| 298 |
self.ui:playSound("pagetur2.wav") |
| 299 |
else |
| 300 |
self.ui:playSound("Wrong2.wav") |
| 301 |
end |
| 302 |
self:updateIcons() |
| 303 |
end |
| 304 |
|
| 305 |
function UICasebook:increasePay() |
| 306 |
local price = self.casebook[self.selected_disease].price |
| 307 |
local amount = 0.01 |
| 308 |
if self.ui.app.key_modifiers.ctrl then |
| 309 |
amount = amount * 25 |
| 310 |
elseif self.ui.app.key_modifiers.shift then |
| 311 |
amount = amount * 5 |
| 312 |
end |
| 313 |
price = price + amount |
| 314 |
if price > 2 then |
| 315 |
price = 2 |
| 316 |
self.ui:playSound("Wrong2.wav") |
| 317 |
else |
| 318 |
self.ui:playSound("selectx.wav") |
| 319 |
end |
| 320 |
self.casebook[self.selected_disease].price = price |
| 321 |
self.percentage_counter = 50 |
| 322 |
end |
| 323 |
|
| 324 |
function UICasebook:decreasePay() |
| 325 |
local price = self.casebook[self.selected_disease].price |
| 326 |
local amount = 0.01 |
| 327 |
if self.ui.app.key_modifiers.ctrl then |
| 328 |
amount = amount * 25 |
| 329 |
elseif self.ui.app.key_modifiers.shift then |
| 330 |
amount = amount * 5 |
| 331 |
end |
| 332 |
price = price - amount |
| 333 |
if price < 0.5 then |
| 334 |
price = 0.5 |
| 335 |
self.ui:playSound("Wrong2.wav") |
| 336 |
else |
| 337 |
self.ui:playSound("selectx.wav") |
| 338 |
end |
| 339 |
self.casebook[self.selected_disease].price = price |
| 340 |
self.percentage_counter = 50 |
| 341 |
end |
| 342 |
|
| 343 |
function UICasebook:concentrateResearch() |
| 344 |
if self.hospital:canConcentrateResearch(self.selected_disease) then |
| 345 |
self.hospital.research:concentrateResearch(self.selected_disease) |
| 346 |
end |
| 347 |
end |
| 348 |
|
| 349 |
function UICasebook:onMouseDown(button, x, y) |
| 350 |
local s = TheApp.config.ui_scale |
| 351 |
-- Normal window operations if outside the disease list |
| 352 |
if x < 395 * s or x > 540 * s or y < 77 * s or y > 394 * s then |
| 353 |
return UIFullscreen.onMouseDown(self, button, x, y) |
| 354 |
end |
| 355 |
|
| 356 |
local index_diff |
| 357 |
if y < 203 * s then |
| 358 |
index_diff = -7 + math.floor((y - (77 * s)) / (18 * s)) |
| 359 |
elseif y > 269 * s then |
| 360 |
index_diff = math.floor((y - (269 * s)) / (18 * s)) + 1 |
| 361 |
else |
| 362 |
return |
| 363 |
end |
| 364 |
|
| 365 |
-- Clicking on a disease name scrolls to the disease |
| 366 |
local new_index = self.selected_index + index_diff |
| 367 |
if new_index >= 1 and new_index <= #self.names_sorted then |
| 368 |
self.selected_index = new_index |
| 369 |
self.selected_disease = self.names_sorted[self.selected_index] |
| 370 |
self.ui:playSound("pagetur2.wav") |
| 371 |
self:updateIcons() |
| 372 |
end |
| 373 |
end |
| 374 |
|
| 375 |
function UICasebook:onMouseWheel(x, y) |
| 376 |
if not UIFullscreen.onMouseWheel(self, x, y) then |
| 377 |
if self:hitTest(self.cursor_x, self.cursor_y) then |
| 378 |
if y > 0 then |
| 379 |
self:scrollUp() |
| 380 |
return true |
| 381 |
else |
| 382 |
self:scrollDown() |
| 383 |
return true |
| 384 |
end |
| 385 |
end |
| 386 |
return false |
| 387 |
else |
| 388 |
return true |
| 389 |
end |
| 390 |
end |
| 391 |
|
| 392 |
function UICasebook:onTick() |
| 393 |
-- Decrease counter for showing percentage of cure price, if applicable |
| 394 |
if self.percentage_counter then |
| 395 |
self.percentage_counter = self.percentage_counter - 1 |
| 396 |
if self.percentage_counter <= 0 then |
| 397 |
self.percentage_counter = false |
| 398 |
end |
| 399 |
end |
| 400 |
return UIFullscreen.onTick(self) |
| 401 |
end |
| 402 |
|
| 403 |
function UICasebook:afterLoad(old, new) |
| 404 |
if old < 236 then |
| 405 |
local gfx = TheApp.gfx |
| 406 |
self.background = gfx:loadRaw("DrugN01V", 640, 480, "QData", "QData", "DrugN01V.pal", true) |
| 407 |
local palette = gfx:getPalette("DrugN01V.pal") |
| 408 |
self.panel_sprites = gfx:loadSpriteTable("QData", "DrugN02V", true, palette, { apply_ui_scale = true }) |
| 409 |
self.title_font = gfx:loadFontAndSpriteTable("QData", "Font25V", false, palette, { apply_ui_scale = true }) |
| 410 |
self.selected_title_font = gfx:loadFontAndSpriteTable("QData", "Font26V", false, palette, { apply_ui_scale = true }) |
| 411 |
self.drug_font = gfx:loadFontAndSpriteTable("QData", "Font24V", false, palette, { apply_ui_scale = true }) |
| 412 |
end |
| 413 |
|
| 414 |
UIFullscreen.afterLoad(self, old, new) |
| 415 |
self:registerKeyHandlers() |
| 416 |
end |