Back to Home

ESO Lua File v100019

libraries/zo_combobox/gamepad/zo_combobox_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
ZO_GAMEPAD_COMBO_BOX_FONT = "ZoFontGamepad27"
ZO_GAMEPAD_COMBO_BOX_HIGHLIGHTED_FONT = "ZoFontGamepad36"
ZO_GAMEPAD_COMBO_BOX_PADDING = 16
-------------------------------------------------------------------------------
-- ZO_ComboBox_Gamepad
-------------------------------------------------------------------------------
ZO_ComboBox_Gamepad = ZO_ComboBox_Base:Subclass()
function ZO_ComboBox_Gamepad:New(...)
    local object = ZO_ComboBox_Base.New(self, ...)
    return object
end
function ZO_ComboBox_Gamepad:Initialize(control)
    ZO_ComboBox_Base.Initialize(self, control)
    control.comboBoxObject = self
    self.m_active = false
    self.m_dropdown = GAMEPAD_COMBO_BOX_DROPDOWN
    self.m_focus = ZO_GamepadFocus:New(control)
    self.m_highlightedIndex = 1
    self.m_highlightColor = ZO_SELECTED_TEXT
    self.m_normalColor = ZO_DISABLED_TEXT
    
    self.m_font = ZO_GAMEPAD_COMBO_BOX_FONT
    self.m_highlightFont = ZO_GAMEPAD_COMBO_BOX_HIGHLIGHTED_FONT
    self.m_itemTemplate = "ZO_ComboBox_Item_Gamepad"
end
function ZO_ComboBox_Gamepad:ShowDropdownInternal()
    self:ClearMenuItems()
    self.m_dropdown:SetPadding(self.m_padding)
    self:AddMenuItems()
    self.m_container:SetHidden(true)
    self.m_dropdown:Show()
    self:SetVisible(true)
    self:HighlightSelectedItem()
end
function ZO_ComboBox_Gamepad:HideDropdownInternal()
    self:ClearMenuItems()
    self:SetVisible(false)
    
    self.m_container:SetHidden(false)
    self.m_dropdown:Hide()
    self:SetActive(false)
end
function ZO_ComboBox_Gamepad:OnClearItems()
    self.m_highlightedIndex = nil
end
function ZO_ComboBox_Gamepad:OnItemAdded()
    if not self.m_highlightedIndex then
        self.m_highlightedIndex = 1
    end
end
function ZO_ComboBox_Gamepad:GetNormalColor(item)
    local itemColor = item.m_normalColor or self.m_normalColor
    return itemColor
end
function ZO_ComboBox_Gamepad:GetHighlightColor(item)
    local itemColor = item.m_highlightColor or self.m_highlightColor
    return itemColor
end
function ZO_ComboBox_Gamepad:SetItemTemplate(template)
    self.m_itemTemplate = template
end
function ZO_ComboBox_Gamepad:GetHeight()
    if(self.m_selectedItemText) then
        return self.m_selectedItemText:GetTextHeight()
    end
    return self.m_container:GetHeight()
end
function ZO_ComboBox_Gamepad:AddMenuItems()
    for i = 1, #self.m_sortedItems do
        -- The variable item must be defined locally here, otherwise it won't work as an upvalue to the selection helper
        local item = self.m_sortedItems[i]
        local control = self.m_dropdown:AddItem(item)
        
        control.nameControl = control:GetNamedChild("Name")
        control.nameControl:SetText(item.name)
        control.nameControl:SetColor(self:GetNormalColor(item):UnpackRGBA())
        ApplyTemplateToControl(control, self.m_itemTemplate)
        control.nameControl:SetFont(self.m_highlightFont) -- Use the highlighted font for sizing purposes
        control.nameControl:SetWidth(self.m_container:GetWidth())
        local height = control.nameControl:GetTextHeight()
        control:SetHeight(height)
        self.m_dropdown:AddHeight(height)
        if self.m_font then
            control.nameControl:SetFont(self.m_font)
        end
        
        local focusEntry = {
            control = control,
            data = item,
            activate = function(control, data) self:OnItemSelected(control, data) end,
            deactivate = function(control, data) self:OnItemDeselected(control, data) end,
        }
        self.m_focus:AddEntry(focusEntry)
    end
    self.m_dropdown:AnchorToControl(self.m_container, 0)
end
function ZO_ComboBox_Gamepad:OnItemSelected(control, data)
    control.nameControl:SetColor(self:GetHighlightColor(data):UnpackRGBA())
    control.nameControl:SetFont(self.m_highlightFont)
    self:UpdateAnchors(control) 
end
function ZO_ComboBox_Gamepad:OnItemDeselected(control, data)
    control.nameControl:SetColor(self:GetNormalColor(data):UnpackRGBA())
    control.nameControl:SetFont(self.m_font)
