| 1 |
--[[ Copyright (c) 2025 Matthew "Matroftt" |
| 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 |
--! Interactive Machine List |
| 22 |
class "UIMachineMenu" (UIResizable) |
| 23 |
|
| 24 |
---@type UIMachineMenu |
| 25 |
local UIMachineMenu = _G["UIMachineMenu"] |
| 26 |
|
| 27 |
local col_bg = { red = 219, green = 81, blue = 12, } |
| 28 |
local col_shadow = { red = 202, green = 69, blue = 8, } |
| 29 |
local col_highlight = { red = 232, green = 95, blue = 16 } |
| 30 |
local col_assigned_button = { red = 84, green = 200, blue = 84 } |
| 31 |
local col_shaded_assigned_button = { red = 92, green = 150, blue = 92 } |
| 32 |
local col_disabled_assigned_button = { red = 84, green = 112, blue = 84 } |
| 33 |
local col_warning_button = { red = 219, green = 36, blue = 36 } |
| 34 |
local col_disabled = { red = 159, green = 90, blue = 56 } |
| 35 |
|
| 36 |
-- Timer, used to update machine menu only once per X ticks to reduce the burden. |
| 37 |
local ticks_to_skip = 10 |
| 38 |
local tick_timer = ticks_to_skip |
| 39 |
|
| 40 |
local row_height = 32 |
| 41 |
local window_margin = 15 |
| 42 |
|
| 43 |
function UIMachineMenu:UIMachineMenu(ui) |
| 44 |
local app = ui.app |
| 45 |
|
| 46 |
-- Calculate menu's height |
| 47 |
local height_divisor = 4 |
| 48 |
local approx_rows_space = math.floor(app.config.height / height_divisor) |
| 49 |
self.rows = math.ceil(approx_rows_space / row_height) |
| 50 |
local rows_space = self.rows * row_height |
| 51 |
|
| 52 |
local width = 480 |
| 53 |
local height = row_height + rows_space + window_margin * 2 -- header + values + margin (top and bottom) |
| 54 |
self:UIResizable(ui, width, height, col_bg) |
| 55 |
self.ui = ui |
| 56 |
self.modal_class = "machine_menu" |
| 57 |
|
| 58 |
self.esc_closes = true |
| 59 |
self.resizable = false |
| 60 |
self:setDefaultPosition(0.05, 0.05) |
| 61 |
|
| 62 |
self.machine_list = {} |
| 63 |
self.list_table = {} |
| 64 |
|
| 65 |
self.default_button_sound = "selectx.wav" |
| 66 |
|
| 67 |
self.sort_method = "sortByRatio" |
| 68 |
self.rows_shown = 0 |
| 69 |
self:createControls() |
| 70 |
|
| 71 |
self:update() |
| 72 |
end |
| 73 |
|
| 74 |
function UIMachineMenu:createControls() |
| 75 |
local rows = self.rows |
| 76 |
|
| 77 |
self.panel_sprites = self.ui.app.gfx:loadSpriteTable("QData", "Req03V", true) |
| 78 |
self.white_font = self.ui.app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 79 |
|
| 80 |
if rows ~= self.rows_shown then |
| 81 |
local function assigned_factory(num) |
| 82 |
return --[[persistable:machine_menu_assigned_button]] function(window) |
| 83 |
window:assignedHandymanButtonClicked(num) |
| 84 |
end |
| 85 |
end |
| 86 |
local function warning_factory(num) |
| 87 |
return --[[persistable:machine_menu_warning_button]] function(window) |
| 88 |
window:warningButtonClicked(num) |
| 89 |
end |
| 90 |
end |
| 91 |
local function name_factory(num) |
| 92 |
return --[[persistable:machine_menu_name_button]] function(window) |
| 93 |
window:machineButtonClicked(num) |
| 94 |
end |
| 95 |
end |
| 96 |
local scrollbarMovedCallback = --[[persistable:machine_menu_scrollbar]] function() |
| 97 |
self:scrollbarMoved() |
| 98 |
end |
| 99 |
|
| 100 |
self.rows_shown = rows |
| 101 |
self.list_table = {} |
| 102 |
local indicator_width = 32 |
| 103 |
local name_width = 180 |
| 104 |
local status_width = 112 |
| 105 |
local x = window_margin |
| 106 |
local y = window_margin |
| 107 |
|
| 108 |
|
| 109 |
-- Draw headers |
| 110 |
local warning_header = self:addBevelPanel(x, window_margin, indicator_width, row_height, col_highlight) |
| 111 |
:setLabel("!"):setTooltip(_S.tooltip.machine_menu.header.smoking) |
| 112 |
x = x + indicator_width |
| 113 |
local assigned_header = self:addBevelPanel(x, window_margin, indicator_width, row_height, col_highlight) |
| 114 |
:setLabel("X"):setTooltip(_S.tooltip.machine_menu.header.assigned) |
| 115 |
x = x + indicator_width |
| 116 |
local name_header = self:addBevelPanel(x, window_margin, name_width, row_height, col_highlight) |
| 117 |
:setLabel(_S.machine_menu.machine) |
| 118 |
:makeButton(0, 0, name_width, row_height, nil, self.setSortByName) |
| 119 |
:setTooltip(_S.tooltip.machine_menu.header.machine) |
| 120 |
x = x + name_width |
| 121 |
local status_panel_header = self:addBevelPanel(x, window_margin, status_width, row_height, |
| 122 |
col_highlight) |
| 123 |
:setLabel(_S.machine_menu.status) |
| 124 |
:makeButton(0, 0, status_width, row_height, nil, self.setSortByRatio) |
| 125 |
:setTooltip(_S.tooltip.machine_menu.header.status .. " " .. _S.tooltip.machine_menu.sort) |
| 126 |
x = x + status_width |
| 127 |
local remaining_strength_panel_header = self:addBevelPanel(x, window_margin, indicator_width, row_height, col_highlight) |
| 128 |
:makeButton(0, 0, indicator_width, row_height, nil, self.setSortByRemain) |
| 129 |
:setTooltip(_S.tooltip.machine_menu.header.remaining_strength .. " " .. _S.tooltip.machine_menu.sort) |
| 130 |
x = x + indicator_width |
| 131 |
local strength_panel_header = self:addBevelPanel(x, window_margin, indicator_width, row_height, col_highlight) |
| 132 |
:makeButton(0, 0, indicator_width, row_height, nil, self.setSortByStrength) |
| 133 |
:setTooltip(_S.tooltip.machine_menu.header.total_strength .. " " .. _S.tooltip.machine_menu.sort) |
| 134 |
|
| 135 |
-- Draw rows |
| 136 |
for i = 1, rows, 1 do |
| 137 |
x = window_margin |
| 138 |
local warning_panel = self:addBevelPanel(x, y + row_height, indicator_width, row_height, col_warning_button) |
| 139 |
local warning_button = warning_panel:makeButton(0, 0, indicator_width, row_height, nil, warning_factory(i)) |
| 140 |
:setTooltip(_S.tooltip.machine_menu.smoking) |
| 141 |
x = x + indicator_width |
| 142 |
local assigned_panel = self:addBevelPanel(x, y + row_height, indicator_width, row_height, col_assigned_button) |
| 143 |
local assigned_button = assigned_panel:makeButton(0, 0, indicator_width, row_height, nil, assigned_factory(i)) |
| 144 |
:setTooltip(_S.tooltip.machine_menu.assigned) |
| 145 |
x = x + indicator_width |
| 146 |
local name_panel = self:addBevelPanel(x, y + row_height, 180, row_height, col_bg) |
| 147 |
local name_button = name_panel:makeButton(0, 0, 200, row_height, nil, name_factory(i)) |
| 148 |
:setTooltip(_S.tooltip.machine_menu.machine) |
| 149 |
x = x + name_width |
| 150 |
local status_panel = self:addBevelPanel(x, y + row_height, status_width, row_height, col_bg) |
| 151 |
:setTooltip(_S.tooltip.machine_window.status) |
| 152 |
x = x + status_width |
| 153 |
local remaining_strength_panel = self:addBevelPanel(x, y + row_height, indicator_width, row_height, col_bg) |
| 154 |
:setTooltip(_S.tooltip.machine_menu.remaining_strength) |
| 155 |
x = x + indicator_width |
| 156 |
local strength_panel = self:addBevelPanel(x, y + row_height, indicator_width, row_height, col_bg) |
| 157 |
:setTooltip(_S.tooltip.machine_menu.total_strength) |
| 158 |
|
| 159 |
table.insert(self.list_table, { |
| 160 |
warning_header = warning_header, |
| 161 |
assigned_header = assigned_header, |
| 162 |
name_header = name_header, |
| 163 |
status_panel_header = status_panel_header, |
| 164 |
remaining_strength_panel_header = remaining_strength_panel_header, |
| 165 |
strength_panel_header = strength_panel_header, |
| 166 |
assigned_panel = assigned_panel, |
| 167 |
assigned_button = assigned_button, |
| 168 |
warning_panel = warning_panel, |
| 169 |
warning_button = warning_button, |
| 170 |
name_panel = name_panel, |
| 171 |
name_button = name_button, |
| 172 |
status_panel = status_panel, |
| 173 |
remaining_strength_panel = remaining_strength_panel, |
| 174 |
strength_panel = strength_panel, |
| 175 |
}) |
| 176 |
y = y + row_height |
| 177 |
end |
| 178 |
|
| 179 |
-- Add scrollbar |
| 180 |
self.scrollbar = self:addColourPanel(446, window_margin + 30, 24, row_height * rows, |
| 181 |
col_shadow.red, col_shadow.green, col_shadow.blue) |
| 182 |
:makeScrollbar(col_bg, scrollbarMovedCallback, 1, 1, 10, 1) |
| 183 |
-- Add close button |
| 184 |
self:addPanel(337, 446, 15):makeButton(0, 0, 24, 24, 338, self.close) |
| 185 |
:setTooltip(_S.tooltip.machine_menu.close) |
| 186 |
end |
| 187 |
end |
| 188 |
|
| 189 |
function UIMachineMenu:scrollToEntity(entity) |
| 190 |
local x, y = self.ui.app.map:WorldToScreen(entity.tile_x, entity.tile_y) |
| 191 |
self.ui:scrollMapTo(x, y) |
| 192 |
self.ui:addWindow(UIMachine(self.ui, entity, entity:getRoom())) |
| 193 |
end |
| 194 |
|
| 195 |
function UIMachineMenu:machineButtonClicked(index) |
| 196 |
local machine_index = index + self.scrollbar.value - 1 |
| 197 |
local machine = self.machine_list[machine_index] |
| 198 |
if machine and machine.object then |
| 199 |
if class.is(machine.object, Room) then |
| 200 |
self:scrollToRoom(machine.object) |
| 201 |
elseif class.is(machine.object, Entity) then |
| 202 |
self:scrollToEntity(machine.object) |
| 203 |
end |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
function UIMachineMenu:warningButtonClicked(index) |
| 208 |
local ui = self.ui |
| 209 |
local machine_index = index + self.scrollbar.value - 1 |
| 210 |
local machine = self.machine_list[machine_index] |
| 211 |
local room = machine.object:getRoom() |
| 212 |
|
| 213 |
if machine and room.is_active then |
| 214 |
local UIMachine = UIMachine(ui, machine.object, room) |
| 215 |
UIMachine:replaceMachine() |
| 216 |
ui:addWindow(UIMachine) |
| 217 |
end |
| 218 |
end |
| 219 |
|
| 220 |
function UIMachineMenu:assignedHandymanButtonClicked(index) |
| 221 |
local ui = self.ui |
| 222 |
local machine_index = index + self.scrollbar.value - 1 |
| 223 |
local machine = self.machine_list[machine_index] |
| 224 |
local room = machine.object:getRoom() |
| 225 |
|
| 226 |
if machine and machine.assigned_to and room.is_active then |
| 227 |
self.ui:addWindow(UIStaff(ui, machine.assigned_to)) |
| 228 |
end |
| 229 |
end |
| 230 |
|
| 231 |
function UIMachineMenu:setSortByName() |
| 232 |
self.sort_method = "sortByName" |
| 233 |
end |
| 234 |
|
| 235 |
function UIMachineMenu:setSortByRemain() |
| 236 |
self.sort_method = "sortByRemain" |
| 237 |
end |
| 238 |
|
| 239 |
function UIMachineMenu:setSortByRatio() |
| 240 |
self.sort_method = "sortByRatio" |
| 241 |
end |
| 242 |
|
| 243 |
function UIMachineMenu:setSortByStrength() |
| 244 |
self.sort_method = "sortByStrength" |
| 245 |
end |
| 246 |
|
| 247 |
function UIMachineMenu:sortMachines(method) |
| 248 |
if method == "sortByRemain" then |
| 249 |
table.sort(self.machine_list, |
| 250 |
function(a, b) |
| 251 |
if a.remaining_strength == nil or b.remaining_strength == nil then return false end |
| 252 |
return a.remaining_strength < b.remaining_strength |
| 253 |
end) |
| 254 |
elseif method == "sortByStrength" then |
| 255 |
table.sort(self.machine_list, |
| 256 |
function(a, b) |
| 257 |
if a.strength == nil or b.strength == nil then return false end |
| 258 |
return a.strength < b.strength |
| 259 |
end) |
| 260 |
elseif method == "sortByRatio" then |
| 261 |
table.sort(self.machine_list, |
| 262 |
function(a, b) |
| 263 |
if a.percentage_strength == nil or b.percentage_strength == nil then return false end |
| 264 |
|
| 265 |
if a.smoking and not b.smoking then |
| 266 |
return true |
| 267 |
elseif not a.smoking and b.smoking then |
| 268 |
return false |
| 269 |
end |
| 270 |
|
| 271 |
if (a.smoking or b.smoking) and (a.remaining_strength ~= b.remaining_strength) then |
| 272 |
return a.remaining_strength < b.remaining_strength |
| 273 |
end |
| 274 |
|
| 275 |
return a.percentage_strength < b.percentage_strength |
| 276 |
end) |
| 277 |
elseif method == "sortByName" then |
| 278 |
table.sort(self.machine_list, |
| 279 |
function(a, b) |
| 280 |
return a.object.object_type.name < b.object.object_type.name |
| 281 |
end) |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
function UIMachineMenu:update() |
| 286 |
local function machineForList(machine, assigned_handyman) |
| 287 |
local remaining_uses_count = machine:getRemainingUses() |
| 288 |
local percentage_strength = math.floor((remaining_uses_count / machine.strength) * 100) |
| 289 |
local is_assigned = assigned_handyman ~= nil |
| 290 |
local result = { |
| 291 |
object = machine, |
| 292 |
smoking = machine:isBreaking(), |
| 293 |
assigned = is_assigned, |
| 294 |
assigned_to = assigned_handyman, |
| 295 |
name = machine.object_type.name, |
| 296 |
strength = machine.strength, |
| 297 |
remaining_strength = remaining_uses_count, |
| 298 |
percentage_strength = percentage_strength, |
| 299 |
total_usage = machine.total_usage |
| 300 |
} |
| 301 |
return result |
| 302 |
end |
| 303 |
|
| 304 |
self.machine_list = {} |
| 305 |
local world = self.ui.app.world |
| 306 |
local dispatcher = world.dispatcher |
| 307 |
local machines = world:getPlayerMachines() |
| 308 |
|
| 309 |
for _, machine in ipairs(machines) do |
| 310 |
if not machine:getRoom().crashed then |
| 311 |
local assigned_handyman |
| 312 |
local repair_call = dispatcher.call_queue[machine] |
| 313 |
if repair_call then |
| 314 |
local repair = repair_call["repair"] |
| 315 |
if repair and repair.assigned then |
| 316 |
assigned_handyman = repair.assigned |
| 317 |
end |
| 318 |
end |
| 319 |
|
| 320 |
local machine_for_list = machineForList(machine, assigned_handyman) |
| 321 |
table.insert(self.machine_list, machine_for_list) |
| 322 |
end |
| 323 |
end |
| 324 |
|
| 325 |
self:sortMachines(self.sort_method) |
| 326 |
|
| 327 |
self.scrollbar:setRange(1, math.max(1, #self.machine_list), self.rows_shown, self.scrollbar.value) |
| 328 |
self:scrollbarMoved() |
| 329 |
end |
| 330 |
|
| 331 |
function UIMachineMenu:draw(canvas, x, y) |
| 332 |
UIResizable.draw(self, canvas, x, y) |
| 333 |
local panel_sprites = self.ui.app.gfx:loadSpriteTable("QData", "Req03V", true) |
| 334 |
local bitmap_sprites = self.ui.app.gfx:loadSpriteTable("Bitmap", "aux_ui", true) |
| 335 |
local scroll_pos = self.scrollbar.value |
| 336 |
local s = TheApp.config.ui_scale |
| 337 |
x, y = self.x * s, self.y * s |
| 338 |
bitmap_sprites:draw(canvas, 25, x + 374 * s, y + (window_margin + 2) * s, { scaleFactor = s }) |
| 339 |
bitmap_sprites:draw(canvas, 26, x + 407 * s, y + (window_margin + 2) * s, { scaleFactor = s }) |
| 340 |
|
| 341 |
for i = 1, self.rows_shown, 1 do |
| 342 |
local machine = self.machine_list[i + scroll_pos - 1] |
| 343 |
if machine then |
| 344 |
bitmap_sprites:draw(canvas, 24, x + 264 * s, y + (window_margin + row_height * i + 3) * s, { scaleFactor = s }) |
| 345 |
local status_bar_width = math.floor((1 - machine.object.times_used/machine.object.strength) * 40 * s + 0.5) |
| 346 |
if status_bar_width ~= 0 then |
| 347 |
for dx = 0, status_bar_width - 1 do |
| 348 |
panel_sprites:draw(canvas, 352, x + 293 * s + dx, y + (window_margin + row_height * i + math.floor(row_height/2) - 2) * s, { scaleFactor = s }) |
| 349 |
end |
| 350 |
end |
| 351 |
end |
| 352 |
end |
| 353 |
end |
| 354 |
|
| 355 |
function UIMachineMenu:scrollbarMoved() |
| 356 |
local scroll_pos = self.scrollbar.value |
| 357 |
for i = 1, self.rows_shown, 1 do |
| 358 |
local machine = self.machine_list[i + scroll_pos - 1] |
| 359 |
local row = self.list_table[i] |
| 360 |
if machine then |
| 361 |
row.assigned_panel:setColour(machine.assigned and col_assigned_button or col_shaded_assigned_button) |
| 362 |
row.assigned_panel:setLabel(machine.assigned and "X" or "") |
| 363 |
row.assigned_button:enable(machine.assigned and true or false) |
| 364 |
row.warning_panel:setLabel(machine.smoking and "!" or "") |
| 365 |
row.warning_panel:setColour(machine.smoking and col_warning_button or col_bg) |
| 366 |
row.warning_button:enable(true) |
| 367 |
row.name_panel:setLabel(" " .. machine.name, nil, "left") |
| 368 |
row.name_panel:setColour(col_bg) |
| 369 |
row.name_button:enable(true) |
| 370 |
row.status_panel:setColour(col_bg) |
| 371 |
row.remaining_strength_panel:setLabel(tostring(machine.remaining_strength)) |
| 372 |
row.remaining_strength_panel:setColour(col_bg) |
| 373 |
row.strength_panel:setLabel(tostring(machine.strength)) |
| 374 |
row.strength_panel:setColour(col_bg) |
| 375 |
else |
| 376 |
row.warning_panel:setColour(col_disabled) |
| 377 |
row.assigned_panel:setColour(col_disabled_assigned_button) |
| 378 |
row.assigned_panel:setLabel("") |
| 379 |
row.name_panel:setLabel("") |
| 380 |
row.assigned_button:enable(false) |
| 381 |
row.warning_button:enable(false) |
| 382 |
row.name_button:enable(false) |
| 383 |
row.status_panel:setColour(col_disabled) |
| 384 |
row.remaining_strength_panel:setColour(col_disabled) |
| 385 |
row.name_panel:setColour(col_disabled) |
| 386 |
row.strength_panel:setColour(col_disabled) |
| 387 |
end |
| 388 |
end |
| 389 |
end |
| 390 |
|
| 391 |
function UIMachineMenu:onTick() |
| 392 |
-- menu updates not every tick |
| 393 |
tick_timer = tick_timer - 1 |
| 394 |
if tick_timer <= 0 then |
| 395 |
tick_timer = ticks_to_skip |
| 396 |
self:update() |
| 397 |
end |
| 398 |
end |
| 399 |
|
| 400 |
function UIMachineMenu:close() |
| 401 |
Window.close(self) |
| 402 |
end |
| 403 |
|
| 404 |
function UIMachineMenu:afterLoad(old, new) |
| 405 |
if old < 236 then |
| 406 |
self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 407 |
end |
| 408 |
if old < 242 then |
| 409 |
self:close() |
| 410 |
end |
| 411 |
Window.afterLoad(self, old, new) |
| 412 |
end |