-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullSize.lua
More file actions
422 lines (372 loc) · 14.2 KB
/
Copy pathPullSize.lua
File metadata and controls
422 lines (372 loc) · 14.2 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
-- PullSize core
-- Counts hostile enemy nameplates and displays the count on screen.
-- Auto-applies a configured nameplate distance in instances.
local ADDON = "PullSize"
local PREFIX = "|cffff6600[" .. ADDON .. "]|r"
local TICK_INTERVAL = 0.1
local CVAR = "nameplateMaxDistance"
-- Public API table. Config panel reaches in through this.
PullSize = PullSize or {}
------------------------------------------------------------------------------
-- Saved variables and defaults
------------------------------------------------------------------------------
local INSTANCE_TYPES_TO_OVERRIDE = {
party = true, -- 5-mans / M+
raid = true,
scenario = true, -- delves
}
-- Default threshold definitions. {minCount, r, g, b}
-- These get copied into PullSizeDB.thresholds on first load so the user can
-- modify them. Sorted ascending by minCount.
local DEFAULT_THRESHOLDS = {
{2, 1.00, 0.84, 0.00}, -- yellow
{3, 1.00, 0.55, 0.00}, -- orange
{6, 1.00, 0.10, 0.10}, -- red
}
local DEFAULT_BELOW_COLOR = {1.00, 1.00, 1.00} -- white
PullSizeDB = PullSizeDB or {}
local function InitDB()
if PullSizeDB.instanceDistance == nil then PullSizeDB.instanceDistance = 10 end
if PullSizeDB.autoSetDistance == nil then PullSizeDB.autoSetDistance = true end
PullSizeDB.display = PullSizeDB.display or {}
if PullSizeDB.locked == nil then PullSizeDB.locked = false end
if PullSizeDB.hideOutOfCombat == nil then PullSizeDB.hideOutOfCombat = false end
if PullSizeDB.dimBelowThreshold == nil then PullSizeDB.dimBelowThreshold = false end
if PullSizeDB.fontSize == nil then PullSizeDB.fontSize = 48 end
if not PullSizeDB.thresholds then
PullSizeDB.thresholds = {}
for i, t in ipairs(DEFAULT_THRESHOLDS) do
PullSizeDB.thresholds[i] = {t[1], t[2], t[3], t[4]}
end
else
-- Patch any threshold entry that's missing a count or color channel
-- by pulling from defaults at the same index.
for i, default in ipairs(DEFAULT_THRESHOLDS) do
local t = PullSizeDB.thresholds[i]
if type(t) ~= "table" then
PullSizeDB.thresholds[i] = {default[1], default[2], default[3], default[4]}
else
if type(t[1]) ~= "number" then t[1] = default[1] end
if type(t[2]) ~= "number" then t[2] = default[2] end
if type(t[3]) ~= "number" then t[3] = default[3] end
if type(t[4]) ~= "number" then t[4] = default[4] end
end
end
end
if not PullSizeDB.belowColor then
PullSizeDB.belowColor = {DEFAULT_BELOW_COLOR[1], DEFAULT_BELOW_COLOR[2], DEFAULT_BELOW_COLOR[3]}
else
for i = 1, 3 do
if type(PullSizeDB.belowColor[i]) ~= "number" then
PullSizeDB.belowColor[i] = DEFAULT_BELOW_COLOR[i]
end
end
end
if PullSizeDB.distancePreset == nil then PullSizeDB.distancePreset = "custom" end
end
------------------------------------------------------------------------------
-- Counting
------------------------------------------------------------------------------
local function CountHostileNameplates()
local plates = C_NamePlate.GetNamePlates()
if not plates then return 0 end
local count = 0
for _, plate in ipairs(plates) do
local unit = plate.unitToken
if unit and UnitExists(unit)
and UnitCanAttack("player", unit)
and not UnitIsDead(unit) then
count = count + 1
end
end
return count
end
------------------------------------------------------------------------------
-- Color resolution from threshold table
------------------------------------------------------------------------------
local function ColorForCount(n)
-- Walk thresholds ascending; the highest minCount that n meets wins.
-- Skip any malformed entry (e.g. missing count) rather than erroring.
local r, g, b = PullSizeDB.belowColor[1], PullSizeDB.belowColor[2], PullSizeDB.belowColor[3]
for _, t in ipairs(PullSizeDB.thresholds) do
local count = t and t[1]
if type(count) == "number" and n >= count then
r, g, b = t[2], t[3], t[4]
elseif type(count) == "number" then
break
end
end
return r, g, b
end
local function LowestThreshold()
local t = PullSizeDB.thresholds[1]
if t and type(t[1]) == "number" then return t[1] end
return 1
end
------------------------------------------------------------------------------
-- Display
------------------------------------------------------------------------------
local display = CreateFrame("Frame", "PullSizeDisplay", UIParent, "BackdropTemplate")
display:SetSize(80, 80)
display:SetPoint("CENTER", UIParent, "CENTER", 0, 150) -- default until SVs apply
display:SetClampedToScreen(true)
display.text = display:CreateFontString(nil, "OVERLAY")
display.text:SetFont("Fonts\\FRIZQT__.TTF", 48, "OUTLINE")
display.text:SetPoint("CENTER", display, "CENTER", 0, 0)
display.text:SetText("0")
display.text:SetTextColor(1, 1, 1, 1)
local function ApplyLock()
if PullSizeDB.locked then
display:SetMovable(false)
display:EnableMouse(false)
else
display:SetMovable(true)
display:EnableMouse(true)
display:RegisterForDrag("LeftButton")
end
end
display:SetScript("OnDragStart", function(self)
if not PullSizeDB.locked then self:StartMoving() end
end)
display:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
local point, _, relPoint, x, y = self:GetPoint()
if point then
PullSizeDB.display.point = point
PullSizeDB.display.relPoint = relPoint
PullSizeDB.display.x = math.floor(x + 0.5)
PullSizeDB.display.y = math.floor(y + 0.5)
end
end)
local function ApplyDisplayPosition()
display:ClearAllPoints()
local d = PullSizeDB.display
if d and d.point and d.x and d.y then
display:SetPoint(d.point, UIParent, d.relPoint or d.point, d.x, d.y)
else
display:SetPoint("CENTER", UIParent, "CENTER", 0, 150)
end
end
local function ApplyFontSize()
display.text:SetFont("Fonts\\FRIZQT__.TTF", PullSizeDB.fontSize, "OUTLINE")
end
-- Out-of-combat visibility tracking. PLAYER_REGEN events flip this.
local inCombat = false
local function ShouldBeVisible()
if PullSizeDB.hideOutOfCombat and not inCombat then return false end
return true
end
local function UpdateDisplay()
if not ShouldBeVisible() then
display:Hide()
return
end
display:Show()
local n = CountHostileNameplates()
display.text:SetText(tostring(n))
local r, g, b = ColorForCount(n)
local alpha = 1
if PullSizeDB.dimBelowThreshold and n < LowestThreshold() then
alpha = 0.35
end
display.text:SetTextColor(r, g, b, alpha)
end
------------------------------------------------------------------------------
-- Nameplate distance management
------------------------------------------------------------------------------
local function GetCurrentDistance()
return tonumber(GetCVar(CVAR)) or 0
end
local function IsRelevantInstance()
local _, instanceType = IsInInstance()
return INSTANCE_TYPES_TO_OVERRIDE[instanceType] == true
end
local function WriteCVar(value)
pcall(C_CVar.SetCVar, CVAR, tostring(value))
return GetCurrentDistance()
end
local function ReconcileDistance()
if not PullSizeDB.autoSetDistance then return end
local inRelevant = IsRelevantInstance()
local current = GetCurrentDistance()
if inRelevant then
if current ~= PullSizeDB.instanceDistance then
WriteCVar(PullSizeDB.instanceDistance)
end
return
end
if PullSizeDB.userOutdoorDistance == nil then
PullSizeDB.userOutdoorDistance = current
return
end
if current == PullSizeDB.instanceDistance then
WriteCVar(PullSizeDB.userOutdoorDistance)
else
PullSizeDB.userOutdoorDistance = current
end
end
------------------------------------------------------------------------------
-- Public API for config panel
------------------------------------------------------------------------------
function PullSize.RefreshDisplay()
ApplyFontSize()
ApplyDisplayPosition()
ApplyLock()
UpdateDisplay()
end
function PullSize.OpenConfig()
if PullSize.OpenSettings then
PullSize.OpenSettings()
else
print(PREFIX .. " config panel not loaded.")
end
end
function PullSize.ApplyInstanceDistance(n)
PullSizeDB.instanceDistance = n
if IsRelevantInstance() and PullSizeDB.autoSetDistance then
WriteCVar(n)
end
end
function PullSize.ResetDisplayPosition()
PullSizeDB.display = {}
ApplyDisplayPosition()
end
function PullSize.RestoreDefaults()
-- Wipe everything but the display position (which has its own reset
-- button), then re-run InitDB to repopulate from defaults.
local keepDisplay = PullSizeDB.display
for k in pairs(PullSizeDB) do
if k ~= "display" then
PullSizeDB[k] = nil
end
end
PullSizeDB.display = keepDisplay
InitDB()
PullSize.RefreshDisplay()
end
------------------------------------------------------------------------------
-- Ticker
------------------------------------------------------------------------------
local ticker
local function StartTicker()
if ticker then return end
ticker = C_Timer.NewTicker(TICK_INTERVAL, UpdateDisplay)
end
------------------------------------------------------------------------------
-- Slash commands
------------------------------------------------------------------------------
SLASH_PULLSIZE1 = "/ps"
SLASH_PULLSIZE2 = "/pullsize"
SlashCmdList["PULLSIZE"] = function(msg)
msg = (msg or ""):lower():match("^%s*(.-)%s*$")
local cmd, arg = msg:match("^(%S+)%s*(.*)$")
cmd = cmd or ""
if cmd == "" or cmd == "config" or cmd == "options" then
PullSize.OpenConfig()
elseif cmd == "show" then
display:Show()
elseif cmd == "hide" then
display:Hide()
elseif cmd == "count" then
print(PREFIX .. " count = " .. CountHostileNameplates())
elseif cmd == "lock" then
PullSizeDB.locked = true
ApplyLock()
print(PREFIX .. " display locked.")
elseif cmd == "unlock" then
PullSizeDB.locked = false
ApplyLock()
print(PREFIX .. " display unlocked.")
elseif cmd == "reset" then
PullSize.ResetDisplayPosition()
print(PREFIX .. " display position reset to default.")
elseif cmd == "nameplate" or cmd == "np" then
if arg == "" then
local _, instanceType = IsInInstance()
print(PREFIX .. " nameplate config:")
print(" auto-set in instances: " .. tostring(PullSizeDB.autoSetDistance))
print(" in-instance distance: " .. PullSizeDB.instanceDistance .. " yards")
print(" outdoor preference: " .. tostring(PullSizeDB.userOutdoorDistance) .. " yards")
print(" current CVar value: " .. GetCurrentDistance() .. " yards")
print(" in relevant instance: " .. tostring(IsRelevantInstance()))
print(" instance type: " .. tostring(instanceType))
elseif arg == "off" then
PullSizeDB.autoSetDistance = false
print(PREFIX .. " auto-set disabled.")
elseif arg == "on" then
PullSizeDB.autoSetDistance = true
ReconcileDistance()
print(PREFIX .. " auto-set enabled.")
else
local n = tonumber(arg)
if n and n >= 5 and n <= 100 then
PullSize.ApplyInstanceDistance(n)
print(PREFIX .. " in-instance nameplate distance set to " .. n .. " yards.")
else
print(PREFIX .. " usage: /ps nameplate <5-100> | on | off")
end
end
elseif cmd == "color" or cmd == "colour" then
local idx = tonumber(arg)
if idx and idx >= 1 and idx <= 3 then
if PullSize.OpenColorPickerFor then
PullSize.OpenColorPickerFor(idx)
else
print(PREFIX .. " color picker not loaded.")
end
else
print(PREFIX .. " usage: /ps color <1|2|3>")
end
elseif cmd == "reconcile" then
ReconcileDistance()
print(PREFIX .. " reconciled.")
elseif cmd == "restore" then
if PullSizeDB.userOutdoorDistance then
WriteCVar(PullSizeDB.userOutdoorDistance)
print(PREFIX .. " restored outdoor nameplate distance ("
.. PullSizeDB.userOutdoorDistance .. " yards).")
else
print(PREFIX .. " no outdoor preference saved.")
end
else
print(PREFIX .. " usage:")
print(" /ps - open config panel")
print(" /ps show | hide - toggle display")
print(" /ps lock | unlock - lock or unlock display")
print(" /ps reset - reset display to default position")
print(" /ps count - print current count")
print(" /ps color <1|2|3> - edit a breakpoint color")
print(" /ps nameplate - show nameplate distance config")
print(" /ps nameplate <yards> - set in-instance nameplate distance")
print(" /ps nameplate on|off - enable/disable auto-set in instances")
print(" /ps reconcile - manually run apply/restore logic")
print(" /ps restore - force-write outdoor preference")
end
end
------------------------------------------------------------------------------
-- Boot
------------------------------------------------------------------------------
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("ZONE_CHANGED_NEW_AREA")
f:RegisterEvent("SCENARIO_COMPLETED")
f:RegisterEvent("PLAYER_REGEN_DISABLED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")
f:SetScript("OnEvent", function(_, event)
if event == "PLAYER_LOGIN" then
InitDB()
ApplyDisplayPosition()
ApplyFontSize()
ApplyLock()
StartTicker()
print(PREFIX .. " loaded. /ps for config and commands.")
elseif event == "PLAYER_REGEN_DISABLED" then
inCombat = true
elseif event == "PLAYER_REGEN_ENABLED" then
inCombat = false
else
InitDB()
ReconcileDistance()
end
end)