end
function ZO_ComboBox_Gamepad:ClearMenuItems()
   self.m_focus:RemoveAllEntries()
   self.m_dropdown:Clear()
end
function ZO_ComboBox_Gamepad:SetActive(active)
    if self.m_active ~= active then
        self.m_active = active
        if self.m_active then
            self.m_focus:Activate()
            self.m_keybindState = KEYBIND_STRIP:PushKeybindGroupState()
            KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptor, self.m_keybindState)
        else
            self.m_focus:Deactivate()
            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptor, self.m_keybindState)
            KEYBIND_STRIP:PopKeybindGroupState()
            
            if self.deactivatedCallback and not self.blockDeactivatedCallback then
               self.deactivatedCallback(self.deactivatedCallbackArgs)
            end
        end
    end
end
function ZO_ComboBox_Gamepad:HighlightSelectedItem()
    self:SetHighlightedItem(self.m_highlightedIndex)
end
function ZO_ComboBox_Gamepad:SelectHighlightedItem()
    local focusItem = self.m_focus:GetFocusItem()
    local focusIndex = self.m_focus:GetFocus()
    if focusIndex then
        self.m_highlightedIndex = focusIndex -- This needs to come before self:SelectItem() otherwise self.m_focus:GetFocus() always returns nil
    end
    if focusItem then
        self:SelectItem(focusItem.data)
        PlaySound(SOUNDS.DEFAULT_CLICK)
    end
   
    self:Deactivate()
end
function ZO_ComboBox_Gamepad:SelectItemByIndex(index, ignoreCallback)
    self.m_highlightedIndex = index
    ZO_ComboBox_Base.SelectItemByIndex(self, index, ignoreCallback)
end
function ZO_ComboBox_Gamepad:SetHighlightedItem(highlightIndex, reselectIndex)
    self.m_focus:SetFocusByIndex(highlightIndex, reselectIndex)
end
function ZO_ComboBox_Gamepad:TrySelectItemByData(itemData)
    for i, data in ipairs(self.m_sortedItems) do
        if data.name == itemData.name then
            self:SelectItemByIndex(i)
            return true
        end
    end
    return false
end
do
    local INCLUDE_SAVED_INDEX = true
    function ZO_ComboBox_Gamepad:GetHighlightedIndex()
        return self.m_focus:GetFocus(INCLUDE_SAVED_INDEX)
    end
end
function ZO_ComboBox_Gamepad:Activate()
    self:SetActive(true)
    PlaySound(SOUNDS.COMBO_CLICK)
end
function ZO_ComboBox_Gamepad:Deactivate(blockCallback)
    self.blockDeactivatedCallback = blockCallback
    self.blockDeactivatedCallback = false
end
function ZO_ComboBox_Gamepad:IsActive()
    return self.m_active
end
function ZO_ComboBox_Gamepad:SetDeactivatedCallback(callback, args)
    self.deactivatedCallbackArgs = args
    self.deactivatedCallback = callback
end
function ZO_ComboBox_Gamepad:SetKeybindAlignment(alignment)
    self.keybindStripDescriptor.alignment = alignment
end
function ZO_ComboBox_Gamepad:InitializeKeybindStripDescriptors()
    self.keybindStripDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        
        -- since we can now have combo boxes in dialogs and in normal ui elements
        -- we want to make sure our combo box is listening for the proper keybinds
        -- based on whether or not a dialog is active
        {
            keybind = "UI_SHORTCUT_NEGATIVE",
            name = GetString(SI_GAMEPAD_BACK_OPTION),
            callback = function()
               self:Deactivate()
            end,
            visible = function() return not ZO_Dialogs_IsShowingDialog() end
        },
        {
            keybind = "DIALOG_NEGATIVE",
            name = GetString(SI_GAMEPAD_BACK_OPTION),
            callback = function()
               self:Deactivate()
            end,
            visible = ZO_Dialogs_IsShowingDialog
        },
        {
            keybind = "UI_SHORTCUT_PRIMARY",
            name = GetString(SI_GAMEPAD_SELECT_OPTION),
            callback = function()
                self:SelectHighlightedItem()
            end,
            visible = function() return not ZO_Dialogs_IsShowingDialog() end
        },
        {
            keybind = "DIALOG_PRIMARY",
            name = GetString(SI_GAMEPAD_SELECT_OPTION),
            callback = function()
                self:SelectHighlightedItem()
            end,
            visible = ZO_Dialogs_IsShowingDialog
        },
    }
end
function ZO_ComboBox_Gamepad:UpdateAnchors(selectedControl)
    -- The control box will always be centered on the original dropdown location
    local topItem = self.m_focus:GetItem(1)
    local topControl = topItem.control
    local offset = topControl:GetTop() - selectedControl:GetTop()
    self.m_dropdown:AnchorToControl(self.m_container, offset)
