ESO Lua File v100011

ingame/voicechat/console/gamepadvoicechathud.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
----------------------
-- VoiceChatHUD Speaker List
----------------------
local CHANNEL_TO_COLOR = {
    [VOICE_CHANNEL_AREA] = ZO_ColorDef:New(GetInterfaceColor(INTERFACE_COLOR_TYPE_VOICE_CHAT_COLORS, VOICE_CHAT_COLORS_AREA)),
    [VOICE_CHANNEL_GROUP] = ZO_ColorDef:New(GetInterfaceColor(INTERFACE_COLOR_TYPE_VOICE_CHAT_COLORS, VOICE_CHAT_COLORS_GROUP)),
    [VOICE_CHANNEL_GUILD] = ZO_ColorDef:New(GetInterfaceColor(INTERFACE_COLOR_TYPE_VOICE_CHAT_COLORS, VOICE_CHAT_COLORS_GUILD)),
}
local CHANNEL_TO_ICON = {
    [VOICE_CHANNEL_AREA] = "EsoUI/Art/VOIP/voip-area.dds",
    [VOICE_CHANNEL_GROUP] = "EsoUI/Art/VOIP/voip-group.dds",
    [VOICE_CHANNEL_GUILD] = "EsoUI/Art/VOIP/voip-guild.dds",
}
ZO_VoiceChatHUDSpeakerList = ZO_Object:Subclass()
function ZO_VoiceChatHUDSpeakerList:New(control, entryTemplate)
    local list = ZO_Object.New(self)
    list.control = control
    list.pool = ZO_ControlPool:New(entryTemplate, control, "Entry")
    
    list.entryPaddingY = -25
    list.height = 0
    return list
end
function ZO_VoiceChatHUDSpeakerList:AddLine(text, channelType, isLocalPlayer)
    local newEntry = self.pool:AcquireObject()
    newEntry:GetNamedChild("Text"):SetText(text)
    if(self.lastEntry) then
        newEntry:SetAnchor(TOPLEFT, self.lastEntry, BOTTOMLEFT, 0, self.entryPaddingY)
    else
        newEntry:SetAnchor(TOPLEFT, nil, TOPLEFT, 0, 0)
    end
    self.control:SetHeight(self.height)
    self.lastEntry = newEntry
    
    local icon = newEntry:GetNamedChild("Icon")
    icon:SetTexture(CHANNEL_TO_ICON[channelType])
    icon:SetColor(CHANNEL_TO_COLOR[channelType]:UnpackRGBA())
    newEntry:GetNamedChild("Bg"):SetColor(CHANNEL_TO_COLOR[channelType]:UnpackRGBA())
    newEntry:GetNamedChild("LocalPlayerFrame"):SetHidden(not isLocalPlayer)
end
function ZO_VoiceChatHUDSpeakerList:Clear()
    self.lastEntry = nil
    self.height = 0
    self.control:SetHeight(self.height)
    self.pool:ReleaseAllObjects()
end
----------------------
-- VoiceChatHUD Manager
----------------------
local LIST_ENTRY_LIMIT = 4
local CLEAR_DELAY = 500 --milliseconds
ZO_VoiceChatHUDGamepadManager = ZO_Object:Subclass()
function ZO_VoiceChatHUDGamepadManager:New(...)
     local voiceChatHUDGamepad = ZO_Object.New(self)
     voiceChatHUDGamepad:Initialize(...)
     return voiceChatHUDGamepad
end
function ZO_VoiceChatHUDGamepadManager:Initialize(control)
    self.control = control
    self.speakerData = {}
    self.channelFilter = nil
    self.currentChannelId = nil
    self.speakerList = ZO_VoiceChatHUDSpeakerList:New(self.control:GetNamedChild("List"), "ZO_VoiceChatHUDGamepadParticipantTemplate")
    self.delayedClearCallData = {}
    
    GAMEPAD_VOICECHAT_HUD_FRAGMENT = ZO_HUDFadeSceneFragment:New(ZO_VoiceChatHUDGamepad)
    GAMEPAD_VOICECHAT_HUD_FRAGMENT:RegisterCallback("StateChange", function(oldState, newState)
         if newState == SCENE_FRAGMENT_SHOWING then
               self:RefreshSpeakerList()
         end
    end)
end
function ZO_VoiceChatHUDGamepadManager:RefreshSpeakerList()
    if not GAMEPAD_VOICECHAT_HUD_FRAGMENT:IsShowing() then
        return
    end
    self.speakerList:Clear()
    
    for i = 1, #self.speakerData do
        local speakerData = self.speakerData[i]
        local isUserLocalPlayer = self:IsUserLocalPlayer(speakerData.userName)
        self.speakerList:AddLine(speakerData.userName, speakerData.channelType, isUserLocalPlayer)
    end
end
function ZO_VoiceChatHUDGamepadManager:InsertName(userName, channelType)
    local speakerDataEntry = {userName = userName, channelType = channelType}
    --The local player is always at the bottom, and other users are pushed from the top
    if self:IsUserLocalPlayer(userName) then
        table.insert(self.speakerData, speakerDataEntry)
    else
        table.insert(self.speakerData, 1, speakerDataEntry)
    end
    if #self.speakerData == LIST_ENTRY_LIMIT and not self:IsLocalPlayerLastListEntry() then
        --The last slot is reserved for the local player
        table.remove(self.speakerData, LIST_ENTRY_LIMIT)
    elseif #self.speakerData == LIST_ENTRY_LIMIT + 1 then
        table.remove(self.speakerData, LIST_ENTRY_LIMIT)
    end
