Back to Home

ESO Lua File v101039

common/gamepad/zo_gamepadiconselector.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
local ICON_PER_ROW = 6
local ICON_PADDING = 40
local ICON_SIZE = 64
local ICON_OFFSET_X = 0
local ICON_OFFSET_Y = 0
ZO_ICONSELECTOR_MOVEMENT_TYPES = 
{
    MOVE_RIGHT = 1,
    MOVE_LEFT = 2,
    MOVE_UP = 3,
    MOVE_DOWN = 4,
    -- LAST allows derived classes to start their movement enumerations after the base movements
    LAST = 5,
}
    if type == ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_RIGHT then
        PlaySound(SOUNDS.HOR_LIST_ITEM_SELECTED)
    elseif type == ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_LEFT then
        PlaySound(SOUNDS.HOR_LIST_ITEM_SELECTED)
    elseif type == ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_UP then
        PlaySound(SOUNDS.GAMEPAD_MENU_UP)
    elseif type == ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_DOWN then
        PlaySound(SOUNDS.GAMEPAD_MENU_DOWN)
    end
end
--------------------
-- Util Functions --
--------------------
local function GetGridRowCol(index, itemsPerRow)
    local row = zo_floor((index - 1) / itemsPerRow)
    local col = (index - 1) % itemsPerRow
    return row, col
end
local function GetGridIndex(row, col, itemsPerRow)
    local index = (row * itemsPerRow) + col
    return index
end
----------------------------
-- ZO_GamepadIconSelector --
----------------------------
ZO_GamepadIconSelector = ZO_Object:Subclass()
function ZO_GamepadIconSelector:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_GamepadIconSelector:Initialize(control, settings)
    self.control = control
    self.control.iconSelectorObject = self
    
    self.iconsPerRow = ICON_PER_ROW
    self.iconPadding = ICON_PADDING
    self.iconSize = ICON_SIZE
    self.iconOffsetX = ICON_OFFSET_X
    self.iconOffsetY = ICON_OFFSET_Y    
    if(settings.iconsPerRow) then
        self.iconsPerRow = settings.iconsPerRow
    end
    if(settings.iconPadding) then
        self.iconPadding = settings.iconPadding
    end
        
    if(settings.iconSize) then
        self.iconSize = settings.iconSize
    end
        
    if(settings.iconOffsetX) then
        self.iconOffsetX = settings.iconOffsetX
    end
    if(settings.iconOffsetY) then
        self.iconOffsetY = settings.iconOffsetY
    end
    if(settings.totalIcons > self.iconsPerRow) then
        self.vertMovementController = ZO_MovementController:New(MOVEMENT_CONTROLLER_DIRECTION_VERTICAL)
    end
    self.horzMovementController = ZO_MovementController:New(MOVEMENT_CONTROLLER_DIRECTION_HORIZONTAL)
    self.iconHighlight = control:GetNamedChild("Highlight")
    self.iconHighlight:SetHidden(true)
    
    self.iconHighlightHint = self.iconHighlight:GetNamedChild("Hint")
    self.iconSelectorControls = {}
    local iconSelectorId = 0
    for i = 1, settings.totalIcons do
        local iconSelector = CreateControlFromVirtual(settings.uniqueName, control, "ZO_GamepadIconSelectorElementTemplate", iconSelectorId)
        iconSelector.iconIndex = i
        iconSelector:SetDimensions(self.iconSize, self.iconSize)
        local row, col = GetGridRowCol(i, self.iconsPerRow)
        iconSelector:SetAnchor(TOPLEFT, nil, TOPLEFT, col * (self.iconSize + self.iconPadding) + self.iconOffsetX, row * (self.iconSize + self.iconPadding) + self.iconOffsetY)
        settings.initFunc(iconSelector, i)
        iconSelectorId = iconSelectorId + 1
        table.insert(self.iconSelectorControls, iconSelector)
    end
    local defaultHighlightIndex = 1
    if(settings.defaultHighlightIndex) then
        defaultHighlightIndex = settings.defaultHighlightIndex
    end
    self:HighlightIconControl(defaultHighlightIndex)
end
function ZO_GamepadIconSelector:ForAllIconControls(func, ...)
    for i = 1, #self.iconSelectorControls do
        func(self.iconSelectorControls[i], i, ...)
    end
