| 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 ipairs, math_floor, unpack, select, assert |
| 22 |
= ipairs, math.floor, unpack, select, assert |
| 23 |
local TH = require("TH") |
| 24 |
|
| 25 |
--! The ingame menu bar which sits (nominally hidden) at the top of the screen. |
| 26 |
class "UIMenuBar" (Window) |
| 27 |
|
| 28 |
---@type UIMenuBar |
| 29 |
local UIMenuBar = _G["UIMenuBar"] |
| 30 |
|
| 31 |
function UIMenuBar:UIMenuBar(ui, map_editor) |
| 32 |
self:Window() |
| 33 |
|
| 34 |
local app = ui.app |
| 35 |
self.ui = ui |
| 36 |
self.on_top = true |
| 37 |
self.x = 0 |
| 38 |
self.y = 0 |
| 39 |
self.width = app.config.width |
| 40 |
self.height = 16 |
| 41 |
self.visible = false |
| 42 |
local selected_label_color = { red = 40, green = 40, blue = 250 } |
| 43 |
self.panel_sprites = app.gfx:loadSpriteTable("Data", "PullDV", true) |
| 44 |
self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 45 |
self.blue_font = app.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true }) |
| 46 |
-- The list of top-level menus, from left to right |
| 47 |
self.menus = {} |
| 48 |
-- The menu which the cursor was most recently over |
| 49 |
-- This should be present in self.open_menus, else it won't be drawn |
| 50 |
self.active_menu = false |
| 51 |
-- The list of menus which should be displayed |
| 52 |
-- This list satisfies: open_menus[x] == nil or open_menus[x].level == x |
| 53 |
self.open_menus = {} |
| 54 |
|
| 55 |
if map_editor then |
| 56 |
self:makeMapeditorMenu(app) |
| 57 |
else |
| 58 |
self:makeGameMenu(app) |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
function UIMenuBar:onTick() |
| 63 |
if #self.open_menus > 0 then |
| 64 |
-- If the deepest menu has no need to be open, close it after a short time |
| 65 |
-- It needs to be open if the cursor is over it, or the cursor is over the |
| 66 |
-- item in its parent corresponding to it. |
| 67 |
local deepest = self.open_menus[#self.open_menus] |
| 68 |
local parent = deepest.parent |
| 69 |
if deepest == self.active_menu or |
| 70 |
(parent and parent == self.active_menu and parent.items[parent.hover_index] and |
| 71 |
parent.items[parent.hover_index].submenu == deepest) then |
| 72 |
self.menu_disappear_counter = nil |
| 73 |
else |
| 74 |
if self.menu_disappear_counter == 0 then |
| 75 |
self.ui.app:saveConfig() |
| 76 |
self.menu_disappear_counter = nil |
| 77 |
local close_to = self.active_menu and self.active_menu.level or 0 |
| 78 |
for i = #self.open_menus, close_to + 1, -1 do |
| 79 |
self.open_menus[i] = nil |
| 80 |
end |
| 81 |
else |
| 82 |
self.menu_disappear_counter = (self.menu_disappear_counter or 26) - 1 |
| 83 |
end |
| 84 |
end |
| 85 |
end |
| 86 |
if self.disappear_counter then |
| 87 |
if self.disappear_counter == 0 then |
| 88 |
for i = #self.open_menus, 1, -1 do |
| 89 |
self.open_menus[i] = nil |
| 90 |
end |
| 91 |
self.active_menu = false |
| 92 |
self.visible = false |
| 93 |
self.disappear_counter = nil |
| 94 |
if self.on_top then |
| 95 |
self.ui:sendToBottom(self) |
| 96 |
self.on_top = false |
| 97 |
end |
| 98 |
else |
| 99 |
self.disappear_counter = self.disappear_counter - 1 |
| 100 |
end |
| 101 |
end |
| 102 |
Window.onTick(self) |
| 103 |
end |
| 104 |
|
| 105 |
function UIMenuBar:onChangeResolution() |
| 106 |
self.width = self.ui.app.config.width |
| 107 |
end |
| 108 |
|
| 109 |
function UIMenuBar:onChangeLanguage() |
| 110 |
local function check_size(menu) |
| 111 |
menu.has_size = false |
| 112 |
for _, item in ipairs(menu.items) do |
| 113 |
if item.submenu then |
| 114 |
check_size(item.submenu) |
| 115 |
end |
| 116 |
end |
| 117 |
end |
| 118 |
for _, menu in ipairs(self.menus) do |
| 119 |
check_size(menu.menu) |
| 120 |
end |
| 121 |
end |
| 122 |
|
| 123 |
local function assign_menu_levels(menu, level) |
| 124 |
menu.level = level |
| 125 |
for _, item in ipairs(menu.items) do |
| 126 |
if item.submenu then |
| 127 |
assign_menu_levels(item.submenu, level + 1) |
| 128 |
end |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
--! Add a menu to the menu bar. |
| 133 |
--!param title Title of the menu (at the bar). |
| 134 |
--!param menu Menu to add. |
| 135 |
function UIMenuBar:addMenu(title, menu) |
| 136 |
assign_menu_levels(menu, 1) |
| 137 |
local menu_item = { |
| 138 |
title = title, |
| 139 |
menu = menu, |
| 140 |
x = 0, |
| 141 |
y = 1, |
| 142 |
height = 16, |
| 143 |
} |
| 144 |
if self.menus[1] then |
| 145 |
menu_item.x = self.menus[#self.menus].x + self.menus[#self.menus].width |
| 146 |
end |
| 147 |
menu_item.width = math.ceil(self.white_font:sizeOf(title) / TheApp.config.ui_scale + 32) |
| 148 |
self.menus[#self.menus + 1] = menu_item |
| 149 |
end |
| 150 |
|
| 151 |
function UIMenuBar:draw(canvas) |
| 152 |
if not self.visible then |
| 153 |
return |
| 154 |
end |
| 155 |
local s = TheApp.config.ui_scale |
| 156 |
local panel_sprites = self.panel_sprites |
| 157 |
local panel_sprites_draw = panel_sprites.draw |
| 158 |
canvas:nonOverlapping() |
| 159 |
panel_sprites_draw(panel_sprites, canvas, 1, 0, 0, { scaleFactor = s }) |
| 160 |
panel_sprites_draw(panel_sprites, canvas, 4, 0, 6 * s, { scaleFactor = s }) |
| 161 |
panel_sprites_draw(panel_sprites, canvas, 7, 0, 10 * s, { scaleFactor = s }) |
| 162 |
|
| 163 |
-- Currently the width is the full pixel width of the window, unlike |
| 164 |
-- the height and other panels which are in scale units. |
| 165 |
local scaled_width = self.width |
| 166 |
for x = 10 * s, scaled_width - 10 * s, 10 * s do |
| 167 |
panel_sprites_draw(panel_sprites, canvas, 2, x, 0, { scaleFactor = s }) |
| 168 |
end |
| 169 |
for x = 10 * s, scaled_width - 10 * s, 10 * s do |
| 170 |
panel_sprites_draw(panel_sprites, canvas, 5, x, 6 * s, { scaleFactor = s }) |
| 171 |
end |
| 172 |
for x = 10 * s, scaled_width - 10 * s, 10 * s do |
| 173 |
panel_sprites_draw(panel_sprites, canvas, 8, x, 10 * s, { scaleFactor = s }) |
| 174 |
end |
| 175 |
canvas:nonOverlapping(false) |
| 176 |
local x = scaled_width - 10 * s |
| 177 |
panel_sprites_draw(panel_sprites, canvas, 3, x, 0, { scaleFactor = s }) |
| 178 |
panel_sprites_draw(panel_sprites, canvas, 6, x, 6 * s, { scaleFactor = s }) |
| 179 |
panel_sprites_draw(panel_sprites, canvas, 9, x, 10 * s, { scaleFactor = s }) |
| 180 |
|
| 181 |
for _, menu in ipairs(self.menus) do |
| 182 |
self.white_font:draw(canvas, menu.title, menu.x * s, menu.y * s, 0, menu.height * s) |
| 183 |
end |
| 184 |
for _, menu in ipairs(self.open_menus) do |
| 185 |
self:drawMenu(menu, canvas) |
| 186 |
end |
| 187 |
|
| 188 |
-- Draw clock |
| 189 |
if self.ui.app.config.twentyfour_hour_clock then |
| 190 |
self.white_font:draw(canvas, os.date("%H:%M"), scaled_width - 45 * s, s, 0, 16 * s) |
| 191 |
else |
| 192 |
self.white_font:draw(canvas, os.date("%I:%M %p"), scaled_width - 65 * s, s, 0, 16 * s) |
| 193 |
end |
| 194 |
end |
| 195 |
|
| 196 |
function UIMenuBar:drawMenu(menu, canvas) |
| 197 |
local s = TheApp.config.ui_scale |
| 198 |
local panel_sprites = self.panel_sprites |
| 199 |
local panel_sprites_draw = panel_sprites.draw |
| 200 |
local x, y, w, h = menu.x * s, menu.y * s, menu.width * s, menu.height * s |
| 201 |
|
| 202 |
menu.render_list:setScaleFactor(s) |
| 203 |
menu.render_list:draw(canvas, x, y) |
| 204 |
|
| 205 |
local btmy = y + h - 6 * s |
| 206 |
panel_sprites_draw(panel_sprites, canvas, 3, x + w - 10 * s, y, { scaleFactor = s }) |
| 207 |
for ypos = y + 6 * s, y + h - 6 * s, 4 * s do |
| 208 |
panel_sprites_draw(panel_sprites, canvas, 6, x + w - 10 * s, ypos, { scaleFactor = s }) |
| 209 |
end |
| 210 |
panel_sprites_draw(panel_sprites, canvas, 9, x + w - 10 * s, btmy, { scaleFactor = s }) |
| 211 |
|
| 212 |
x = menu.x * s |
| 213 |
y = menu.y * s + 4 * s |
| 214 |
for i, item in ipairs(menu.items) do |
| 215 |
-- Update the checkbox status if necessary before drawing |
| 216 |
if item.is_check_item and item.condition then |
| 217 |
item.checked = item.condition() |
| 218 |
end |
| 219 |
|
| 220 |
local font = self.white_font |
| 221 |
if i == menu.hover_index then |
| 222 |
font = self.blue_font |
| 223 |
end |
| 224 |
font:draw(canvas, item.title, x, y) |
| 225 |
if item.submenu then |
| 226 |
font:draw(canvas, "+", x + w - 10 * s, y) |
| 227 |
elseif item.checked then |
| 228 |
panel_sprites_draw(panel_sprites, canvas, 10, x, y, { scaleFactor = s }) |
| 229 |
end |
| 230 |
y = y + 14 * s |
| 231 |
end |
| 232 |
end |
| 233 |
|
| 234 |
function UIMenuBar:hitTestBar(x, y) |
| 235 |
local s = TheApp.config.ui_scale |
| 236 |
if y < 16 * s then |
| 237 |
for _, menu in ipairs(self.menus) do |
| 238 |
if menu.x * s <= x and x < menu.x * s + menu.width * s then |
| 239 |
local submenu = menu.menu |
| 240 |
submenu.x = menu.x |
| 241 |
submenu.y = menu.y + menu.height - 2 |
| 242 |
submenu.hover_index = 0 |
| 243 |
self:calculateMenuSize(submenu) |
| 244 |
return submenu |
| 245 |
end |
| 246 |
end |
| 247 |
end |
| 248 |
return false |
| 249 |
end |
| 250 |
|
| 251 |
function UIMenuBar:onMouseMove(x, y, dx, dy) |
| 252 |
local s = TheApp.config.ui_scale |
| 253 |
local padding = 6 |
| 254 |
local visible = y < self.height * s + padding * s |
| 255 |
local newactive = false |
| 256 |
if not self.active_menu then |
| 257 |
for i = #self.open_menus, 1, -1 do |
| 258 |
if self.open_menus[i]:hitTest(x, y, padding) then |
| 259 |
self.active_menu = self.open_menus[i] |
| 260 |
newactive = true |
| 261 |
break |
| 262 |
end |
| 263 |
end |
| 264 |
end |
| 265 |
if self.active_menu then |
| 266 |
local menu = self.active_menu |
| 267 |
while true do |
| 268 |
local hit = menu:hitTest(x, y, padding) |
| 269 |
local toparent = true |
| 270 |
if hit then |
| 271 |
toparent = false |
| 272 |
visible = true |
| 273 |
if hit ~= true then |
| 274 |
if hit ~= menu.prev_hover_index then |
| 275 |
if menu.hover_sound then |
| 276 |
self.ui:stopSound(menu.hover_sound) |
| 277 |
end |
| 278 |
menu.hover_sound = self.ui:playSound("HLight5.wav") |
| 279 |
end |
| 280 |
menu.hover_index = hit |
| 281 |
menu.prev_hover_index = menu.hover_index |
| 282 |
else |
| 283 |
menu.hover_index = 0 |
| 284 |
if menu.parent and menu.parent:hitTest(x, y, 0) then |
| 285 |
toparent = true |
| 286 |
end |
| 287 |
end |
| 288 |
local child = menu.items[menu.hover_index] |
| 289 |
child = not toparent and child and child.submenu |
| 290 |
if child then |
| 291 |
child.x = menu.x + menu.width - 10 |
| 292 |
child.y = menu.y + menu.hover_index * 14 - 14 |
| 293 |
self:calculateMenuSize(child) |
| 294 |
self.open_menus[child.level] = child |
| 295 |
for i = #self.open_menus, child.level + 1, -1 do |
| 296 |
self.open_menus[i] = nil |
| 297 |
end |
| 298 |
if child:hitTest(x, y, 0) then |
| 299 |
menu.hover_index = 0 |
| 300 |
self.active_menu = child |
| 301 |
child.parent = menu |
| 302 |
menu = child |
| 303 |
newactive = true |
| 304 |
else |
| 305 |
break |
| 306 |
end |
| 307 |
elseif not toparent then |
| 308 |
break |
| 309 |
end |
| 310 |
end |
| 311 |
if toparent then |
| 312 |
newactive = true |
| 313 |
menu.hover_index = 0 |
| 314 |
menu = menu.parent |
| 315 |
if not menu then |
| 316 |
local bar_menu = self:hitTestBar(x, y) |
| 317 |
if bar_menu then |
| 318 |
self.open_menus = {bar_menu} |
| 319 |
end |
| 320 |
self.active_menu = bar_menu or (visible and self.active_menu) |
| 321 |
break |
| 322 |
end |
| 323 |
self.active_menu = menu |
| 324 |
end |
| 325 |
end |
| 326 |
elseif self.ui.down_count ~= 0 then |
| 327 |
local bar_menu = self:hitTestBar(x, y) |
| 328 |
if bar_menu then |
| 329 |
self.open_menus = {bar_menu} |
| 330 |
self.active_menu = bar_menu |
| 331 |
end |
| 332 |
end |
| 333 |
newactive = newactive or (visible and not self.visible) |
| 334 |
if visible then |
| 335 |
self:appear() |
| 336 |
else |
| 337 |
self:disappear() |
| 338 |
end |
| 339 |
return newactive |
| 340 |
end |
| 341 |
|
| 342 |
function UIMenuBar:appear() |
| 343 |
self.disappear_counter = nil |
| 344 |
self.visible = true |
| 345 |
if not self.on_top then |
| 346 |
self.ui:sendToTop(self) |
| 347 |
self.on_top = true |
| 348 |
end |
| 349 |
end |
| 350 |
|
| 351 |
function UIMenuBar:disappear() |
| 352 |
if not self.disappear_counter then |
| 353 |
self.disappear_counter = 100 |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
function UIMenuBar:onMouseDown(button, x, y) |
| 358 |
if button ~= "left" or not self.visible then |
| 359 |
return |
| 360 |
end |
| 361 |
local repaint = false |
| 362 |
while self.active_menu do |
| 363 |
local menu = self.active_menu |
| 364 |
if menu:hitTest(x, y, 0) then |
| 365 |
if repaint then |
| 366 |
self:onMouseMove(x, y) |
| 367 |
end |
| 368 |
return true |
| 369 |
end |
| 370 |
for i = #self.open_menus, self.active_menu.level, -1 do |
| 371 |
self.open_menus[i] = nil |
| 372 |
end |
| 373 |
self.active_menu = menu.parent |
| 374 |
repaint = true |
| 375 |
end |
| 376 |
local new_active = self:hitTestBar(x, y) |
| 377 |
if new_active ~= self.active_menu then |
| 378 |
self.open_menus = {new_active} |
| 379 |
self.active_menu = new_active |
| 380 |
repaint = true |
| 381 |
self.ui:playSound("selectx.wav") |
| 382 |
end |
| 383 |
return repaint |
| 384 |
end |
| 385 |
|
| 386 |
function UIMenuBar:onMouseUp(button, x, y) |
| 387 |
if button ~= "left" or not self.visible then |
| 388 |
return |
| 389 |
end |
| 390 |
local repaint = false |
| 391 |
local s = TheApp.config.ui_scale |
| 392 |
while self.active_menu do |
| 393 |
local index = self.active_menu:hitTest(x, y, 0) |
| 394 |
if index == false then |
| 395 |
if not self.active_menu.parent and y < 16 * s then |
| 396 |
break |
| 397 |
else |
| 398 |
self.active_menu = self.active_menu.parent |
| 399 |
end |
| 400 |
elseif index == true then |
| 401 |
break |
| 402 |
else |
| 403 |
local item = self.active_menu.items[index] |
| 404 |
if item.submenu then |
| 405 |
break |
| 406 |
elseif item.is_check_item then |
| 407 |
if item.group then |
| 408 |
if not item.checked then |
| 409 |
item.checked = true |
| 410 |
for _, itm in ipairs(self.active_menu.items) do |
| 411 |
if itm ~= item and itm.group == item.group then |
| 412 |
itm.checked = false |
| 413 |
end |
| 414 |
end |
| 415 |
if item.handler then |
| 416 |
item.handler(item, self.active_menu) |
| 417 |
end |
| 418 |
end |
| 419 |
else |
| 420 |
item.checked = not item.checked |
| 421 |
if item.handler then |
| 422 |
item.handler(item, self.active_menu) |
| 423 |
end |
| 424 |
end |
| 425 |
else |
| 426 |
if item.handler then |
| 427 |
item.handler(item, self.active_menu) |
| 428 |
end |
| 429 |
if y > 22 * s then |
| 430 |
self:disappear() |
| 431 |
end |
| 432 |
self.active_menu = false |
| 433 |
end |
| 434 |
self.ui:playSound("selectx.wav") |
| 435 |
repaint = true |
| 436 |
break |
| 437 |
end |
| 438 |
end |
| 439 |
for i = #self.open_menus, (self.active_menu and self.active_menu.level or 0) + 1, -1 do |
| 440 |
self.open_menus[i] = nil |
| 441 |
end |
| 442 |
return repaint |
| 443 |
end |
| 444 |
|
| 445 |
function UIMenuBar:calculateMenuSize(menu) |
| 446 |
if menu.has_size ~= self then |
| 447 |
local w = 20 |
| 448 |
local h = 6 |
| 449 |
for _, item in ipairs(menu.items) do |
| 450 |
local item_w = math.ceil(self.white_font:sizeOf(item.title) / TheApp.config.ui_scale + 10) |
| 451 |
if item_w > w then |
| 452 |
w = item_w |
| 453 |
end |
| 454 |
h = h + 14 |
| 455 |
end |
| 456 |
if h < 20 then |
| 457 |
h = 20 |
| 458 |
end |
| 459 |
menu.width = w |
| 460 |
menu.height = h |
| 461 |
menu.has_size = self |
| 462 |
local render_list = TH.spriteList() |
| 463 |
menu.render_list = render_list |
| 464 |
render_list:setSheet(self.panel_sprites) |
| 465 |
|
| 466 |
render_list:append(1, 0, 0) |
| 467 |
for x = 10, w - 10, 10 do |
| 468 |
render_list:append(2, x, 0) |
| 469 |
end |
| 470 |
for y = 6, h - 6, 4 do |
| 471 |
render_list:append(4, 0, y) |
| 472 |
for x = 10, w - 10, 10 do |
| 473 |
render_list:append(5, x, y) |
| 474 |
end |
| 475 |
end |
| 476 |
local btmy = h - 6 |
| 477 |
render_list:append(7, 0, btmy) |
| 478 |
for x = 10, w - 10, 10 do |
| 479 |
render_list:append(8, x, btmy) |
| 480 |
end |
| 481 |
|
| 482 |
end |
| 483 |
end |
| 484 |
|
| 485 |
class "UIMenu" |
| 486 |
|
| 487 |
---@type UIMenu |
| 488 |
local UIMenu = _G["UIMenu"] |
| 489 |
|
| 490 |
function UIMenu:UIMenu() |
| 491 |
self.items = {} |
| 492 |
self.parent = false |
| 493 |
self.hover_index = 0 |
| 494 |
self.prev_hover_index = nil |
| 495 |
self.hover_sound = nil |
| 496 |
self.has_size = false |
| 497 |
end |
| 498 |
|
| 499 |
function UIMenu:hitTest(x, y, padding) |
| 500 |
-- number -> hit that item |
| 501 |
-- true -> hit menu, but not an item |
| 502 |
-- false -> no hit |
| 503 |
local s = TheApp.config.ui_scale |
| 504 |
if self.x * s - padding * s <= x and x < self.x * s + self.width * s + padding * s and |
| 505 |
self.y * s - padding * s <= y and y < self.y * s + self.height * s + padding * s then |
| 506 |
if self.x * s <= x and x < self.x * s + self.width * s then |
| 507 |
local index = math_floor((y - self.y * s + 12 * s) / (14 * s)) |
| 508 |
if 1 <= index and index <= #self.items then |
| 509 |
return index |
| 510 |
end |
| 511 |
end |
| 512 |
return true |
| 513 |
else |
| 514 |
return false |
| 515 |
end |
| 516 |
end |
| 517 |
|
| 518 |
function UIMenu:appendBase(item) |
| 519 |
self.items[#self.items + 1] = item |
| 520 |
self.has_size = false |
| 521 |
return self |
| 522 |
end |
| 523 |
|
| 524 |
function UIMenu:appendItem(text, callback) |
| 525 |
return self:appendBase { |
| 526 |
title = text, |
| 527 |
handler = callback, |
| 528 |
} |
| 529 |
end |
| 530 |
|
| 531 |
function UIMenu:appendCheckItem(text, checked, callback, group, condition) |
| 532 |
return self:appendBase { |
| 533 |
is_check_item = true, |
| 534 |
title = text, |
| 535 |
checked = not not checked, |
| 536 |
handler = callback, |
| 537 |
group = group, |
| 538 |
condition = condition |
| 539 |
} |
| 540 |
end |
| 541 |
|
| 542 |
function UIMenu:appendMenu(text, menu) |
| 543 |
menu.parent = self |
| 544 |
return self:appendBase { |
| 545 |
title = text, |
| 546 |
submenu = assert(menu, "No submenu"), |
| 547 |
} |
| 548 |
end |
| 549 |
|
| 550 |
-- A function to set the menu hotkey strings, whether a string or table. |
| 551 |
-- Usage: hotkey_value_label(hotkey name as string) |
| 552 |
-- Ex: hotkey_value_label("ingame_loadMenu") will produce (SHIFT+L) by default. |
| 553 |
--!param hotkey_name (string) Name of hotkey to be converted to string. |
| 554 |
--!param hotkeys (table) The app's hotkey table. I.E. app.hotkeys |
| 555 |
--!return (string) The hotkey returned in this format: (KEY+KEY) |
| 556 |
local function hotkey_value_label(hotkey_name, hotkeys) |
| 557 |
local hotkey_value = hotkeys[hotkey_name] |
| 558 |
if hotkey_value == nil then |
| 559 |
return "" |
| 560 |
end |
| 561 |
return string.upper(array_join(hotkey_value, "+")) |
| 562 |
end |
| 563 |
|
| 564 |
-- Add audio options to the menu. These are on/off and volume control for |
| 565 |
-- announcements, game sounds and music, and open the jukebox. |
| 566 |
--!param menu (object) The menu that the options are added to. |
| 567 |
--!param game (boolean) True if this menu is for the game, where announcements and |
| 568 |
-- game sounds controls are added. |
| 569 |
local function audio_options(menu, game) |
| 570 |
local app = TheApp |
| 571 |
|
| 572 |
local function vol(level, setting) |
| 573 |
if setting == "music" then |
| 574 |
return level == app.config.music_volume, |
| 575 |
function() |
| 576 |
app.audio:setBackgroundVolume(level) |
| 577 |
end, |
| 578 |
"" |
| 579 |
elseif setting == "sound" then |
| 580 |
return level == app.config.sound_volume, |
| 581 |
function() |
| 582 |
app.audio:setSoundVolume(level) |
| 583 |
end, |
| 584 |
"" |
| 585 |
else |
| 586 |
return level == app.config.announcement_volume, |
| 587 |
function() |
| 588 |
app.audio:setAnnouncementVolume(level) |
| 589 |
end, |
| 590 |
"" |
| 591 |
end |
| 592 |
end |
| 593 |
|
| 594 |
local hotkeys = app.hotkeys |
| 595 |
if game then |
| 596 |
-- Hospital sounds |
| 597 |
menu:appendCheckItem(_S.menu_options.sound:format(hotkey_value_label("ingame_toggleSounds", hotkeys)), |
| 598 |
app.config.play_sounds, |
| 599 |
function(item) |
| 600 |
app.audio:playSoundEffects(item.checked) |
| 601 |
end, |
| 602 |
nil, |
| 603 |
function() |
| 604 |
return app.config.play_sounds |
| 605 |
end) |
| 606 |
|
| 607 |
-- Hospital announcements |
| 608 |
menu:appendCheckItem(_S.menu_options.announcements:format(hotkey_value_label("ingame_toggleAnnouncements", hotkeys)), |
| 609 |
app.config.play_announcements, |
| 610 |
function(item) |
| 611 |
app.config.play_announcements = item.checked |
| 612 |
end, |
| 613 |
nil, |
| 614 |
function() |
| 615 |
return app.config.play_announcements |
| 616 |
end) |
| 617 |
end |
| 618 |
|
| 619 |
-- Jukebox music |
| 620 |
if app.audio.has_bg_music then |
| 621 |
menu:appendCheckItem(_S.menu_options.music:format(hotkey_value_label("ingame_toggleMusic", hotkeys)), |
| 622 |
app.config.play_music, |
| 623 |
function(item) |
| 624 |
app.config.play_music = item.checked |
| 625 |
app.ui:togglePlayMusic(item) |
| 626 |
end, |
| 627 |
nil, |
| 628 |
function() |
| 629 |
return app.config.play_music |
| 630 |
end) |
| 631 |
end |
| 632 |
|
| 633 |
-- The three volume menus, and jukebox |
| 634 |
local function appendVolume(setting) |
| 635 |
local volume_menu = UIMenu() |
| 636 |
for level = 10, 100, 10 do |
| 637 |
volume_menu:appendCheckItem(_S.menu_options_volume[level], vol(level / 100, setting)) |
| 638 |
end |
| 639 |
return volume_menu |
| 640 |
end |
| 641 |
if game then |
| 642 |
menu:appendMenu(_S.menu_options.sound_vol, appendVolume("sound")) |
| 643 |
:appendMenu(_S.menu_options.announcements_vol, appendVolume("announcement")) |
| 644 |
end |
| 645 |
if app.audio.has_bg_music then |
| 646 |
menu:appendMenu(_S.menu_options.music_vol, appendVolume("music")) |
| 647 |
:appendItem(_S.menu_options.jukebox:format(hotkey_value_label("ingame_jukebox", hotkeys)), |
| 648 |
function() app.ui:addWindow(UIJukebox(app)) end) |
| 649 |
end |
| 650 |
end |
| 651 |
|
| 652 |
-- Add display options to the menu. |
| 653 |
--!param menu (object) The menu that the options are added to. |
| 654 |
local function display_options(menu) |
| 655 |
local app = TheApp |
| 656 |
|
| 657 |
-- Lock windows |
| 658 |
local function boolean_runtime_config(option) |
| 659 |
return not not app.runtime_config[option], function(item) |
| 660 |
app.runtime_config[option] = item.checked |
| 661 |
end |
| 662 |
end |
| 663 |
menu:appendCheckItem(_S.menu_options.lock_windows, boolean_runtime_config("lock_windows")) |
| 664 |
|
| 665 |
-- Edge scrolling |
| 666 |
menu:appendCheckItem(_S.menu_options.edge_scrolling, |
| 667 |
not app.config.prevent_edge_scrolling, |
| 668 |
function(item) app.config.prevent_edge_scrolling = not item.checked end, |
| 669 |
nil, |
| 670 |
function() return not app.config.prevent_edge_scrolling end) |
| 671 |
|
| 672 |
-- Mouse capture |
| 673 |
menu:appendCheckItem(_S.menu_options.capture_mouse, |
| 674 |
app.config.capture_mouse, |
| 675 |
function(item) app.config.capture_mouse = item.checked |
| 676 |
app:setCaptureMouse() |
| 677 |
end) |
| 678 |
|
| 679 |
-- 24 hour clock |
| 680 |
menu:appendCheckItem(_S.menu_options.twentyfour_hour_clock, |
| 681 |
app.config.twentyfour_hour_clock, |
| 682 |
function(item) |
| 683 |
app.config.twentyfour_hour_clock = item.checked |
| 684 |
end) |
| 685 |
end |
| 686 |
|
| 687 |
local function overlay(...) |
| 688 |
local args = {n = select('#', ...), ...} |
| 689 |
return function(item, m) |
| 690 |
if args.n > 0 then |
| 691 |
TheApp.map:loadDebugText(unpack(args, 1, args.n)) |
| 692 |
else |
| 693 |
TheApp.map:clearDebugText() |
| 694 |
end |
| 695 |
end |
| 696 |
end |
| 697 |
|
| 698 |
--! Make a menu for the map editor. |
| 699 |
--!param app Application. |
| 700 |
function UIMenuBar:makeMapeditorMenu(app) |
| 701 |
local menu = UIMenu() |
| 702 |
local hotkeys = app.hotkeys |
| 703 |
|
| 704 |
menu:appendItem(_S.menu_file.load:format(hotkey_value_label("ingame_loadMenu", hotkeys)), function() self.ui:addWindow(UILoadMap(self.ui, "map")) end) |
| 705 |
:appendItem(_S.menu_file.save:format(hotkey_value_label("ingame_saveMenu", hotkeys)), function() self.ui:addWindow(UISaveMap(self.ui)) end) |
| 706 |
:appendItem(_S.menu_file.restart:format(hotkey_value_label("ingame_restartLevel", hotkeys)), function() self.ui:restartMapEditor() end) |
| 707 |
:appendItem(_S.menu_file.quit:format(hotkey_value_label("ingame_quitLevel", hotkeys)), function() self.ui:quit(true) end) |
| 708 |
self:addMenu(_S.menu.file, menu) |
| 709 |
|
| 710 |
menu = UIMenu() |
| 711 |
audio_options(menu) |
| 712 |
display_options(menu) |
| 713 |
|
| 714 |
menu:appendMenu(_S.menu_debug.map_overlay, UIMenu() |
| 715 |
:appendCheckItem(_S.menu_debug_overlay.none, true, overlay(), "overlay") |
| 716 |
:appendCheckItem(_S.menu_debug_overlay.flags, false, overlay("flags"), "overlay") |
| 717 |
:appendCheckItem(_S.menu_debug_overlay.positions, false, overlay("positions"), "overlay") |
| 718 |
:appendCheckItem(_S.menu_debug_overlay.parcel, false, overlay("parcel"), "overlay")) |
| 719 |
self:addMenu(_S.menu.options, menu) |
| 720 |
|
| 721 |
menu = UIMenu() |
| 722 |
for i = 1, 4 do |
| 723 |
menu:appendCheckItem(_S.menu_player_count["players_" .. i], |
| 724 |
self.ui.app.map:getPlayerCount() == i, |
| 725 |
function() self.ui.map_editor:setPlayerCount(i) end, |
| 726 |
"playercount", |
| 727 |
function () return self.ui.app.map:getPlayerCount() == i end) |
| 728 |
end |
| 729 |
self:addMenu(_S.menu.player_count, menu) |
| 730 |
end |
| 731 |
|
| 732 |
--! Make a menu for the game. |
| 733 |
--!param app Application. |
| 734 |
function UIMenuBar:makeGameMenu(app) |
| 735 |
local menu = UIMenu() |
| 736 |
local hotkeys = app.hotkeys |
| 737 |
|
| 738 |
menu:appendItem(_S.menu_file.load:format(hotkey_value_label("ingame_loadMenu", hotkeys)), function() self.ui:addWindow(UILoadGame(self.ui, "game")) end) |
| 739 |
:appendItem(_S.menu_file.save:format(hotkey_value_label("ingame_saveMenu", hotkeys)), function() self.ui:addWindow(UISaveGame(self.ui)) end) |
| 740 |
:appendItem(_S.menu_file.restart:format(hotkey_value_label("ingame_restartLevel", hotkeys)), function() app:restart() end) |
| 741 |
:appendItem(_S.menu_file.quit:format(hotkey_value_label("ingame_quitLevel", hotkeys)), function() self.ui:quit() end) |
| 742 |
self:addMenu(_S.menu.file, menu) |
| 743 |
|
| 744 |
local options = UIMenu() |
| 745 |
audio_options(options, true) |
| 746 |
display_options(options) |
| 747 |
|
| 748 |
options:appendCheckItem(_S.menu_options.adviser_disabled:format(hotkey_value_label("ingame_toggleAdvisor", hotkeys)), |
| 749 |
not app.config.adviser_disabled, |
| 750 |
function(item) |
| 751 |
app.config.adviser_disabled = not item.checked |
| 752 |
end, |
| 753 |
nil, |
| 754 |
function() |
| 755 |
return not app.config.adviser_disabled |
| 756 |
end) |
| 757 |
|
| 758 |
local function temperatureDisplay(method) |
| 759 |
return method == 1, function() |
| 760 |
app.world.map:setTemperatureDisplayMethod(method) |
| 761 |
end, "", function () |
| 762 |
return app.world.map.temperature_display_method == method |
| 763 |
end |
| 764 |
end |
| 765 |
|
| 766 |
local function wageIncreaseRequests(grant) |
| 767 |
return grant, function() |
| 768 |
app.world:getLocalPlayerHospital().policies.grant_wage_increase = grant |
| 769 |
end, "", function () |
| 770 |
if app.world:getLocalPlayerHospital().policies.grant_wage_increase == nil then |
| 771 |
app.world:getLocalPlayerHospital().policies.grant_wage_increase = app.config.grant_wage_increase |
| 772 |
end |
| 773 |
return app.world:getLocalPlayerHospital().policies.grant_wage_increase == grant |
| 774 |
end |
| 775 |
end |
| 776 |
|
| 777 |
options:appendMenu(_S.menu_options.wage_increase, UIMenu() |
| 778 |
:appendCheckItem(_S.menu_options_wage_increase.grant, wageIncreaseRequests(true)) |
| 779 |
:appendCheckItem(_S.menu_options_wage_increase.deny, wageIncreaseRequests(false)) |
| 780 |
) |
| 781 |
|
| 782 |
options:appendMenu(_S.menu_options.warmth_colors, UIMenu() |
| 783 |
:appendCheckItem(_S.menu_options_warmth_colors.choice_1, temperatureDisplay(1)) |
| 784 |
:appendCheckItem(_S.menu_options_warmth_colors.choice_2, temperatureDisplay(2)) |
| 785 |
:appendCheckItem(_S.menu_options_warmth_colors.choice_3, temperatureDisplay(3)) |
| 786 |
) |
| 787 |
|
| 788 |
local function rate(speed) |
| 789 |
return speed == "Normal", function() |
| 790 |
app.world:setSpeed(speed) |
| 791 |
end, "", function() |
| 792 |
return app.world:isCurrentSpeed(speed) |
| 793 |
end |
| 794 |
end |
| 795 |
|
| 796 |
options:appendMenu(_S.menu_options.game_speed, UIMenu() |
| 797 |
:appendCheckItem(_S.menu_options_game_speed.pause:format(hotkey_value_label("ingame_pause", hotkeys)), rate("Pause")) |
| 798 |
:appendCheckItem(_S.menu_options_game_speed.slowest:format(hotkey_value_label("ingame_gamespeed_slowest", hotkeys)), rate("Slowest")) |
| 799 |
:appendCheckItem(_S.menu_options_game_speed.slower:format(hotkey_value_label("ingame_gamespeed_slower", hotkeys)), rate("Slower")) |
| 800 |
:appendCheckItem(_S.menu_options_game_speed.normal:format(hotkey_value_label("ingame_gamespeed_normal", hotkeys)), rate("Normal")) -- (default) |
| 801 |
:appendCheckItem(_S.menu_options_game_speed.max_speed:format(hotkey_value_label("ingame_gamespeed_max", hotkeys)), rate("Max speed")) |
| 802 |
:appendCheckItem(_S.menu_options_game_speed.and_then_some_more:format(hotkey_value_label("ingame_gamespeed_thensome", hotkeys)), rate("And then some more")) |
| 803 |
) |
| 804 |
|
| 805 |
self:addMenu(_S.menu.options, options) |
| 806 |
|
| 807 |
self:addMenu(_S.menu.charts, UIMenu() |
| 808 |
:appendItem(_S.menu_charts.bank_manager:format(hotkey_value_label("ingame_panel_bankManager", hotkeys)), function() self.ui.bottom_panel:dialogBankManager(true) end) |
| 809 |
:appendItem(_S.menu_charts.statement:format(hotkey_value_label("ingame_panel_bankStats", hotkeys)), function() self.ui.bottom_panel:dialogBankStats(true) end) |
| 810 |
:appendItem(_S.menu_charts.staff_listing:format(hotkey_value_label("ingame_panel_staffManage", hotkeys)), function() self.ui.bottom_panel:dialogStaffManagement(true) end) |
| 811 |
:appendItem(_S.menu_charts.town_map:format(hotkey_value_label("ingame_panel_townMap", hotkeys)), function() self.ui.bottom_panel:dialogTownMap(true) end) |
| 812 |
:appendItem(_S.menu_charts.casebook:format(hotkey_value_label("ingame_panel_casebook", hotkeys)), function() self.ui.bottom_panel:dialogDrugCasebook(true) end) |
| 813 |
:appendItem(_S.menu_charts.research:format(hotkey_value_label("ingame_panel_research", hotkeys)), function() self.ui.bottom_panel:dialogResearch(true) end) |
| 814 |
:appendItem(_S.menu_charts.status:format(hotkey_value_label("ingame_panel_status", hotkeys)), function() self.ui.bottom_panel:dialogStatus(true) end) |
| 815 |
:appendItem(_S.menu_charts.graphs:format(hotkey_value_label("ingame_panel_charts", hotkeys)), function() self.ui.bottom_panel:dialogCharts(true) end) |
| 816 |
:appendItem(_S.menu_charts.policy:format(hotkey_value_label("ingame_panel_policy", hotkeys)), function() self.ui.bottom_panel:dialogPolicy(true) end) |
| 817 |
:appendItem(_S.menu_charts.machine_menu:format(hotkey_value_label("ingame_panel_machineMenu", hotkeys)), function() self.ui:addWindow(UIMachineMenu(self.ui)) end) |
| 818 |
:appendItem(_S.menu_charts.adviser_history:format(hotkey_value_label("ingame_panel_adviserHistory", hotkeys)), function() self.ui:addWindow(UIAdviserHistory(self.ui)) end) |
| 819 |
:appendItem(_S.menu_charts.briefing, function() self.ui:showBriefing() end) |
| 820 |
) |
| 821 |
|
| 822 |
local function allowBlockingAreas(option) |
| 823 |
return option == 2, function() |
| 824 |
app.config.blocking_off_areas = option |
| 825 |
end, "", function () |
| 826 |
return app.config.blocking_off_areas == option |
| 827 |
end |
| 828 |
end |
| 829 |
local function limit_camera(item) |
| 830 |
app.ui:limitCamera(item.checked) |
| 831 |
end |
| 832 |
local function disable_salary_raise(item) |
| 833 |
app.world:debugDisableSalaryRaise(item.checked) |
| 834 |
end |
| 835 |
local levels_menu = UIMenu() |
| 836 |
for L = 1, 12 do |
| 837 |
levels_menu:appendItem((" L%i "):format(L), function() |
| 838 |
if app:loadLevel(L, nil, nil, nil, nil, nil, _S.errors.load_level_prefix, nil) then |
| 839 |
self.ui.app.moviePlayer:playAdvanceMovie(L) |
| 840 |
end |
| 841 |
end) |
| 842 |
end |
| 843 |
if self.ui.app.config.debug then |
| 844 |
self:addMenu(_S.menu.debug, UIMenu() -- Debug |
| 845 |
:appendMenu(_S.menu_debug.jump_to_level, levels_menu) |
| 846 |
:appendMenu(_S.menu_debug.allow_blocking_off_areas, UIMenu() |
| 847 |
:appendCheckItem(_S.menu_debug_overlay_blocking_off_areas.choice_1, allowBlockingAreas(1)) |
| 848 |
:appendCheckItem(_S.menu_debug_overlay_blocking_off_areas.choice_2, allowBlockingAreas(2)) |
| 849 |
:appendCheckItem(_S.menu_debug_overlay_blocking_off_areas.choice_3, allowBlockingAreas(3)) |
| 850 |
) |
| 851 |
:appendCheckItem(_S.menu_debug.limit_camera, true, limit_camera, nil, function() return self.ui.limit_to_visible_diamond end) |
| 852 |
:appendCheckItem(_S.menu_debug.disable_salary_raise, false, disable_salary_raise, nil, function() return self.ui.app.world.debug_disable_salary_raise end) |
| 853 |
:appendItem(_S.menu_debug.make_debug_fax, function() self.ui:makeDebugFax() end) |
| 854 |
:appendItem(_S.menu_debug.make_debug_patient, function() self.ui:addWindow(UIMakeDebugPatient(self.ui)) end) |
| 855 |
:appendItem(_S.menu_debug.cheats:format(hotkey_value_label("ingame_showCheatWindow", hotkeys)), function() self.ui:addWindow(UICheats(self.ui)) end) |
| 856 |
:appendItem(_S.menu_debug.lua_console:format(hotkey_value_label("global_showLuaConsole", hotkeys)), function() self.ui:addWindow(UILuaConsole(self.ui)) end) |
| 857 |
:appendItem(_S.menu_debug.debug_script:format(hotkey_value_label("global_runDebugScript", hotkeys)), function() self.ui:runDebugScript() end) |
| 858 |
:appendItem(_S.menu_debug.calls_dispatcher, function() self.ui:addWindow(UICallsDispatcher(self.ui)) end) |
| 859 |
:appendItem(_S.menu_debug.dump_strings:format(hotkey_value_label("ingame_poopStrings", hotkeys)), function() self.ui.app:dumpStrings() end) |
| 860 |
:appendItem(_S.menu_debug.dump_gamelog:format(hotkey_value_label("ingame_poopLog", hotkeys)), function() self.ui.app.world:dumpGameLog() end) |
| 861 |
:appendMenu(_S.menu_debug.map_overlay, UIMenu() |
| 862 |
:appendCheckItem(_S.menu_debug_overlay.none, true, overlay(), "") |
| 863 |
:appendCheckItem(_S.menu_debug_overlay.flags, false, overlay("flags"), "") |
| 864 |
:appendCheckItem(_S.menu_debug_overlay.positions, false, overlay("positions"), "") |
| 865 |
:appendCheckItem(_S.menu_debug_overlay.heat, false, overlay("heat"), "") |
| 866 |
:appendCheckItem(_S.menu_debug_overlay.byte_0_1, false, overlay(0, 1, false), "") |
| 867 |
:appendCheckItem(_S.menu_debug_overlay.byte_floor, false, overlay(2, 2, false), "") |
| 868 |
:appendCheckItem(_S.menu_debug_overlay.byte_n_wall, false, overlay(3, 3, false), "") |
| 869 |
:appendCheckItem(_S.menu_debug_overlay.byte_w_wall, false, overlay(4, 4, false), "") |
| 870 |
:appendCheckItem(_S.menu_debug_overlay.byte_5, false, overlay(5, 5, true), "") |
| 871 |
:appendCheckItem(_S.menu_debug_overlay.byte_6, false, overlay(6, 6, true), "") |
| 872 |
:appendCheckItem(_S.menu_debug_overlay.byte_7, false, overlay(7, 7, true), "") |
| 873 |
:appendCheckItem(_S.menu_debug_overlay.parcel, false, overlay("parcel"), "") |
| 874 |
) |
| 875 |
:appendItem(_S.menu_debug.sprite_viewer, function() corsixth.require("sprite_viewer")() end) |
| 876 |
) |
| 877 |
end |
| 878 |
end |