addon/Addon/Code/Speech.lua

95 lines
No EOL
2.7 KiB
Lua
Executable file

local _, addon = ...
local messages = {}
local GAP = 2
local function ReflowMessages()
local offsetY = 0
for i = #messages, 1, -1 do
local Frame = messages[i]
Frame:ClearAllPoints()
Frame:SetPoint("BOTTOMLEFT", addon.SpeechFrame, "BOTTOMLEFT", 0, offsetY)
Frame:Show()
offsetY = offsetY + Frame:GetHeight() + GAP
end
if #messages == 0 then
addon.SpeechFrame:Hide()
end
end
local function RemoveMessage(frame)
for i, fs in ipairs(messages) do
if fs == frame then
local animation = fs:CreateAnimationGroup()
local fadeOut = animation:CreateAnimation("Alpha")
fadeOut:SetFromAlpha(1)
fadeOut:SetToAlpha(0)
fadeOut:SetDuration(1)
fadeOut:SetStartDelay(0)
animation:SetScript("OnFinished", function()
table.remove(messages, i)
fs:Hide()
ReflowMessages()
end)
animation:Play()
break
end
end
end
local function AddMessage(sender, message)
local text = string.format("|cffffd200%s|r\n%s", sender, message)
local MessageFrame = addon.SpeechFrame:CreateMessage(text)
-- put into table and rerender while queue
table.insert(messages, MessageFrame)
ReflowMessages()
-- hide after timeout
C_Timer.After(20, function()
RemoveMessage(MessageFrame)
end)
end
local function ShowSpeechTranslation(sender, message)
local text = CzechQuestsAddon:GetData("speech", message)
if text == nil and CzechQuestsAddon_Store.config.SPEECH_ORIGINAL_WHEN_MISSING then
text = message
end
if text ~= nil then
addon.SpeechFrame:Show()
AddMessage(sender, text)
end
end
local function InitSpeeches()
local frame = addon.SpeechFrame
-- Register Speech events
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("CHAT_MSG_MONSTER_SAY")
frame:RegisterEvent("CHAT_MSG_MONSTER_YELL")
frame:RegisterEvent("CHAT_MSG_MONSTER_EMOTE")
frame:RegisterEvent("CHAT_MSG_MONSTER_PARTY")
frame:RegisterEvent("CHAT_MSG_MONSTER_WHISPER")
frame:SetScript("OnEvent", function(self, event, message, sender)
if not CzechQuestsAddon_Store.config.SPEECH_ENABLED then
return
end
if event == "PLAYER_LOGIN" then
frame:Init()
end
if event == "CHAT_MSG_MONSTER_SAY"
or event == "CHAT_MSG_MONSTER_YELL"
or event == "CHAT_MSG_MONSTER_EMOTE"
or event == "CHAT_MSG_MONSTER_PARTY"
or event == "CHAT_MSG_MONSTER_WHISPER" then
ShowSpeechTranslation(sender, message)
end
end)
end
addon.API.InitSpeeches = InitSpeeches