-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLL.lua
More file actions
403 lines (357 loc) · 18.4 KB
/
Copy pathLL.lua
File metadata and controls
403 lines (357 loc) · 18.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
-- Main Addon Namespace
local LL = {
linesShown = false,
lines = {},
SD_H = 3, SD_V = 3, border = 0,
MAX_SD_H = 20, MAX_SD_V = 40, MAX_BORDER = 100,
aspectRatios = {{"4:3", 1.3333}, {"3:2", 1.5}, {"16:10", 1.6}, {"16:9", 1.7778}, {"21:9", 2.3333}, {"32:9", 3.5556}, {"Real", -1}},
currentAspectRatio = -1,
realAspectRatio = 1.7778,
backgroundEnabled = false,
backgroundFrame = nil,
backgroundColor = {r = 0.0, g = 0.5, b = 1.0}
}
-- Define Colors for Lines
local COLORS = {
gray = {r = 0.5, g = 0.5, b = 0.5},
yellow = {r = 1.0, g = 1.0, b = 0.0},
magenta = {r = 1.0, g = 0.0, b = 1.0},
blue = {r = 0.0, g = 0.0, b = 1.0},
}
-- Utility Functions
local function CreateTextureLine(name, parent, layer)
local texture = parent:CreateTexture("LL_Line_" .. name, layer)
texture:SetColorTexture(1, 1, 1, 1)
table.insert(LL.lines, texture)
return texture
end
local function SetTextureLine(texture, x, y, width, height, color, alpha)
texture:ClearAllPoints()
texture:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, -y)
texture:SetSize(width, height)
texture:SetColorTexture(color.r, color.g, color.b, alpha)
end
-- Background Functions
function LL:ShowBackground()
if not self.backgroundFrame then
self.backgroundFrame = CreateFrame("Frame", "LL_BackgroundFrame", UIParent)
self.backgroundFrame:SetFrameStrata("BACKGROUND")
local bgTexture = self.backgroundFrame:CreateTexture(nil, "BACKGROUND")
bgTexture:SetAllPoints(self.backgroundFrame)
bgTexture:SetColorTexture(self.backgroundColor.r, self.backgroundColor.g, self.backgroundColor.b, 0.9)
self.backgroundFrame.texture = bgTexture
end
self.backgroundFrame:SetAllPoints(UIParent)
self.backgroundFrame:Show()
end
function LL:HideBackground()
if self.backgroundFrame then self.backgroundFrame:Hide() end
end
-- Main Drawing Function
function LL:ShowLines()
local screenWidth, screenHeight = UIParent:GetSize()
local aspectRatio = self.currentAspectRatio == -1 and self.realAspectRatio or self.currentAspectRatio
local edgeBoxWidth, edgeBoxHeight, leftOffset, topOffset
if self.currentAspectRatio == -1 then
edgeBoxWidth, edgeBoxHeight = screenWidth - 2 * self.border, screenHeight - 2 * self.border
leftOffset, topOffset = self.border, self.border
else
edgeBoxHeight = screenHeight - 2 * self.border
edgeBoxWidth = math.min(edgeBoxHeight * aspectRatio, screenWidth - 2 * self.border)
leftOffset, topOffset = (screenWidth - edgeBoxWidth) / 2, self.border
end
self:HideLines()
-- Draw Center Crosshair
local centerX, centerY = screenWidth / 2, screenHeight / 2
SetTextureLine(CreateTextureLine("CenterV", UIParent, "BACKGROUND"), math.floor(centerX), 0, 1, screenHeight, COLORS.gray, 1.0)
SetTextureLine(CreateTextureLine("CenterH", UIParent, "BACKGROUND"), 0, math.floor(centerY), screenWidth, 1, COLORS.gray, 1.0)
-- Draw Division Lines
for i = 1, self.SD_H - 1 do
local hPosition = topOffset + (edgeBoxHeight / self.SD_H) * i
SetTextureLine(CreateTextureLine("DivH" .. i, UIParent, "BACKGROUND"), math.floor(leftOffset), math.floor(hPosition), edgeBoxWidth, 1, COLORS.yellow, 1.0)
end
for i = 1, self.SD_V - 1 do
local leftPos = leftOffset + (edgeBoxWidth / self.SD_V) * i
SetTextureLine(CreateTextureLine("DivV" .. i, UIParent, "BACKGROUND"), leftPos < centerX and math.floor(leftPos) or math.ceil(leftPos), topOffset, 1, edgeBoxHeight, COLORS.yellow, 1.0)
end
-- Draw Original Screen Edges
if aspectRatio < self.realAspectRatio then
SetTextureLine(CreateTextureLine("OrigEdgeV_Left", UIParent, "BACKGROUND"), math.floor(self.border), self.border, 1, screenHeight - 2 * self.border, COLORS.blue, 1.0)
SetTextureLine(CreateTextureLine("OrigEdgeV_Right", UIParent, "BACKGROUND"), math.ceil(screenWidth - self.border - 1), self.border, 1, screenHeight - 2 * self.border, COLORS.blue, 1.0)
SetTextureLine(CreateTextureLine("OrigEdgeH_Top", UIParent, "BACKGROUND"), self.border, math.floor(self.border), screenWidth - 2 * self.border, 1, COLORS.blue, 1.0)
SetTextureLine(CreateTextureLine("OrigEdgeH_Bottom", UIParent, "BACKGROUND"), self.border, math.ceil(screenHeight - self.border - 1), screenWidth - 2 * self.border, 1, COLORS.blue, 1.0)
end
-- Draw Edge Box
SetTextureLine(CreateTextureLine("EdgeH_Top", UIParent, "BACKGROUND"), math.floor(leftOffset), math.floor(topOffset), edgeBoxWidth, 1, COLORS.magenta, 1.0)
SetTextureLine(CreateTextureLine("EdgeH_Bottom", UIParent, "BACKGROUND"), math.floor(leftOffset), math.ceil(topOffset + edgeBoxHeight - 1), edgeBoxWidth, 1, COLORS.magenta, 1.0)
SetTextureLine(CreateTextureLine("EdgeV_Left", UIParent, "BACKGROUND"), math.floor(leftOffset), topOffset, 1, edgeBoxHeight, COLORS.magenta, 1.0)
SetTextureLine(CreateTextureLine("EdgeV_Right", UIParent, "BACKGROUND"), math.ceil(leftOffset + edgeBoxWidth - 1), topOffset, 1, edgeBoxHeight, COLORS.magenta, 1.0)
self.linesShown = true
if self.backgroundEnabled then self:ShowBackground() end
end
function LL:HideLines()
if not self.linesShown then return end
for _, line in ipairs(self.lines) do line:Hide() end
self.lines, self.linesShown = {}, false
self:HideBackground()
end
-- Parameter Setting Function
function LL:SetParameters(inputs)
local new_SD_H, new_SD_V, new_border, new_backgroundEnabled = self.SD_H, self.SD_V, self.border, self.backgroundEnabled
local changed_SD_H, changed_SD_V, changed_border, changed_background = false, false, false, false
local error_messages = {}
for _, input in ipairs(inputs) do
local original_input = input
input = string.upper(input)
if input == "BG" then
new_backgroundEnabled, changed_background = not self.backgroundEnabled, true
else
local param_type, param_value = string.match(input, "^([A-Z]+)(%d*)$")
if param_type then
if param_type == "H" or param_type == "V" or param_type == "B" then
if param_value ~= "" then
local value = tonumber(param_value) or (function() local num, den = string.match(param_value, "^(%d+)/(%d+)$"); return num == "1" and den and tonumber(den) or nil end)()
if param_type == "B" then
if value and value >= 0 and value <= self.MAX_BORDER and math.floor(value) == value then
new_border, changed_border = value, true
else
table.insert(error_messages, string.format("Invalid border value: %s. Must be an integer between 0 and %d.", param_value, self.MAX_BORDER))
end
else
local max_sd = param_type == "H" and self.MAX_SD_H or self.MAX_SD_V
if value and value >= 2 and value <= max_sd and math.floor(value) == value then
if param_type == "H" then new_SD_H, changed_SD_H = value, true
elseif param_type == "V" then new_SD_V, changed_SD_V = value, true end
else
table.insert(error_messages, string.format("Invalid divisor for axis %s: %s. Must be an integer between 2 and %d.", param_type, param_value, max_sd))
end
end
else
table.insert(error_messages, string.format("Parameter %s requires a value. Example: %s3", param_type, param_type))
end
else
table.insert(error_messages, string.format("Unknown parameter prefix: %s in parameter: %s", param_type, original_input))
end
else
table.insert(error_messages, string.format("Invalid parameter format: %s.", original_input))
end
end
end
-- Apply changes and provide feedback
if changed_SD_H or changed_SD_V or changed_border or changed_background then
self.SD_H, self.SD_V, self.border, self.backgroundEnabled = new_SD_H, new_SD_V, new_border, new_backgroundEnabled
self:SaveConfig()
if self.linesShown then self:HideLines(); self:ShowLines() end
if changed_SD_H then print("LL: Horizontal divisions set to " .. self.SD_H) end
if changed_SD_V then print("LL: Vertical divisions set to " .. self.SD_V) end
if changed_border then print("LL: Border set to " .. self.border) end
if changed_background then print("LL: Background " .. (self.backgroundEnabled and "enabled" or "disabled")) end
end
for _, msg in ipairs(error_messages) do print("LL: " .. msg) end
if not (changed_SD_H or changed_SD_V or changed_border or changed_background) and #error_messages == 0 and #inputs > 0 then
print(string.format("LL: Invalid parameters. Use '/ll Hx Vy Bz BG', where x is an integer between 2 and %d, y is an integer between 2 and %d, z is an integer between 0 and %d, and BG toggles the background.", self.MAX_SD_H, self.MAX_SD_V, self.MAX_BORDER))
end
end
-- Configuration Functions
function LL:LoadConfig()
if LL_Config then
self.SD_H = tonumber(LL_Config.SD_H) and tonumber(LL_Config.SD_H) >= 2 and tonumber(LL_Config.SD_H) <= self.MAX_SD_H and math.floor(tonumber(LL_Config.SD_H)) or self.SD_H
self.SD_V = tonumber(LL_Config.SD_V) and tonumber(LL_Config.SD_V) >= 2 and tonumber(LL_Config.SD_V) <= self.MAX_SD_V and math.floor(tonumber(LL_Config.SD_V)) or self.SD_V
self.border = tonumber(LL_Config.border) and tonumber(LL_Config.border) >= 0 and tonumber(LL_Config.border) <= self.MAX_BORDER and math.floor(tonumber(LL_Config.border)) or self.border
self.currentAspectRatio = tonumber(LL_Config.aspectRatio) and (tonumber(LL_Config.aspectRatio) == -1 or tonumber(LL_Config.aspectRatio) <= self.realAspectRatio) and tonumber(LL_Config.aspectRatio) or self.currentAspectRatio
self.backgroundEnabled = LL_Config.backgroundEnabled == "1"
if not self.backgroundEnabled then self:HideBackground() end
else
LL_Config = {}
print("LL: Welcome to LayoutLines. Please type /ll ? for usage commands.")
end
end
function LL:SaveConfig()
LL_Config.SD_H, LL_Config.SD_V, LL_Config.border = tostring(self.SD_H), tostring(self.SD_V), tostring(self.border)
LL_Config.aspectRatio, LL_Config.backgroundEnabled = tostring(self.currentAspectRatio), self.backgroundEnabled and "1" or "0"
end
-- Usage and Toggle Functions
function LL:PrintUsage()
print("LayoutLines Addon Usage:")
print("/ll - Toggle guidelines on/off")
print("/ll Hx Vy Bz BG - Set divisions and options:")
print(string.format(" Hx - Set horizontal divisions (x between 2 and %d)", self.MAX_SD_H))
print(string.format(" Vy - Set vertical divisions (y between 2 and %d)", self.MAX_SD_V))
print(string.format(" Bz - Set border width (z between 0 and %d)", self.MAX_BORDER))
print(" BG - Toggle background on/off")
print("/ll ? or /ll help - Show this help message")
print("/ll opt or /ll options - Open settings interface")
end
function LL:ToggleLines(fromChatCommand)
if self.linesShown then
self:HideLines()
if fromChatCommand and self.settingsFrame and self.settingsFrame:IsShown() then
self.settingsFrame:Hide()
end
else
self:ShowLines()
end
end
-- Settings Frame Functions
function LL:OpenSettingsFrame()
if not self.settingsFrame then
self:CreateSettingsFrame()
end
self.settingsFrame:Show()
self.settingsFrame:SetFrameStrata("TOOLTIP")
self.settingsFrame:SetFrameLevel(2000)
end
function LL:CreateSettingsFrame()
local frameWidth, frameHeight = 400, 350
local frame = CreateFrame("Frame", "LL_SettingsFrame", UIParent, "BasicFrameTemplateWithInset")
frame:SetSize(frameWidth, frameHeight)
frame:SetPoint("CENTER")
frame:SetMovable(true)
frame:EnableMouse(true)
frame:SetResizable(false)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
-- Set the frame to appear on top of other UI elements
frame:SetFrameStrata("TOOLTIP")
frame:SetFrameLevel(2000)
-- Ensure the frame is visible when opened
frame:SetScript("OnShow", function(self)
self:SetFrameStrata("TOOLTIP")
self:SetFrameLevel(2000)
end)
self.settingsFrame = frame
frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
frame.title:SetPoint("TOP", 0, -10)
frame.title:SetText("LayoutLines Settings")
local hvGroup = CreateFrame("Frame", nil, frame)
hvGroup:SetSize(frameWidth - 40, 50)
hvGroup:SetPoint("TOP", 0, -50)
local function CreateSlider(name, parent, min, max, value, width, point, text)
local slider = CreateFrame("Slider", "LL_"..name.."Slider", parent, "OptionsSliderTemplate")
slider:SetPoint(unpack(point))
slider:SetMinMaxValues(min, max)
slider:SetValueStep(1)
slider:SetValue(value)
slider:SetWidth(width)
slider:SetObeyStepOnDrag(true)
slider:SetScript("OnValueChanged", function(self, value)
value = math.floor(value + 0.5)
LL[name] = value
_G[self:GetName() .. 'Text']:SetText(text .. value)
LL:SaveConfig()
if LL.linesShown then LL:HideLines(); LL:ShowLines() end
end)
_G[slider:GetName() .. 'Low']:SetText(tostring(min))
_G[slider:GetName() .. 'High']:SetText(tostring(max))
_G[slider:GetName() .. 'Text']:SetText(text .. value)
return slider
end
CreateSlider("SD_H", hvGroup, 2, self.MAX_SD_H, self.SD_H, 150, {"LEFT", 10, 0}, "Horizontal Divisions: ")
CreateSlider("SD_V", hvGroup, 2, self.MAX_SD_V, self.SD_V, 150, {"RIGHT", -10, 0}, "Vertical Divisions: ")
CreateSlider("border", frame, 0, self.MAX_BORDER, self.border, frameWidth - 60, {"TOP", hvGroup, "BOTTOM", 0, -30}, "Border Width (pixels): ")
local aspectRatioDropdown = CreateFrame("Frame", "LL_AspectRatioDropdown", frame, "UIDropDownMenuTemplate")
aspectRatioDropdown:SetPoint("TOP", _G["LL_borderSlider"], "BOTTOM", 0, -30)
local aspectRatioLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
aspectRatioLabel:SetPoint("BOTTOMLEFT", aspectRatioDropdown, "TOPLEFT", 20, 0)
aspectRatioLabel:SetText("Aspect Ratio:")
UIDropDownMenu_SetWidth(aspectRatioDropdown, 100)
UIDropDownMenu_SetText(aspectRatioDropdown, self:GetCurrentAspectRatioText())
UIDropDownMenu_Initialize(aspectRatioDropdown, function(self, level, menuList)
local info = UIDropDownMenu_CreateInfo()
for i, ratio in ipairs(LL.aspectRatios) do
info.text, info.value = ratio[1], ratio[2]
info.func = function(self)
LL:SetAspectRatio(self.value)
UIDropDownMenu_SetSelectedValue(aspectRatioDropdown, self.value)
UIDropDownMenu_SetText(aspectRatioDropdown, self:GetText())
CloseDropDownMenus()
end
info.checked = (ratio[2] == LL.currentAspectRatio)
info.disabled = (ratio[2] ~= -1 and ratio[2] > LL.realAspectRatio)
UIDropDownMenu_AddButton(info)
end
end)
local bgCheckbox = CreateFrame("CheckButton", nil, frame, "UICheckButtonTemplate")
bgCheckbox:SetPoint("TOP", aspectRatioDropdown, "BOTTOM", 0, -20)
bgCheckbox.text = bgCheckbox:CreateFontString(nil, "OVERLAY", "GameFontNormal")
bgCheckbox.text:SetPoint("LEFT", bgCheckbox, "RIGHT", 0, 0)
bgCheckbox.text:SetText("Enable Background")
bgCheckbox:SetChecked(self.backgroundEnabled)
bgCheckbox:SetScript("OnClick", function(self)
LL.backgroundEnabled = self:GetChecked()
LL:SaveConfig()
if LL.linesShown then LL:HideLines(); LL:ShowLines() end
end)
-- Modify the toggle button to also bring the frame to the front
local toggleButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
toggleButton:SetSize(200, 40)
toggleButton:SetPoint("BOTTOM", 0, 20)
toggleButton:SetText("Toggle Guidelines")
toggleButton:SetScript("OnClick", function()
LL:ToggleLines(false) -- Pass false to indicate it's not from chat command
frame:SetFrameStrata("TOOLTIP")
frame:SetFrameLevel(2000)
end)
end
function LL:GetCurrentAspectRatioText()
if self.currentAspectRatio == -1 then return "Real (" .. string.format("%.2f", self.realAspectRatio) .. ")" end
for _, ratio in ipairs(self.aspectRatios) do
if ratio[2] == self.currentAspectRatio then return ratio[1] end
end
return "Custom"
end
function LL:SetAspectRatio(ratio)
if ratio == -1 or ratio <= self.realAspectRatio then
self.currentAspectRatio = ratio
self:SaveConfig()
if self.linesShown then self:HideLines(); self:ShowLines() end
end
end
-- Slash Command Registration
SLASH_LL1 = "/ll"
SlashCmdList["LL"] = function(msg)
msg = msg:lower()
if msg == "?" or msg == "help" then
LL:PrintUsage()
elseif msg == "opt" or msg == "options" then
LL:OpenSettingsFrame()
elseif #msg == 0 then
LL:ToggleLines(true) -- Pass true to indicate it's from chat command
else
LL:SetParameters({string.split(" ", msg)})
end
end
-- Event Handling with Debounce for UI_SCALE_CHANGED
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:RegisterEvent("DISPLAY_SIZE_CHANGED")
eventFrame:RegisterEvent("UI_SCALE_CHANGED")
local debounceTimer = nil
local debounceDelay = 0.5 -- 500ms debounce delay
local function updateLayout()
LL.realAspectRatio = UIParent:GetWidth() / UIParent:GetHeight()
if LL.linesShown then
LL:HideLines()
LL:ShowLines()
end
end
eventFrame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" then
LL.realAspectRatio = UIParent:GetWidth() / UIParent:GetHeight()
LL:LoadConfig()
LL:HideLines()
elseif event == "DISPLAY_SIZE_CHANGED" then
-- Immediate update for display size changes
updateLayout()
elseif event == "UI_SCALE_CHANGED" then
-- Debounced update for UI scale changes
if debounceTimer then
debounceTimer:Cancel()
end
debounceTimer = C_Timer.NewTimer(debounceDelay, function()
updateLayout()
debounceTimer = nil
end)
end
end)