ESO Lua File v100010

common/zo_options/gamepad/zo_options_gamepad.lua

[◄ back to folders ]
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
ZO_GamepadOptions = ZO_Object.MultiSubclass(ZO_SharedOptions, ZO_Gamepad_ParametricList_Screen)
function ZO_GamepadOptions:New(control)
    local options = ZO_Object.New(self)
    options:Initialize(control)
    return options
end
function ZO_GamepadOptions:Initialize(control) 
    ZO_SharedOptions.Initialize(self, control)
    ZO_Gamepad_ParametricList_Screen.Initialize(self, control, ZO_GAMEPAD_HEADER_TABBAR_DONT_CREATE)
    self.isGamepadOptions = true
    OPTIONS_MENU_FRAGMENT = ZO_SimpleSceneFragment:New(control)
    OPTIONS_MENU_FRAGMENT:SetHideOnSceneHidden(true)
    OPTIONS_MENU_FRAGMENT:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_SHOWING then
            self:PerformDeferredInitialization()
            self:RefreshOptionsList()
            self:SetCurrentList(self.optionsList)
            KEYBIND_STRIP:AddKeybindButtonGroup(self.rootKeybindDescriptor)
        elseif newState == SCENE_HIDDEN then
            self:DeactivateList()
            self:SaveCachedClientSettings()
            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.rootKeybindDescriptor)
            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.checkboxDescriptor)
        end
    end)
end
function ZO_GamepadOptions:DeactivateList()
    local selectedControl = self.optionsList:GetSelectedControl()
    if selectedControl then
        if selectedControl.slider then
            selectedControl.slider:Deactivate()
        elseif selectedControl.horizontalListObject then
            selectedControl.horizontalListObject:Deactivate()
        end
    end
    self.isPrimaryActionActive = false
end
function ZO_GamepadOptions:PerformDeferredInitialization()
    if self.rootKeybindDescriptor then return end
end
function ZO_GamepadOptions:InitializeHeader()
    local headerData =
    {
        titleText = GetString(SI_GAMEPAD_OPTIONS_MENU),           
    }
    ZO_GamepadGenericHeader_SetDataLayout(self.header, ZO_GAMEPAD_HEADER_LAYOUTS.DATA_PAIRS_TOGETHER)
    ZO_GamepadGenericHeader_RefreshData(self.header, headerData)
end
local INPUT_TYPE_PRIMARY = 1
local INPUT_TYPE_LEFT = 2
local INPUT_TYPE_RIGHT = 3
function ZO_GamepadOptions:Select(inputType)
    local control = self.optionsList:GetSelectedControl()
    local controlType = self:GetControlTypeFromControl(control)
    if controlType == OPTIONS_CHECKBOX then
        local checkbox = control:GetNamedChild("Checkbox")
        local checkboxState = checkbox:GetState()
        --if checkbox is on pressing left does nothing, likewise off and right, Primary always toggles
        if (inputType == INPUT_TYPE_LEFT and checkboxState == BSTATE_PRESSED) or 
            (inputType == INPUT_TYPE_RIGHT and checkboxState == BSTATE_NORMAL) then
            return
        end
        ZO_CheckButton_OnClicked(control:GetNamedChild("Checkbox"))
    elseif controlType == OPTIONS_INVOKE_CALLBACK then
    end
end
function ZO_GamepadOptions:InitializeKeybindStrip()
    self.rootKeybindDescriptor = 
    {
        {
            alignment = KEYBIND_STRIP_ALIGN_LEFT,
            name = GetString(SI_OPTIONS_DEFAULTS),
            keybind = "UI_SHORTCUT_SECONDARY",
            order = 1000,
            callback = function()
                if not self.inputBlocked then
                    ZO_Dialogs_ShowGamepadDialog("OPTIONS_RESET_TO_DEFAULTS")
                end
            end,
        },
    }
    self.checkboxDescriptor = 
    {
        {
            keybind = "UI_SHORTCUT_INPUT_LEFT",
            ethereal = true,
            callback = function() 
                if not self.inputBlocked then 
                    self:Select(INPUT_TYPE_LEFT) 
                end
            end,
        },
        {
            keybind = "UI_SHORTCUT_INPUT_RIGHT",
            ethereal = true,
            callback = function() 
                if not self.inputBlocked then
                    self:Select(INPUT_TYPE_RIGHT) 
                end
            end,
        },
        {
            alignment = KEYBIND_STRIP_ALIGN_LEFT,
            name = GetString(SI_GAMEPAD_SELECT_OPTION),
            keybind = "UI_SHORTCUT_PRIMARY",
            order = -500,
            callback = function() 
                if not self.inputBlocked then
                    self:Select(INPUT_TYPE_PRIMARY) 
                end
            end,
        },
    }
    local function OnBack()
        if not self.inputBlocked then
            SCENE_MANAGER:HideCurrentScene()
        end
    end
    ZO_Gamepad_AddBackNavigationKeybindDescriptors(self.rootKeybindDescriptor, GAME_NAVIGATION_TYPE_BUTTON, OnBack)
    ZO_Gamepad_AddListTriggerKeybindDescriptors(self.rootKeybindDescriptor, self.optionsList)
