ESO Lua File v100010

libraries/zo_dialog/gamepad/zo_genericdialog_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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
----------------------
-- Helper Functions --
----------------------
local DialogKeybindStripDescriptor = ZO_Object:Subclass()
function DialogKeybindStripDescriptor:New()
    local obj = ZO_Object.New(self)
    obj:Initialize()
    return obj
end
function DialogKeybindStripDescriptor:Initialize()
    -- dialog buttons expect the dialog to be the callbackArg
    self.callback = function()
        if self.buttonCallback then
            self.buttonCallback(self.dialog)
        end
    end
    self:Reset()
end
function DialogKeybindStripDescriptor:Reset()
    self.visible = nil
    self.buttonCallback = nil
    self.name = nil
    self.dialog = nil
    self.keybind = nil
    self.sound = nil
    self.alignment = KEYBIND_STRIP_ALIGN_LEFT
end
function DialogKeybindStripDescriptor:SetAlignment(alignment)
    if alignment then
        self.alignment = alignment
    end
end
function DialogKeybindStripDescriptor:SetButtonCallback(buttonCallback)
end
-- keybinds expect text to be under 'name' and already looked up.
function DialogKeybindStripDescriptor:SetText(text)
    self.name = text
    if(type(self.name) == "number") then
        self.name = GetString(self.name)
    end
end
function DialogKeybindStripDescriptor:SetDialog(dialog)
    self.dialog = dialog
end
function DialogKeybindStripDescriptor:SetVisible(visible)
    self.visible = visible
end
function DialogKeybindStripDescriptor:SetKeybind(keybind, index)
    if keybind then
        self.keybind = keybind
    else
        if index == 1 then
            self.keybind = "DIALOG_PRIMARY"
        elseif index == 2 then
            self.keybind = "DIALOG_NEGATIVE"
        end
    end
end
function DialogKeybindStripDescriptor:SetSound(sound)
    self.sound = sound
end
function DialogKeybindStripDescriptor:SetDefaultSoundFromKeybind(isBasic)
    if isBasic then
        if self.keybind == "DIALOG_PRIMARY" then
            self.sound = SOUNDS.DIALOG_ACCEPT
        elseif self.keybind == "DIALOG_NEGATIVE" then
            self.sound = SOUNDS.DIALOG_DECLINE
        end
    end
end
local function MakeKeybindStripDescriptor(pool)
    local descriptor = DialogKeybindStripDescriptor:New()
    return descriptor
end
local function ResetKeybindStripDescriptor(descriptor)
    descriptor:Reset()