end
function ZO_VoiceChatHUDGamepadManager:RemoveName(userName)
    for i = 1, #self.speakerData do
        if self.speakerData[i].userName == userName then
            table.remove(self.speakerData, i)
            self:RefreshSpeakerList()
            break
        end
    end
end
function ZO_VoiceChatHUDGamepadManager:DelayedRemoveName(callId)
    local userName = self.delayedClearCallData[callId]
    self.delayedClearCallData[callId] = nil
    local tableId = self.delayedClearCallData[userName]
    if callId == tableId then --false when callId is older when multiple callbacks exist to remove the player
        self:RemoveName(userName)
        self.delayedClearCallData[userName] = nil
    end
end
function ZO_VoiceChatHUDGamepadManager:IsUserSpeaking(userName)
    for i = 1, #self.speakerData do
        if self.speakerData[i].userName == userName then
            return true
        end
    end
    return false
end
function ZO_VoiceChatHUDGamepadManager:IsUserLocalPlayer(userName)
    return userName == UndecorateDisplayName(GetDisplayName())
end
function ZO_VoiceChatHUDGamepadManager:IsLocalPlayerSpeaking()
    local localName = UndecorateDisplayName(GetDisplayName())
    return self:IsUserSpeaking(localName)
end
function ZO_VoiceChatHUDGamepadManager:IsLocalPlayerLastListEntry()
    local lastName = self.speakerData[#self.speakerData].userName
    return self:IsUserLocalPlayer(lastName)
end
---- Events
function ZO_VoiceChatHUDGamepadManager:RegisterForEvents()
    self.control:RegisterForEvent(EVENT_VOICE_USER_SPEAKING, function(eventCode, ...) self:OnUserSpeaking(...) end)
    self.control:RegisterForEvent(EVENT_VOICE_CHANNEL_JOINED, function(eventCode, ...) self:OnChannelJoined(...) end)
    self.control:RegisterForEvent(EVENT_VOICE_CHANNEL_LEFT, function(eventCode, ...) self:OnChannelLeft(...) end)
    self.control:RegisterForEvent(EVENT_VOICE_USER_JOINED_CHANNEL, function(eventCode, ...) self:OnOtherPlayerJoinedChannel(...) end)
    self.control:RegisterForEvent(EVENT_VOICE_CHANNEL_AVAILABLE, function(eventCode, ...) self:OnNewChannelAvailable(...) end)
    --A user can leave in the middle of talking, so check for this as well.
    self.control:RegisterForEvent(EVENT_VOICE_USER_LEFT_CHANNEL, function(eventCode, ...) self:OnOtherPlayerLeftChannel(...) end)
end
local function OnDelayedRemoveName(callId)
    VOICE_CHAT_HUD_GAMEPAD:DelayedRemoveName(callId)
end
function ZO_VoiceChatHUDGamepadManager:OnUserSpeaking(channelName, userName, characterName, speaking)
    --TODO: Make use of characterName
    local channelType, _, _ = GetVoiceChannelInfo(channelName)
    --Ignore speakers for channels not joined, but that we still get events from
    if channelType ~= self.channelFilter then
        return
    end
    
    if speaking then
        --Undo delayed removal from list
        self.delayedClearCallData[userName] = nil
        if self:IsUserSpeaking(userName) then
            return
        else
            self:InsertName(userName, channelType)
        end
    else
        local callId = zo_callLater(OnDelayedRemoveName, CLEAR_DELAY)
        self.delayedClearCallData[callId] = userName
        self.delayedClearCallData[userName] = callId
    end
end
function ZO_VoiceChatHUDGamepadManager:OnChannelJoined(channelName)
    local channelType, _, _ = GetVoiceChannelInfo(channelName)
    
    self.currentChannelId = channelName
    self.channelFilter = channelType
    self.speakerData = {}
end
function ZO_VoiceChatHUDGamepadManager:OnChannelLeft(channelName)
    local channelType, _, _ = GetVoiceChannelInfo(channelName)
    if self.currentChannelId == channelName then
        self.currentChannelId = nil
        self.channelFilter = nil
    end
    self.speakerData = {}
end
function ZO_VoiceChatHUDGamepadManager:OnOtherPlayerJoinedChannel(channelName, userName, characterName, isSpeaking)
    if isSpeaking then
        self:OnUserSpeaking(channelName, userName, characterName, isSpeaking)
    end
end
function ZO_VoiceChatHUDGamepadManager:OnNewChannelAvailable(channelName, isMuted, isJoined, isTransmitting)
    local channelType, _, _ = GetVoiceChannelInfo(channelName)
    if isJoined and isTransmitting then
        self.currentChannelId = channelName
        self.channelFilter = channelType
    end
end
function ZO_VoiceChatHUDGamepadManager:OnOtherPlayerLeftChannel(channelName, userName)
    if self:IsUserSpeaking(userName) then
        self:RemoveName(userName)
    end
end
----------------------
-- XML Calls
----------------------
     VOICE_CHAT_HUD_GAMEPAD = ZO_VoiceChatHUDGamepadManager:New(control)
end