end
function ZO_GamepadOptions:HasInfoPanel()
    return OPTIONS_MENU_INFO_PANEL_FRAGMENT ~= nil
end
function ZO_GamepadOptions:InitializeGamepadInfoPanelTable()
    if not self:HasInfoPanel() then return end --no infopanel in pregame
    local showXbox = GetUIPlatform() ~= UI_PLATFORM_PS4
    local control = self.control:GetNamedChild("InfoPanel")
    ApplyTemplateToControl(control, showXbox and "ZO_GamepadOptionsInfoPanelXboxTemplate" or "ZO_GamepadOptionsInfoPanelPS4Template")
    self.keyCodeToTextControl = 
    {
        [KEY_GAMEPAD_BUTTON_1] = control:GetNamedChild("Button1"),
        [KEY_GAMEPAD_BUTTON_2] = control:GetNamedChild("Button2"),        
        [KEY_GAMEPAD_BUTTON_3] = control:GetNamedChild("Button3"),
        [KEY_GAMEPAD_BUTTON_4] = control:GetNamedChild("Button4"),
        [KEY_GAMEPAD_LEFT_TRIGGER] = control:GetNamedChild("LeftTrigger"),
        [KEY_GAMEPAD_RIGHT_TRIGGER] = control:GetNamedChild("RightTrigger"),
        [KEY_GAMEPAD_LEFT_SHOULDER] = control:GetNamedChild("LeftShoulder"),
        [KEY_GAMEPAD_RIGHT_SHOULDER] = control:GetNamedChild("RightShoulder"),
        [KEY_GAMEPAD_LEFT_STICK] = control:GetNamedChild("LeftStick"),
        [KEY_GAMEPAD_RIGHT_STICK] = control:GetNamedChild("RightStick"),
        [KEY_GAMEPAD_DPAD_UP] = control:GetNamedChild("DpadUp"),
        [KEY_GAMEPAD_DPAD_DOWN] = control:GetNamedChild("DpadDown"),
        [KEY_GAMEPAD_DPAD_LEFT] = control:GetNamedChild("DpadLeft"),
        [KEY_GAMEPAD_DPAD_RIGHT] = control:GetNamedChild("DpadRight"),
        [KEY_GAMEPAD_START] = control:GetNamedChild("Start"),
        [KEY_GAMEPAD_BACK] = control:GetNamedChild("Back"),
    }
    local centerParent = control:GetNamedChild("CenterParent")
    self.actionToLabelControl =
    {
        GAMEPAD_ACTION_BUTTON_8 = control:GetNamedChild("UltimateLabel"),
        SPECIAL_MOVE_INTERRUPT = control:GetNamedChild("BashLabel"),
        ROLL_DODGE = centerParent:GetNamedChild("RollDodgeLabel"),
        TOGGLE_MOUNT = centerParent:GetNamedChild("ToggleMountLabel"),
        USE_SYNERGY = centerParent:GetNamedChild("SynergyLabel"),
        SHEATHE_WEAPON_TOGGLE = centerParent:GetNamedChild("SheatheWeaponLabel"),
    }
end
    return left.text == right.text
end
function ZO_GamepadOptions_HorizontalListSetup(control, data, selected, reselectingDuringRebuild, enabled, selectedFromParent)
    control.label:SetText(data.text)
    
    local color = selectedFromParent and ZO_SELECTED_TEXT or ZO_DISABLED_TEXT
    control.label:SetColor(color:UnpackRGBA())