end
-------------------------------------------------------------------------------
-- ZO_GamepadComboBoxDropdown
-------------------------------------------------------------------------------
-- ZO_ComboBox_Gamepad_Dropdown is a singleton that is used by the current dropdown to display a list
ZO_GamepadComboBoxDropdown = ZO_Object:Subclass()
function ZO_GamepadComboBoxDropdown:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_GamepadComboBoxDropdown:Initialize(control)
    self.dropdownControl = control
    self.scrollControl = control:GetNamedChild("Scroll")
    self.backgroundControl = control:GetNamedChild("Background")
    self.templateName = "ZO_ComboBox_Item_Gamepad"
    self.pool = ZO_ControlPool:New(self.templateName, self.scrollControl, self.templateName)
    self.lastControlAdded = nil
    self.height = 0
    self.padding = 0
    self.borderPadding = ZO_GAMEPAD_COMBO_BOX_PADDING
    self.minY = 70
    local function RefreshMaxY()
        self.maxY = GuiRoot:GetHeight() + ZO_GAMEPAD_QUADRANT_BOTTOM_OFFSET
    end
    RefreshMaxY()
    EVENT_MANAGER:RegisterForEvent("GamepadComboBoxDropdown", EVENT_SCREEN_RESIZED, RefreshMaxY)
end
function ZO_GamepadComboBoxDropdown:SetPadding(padding)
    self.padding = padding
end
function ZO_GamepadComboBoxDropdown:Show()
    self.dropdownControl:SetHidden(false)
end
function ZO_GamepadComboBoxDropdown:Hide()
    self.dropdownControl:SetHidden(true)
end
function ZO_GamepadComboBoxDropdown:AnchorToControl(control, offsetY)
    local controlTop = control:GetTop()
    local dropDownTop = controlTop + offsetY - self.borderPadding 
    local dropDownBottom = controlTop + offsetY + self.height + self.borderPadding
    local topYDelta = 0
    if dropDownTop < self.minY then
        topYDelta = (self.minY - dropDownTop)
    end
    local bottomYDelta = 0
    if dropDownBottom > self.maxY then
        bottomYDelta = (dropDownBottom - self.maxY)
    end
    self.dropdownControl:SetAnchor(TOPLEFT, control, TOPLEFT, 0, offsetY)
    self.dropdownControl:SetDimensions(control:GetWidth(), self.height)
    local backgroundOffsetYTop = -self.borderPadding + topYDelta
    local backgroundOffsetBottomTop = self.borderPadding - bottomYDelta
    self.scrollControl:SetAnchor(TOPLEFT, self.dropdownControl, TOPLEFT, -ZO_GAMEPAD_COMBO_BOX_PADDING, backgroundOffsetYTop)
    self.scrollControl:SetAnchor(BOTTOMRIGHT, self.dropdownControl, BOTTOMRIGHT, ZO_GAMEPAD_COMBO_BOX_PADDING, backgroundOffsetBottomTop)
    self.backgroundControl:SetAnchor(TOPLEFT, self.dropdownControl, TOPLEFT, -ZO_GAMEPAD_COMBO_BOX_PADDING, backgroundOffsetYTop)
    self.backgroundControl:SetAnchor(BOTTOMRIGHT, self.dropdownControl, BOTTOMRIGHT, ZO_GAMEPAD_COMBO_BOX_PADDING, backgroundOffsetBottomTop)
end
function ZO_GamepadComboBoxDropdown:AcquireControl(item, relativeControl)
    local padding = self.padding
    local templateName = self.templateName
    local control, key = self.pool:AcquireObject()
    control:SetAnchor(RIGHT, self.m_container, RIGHT, 0, padding, ANCHOR_CONSTRAINS_X)
    if relativeControl then
        control:SetAnchor(TOPLEFT, relativeControl, BOTTOMLEFT, 0, padding)
    else
        control:SetAnchor(TOPLEFT, self.dropdownControl, TOPLEFT, 0, padding)
    end
    control.key = key
    control.item = item
    return control
end
function ZO_GamepadComboBoxDropdown:AddHeight(height)
    self.height = self.height + height
end
function ZO_GamepadComboBoxDropdown:AddItem(data)
    local control = self:AcquireControl(data, self.lastControlAdded)
    self.lastControlAdded = control
    return control
end
function ZO_GamepadComboBoxDropdown:Clear()
    self.pool:ReleaseAllObjects()
    self.lastControlAdded = nil
    self.height = 0
end
-- This is a control used by all gamepad combo boxes to display the dropdown
    GAMEPAD_COMBO_BOX_DROPDOWN = ZO_GamepadComboBoxDropdown:New(control)
end