Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
421 lines (377 loc) · 13.5 KB
/
Copy pathCore.lua
File metadata and controls
421 lines (377 loc) · 13.5 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
local ADDON, ns = ...
-- ================== Config ==================
local DEFAULT_BREAK_MINUTES = 10 -- used if BigWigs line has no minutes
local PREFIX = "PixelMemes" -- our addon comm prefix
local GAP = 10
-- SavedVariables defaults
PixelMemesDB = PixelMemesDB or {
scale = 1.0,
point = nil,
}
-- ---- Sizes ----
local IMAGE_SIZE = 520
local FRAME_W, FRAME_H = IMAGE_SIZE, IMAGE_SIZE
local function PlayerName()
local n = UnitName("player")
return n
end
local function GetGroupChannel()
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) or IsInRaid(LE_PARTY_CATEGORY_INSTANCE) then
return "INSTANCE_CHAT"
elseif IsInRaid() then
return "RAID"
elseif IsInGroup() then
return "PARTY"
end
return nil
end
-- Try to extract minutes from arbitrary chat lines
local function ExtractMinutesFromMessage(msg)
if not msg or msg == "" then return nil end
local s = msg:lower()
return tonumber(s:match("break%W+for%W+(%d+)"))
or tonumber(s:match("(%d+)%s*minutes"))
or tonumber(s:match("(%d+)%s*minute"))
or tonumber(s:match("(%d+)%s*mins"))
or tonumber(s:match("(%d+)%s*min"))
or tonumber(s:match("in%W+(%d+)%s*min"))
or tonumber(s:match("(%d+)%s*%f[%a]m%f[%A]"))
end
-- ================== UI ==================
local f = CreateFrame("Frame", "PixelMemesFrame", UIParent, "BackdropTemplate")
f:SetSize(FRAME_W, FRAME_H)
f:SetClampedToScreen(true)
f:SetMovable(true)
f:EnableMouse(true)
f:RegisterForDrag("LeftButton")
f:Hide()
f:SetScript("OnDragStart", function(self) self:StartMoving() end)
f:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
local point, _, relPoint, x, y = self:GetPoint(1)
PixelMemesDB.point = {point, relPoint, x, y}
end)
local function ApplySavedLayout()
f:ClearAllPoints()
if PixelMemesDB.point then
local p, rp, x, y = unpack(PixelMemesDB.point)
f:SetPoint(p, UIParent, rp, x, y)
else
f:SetPoint("CENTER")
end
f:SetScale(PixelMemesDB.scale or 1.0)
end
-- Image texture fills the frame
local tex = f:CreateTexture(nil, "ARTWORK")
tex:SetAllPoints(f)
tex:SetTexCoord(0, 1, 0, 1)
-- Title above image
local title = f:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("BOTTOM", tex, "TOP", 0, 40)
title:SetJustifyH("CENTER")
title:SetText("Break Time!")
do
local font, _, flags = title:GetFont()
title:SetFont(font, 45, flags)
end
-- Timer just below title
local timerText = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightHuge")
timerText:SetPoint("TOP", title, "BOTTOM", 0, -5)
timerText:SetJustifyH("CENTER")
timerText:SetText("")
do
local font, _, flags = timerText:GetFont()
timerText:SetFont(font, 30, flags)
end
-- Close button
local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
close:SetPoint("TOPRIGHT", 0, 0)
-- Scale slider
local slider = CreateFrame("Slider", "PixelMemesScaleSlider", UIParent, "OptionsSliderTemplate")
slider:SetWidth(300)
slider:SetMinMaxValues(0.5, 2.0)
slider:SetValueStep(0.05)
slider:SetObeyStepOnDrag(true)
slider:SetPoint("TOP", f, "BOTTOM", 0, -20)
slider:Hide()
_G[slider:GetName().."Low"]:SetText("0.5x")
_G[slider:GetName().."High"]:SetText("2.0x")
_G[slider:GetName().."Text"]:SetText("Scale")
slider:SetScript("OnValueChanged", function(self, value)
value = math.max(0.5, math.min(2.0, tonumber(value) or 1))
f:SetScale(value)
PixelMemesDB.scale = value
end)
f:HookScript("OnShow", function()
slider:SetValue(PixelMemesDB.scale or 1.0)
slider:Show()
end)
f:HookScript("OnHide", function() slider:Hide() end)
f:EnableMouseWheel(true)
f:SetScript("OnMouseWheel", function(self, delta)
if IsControlKeyDown() then
local v = (PixelMemesDB.scale or 1.0) + (delta > 0 and 0.05 or -0.05)
v = math.max(0.5, math.min(2.0, v))
slider:SetValue(v)
end
end)
-- ================== Logic ==================
local endTime, running = 0, false
local rotationTicker = nil
local ROTATION_INTERVAL = 20 -- seconds between image rotations
local function StopRotationTicker()
if rotationTicker then
if type(rotationTicker.Cancel) == "function" then
rotationTicker:Cancel()
end
rotationTicker = nil
end
end
local function pickRandomMeme(chosen)
local DEFAULT_TEXTURE = "Interface\\FriendsFrame\\Battlenet-Portrait"
if type(PixelMemes_List) ~= "table" or #PixelMemes_List == 0 then
tex:SetTexture(DEFAULT_TEXTURE)
-- print("PixelMemes: pickRandomMeme - list empty, using default:", DEFAULT_TEXTURE)
return DEFAULT_TEXTURE
end
if chosen and type(chosen) == "string" then
tex:SetTexture(chosen)
-- print("PixelMemes: pickRandomMeme - forced texture:", chosen)
return chosen
end
local idx = math.random(1, #PixelMemes_List)
local path = PixelMemes_List[idx]
tex:SetTexture(path)
-- print("PixelMemes: pickRandomMeme - chosen index", idx, "path", path, "list_size=", #PixelMemes_List)
return path
end
local function formatTime(sec)
sec = math.max(0, math.floor(sec + 0.5))
local m = math.floor(sec / 60)
local s = sec % 60
return string.format("%02d:%02d", m, s)
end
local function StartBreak(minutes, startedBy, specifiedTexture)
minutes = tonumber(minutes)
if not minutes or minutes <= 0 then return end
local chosenTexture = pickRandomMeme(specifiedTexture)
ApplySavedLayout()
f:Show()
endTime = GetTime() + (minutes * 60)
running = true
-- debug about what we're about to send (if anything)
-- print("PixelMemes: StartBreak - mins=", minutes, "startedBy=", startedBy, "chosenTexture=", tostring(chosenTexture))
-- If we're group leader, broadcast minutes + chosen texture to the group
local chan = GetGroupChannel()
if chan and UnitIsGroupLeader("player") and C_ChatInfo and C_ChatInfo.IsAddonMessagePrefixRegistered and C_ChatInfo.IsAddonMessagePrefixRegistered(PREFIX) then
local DEFAULT_TEXTURE = "Interface\\FriendsFrame\\Battlenet-Portrait"
-- don't broadcast the default placeholder when the list is empty
if chosenTexture == DEFAULT_TEXTURE and (type(PixelMemes_List) ~= "table" or #PixelMemes_List == 0) then
-- print("PixelMemes: Not sending addon message - meme list empty on leader")
else
-- use a tab separator; receivers will split by tab
local msg = tostring(minutes) .. "\t" .. tostring(chosenTexture)
C_ChatInfo.SendAddonMessage(PREFIX, msg, chan)
-- print("PixelMemes: Sent", minutes, chosenTexture)
-- Start a rotation ticker to send new images periodically
StopRotationTicker()
if type(PixelMemes_List) == "table" and #PixelMemes_List > 0 and C_Timer and type(C_Timer.NewTicker) == "function" then
rotationTicker = C_Timer.NewTicker(ROTATION_INTERVAL, function()
local newPath = pickRandomMeme()
if newPath and newPath ~= DEFAULT_TEXTURE then
C_ChatInfo.SendAddonMessage(PREFIX, "ROT\t" .. tostring(newPath), chan)
end
end)
end
end
end
end
f:SetScript("OnUpdate", function(self)
if running then
local remaining = endTime - GetTime()
if remaining <= 0 then
running = false
timerText:SetText("00:00")
PlaySound(SOUNDKIT.ALARM_CLOCK_WARNING_3)
StopRotationTicker()
self:Hide()
else
timerText:SetText(formatTime(remaining))
end
end
end)
-- ================== Slash command ==================
SLASH_PIXELMEMES1 = "/pmeme"
SlashCmdList["PIXELMEMES"] = function(msg)
local minutes = tonumber(msg)
if not minutes or minutes <= 0 then
return
end
StartBreak(minutes, PlayerName())
end
SLASH_PIXELMEMESHIDE1 = "/breakhide"
SlashCmdList["PIXELMEMESHIDE"] = function()
f:Hide()
running = false
StopRotationTicker()
end
-- ================== Hooks (if DBM/BigWigs are installed locally) ==================
local function SetupDBMHook()
if not _G.DBM then return end
if type(_G.DBM.RegisterCallback) == "function" then
_G.DBM:RegisterCallback("DBM_TimerStart", function(_, id, label, timer)
local txt = type(label) == "string" and label:lower() or ""
if txt:find("%f[%a]break%f[%A]") then
local mins = math.max(1, math.floor((tonumber(timer) or 0) / 60 + 0.5))
StartBreak(mins, "DBM")
end
end)
end
if type(_G.DBM.CreateBreakTimer) == "function" then
hooksecurefunc(_G.DBM, "CreateBreakTimer", function(_, mins)
mins = tonumber(mins) or DEFAULT_BREAK_MINUTES
if mins > 0 then
StartBreak(mins, "DBM")
end
end)
end
if type(_G.DBM.CreatePizzaTimer) == "function" then
hooksecurefunc(_G.DBM, "CreatePizzaTimer", function(_, seconds, text)
local txt = type(text) == "string" and text:lower() or ""
if txt:find("%f[%a]break%f[%A]") then
local mins = math.max(1, math.floor((tonumber(seconds) or 0) / 60 + 0.5))
StartBreak(mins, "DBM")
end
end)
end
end
local function SetupBigWigsHook()
local BW = _G.BigWigs
if not BW or type(BW.RegisterMessage) ~= "function" then return end
BW:RegisterMessage("BigWigs_StartBreak", function(_, _, seconds, nick)
local secs = tonumber(seconds) or (DEFAULT_BREAK_MINUTES * 60)
local mins = math.max(1, math.floor(secs / 60 + 0.5))
StartBreak(mins, nick and nick:gsub("-.*","") or "BigWigs")
end)
BW:RegisterMessage("BigWigs_StopBreak", function()
if running then running = false StopRotationTicker() f:Hide() end
end)
BW:RegisterMessage("BigWigs_PluginComm", function(_, msg, seconds, sender)
if msg == "Break" and seconds then
local secs = tonumber(seconds)
if secs then
local mins = math.max(1, math.floor(secs / 60 + 0.5))
StartBreak(mins, sender and sender:gsub("-.*","") or "BigWigs")
end
end
end)
end
-- ================== Events (including chat + addon comm fallbacks) ==================
local ev = CreateFrame("Frame")
ev:RegisterEvent("PLAYER_LOGIN")
ev:RegisterEvent("PLAYER_ENTERING_WORLD")
ev:RegisterEvent("ADDON_LOADED")
ev:RegisterEvent("CHAT_MSG_ADDON")
ev:RegisterEvent("CHAT_MSG_RAID")
ev:RegisterEvent("CHAT_MSG_RAID_WARNING")
ev:RegisterEvent("CHAT_MSG_PARTY")
ev:RegisterEvent("CHAT_MSG_INSTANCE_CHAT")
ev:RegisterEvent("CHAT_MSG_SAY")
ev:RegisterEvent("CHAT_MSG_YELL")
ev:SetScript("OnEvent", function(_, event, ...)
if event == "PLAYER_LOGIN" then
ApplySavedLayout()
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then
C_ChatInfo.RegisterAddonMessagePrefix(PREFIX)
C_ChatInfo.RegisterAddonMessagePrefix("D4")
C_ChatInfo.RegisterAddonMessagePrefix("D4C")
C_ChatInfo.RegisterAddonMessagePrefix("D5")
C_ChatInfo.RegisterAddonMessagePrefix("D5C")
C_ChatInfo.RegisterAddonMessagePrefix("BigWigs")
end
SetupDBMHook()
SetupBigWigsHook()
return
end
if event == "PLAYER_ENTERING_WORLD" then
SetupDBMHook()
SetupBigWigsHook()
return
end
if event == "ADDON_LOADED" then
local name = ...
if name == "DBM-Core" or name == "DBM" then
SetupDBMHook()
elseif name == "BigWigs" or name == "BigWigs_Core" then
SetupBigWigsHook()
end
return
end
if event == "CHAT_MSG_ADDON" then
local prefix, message, channel, sender = ...
sender = sender and sender:gsub("-.*","") or "?"
if not prefix or not message then return end
if prefix == PREFIX then
-- message formats:
-- 1) "<minutes>\t<texturePath?>" (initial break start)
-- 2) "ROT\t<texturePath>" (rotation update only)
local a, b = strsplit("\t", message)
if a == "ROT" then
local path = b
if path and path ~= "" then
tex:SetTexture(path)
end
return
end
local mins, texpath = a, b
local m = tonumber(mins)
if m and m > 0 and sender ~= PlayerName() then
StartBreak(m, sender, texpath)
end
return
end
if (prefix == "D4" or prefix == "D4C" or prefix == "D5" or prefix == "D5C") and message then
local _, _, code, secs = strsplit("\t", message)
if code == "BT" then
local s = tonumber(secs)
if s and s > 0 then
local mins = math.max(1, math.floor(s / 60 + 0.5))
StartBreak(mins, sender)
end
end
return
end
if prefix == "BigWigs" and message then
local msg, secs, nick = strsplit("\t", message)
if msg == "Break" and secs then
local s = tonumber(secs)
if s and s > 0 then
local mins = math.max(1, math.floor(s / 60 + 0.5))
StartBreak(mins, (nick and nick:gsub("-.*","")) or sender)
end
end
return
end
return
end
local msg, author = ...
if not msg then return end
local lower = msg:lower()
if not lower:find("%f[%a]break%f[%A]") then return end
local mins = ExtractMinutesFromMessage(msg)
if mins and mins > 0 then
author = author and author:gsub("-.*","") or author
StartBreak(mins, author)
return
end
if lower:find("bigwigs") and (lower:find("break timer") or lower:find("sending a break")) then
author = author and author:gsub("-.*","") or author
StartBreak(DEFAULT_BREAK_MINUTES, author)
return
end
if lower:find("break") and (lower:find("started") or lower:find("initiated") or lower:find("timer")) then
author = author and author:gsub("-.*","") or author
StartBreak(DEFAULT_BREAK_MINUTES, author)
return
end
end)