end
local GAMEPAD_OPTIONS_HEADER_SELECTED_PADDING = -20
function ZO_GamepadOptions:SetupList(list)
    local function OptionsSetup(control, data, selected, reselectingDuringRebuild, enabled, active)       
        control.data = data
        self:InitializeControl(control, selected)     
    end
    list:SetHeaderPadding(GAMEPAD_HEADER_DEFAULT_PADDING, GAMEPAD_OPTIONS_HEADER_SELECTED_PADDING)
    list:AddDataTemplate("ZO_GamepadOptionsSliderRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction)    
    list:AddDataTemplateWithHeader("ZO_GamepadOptionsSliderRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction, nil, "ZO_GamepadOptionsHeaderTemplate")
    list:AddDataTemplate("ZO_GamepadOptionsCheckboxRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction)    
    list:AddDataTemplateWithHeader("ZO_GamepadOptionsCheckboxRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction, nil, "ZO_GamepadOptionsHeaderTemplate")
    list:AddDataTemplate("ZO_GamepadOptionsHorizontalListRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction)    
    list:AddDataTemplateWithHeader("ZO_GamepadOptionsHorizontalListRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction, nil, "ZO_GamepadOptionsHeaderTemplate")
    list:AddDataTemplateWithHeader("ZO_GamepadOptionsLabelRow", OptionsSetup, ZO_GamepadMenuEntryTemplateParametricListFunction, nil, "ZO_GamepadOptionsHeaderTemplate")
end
function ZO_GamepadOptions:InitializeOptionsList()
    self.optionsList = self:GetMainList()
end
function ZO_GamepadOptions:OnSelectionChanged(list)
    local control = list:GetSelectedControl()
    local controlType = self:GetControlTypeFromControl(control)
    if controlType == OPTIONS_CHECKBOX or controlType == OPTIONS_INVOKE_CALLBACK then
        if not self.isPrimaryActionActive then 
            KEYBIND_STRIP:AddKeybindButtonGroup(self.checkboxDescriptor)
            self.isPrimaryActionActive = true
        end
    else
        KEYBIND_STRIP:RemoveKeybindButtonGroup(self.checkboxDescriptor)
        self.isPrimaryActionActive = false
    end
end
    
    local color = selected and ZO_SELECTED_TEXT or ZO_DISABLED_TEXT
    local r,g,b,a = color:UnpackRGBA()
    local label = GetControl(control, "Name")
    SetMenuEntryFontFace(label, selected)
    local slider = GetControl(control, "Slider")
    if slider then
        slider:SetColor(r,g,b,a)
        slider:GetNamedChild("Left"):SetColor(r,g,b,a)
        slider:GetNamedChild("Right"):SetColor(r,g,b,a)
        slider:GetNamedChild("Center"):SetColor(r,g,b,a)
    end
    local checkBox = GetControl(control, "Checkbox")
    if checkBox then
        checkBox.selected = selected  
    end
    if control.horizontalListControl then
        control.horizontalListObject:SetSelectedFromParent(selected)
    end
end
function ZO_GamepadOptions:InitializeControl(control, selected)
    local label = GetControl(control, "Name")
    ZO_SharedOptions.InitializeControl(self, control, selected)
    control.data.isAnOption = IsControlAnOption(control)
    if control.data.gamepadTextOverride then
        local text
        if type(control.data.gamepadTextOverride) == "string" then
            text = control.data.gamepadTextOverride
        else
            text = GetString(control.data.gamepadTextOverride)
        end
        label:SetText(text)
    end
end
local GAMEPAD_BUTTON_SCALE = .75
function ZO_GamepadOptions:RefreshGamepadInfoPanel()
    if not self:HasInfoPanel() then return end --no infopanel in pregame
    local generalLayer = GetString(SI_KEYBINDINGS_LAYER_GENERAL)
    for key, control in pairs(self.keyCodeToTextControl) do
        local actionName
        if GetUIPlatform() == UI_PLATFORM_PS4 and key == KEY_GAMEPAD_BACK then
            actionName = GetActionNameFromKey(generalLayer, KEY_GAMEPAD_TOUCHPAD_PRESSED)    -- map on PS4 is Touchpad
        else
            actionName = GetActionNameFromKey(generalLayer, key)
        end
        local localizedActionName = ZO_Keybinding_GetGamepadActionName(actionName)
        control:SetText(localizedActionName)
    end
    for actionName, control in pairs(self.actionToLabelControl) do
        local icon, width, height, isHoldKey = self:GetButtonTextureInfoFromActionName(actionName)
        if icon then    -- temp check until art assets are made for every key
            local iconControl = control:GetNamedChild("Icon")
            iconControl:SetTexture(icon)  
            iconControl:SetDimensions(width * GAMEPAD_BUTTON_SCALE, height * GAMEPAD_BUTTON_SCALE)
            local localizedActionName = ZO_Keybinding_GetGamepadActionName(actionName)
            control:SetText(localizedActionName)
            local holdLabel = control:GetNamedChild("HoldLabel")
            if holdLabel then
                holdLabel:SetHidden(not isHoldKey)
            end
        end
    end          
end
function ZO_GamepadOptions:GetButtonTextureInfoFromActionName(actionName)
    local layerIndex, categoryIndex, actionIndex = GetActionIndicesFromName(actionName)
    for bindingIndex = 1, GetMaxBindingsPerAction() do
        local key, mod1, mod2, mod3, mod4 = GetActionBindingInfo(layerIndex, categoryIndex, actionIndex, bindingIndex)
        if key ~= KEY_INVALID then                  
            -- If the key matches the preferred mode then just use it
            if IsKeyCodeGamepadKey(key) then
                local icon, width, height = GetGamepadIconPathForKeyCode(key)
                local isKeyHold = IsKeyCodeHoldKey(key)
                return icon, width, height, isKeyHold
            end
        end
    end
end
    GAMEPAD_OPTIONS:RefreshGamepadInfoPanel()
end
    GAMEPAD_OPTIONS = ZO_GamepadOptions:New(control)
    SYSTEMS:RegisterGamepadObject("options", GAMEPAD_OPTIONS)
end
local TEMPLATE_NAMES = 
{
    [OPTIONS_HORIZONTAL_SCROLL_LIST] = "ZO_GamepadOptionsHorizontalListRow",
    [OPTIONS_SLIDER] = "ZO_GamepadOptionsSliderRow",
    [OPTIONS_CHECKBOX] = "ZO_GamepadOptionsCheckboxRow",
    [OPTIONS_INVOKE_CALLBACK] = "ZO_GamepadOptionsLabelRow"
}
function ZO_GamepadOptions:RefreshOptionsList()
    self.optionsList:Clear()
    
    self:AddSettingGroup(SETTING_PANEL_GAMEPAD_PREGAME)
    self:AddSettingGroup(SETTING_PANEL_VIDEO)
    self:AddSettingGroup(SETTING_PANEL_CAMERA)
    self:AddSettingGroup(SETTING_PANEL_GAMEPLAY)
    self:AddSettingGroup(SETTING_PANEL_INTERFACE)
    self:AddSettingGroup(SETTING_PANEL_AUDIO)
    self.optionsList:Commit()
end
function ZO_GamepadOptions:AddSettingGroup(panelId)
    local settings = GAMEPAD_SETTINGS_DATA[panelId]
    if settings then
        local header = GetString("SI_SETTINGSYSTEMPANEL", panelId)
        for i = 1, #settings do
            local setting = settings[i]
            local data = self:GetSettingsData(setting.panel, setting.system, setting.settingId)
            local controlType = self:GetControlType(data.controlType)
            local templateName = TEMPLATE_NAMES[controlType]
            
            local isHeader = header or data.header
            
            if isHeader then 
                templateName = templateName .. "WithHeader"
                if not data.header then 
                    data.header = header 
                end
                header = nil
            end
            self.optionsList:AddEntry(templateName, data)
        end
    end
end
function ZO_GamepadOptions:LoadDefaults() 
    for panel, settings in pairs(GAMEPAD_SETTINGS_DATA) do
        for i = 1, #settings do
            local setting = settings[i]
            local data = self:GetSettingsData(setting.panel, setting.system, setting.settingId)
            ZO_SharedOptions.LoadDefaults(self, data)
        end
    end
end
function ZO_GamepadOptions:SetGamepadOptionsInputBlocked(blocked)
    self.inputBlocked = blocked
end