Skip to content

Commit

Permalink
[Feature] Custom Texts widget (#89)
Browse files Browse the repository at this point in the history
* const

* move ReadyCheckIcon annotation down to icon section

* annotations

* initial defaults

* add menu options

* make CreateHealthText able to be called and get the widget returned instead of attached to button

* ProcessCustomTextFormat now checks for health format

* implement CustomText

* Resolve shallow copy issues

* add custom text to target/focus/pet
  • Loading branch information
Krealle authored Sep 27, 2024
1 parent ad79bb8 commit 1fa65f9
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cell_UnitFrames.toc
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Core/Init.lua
Core/Events.lua

Data/Constants.lua
Util/Utils.lua
Data/Defaults.lua
Data/Database.lua
Data/DBUtil.lua
Data/Revise.lua

Util/Utils.lua
Util/Handler.lua
Util/HideBlizzard.lua
Util/HelpTips.lua
Expand All @@ -50,6 +50,7 @@ Widgets/Texts/NameText.lua
Widgets/Texts/HealthText.lua
Widgets/Texts/PowerText.lua
Widgets/Texts/LevelText.lua
Widgets/Texts/CustomText.lua

Widgets/Bars/HealthBar.lua
Widgets/Bars/PowerBar.lua
Expand Down
2 changes: 2 additions & 0 deletions Data/Constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const.WIDGET_KIND = {
HEALTH_TEXT = "healthText",
POWER_TEXT = "powerText",
LEVEL_TEXT = "levelText",
CUSTOM_TEXT = "customText",
BUFFS = "buffs",
DEBUFFS = "debuffs",
RAID_ICON = "raidIcon",
Expand Down Expand Up @@ -155,6 +156,7 @@ const.OPTION_KIND = {
REVERSE_FILL = "reverseFill",
OVER_SHIELD = "overShield",
SHOW_DEAD_STATUS = "showDeadStatus",
CUSTOM_TEXTS = "customTexts",
}

---@enum AURA_OPTION_KIND
Expand Down
36 changes: 36 additions & 0 deletions Data/Defaults.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---@class CUF
local CUF = select(2, ...)

local Util = CUF.Util

-------------------------------------------------
-- MARK: Defaults
-------------------------------------------------
Expand Down Expand Up @@ -243,6 +245,25 @@ Defaults.ColorsMenuOrder = {
}
}

---@type CustomTextWidgetTable
Defaults.CustomText = {
enabled = false,
textFormat = "",
color = {
type = "custom",
rgb = { 1, 1, 1 },
},
position = {
point = "CENTER",
offsetY = 0,
offsetX = 0,
relativePoint = "CENTER",
},
font = Defaults.Options.smallFontOpt,
hideIfFull = false,
hideIfEmpty = false,
}

-------------------------------------------------
-- MARK: Widgets (Text)
-------------------------------------------------
Expand Down Expand Up @@ -314,6 +335,18 @@ Defaults.Widgets = {
offsetX = 0,
relativePoint = "CENTER",
},
},
---@type CustomTextMainWidgetTable
customText = {
enabled = false,
frameLevel = 11,
texts = {
text1 = Util:CopyDeep(Defaults.CustomText),
text2 = Util:CopyDeep(Defaults.CustomText),
text3 = Util:CopyDeep(Defaults.CustomText),
text4 = Util:CopyDeep(Defaults.CustomText),
text5 = Util:CopyDeep(Defaults.CustomText)
}
}, -- MARK: Widgets (Auras)
---@type AuraWidgetTable
buffs = {
Expand Down Expand Up @@ -639,6 +672,7 @@ Defaults.Layouts = {
healthText = Defaults.Widgets.healthText,
powerText = Defaults.Widgets.powerText,
levelText = Defaults.Widgets.levelText,
customText = Defaults.Widgets.customText,
buffs = Defaults.Widgets.buffs,
debuffs = Defaults.Widgets.debuffs,
raidIcon = Defaults.Widgets.raidIcon,
Expand All @@ -664,6 +698,7 @@ Defaults.Layouts = {
healthText = Defaults.Widgets.healthText,
powerText = Defaults.Widgets.powerText,
levelText = Defaults.Widgets.levelText,
customText = Defaults.Widgets.customText,
buffs = Defaults.Widgets.buffs,
debuffs = Defaults.Widgets.debuffs,
raidIcon = Defaults.Widgets.raidIcon,
Expand Down Expand Up @@ -720,6 +755,7 @@ Defaults.Layouts = {
healthText = Defaults.Widgets.healthText,
powerText = Defaults.Widgets.powerText,
levelText = Defaults.Widgets.levelText,
customText = Defaults.Widgets.customText,
buffs = Defaults.Widgets.buffs,
debuffs = Defaults.Widgets.debuffs,
raidIcon = Defaults.Widgets.raidIcon,
Expand Down
1 change: 1 addition & 0 deletions Locales/enUS.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ L.Widgets = "Widgets"
L.nameText = "Name Text"
L.healthText = "Health Text"
L.powerText = "Power Text"
L.customText = "Custom Text"
L.buffs = "Buffs"
L.debuffs = "Debuffs"
L.raidIcon = "Raid Icon"
Expand Down
176 changes: 176 additions & 0 deletions Menu/Builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Builder.MenuOptions = {
CastBarIcon = 28,
NameFormat = 29,
ShieldBarOptions = 30,
CustomText = 31,
}

-------------------------------------------------
Expand Down Expand Up @@ -1609,6 +1610,180 @@ function Builder:CreateShieldBarOptions(parent, widgetName)
return f
end

-------------------------------------------------
-- MARK: Custom Text
-------------------------------------------------

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

local function Set_DB(_, path, value)
HandleWidgetOption(widgetName, "texts.text" .. f.selectedIndex .. "." .. path, value)
end
local function Get_DB(_, path)
return HandleWidgetOption(widgetName, "texts.text" .. f.selectedIndex .. "." .. path)
end

---@type CellDropdown
local textDropdown = Cell:CreateDropdown(parent, 200)
textDropdown:SetLabel(L.customText)
textDropdown:SetPoint("TOPLEFT", f)

local enableCB = self:CreateCheckBox(parent, widgetName, L["Enabled"], const.OPTION_KIND.ENABLED)
self:AnchorRight(enableCB, textDropdown)
enableCB.Set_DB = Set_DB
enableCB.Get_DB = Get_DB

-- Format
local formatEditBox = self:CreateEditBox(parent, widgetName, L["Text Format"], 300, const.OPTION_KIND.TEXT_FORMAT)
self:AnchorBelow(formatEditBox, textDropdown)
formatEditBox.Set_DB = Set_DB
formatEditBox.Get_DB = Get_DB
CUF:SetTooltips(formatEditBox, "ANCHOR_TOPLEFT", 0, 3, L.ValidTags,
unpack(CUF.widgets.CustomHealtFormatsTooltip))

-- Hide if
local hideIfEmpty = self:CreateCheckBox(f, widgetName, L.HideIfEmpty,
const.OPTION_KIND.HIDE_IF_EMPTY)
self:AnchorBelowCB(hideIfEmpty, formatEditBox)
local hideIfFull = self:CreateCheckBox(f, widgetName, L.HideIfFull,
const.OPTION_KIND.HIDE_IF_FULL)
self:AnchorRightOfCB(hideIfFull, hideIfEmpty)

hideIfEmpty.Set_DB = Set_DB
hideIfEmpty.Get_DB = Get_DB
hideIfFull.Set_DB = Set_DB
hideIfFull.Get_DB = Get_DB

-- Color
local items = {
{ L["Class Color"], const.ColorType.CLASS_COLOR },
{ L["Custom Color"], const.ColorType.CUSTOM },
}

---@type CellDropdown
local colorDropdown = Cell:CreateDropdown(parent, 200)
colorDropdown:SetLabel(L["Color"])
self:AnchorBelow(colorDropdown, hideIfEmpty)

local colorPicker = Cell:CreateColorPicker(f, "", false, function(r, g, b, a)
Set_DB(widgetName, "color.rgb", { r, g, b })
end)
colorPicker:SetPoint("LEFT", colorDropdown, "RIGHT", 2, 0)

for _, item in pairs(items) do
colorDropdown:AddItem({
["text"] = item[1],
["value"] = item[2],
["onClick"] = function()
if item[2] == const.ColorType.CUSTOM then
colorPicker:Show()
else
colorPicker:Hide()
end

Set_DB(widgetName, "color.type", item[2])
end,
})
end

-- Anchor
local anchorOptions = self:CreateAnchorOptions(parent, widgetName)
self:AnchorBelow(anchorOptions, colorDropdown)

anchorOptions.anchorDropdown.Set_DB = Set_DB
anchorOptions.anchorDropdown.Get_DB = Get_DB
anchorOptions.sliderX.Set_DB = Set_DB
anchorOptions.sliderX.Get_DB = Get_DB
anchorOptions.sliderY.Set_DB = Set_DB
anchorOptions.sliderY.Get_DB = Get_DB

-- Font
local fontItems = Util:GetFontItems()

local styleDropdown = self:CreateDropdown(parent, widgetName, "Font", nil, fontItems, "font.style")
self:AnchorBelow(styleDropdown, anchorOptions)

local outlineDropdown = self:CreateDropdown(parent, widgetName, "Outline", nil, const.OUTLINES, "font.outline")
self:AnchorRight(outlineDropdown, styleDropdown)

local sizeSlider = self:CreateSlider(f, widgetName, L["Size"], nil, 5, 50, "font.size")
self:AnchorRight(sizeSlider, outlineDropdown)

local shadowCB = Cell:CreateCheckButton(parent, L["Shadow"], function(checked, cb)
Set_DB(widgetName, "font.shadow", checked)
end)
shadowCB:SetPoint("TOPLEFT", styleDropdown, "BOTTOMLEFT", 0, -10)

styleDropdown.Set_DB = Set_DB
styleDropdown.Get_DB = Get_DB
outlineDropdown.Set_DB = Set_DB
outlineDropdown.Get_DB = Get_DB
sizeSlider.Set_DB = Set_DB
sizeSlider.Get_DB = Get_DB

local function LoadText()
---@type HealthTextWidgetTable
local widgetTable = HandleWidgetOption(widgetName, "texts.text" .. f.selectedIndex)

textDropdown:SetSelectedValue(f.selectedIndex)
enableCB:SetChecked(widgetTable.enabled)

-- Format
formatEditBox:SetText(widgetTable.textFormat)
hideIfEmpty:SetChecked(widgetTable.hideIfEmpty)
hideIfFull:SetChecked(widgetTable.hideIfFull)

-- Color
colorDropdown:SetSelectedValue(widgetTable.color.type)
local r, g, b, a = unpack(widgetTable.color.rgb)
colorPicker:SetColor(r, g, b, a)

if widgetTable.color.type == const.ColorType.CUSTOM then
colorPicker:Show()
else
colorPicker:Hide()
end

-- Anchor
anchorOptions.anchorDropdown:SetSelectedValue(widgetTable.position.point)
anchorOptions.sliderX:SetValue(widgetTable.position.offsetX)
anchorOptions.sliderY:SetValue(widgetTable.position.offsetY)

-- Font
styleDropdown:SetSelectedValue(widgetTable.font.style)
outlineDropdown:SetSelectedValue(widgetTable.font.outline)
sizeSlider:SetValue(widgetTable.font.size)
shadowCB:SetChecked(widgetTable.font.shadow)
end

for i = 1, 5 do
textDropdown:AddItem({
["text"] = "Text " .. i,
["value"] = i,
["onClick"] = function()
f.selectedIndex = i
LoadText()
end,
})
end

local function LoadPageDB()
f.selectedIndex = 1
LoadText()
end
Handler:RegisterOption(LoadPageDB, widgetName, "CustomTextOptions")

return f
end

-------------------------------------------------
-- MARK: MenuBuilder.MenuFuncs
-- Down here because of annotations
Expand Down Expand Up @@ -1645,4 +1820,5 @@ Builder.MenuFuncs = {
[Builder.MenuOptions.CastBarIcon] = Builder.CreateCastBarIconOptions,
[Builder.MenuOptions.ClassBarOptions] = Builder.CreateClassBarOptions,
[Builder.MenuOptions.ShieldBarOptions] = Builder.CreateShieldBarOptions,
[Builder.MenuOptions.CustomText] = Builder.CreateCustomTextOptions,
}
4 changes: 2 additions & 2 deletions Util/Utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ function Util:AddMissingProps(tableA, tableB, propsToIgnore)

for key, bVal in pairs(tableB) do
if tableA[key] == nil then
tableA[key] = bVal
tableA[key] = Util:CopyDeep(bVal)
elseif type(tableA[key]) ~= type(bVal) then
tableA[key] = bVal
tableA[key] = Util:CopyDeep(bVal)
elseif type(bVal) == "table" and not propsToIgnore[key] then
self:AddMissingProps(tableA[key], bVal, propsToIgnore)
end
Expand Down
23 changes: 20 additions & 3 deletions WidgetAnnotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
---| RestingIconWidgetTable
---| CastBarWidgetTable
---| ClassBarWidgetTable
---| CustomTextWidgetTable

---@alias Widget
---| NameTextWidget
Expand All @@ -31,6 +32,7 @@
---| RestingIconWidget
---| CastBarWidget
---| ClassBarWidget
---| CustomTextWidget

---@class CUFUnitButton.Widgets
---@field healthBar HealthBarWidget
Expand All @@ -55,6 +57,7 @@
---@field restingIcon RestingIconWidget
---@field castBar CastBarWidget
---@field classBar ClassBarWidget
---@field customText CustomTextWidget

---@class BaseWidget
---@field enabled boolean
Expand Down Expand Up @@ -106,11 +109,19 @@
---@field color ColorOpt
---@field position PositionOpt

---@class ReadyCheckIconWidgetTable
---@class CustomTextWidgetTable
---@field enabled boolean
---@field frameLevel number
---@field size SizeOpt
---@field textFormat string
---@field color ColorOpt
---@field position PositionOpt
---@field font SmallFontOpt
---@field hideIfFull boolean
---@field hideIfEmpty boolean

---@class CustomTextMainWidgetTable
---@field enabled boolean
---@field frameLevel number
---@field texts CustomTextWidgetTable[]

-------------------------------------------------
-- MARK: Icon Widgets
Expand Down Expand Up @@ -146,6 +157,12 @@
---@field frameLevel number
---@field size SizeOpt

---@class ReadyCheckIconWidgetTable
---@field enabled boolean
---@field frameLevel number
---@field size SizeOpt
---@field position PositionOpt

-------------------------------------------------
-- MARK: Aura Widgets
-------------------------------------------------
Expand Down
Loading

0 comments on commit 1fa65f9

Please sign in to comment.