Repository navigation
Expand file tree
/
Copy pathOptions.lua
More file actions
257 lines (216 loc) · 8.4 KB
/
Copy pathOptions.lua
File metadata and controls
257 lines (216 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
local ADDON_NAME = ...
PetXPBarPlus = PetXPBarPlus or {}
local Addon = PetXPBarPlus
local Compat = Addon.Compat
local PURPLE = { r = 0.58, g = 0.24, b = 0.86 }
local BLUE = { r = 25 / 255, g = 125 / 255, b = 255 / 255 }
local DEFAULTS = {
showXPBar = true,
showPetLevel = true,
alwaysShowXPBar = false,
alwaysShowPetLevel = false,
locked = false,
}
Addon.DEFAULTS = DEFAULTS
local function CopyColor(color)
return { r = color.r, g = color.g, b = color.b }
end
local function InitializeDB()
PetXPBarPlusDB = type(PetXPBarPlusDB) == "table" and PetXPBarPlusDB or {}
Addon.db = PetXPBarPlusDB
for key, value in pairs(DEFAULTS) do
if Addon.db[key] == nil then
Addon.db[key] = value
end
end
if type(Addon.db.xpColor) ~= "table" then
Addon.db.xpColor = CopyColor(Compat.isForever and PURPLE or BLUE)
end
end
local controls = {}
local colorRadios = {}
local customLabel
local function ColorToHex(color)
local r = math.floor((color.r or 0) * 255 + 0.5)
local g = math.floor((color.g or 0) * 255 + 0.5)
local b = math.floor((color.b or 0) * 255 + 0.5)
return string.format("#%02X%02X%02X", r, g, b)
end
local function SameColor(a, b)
return a and b and math.abs(a.r - b.r) < 0.001 and math.abs(a.g - b.g) < 0.001 and math.abs(a.b - b.b) < 0.001
end
local function Apply()
if Addon.ApplyDisplayOptions then Addon.ApplyDisplayOptions() end
if Addon.ApplyXPBarColor then Addon.ApplyXPBarColor() end
if Addon.SetLocked then Addon.SetLocked(Addon.db.locked) end
end
local function RefreshControls()
for key, check in pairs(controls) do
check:SetChecked(Addon.db[key])
end
local mode = Addon.db.xpColorMode
if not mode then
if SameColor(Addon.db.xpColor, PURPLE) then mode = "purple"
elseif SameColor(Addon.db.xpColor, BLUE) then mode = "blue"
else mode = "custom" end
end
for key, radio in pairs(colorRadios) do
radio:SetChecked(key == mode)
end
if customLabel then
customLabel:SetText(mode == "custom" and ("Custom (" .. ColorToHex(Addon.db.xpColor) .. ")") or "Custom (Pick)")
end
end
local function SetColor(color, mode)
Addon.db.xpColor = CopyColor(color)
if mode then Addon.db.xpColorMode = mode end
RefreshControls()
Apply()
end
local function ResetDefaults()
for key, value in pairs(DEFAULTS) do
Addon.db[key] = value
end
Addon.db.xpColor = CopyColor(Compat.isForever and PURPLE or BLUE)
Addon.db.xpColorMode = Compat.isForever and "purple" or "blue"
if Addon.ResetPosition then Addon.ResetPosition() end
RefreshControls()
Apply()
end
Addon.ResetDefaults = ResetDefaults
local panel = CreateFrame("Frame", "PetXPBarPlusOptionsPanel")
panel.name = "PetXPBarPlus"
local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
title:SetPoint("TOPLEFT", 16, -16)
title:SetText("PetXPBarPlus")
local subtitle = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
subtitle:SetText("Pet experience display options.")
local function MakeCheckbox(label, key, y)
local check = CreateFrame("CheckButton", nil, panel, "UICheckButtonTemplate")
check:SetPoint("TOPLEFT", 16, y)
local textRegion = check.Text or check.text
if textRegion then textRegion:SetText(label) end
controls[key] = check
check:SetScript("OnShow", function(self) self:SetChecked(Addon.db[key]) end)
check:SetScript("OnClick", function(self)
Addon.db[key] = self:GetChecked() and true or false
Apply()
end)
return check
end
MakeCheckbox("Show XP Bar", "showXPBar", -58)
MakeCheckbox("Show Pet Level", "showPetLevel", -88)
local behaviorTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
behaviorTitle:SetPoint("TOPLEFT", 16, -126)
behaviorTitle:SetText("Visibility Behavior")
MakeCheckbox("Always show XP Bar", "alwaysShowXPBar", -150)
MakeCheckbox("Always show Pet Level", "alwaysShowPetLevel", -180)
local behaviorNote = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
behaviorNote:SetPoint("TOPLEFT", 42, -208)
behaviorNote:SetWidth(430)
behaviorNote:SetJustifyH("LEFT")
behaviorNote:SetText("By default, the XP bar hides when your pet cannot gain XP, and the pet level hides at the level cap.")
local appearanceTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
appearanceTitle:SetPoint("TOPLEFT", 16, -244)
appearanceTitle:SetText("XP Bar Color")
local function MakeButton(label, x, y, width, onClick)
local button = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
button:SetSize(width, 22)
button:SetPoint("TOPLEFT", x, y)
button:SetText(label)
button:SetScript("OnClick", onClick)
return button
end
local function MakeColorRadio(key, label, color, y)
local radio = CreateFrame("CheckButton", nil, panel, "UIRadioButtonTemplate")
radio:SetPoint("TOPLEFT", 16, y)
colorRadios[key] = radio
local sample = panel:CreateTexture(nil, "ARTWORK")
sample:SetSize(18, 18)
sample:SetPoint("LEFT", radio, "RIGHT", 4, 0)
sample:SetColorTexture(color.r, color.g, color.b)
local textRegion = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
textRegion:SetPoint("LEFT", sample, "RIGHT", 8, 0)
textRegion:SetText(label)
radio:SetScript("OnClick", function()
SetColor(color, key)
end)
return radio, textRegion
end
MakeColorRadio("purple", "Forever", PURPLE, -268)
MakeColorRadio("blue", "Classic", BLUE, -298)
local customRadio = CreateFrame("CheckButton", nil, panel, "UIRadioButtonTemplate")
customRadio:SetPoint("TOPLEFT", 16, -328)
colorRadios.custom = customRadio
local customSample = panel:CreateTexture(nil, "ARTWORK")
customSample:SetSize(18, 18)
customSample:SetPoint("LEFT", customRadio, "RIGHT", 4, 0)
customSample:SetColorTexture(1, 1, 1)
customLabel = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
customLabel:SetPoint("LEFT", customSample, "RIGHT", 8, 0)
customLabel:SetText("Custom (Pick)")
local function OpenColorPicker()
local old = CopyColor(Addon.db.xpColor)
local oldMode = Addon.db.xpColorMode
Addon.db.xpColorMode = "custom"
RefreshControls()
local function changed()
local r, g, b = ColorPickerFrame:GetColorRGB()
customSample:SetColorTexture(r, g, b)
SetColor({ r = r, g = g, b = b }, "custom")
end
local function cancelled(previous)
local restore = (type(previous) == "table" and previous.r) and previous or old
customSample:SetColorTexture(restore.r, restore.g, restore.b)
SetColor(restore, oldMode)
end
if ColorPickerFrame.SetupColorPickerAndShow then
ColorPickerFrame:SetupColorPickerAndShow({
r = old.r, g = old.g, b = old.b,
swatchFunc = changed,
cancelFunc = cancelled,
hasOpacity = false,
})
else
ColorPickerFrame.hasOpacity = false
ColorPickerFrame.previousValues = old
ColorPickerFrame.func = changed
ColorPickerFrame.cancelFunc = cancelled
ColorPickerFrame:SetColorRGB(old.r, old.g, old.b)
ColorPickerFrame:Show()
end
end
customRadio:SetScript("OnClick", OpenColorPicker)
customLabel:SetScript("OnMouseDown", OpenColorPicker)
customLabel:EnableMouse(true)
local positionTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
positionTitle:SetPoint("TOPLEFT", 16, -370)
positionTitle:SetText("Position")
MakeCheckbox("Lock Position", "locked", -392)
MakeButton("Reset Position", 42, -424, 112, function()
if Addon.ResetPosition then Addon.ResetPosition() end
end)
local defaultsButton = MakeButton("Defaults", 16, -470, 96, ResetDefaults)
panel:SetScript("OnShow", function()
RefreshControls()
end)
local category = Compat.RegisterOptionsPanel(panel)
function Addon.OpenOptions()
Compat.OpenOptionsPanel(panel, category)
end
-- SavedVariables are guaranteed to be available for this addon when
-- ADDON_LOADED fires. Initializing earlier can replace the table before the
-- client restores PetXPBarPlusDB, which makes settings appear to work for the
-- current session but revert after logout/reload on affected clients.
local loader = CreateFrame("Frame")
loader:RegisterEvent("ADDON_LOADED")
loader:SetScript("OnEvent", function(self, _, loadedAddon)
if loadedAddon ~= ADDON_NAME then
return
end
self:UnregisterEvent("ADDON_LOADED")
InitializeDB()
RefreshControls()
Apply()
end)