| 1 |
--[[ Copyright (c) 2010 Manuel "Roujin" Wolf |
| 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 |
--! Options window used in the main menu and ingame. |
| 22 |
class "UIOptions" (UIResizable) |
| 23 |
|
| 24 |
---@type UIOptions |
| 25 |
local UIOptions = _G["UIOptions"] |
| 26 |
|
| 27 |
-- Constants for most button's width and height |
| 28 |
local BTN_WIDTH = 135 |
| 29 |
local BTN_HEIGHT = 20 |
| 30 |
|
| 31 |
-- Colour definitions |
| 32 |
local col = { |
| 33 |
bg = Colours.PanelDefault, |
| 34 |
button = Colours.PanelDefault, |
| 35 |
setting = Colours.Setting, |
| 36 |
setting_active = Colours.SettingActive, |
| 37 |
scrollbar = Colours.Scrollbar, |
| 38 |
disabled = Colours.Disabled, |
| 39 |
title = Colours.Title, |
| 40 |
caption = Colours.Caption, |
| 41 |
textbox = Colours.Textbox, |
| 42 |
} |
| 43 |
|
| 44 |
-- Private functions |
| 45 |
|
| 46 |
--- Calculates the Y position for the dialog box in the option menu |
| 47 |
-- and increments along the current position for the next element |
| 48 |
-- @return The Y position to place the element at |
| 49 |
function UIOptions:_getOptionYPos() |
| 50 |
-- Offset from top of options box |
| 51 |
local STARTING_Y_POS = 15 |
| 52 |
-- Y Height is 20 for panel size + 10 for spacing |
| 53 |
local Y_HEIGHT = 30 |
| 54 |
|
| 55 |
-- Multiply by the index so that index=1 is at STARTING_Y_POS |
| 56 |
local calculated_pos = STARTING_Y_POS + Y_HEIGHT * (self._current_option_index - 1) |
| 57 |
self._current_option_index = self._current_option_index + 1 |
| 58 |
return calculated_pos |
| 59 |
end |
| 60 |
|
| 61 |
--! Resets the index to start at the top of a new column, below the title, |
| 62 |
-- for the Y position calculation. |
| 63 |
function UIOptions:_startNewColumn() |
| 64 |
self._current_option_index = 2 |
| 65 |
self.column_count = self.column_count + 1 |
| 66 |
end |
| 67 |
|
| 68 |
-- Generate predefined resolutions the player can choose from; as well as |
| 69 |
-- including the custom option at the bottom. Where UI scaling prevents a |
| 70 |
-- resolution option from being selected, grey it out instead and move to |
| 71 |
-- the bottom of the list. |
| 72 |
local available_resolutions = function() |
| 73 |
local suggested_resolutions = { |
| 74 |
{text = "640x480 (4:3)", width = 640, height = 480 }, |
| 75 |
{text = "800x600 (4:3)", width = 800, height = 600 }, |
| 76 |
{text = "1024x768 (4:3)", width = 1024, height = 768 }, |
| 77 |
{text = "1280x960 (4:3)", width = 1280, height = 960 }, |
| 78 |
{text = "1600x1200 (4:3)", width = 1600, height = 1200 }, |
| 79 |
{text = "1920x1440 (4:3)", width = 1920, height = 1440 }, |
| 80 |
{text = "1280x1024 (5:4)", width = 1280, height = 1024 }, |
| 81 |
{text = "1280x720 (16:9)", width = 1280, height = 720 }, |
| 82 |
{text = "1366x768 (16:9)", width = 1366, height = 768 }, |
| 83 |
{text = "1600x900 (16:9)", width = 1600, height = 900 }, |
| 84 |
{text = "1920x1080 (16:9)", width = 1920, height = 1080 }, |
| 85 |
{text = "2560x1440 (16:9)", width = 2560, height = 1440 }, |
| 86 |
{text = "3840x2160 (16:9)", width = 3840, height = 2160 }, |
| 87 |
{text = "1280x800 (16:10)", width = 1280, height = 800 }, |
| 88 |
{text = "1440x900 (16:10)", width = 1440, height = 900 }, |
| 89 |
{text = "1680x1050 (16:10)", width = 1680, height = 1050 }, |
| 90 |
{text = "1920x1200 (16:10)", width = 1920, height = 1200 }, |
| 91 |
} |
| 92 |
|
| 93 |
local s = TheApp.config.ui_scale |
| 94 |
local enable_list, disable_list = {}, {} |
| 95 |
for _, opt in ipairs(suggested_resolutions) do |
| 96 |
local enabled = App.MIN_WINDOW_WIDTH * s <= opt.width and |
| 97 |
App.MIN_WINDOW_HEIGHT * s <= opt.height |
| 98 |
opt.disabled = not enabled |
| 99 |
opt.tooltip = opt.disabled and { _S.tooltip.options_window.resolution_unavailable } |
| 100 |
if enabled then |
| 101 |
enable_list[#enable_list + 1] = opt |
| 102 |
else |
| 103 |
disable_list[#disable_list + 1] = opt |
| 104 |
end |
| 105 |
end |
| 106 |
|
| 107 |
local res = enable_list |
| 108 |
-- Show custom button before disabled items |
| 109 |
res[#res + 1] = { |
| 110 |
text = _S.options_window.custom_resolution, custom = true |
| 111 |
} |
| 112 |
|
| 113 |
for i = 1, #disable_list do |
| 114 |
res[#res + 1] = disable_list[i] |
| 115 |
end |
| 116 |
|
| 117 |
return res |
| 118 |
end |
| 119 |
|
| 120 |
local available_ui_scales = function() |
| 121 |
local res = {} |
| 122 |
local s = 1 |
| 123 |
while s * App.MIN_WINDOW_WIDTH <= TheApp.config.width and |
| 124 |
s * App.MIN_WINDOW_HEIGHT <= TheApp.config.height do |
| 125 |
res[#res + 1] = { text = tostring(s * 100) .. '%', scale = s } |
| 126 |
s = s + 1 |
| 127 |
end |
| 128 |
return res |
| 129 |
end |
| 130 |
|
| 131 |
local available_autosave_frequency = function() |
| 132 |
local options = { |
| 133 |
{ text = _S.autosave_frequency.monthly, value = 1, tooltip = { _S.tooltip.autosave_frequency.monthly } }, |
| 134 |
{ text = _S.autosave_frequency.weekly, value = 2, tooltip = { _S.tooltip.autosave_frequency.weekly } }, |
| 135 |
{ text = _S.autosave_frequency.daily, value = 3, tooltip = { _S.tooltip.autosave_frequency.daily } }, |
| 136 |
} |
| 137 |
return options |
| 138 |
end |
| 139 |
|
| 140 |
local current_autosave_frequency = function() |
| 141 |
local value = TheApp.config.autosave_frequency |
| 142 |
local options = available_autosave_frequency() |
| 143 |
for _, option in pairs(options) do |
| 144 |
if option.value == value then |
| 145 |
return option.text |
| 146 |
end |
| 147 |
end |
| 148 |
return "" |
| 149 |
end |
| 150 |
|
| 151 |
function UIOptions:UIOptions(ui, mode) |
| 152 |
local width = 620 |
| 153 |
local height = 330 |
| 154 |
self:UIResizable(ui, width, height, col.bg) |
| 155 |
|
| 156 |
local app = ui.app |
| 157 |
self.mode = mode |
| 158 |
self.modal_class = mode == "menu" and "main menu" or "options" |
| 159 |
self.on_top = mode == "menu" |
| 160 |
self.esc_closes = true |
| 161 |
self.resizable = false |
| 162 |
self:setDefaultPosition(0.5, 0.25) |
| 163 |
self.default_button_sound = "selectx.wav" |
| 164 |
self.app = app |
| 165 |
|
| 166 |
-- Tracks the current position of the object |
| 167 |
self._current_option_index = 1 |
| 168 |
self.column_count = 1 |
| 169 |
|
| 170 |
self:checkForAvailableLanguages() |
| 171 |
|
| 172 |
-- Window parts definition |
| 173 |
-- Title |
| 174 |
local title_y_pos = self:_getOptionYPos() |
| 175 |
self:addBevelPanel(175, title_y_pos, BTN_WIDTH * 2, 20, col.title):setLabel(_S.options_window.caption) |
| 176 |
.lowered = true |
| 177 |
|
| 178 |
-- Create our setting items. This create a caption/label for the setting |
| 179 |
-- and the setting itself. We return both elements of the setting (panel |
| 180 |
-- and the button made from the panel) |
| 181 |
local function createOptionsElement(option_label, option_tooltip, |
| 182 |
setting_label, setting_tooltip, setting_colours, callback, |
| 183 |
toggle_state) |
| 184 |
local y_pos = self:_getOptionYPos() |
| 185 |
local x_offset = 300 * (self.column_count - 1) |
| 186 |
local label_x, setting_x = 20 + x_offset, 165 + x_offset |
| 187 |
|
| 188 |
-- Make the setting name panel |
| 189 |
self:addBevelPanel(label_x, y_pos, BTN_WIDTH, BTN_HEIGHT, col.caption, col.bg, col.bg) |
| 190 |
:setLabel(option_label) |
| 191 |
:setTooltip(option_tooltip) |
| 192 |
.lowered = true |
| 193 |
local s_col = setting_colours |
| 194 |
-- Make the setting value panel |
| 195 |
local setting_panel = self:addBevelPanel(setting_x, y_pos, BTN_WIDTH, |
| 196 |
BTN_HEIGHT, s_col.bg, s_col.highlight, s_col.shadow, |
| 197 |
s_col.disabled, s_col.active) |
| 198 |
:setLabel(setting_label) |
| 199 |
:setTooltip(setting_tooltip) |
| 200 |
-- Make the value panel a button |
| 201 |
local setting_button = setting_panel:makeToggleButton(0, 0, BTN_WIDTH, |
| 202 |
BTN_HEIGHT, nil, callback) |
| 203 |
:setToggleState(toggle_state) |
| 204 |
-- Return the setting value info |
| 205 |
return setting_panel, setting_button |
| 206 |
end |
| 207 |
|
| 208 |
if app:isUpdateCheckAvailable() then |
| 209 |
-- Check for updates |
| 210 |
local updates_string = app.config.check_for_updates and |
| 211 |
_S.options_window.option_enabled or _S.options_window.option_disabled |
| 212 |
self.updates_panel, self.updates_button = createOptionsElement( |
| 213 |
_S.options_window.check_for_updates, _S.tooltip.options_window.check_for_updates, |
| 214 |
updates_string, nil, { bg = col.setting }, |
| 215 |
self.buttonUpdates, app.config.check_for_updates) |
| 216 |
end |
| 217 |
|
| 218 |
-- Fullscreen |
| 219 |
local fullscreen_label = app.fullscreen and _S.options_window.option_on |
| 220 |
or _S.options_window.option_off |
| 221 |
self.fullscreen_panel, self.fullscreen_button = createOptionsElement( |
| 222 |
_S.options_window.fullscreen, _S.tooltip.options_window.fullscreen, |
| 223 |
fullscreen_label, _S.tooltip.options_window.fullscreen_button, { bg = col.setting }, |
| 224 |
self.buttonFullscreen, app.fullscreen) |
| 225 |
|
| 226 |
|
| 227 |
-- Screen resolution |
| 228 |
-- We will set the button label after making up the UI scale option below |
| 229 |
self.resolution_panel, self.resolution_button = createOptionsElement( |
| 230 |
_S.options_window.resolution, _S.tooltip.options_window.resolution, |
| 231 |
"", _S.tooltip.options_window.select_resolution, |
| 232 |
{ bg = col.setting, active = col.setting_active }, |
| 233 |
self.dropdownResolution, false) |
| 234 |
|
| 235 |
-- UI Scale |
| 236 |
local scale_label = TheApp.config.ui_scale * 100 .. "%" |
| 237 |
self.scale_ui_panel, self.scale_ui_button = createOptionsElement( |
| 238 |
_S.options_window.scale_ui, _S.tooltip.options_window.scale_ui, |
| 239 |
scale_label, nil, |
| 240 |
{ bg = col.setting, active = col.setting_active }, |
| 241 |
self.dropdownUIScale, false) |
| 242 |
|
| 243 |
-- Now set the resolution button label and the ui scale button state |
| 244 |
self:processWindowResizeEvent() |
| 245 |
|
| 246 |
-- Mouse capture |
| 247 |
local capture_label = app.config.capture_mouse and |
| 248 |
_S.options_window.option_on or _S.options_window.option_off |
| 249 |
self.mouse_capture_panel, self.mouse_capture_button = createOptionsElement( |
| 250 |
_S.options_window.capture_mouse, _S.tooltip.options_window.capture_mouse, |
| 251 |
capture_label, _S.tooltip.options_window.capture_mouse, { bg = col.setting }, |
| 252 |
self.buttonMouseCapture, app.config.capture_mouse) |
| 253 |
|
| 254 |
|
| 255 |
-- Language |
| 256 |
-- Get language name in the language to normalize display. |
| 257 |
-- If it doesn't exist, display the current config option. |
| 258 |
local lang = self.app.strings:getLanguageNames(app.config.language) |
| 259 |
if lang then |
| 260 |
lang = lang[1] |
| 261 |
else |
| 262 |
lang = app.config.language |
| 263 |
end |
| 264 |
|
| 265 |
-- Start a new column of buttons |
| 266 |
self:_startNewColumn() |
| 267 |
|
| 268 |
-- Language setting. |
| 269 |
self.language_panel, self.language_button = createOptionsElement( |
| 270 |
_S.options_window.language, _S.tooltip.options_window.language, |
| 271 |
lang, _S.tooltip.options_window.select_language, |
| 272 |
{ bg = col.setting, active = col.setting_active }, |
| 273 |
self.dropdownLanguage, false) |
| 274 |
|
| 275 |
|
| 276 |
-- Set scroll speed. |
| 277 |
local cur_scrollspeed = tostring(self.ui.app.config.scroll_speed) |
| 278 |
self.scrollspeed_panel, self.scrollspeed_button = createOptionsElement( |
| 279 |
_S.options_window.scrollspeed, _S.tooltip.options_window.scrollspeed, |
| 280 |
cur_scrollspeed, _S.tooltip.options_window.scrollspeed, |
| 281 |
{ bg = col.setting, active = col.setting_active }, |
| 282 |
self.buttonScrollSpeed, false) |
| 283 |
|
| 284 |
-- Set shift scroll speed. |
| 285 |
local cur_shiftscrollspeed = tostring(self.ui.app.config.shift_scroll_speed) |
| 286 |
self.shift_scrollspeed_panel, self.shift_scrollspeed_button = createOptionsElement( |
| 287 |
_S.options_window.shift_scrollspeed, _S.tooltip.options_window.shift_scrollspeed, |
| 288 |
cur_shiftscrollspeed, _S.tooltip.options_window.shift_scrollspeed, |
| 289 |
{ bg = col.setting, active = col.setting_active }, |
| 290 |
self.buttonShiftScrollSpeed, false) |
| 291 |
|
| 292 |
-- Set zoom speed. |
| 293 |
local cur_zoomspeed = tostring(self.ui.app.config.zoom_speed) |
| 294 |
self.zoomspeed_panel, self.zoomspeed_button = createOptionsElement( |
| 295 |
_S.options_window.zoom_speed, _S.tooltip.options_window.zoom_speed, |
| 296 |
cur_zoomspeed, _S.tooltip.options_window.zoom_speed, |
| 297 |
{ bg = col.setting, active = col.setting_active }, |
| 298 |
self.buttonZoomSpeed, false) |
| 299 |
|
| 300 |
|
| 301 |
-- Autosave frequency |
| 302 |
local autosave_frequency_label = current_autosave_frequency() |
| 303 |
self.autosave_frequency_panel, self.autosave_frequency_button = createOptionsElement( |
| 304 |
_S.options_window.autosave_frequency, _S.tooltip.options_window.autosave_frequency, |
| 305 |
autosave_frequency_label, _S.tooltip.options_window.autosave_frequency, |
| 306 |
{ bg = col.setting, active = col.setting_active }, |
| 307 |
self.dropdownAutosaveFrequency, false) |
| 308 |
|
| 309 |
|
| 310 |
-- The right row is currently uneven with the left row, add an additional spacer |
| 311 |
-- to avoid an overlap. |
| 312 |
self:_getOptionYPos() |
| 313 |
|
| 314 |
local lower_row_y_pos = self:_getOptionYPos() |
| 315 |
-- "Customise" button |
| 316 |
self:addBevelPanel(20, lower_row_y_pos, BTN_WIDTH, 30, col.button):setLabel(_S.options_window.customise) |
| 317 |
:makeButton(0, 0, BTN_WIDTH, 30, nil, self.buttonCustomise) |
| 318 |
:setTooltip(_S.tooltip.options_window.customise_button) |
| 319 |
|
| 320 |
-- "Folders" button |
| 321 |
self:addBevelPanel(165, lower_row_y_pos, BTN_WIDTH, 30, col.button):setLabel(_S.options_window.folder) |
| 322 |
:makeButton(0, 0, BTN_WIDTH, 30, nil, self.buttonFolder) |
| 323 |
:setTooltip(_S.tooltip.options_window.folder_button) |
| 324 |
|
| 325 |
-- "Hotkeys" button |
| 326 |
self:addBevelPanel(320, lower_row_y_pos, BTN_WIDTH, 30, col.button):setLabel(_S.options_window.hotkey) |
| 327 |
:makeButton(0, 0, BTN_WIDTH, 30, nil, self.buttonHotkey) |
| 328 |
:setTooltip(_S.tooltip.options_window.hotkey) |
| 329 |
|
| 330 |
-- "Sound Options" button |
| 331 |
self:addBevelPanel(465, lower_row_y_pos, BTN_WIDTH, 30, col.button):setLabel(_S.options_window.sound) |
| 332 |
:makeButton(0, 0, BTN_WIDTH, 30, nil, self.buttonSound) |
| 333 |
:setTooltip(_S.tooltip.options_window.sound) |
| 334 |
|
| 335 |
-- "Back" button |
| 336 |
-- Give some extra space to back button. This is fine as long as it is the last button in the options menu |
| 337 |
local back_button_y_pos = self:_getOptionYPos() + 20 |
| 338 |
self:addBevelPanel(175, back_button_y_pos, BTN_WIDTH * 2, 40, col.button):setLabel(_S.options_window.back) |
| 339 |
:makeButton(0, 0, 280, 40, nil, self.buttonBack) |
| 340 |
:setTooltip(_S.tooltip.options_window.back) |
| 341 |
end |
| 342 |
|
| 343 |
-- Stubs for backward compatibility |
| 344 |
local --[[persistable:options_window_language_button]] function language_button() end |
| 345 |
local --[[persistable:options_width_textbox_reset]] function width_textbox_reset() end |
| 346 |
local --[[persistable:options_height_textbox_reset]] function height_textbox_reset() end |
| 347 |
|
| 348 |
function UIOptions:checkForAvailableLanguages() |
| 349 |
local app = self.app |
| 350 |
-- Set up list of available languages |
| 351 |
local langs, c = {}, 1 |
| 352 |
for _, lang in pairs(app.strings.languages) do |
| 353 |
local font = app.strings:getFont(lang) |
| 354 |
local eng_name = app.strings.languages_english[lang] |
| 355 |
c = c + 1 |
| 356 |
-- If freetype support and a unicode font setting are not present then |
| 357 |
-- languages not supported by the builtin font are named in English and cannot be selected |
| 358 |
if app.gfx:hasLanguageFont(font) then |
| 359 |
font = font and app.gfx:loadLanguageFont(font, app.gfx:loadSpriteTable("QData", "Font01V"), { apply_ui_scale = true }) |
| 360 |
langs[#langs + 1] = { text = lang, name = lang, font = font, disabled = false, |
| 361 |
tooltip = { _S.tooltip.options_window.language_dropdown_item:format(eng_name) } } |
| 362 |
else |
| 363 |
langs[#langs + 1] = { text = eng_name, name = lang, font = self.builtin_font, disabled = true, |
| 364 |
tooltip = { _S.tooltip.options_window.language_dropdown_no_font } } |
| 365 |
end |
| 366 |
end |
| 367 |
self.available_languages = langs |
| 368 |
end |
| 369 |
|
| 370 |
function UIOptions:dropdownLanguage(activate) |
| 371 |
if activate then |
| 372 |
self:dropdownResolution(false) |
| 373 |
self:dropdownUIScale(false) |
| 374 |
self:dropdownAutosaveFrequency(false) |
| 375 |
self.language_dropdown = UIDropdown(self.ui, self, self.language_button, self.available_languages, self.selectLanguage, col.setting_active, col.scrollbar, col.disabled) |
| 376 |
self:addWindow(self.language_dropdown) |
| 377 |
else |
| 378 |
self.language_button:setToggleState(false) |
| 379 |
if self.language_dropdown then |
| 380 |
self.language_dropdown:close() |
| 381 |
self.language_dropdown = nil |
| 382 |
end |
| 383 |
end |
| 384 |
end |
| 385 |
|
| 386 |
function UIOptions:selectLanguage(number) |
| 387 |
local lang = self.app.strings.languages_english[self.available_languages[number].name] |
| 388 |
local app = self.ui.app |
| 389 |
app.config.language = (lang) |
| 390 |
app:initLanguage() |
| 391 |
app:saveConfig() |
| 392 |
end |
| 393 |
|
| 394 |
function UIOptions:dropdownResolution(activate) |
| 395 |
if activate then |
| 396 |
self.available_resolutions = available_resolutions() |
| 397 |
self:dropdownLanguage(false) |
| 398 |
self:dropdownUIScale(false) |
| 399 |
self:dropdownAutosaveFrequency(false) |
| 400 |
self.resolution_dropdown = UIDropdown(self.ui, self, self.resolution_button, self.available_resolutions, self.selectResolution, col.setting_active, col.scrollbar, col.disabled) |
| 401 |
self:addWindow(self.resolution_dropdown) |
| 402 |
else |
| 403 |
self.resolution_button:setToggleState(false) |
| 404 |
if self.resolution_dropdown then |
| 405 |
self.resolution_dropdown:close() |
| 406 |
self.resolution_dropdown = nil |
| 407 |
end |
| 408 |
end |
| 409 |
end |
| 410 |
|
| 411 |
function UIOptions:selectResolution(number) |
| 412 |
local res = self.available_resolutions[number] |
| 413 |
|
| 414 |
local callback = --[[persistable:options_resolution_callback]] function(width, height) |
| 415 |
if not self.ui:changeResolution(width, height) then |
| 416 |
local err = {_S.errors.unavailable_screen_size} |
| 417 |
self.ui:addWindow(UIInformation(self.ui, err)) |
| 418 |
end |
| 419 |
end |
| 420 |
|
| 421 |
if res.custom then |
| 422 |
self.resolution_panel:setLabel(self.ui.app.config.width .. "x" .. self.ui.app.config.height) |
| 423 |
self.ui:addWindow(UIResolution(self.ui, callback)) |
| 424 |
else |
| 425 |
callback(res.width, res.height) |
| 426 |
end |
| 427 |
end |
| 428 |
|
| 429 |
function UIOptions:dropdownUIScale(activate) |
| 430 |
if activate then |
| 431 |
self.available_ui_scales = available_ui_scales() |
| 432 |
self:dropdownLanguage(false) |
| 433 |
self:dropdownResolution(false) |
| 434 |
self:dropdownAutosaveFrequency(false) |
| 435 |
self.scale_ui_dropdown = UIDropdown(self.ui, self, self.scale_ui_button, self.available_ui_scales, self.selectUIScale, col.setting_active, col.scrollbar) |
| 436 |
self:addWindow(self.scale_ui_dropdown) |
| 437 |
else |
| 438 |
self.scale_ui_button:setToggleState(false) |
| 439 |
if self.scale_ui_dropdown then |
| 440 |
self.scale_ui_dropdown:close() |
| 441 |
self.scale_ui_dropdown = nil |
| 442 |
end |
| 443 |
end |
| 444 |
end |
| 445 |
|
| 446 |
-- Check if UI scale button should be enabled, and update the tooltip. |
| 447 |
function UIOptions:updateUIScaleAvailabilityState() |
| 448 |
local ui_scales_available = #available_ui_scales() > 1 |
| 449 |
self.scale_ui_button:enable(ui_scales_available) |
| 450 |
self.scale_ui_button:setTooltip(ui_scales_available and |
| 451 |
_S.tooltip.options_window.select_ui_scale or |
| 452 |
_S.tooltip.options_window.ui_scale_unavailable) |
| 453 |
end |
| 454 |
|
| 455 |
function UIOptions:selectUIScale(number) |
| 456 |
local res = self.available_ui_scales[number] |
| 457 |
TheApp.config.ui_scale = res.scale |
| 458 |
TheApp:saveConfig() |
| 459 |
self.scale_ui_panel:setLabel(res.text) |
| 460 |
self.ui:changeResolution(TheApp.config.width, TheApp.config.height) |
| 461 |
TheApp.gfx:onChangeUIScale() |
| 462 |
end |
| 463 |
|
| 464 |
function UIOptions:dropdownAutosaveFrequency(activate) |
| 465 |
if activate then |
| 466 |
self:dropdownLanguage(false) |
| 467 |
self:dropdownResolution(false) |
| 468 |
self:dropdownUIScale(false) |
| 469 |
self.autosave_dropdown = UIDropdown(self.ui, self, self.autosave_frequency_button, available_autosave_frequency(), self.selectAutosaveFrequency, col.setting_active, col.scrollbar) |
| 470 |
self:addWindow(self.autosave_dropdown) |
| 471 |
else |
| 472 |
self.autosave_frequency_button:setToggleState(false) |
| 473 |
if self.autosave_dropdown then |
| 474 |
self.autosave_dropdown:close() |
| 475 |
self.autosave_dropdown = nil |
| 476 |
end |
| 477 |
end |
| 478 |
end |
| 479 |
|
| 480 |
function UIOptions:selectAutosaveFrequency(number) |
| 481 |
local option = available_autosave_frequency()[number] |
| 482 |
self.autosave_frequency_panel:setLabel(option.text) |
| 483 |
TheApp.config.autosave_frequency = option.value |
| 484 |
TheApp:saveConfig() |
| 485 |
end |
| 486 |
|
| 487 |
--! Changes check for update setting to on/of |
| 488 |
function UIOptions:toggleUpdateCheck() |
| 489 |
self.ui.app.config.check_for_updates = not self.ui.app.config.check_for_updates |
| 490 |
self.ui.app:saveConfig() |
| 491 |
end |
| 492 |
|
| 493 |
--! Function handles button toggle of checking for updates |
| 494 |
function UIOptions:buttonUpdates() |
| 495 |
self:toggleUpdateCheck() |
| 496 |
local new_updates_string = self.ui.app.config.check_for_updates and |
| 497 |
_S.options_window.option_enabled or _S.options_window.option_disabled |
| 498 |
self.updates_panel:setLabel(new_updates_string) |
| 499 |
end |
| 500 |
|
| 501 |
function UIOptions:buttonFullscreen() |
| 502 |
if not self.ui:toggleFullscreen() then |
| 503 |
local err = {_S.errors.unavailable_screen_size} |
| 504 |
self.ui:addWindow(UIInformation(self.ui, err)) |
| 505 |
self.fullscreen_button:toggle() |
| 506 |
end |
| 507 |
self.fullscreen_panel:setLabel(self.ui.app.fullscreen and _S.options_window.option_on or _S.options_window.option_off) |
| 508 |
end |
| 509 |
|
| 510 |
function UIOptions:buttonMouseCapture() |
| 511 |
local app = self.ui.app |
| 512 |
app.config.capture_mouse = not app.config.capture_mouse |
| 513 |
app:saveConfig() |
| 514 |
self.mouse_capture_button:setLabel(app.config.capture_mouse and _S.options_window.option_on or _S.options_window.option_off) |
| 515 |
end |
| 516 |
|
| 517 |
function UIOptions:buttonCustomise() |
| 518 |
local window = UICustomise(self.ui, "menu") |
| 519 |
self.ui:addWindow(window) |
| 520 |
end |
| 521 |
|
| 522 |
function UIOptions:buttonFolder() |
| 523 |
local window = UIFolder(self.ui, "menu") |
| 524 |
self.ui:addWindow(window) |
| 525 |
end |
| 526 |
|
| 527 |
function UIOptions:buttonHotkey() |
| 528 |
local window = UIHotkeyAssign(self.ui, "menu") |
| 529 |
self.ui:addWindow(window) |
| 530 |
end |
| 531 |
|
| 532 |
function UIOptions:buttonSound() |
| 533 |
local window = UISoundSettings(self.ui, "menu") |
| 534 |
self.ui:addWindow(window) |
| 535 |
end |
| 536 |
|
| 537 |
function UIOptions:buttonScrollSpeed() |
| 538 |
local callback = function(scrollspeed_number) |
| 539 |
self.scrollspeed_panel : setLabel(tostring(scrollspeed_number)) |
| 540 |
self.scrollspeed_button : setToggleState(false) |
| 541 |
end |
| 542 |
|
| 543 |
self.ui:addWindow(UIScrollSpeed(self.ui, callback)) |
| 544 |
end |
| 545 |
|
| 546 |
function UIOptions:buttonBack() |
| 547 |
self:close() |
| 548 |
end |
| 549 |
|
| 550 |
function UIOptions:buttonShiftScrollSpeed() |
| 551 |
local callback = function(shift_scrollspeed_number) |
| 552 |
self.shift_scrollspeed_panel : setLabel( tostring(shift_scrollspeed_number) ) |
| 553 |
self.shift_scrollspeed_button : setToggleState(false) |
| 554 |
end |
| 555 |
|
| 556 |
self.ui:addWindow(UIShiftScrollSpeed(self.ui, callback)) |
| 557 |
end |
| 558 |
|
| 559 |
function UIOptions:buttonBack() |
| 560 |
self:close() |
| 561 |
end |
| 562 |
|
| 563 |
function UIOptions:buttonZoomSpeed() |
| 564 |
local callback = function(zoomspeed_number) |
| 565 |
self.zoomspeed_panel : setLabel( tostring(zoomspeed_number) ) |
| 566 |
self.zoomspeed_button : setToggleState(false) |
| 567 |
end |
| 568 |
|
| 569 |
self.ui:addWindow( UIZoomSpeed(self.ui, callback) ) |
| 570 |
end |
| 571 |
|
| 572 |
function UIOptions:onChangeResolution() |
| 573 |
self:processWindowResizeEvent() |
| 574 |
self:setDefaultPosition(0.5, 0.25) |
| 575 |
end |
| 576 |
|
| 577 |
-- Handle required button changes from a window resize event from the user (via UI |
| 578 |
-- or adjusting window boundaries) |
| 579 |
function UIOptions:processWindowResizeEvent() |
| 580 |
self:updateUIScaleAvailabilityState() |
| 581 |
self.resolution_panel:setLabel(self.ui.app.config.width .. "x" .. |
| 582 |
self.ui.app.config.height) |
| 583 |
end |
| 584 |
|
| 585 |
function UIOptions:close() |
| 586 |
UIResizable.close(self) |
| 587 |
if self.mode == "menu" then |
| 588 |
self.ui:addWindow(UIMainMenu(self.ui)) |
| 589 |
end |
| 590 |
end |
| 591 |
|
| 592 |
--! A custom resolution selection window |
| 593 |
class "UIResolution" (UIResizable) |
| 594 |
|
| 595 |
---@type UIResolution |
| 596 |
local UIResolution = _G["UIResolution"] |
| 597 |
|
| 598 |
function UIResolution:UIResolution(ui, callback) |
| 599 |
self:UIResizable(ui, 200, 140, col.bg) |
| 600 |
|
| 601 |
local app = ui.app |
| 602 |
self.modal_class = "resolution" |
| 603 |
self.on_top = true |
| 604 |
self.esc_closes = true |
| 605 |
self.resizable = false |
| 606 |
self:setDefaultPosition(0.5, 0.5) |
| 607 |
self.default_button_sound = "selectx.wav" |
| 608 |
|
| 609 |
self.callback = callback |
| 610 |
|
| 611 |
-- Window parts definition |
| 612 |
-- Title |
| 613 |
self:addBevelPanel(20, 10, 160, 20, col.title):setLabel(_S.options_window.resolution) |
| 614 |
.lowered = true |
| 615 |
|
| 616 |
-- Textboxes |
| 617 |
self:addBevelPanel(20, 40, 80, 20, col.caption, col.bg, col.bg):setLabel(_S.options_window.width) |
| 618 |
self.width_textbox = self:addBevelPanel(100, 40, 80, 20, col.textbox, col.bg, col.bg) |
| 619 |
:setTooltip(_S.tooltip.options_window.width) |
| 620 |
:makeTextbox():allowedInput("numbers"):characterLimit(4):setText(tostring(app.config.width)) |
| 621 |
|
| 622 |
self:addBevelPanel(20, 60, 80, 20, col.caption, col.bg, col.bg):setLabel(_S.options_window.height) |
| 623 |
self.height_textbox = self:addBevelPanel(100, 60, 80, 20, col.textbox, col.bg, col.bg) |
| 624 |
:setTooltip(_S.tooltip.options_window.height) |
| 625 |
:makeTextbox():allowedInput("numbers"):characterLimit(4):setText(tostring(app.config.height)) |
| 626 |
|
| 627 |
-- Apply and cancel |
| 628 |
self:addBevelPanel(20, 90, 80, 40, col.button):setLabel(_S.options_window.apply) |
| 629 |
:makeButton(0, 0, 80, 40, nil, self.ok):setTooltip(_S.tooltip.options_window.apply) |
| 630 |
self:addBevelPanel(100, 90, 80, 40, col.button):setLabel(_S.options_window.cancel) |
| 631 |
:makeButton(0, 0, 80, 40, nil, self.cancel):setTooltip(_S.tooltip.options_window.cancel) |
| 632 |
end |
| 633 |
|
| 634 |
function UIResolution:cancel() |
| 635 |
self:close(false) |
| 636 |
end |
| 637 |
|
| 638 |
function UIResolution:ok() |
| 639 |
local width, height = tonumber(self.width_textbox.text) or 0, tonumber(self.height_textbox.text) or 0 |
| 640 |
local s = TheApp.config.ui_scale |
| 641 |
local min_w = App.MIN_WINDOW_WIDTH * s |
| 642 |
local min_h = App.MIN_WINDOW_HEIGHT * s |
| 643 |
if width < min_w or height < min_h then |
| 644 |
local err = {_S.errors.minimum_screen_size:format(min_w, min_h)} |
| 645 |
self.ui:addWindow(UIInformation(self.ui, err)) |
| 646 |
elseif width > 3000 or height > 2000 then |
| 647 |
self.ui:addWindow(UIConfirmDialog(self.ui, false, |
| 648 |
_S.confirmation.maximum_screen_size, |
| 649 |
--[[persistable:maximum_screen_size_confirm_dialog]]function() |
| 650 |
self:close(true) |
| 651 |
self:close(false) |
| 652 |
end |
| 653 |
)) |
| 654 |
else |
| 655 |
self:close(true) |
| 656 |
end |
| 657 |
end |
| 658 |
|
| 659 |
function UIResolution:onMouseUp(button, x, y) |
| 660 |
if not self:hitTest(x, y) then |
| 661 |
self:close(false) |
| 662 |
end |
| 663 |
UIResizable.onMouseUp(self, button, x, y) |
| 664 |
end |
| 665 |
|
| 666 |
--! Closes the resolution dialog |
| 667 |
--!param ok (boolean or nil) whether the resolution entry was confirmed (true) or aborted (false) |
| 668 |
function UIResolution:close(ok) |
| 669 |
UIResizable.close(self) |
| 670 |
if ok and self.callback then |
| 671 |
self.callback(tonumber(self.width_textbox.text) or 0, tonumber(self.height_textbox.text) or 0) |
| 672 |
end |
| 673 |
end |
| 674 |
|
| 675 |
--! A window for setting the scroll speed of the camera. |
| 676 |
class "UIScrollSpeed" (UIResizable) |
| 677 |
|
| 678 |
---@type UIScrollSpeed |
| 679 |
local UIScrollSpeed = _G["UIScrollSpeed"] |
| 680 |
|
| 681 |
function UIScrollSpeed:UIScrollSpeed(ui, callback) |
| 682 |
self:UIResizable(ui, 200, 140, col.bg) |
| 683 |
|
| 684 |
self.on_top = true |
| 685 |
self.esc_closes = true |
| 686 |
self.resizable = false |
| 687 |
self:setDefaultPosition(0.5, 0.5) |
| 688 |
self.default_button_sound = "selectx.wav" |
| 689 |
self.scrollspeed_temp = 2 |
| 690 |
|
| 691 |
self.callback = callback |
| 692 |
|
| 693 |
self:addBevelPanel(20, 10, 160, 20, col.title):setLabel(_S.options_window.scrollspeed).lowered = true |
| 694 |
|
| 695 |
self:addBevelPanel(20, 50, 90, 20, col.caption, col.bg, col.bg):setLabel(_S.options_window.scrollspeed) |
| 696 |
-- |
| 697 |
self.scrollspeed_textbox = self:addBevelPanel(110, 50, 70, 20, col.textbox, col.bg, col.bg) |
| 698 |
:setTooltip(_S.tooltip.options_window.scrollspeed) |
| 699 |
:makeTextbox():allowedInput("numbers"):characterLimit(4):setText(tostring(self.ui.app.config.scroll_speed)) |
| 700 |
|
| 701 |
--Apply and cancel. |
| 702 |
self:addBevelPanel(20, 90, 80, 40, col.button):setLabel(_S.options_window.apply) |
| 703 |
:makeButton(0, 0, 80, 40, nil, self.ok):setTooltip(_S.tooltip.options_window.apply_scrollspeed) |
| 704 |
self:addBevelPanel(100, 90, 80, 40, col.button):setLabel(_S.options_window.cancel) |
| 705 |
:makeButton(0, 0, 80, 40, nil, self.cancel):setTooltip(_S.tooltip.options_window.cancel_scrollspeed) |
| 706 |
end |
| 707 |
|
| 708 |
function UIScrollSpeed:ok() |
| 709 |
self.scrollspeed_temp = tonumber(self.scrollspeed_textbox.text) or 2 |
| 710 |
|
| 711 |
if self.scrollspeed_temp < 1 then |
| 712 |
self.scrollspeed_temp = 1 |
| 713 |
elseif self.scrollspeed_temp > 10 then |
| 714 |
self.scrollspeed_temp = 10 |
| 715 |
end |
| 716 |
|
| 717 |
self:close(true) |
| 718 |
end |
| 719 |
|
| 720 |
function UIScrollSpeed:cancel() |
| 721 |
self:close(false) |
| 722 |
end |
| 723 |
|
| 724 |
function UIScrollSpeed:onMouseUp(button, x, y) |
| 725 |
if not self:hitTest(x, y) then |
| 726 |
self:close(false) |
| 727 |
end |
| 728 |
UIResizable.onMouseUp(self, button, x, y) |
| 729 |
end |
| 730 |
|
| 731 |
--!param ok (boolean or nil) whether the resolution entry was confirmed (true) or aborted (false) |
| 732 |
function UIScrollSpeed:close(ok) |
| 733 |
UIResizable.close(self) |
| 734 |
|
| 735 |
if ok then |
| 736 |
self.scrollspeed_textbox.text = self.scrollspeed_temp or 2 |
| 737 |
self.ui.app.config.scroll_speed = self.scrollspeed_textbox.text |
| 738 |
self.callback(self.scrollspeed_textbox.text) |
| 739 |
else |
| 740 |
self.callback(self.ui.app.config.scroll_speed) |
| 741 |
end |
| 742 |
end |
| 743 |
|
| 744 |
|
| 745 |
--! A window for setting the scroll speed of the camera while pressing the SHIFT key.. |
| 746 |
class "UIShiftScrollSpeed" (UIResizable) |
| 747 |
|
| 748 |
---@type UIShiftScrollSpeed |
| 749 |
local UIShiftScrollSpeed = _G["UIShiftScrollSpeed"] |
| 750 |
|
| 751 |
function UIShiftScrollSpeed:UIShiftScrollSpeed(ui, callback) |
| 752 |
self:UIResizable(ui, 200, 140, col.bg) |
| 753 |
|
| 754 |
self.on_top = true |
| 755 |
self.esc_closes = true |
| 756 |
self.resizable = false |
| 757 |
self:setDefaultPosition(0.5, 0.5) |
| 758 |
self.default_button_sound = "selectx.wav" |
| 759 |
self.shift_scrollspeed_temp = 4 |
| 760 |
|
| 761 |
self.callback = callback |
| 762 |
|
| 763 |
self:addBevelPanel(20, 10, 160, 20, col.title):setLabel(_S.options_window.shift_scrollspeed).lowered = true |
| 764 |
|
| 765 |
self:addBevelPanel(20, 50, 120, 20, col.caption, col.bg, col.bg):setLabel(_S.options_window.shift_scrollspeed) |
| 766 |
-- |
| 767 |
self.shift_scrollspeed_textbox = self:addBevelPanel(140, 50, 40, 20, col.textbox, col.bg, col.bg) |
| 768 |
:setTooltip(_S.tooltip.options_window.shift_scrollspeed) |
| 769 |
:makeTextbox():allowedInput("numbers"):characterLimit(4):setText(tostring(self.ui.app.config.shift_scroll_speed)) |
| 770 |
|
| 771 |
--Apply and cancel. |
| 772 |
self:addBevelPanel(20, 90, 80, 40, col.button):setLabel(_S.options_window.apply) |
| 773 |
:makeButton(0, 0, 80, 40, nil, self.ok):setTooltip(_S.tooltip.options_window.apply_shift_scrollspeed) |
| 774 |
self:addBevelPanel(100, 90, 80, 40, col.button):setLabel(_S.options_window.cancel) |
| 775 |
:makeButton(0, 0, 80, 40, nil, self.cancel):setTooltip(_S.tooltip.options_window.cancel_shift_scrollspeed) |
| 776 |
end |
| 777 |
|
| 778 |
function UIShiftScrollSpeed:ok() |
| 779 |
self.shift_scrollspeed_temp = tonumber(self.shift_scrollspeed_textbox.text) or 4 |
| 780 |
|
| 781 |
if self.shift_scrollspeed_temp < 1 then |
| 782 |
self.shift_scrollspeed_temp = 1 |
| 783 |
elseif self.shift_scrollspeed_temp > 10 then |
| 784 |
self.shift_scrollspeed_temp = 10 |
| 785 |
end |
| 786 |
|
| 787 |
self:close(true) |
| 788 |
end |
| 789 |
|
| 790 |
function UIShiftScrollSpeed:cancel() |
| 791 |
self:close(false) |
| 792 |
end |
| 793 |
|
| 794 |
function UIShiftScrollSpeed:onMouseUp(button, x, y) |
| 795 |
if not self:hitTest(x, y) then |
| 796 |
self:close(false) |
| 797 |
end |
| 798 |
UIResizable.onMouseUp(self, button, x, y) |
| 799 |
end |
| 800 |
|
| 801 |
--!param ok (boolean or nil) whether the resolution entry was confirmed (true) or aborted (false) |
| 802 |
function UIShiftScrollSpeed:close(ok) |
| 803 |
UIResizable.close(self) |
| 804 |
|
| 805 |
if ok then |
| 806 |
self.shift_scrollspeed_textbox.text = self.shift_scrollspeed_temp or 4 |
| 807 |
self.ui.app.config.shift_scroll_speed = self.shift_scrollspeed_textbox.text |
| 808 |
self.callback(self.shift_scrollspeed_textbox.text) |
| 809 |
else |
| 810 |
self.callback(self.ui.app.config.shift_scroll_speed) |
| 811 |
end |
| 812 |
end |
| 813 |
|
| 814 |
--! Window to set the zoom speed of the scroll wheel while in-game. |
| 815 |
class "UIZoomSpeed" (UIResizable) |
| 816 |
|
| 817 |
---@type UIZoomSpeed |
| 818 |
local UIZoomSpeed = _G["UIZoomSpeed"] |
| 819 |
|
| 820 |
function UIZoomSpeed:UIZoomSpeed(ui, callback) |
| 821 |
self:UIResizable(ui, 200, 140, col.bg) |
| 822 |
|
| 823 |
self.on_top = true |
| 824 |
self.esc_closes = true |
| 825 |
self.resizable = false |
| 826 |
self:setDefaultPosition(0.5, 0.5) |
| 827 |
self.default_button_sound = "selectx.wav" |
| 828 |
self.zoomspeed_temp = 80 |
| 829 |
|
| 830 |
self.callback = callback |
| 831 |
|
| 832 |
-- |
| 833 |
self:addBevelPanel(20, 10, 160, 20, col.title):setLabel(_S.options_window.zoom_speed).lowered = true |
| 834 |
|
| 835 |
-- |
| 836 |
self:addBevelPanel(20, 50, 90, 20, col.caption, col.bg, col.bg):setLabel(_S.options_window.zoom_speed) |
| 837 |
|
| 838 |
-- |
| 839 |
self.zoomspeed_textbox = self:addBevelPanel(110, 50, 70, 20, col.textbox, col.bg, col.bg) |
| 840 |
:setTooltip(_S.tooltip.options_window.zoom_speed) |
| 841 |
:makeTextbox():allowedInput("numbers"):characterLimit(4):setText( tostring(self.ui.app.config.zoom_speed) ) |
| 842 |
|
| 843 |
--Apply and cancel. |
| 844 |
self:addBevelPanel(20, 90, 80, 40, col.button):setLabel(_S.options_window.apply) |
| 845 |
:makeButton(0, 0, 80, 40, nil, self.ok):setTooltip(_S.tooltip.options_window.apply_zoomspeed) |
| 846 |
self:addBevelPanel(100, 90, 80, 40, col.button):setLabel(_S.options_window.cancel) |
| 847 |
:makeButton(0, 0, 80, 40, nil, self.cancel):setTooltip(_S.tooltip.options_window.cancel_zoomspeed) |
| 848 |
end |
| 849 |
|
| 850 |
function UIZoomSpeed:ok() |
| 851 |
self.zoomspeed_temp = tonumber( self.zoomspeed_textbox.text ) or 80 |
| 852 |
|
| 853 |
if self.zoomspeed_temp < 10 then |
| 854 |
self.zoomspeed_temp = 10 |
| 855 |
elseif self.zoomspeed_temp > 1000 then |
| 856 |
self.zoomspeed_temp = 1000 |
| 857 |
end |
| 858 |
|
| 859 |
self:close(true) |
| 860 |
end |
| 861 |
|
| 862 |
function UIZoomSpeed:cancel() |
| 863 |
self:close(false) |
| 864 |
end |
| 865 |
|
| 866 |
function UIZoomSpeed:onMouseUp(button, x, y) |
| 867 |
if not self:hitTest(x, y) then |
| 868 |
self:close(false) |
| 869 |
end |
| 870 |
UIResizable.onMouseUp(self, button, x, y) |
| 871 |
end |
| 872 |
|
| 873 |
--!param ok (boolean or nil) whether the resolution entry was confirmed (true) or aborted (false) |
| 874 |
function UIZoomSpeed:close(ok) |
| 875 |
UIResizable.close(self) |
| 876 |
|
| 877 |
if ok then |
| 878 |
self.zoomspeed_textbox.text = self.zoomspeed_temp or 2 |
| 879 |
self.ui.app.config.zoom_speed = self.zoomspeed_textbox.text |
| 880 |
self.callback(self.zoomspeed_textbox.text) |
| 881 |
else |
| 882 |
self.callback(self.ui.app.config.zoom_speed) |
| 883 |
end |
| 884 |
end |