Repository navigation
Expand file tree
/
Copy pathLabels.lua
More file actions
315 lines (248 loc) · 9.15 KB
/
Copy pathLabels.lua
File metadata and controls
315 lines (248 loc) · 9.15 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
-- UILabels - Label Management
-- Creates, updates, and manages label frames
UILabels = UILabels or {}
UILabels.Labels = {}
-- Storage for created label frames
UILabels.Labels.frames = {}
UILabels.Labels.editMode = false
-- Create a single label frame
function UILabels.Labels:CreateLabel(labelID, config)
-- Destroy existing if present
if self.frames[labelID] then
self:DestroyLabel(labelID)
end
-- Create parent frame
local parent = CreateFrame("Frame", "UILabels_Label_" .. labelID, UIParent)
parent:SetFrameStrata(UILabelsDB.globalSettings.frameStrata or "HIGH")
parent:SetFrameLevel(100)
parent:SetWidth(1)
parent:SetHeight(1)
parent:SetPoint(config.anchor or "BOTTOM", UIParent, config.relativePoint or "BOTTOM", config.x, config.y)
-- Create font string
local fontString = parent:CreateFontString(nil, "OVERLAY")
fontString:SetPoint(config.anchor or "BOTTOM", UIParent, config.relativePoint or "BOTTOM", config.x, config.y)
-- Set font
local fontPath = config.fontPath or UILabelsDB.globalSettings.defaultFont or UILabels.Utils:GetBestFontPath()
local fontSize = config.fontSize or UILabelsDB.globalSettings.defaultFontSize or 12
local outline = config.outline or UILabelsDB.globalSettings.defaultOutline or "OUTLINE"
local success = fontString:SetFont(fontPath, fontSize, outline)
if not success then
-- Fallback to default font
fontString:SetFont(UILabels.Defaults.FALLBACK_FONT, fontSize, outline)
UILabels.Utils:Debug("Font failed for label " .. labelID .. ", using fallback")
end
-- Set text and color
fontString:SetText(config.text or "")
local color = config.color or UILabelsDB.globalSettings.defaultColor or {r=1, g=1, b=0}
fontString:SetTextColor(color.r, color.g, color.b)
-- Set visibility
if config.visible == false then
parent:Hide()
else
parent:Show()
end
-- Store references
parent.fontString = fontString
parent.config = config
parent.labelID = labelID
-- Make draggable if in edit mode
if self.editMode then
self:MakeDraggable(parent)
end
self.frames[labelID] = parent
return parent
end
-- Create all labels from database
function UILabels.Labels:CreateAllLabels()
local labels = UILabels.Database:GetAllLabels()
for labelID, config in pairs(labels) do
self:CreateLabel(labelID, config)
end
UILabels.Utils:Debug("Created " .. self:GetLabelCount() .. " labels")
end
-- Update an existing label
function UILabels.Labels:UpdateLabel(labelID, config)
local frame = self.frames[labelID]
if not frame then
-- Label doesn't exist, create it
self:CreateLabel(labelID, config)
return
end
local fontString = frame.fontString
-- Update position
fontString:ClearAllPoints()
fontString:SetPoint(config.anchor or "BOTTOM", UIParent, config.relativePoint or "BOTTOM", config.x, config.y)
-- Update font
local fontPath = config.fontPath or UILabelsDB.globalSettings.defaultFont or UILabels.Utils:GetBestFontPath()
local fontSize = config.fontSize or 12
local outline = config.outline or "OUTLINE"
fontString:SetFont(fontPath, fontSize, outline)
-- Update text and color
fontString:SetText(config.text or "")
local color = config.color or {r=1, g=1, b=0}
fontString:SetTextColor(color.r, color.g, color.b)
-- Update visibility
if config.visible == false then
frame:Hide()
else
frame:Show()
end
-- Update stored config
frame.config = config
end
-- Destroy a label
function UILabels.Labels:DestroyLabel(labelID)
local frame = self.frames[labelID]
if frame then
frame:Hide()
frame.fontString = nil
frame:SetScript("OnMouseDown", nil)
frame:SetScript("OnMouseUp", nil)
frame:SetScript("OnDragStart", nil)
frame:SetScript("OnDragStop", nil)
frame = nil
self.frames[labelID] = nil
end
end
-- Refresh all labels (destroy and recreate)
function UILabels.Labels:RefreshAllLabels()
-- Destroy all
for labelID, _ in pairs(self.frames) do
self:DestroyLabel(labelID)
end
-- Recreate all
self:CreateAllLabels()
UILabels.Utils:Debug("Labels refreshed")
end
-- Show all labels
function UILabels.Labels:ShowAllLabels()
for _, frame in pairs(self.frames) do
frame:Show()
end
end
-- Hide all labels
function UILabels.Labels:HideAllLabels()
for _, frame in pairs(self.frames) do
frame:Hide()
end
end
-- Get label count
function UILabels.Labels:GetLabelCount()
local count = 0
for _ in pairs(self.frames) do
count = count + 1
end
return count
end
-- Toggle edit mode
function UILabels.Labels:ToggleEditMode()
self.editMode = not self.editMode
if self.editMode then
UILabels.Utils:Print("Edit mode |cff00ff00ENABLED|r - Drag labels to reposition")
-- Make all labels draggable
for _, frame in pairs(self.frames) do
self:MakeDraggable(frame)
end
else
UILabels.Utils:Print("Edit mode |cffff0000DISABLED|r - Positions saved")
-- Remove draggable functionality
for _, frame in pairs(self.frames) do
self:MakeNonDraggable(frame)
end
end
end
-- Enable edit mode
function UILabels.Labels:EnableEditMode()
if not self.editMode then
self:ToggleEditMode()
end
end
-- Disable edit mode
function UILabels.Labels:DisableEditMode()
if self.editMode then
self:ToggleEditMode()
end
end
-- Make a label draggable
function UILabels.Labels:MakeDraggable(frame)
local fontString = frame.fontString
local config = frame.config
-- Get text width
local textWidth = fontString:GetStringWidth()
-- Estimate height based on font size
local fontSize = config.fontSize or 12
local textHeight = fontSize + 4
-- Set frame size
local frameWidth = textWidth + 8
local frameHeight = textHeight + 8
-- Calculate offset needed to keep text in same visual position
-- The frame anchor is at the bottom, but we want the fontstring centered
-- So we need to offset by half the frame height
local anchor = config.anchor or "BOTTOM"
local relativePoint = config.relativePoint or config.anchor or "BOTTOM"
-- Calculate Y offset based on anchor point
local yOffset = 0
if anchor == "BOTTOM" or anchor == "BOTTOMLEFT" or anchor == "BOTTOMRIGHT" then
yOffset = frameHeight / 2
elseif anchor == "TOP" or anchor == "TOPLEFT" or anchor == "TOPRIGHT" then
yOffset = -frameHeight / 2
end
frame:SetWidth(frameWidth)
frame:SetHeight(frameHeight)
-- Position frame with offset to keep text in same spot
frame:ClearAllPoints()
frame:SetPoint(anchor, UIParent, relativePoint, config.x, config.y - yOffset)
frame:EnableMouse(true)
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
-- Create visual indicator background
if not frame.editBorder then
frame.editBorder = frame:CreateTexture(nil, "BACKGROUND")
frame.editBorder:SetTexture(0, 1, 0, 0.3)
frame.editBorder:SetAllPoints(frame)
end
frame.editBorder:Show()
-- Center the fontstring in the frame
fontString:ClearAllPoints()
fontString:SetPoint("CENTER", frame, "CENTER", 0, 0)
-- Store the offset for later use
frame.editModeYOffset = yOffset
frame:SetScript("OnDragStart", function()
this:StartMoving()
end)
frame:SetScript("OnDragStop", function()
this:StopMovingOrSizing()
-- Get position of the frame
local _, _, _, x, y = this:GetPoint(1)
-- Add back the offset to get the actual fontstring position
local actualY = y + (this.editModeYOffset or 0)
-- Store the position
this.config.x = math.floor(x + 0.5)
this.config.y = math.floor(actualY + 0.5)
-- Save to database
UILabels.Database:SetLabel(this.labelID, this.config)
UILabels.Utils:Print("Label #" .. this.labelID .. " moved to " .. this.config.x .. ", " .. this.config.y)
end)
end
-- Make a label non-draggable
function UILabels.Labels:MakeNonDraggable(frame)
frame:EnableMouse(false)
frame:SetMovable(false)
frame:RegisterForDrag()
if frame.editBorder then
frame.editBorder:Hide()
end
-- Get config (already saved by OnDragStop)
local config = frame.config
-- Reset frame to minimal size
frame:SetWidth(1)
frame:SetHeight(1)
-- Reposition frame using saved coordinates
frame:ClearAllPoints()
frame:SetPoint(config.anchor or "BOTTOM", UIParent, config.relativePoint or "BOTTOM", config.x, config.y)
-- Reposition fontstring to its proper anchor
frame.fontString:ClearAllPoints()
frame.fontString:SetPoint(config.anchor or "BOTTOM", UIParent, config.relativePoint or "BOTTOM", config.x, config.y)
frame:SetScript("OnDragStart", nil)
frame:SetScript("OnDragStop", nil)
end