end
------------------------
-- Selector Highlight --
------------------------
function ZO_GamepadIconSelector:ModifyHighlightIndex(rowDiff, colDiff)
    local oldHighlightIndex = self.highlightIndex
    local itemsPerRow = self.iconsPerRow
    local totalItems = #self.iconSelectorControls
    local minCol = 0
    local minRow = 0
    local row, col = GetGridRowCol(self.highlightIndex, itemsPerRow)
    local maxRow, maxCol = GetGridRowCol(totalItems, itemsPerRow)
    
    col = col + colDiff
    if(row ~= maxRow) then
        if(col < minCol) then
            col = itemsPerRow - 1
        elseif(col > itemsPerRow - 1) then
            col = minCol
        end
    else
        if(col < minCol) then
            col = maxCol
        elseif(col > maxCol) then
            col = minCol
        end
    end
    row = row + rowDiff
    if(row < minRow) then
        row = maxRow
    elseif (row > maxRow) then
        row = 0
    end
    if(row == maxRow and col > maxCol) then
        if(rowDiff > 0) then
            row = minRow
        else
            row = row - 1
        end
    end
        
    local newHighlightIndex = GetGridIndex(row, col, itemsPerRow) + 1
    self:HighlightIconControl(newHighlightIndex)
    return newHighlightIndex ~= oldHighlightIndex
end
function ZO_GamepadIconSelector:HighlightIconControl(index)
    local control = self.iconSelectorControls[index]
    self.highlightIndex = index
    self.iconHighlight:ClearAnchors()
    self.iconHighlight:SetAnchor(CENTER, control, CENTER, 0, 0)
    self.iconHighlight:SetHidden(false)
    if(control.highlightHint) then
        self.iconHighlightHint:SetText(control.highlightHint)
        self.iconHighlightHint:SetHidden(false)
    else
        self.iconHighlightHint:SetHidden(true)
    end
end
function ZO_GamepadIconSelector:GetHighlightIndex()
    return self.highlightIndex
end
function ZO_GamepadIconSelector_RefreshIconSelectionIndicator(control, index, selectedIconIndex)
    local selectedIndicator = control:GetNamedChild("SelectedIndicator")
    selectedIndicator:SetHidden(index ~= selectedIconIndex)
end
-------------------------
-- Movement Controller --
-------------------------
function ZO_GamepadIconSelector:Activate()
end
function ZO_GamepadIconSelector:Deactivate()
end
function ZO_GamepadIconSelector:SetDirectionalInputEnabled(enabled)
    if(self.directionInputEnabled ~= enabled) then
        self.directionInputEnabled = enabled
        self.iconHighlight:SetHidden(not enabled)
        if enabled then
            DIRECTIONAL_INPUT:Activate(self, self.control)
        else
            DIRECTIONAL_INPUT:Deactivate(self)
        end
    end
end
function ZO_GamepadIconSelector:UpdateDirectionalInput()
    if(self.horzMovementController) then
        local result = self.horzMovementController:CheckMovement()
        if result == MOVEMENT_CONTROLLER_MOVE_NEXT then
            self:MoveRight()
        elseif result == MOVEMENT_CONTROLLER_MOVE_PREVIOUS then
            self:MoveLeft()
        end
    end
    if(self.vertMovementController) then
        local result = self.vertMovementController:CheckMovement()
        if result == MOVEMENT_CONTROLLER_MOVE_NEXT then
            self:MoveDown()
        elseif result == MOVEMENT_CONTROLLER_MOVE_PREVIOUS then
            self:MoveUp()
        end
    end
end
function ZO_GamepadIconSelector:MoveUp()
    if self:ModifyHighlightIndex(-1, 0) then
        self.onPlaySoundFunction(ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_UP)
    end
end
function ZO_GamepadIconSelector:MoveDown()
    if self:ModifyHighlightIndex(1, 0) then
        self.onPlaySoundFunction(ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_DOWN)
    end
end
function ZO_GamepadIconSelector:MoveLeft()
    if self:ModifyHighlightIndex(0, -1) then
        self.onPlaySoundFunction(ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_LEFT)
    end
end
function ZO_GamepadIconSelector:MoveRight()
    if self:ModifyHighlightIndex(0, 1) then
        self.onPlaySoundFunction(ZO_ICONSELECTOR_MOVEMENT_TYPES.MOVE_RIGHT)
    end
end
function ZO_GamepadIconSelector:SetPlaySoundFunction(fn)
end