Code refactory

This commit is contained in:
Salikh Gurgenidze
2025-11-19 19:22:52 +04:00
parent 0ff0aed647
commit a506b88f47
6 changed files with 109 additions and 162 deletions
+59
View File
@@ -52,11 +52,70 @@ addon.Constants = {
BUTTONS_PER_ROW = 10,
MIN_ICON_SIZE = 30,
MAX_ICON_SIZE = 64,
-- Backdrop configurations (to avoid duplication across UI files)
Backdrops = {
-- Standard frame backdrop (used for main windows)
DEFAULT_FRAME = {
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
},
-- Minimalist border (used when "hide borders" setting is enabled)
MINIMALIST_BORDER = {
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 32,
edgeSize = 2,
insets = { left = 0, right = 0, top = 0, bottom = 0 }
},
-- Dropdown/popup backdrop (used for character selection dropdowns)
DROPDOWN = {
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 16,
edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
}
},
-- Backdrop colors (common configurations)
BackdropColors = {
DEFAULT = {r = 0, g = 0, b = 0, a = 0.9},
DROPDOWN = {r = 0, g = 0, b = 0, a = 0.95},
},
}
-- Initialize modules storage
addon.Modules = {}
-- Helper to apply backdrop with color
function addon:ApplyBackdrop(frame, backdropType, colorType)
local backdrop = self.Constants.Backdrops[backdropType]
local color = self.Constants.BackdropColors[colorType or "DEFAULT"]
if not backdrop or not color then
self:Debug("Invalid backdrop type: %s or color type: %s",
tostring(backdropType), tostring(colorType))
return
end
frame:SetBackdrop(backdrop)
frame:SetBackdropColor(color.r, color.g, color.b, color.a)
-- Set border color to white for minimalist borders
if backdropType == "MINIMALIST_BORDER" then
frame:SetBackdropBorderColor(1, 1, 1, 1)
end
end
-- Print function with addon prefix
function addon:Print(msg, a1, a2, a3, a4, a5)
local text = string.format(msg, a1, a2, a3, a4, a5)
+37 -39
View File
@@ -47,32 +47,34 @@ end
-- Get item info with caching
local itemCache = {}
local function DebugGetItemInfo(itemID, itemName)
if not itemID then
addon:Print("NO ITEM ID for: " .. tostring(itemName))
return
end
local itemName, itemLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount,
itemSubType, itemTexture, itemEquipLoc, itemSellPrice = GetItemInfo(itemID)
addon:Print("=== GetItemInfo DEBUG ===")
addon:Print(string.format("Input ItemID: %d", itemID))
addon:Print(string.format("Input Name: %s", tostring(itemName)))
addon:Print("--- Return Values ---")
addon:Print(string.format("1. itemName: '%s'", tostring(itemName)))
addon:Print(string.format("2. itemLink: '%s'", tostring(itemLink)))
addon:Print(string.format("3. itemRarity: %s", itemRarity or 0))
addon:Print(string.format("4. itemLevel: %s", itemLevel or 0))
addon:Print(string.format("5. itemMinLevel: %s", itemMinLevel or 0))
addon:Print(string.format("6. itemType: '%s'", tostring(itemType)))
addon:Print(string.format("7. itemSubType: '%s'", tostring(itemSubType)))
addon:Print(string.format("8. itemStackCount: %s", itemStackCount or 0))
addon:Print(string.format("9. itemEquipLoc: '%s'", tostring(itemEquipLoc)))
addon:Print(string.format("10. itemTexture: '%s'", tostring(itemTexture)))
addon:Print(string.format("11. itemSellPrice: %s", itemSellPrice or 0))
addon:Print("=====================")
-- Extract itemID from itemLink (Lua 5.0 compatible)
-- Returns: itemID as number, or nil if extraction fails
function Utils:ExtractItemID(itemLink)
if not itemLink then return nil end
local _, _, itemID = string.find(itemLink, "item:(%d+)")
return itemID and tonumber(itemID) or nil
end
-- Get item info with error handling (does not cache)
-- Turtle WoW GetItemInfo signature:
-- itemName, itemLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice
function Utils:GetItemInfoSafe(itemID)
if not itemID then
addon:Debug("GetItemInfoSafe: nil itemID")
return nil
end
local itemName, itemLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice = GetItemInfo(itemID)
if not itemName then
addon:Debug("GetItemInfoSafe: GetItemInfo failed for itemID %d", itemID)
return nil
end
return itemName, itemLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice
end
-- Get item info with caching (wrapper for backward compatibility)
function Utils:GetItemInfo(itemLink)
if not itemLink then return nil end
@@ -80,13 +82,11 @@ function Utils:GetItemInfo(itemLink)
return unpack(itemCache[itemLink])
end
-- Extract itemID from itemLink
local _, _, itemID = string.find(itemLink, "item:(%d+)")
local itemID = self:ExtractItemID(itemLink)
if not itemID then return nil end
-- Turtle WoW GetItemInfo signature:
-- itemName, itemLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice
local itemName, retLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice = GetItemInfo(tonumber(itemID))
local itemName, retLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice = self:GetItemInfoSafe(itemID)
if itemName then
itemCache[itemLink] = {itemName, retLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice}
return itemName, retLink, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice
@@ -259,14 +259,12 @@ function Utils:GetSpecializedBagType(bagID)
return nil
end
-- Extract itemID from link
local _, _, itemID = string.find(link, "item:(%d+)")
local itemID = self:ExtractItemID(link)
if not itemID then
return nil
end
-- Use GetItemInfo to get subType (Turtle WoW signature)
local itemName, _, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType = GetItemInfo(tonumber(itemID))
local itemName, _, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType = self:GetItemInfoSafe(itemID)
if itemType then
-- Check for exact subtype matches
local typeLower = string.lower(itemType)
@@ -310,8 +308,8 @@ local SOUL_SHARD_ID = 6265
-- Check if item is a Soul Shard
function Utils:IsSoulShard(itemLink)
if not itemLink then return false end
local _, _, itemID = string.find(itemLink, "item:(%d+)")
return tonumber(itemID) == SOUL_SHARD_ID
local itemID = self:ExtractItemID(itemLink)
return itemID == SOUL_SHARD_ID
end
-- Extract hyperlink from item link for tooltip scanning
@@ -344,11 +342,11 @@ function Utils:GetItemPreferredContainer(itemLink)
return "soul"
end
-- Extract itemID and get item info
local _, _, itemID = string.find(itemLink, "item:(%d+)")
-- Get item info using utility function
local itemID = self:ExtractItemID(itemLink)
if not itemID then return nil end
local itemName, _, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType = GetItemInfo(tonumber(itemID))
local itemName, _, itemRarity, itemLevel, itemCategory, itemType, itemStackCount, itemSubType = self:GetItemInfoSafe(itemID)
if not itemType then return nil end
-- Only route PROJECTILE category items that are specifically arrows or bullets
+2 -3
View File
@@ -36,11 +36,10 @@ function EquipmentScanner:ScanEquippedItems()
local itemLink = GetInventoryItemLink("player", slotID)
if itemLink then
-- Extract itemID from link
local _, _, itemID = string.find(itemLink, "item:(%d+)")
local itemID = addon.Modules.Utils:ExtractItemID(itemLink)
local itemName
if itemID then
itemName = GetItemInfo(tonumber(itemID))
itemName = addon.Modules.Utils:GetItemInfoSafe(itemID)
end
equipped[slotName] = {
link = itemLink,
+3 -59
View File
@@ -1278,28 +1278,11 @@ function BagFrame:UpdateBorderVisibility()
hideBorders = false
end
-- Use helper function with constants
if hideBorders then
-- Hide decorative borders but add thin white border
frame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 32,
edgeSize = 2,
insets = { left = 0, right = 0, top = 0, bottom = 0 }
})
frame:SetBackdropColor(0, 0, 0, 0.9)
frame:SetBackdropBorderColor(1, 1, 1, 1)
addon:ApplyBackdrop(frame, "MINIMALIST_BORDER", "DEFAULT")
else
frame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
frame:SetBackdropColor(0, 0, 0, 0.9)
addon:ApplyBackdrop(frame, "DEFAULT_FRAME", "DEFAULT")
end
end
@@ -1396,42 +1379,6 @@ local function ReplaceBagOpenFunctions()
end
-- Update your existing HookDefaultBags function to be more comprehensive
local function HookDefaultBags()
-- Override ToggleBackpack
local originalToggleBackpack = ToggleBackpack
function ToggleBackpack()
BagFrame:Toggle()
end
-- Override OpenAllBags
local originalOpenAllBags = OpenAllBags
function OpenAllBags()
Guda_BagFrame:Show()
end
-- Override CloseAllBags
local originalCloseAllBags = CloseAllBags
function CloseAllBags()
Guda_BagFrame:Hide()
end
-- Use the container hook approach
HookBagContainers()
-- OR use the function replacement approach (comment out one)
ReplaceBagOpenFunctions()
end
-- Also add this to your initialization to ensure it runs after UI is loaded
local function DelayedHook()
-- Wait a bit for the UI to load completely
local frame = CreateFrame("Frame")
frame:SetScript("OnUpdate", function()
frame:SetScript("OnUpdate", nil)
HookBagContainers()
end)
end
-- Bag Slot Button Handlers
-- OnLoad handler for bag slot buttons
@@ -1741,9 +1688,6 @@ function BagFrame:Initialize()
-- Hook default bag functions
HookDefaultBags()
-- Delayed hook for bag containers (runs after UI loads)
DelayedHook()
-- Update on bag changes
addon.Modules.Events:OnBagUpdate(function()
if not currentViewChar then
+3 -20
View File
@@ -754,28 +754,11 @@ function BankFrame:UpdateBorderVisibility()
hideBorders = false
end
-- Use helper function with constants
if hideBorders then
-- Hide decorative borders but add thin white border
frame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 32,
edgeSize = 2,
insets = { left = 0, right = 0, top = 0, bottom = 0 }
})
frame:SetBackdropColor(0, 0, 0, 0.9)
frame:SetBackdropBorderColor(1, 1, 1, 1)
addon:ApplyBackdrop(frame, "MINIMALIST_BORDER", "DEFAULT")
else
frame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
frame:SetBackdropColor(0, 0, 0, 0.9)
addon:ApplyBackdrop(frame, "DEFAULT_FRAME", "DEFAULT")
end
end
+5 -41
View File
@@ -364,58 +364,22 @@ function Guda_SettingsPopup_HideBordersCheckbox_OnClick(self)
Guda.Modules.DB:SetSetting("hideBorders", isChecked)
end
-- Update border visibility on both bag and bank frames
-- Update border visibility on both bag and bank frames using helper function
local bagFrame = getglobal("Guda_BagFrame")
if bagFrame then
if isChecked then
-- Hide decorative borders but add thin white border
bagFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 12,
edgeSize = 2,
insets = { left = 0, right = 0, top = 0, bottom = 0 }
})
bagFrame:SetBackdropColor(0, 0, 0, 0.9)
bagFrame:SetBackdropBorderColor(1, 1, 1, 1)
Guda:ApplyBackdrop(bagFrame, "MINIMALIST_BORDER", "DEFAULT")
else
bagFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
bagFrame:SetBackdropColor(0, 0, 0, 0.9)
Guda:ApplyBackdrop(bagFrame, "DEFAULT_FRAME", "DEFAULT")
end
end
local bankFrame = getglobal("Guda_BankFrame")
if bankFrame then
if isChecked then
-- Hide decorative borders but add thin white border
bankFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 32,
edgeSize = 2,
insets = { left = 0, right = 0, top = 0, bottom = 0 }
})
bankFrame:SetBackdropColor(0, 0, 0, 0.9)
bankFrame:SetBackdropBorderColor(1, 1, 1, 1)
Guda:ApplyBackdrop(bankFrame, "MINIMALIST_BORDER", "DEFAULT")
else
bankFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
bankFrame:SetBackdropColor(0, 0, 0, 0.9)
Guda:ApplyBackdrop(bankFrame, "DEFAULT_FRAME", "DEFAULT")
end
end
end