Skip to content

Commit

Permalink
[Feature] Add option to anchor Shield Bar to Health Bar (#70)
Browse files Browse the repository at this point in the history
* update shieldBar widget table

* new ShieldBar options

* impl health bar anchoring

* annotations
  • Loading branch information
Krealle authored Sep 14, 2024
1 parent 99e6857 commit 10b0ca9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 22 deletions.
1 change: 1 addition & 0 deletions Data/Constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const.OPTION_KIND = {
SPACING = "spacing",
VERTICAL_FILL = "verticalFill",
SAME_SIZE_AS_HEALTH_BAR = "sameSizeAsHealthBar",
REVERSE_FILL = "reverseFill",
}

---@enum AURA_OPTION_KIND
Expand Down
9 changes: 2 additions & 7 deletions Data/Defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,8 @@ Defaults.Widgets = {
shieldBar = {
enabled = false,
frameLevel = 10,
rgba = { 1, 1, 0, 0.25 },
position = {
point = "BOTTOMRIGHT",
offsetY = 0,
offsetX = 0,
relativePoint = "BOTTOMRIGHT",
},
point = "RIGHT",
reverseFill = false,
},
---@type CastBarWidgetTable
castBar = {
Expand Down
1 change: 1 addition & 0 deletions Locales/enUS.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ L.readyCheckIcon = "Ready Check Icon"
L.restingIcon = "Resting Icon"
L.castBar = "Cast Bar"
L.classBar = "Class Bar"
L.healthBar = "Health Bar"

-- Misc
L.Frame = "Frame"
Expand Down
37 changes: 37 additions & 0 deletions Menu/Builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Builder.MenuOptions = {
CastBarBorder = 27,
CastBarIcon = 28,
NameFormat = 29,
ShieldBarOptions = 30,
}

-------------------------------------------------
Expand Down Expand Up @@ -1551,6 +1552,41 @@ function Builder:CreateClassBarOptions(parent, widgetName)
return f
end

-------------------------------------------------
-- MARK: Shield Bar
-------------------------------------------------

---@param parent Frame
---@param widgetName WIDGET_KIND
---@return ShieldBarOptions
function Builder:CreateShieldBarOptions(parent, widgetName)
---@class ShieldBarOptions: OptionsFrame
local f = CUF:CreateFrame(nil, parent, 1, 1, true, true)
f.id = "ShieldBarOptions"
f.optionHeight = 25

-- First Row
local anchorItems = {
"RIGHT",
"LEFT",
"healthBar"
}
f.anchorOptions = self:CreateDropdown(f, widgetName, L["Anchor Point"], 117, anchorItems,
const.OPTION_KIND.ANCHOR_POINT)
f.anchorOptions:SetPoint("TOPLEFT", 0, -5)

f.reverseFill = self:CreateCheckBox(f, widgetName, L["Reverse Fill"],
const.OPTION_KIND.REVERSE_FILL)
self:AnchorRight(f.reverseFill, f.anchorOptions)

-- Dirty hook, should be made generic really
hooksecurefunc(f.anchorOptions.text, "SetText", function(_, text)
f.reverseFill:SetEnabled(text == L.healthBar)
end)

return f
end

-------------------------------------------------
-- MARK: MenuBuilder.MenuFuncs
-- Down here because of annotations
Expand Down Expand Up @@ -1586,4 +1622,5 @@ Builder.MenuFuncs = {
[Builder.MenuOptions.CastBarBorder] = Builder.CreatCastBarBorderOptions,
[Builder.MenuOptions.CastBarIcon] = Builder.CreateCastBarIconOptions,
[Builder.MenuOptions.ClassBarOptions] = Builder.CreateClassBarOptions,
[Builder.MenuOptions.ShieldBarOptions] = Builder.CreateShieldBarOptions,
}
4 changes: 2 additions & 2 deletions WidgetAnnotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@
---@class ShieldBarWidgetTable
---@field enabled boolean
---@field frameLevel number
---@field position PositionOpt
---@field rgba RGBAOpt
---@field point "RIGHT"|"LEFT"|"healthBar"
---@field reverseFill boolean

---@class CastBarWidgetTable
---@field enabled boolean
Expand Down
74 changes: 61 additions & 13 deletions Widgets/Bars/ShieldBar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
-------------------------------------------------

menu:AddWidget(const.WIDGET_KIND.SHIELD_BAR,
Builder.MenuOptions.FullAnchor,
Builder.MenuOptions.ShieldBarOptions,
Builder.MenuOptions.FrameLevel)

---@param button CUFUnitButton
Expand All @@ -31,11 +31,18 @@ menu:AddWidget(const.WIDGET_KIND.SHIELD_BAR,
---@param subSetting string
function W.UpdateShieldBarWidget(button, unit, setting, subSetting, ...)
local widget = button.widgets.shieldBar
--local styleTable = DB.GetCurrentWidgetTable(const.WIDGET_KIND.SHIELD_BAR, unit) --[[@as ShieldBarWidgetTable]]
local styleTable = DB.GetCurrentWidgetTable(const.WIDGET_KIND.SHIELD_BAR, unit) --[[@as ShieldBarWidgetTable]]

if not setting or setting == const.OPTION_KIND.COLOR then
widget:UpdateStyle()
end
if not setting or setting == const.OPTION_KIND.ANCHOR_POINT then
widget:UpdatePosition(styleTable)
end
if not setting or setting == const.OPTION_KIND.REVERSE_FILL then
widget.reverseFill = styleTable.reverseFill
widget:Repoint()
end

if widget.enabled and button:IsVisible() then
widget.Update(button)
Expand Down Expand Up @@ -100,16 +107,53 @@ end
---@param bar ShieldBarWidget
---@param percent number
local function ShieldBar_SetValue(bar, percent)
percent = math.min(percent, 1)

local maxWidth = bar.parentHealthBar:GetWidth()
local barWidth
if percent >= 1 then
barWidth = maxWidth
else
barWidth = maxWidth * percent
local barWidth = maxWidth * percent

if bar.currentPoint == "healthBar" then
local maxLossWidth = bar.parentHealthBarLoss:GetWidth()
local ratio = maxLossWidth / maxWidth

if percent > ratio then
if bar.reverseFill then
bar:Repoint("RIGHT")
else
barWidth = maxLossWidth
end
elseif bar.reverseFill then
bar:Repoint()
end
end

bar:SetWidth(barWidth)
end

---@param bar ShieldBarWidget
---@param anchorPoint string?
local function Repoint(bar, anchorPoint)
local point = anchorPoint or bar.currentPoint
if bar.currentAnchorPoint == point then return end
bar.currentAnchorPoint = point

bar:ClearAllPoints()

if point == "RIGHT" or point == "LEFT" then
bar:SetPoint("TOP", bar.parentHealthBar, "TOP", 0, 0)
bar:SetPoint("BOTTOM", bar.parentHealthBar, "BOTTOM", 0, 0)
bar:SetPoint(point, bar.parentHealthBar, point, 0, 0)
--[[ elseif point == "TOP" or point == "BOTTOM" then
bar:SetPoint("LEFT", bar.parentHealthBar, "LEFT", 0, 0)
bar:SetPoint("RIGHT", bar.parentHealthBar, "RIGHT", 0, 0)
bar:SetPoint(point, bar.parentHealthBar, point, 0, 0) ]]
else
bar:SetPoint("TOP", bar.parentHealthBarLoss, "TOP", 0, 0)
bar:SetPoint("BOTTOM", bar.parentHealthBarLoss, "BOTTOM", 0, 0)
bar:SetPoint("LEFT", bar.parentHealthBarLoss, "LEFT", 0, 0)
end
end

-------------------------------------------------
-- MARK: CreateShieldBar
-------------------------------------------------
Expand All @@ -124,8 +168,13 @@ function W:CreateShieldBar(button)
shieldBar.enabled = false
shieldBar._isSelected = false
shieldBar.parentHealthBar = button.widgets.healthBar
shieldBar.parentHealthBarLoss = button.widgets.healthBarLoss
shieldBar._owner = button

shieldBar.reverseFill = false
shieldBar.currentPoint = "RIGHT"
shieldBar.currentAnchorPoint = ""

shieldBar:Hide()
shieldBar:SetBackdrop({ edgeFile = Cell.vars.whiteTexture, edgeSize = 0.1 })
shieldBar:SetBackdropBorderColor(0, 0, 0, 1)
Expand All @@ -140,12 +189,10 @@ function W:CreateShieldBar(button)
end

---@param styleTable ShieldBarWidgetTable
function shieldBar:SetPosition(styleTable)
local pos = styleTable.position
self:ClearAllPoints()
self:SetPoint("TOP", self.parentHealthBar, "TOP", 0, 0)
self:SetPoint("BOTTOM", self.parentHealthBar, "BOTTOM", 0, 0)
self:SetPoint(pos.point, self.parentHealthBar, pos.relativePoint, pos.offsetX, pos.offsetY)
function shieldBar:UpdatePosition(styleTable)
local point = styleTable.point
self.currentPoint = point
self:Repoint()
end

---@param bar ShieldBarWidget
Expand All @@ -158,6 +205,7 @@ function W:CreateShieldBar(button)
shieldBar.SetValue = ShieldBar_SetValue
shieldBar.SetEnabled = W.SetEnabled
shieldBar.SetWidgetFrameLevel = W.SetWidgetFrameLevel
shieldBar.Repoint = Repoint

shieldBar.Update = Update
shieldBar.Enable = Enable
Expand Down

0 comments on commit 10b0ca9

Please sign in to comment.