forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.lua
More file actions
509 lines (427 loc) · 19.7 KB
/
options.lua
File metadata and controls
509 lines (427 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
--*****************************************************************************
--* File: lua/modules/ui/dialogs/options.lua
--* Author: Chris Blackwell
--* Summary: Manages the options dialog
--*
--* Copyright © 2006 Gas Powered Games, Inc. All rights reserved.
--*****************************************************************************
local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
local Text = import('/lua/maui/text.lua').Text
local Button = import('/lua/maui/button.lua').Button
local MenuCommon = import('/lua/ui/menus/menucommon.lua')
local Group = import('/lua/maui/group.lua').Group
local Grid = import('/lua/maui/grid.lua').Grid
local Slider = import('/lua/maui/slider.lua').Slider
local Combo = import('/lua/ui/controls/combo.lua').Combo
local IntegerSlider = import('/lua/maui/slider.lua').IntegerSlider
local OptionsLogic = import('/lua/options/optionslogic.lua')
local Tooltip = import('/lua/ui/game/tooltip.lua')
-- this will hold the working set of options, which won't be valid until applied
local currentOptionsSet = false
local currentTabButton = false
local currentTabBitmap = false
-- contains a map of current option controls keyed by their option keys
local optionKeyToControlMap = false
-- this table is keyed with the different types of controls that can be created
-- each key's value is the function that actually creates the type
-- the signature of the function is: fucntion(parent, optionItemData) and should return it's base control
-- note that each control should create a change function that allows the control to have its value changed
-- not that each control should create a SetCustomData(newCustomData, newDefault) function that will initialize the control with new custom data
local controlTypeCreate = {
toggle = function(parent, optionItemData)
local combo = Combo(parent, 14, 10, nil, nil, "UI_Tab_Click_01", "UI_Tab_Rollover_01")
LayoutHelpers.SetWidth(combo, 250)
combo.SetCustomData = function(newCustomData, newDefault)
local itemArray = {}
local default = 1
local matchedCurrentValue = false
combo:ClearItems()
combo.keyMap = {}
for index, val in newCustomData.states do
if currentOptionsSet[optionItemData.key] == val.key then
default = index
matchedCurrentValue = true
end
itemArray[index] = val.text
combo.keyMap[index] = val.key
end
combo.Key = newDefault
combo:AddItems(itemArray, default)
if table.getsize(itemArray) == 1 then
combo:Disable()
else
combo:Enable()
end
-- if we didn't find a match for our current value, we need to set the value to default
if not matchedCurrentValue then
currentOptionsSet[optionItemData.key] = newDefault
end
end
combo.SetCustomData(optionItemData.custom, optionItemData.default)
combo.OnClick = function(self, index, text, skipUpdate)
self.Key = index
currentOptionsSet[optionItemData.key] = combo.keyMap[index]
if optionItemData.update and not skipUpdate then
optionItemData.update(self,combo.keyMap[index])
end
end
combo.OnDestroy = function(self)
optionItemData.control = nil
optionItemData.change = nil
end
optionItemData.control = combo
optionItemData.change = function(control, value, skipUpdate)
-- find key in control
for index, key in control.keyMap do
if key == value then
-- don't do anything if we're already set to this key
if control:GetItem() ~= index then
control:SetItem(index)
control:OnClick(index, nil, skipUpdate)
return
end
end
end
end
return combo
end,
button = function(parent, optionItemData)
local bg = Bitmap(parent, UIUtil.SkinnableFile('/dialogs/options-02/content-btn-line_bmp.dds'))
bg._button = UIUtil.CreateButtonStd(bg, '/dialogs/standard-small_btn/standard-small', optionItemData.custom.text, 12, 2, 0, "UI_Opt_Mini_Button_Click", "UI_Opt_Mini_Button_Over")
LayoutHelpers.AtCenterIn(bg._button, bg)
bg._button.OnClick = function(self, modifiers)
if optionItemData.update then
optionItemData.update(self, 0)
end
end
optionItemData.control = bg
optionItemData.change = function(control, value)
if optionItemData.update then
optionItemData.update(control, value)
end
end
bg.OnDestroy = function(self)
optionItemData.control = nil
optionItemData.change = nil
end
bg.SetCustomData = function(newCustomData, newDefault)
bg._button.label:SetText(newCustomData)
end
return bg
end,
slider = function(parent, optionItemData)
local sliderGroup = Group(parent)
sliderGroup.Width:Set(parent.Width)
sliderGroup.Height:Set(parent.Height)
sliderGroup._slider = false
if optionItemData.custom.inc == 0 then
sliderGroup._slider = Slider(sliderGroup, false, optionItemData.custom.min, optionItemData.custom.max, UIUtil.SkinnableFile('/slider02/slider_btn_up.dds'), UIUtil.SkinnableFile('/slider02/slider_btn_over.dds'), UIUtil.SkinnableFile('/slider02/slider_btn_down.dds'), UIUtil.SkinnableFile('/slider02/slider-back_bmp.dds'))
else
sliderGroup._slider = IntegerSlider(sliderGroup, false, optionItemData.custom.min, optionItemData.custom.max, optionItemData.custom.inc, UIUtil.SkinnableFile('/slider02/slider_btn_up.dds'), UIUtil.SkinnableFile('/slider02/slider_btn_over.dds'), UIUtil.SkinnableFile('/slider02/slider_btn_down.dds'), UIUtil.SkinnableFile('/dialogs/options-02/slider-back_bmp.dds'))
end
LayoutHelpers.AtLeftTopIn(sliderGroup._slider, sliderGroup)
sliderGroup._value = UIUtil.CreateText(sliderGroup, "", 12)
LayoutHelpers.RightOf(sliderGroup._value, sliderGroup._slider)
sliderGroup._slider.OnValueChanged = function(self, newValue)
sliderGroup._value:SetText(math.floor(tostring(newValue)))
end
sliderGroup._slider.OnValueSet = function(self, newValue)
if optionItemData.update then
optionItemData.update(self, newValue)
end
currentOptionsSet[optionItemData.key] = newValue
end
sliderGroup._slider.OnBeginChange = function(self)
if optionItemData.beginChange then
optionItemData.beginChange(self)
end
end
sliderGroup._slider.OnEndChange = function(self)
if optionItemData.endChange then
optionItemData.endChange(self)
end
end
sliderGroup._slider.OnScrub = function(self,value)
if optionItemData.update then
optionItemData.update(self,value)
end
end
optionItemData.control = sliderGroup._slider
optionItemData.change = function(control, value, skipUpdate)
if not skipUpdate then
control:SetValue(value)
end
end
sliderGroup.OnDestroy = function(self)
optionItemData.control = nil
optionItemData.change = nil
end
-- set initial value
sliderGroup._slider:SetValue(currentOptionsSet[optionItemData.key])
sliderGroup.SetCustomData = function(newCustomData, newDefault)
-- this isn't really correct as it should check the indent, and recreate the control if needed
-- and set the indent (which isn't exposed in slider, doh!) but this isn't really used
-- at this point, so it's not worth putting work in to
sliderGroup._slider:SetStartValue(newCustomData.min)
sliderGroup._slider:SetEndValue(newCustomData.max)
end
return sliderGroup
end,
}
local function CreateOption(parent, optionItemData)
local bg = Bitmap(parent, UIUtil.SkinnableFile('/dialogs/options-02/content-box_bmp.dds'))
bg._label = UIUtil.CreateText(bg, optionItemData.title, 16, UIUtil.bodyFont)
LayoutHelpers.AtLeftTopIn(bg._label, bg, 9, 6)
bg._label._tipText = optionItemData.key
bg._label.HandleEvent = function(self, event)
if event.Type == 'MouseEnter' then
Tooltip.CreateMouseoverDisplay(self, "options_" .. bg._label._tipText, .5, true)
elseif event.Type == 'MouseExit' then
Tooltip.DestroyMouseoverDisplay()
end
end
-- this is here to help position the control
--TODO get this data from layout!
local controlGroup = Group(bg)
LayoutHelpers.AtLeftTopIn(controlGroup, bg, 338, 5)
LayoutHelpers.SetDimensions(controlGroup, 252, 24)
if controlTypeCreate[optionItemData.type] then
bg._control = controlTypeCreate[optionItemData.type](controlGroup, optionItemData)
else
LOG("Warning: Option item data [" .. optionItemData.key .. "] contains an unknown control type: " .. optionItemData.type .. ". Valid types are")
for k,v in controlTypeCreate do
LOG(k)
end
end
if bg._control then
LayoutHelpers.AtCenterIn(bg._control, controlGroup)
end
optionKeyToControlMap[optionItemData.key] = bg._control
return bg
end
local dialog = false
function CreateDialog(over, exitBehavior)
currentOptionsSet = OptionsLogic.GetCurrent()
local parent = false
-- lots of state
local function KillDialog()
currentTabButton = false
currentTabBitmap = false
OptionsLogic.SetCustomDataChangedCallback(nil)
OptionsLogic.SetSummonRestartDialogCallback(nil)
OptionsLogic.SetSummonVerifyDialogCallback(nil)
OptionsLogic.Repopulate()
if over then
dialog:Destroy()
else
parent:Destroy()
end
end
if over then
parent = over
else
parent = UIUtil.CreateScreenGroup(GetFrame(0), "Options ScreenGroup")
local background = MenuCommon.SetupBackground(GetFrame(0))
end
dialog = Bitmap(parent, UIUtil.UIFile('/scx_menu/options/panel_bmp.dds'))
LayoutHelpers.AtCenterIn(dialog, parent)
dialog.brackets = UIUtil.CreateDialogBrackets(dialog, 41, 24, 41, 24)
local title = UIUtil.CreateText(dialog, "<LOC _Options>", 24, UIUtil.titleFont)
LayoutHelpers.AtTopIn(title, dialog, 30)
LayoutHelpers.AtHorizontalCenterIn(title, dialog)
if over then
dialog.Depth:Set(GetFrame(over:GetRootFrame():GetTargetHead()):GetTopmostDepth() + 1)
end
local function roHandler(self, event)
if event == 'enter' then
self.label:SetColor('ff000000')
elseif event == 'exit' then
self.label:SetColor(UIUtil.fontColor)
end
end
-- layout buttons
local applyBtn = Button(dialog,
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_up.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_down.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_over.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_dis.dds'),
"UI_Opt_Yes_No", "UI_Opt_Affirm_Over")
LayoutHelpers.AtRightTopIn(applyBtn, dialog, 15, 515)
applyBtn.label = UIUtil.CreateText(applyBtn, LOC("<LOC _Apply>"), 16)
LayoutHelpers.AtCenterIn(applyBtn.label, applyBtn)
Tooltip.AddButtonTooltip(applyBtn, 'options_tab_apply')
applyBtn.OnRolloverEvent = roHandler
dialog.cancelBtn = Button(dialog,
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_up.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_down.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_over.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_dis.dds'),
"UI_Opt_Yes_No", "UI_Opt_Affirm_Over")
dialog.cancelBtn.label = UIUtil.CreateText(dialog.cancelBtn, LOC("<LOC _Cancel>"), 16)
LayoutHelpers.AtCenterIn(dialog.cancelBtn.label, dialog.cancelBtn)
LayoutHelpers.LeftOf(dialog.cancelBtn, applyBtn, -6)
dialog.cancelBtn.OnRolloverEvent = roHandler
local okBtn = Button(dialog,
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_up.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_down.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_over.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_dis.dds'),
"UI_Opt_Yes_No", "UI_Opt_Affirm_Over")
okBtn.label = UIUtil.CreateText(okBtn, LOC("<LOC _Ok>"), 16)
LayoutHelpers.AtCenterIn(okBtn.label, okBtn)
LayoutHelpers.LeftOf(okBtn, dialog.cancelBtn, -6)
okBtn.OnRolloverEvent = roHandler
local resetBtn = Button(dialog,
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_up.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_down.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_over.dds'),
UIUtil.UIFile('/scx_menu/small-short-btn/small-btn_dis.dds'),
"UI_Opt_Yes_No", "UI_Opt_Affirm_Over")
resetBtn.label = UIUtil.CreateText(resetBtn, LOC("<LOC _Reset>"), 16)
LayoutHelpers.AtCenterIn(resetBtn.label, resetBtn)
LayoutHelpers.LeftOf(resetBtn, okBtn, -6)
Tooltip.AddButtonTooltip(resetBtn, 'options_reset_all')
resetBtn.OnRolloverEvent = roHandler
-- set up button logic
okBtn.OnClick = function(self, modifiers)
OptionsLogic.SetCurrent(currentOptionsSet)
KillDialog()
if exitBehavior then exitBehavior() end
end
dialog.cancelBtn.OnClick = function(self, modifiers)
if currentTabButton then
for index,option in currentTabButton.tabData.items do
if option.cancel then
option.cancel()
end
end
end
KillDialog()
if exitBehavior then exitBehavior() end
end
applyBtn.OnClick = function(self, modifiers)
OptionsLogic.SetCurrent(currentOptionsSet)
end
resetBtn.OnClick = function(self, modifiers)
local function DoReset()
OptionsLogic.ResetToDefaults()
-- creating the dialog will reload the old options without saving the new ones and will reset all the controls
KillDialog()
if exitBehavior then exitBehavior() end
end
UIUtil.QuickDialog(dialog, "<LOC options_0002>Are you sure you want to reset to default values?",
"<LOC _Yes>", DoReset,
"<LOC _No>", nil,
nil, nil,
true,
{escapeButton = 2, enterButton = 1, worldCover = false})
end
UIUtil.MakeInputModal(dialog, function() okBtn.OnClick(okBtn) end, function() dialog.cancelBtn.OnClick(dialog.cancelBtn) end)
-- set up option grid
local elementWidth, elementHeight = GetTextureDimensions(UIUtil.UIFile('/dialogs/options-02/content-box_bmp.dds'))
local optionGrid = Grid(dialog, elementWidth, elementHeight)
LayoutHelpers.RelativeTo(optionGrid, dialog, UIUtil.SkinnableFile('/dialogs/options-02/options-02_layout.lua'), 'gameplay_bmp', 'panel_bmp')
LayoutHelpers.AtRightBottomIn(optionGrid, dialog, 43, 100)
LayoutHelpers.DimensionsRelativeTo(optionGrid, UIUtil.SkinnableFile('/dialogs/options-02/options-02_layout.lua'), 'gameplay_bmp')
local scrollbar = UIUtil.CreateVertScrollbarFor(optionGrid, -4)
-- set up a page
function SetNewPage(tabControl)
-- kill any other page
if currentTabBitmap then
currentTabBitmap:Destroy()
currentTabBitmap = false
currentTabButton:Show()
end
-- store the tab data for this tab for easy access
local tabData = tabControl.tabData
-- show the "selected" state of the tab, which hides the button and shows a bitmap
currentTabButton = tabControl
currentTabButton:Hide()
currentTabBitmap = Bitmap(dialog, UIUtil.SkinnableFile('/scx_menu/tab_btn/tab_btn_selected.dds'))
LayoutHelpers.AtCenterIn(currentTabBitmap, currentTabButton)
local tabLabel = UIUtil.CreateText(currentTabBitmap, tabData.title, 16, UIUtil.titleFont)
LayoutHelpers.AtCenterIn(tabLabel, currentTabBitmap)
-- remove controls and populate grid
optionGrid:DeleteAndDestroyAll(true)
optionGrid:AppendCols(1, true)
-- initialzie key to control map each time page is changed, as controls get destroyed
optionKeyToControlMap = {}
for index, option in tabData.items do
optionGrid:AppendRows(1, true)
local optCtrl = CreateOption(optionGrid, option)
optionGrid:SetItem(optCtrl, 1, index, true)
if option.init then
option.init()
end
end
optionGrid:EndBatch()
end
-- tab layout
local prev = false
local defaultTab = false
-- get the tab data
local options = import('/lua/options/options.lua').options
local optionsOrder = import('/lua/options/options.lua').optionsOrder
for index, key in optionsOrder do
tabData = options[key]
local curButton = UIUtil.CreateButtonStd(dialog, '/scx_menu/tab_btn/tab', tabData.title, 16, 0, 0, "UI_Tab_Click_01", "UI_Tab_Rollover_01")
curButton.label:SetDropShadow(true)
if prev then
LayoutHelpers.RightOf(curButton, prev, -10)
else
LayoutHelpers.AtLeftTopIn(curButton, dialog, 10, 64)
defaultTab = curButton
end
prev = curButton
curButton.OnClick = function(self, modifiers)
SetNewPage(self)
end
curButton.tabData = tabData
end
SetNewPage(defaultTab)
if not optionGrid:IsScrollable("Vert") then
scrollbar:Hide()
end
OptionsLogic.SetCustomDataChangedCallback(function(optionKey, newCustomData, newDefault)
if optionKeyToControlMap and optionKeyToControlMap[optionKey] then
optionKeyToControlMap[optionKey].SetCustomData(newCustomData, newDefault)
end
end)
local function OptionRestartFunc(proceedFunc, cancelFunc)
UIUtil.QuickDialog(GetFrame(0) , "<LOC options_0001>You have modified an option which requires you to restart Forged Alliance. Selecting OK will exit the game, selecting Cancel will revert the option to its prior setting."
, "<LOC _OK>", proceedFunc
,"<LOC _Cancel>", cancelFunc
, nil, nil
, true
, {escapeButton = 2, enterButton = 1, worldCover = false}
)
end
OptionsLogic.SetSummonRestartDialogCallback(OptionRestartFunc)
local function VerifyFunc(undoFunc)
local secondsToWait = 15
local thread
local dlg = UIUtil.QuickDialog(GetFrame(0), "<LOC options_0003>Click OK to accept these settings."
, LOC("<LOC _Ok>") .. " [" .. secondsToWait .. "]", function() KillThread(thread) end
, "<LOC _Cancel>", function() KillThread(thread) undoFunc() end
, nil, nil
, true
, {escapeButton = 2, enterButton = 1, worldCover = false}
)
thread = ForkThread(function()
for sec = 1, secondsToWait do
WaitSeconds(1)
dlg.content._button1.label:SetText(LOC("<LOC _Ok>") .. " [" .. (secondsToWait - sec) .. "]")
end
dlg:Destroy()
undoFunc()
end)
end
OptionsLogic.SetSummonVerifyDialogCallback(VerifyFunc)
end
function OnNISBegin()
if dialog then
dialog.cancelBtn:OnClick()
end
end