Skip to content

Commit

Permalink
♻️ Optimized Action Bars & Micro Menu
Browse files Browse the repository at this point in the history
🚀 Improved performance by caching globals and reducing redundant lookups.
🔄 Reused common functions and cleaned up unnecessary comments.
  • Loading branch information
Kkthnx committed Aug 19, 2024
1 parent 1e39693 commit fc5cb5e
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 230 deletions.
53 changes: 33 additions & 20 deletions KkthnxUI/Libraries/oUF/elements/range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,25 @@ local function Enable(self)
if element then
element.__owner = self
element.insideAlpha = element.insideAlpha or 1
element.outsideAlpha = element.outsideAlpha or 0.55

-- self:RegisterEvent("UNIT_IN_RANGE_UPDATE", Path)

if not OnRangeFrame then
OnRangeFrame = CreateFrame("Frame")
OnRangeFrame:SetScript("OnUpdate", OnRangeUpdate)
element.outsideAlpha = element.outsideAlpha or 0.35

-- Ensure that KkthnxUI[2]["Unitframe"] exists before checking Range
if KkthnxUI and KkthnxUI[2] and KkthnxUI[2]["Unitframe"] then
if not KkthnxUI[2]["Unitframe"].Range then
-- Register the event if Range is not enabled
self:RegisterEvent("UNIT_IN_RANGE_UPDATE", Path)
else
-- Set up OnRangeFrame if Range is enabled
if not OnRangeFrame then
OnRangeFrame = CreateFrame("Frame")
OnRangeFrame:SetScript("OnUpdate", OnRangeUpdate)
end

tinsert(_FRAMES, self)
OnRangeFrame:Show()
end
end

tinsert(_FRAMES, self)
OnRangeFrame:Show()

return true
end
end
Expand All @@ -129,18 +136,24 @@ local function Disable(self)
if element then
self:SetAlpha(element.insideAlpha)

-- self:UnregisterEvent("UNIT_IN_RANGE_UPDATE", Path)

for index, frame in next, _FRAMES do
if frame == self then
tremove(_FRAMES, index)
break
if KkthnxUI and KkthnxUI[2] and KkthnxUI[2]["Unitframe"] then
if not KkthnxUI[2]["Unitframe"].Range then
-- Unregister the event if KkthnxUI[2]["Unitframe"] exists
self:UnregisterEvent("UNIT_IN_RANGE_UPDATE", Path)
else
-- Remove self from _FRAMES and hide OnRangeFrame if _FRAMES is empty
for index, frame in ipairs(_FRAMES) do
if frame == self then
tremove(_FRAMES, index)
break
end
end

if #_FRAMES == 0 and OnRangeFrame then
OnRangeFrame:Hide()
end
end
end

if #_FRAMES == 0 then
OnRangeFrame:Hide()
end
end
end

Expand Down
11 changes: 6 additions & 5 deletions KkthnxUI/Modules/ActionBars/ButtonStyle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,20 @@ function Module:ReskinBars()
Module:StyleActionButton(_G["KKUI_ActionBar" .. i .. "Button" .. j])
end
end
--petbar buttons

-- petbar buttons
for i = 1, NUM_PET_ACTION_SLOTS do
Module:StyleActionButton(_G["PetActionButton" .. i])
end
--stancebar buttons
-- stancebar buttons
for i = 1, 10 do
Module:StyleActionButton(_G["StanceButton" .. i])
end
--leave vehicle
-- leave vehicle
Module:StyleActionButton(_G["KKUI_LeaveVehicleButton"])
--extra action button
-- extra action button
Module:StyleActionButton(ExtraActionButton1)
--spell flyout
-- spell flyout
SpellFlyout.Background:SetAlpha(0)
local numFlyouts = 1

Expand Down
43 changes: 5 additions & 38 deletions KkthnxUI/Modules/ActionBars/Elements/Bars.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ function Module:UpdateActionSize(name)
end
end

