| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
Copyright (c) 2024 Felipe "FMMazur" Mazur |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. --]] |
| 21 |
|
| 22 |
local TH = require("TH") |
| 23 |
local math_floor |
| 24 |
= math.floor |
| 25 |
|
| 26 |
--! Dialog for purchasing `Object`s (for the corridor or for rooms). |
| 27 |
class "UIFurnishCorridor" (Window) |
| 28 |
|
| 29 |
---@type UIFurnishCorridor |
| 30 |
local UIFurnishCorridor = _G["UIFurnishCorridor"] |
| 31 |
|
| 32 |
function UIFurnishCorridor:UIFurnishCorridor(ui, objects, edit_dialog) |
| 33 |
self:Window() |
| 34 |
|
| 35 |
local app = ui.app |
| 36 |
if edit_dialog then |
| 37 |
self.modal_class = "furnish" |
| 38 |
self.edit_dialog = edit_dialog |
| 39 |
else |
| 40 |
self.modal_class = "main" |
| 41 |
end |
| 42 |
self.esc_closes = true |
| 43 |
self.ui = ui |
| 44 |
self.anims = app.anims |
| 45 |
self.width = 360 |
| 46 |
self.height = 274 |
| 47 |
self:setDefaultPosition(0.5, 0.4) |
| 48 |
local selected_label_color = { red = 40, green = 40, blue = 250 } |
| 49 |
self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req10V", true) |
| 50 |
self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 51 |
self.blue_font = app.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true }) |
| 52 |
self.title_text = _S.buy_objects_window.choose_items |
| 53 |
self.price_text = (_S.buy_objects_window.price .. " "):gsub(" $", " ") |
| 54 |
self.total_text = (_S.buy_objects_window.total .. " "):gsub(" $", " ") |
| 55 |
self.item_price = 0 |
| 56 |
self.total_price = 0 |
| 57 |
|
| 58 |
self.list_hover_index = 0 |
| 59 |
self.hover_sound = nil |
| 60 |
self.preview_anim = TH.animation() |
| 61 |
|
| 62 |
self.objects = { |
| 63 |
} |
| 64 |
if objects then |
| 65 |
for _, object in pairs(objects) do |
| 66 |
self.objects[#self.objects + 1] = {object = object.object, start_qty = object.qty, qty = object.qty, min_qty = object.min_qty} -- Had to make a copy of objects list. Otherwise, we will modify the original variable (Opening dialog twice keeps memory of previously chosen quantities) |
| 67 |
end |
| 68 |
else |
| 69 |
for _, object in ipairs(app.objects) do |
| 70 |
if object.corridor_object then |
| 71 |
self.objects[#self.objects + 1] = {object = object, start_qty = 0, qty = 0, min_qty = 0} |
| 72 |
end |
| 73 |
end |
| 74 |
table.sort(self.objects, function(o1, o2) |
| 75 |
return o1.object.corridor_object < o2.object.corridor_object |
| 76 |
end) |
| 77 |
end |
| 78 |
|
| 79 |
self:addPanel(228, 0, 0) -- Grid top |
| 80 |
for y = 33, 103, 10 do |
| 81 |
self:addPanel(229, 0, y) -- Grid body |
| 82 |
end |
| 83 |
self:addPanel(230, 0, 113) -- Grid bottom |
| 84 |
self:addPanel(231, 0, 148) -- Cost / total top |
| 85 |
self:addPanel(232, 0, 173) -- Cost / total body |
| 86 |
self:addPanel(233, 0, 215) -- Cost / total bottom |
| 87 |
self:addPanel(234, 0, 248) -- Close button background |
| 88 |
self:addPanel(234, 0, 252) -- Close button background extension |
| 89 |
self:addPanel(242, 9, 237):makeButton(0, 0, 129, 28, 243, self.close):setTooltip(_S.tooltip.buy_objects_window.cancel) |
| 90 |
|
| 91 |
self:addPanel(235, 146, 0) -- List top |
| 92 |
self:addPanel(236, 146, 223) -- List bottom |
| 93 |
self:addPanel(237, 154, 238):makeButton(0, 0, 197, 28, 238, self.confirm):setTooltip(_S.tooltip.buy_objects_window.confirm):setSound("selectx.wav") |
| 94 |
local i = 1 |
| 95 |
local function item_callback(index, qty) |
| 96 |
local is_negative_quantity = qty < 0 |
| 97 |
return --[[persistable:furnish_corridor_item_callback]] function(window) |
| 98 |
-- lewri: a persistence bug from the 'decrease' button converts integer to a |
| 99 |
-- float, for now, sanitise it back to an integer |
| 100 |
qty = math.floor(qty) |
| 101 |
local quantity, error = window:purchaseItem(index, qty) |
| 102 |
|
| 103 |
if quantity == nil and not is_negative_quantity then |
| 104 |
-- give visual warning that player doesn't have enough $ to buy or can't buy more |
| 105 |
window.ui.adviser:say(error, false, true) |
| 106 |
window.ui:playSound("wrong2.wav") |
| 107 |
elseif qty > 0 then |
| 108 |
window.ui:playSound("AddItemJ.wav") |
| 109 |
else |
| 110 |
window.ui:playSound("DelItemJ.wav") |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
for y = 34, 205, 19 do |
| 115 |
local x = 146 |
| 116 |
self:addPanel(239, x, y) -- List body |
| 117 |
if i <= #self.objects then |
| 118 |
self:addPanel(240, x + 12, y):makeButton(0, 0, 125, 19, 241, item_callback(i, 1), nil, item_callback(i, -1)):setTooltip(self.objects[i].object.tooltip) |
| 119 |
self:addPanel(244, x + 139, y + 1):makeRepeatButton(0, 0, 17, 17, 245, item_callback(i, -1)):setTooltip(_S.tooltip.buy_objects_window.decrease) |
| 120 |
self:addPanel(246, x + 183, y + 1):makeRepeatButton(0, 0, 17, 17, 247, item_callback(i, 1)):setTooltip(_S.tooltip.buy_objects_window.increase) |
| 121 |
end |
| 122 |
i = i + 1 |
| 123 |
end |
| 124 |
|
| 125 |
self:makeTooltip(_S.tooltip.buy_objects_window.price, 20, 168, 127, 187) |
| 126 |
self:makeTooltip(_S.tooltip.buy_objects_window.total_value, 20, 196, 127, 215) |
| 127 |
|
| 128 |
self:registerKeyHandlers() |
| 129 |
end |
| 130 |
|
| 131 |
function UIFurnishCorridor:registerKeyHandlers() |
| 132 |
self:addKeyHandler("global_confirm", self.confirm) |
| 133 |
self:addKeyHandler("global_confirm_alt", self.confirm) |
| 134 |
end |
| 135 |
|
| 136 |
function UIFurnishCorridor:purchaseItem(index, quantity) |
| 137 |
local o = self.objects[index] |
| 138 |
local is_negative_quantity = quantity < 0 |
| 139 |
|
| 140 |
if (quantity > 0 and o.qty >= 99) then |
| 141 |
return nil, _A.warnings.cannot_buy:format(o.object.name) |
| 142 |
end |
| 143 |
|
| 144 |
if self.ui.app.key_modifiers.ctrl then |
| 145 |
quantity = quantity * 10 |
| 146 |
elseif self.ui.app.key_modifiers.shift then |
| 147 |
quantity = quantity * 5 |
| 148 |
end |
| 149 |
quantity = quantity + o.qty |
| 150 |
if quantity < o.min_qty then |
| 151 |
quantity = o.min_qty |
| 152 |
elseif quantity > 99 then |
| 153 |
quantity = 99 |
| 154 |
end |
| 155 |
|
| 156 |
local bought = quantity - o.qty |
| 157 |
local hospital = self.ui.hospital |
| 158 |
local total_price = self.total_price + bought * hospital:getObjectBuildCost(o.object.id) |
| 159 |
|
| 160 |
if hospital.balance >= total_price or is_negative_quantity then |
| 161 |
o.qty = o.qty + bought |
| 162 |
self.total_price = total_price |
| 163 |
if o.object.id == "reception_desk" then |
| 164 |
if o.qty > 0 then |
| 165 |
self.ui:tutorialStep(1, 2, 3) |
| 166 |
else |
| 167 |
self.ui:tutorialStep(1, 3, 2) |
| 168 |
end |
| 169 |
end |
| 170 |
else |
| 171 |
return nil, _A.warnings.cannot_afford_2 |
| 172 |
end |
| 173 |
return quantity |
| 174 |
end |
| 175 |
|
| 176 |
function UIFurnishCorridor:confirm() |
| 177 |
self.ui:tutorialStep(1, 3, 4) |
| 178 |
|
| 179 |
local to_purchase = {} |
| 180 |
local to_sell = {} |
| 181 |
for _, o in ipairs(self.objects) do |
| 182 |
local build_cost = self.ui.hospital:getObjectBuildCost(o.object.id) |
| 183 |
if o.qty - o.start_qty > 0 then |
| 184 |
local diff_qty = o.qty - o.start_qty |
| 185 |
to_purchase[#to_purchase + 1] = { object = o.object, qty = diff_qty } |
| 186 |
self.ui.hospital:spendMoney(build_cost * diff_qty, _S.transactions.buy_object .. ": " .. o.object.name, build_cost * diff_qty) |
| 187 |
elseif o.qty - o.start_qty < 0 then |
| 188 |
local diff_qty = o.start_qty - o.qty |
| 189 |
to_sell[#to_sell + 1] = { object = o.object, qty = diff_qty } |
| 190 |
self.ui.hospital:receiveMoney(build_cost * diff_qty, _S.transactions.sell_object .. ": " .. o.object.name, build_cost * diff_qty) |
| 191 |
end |
| 192 |
end |
| 193 |
|
| 194 |
if self.edit_dialog then |
| 195 |
self.edit_dialog:addObjects(to_purchase, false) -- payment already handled here |
| 196 |
self.edit_dialog:removeObjects(to_sell) -- payment already handled here |
| 197 |
self:close() |
| 198 |
else |
| 199 |
if #to_purchase == 0 then |
| 200 |
self:close() |
| 201 |
else |
| 202 |
self.ui:addWindow(UIPlaceObjects(self.ui, to_purchase)) |
| 203 |
end |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
function UIFurnishCorridor:close() |
| 208 |
self.ui:tutorialStep(1, {2, 3}, 1) |
| 209 |
if self.edit_dialog then |
| 210 |
self.edit_dialog:addObjects() -- No objects added. Call the function anyway to handle visibility etc. |
| 211 |
end |
| 212 |
Window.close(self) |
| 213 |
end |
| 214 |
|
| 215 |
function UIFurnishCorridor:draw(canvas, x, y) |
| 216 |
Window.draw(self, canvas, x, y) |
| 217 |
|
| 218 |
local s = TheApp.config.ui_scale |
| 219 |
x, y = x + self.x * s, y + self.y * s |
| 220 |
self.white_font:draw(canvas, self.title_text, x + 163 * s, y + 18 * s) |
| 221 |
self.white_font:draw(canvas, self.price_text .. self.item_price, x + 24 * s, y + 173 * s) |
| 222 |
self.white_font:draw(canvas, self.total_text .. self.total_price, x + 24 * s, y + 202 * s) |
| 223 |
|
| 224 |
for i, o in ipairs(self.objects) do |
| 225 |
local font = self.white_font |
| 226 |
if i == self.list_hover_index then |
| 227 |
font = self.blue_font |
| 228 |
end |
| 229 |
font:draw(canvas, o.object.name, x + 163 * s, y + 20 * s + i * 19 * s) |
| 230 |
font:draw(canvas, o.qty, x + 306 * s, y + 20 * s + i * 19 * s, 19 * s, 0) |
| 231 |
end |
| 232 |
|
| 233 |
self.preview_anim:setScaleFactor(s) |
| 234 |
self.preview_anim:draw(canvas, x + 72 * s, y + 57 * s) |
| 235 |
end |
| 236 |
|
| 237 |
function UIFurnishCorridor:onMouseMove(x, y, dx, dy) |
| 238 |
local repaint = Window.onMouseMove(self, x, y, dx, dy) |
| 239 |
|
| 240 |
local s = TheApp.config.ui_scale |
| 241 |
local hover_idx = 0 |
| 242 |
if 158 * s <= x and x < 346 * s and 34 * s <= y and y < 224 * s then |
| 243 |
hover_idx = math_floor((y - 15 * s) / (19 * s)) |
| 244 |
end |
| 245 |
|
| 246 |
if hover_idx ~= self.list_hover_index then |
| 247 |
if 1 <= hover_idx and hover_idx <= #self.objects then |
| 248 |
local obj = self.objects[hover_idx].object |
| 249 |
self.item_price = self.ui.hospital:getObjectBuildCost(obj.id) |
| 250 |
self.preview_anim:setAnimation(self.anims, obj.build_preview_animation) |
| 251 |
if self.hover_sound then |
| 252 |
self.ui:stopSound(self.hover_sound) |
| 253 |
end |
| 254 |
self.hover_sound = self.ui:playSound("HLight5.wav") |
| 255 |
end |
| 256 |
self.list_hover_index = hover_idx |
| 257 |
repaint = true |
| 258 |
end |
| 259 |
|
| 260 |
return repaint |
| 261 |
end |
| 262 |
|
| 263 |
function UIFurnishCorridor:afterLoad(old, new) |
| 264 |
-- If we saved with the window open, quantities might get converted to floats |
| 265 |
-- This may be overkill |
| 266 |
for _, obj in pairs(self.objects) do |
| 267 |
obj.qty = math.floor(obj.qty) |
| 268 |
end |
| 269 |
|
| 270 |
if old < 236 then |
| 271 |
local selected_label_color = { red = 40, green = 40, blue = 250 } |
| 272 |
self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 273 |
self.blue_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true }) |
| 274 |
end |
| 275 |
|
| 276 |
Window.afterLoad(self, old, new) |
| 277 |
self:registerKeyHandlers() |
| 278 |
end |