end
local g_keybindStripDescriptors = ZO_ObjectPool:New(MakeKeybindStripDescriptor, ResetKeybindStripDescriptor)
local g_keybindGroupDesc = {}
local function TryShowKeybind(dialog, data)
    ZO_ClearNumericallyIndexedTable(g_keybindGroupDesc)
    local buttons = dialog.info.buttons
    local numButtons = buttons and #buttons or 0
    local dialogType = dialog.info.gamepadInfo.dialogType
    if numButtons > 0 then
        for index, value in ipairs(buttons) do
            if(type(value) == "table") then
                local keybindDesc = g_keybindStripDescriptors:AcquireObject()
                if(dialog.textParams ~= nil and dialog.textParams.buttonTextOverrides ~= nil and dialog.textParams.buttonTextOverrides[index] ~= nil) then
                    keybindDesc:SetText(dialog.textParams.buttonTextOverrides[index])
                else
                    keybindDesc:SetText(value.text)
                end
                if(dialog.textParams ~= nil and dialog.textParams.buttonKeybindOverrides ~= nil and dialog.textParams.buttonKeybindOverrides[index] ~= nil) then
                    keybindDesc:SetKeybind(dialog.textParams.buttonKeybindOverrides[index])
                else
                    keybindDesc:SetKeybind(value.keybind, index)
                end
                
                keybindDesc:SetDialog(dialog)
                keybindDesc:SetAlignment(buttons.alignment)
                keybindDesc:SetButtonCallback(value.callback)
                keybindDesc:SetVisible(value.visible)
                if value.clickSound then
                    keybindDesc:SetSound(value.clickSound)
                else
                    keybindDesc:SetDefaultSoundFromKeybind((dialogType == GAMEPAD_DIALOGS.BASIC) and (numButtons > 1))
                end
                table.insert(g_keybindGroupDesc, keybindDesc)
            end
        end
    end
    if(#g_keybindGroupDesc > 0) then
        KEYBIND_STRIP:AddKeybindButtonGroup(g_keybindGroupDesc)
    end
end
local function TryRemoveKeybind()
    if(#g_keybindGroupDesc > 0) then
        KEYBIND_STRIP:RemoveKeybindButtonGroup(g_keybindGroupDesc)
    end
end
    DIRECTIONAL_INPUT:Consume(ZO_DI_LEFT_STICK, ZO_DI_DPAD)
end
----------------------------
-- Global Gamepad Dialogs --
----------------------------
GAMEPAD_DIALOGS = {
    BASIC = 1,
    PARAMETRIC = 2,
    COOLDOWN = 3,
    TRANSACTION = 4
}
function ZO_GenericGamepadDialog_GetControl(dialogType)
    if(dialogType == GAMEPAD_DIALOGS.BASIC) then
        return ZO_GamepadDialogBase
    elseif(dialogType == GAMEPAD_DIALOGS.PARAMETRIC) then
        return ZO_GamepadDialogPara
    elseif(dialogType == GAMEPAD_DIALOGS.COOLDOWN) then
        return ZO_GamepadDialogCool
    elseif(dialogType == GAMEPAD_DIALOGS.TRANSACTION) then
        return ZO_GamepadDialogTransaction
    end
    return nil
end
-----------------------
-- Dialog Management --
-----------------------
local function OnDialogShowing(dialog)
    PushActionLayerByName(GetString(SI_KEYBINDINGS_LAYER_DIALOG))
    dialog:Activate()
    KEYBIND_STRIP:PushKeybindGroupState()
    KEYBIND_STRIP:SetDrawOrder(DT_HIGH, DL_BACKGROUND)
    if(dialog.shouldShowTooltip) then
        ZO_GenericGamepadDialog_ShowTooltip(dialog)
    end
    SCENE_MANAGER:AddFragment(GAMEPAD_DIALOG_BACKGROUND_FADE_FRAGMENT)
    dialog.hideSceneOnClose = false
end
local function OnDialogShown(dialog)
    TryShowKeybind(dialog)
    if dialog.isShowingOnBase then
        PlaySound(SOUNDS.GAMEPAD_OPEN_WINDOW)
    else
        PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
    end
end
local function OnDialogHiding(dialog)
    SCENE_MANAGER:RemoveFragment(GAMEPAD_DIALOG_BACKGROUND_FADE_FRAGMENT)
end
local function OnDialogHidden(dialog)
    local releasedFromButton = dialog.releasedFromButton
    dialog.releasedFromButton = nil
    RemoveActionLayerByName(GetString(SI_KEYBINDINGS_LAYER_DIALOG))
    dialog:Deactivate()
    KEYBIND_STRIP:PopKeybindGroupState()
     g_keybindStripDescriptors:ReleaseAllObjects()
    if(dialog.owningScene ~= nil) then
        dialog.hideFunction(dialog, releasedFromButton)
    end
    ZO_Dialogs_ReleaseDialog(dialog, releasedFromButton)
    if dialog.isShowingOnBase then
        PlaySound(SOUNDS.GAMEPAD_CLOSE_WINDOW)
    else
        PlaySound(SOUNDS.GAMEPAD_MENU_BACK)
    end
    if dialog.info.finishedCallback then
        dialog.info.finishedCallback(dialog)
    end
    if dialog.hideSceneOnClose then
        SCENE_MANAGER:HideCurrentScene()
    end
    
end
-- this always gets called
function ZO_GenericGamepadDialog_BaseSetup(dialog, title, mainText)
    local headerData = dialog.headerData
    ZO_ClearTable(headerData) -- make sure we are not bringing over header data from the previous setup.
    headerData.titleText = title
    headerData.messageText = mainText
    if (not ZO_GenericGamepadDialog_DefaultSetup(dialog, dialog.data)) then
        ZO_GamepadGenericHeader_Refresh(dialog.header, headerData)
    end
end
    local headerData = dialog.headerData
    local needRefresh = false
    if (data) then
        if (data.data1) then
            headerData.data1HeaderText  = data.data1.header
            headerData.data1Text  = data.data1.value
            needRefresh = true
        end
        if (data.data2) then
            headerData.data2HeaderText  = data.data2.header
            headerData.data2Text  = data.data2.value
            needRefresh = true
        end
        if (data.data3) then
            headerData.data3HeaderText  = data.data3.header
            headerData.data3Text  = data.data3.value
            needRefresh = true
        end
        if (data.data4) then
            headerData.data4HeaderText  = data.data4.header
            headerData.data4Text  = data.data4.value
            needRefresh = true
        end
    end
    if (needRefresh) then
        ZO_GamepadGenericHeader_Refresh(dialog.header, headerData)
    end
    return needRefresh
end
    -- Management
    dialog.Activate = function() 
        if not dialog.info.blockDirectionalInput then
            DIRECTIONAL_INPUT:Activate(dialog) 
        end
    end
    dialog.Deactivate = function() DIRECTIONAL_INPUT:Deactivate(dialog) end
    dialog.fragment = ZO_ConveyorSceneFragment:New(dialog, ZO_GamepadQuadrants_GetAnchor(ZO_GAMPEPAD_ANCHORS.QUADRANT_1), true)
    dialog.hideFunction = function(dialog, releasedFromButton)
        local hideLater = function()
            if dialog.owningScene == nil then return end --player pressed another button before call later
            dialog.releasedFromButton = releasedFromButton
            TryRemoveKeybind()
            if dialog.isShowingOnBase then
                SCENE_MANAGER:RemoveFragmentGroup(ZO_GAMEPAD_DIALOG_FRAGMENT_GROUP)
            end
            SCENE_MANAGER:RemoveFragment(dialog.fragment)
            dialog.owningScene = nil
            if dialog.entryList then
                dialog.entryList:Deactivate()
            end
        end
        if dialog.info.onHideDelayMS and dialog:IsHidden() ~= true then
            zo_callLater(hideLater, dialog.info.onHideDelayMS)
            return
        end
        hideLater()
    end
    dialog.fragment:RegisterCallback("StateChange", function(oldState, newState)
                                                        if(newState == SCENE_FRAGMENT_SHOWING) then
                                                            OnDialogShowing(dialog)
                                                        elseif(newState == SCENE_FRAGMENT_SHOWN) then
                                                            OnDialogShown(dialog)
                                                        elseif(newState == SCENE_FRAGMENT_HIDING) then
                                                            OnDialogHiding(dialog)
                                                        elseif(newState == SCENE_FRAGMENT_HIDDEN) then
                                                            OnDialogHidden(dialog)
                                                        end
                                                    end)
    dialog.headerData = {}
    dialog.header = dialog:GetNamedChild("HeaderContainer").header
    ZO_GamepadGenericHeader_Initialize(dialog.header)
end
function ZO_GenericGamepadDialog_Show(dialog)
    --Attach the dialog fragment to whichever scene is currenty showing
    dialog.isShowingOnBase = false
    local currentScene
    if SCENE_MANAGER:IsShowingBaseScene() then
        currentScene = SCENE_MANAGER:GetScene(ZO_GAMEPAD_DIALOG_BASE_SCENE_NAME)
        SCENE_MANAGER:AddFragmentGroup(ZO_GAMEPAD_DIALOG_FRAGMENT_GROUP)
        dialog.isShowingOnBase = true
    else
        currentScene = SCENE_MANAGER:GetCurrentScene()
    end
    dialog.owningScene = currentScene
    SCENE_MANAGER:AddFragment(dialog.fragment)
    if dialog.entryList then
        dialog.entryList:Activate()
    end
end
-------------
-- Tooltip --
-------------
    dialog.shouldShowTooltip = true
    GAMEPAD_TOOLTIPS:SetBgType(GAMEPAD_LEFT_DIALOG_TOOLTIP, GAMEPAD_TOOLTIP_DARK_BG)
    GAMEPAD_TOOLTIPS:ShowBg(GAMEPAD_LEFT_DIALOG_TOOLTIP)
end
function ZO_GenericGamepadDialog_HideTooltip(dialog)   
    if dialog.shouldShowTooltip then   
        
        GAMEPAD_TOOLTIPS:HideBg(GAMEPAD_LEFT_DIALOG_TOOLTIP)
        GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_LEFT_DIALOG_TOOLTIP)
        GAMEPAD_TOOLTIPS:Reset(GAMEPAD_LEFT_DIALOG_TOOLTIP)
        dialog.shouldShowTooltip = false
    end
end
-------------------------------------
-- Parametric List Dialog Template --
-------------------------------------
-- Dialog
    local baseHideFunction = dialog.hideFunction 
    dialog.hideFunction = function(dialog, releasedFromButton)
        baseHideFunction(dialog, releasedFromButton)
        dialog.entryList:SetOnSelectedDataChangedCallback(nil)
        dialog.entryList:Deactivate()
    end
    local listControl = dialog:GetNamedChild("EntryList"):GetNamedChild("List")
    dialog.entryList = ZO_GamepadVerticalItemParametricScrollList:New(listControl)
    dialog.entryList:SetAlignToScreenCenter(true)
end
local function ParametricListControlSetupFunc(control, data, selected, reselectingDuringRebuild, enabled, active)
    if control.resetFunction then
        control.resetFunction()
    end
    data.setup(control, data, selected, reselectingDuringRebuild, enabled, active)
end
function ZO_GenericParametricListGamepadDialogTemplate_Setup(dialog, limitNumEntries, data)
    if(data) then
        ZO_GenericGamepadDialog_DefaultSetup(dialog, data)
    end
    dialog.entryList:Clear()
    if dialog.info.parametricListOnSelectionChangedCallback then
         dialog.entryList:SetOnSelectedDataChangedCallback(dialog.info.parametricListOnSelectionChangedCallback)
    end
    for i, value in ipairs(dialog.info.parametricList) do
        if(limitNumEntries == nil or i <= limitNumEntries) then
            local visible = true
            if value.templateData and (value.templateData.visible ~= nil) then -- false should be treated as false, while nil should be treated as true.
                visible = value.templateData.visible
                if type(visible) == "function" then
                    visible = visible()
                end
            end
            
            if visible then
                local name = nil
                if(value.text ~= nil) then
                    if(type(value.text) == "number") then
                        name = GetString(value.text)
                    else
                        name = value.text
                    end
                else
                    name = "EntryItem" .. tostring(i)
                end
                local data = ZO_GamepadEntryData:New(name, value.icon)
                if(value.templateData) then
                    for dataKey, dataValue in pairs(value.templateData) do
                        data[dataKey] = dataValue
                    end
                end
                if value.initializeData then
                    value.initializeData(data, value.templateData)
                end
        
                if(not dialog.entryList:HasDataTemplate(value.template)) then
                    dialog.entryList:AddDataTemplateWithHeader(value.template, ParametricListControlSetupFunc, ZO_GamepadMenuEntryTemplateParametricListFunction, nil, "ZO_GamepadMenuEntryHeaderTemplate")
                    dialog.entryList:AddDataTemplate(value.template, ParametricListControlSetupFunc, ZO_GamepadMenuEntryTemplateParametricListFunction)
                end
        
                if(value.header ~= nil) then
                    data:SetHeader(GetString(value.header)) 
                    dialog.entryList:AddEntryWithHeader(value.template, data)
                else
                    dialog.entryList:AddEntry(value.template, data)
                end
            end
        end
    end
    dialog.entryList:CommitWithoutReselect()
end
-------------------------------------
-- Cooldown Dialog Template --
-------------------------------------
    dialog.cooldownLabelControl = dialog:GetNamedChild("CooldownLabel")
    dialog.loadingControl = dialog:GetNamedChild("Loading")
end
    local loadingMode = dialog.info.loading ~= nil
    dialog.cooldownLabelControl:SetText("")
    dialog.loadingControl:SetHidden(not loadingMode)
    if(loadingMode) then
        local loadingString = dialog.info.loading.text
        if type(loadingString) == "function" then
            loadingString = loadingString(dialog)
        end
        if loadingString and loadingString ~= "" then
            dialog.cooldownLabelControl:SetText(loadingString)
            dialog.cooldownLabelControl:SetColor(ZO_NORMAL_TEXT:UnpackRGBA())
        end
    else
        dialog.cooldownLabelControl:SetColor(ZO_SELECTED_TEXT:UnpackRGBA())
    end
end
-------------------------------------
-- Transaction Dialog Template --
-------------------------------------
    dialog.messageControl = dialog:GetNamedChild("Message")
    
    local itemContainer = dialog:GetNamedChild("ItemContainer")
    dialog.itemIconControl = itemContainer:GetNamedChild("Icon")
    dialog.itemLabelControl = itemContainer:GetNamedChild("Name")
    dialog.itemStackCount = itemContainer:GetNamedChild("IconStackCount")
    
    local data1Header = dialog:GetNamedChild("HeaderContainerHeaderData1Header")
    data1Header:SetParent(itemContainer)
    data1Header:ClearAnchors()
    data1Header:SetAnchor(TOPLEFT, itemContainer, BOTTOMLEFT)
    data1Header:SetAnchor(TOPRIGHT, itemContainer, BOTTOMRIGHT, 0, SCREEN_HEADER_DATA_OFFSET_Y)
end
    if(data) then
        dialog.messageControl:SetText(dialog.headerData.messageText)
        dialog.headerData.messageText = nil
        ZO_GenericGamepadDialog_DefaultSetup(dialog, data)
        dialog.itemIconControl:SetTexture(data.itemIcon)
        dialog.itemLabelControl:SetText(data.itemName)
        local hasStack = data.itemStackSize > 0
        if hasStack then
            dialog.itemStackCount:SetText(data.itemStackSize)
        end
        dialog.itemStackCount:SetHidden(not hasStack)
    end
end
---------------------------------------
-- Pamametric List Entry Setup Funcs --
---------------------------------------
local function ProcessData(control, data, isTexture)
    if(control == nil) then
        return false
    end
    local dataUpdated = false
    if type(data) == "function" then
        data = data(control)
        if not data then
            dataUpdated = true
        end
    end
    if data then
        if type(data) == "string" then
            if isTexture then
                control:SetTexture(data)
            else
                control:SetText(data)
            end
            dataUpdated = true
        end
    end
    control:SetHidden(not dataUpdated)
    if(not dataUpdated and not isTexture) then
        control:SetText("")
    end
    return dataUpdated
end
local function GetLabelHeight(label) 
    return select(2, label:GetTextDimensions())
end
function ZO_GamepadIconEntryWithDescriptionItem_Setup(control, data, selected, reselectingDuringRebuild, enabled, active)
    local IS_TEXTURE = true
    ProcessData(control.iconControl, data.icon, IS_TEXTURE)
    
    if(data.textColor) then
        control.labelControl:SetColor(r, g, b, 1)
    end
    ProcessData(control.labelControl, data.text)
    
    ProcessData(control.labelDataControl, data.textData)
    ProcessData(control.descriptionControl, data.description)
    local x, y = control:GetDimensions()
    local height = GetLabelHeight(control.labelControl)
    height = height + GetLabelHeight(control.descriptionControl)
    control:SetDimensions(x, height)
end
function ZO_GamepadIconEntryWithSubItem_Setup(control, data, selected, reselectingDuringRebuild, enabled, active)
    local IS_TEXTURE = true
    ProcessData(control.iconControl, data.icon, IS_TEXTURE)
    
    if(data.textColor) then
        control.labelControl:SetColor(data.textColor.r, data.textColor.g, data.textColor.b, 1)
    end
    ProcessData(control.labelControl, data.text)
    
    ProcessData(control.subLabelControl, data.subText)
    ProcessData(control.subIconControl, data.subIcon, IS_TEXTURE)
end