-- Update the button configuration for action bars
local directions = { "UP", "DOWN", "LEFT", "RIGHT" }
function Module:UpdateButtonConfig(i)
-- Initialize buttonConfig if it does not exist
if not self.buttonConfig then
self.buttonConfig = {
-- hideElements table will store which elements of the button should be hidden
hideElements = {},
-- text table will store the font and position settings for the button's text elements
text = {
hotkey = {
font = {},
Expand All @@ -154,32 +152,20 @@ function Module:UpdateButtonConfig(i)
},
}
end
-- Set the clickOnDown attribute to true

self.buttonConfig.clickOnDown = true
-- Set the showGrid attribute based on the value of C["ActionBar"]["Grid"]
self.buttonConfig.showGrid = C["ActionBar"]["Grid"]
-- Set the flyoutDirection attribute based on the value of C["ActionBar"]["Bar" .. i .. "Flyout"]
self.buttonConfig.flyoutDirection = directions[C["ActionBar"]["Bar" .. i .. "Flyout"]]

-- Get the hotkey field of the buttonConfig's text table
local hotkey = self.buttonConfig.text.hotkey

-- Set the font field of the hotkey table to the value of K.UIFont
hotkey.font.font = K.UIFont
-- Set the size field of the hotkey table's font field to the value of C["ActionBar"]["Bar" .. i .. "Font"]
hotkey.font.size = C["ActionBar"]["Bar" .. i .. "Font"]
-- Set the flags field of the hotkey table's font field to the value of K.UIFontStyle
hotkey.font.flags = K.UIFontStyle

-- Set the anchor field of the hotkey table's position field to "TOPRIGHT"
hotkey.position.anchor = "TOPRIGHT"
-- Set the relAnchor field of the hotkey table's position field to false
hotkey.position.relAnchor = false
-- Set the offsetX field of the hotkey table's position field to 0
hotkey.position.offsetX = 0
-- Set the offsetY field of the hotkey table's position field to -2
hotkey.position.offsetY = -2
-- Set the justifyH field of the hotkey table to "RIGHT"
hotkey.justifyH = "RIGHT"

-- Initialize the count text configuration
Expand Down Expand Up @@ -221,66 +207,47 @@ function Module:UpdateButtonConfig(i)
hideElements.macro = not C["ActionBar"]["Macro"]
hideElements.equipped = not C["ActionBar"]["EquipColor"]

-- Get the value of the CVAR "lockActionBars"
-- Update button attributes and configuration based on CVAR values and button config
local lockBars = GetCVar("lockActionBars") == "1"
-- Iterate through the buttons
for _, button in next, self.buttons do
-- Set the key bound target for the button and the button config
self.buttonConfig.keyBoundTarget = button.bindName
button.keyBoundTarget = self.buttonConfig.keyBoundTarget

-- Set the button lock attribute based on the CVAR value
button:SetAttribute("buttonlock", lockBars)
-- Set the unlocked prevent drag attribute to the opposite of the button lock attribute
button:SetAttribute("unlockedpreventdrag", not lockBars)
-- Set the check mouseover cast attribute to true
button:SetAttribute("checkmouseovercast", true)
-- Set the check focus cast attribute to true
button:SetAttribute("checkfocuscast", true)
-- Set the check self cast attribute to true
button:SetAttribute("checkselfcast", true)
-- Set the unit 2 attribute to "player"
-- button:SetAttribute("*unit2", "player")

-- Update the config for the button
button:UpdateConfig(self.buttonConfig)
end
end

-- Update action bar visibility and configuration based on settings
local fullPage = "[bar:6]6;[bar:5]5;[bar:4]4;[bar:3]3;[bar:2]2;[possessbar]16;[overridebar]18;[shapeshift]17;[vehicleui]16;[bonusbar:5]11;[bonusbar:4]10;[bonusbar:3]9;[bonusbar:2]8;[bonusbar:1]7;1"

function Module:UpdateBarVisibility()
-- Iterate through the action bars
for i = 1, 8 do
-- Get the frame of the action bar
local frame = _G["KKUI_ActionBar" .. i]
-- Check if the frame exists
if frame then
-- Check if the action bar is enabled in the configuration table
if C["ActionBar"]["Bar" .. i] then
-- Show the frame and enable the mover
frame:Show()
frame.mover.isDisable = false
-- Register the frame with the visibility driver
RegisterStateDriver(frame, "visibility", frame.visibility)
else
-- Hide the frame and disable the mover
frame:Hide()
frame.mover.isDisable = true
-- Unregister the frame from the visibility driver
UnregisterStateDriver(frame, "visibility")
end
end
end
end

function Module:UpdateBarConfig()
-- Iterate through the action bars
for i = 1, 8 do
-- Get the frame of the action bar
local frame = _G["KKUI_ActionBar" .. i]
-- Check if the frame exists
if frame then
-- Update the button configuration for the current frame and action bar number
Module.UpdateButtonConfig(frame, i)
end
end
Expand Down
33 changes: 13 additions & 20 deletions KkthnxUI/Modules/ActionBars/Elements/ExtraQuestButton.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
-- ExtraQuestButton Modification by p3lim for KkthnxUI
local K, C = KkthnxUI[1], KkthnxUI[2]

local K, C, L = KkthnxUI[1], KkthnxUI[2], KkthnxUI[3]

-- Utility functions
local next, type, sqrt, GetTime, format = next, type, sqrt, GetTime, format

-- Game state and action functions
local RegisterStateDriver, InCombatLockdown = RegisterStateDriver, InCombatLockdown

-- Item and action bar related functions
local IsItemInRange, ItemHasRange, HasExtraActionBar = IsItemInRange, ItemHasRange, HasExtraActionBar
local GetItemCooldown, GetItemCount, GetItemIcon, GetItemInfoFromHyperlink = GetItemCooldown, GetItemCount, GetItemIcon, GetItemInfoFromHyperlink

-- Binding and quest related functions
local GetBindingKey, GetBindingText, GetQuestLogSpecialItemInfo, QuestHasPOIInfo = GetBindingKey, GetBindingText, GetQuestLogSpecialItemInfo, QuestHasPOIInfo

-- Map and quest log functions from C_Map and C_QuestLog namespaces
local C_Item_IsItemInRange, C_Item_ItemHasRange, HasExtraActionBar = C_Item.IsItemInRange, C_Item.ItemHasRange, HasExtraActionBar
local C_Item_GetItemCooldown, C_Item_GetItemCount, C_Item_GetItemIconByID, GetItemInfoFromHyperlink = C_Item.GetItemCooldown, C_Item.GetItemCount, C_Item.GetItemIconByID, GetItemInfoFromHyperlink
local GetBindingKey, GetBindingText = GetBindingKey, GetBindingText
local GetQuestLogSpecialItemInfo, QuestHasPOIInfo = GetQuestLogSpecialItemInfo, QuestHasPOIInfo
local C_Map_GetBestMapForUnit = C_Map.GetBestMapForUnit
local C_QuestLog_GetInfo = C_QuestLog.GetInfo
local C_QuestLog_IsOnMap = C_QuestLog.IsOnMap
Expand Down Expand Up @@ -78,7 +69,7 @@ local onAttributeChanged = [[

function ExtraQuestButton:BAG_UPDATE_COOLDOWN()
if self:IsShown() and self.itemID then
local start, duration = GetItemCooldown(self.itemID)
local start, duration = C_Item_GetItemCooldown(self.itemID)
if duration > 0 then
self.Cooldown:SetCooldown(start, duration)
self.Cooldown:Show()
Expand All @@ -90,7 +81,7 @@ end

function ExtraQuestButton:UpdateCount()
if self:IsShown() then
local count = GetItemCount(self.itemLink)
local count = C_Item_GetItemCount(self.itemLink)
self.Count:SetText(tostring(count and count > 1 and count or ""))
end
end
Expand Down Expand Up @@ -228,7 +219,7 @@ ExtraQuestButton:SetScript("OnUpdate", function(self, elapsed)

if not inCombat then -- IsItemInRange is combat restricted now...
-- BUG: IsItemInRange() is broken versus friendly NPCs (and possibly others)
local inRange = IsItemInRange(self.itemLink, "target")
local inRange = C_Item_IsItemInRange(self.itemLink, "target")

if HotKey:GetText() == RANGE_INDICATOR then
if inRange == false then
Expand Down Expand Up @@ -291,7 +282,7 @@ function ExtraQuestButton:SetItem(itemLink)
end

if itemLink then
self.Icon:SetTexture(C_Item.GetItemIconByID(itemLink))
self.Icon:SetTexture(C_Item_GetItemIconByID(itemLink))
local itemID = GetItemInfoFromHyperlink(itemLink)
self.itemID = itemID
self.itemLink = itemLink
Expand All @@ -304,7 +295,7 @@ function ExtraQuestButton:SetItem(itemLink)
if self.itemID then
local HotKey = self.HotKey
local key = GetBindingKey("EXTRAACTIONBUTTON1")
local hasRange = ItemHasRange(itemLink)
local hasRange = C_Item_ItemHasRange(itemLink)
if key then
HotKey:SetText(GetBindingText(key, 1))
HotKey:Show()
Expand Down Expand Up @@ -345,10 +336,12 @@ local function GetQuestDistanceWithItem(questID)
itemLink = format("|Hitem:%d|h", fallbackItemID)
end
end

if not itemLink then
return
end
if GetItemCount(itemLink) == 0 then

if C_Item_GetItemCount(itemLink) == 0 then
return
end

Expand Down
Loading

0 comments on commit fc5cb5e

Please sign in to comment.