You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
9.5 KiB

4 years ago
--- Kaliel's Tracker
5 months ago
--- Copyright (c) 2012-2025, Marouan Sabbagh <mar.sabbagh@gmail.com>
4 years ago
--- All Rights Reserved.
---
--- This file is part of addon Kaliel's Tracker.
5 months ago
---@type KT
4 years ago
local addonName, KT = ...
5 months ago
---@class ActiveButton
local M = KT:NewModule("ActiveButton")
4 years ago
KT.ActiveButton = M
local _DBG = function(...) if _DBG then _DBG("KT", ...) end end
local db, dbChar
local KTF = KT.frame
local eventFrame
local activeFrame, abutton
local blizzardButtonIconID = 0
4 years ago
local isBartender, isElvui, isTukui = false, false, false
5 months ago
local DEFAULT_ICON = "Interface\\Icons\\INV_Misc_QuestionMark"
-- Internal ------------------------------------------------------------------------------------------------------------
local function UpdateBlizzardButtonIconID()
local iconID = 0
if HasExtraActionBar() then
local button = ExtraActionBarFrame.button
local actionType, id = GetActionInfo(button.action)
if actionType == "spell" then
iconID = C_Spell.GetSpellTexture(id)
end
end
blizzardButtonIconID = iconID
end
4 years ago
local function UpdateHotkey()
local key = GetBindingKey("EXTRAACTIONBUTTON1")
local button = KTF.ActiveButton
local hotkey = button.HotKey
local hotkeyExtra = ExtraActionButton1.HotKey
local text = db.qiActiveButtonBindingShow and GetBindingText(key, 1) or ""
ClearOverrideBindings(button)
if key then
hotkeyExtra:SetText(RANGE_INDICATOR)
hotkeyExtra:Hide()
SetOverrideBindingClick(button, false, key, button:GetName())
end
if text == "" then
hotkey:SetText(RANGE_INDICATOR)
hotkey:Hide()
else
hotkey:SetText(text)
hotkey:Show()
end
end
local function RemoveHotkey(button)
local key = GetBindingKey("EXTRAACTIONBUTTON1")
local hotkeyExtra = ExtraActionButton1.HotKey
if key then
hotkeyExtra:SetText(GetBindingText(key, 1))
hotkeyExtra:Show()
ClearOverrideBindings(button)
end
end
5 months ago
local function EventFrame_OnUpdate(self)
if KTF.Buttons:IsShown() then
if self.timer > 50 then
KT:prot("Update", M)
self.timer = 0
else
self.timer = self.timer + TOOLTIP_UPDATE_TIME
end
end
end
local function ActiveFrame_SetPosition()
local point, relativeTo, relativePoint, xOfs, yOfs = "BOTTOM", UIParent, "BOTTOM", 0, 285
if db.qiActiveButtonPosition then
point, relativeTo, relativePoint, xOfs, yOfs = unpack(db.qiActiveButtonPosition)
5 months ago
if point ~= "CENTER" then
if point ~= "TOP" and point ~= "BOTTOM" then
local xOfsMod = activeFrame:GetWidth() / 2
if point == "TOPLEFT" or point == "BOTTOMLEFT" or point == "LEFT" then
xOfs = max(xOfs, -1 * xOfsMod)
else
xOfs = min(xOfs, xOfsMod)
end
end
if point ~= "LEFT" and point ~= "RIGHT" then
local yOfsMod = activeFrame:GetHeight() / 2
if point == "TOPLEFT" or point == "TOPRIGHT" or point == "TOP" then
yOfs = min(yOfs, yOfsMod)
else
yOfs = max(yOfs, -1 * yOfsMod)
end
end
end
else
if isBartender then
yOfs = yOfs - 40
elseif isElvui then
yOfs = yOfs -14
elseif isTukui then
yOfs = yOfs + 26
4 years ago
end
end
5 months ago
KT:prot("ClearAllPoints", activeFrame)
KT:prot("SetPoint", activeFrame, point, relativeTo, relativePoint, xOfs, yOfs)
4 years ago
end
local function ActiveFrame_Hide()
if activeFrame:IsShown() then
activeFrame:Hide()
5 months ago
abutton.text:SetText("")
RemoveHotkey(abutton)
end
end
4 years ago
local function SetFrames()
-- Event frame
if not eventFrame then
eventFrame = CreateFrame("Frame")
5 months ago
eventFrame.timer = 0
eventFrame:SetScript("OnEvent", function(_, event)
4 years ago
_DBG("Event - "..event, true)
5 months ago
if event == "UPDATE_EXTRA_ACTIONBAR" then
UpdateBlizzardButtonIconID()
5 months ago
KT:prot("Update", M)
4 years ago
elseif event == "UPDATE_BINDINGS" then
if activeFrame:IsShown() then
UpdateHotkey()
end
elseif event == "PET_BATTLE_OPENING_START" then
KT:prot("Hide", activeFrame)
elseif event == "PET_BATTLE_CLOSE" then
5 months ago
KT:prot("Update", M)
4 years ago
end
end)
5 months ago
eventFrame:SetScript("OnUpdate", EventFrame_OnUpdate)
4 years ago
end
eventFrame:RegisterEvent("UPDATE_EXTRA_ACTIONBAR")
4 years ago
eventFrame:RegisterEvent("UPDATE_BINDINGS")
eventFrame:RegisterEvent("PET_BATTLE_OPENING_START")
eventFrame:RegisterEvent("PET_BATTLE_CLOSE")
-- Main frame
5 months ago
activeFrame = KTF.ActiveFrame
if not activeFrame then
activeFrame = CreateFrame("Frame", nil, UIParent)
activeFrame:SetSize(256, 128)
activeFrame:SetFrameLevel(80)
4 years ago
activeFrame:Hide()
KTF.ActiveFrame = activeFrame
end
5 months ago
ActiveFrame_SetPosition()
4 years ago
5 months ago
-- Mover
local mover = KT:Mover_Create(addonName, KTF.ActiveFrame)
function mover:Show()
self.mixin.Show(self)
self.frame:Show()
end
function mover:Hide()
self.mixin.Hide(self)
M:Update()
end
function mover:OnDragStop(frame)
db.qiActiveButtonPosition = { frame:GetPoint() }
ActiveFrame_SetPosition()
end
function mover:OnMouseUp(frame, button)
if button == "RightButton" then
db.qiActiveButtonPosition = nil
ActiveFrame_SetPosition()
end
end
4 years ago
-- Button frame
if not KTF.ActiveButton then
5 months ago
local button = CreateFrame("Button", addonName.."ActiveButton", activeFrame, "SecureActionButtonTemplate")
4 years ago
button:SetSize(52, 52)
5 months ago
button:SetPoint("CENTER")
5 months ago
button.icon = button:CreateTexture(nil, "BACKGROUND")
4 years ago
button.icon:SetPoint("TOPLEFT", 0, -1)
button.icon:SetPoint("BOTTOMRIGHT", 0, -1)
5 months ago
button.Style = button:CreateTexture(nil, "OVERLAY")
4 years ago
button.Style:SetSize(256, 128)
button.Style:SetPoint("CENTER", -2, 0)
button.Style:SetTexture("Interface\\ExtraButton\\ChampionLight")
5 months ago
button.Count = button:CreateFontString(nil, "OVERLAY", "NumberFontNormal")
4 years ago
button.Count:SetJustifyH("RIGHT")
button.Count:SetPoint("BOTTOMRIGHT", button.icon, -4, 4)
5 months ago
button.Cooldown = CreateFrame("Cooldown", nil, button, "CooldownFrameTemplate")
4 years ago
button.Cooldown:ClearAllPoints()
button.Cooldown:SetPoint("TOPLEFT", 4, -4)
button.Cooldown:SetPoint("BOTTOMRIGHT", -3, 2)
5 months ago
button.HotKey = button:CreateFontString(nil, "ARTWORK", "NumberFontNormalSmallGray")
4 years ago
button.HotKey:SetSize(30, 10)
button.HotKey:SetJustifyH("RIGHT")
button.HotKey:SetText(RANGE_INDICATOR)
button.HotKey:SetPoint("TOPRIGHT", button.icon, -2, -7)
5 months ago
button.text = button:CreateFontString(nil, "ARTWORK", "NumberFontNormalSmall")
4 years ago
button.text:SetSize(20, 10)
button.text:SetJustifyH("LEFT")
button.text:SetPoint("TOPLEFT", button.icon, 4, -7)
button:SetScript("OnEvent", KT.ItemButton.OnEvent)
button:SetScript("OnUpdate", KT.ItemButton.OnUpdate)
button:SetScript("OnShow", KT.ItemButton.OnShow)
button:SetScript("OnHide", KT.ItemButton.OnHide)
button:SetScript("OnEnter", KT.ItemButton.OnEnter)
button:SetScript("OnLeave", KT.ItemButton.OnLeave)
button:RegisterForClicks("AnyDown", "AnyUp")
button:SetAttribute("type", "item")
4 years ago
button:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
do local tex = button:GetPushedTexture()
tex:SetPoint("TOPLEFT", 0, -1)
tex:SetPoint("BOTTOMRIGHT", 0, -1)
end
button:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square", "ADD")
do local tex = button:GetHighlightTexture()
tex:SetPoint("TOPLEFT", 0, -1)
tex:SetPoint("BOTTOMRIGHT", 0, -1)
end
5 months ago
SetItemButtonTexture(button, DEFAULT_ICON)
4 years ago
KT:Masque_AddButton(button, 2)
KTF.ActiveButton = button
end
abutton = KTF.ActiveButton
end
5 months ago
-- External ------------------------------------------------------------------------------------------------------------
4 years ago
function M:OnInitialize()
_DBG("|cffffff00Init|r - "..self:GetName(), true)
5 months ago
4 years ago
db = KT.db.profile
dbChar = KT.db.char
-- Cleanup (temporarily)
dbChar.activeButtonPosition = nil
4 years ago
end
function M:OnEnable()
_DBG("|cff00ff00Enable|r - "..self:GetName(), true)
isBartender = C_AddOns.IsAddOnLoaded("Bartender4")
isElvui = C_AddOns.IsAddOnLoaded("ElvUI")
isTukui = C_AddOns.IsAddOnLoaded("Tukui")
4 years ago
SetFrames()
5 months ago
KT:RegSignal("BUTTONS_UPDATED", "prot", KT, "Update", self)
4 years ago
end
function M:Update(id)
5 months ago
if KT.EditMode.opened then return end
4 years ago
5 months ago
local closestQuestID = id
if not closestQuestID then
if not dbChar.collapsed then
5 months ago
local superTrackedQuestID = C_SuperTrack.GetSuperTrackedQuestID() or 0
for questID in pairs(KT.fixedButtons) do
if questID == superTrackedQuestID then
4 years ago
closestQuestID = questID
break
end
5 months ago
if C_Minimap.IsInsideQuestBlob(questID) then
closestQuestID = questID
4 years ago
end
end
end
end
5 months ago
if closestQuestID and not KT.EXCLUDED_QUEST_ITEMS[closestQuestID] then
4 years ago
local button = KT:GetFixedButton(closestQuestID)
5 months ago
if button and button.item ~= blizzardButtonIconID then
local autoShowTooltip = false
if GameTooltip:IsShown() and GameTooltip:GetOwner() == abutton then
KT.ItemButton.OnLeave(abutton)
autoShowTooltip = true
end
4 years ago
abutton.block = button.block
5 months ago
abutton:SetAttribute("questLogIndex", button:GetAttribute("questLogIndex"))
abutton:SetAttribute("questID", closestQuestID)
abutton.charges = button.charges
abutton.rangeTimer = button.rangeTimer
abutton.item = button.item
abutton.link = button.link
SetItemButtonTexture(abutton, button.item)
SetItemButtonCount(abutton, button.charges)
KT.ItemButton.UpdateCooldown(abutton)
abutton.text:SetText(button.num)
abutton:SetAttribute("item", button.link)
if not activeFrame:IsShown() then
UpdateHotkey()
activeFrame:SetShown(not KT.locked)
end
4 years ago
if autoShowTooltip then
KT.ItemButton.OnEnter(abutton)
end
else
ActiveFrame_Hide()
4 years ago
end
else
ActiveFrame_Hide()
4 years ago
end
end