addon/Quests/Quests.lua

265 lines
9.6 KiB
Lua
Executable file

local lastQuestFrameEvent = nil
local addonName, addonDataTable = ...
local function ResolveFemaleVersion(maleVersion, femaleVersion)
local gender = UnitSex("player")
if gender == 2 then
return maleVersion
end
if gender == 3 then
if (femaleVersion == "") then
return maleVersion
else
return femaleVersion
end
end
end
local function GetDataByKey(dataType, dataId)
if addonDataTable[dataType] then
if dataType == "questData" then
if addonDataTable[dataType][dataId] then
local quest = addonDataTable[dataType][dataId];
local standardQuest = {
title = ResolveFemaleVersion(quest.titleMale, quest.titleFemale),
objective = ResolveFemaleVersion(quest.objectiveMale, quest.objectiveFemale),
description = ResolveFemaleVersion(quest.descriptionMale, quest.descriptionFemale),
progress = ResolveFemaleVersion(quest.progressMale, quest.progressFemale),
completion = ResolveFemaleVersion(quest.completionMale, quest.completionFemale),
}
return standardQuest
end
elseif addonDataTable[dataType][dataId] then
return addonDataTable[dataType][dataId]
end
end
return nil
end
local function replacePlaceholders(text)
if text == nil then
return text
end
local playerName = UnitName("player")
local _, playerClass = UnitClass("player")
local _, playerRace = UnitRace("player")
local class = GetDataByKey("classData", playerClass);
local race = GetDataByKey("raceData", playerRace);
local formatted = text;
formatted = string.gsub(formatted, '<name>', playerName);
formatted = string.gsub(formatted, '<cF0>', class.f0);
formatted = string.gsub(formatted, '<cF1>', class.f1);
formatted = string.gsub(formatted, '<cF2>', class.f2);
formatted = string.gsub(formatted, '<cF3>', class.f3);
formatted = string.gsub(formatted, '<cF4>', class.f4);
formatted = string.gsub(formatted, '<cF5>', class.f5);
formatted = string.gsub(formatted, '<cM0>', class.m0);
formatted = string.gsub(formatted, '<cM1>', class.m1);
formatted = string.gsub(formatted, '<cM2>', class.m2);
formatted = string.gsub(formatted, '<cM3>', class.m3);
formatted = string.gsub(formatted, '<cM4>', class.m4);
formatted = string.gsub(formatted, '<rF0>', race.f0);
formatted = string.gsub(formatted, '<rF1>', race.f1);
formatted = string.gsub(formatted, '<rF2>', race.f2);
formatted = string.gsub(formatted, '<rF3>', race.f3);
formatted = string.gsub(formatted, '<rF4>', race.f4);
formatted = string.gsub(formatted, '<rF5>', race.f5);
formatted = string.gsub(formatted, '<rM0>', race.m0);
formatted = string.gsub(formatted, '<rM1>', race.m1);
formatted = string.gsub(formatted, '<rM2>', race.m2);
formatted = string.gsub(formatted, '<rM3>', race.m3);
formatted = string.gsub(formatted, '<rM4>', race.m4);
return formatted
end
local function ShowTranslatedQuestFrame(questId, headerText, objectiveText, descriptionHeader, descriptionText, parentFrame, yOffset)
objectiveText = replacePlaceholders(objectiveText);
descriptionText = replacePlaceholders(descriptionText);
if CzechQuestsOptions["DEBUG_MODE"] then
headerText = headerText .. " (" .. questId .. ")"
end
TranslationInfoFramePrimaryHeader:SetText(headerText:upper())
TranslationInfoFramePrimaryText:SetText(objectiveText)
TranslationInfoFrameSecondaryHeader:SetText(descriptionHeader:upper())
TranslationInfoFrameSecondaryText:SetText(descriptionText)
textTopMargin = -TranslationInfoFramePrimaryHeader:GetHeight() - 15
descriptionHeaderTopMargin = textTopMargin - TranslationInfoFramePrimaryText:GetHeight() - 20
descriptionTextTopMargin = descriptionHeaderTopMargin - TranslationInfoFrameSecondaryHeader:GetHeight() - 5
local heightPadding = 10
local addPadding = function(text, value)
if text ~= "" then
heightPadding = heightPadding + value
end
end
addPadding(headerText, 10)
addPadding(objectiveText, 5)
addPadding(descriptionHeader, 25)
addPadding(descriptionText, 5)
TranslationInfoFramePrimaryHeader:SetPoint("TOPLEFT", 10, -10)
TranslationInfoFramePrimaryText:SetPoint("TOPLEFT", 10, textTopMargin)
TranslationInfoFrameSecondaryHeader:SetPoint("TOPLEFT", 10, descriptionHeaderTopMargin)
TranslationInfoFrameSecondaryText:SetPoint("TOPLEFT", 10, descriptionTextTopMargin)
TranslationInfoFrame:SetPoint("TOPLEFT", parentFrame, "TOPRIGHT", -15, yOffset)
TranslationInfoFrame:SetParent(parentFrame)
TranslationInfoFrame:SetHeight(
TranslationInfoFramePrimaryHeader:GetHeight() +
TranslationInfoFramePrimaryText:GetHeight() +
TranslationInfoFrameSecondaryHeader:GetHeight() +
TranslationInfoFrameSecondaryText:GetHeight() +
heightPadding
)
end
local function ShowTranslatedQuestItemFrame(itemText, parentFrame, yOffset)
itemText = replacePlaceholders(itemText);
TranslationInfoFramePrimaryText:SetText(itemText)
TranslationInfoFramePrimaryText:SetPoint("TOPLEFT", 10, 5)
TranslationInfoFrame:SetPoint("TOPLEFT", parentFrame, "TOPRIGHT", -15, yOffset)
TranslationInfoFrame:SetParent(parentFrame)
TranslationInfoFrame:SetHeight(TranslationInfoFramePrimaryText:GetHeight() + 30)
TranslationInfoFramePrimaryText:SetPoint("TOPLEFT", 10, -10)
TranslationInfoFrame:SetPoint("TOPLEFT", parentFrame, "TOPRIGHT", -15, yOffset)
end
local function UpdateTranslationInfoFrame(frameEvent)
local selectedQuestIndex, questId, questData, itemName
if TranslationInfoFrame:IsShown() then
TranslationInfoFrame:Hide()
end
if QuestLogFrame:IsShown() then
selectedQuestIndex = GetQuestLogSelection()
if selectedQuestIndex > 0 then
questId = select(8, GetQuestLogTitle(selectedQuestIndex))
questData = GetDataByKey("questData", questId)
if questData then
TranslationInfoFrame:Show()
ShowTranslatedQuestFrame(
questId,
questData.title,
questData.objective,
addonDataTable.translations["description"],
questData.description,
QuestLogFrame,
-80
)
else
TranslationInfoFrame:Hide()
end
end
end
if QuestFrame:IsShown() then
questId = GetQuestID()
if questId then
questData = GetDataByKey("questData", questId)
if questData then
TranslationInfoFrame:Show()
if frameEvent == "QUEST_PROGRESS" then
ShowTranslatedQuestFrame(
questId,
questData.title,
questData.progress,
"",
"",
QuestFrame,
-80
)
elseif frameEvent == "QUEST_COMPLETE" then
ShowTranslatedQuestFrame(
questId,
questData.title,
questData.completion,
"",
"",
QuestFrame,
-80
)
elseif frameEvent == "QUEST_DETAIL" then
ShowTranslatedQuestFrame(
questId,
questData.title,
questData.description,
addonDataTable.translations["objectives"],
questData.objective,
QuestFrame,
-80
)
elseif frameEvent == "QUEST_FINISHED" then
TranslationInfoFrame:Hide()
end
else
TranslationInfoFrame:Hide()
end
end
end
if ItemTextFrame:IsShown() then
itemName = ItemTextGetItem();
local itemText = GetDataByKey("questItems", itemName)
TranslationInfoFrame:Show()
ShowTranslatedQuestItemFrame(itemText, ItemTextFrame, -80)
end
end
TranslationInfoFrame:SetScript("OnEvent", function(self, event, ...)
if event == "QUEST_PROGRESS" or event == "QUEST_COMPLETE" or event == "QUEST_FINISHED" or event == "QUEST_DETAIL" then
UpdateTranslationInfoFrame(event)
end
if (event == "ITEM_TEXT_READY") then
UpdateTranslationInfoFrame(event)
end
end)
-- Hover is enabled only for QuestLogFrame because i do know event for change quest from list
local function SetHoverScripts(frame, children)
local frameName = frame:GetName()
if not string.find(frameName, "QuestLogItem") and not string.find(frameName, "QuestProgressItem") then
frame:SetScript("OnEnter", function()
UpdateTranslationInfoFrame()
end)
frame:SetScript("OnLeave", function()
TranslationInfoFrame:Hide()
end)
if children then
for i = 1, frame:GetNumChildren() do
local child = select(i, frame:GetChildren())
SetHoverScripts(child, true)
end
end
end
end
SetHoverScripts(QuestLogFrame, false)