comparison Libs/LibDBIcon-1.0/LibDBIcon-1.0.lua @ 22:3596dadf9a90 v8.1.0.022

- ToC and Libraries update.
author Tercioo
date Mon, 31 Dec 2018 11:30:31 -0200
parents 9ad7f3c634f1
children
comparison
equal deleted inserted replaced
21:f29a995e98e3 22:3596dadf9a90
4 -- 4 --
5 -- Allows addons to easily create a lightweight minimap icon as an alternative to heavier LDB displays. 5 -- Allows addons to easily create a lightweight minimap icon as an alternative to heavier LDB displays.
6 -- 6 --
7 7
8 local DBICON10 = "LibDBIcon-1.0" 8 local DBICON10 = "LibDBIcon-1.0"
9 local DBICON10_MINOR = 36 -- Bump on changes 9 local DBICON10_MINOR = 43 -- Bump on changes
10 if not LibStub then error(DBICON10 .. " requires LibStub.") end 10 if not LibStub then error(DBICON10 .. " requires LibStub.") end
11 local ldb = LibStub("LibDataBroker-1.1", true) 11 local ldb = LibStub("LibDataBroker-1.1", true)
12 if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end 12 if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end
13 local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR) 13 local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR)
14 if not lib then return end 14 if not lib then return end
15 15
16 lib.disabled = lib.disabled or nil
17 lib.objects = lib.objects or {} 16 lib.objects = lib.objects or {}
18 lib.callbackRegistered = lib.callbackRegistered or nil 17 lib.callbackRegistered = lib.callbackRegistered or nil
19 lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib) 18 lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
20 lib.notCreated = lib.notCreated or {} 19 lib.notCreated = lib.notCreated or {}
20 lib.radius = lib.radius or 5
21 lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibDBIconTooltip", UIParent, "GameTooltipTemplate") 21 lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibDBIconTooltip", UIParent, "GameTooltipTemplate")
22 local next, Minimap = next, Minimap
23 local isDraggingButton = false
22 24
23 function lib:IconCallback(event, name, key, value) 25 function lib:IconCallback(event, name, key, value)
24 if lib.objects[name] then 26 if lib.objects[name] then
25 if key == "icon" then 27 if key == "icon" then
26 lib.objects[name].icon:SetTexture(value) 28 lib.objects[name].icon:SetTexture(value)
54 local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM" 56 local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
55 return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf 57 return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
56 end 58 end
57 59
58 local function onEnter(self) 60 local function onEnter(self)
59 if self.isMoving then return end 61 if isDraggingButton then return end
62
63 for _, button in next, lib.objects do
64 if button.showOnMouseover then
65 button.fadeOut:Stop()
66 button:SetAlpha(1)
67 end
68 end
69
60 local obj = self.dataObject 70 local obj = self.dataObject
61 if obj.OnTooltipShow then 71 if obj.OnTooltipShow then
62 lib.tooltip:SetOwner(self, "ANCHOR_NONE") 72 lib.tooltip:SetOwner(self, "ANCHOR_NONE")
63 lib.tooltip:SetPoint(getAnchors(self)) 73 lib.tooltip:SetPoint(getAnchors(self))
64 obj.OnTooltipShow(lib.tooltip) 74 obj.OnTooltipShow(lib.tooltip)
67 obj.OnEnter(self) 77 obj.OnEnter(self)
68 end 78 end
69 end 79 end
70 80
71 local function onLeave(self) 81 local function onLeave(self)
82 lib.tooltip:Hide()
83
84 if not isDraggingButton then
85 for _, button in next, lib.objects do
86 if button.showOnMouseover then
87 button.fadeOut:Play()
88 end
89 end
90 end
91
72 local obj = self.dataObject 92 local obj = self.dataObject
73 lib.tooltip:Hide() 93 if obj.OnLeave then
74 if obj.OnLeave then obj.OnLeave(self) end 94 obj.OnLeave(self)
95 end
75 end 96 end
76 97
77 -------------------------------------------------------------------------------- 98 --------------------------------------------------------------------------------
78 99
79 local onClick, onMouseUp, onMouseDown, onDragStart, onDragStop, updatePosition 100 local onDragStart, updatePosition
80 101
81 do 102 do
82 local minimapShapes = { 103 local minimapShapes = {
83 ["ROUND"] = {true, true, true, true}, 104 ["ROUND"] = {true, true, true, true},
84 ["SQUARE"] = {false, false, false, false}, 105 ["SQUARE"] = {false, false, false, false},
94 ["TRICORNER-TOPRIGHT"] = {true, false, true, true}, 115 ["TRICORNER-TOPRIGHT"] = {true, false, true, true},
95 ["TRICORNER-BOTTOMLEFT"] = {true, true, false, true}, 116 ["TRICORNER-BOTTOMLEFT"] = {true, true, false, true},
96 ["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false}, 117 ["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false},
97 } 118 }
98 119
99 function updatePosition(button) 120 local rad, cos, sin, sqrt, max, min = math.rad, math.cos, math.sin, math.sqrt, math.max, math.min
100 local angle = math.rad(button.db and button.db.minimapPos or button.minimapPos or 225) 121 function updatePosition(button, position)
101 local x, y, q = math.cos(angle), math.sin(angle), 1 122 local angle = rad(position or 225)
123 local x, y, q = cos(angle), sin(angle), 1
102 if x < 0 then q = q + 1 end 124 if x < 0 then q = q + 1 end
103 if y > 0 then q = q + 2 end 125 if y > 0 then q = q + 2 end
104 local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND" 126 local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
105 local quadTable = minimapShapes[minimapShape] 127 local quadTable = minimapShapes[minimapShape]
128 local w = (Minimap:GetWidth() / 2) + lib.radius
129 local h = (Minimap:GetHeight() / 2) + lib.radius
106 if quadTable[q] then 130 if quadTable[q] then
107 x, y = x*80, y*80 131 x, y = x*w, y*h
108 else 132 else
109 local diagRadius = 103.13708498985 --math.sqrt(2*(80)^2)-10 133 local diagRadiusW = sqrt(2*(w)^2)-10
110 x = math.max(-80, math.min(x*diagRadius, 80)) 134 local diagRadiusH = sqrt(2*(h)^2)-10
111 y = math.max(-80, math.min(y*diagRadius, 80)) 135 x = max(-w, min(x*diagRadiusW, w))
136 y = max(-h, min(y*diagRadiusH, h))
112 end 137 end
113 button:SetPoint("CENTER", Minimap, "CENTER", x, y) 138 button:SetPoint("CENTER", Minimap, "CENTER", x, y)
114 end 139 end
115 end 140 end
116 141
117 function onClick(self, b) if self.dataObject.OnClick then self.dataObject.OnClick(self, b) end end 142 local function onClick(self, b)
118 function onMouseDown(self) self.isMouseDown = true; self.icon:UpdateCoord() end 143 if self.dataObject.OnClick then
119 function onMouseUp(self) self.isMouseDown = false; self.icon:UpdateCoord() end 144 self.dataObject.OnClick(self, b)
145 end
146 end
147
148 local function onMouseDown(self)
149 self.isMouseDown = true
150 self.icon:UpdateCoord()
151 end
152
153 local function onMouseUp(self)
154 self.isMouseDown = false
155 self.icon:UpdateCoord()
156 end
120 157
121 do 158 do
159 local deg, atan2 = math.deg, math.atan2
122 local function onUpdate(self) 160 local function onUpdate(self)
123 local mx, my = Minimap:GetCenter() 161 local mx, my = Minimap:GetCenter()
124 local px, py = GetCursorPosition() 162 local px, py = GetCursorPosition()
125 local scale = Minimap:GetEffectiveScale() 163 local scale = Minimap:GetEffectiveScale()
126 px, py = px / scale, py / scale 164 px, py = px / scale, py / scale
165 local pos = 225
127 if self.db then 166 if self.db then
128 self.db.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360 167 pos = deg(atan2(py - my, px - mx)) % 360
168 self.db.minimapPos = pos
129 else 169 else
130 self.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360 170 pos = deg(atan2(py - my, px - mx)) % 360
131 end 171 self.minimapPos = pos
132 updatePosition(self) 172 end
173 updatePosition(self, pos)
133 end 174 end
134 175
135 function onDragStart(self) 176 function onDragStart(self)
136 self:LockHighlight() 177 self:LockHighlight()
137 self.isMouseDown = true 178 self.isMouseDown = true
138 self.icon:UpdateCoord() 179 self.icon:UpdateCoord()
139 self:SetScript("OnUpdate", onUpdate) 180 self:SetScript("OnUpdate", onUpdate)
140 self.isMoving = true 181 isDraggingButton = true
141 lib.tooltip:Hide() 182 lib.tooltip:Hide()
142 end 183 for _, button in next, lib.objects do
143 end 184 if button.showOnMouseover then
144 185 button.fadeOut:Stop()
145 function onDragStop(self) 186 button:SetAlpha(1)
187 end
188 end
189 end
190 end
191
192 local function onDragStop(self)
146 self:SetScript("OnUpdate", nil) 193 self:SetScript("OnUpdate", nil)
147 self.isMouseDown = false 194 self.isMouseDown = false
148 self.icon:UpdateCoord() 195 self.icon:UpdateCoord()
149 self:UnlockHighlight() 196 self:UnlockHighlight()
150 self.isMoving = nil 197 isDraggingButton = false
198 for _, button in next, lib.objects do
199 if button.showOnMouseover then
200 button.fadeOut:Play()
201 end
202 end
151 end 203 end
152 204
153 local defaultCoords = {0, 1, 0, 1} 205 local defaultCoords = {0, 1, 0, 1}
154 local function updateCoord(self) 206 local function updateCoord(self)
155 local coords = self:GetParent().dataObject.iconCoords or defaultCoords 207 local coords = self:GetParent().dataObject.iconCoords or defaultCoords
200 button:SetScript("OnDragStop", onDragStop) 252 button:SetScript("OnDragStop", onDragStop)
201 end 253 end
202 button:SetScript("OnMouseDown", onMouseDown) 254 button:SetScript("OnMouseDown", onMouseDown)
203 button:SetScript("OnMouseUp", onMouseUp) 255 button:SetScript("OnMouseUp", onMouseUp)
204 256
257 button.fadeOut = button:CreateAnimationGroup()
258 local animOut = button.fadeOut:CreateAnimation("Alpha")
259 animOut:SetOrder(1)
260 animOut:SetDuration(0.2)
261 animOut:SetFromAlpha(1)
262 animOut:SetToAlpha(0)
263 animOut:SetStartDelay(1)
264 button.fadeOut:SetToFinalAlpha(true)
265
205 lib.objects[name] = button 266 lib.objects[name] = button
206 267
207 if lib.loggedIn then 268 if lib.loggedIn then
208 updatePosition(button) 269 updatePosition(button, db and db.minimapPos)
209 if not db or not db.hide then button:Show() 270 if not db or not db.hide then
210 else button:Hide() end 271 button:Show()
272 else
273 button:Hide()
274 end
211 end 275 end
212 lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback 276 lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
213 end 277 end
214 278
215 -- We could use a metatable.__index on lib.objects, but then we'd create 279 -- We could use a metatable.__index on lib.objects, but then we'd create
219 createButton(name, lib.notCreated[name][1], lib.notCreated[name][2]) 283 createButton(name, lib.notCreated[name][1], lib.notCreated[name][2])
220 lib.notCreated[name] = nil 284 lib.notCreated[name] = nil
221 end 285 end
222 end 286 end
223 287
224 lib.loggedIn = lib.loggedIn or false
225 -- Wait a bit with the initial positioning to let any GetMinimapShape addons 288 -- Wait a bit with the initial positioning to let any GetMinimapShape addons
226 -- load up. 289 -- load up.
227 if not lib.loggedIn then 290 if not lib.loggedIn then
228 local f = CreateFrame("Frame") 291 local f = CreateFrame("Frame")
229 f:SetScript("OnEvent", function() 292 f:SetScript("OnEvent", function(f)
230 for _, object in pairs(lib.objects) do 293 for _, button in next, lib.objects do
231 updatePosition(object) 294 updatePosition(button, button.db and button.db.minimapPos)
232 if not lib.disabled and (not object.db or not object.db.hide) then object:Show() 295 if not button.db or not button.db.hide then
233 else object:Hide() end 296 button:Show()
297 else
298 button:Hide()
299 end
234 end 300 end
235 lib.loggedIn = true 301 lib.loggedIn = true
236 f:SetScript("OnEvent", nil) 302 f:SetScript("OnEvent", nil)
237 f = nil
238 end) 303 end)
239 f:RegisterEvent("PLAYER_LOGIN") 304 f:RegisterEvent("PLAYER_LOGIN")
240 end 305 end
241 306
242 local function getDatabase(name) 307 local function getDatabase(name)
243 return lib.notCreated[name] and lib.notCreated[name][2] or lib.objects[name].db 308 return lib.notCreated[name] and lib.notCreated[name][2] or lib.objects[name].db
244 end 309 end
245 310
246 function lib:Register(name, object, db) 311 function lib:Register(name, object, db)
247 if not object.icon then error("Can't register LDB objects without icons set!") end 312 if not object.icon then error("Can't register LDB objects without icons set!") end
248 if lib.objects[name] or lib.notCreated[name] then error("Already registered, nubcake.") end 313 if lib.objects[name] or lib.notCreated[name] then error(DBICON10.. ": Object '".. name .."' is already registered.") end
249 if not lib.disabled and (not db or not db.hide) then 314 if not db or not db.hide then
250 createButton(name, object, db) 315 createButton(name, object, db)
251 else 316 else
252 lib.notCreated[name] = {object, db} 317 lib.notCreated[name] = {object, db}
253 end 318 end
254 end 319 end
258 if lib.objects[name] then 323 if lib.objects[name] then
259 lib.objects[name]:SetScript("OnDragStart", nil) 324 lib.objects[name]:SetScript("OnDragStart", nil)
260 lib.objects[name]:SetScript("OnDragStop", nil) 325 lib.objects[name]:SetScript("OnDragStop", nil)
261 end 326 end
262 local db = getDatabase(name) 327 local db = getDatabase(name)
263 if db then db.lock = true end 328 if db then
329 db.lock = true
330 end
264 end 331 end
265 332
266 function lib:Unlock(name) 333 function lib:Unlock(name)
267 if not lib:IsRegistered(name) then return end 334 if not lib:IsRegistered(name) then return end
268 if lib.objects[name] then 335 if lib.objects[name] then
269 lib.objects[name]:SetScript("OnDragStart", onDragStart) 336 lib.objects[name]:SetScript("OnDragStart", onDragStart)
270 lib.objects[name]:SetScript("OnDragStop", onDragStop) 337 lib.objects[name]:SetScript("OnDragStop", onDragStop)
271 end 338 end
272 local db = getDatabase(name) 339 local db = getDatabase(name)
273 if db then db.lock = nil end 340 if db then
341 db.lock = nil
342 end
274 end 343 end
275 344
276 function lib:Hide(name) 345 function lib:Hide(name)
277 if not lib.objects[name] then return end 346 if not lib.objects[name] then return end
278 lib.objects[name]:Hide() 347 lib.objects[name]:Hide()
279 end 348 end
349
280 function lib:Show(name) 350 function lib:Show(name)
281 if lib.disabled then return end
282 check(name) 351 check(name)
283 lib.objects[name]:Show() 352 local button = lib.objects[name]
284 updatePosition(lib.objects[name]) 353 if button then
285 end 354 button:Show()
355 updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
356 end
357 end
358
286 function lib:IsRegistered(name) 359 function lib:IsRegistered(name)
287 return (lib.objects[name] or lib.notCreated[name]) and true or false 360 return (lib.objects[name] or lib.notCreated[name]) and true or false
288 end 361 end
362
289 function lib:Refresh(name, db) 363 function lib:Refresh(name, db)
290 if lib.disabled then return end
291 check(name) 364 check(name)
292 local button = lib.objects[name] 365 local button = lib.objects[name]
293 if db then button.db = db end 366 if db then
294 updatePosition(button) 367 button.db = db
368 end
369 updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
295 if not button.db or not button.db.hide then 370 if not button.db or not button.db.hide then
296 button:Show() 371 button:Show()
297 else 372 else
298 button:Hide() 373 button:Hide()
299 end 374 end
303 else 378 else
304 button:SetScript("OnDragStart", nil) 379 button:SetScript("OnDragStart", nil)
305 button:SetScript("OnDragStop", nil) 380 button:SetScript("OnDragStop", nil)
306 end 381 end
307 end 382 end
383
308 function lib:GetMinimapButton(name) 384 function lib:GetMinimapButton(name)
309 return lib.objects[name] 385 return lib.objects[name]
310 end 386 end
311 387
312 function lib:EnableLibrary() 388 do
313 lib.disabled = nil 389 local function OnMinimapEnter()
314 for name, object in pairs(lib.objects) do 390 if isDraggingButton then return end
315 if not object.db or not object.db.hide then 391 for _, button in next, lib.objects do
316 object:Show() 392 if button.showOnMouseover then
317 updatePosition(object) 393 button.fadeOut:Stop()
318 end 394 button:SetAlpha(1)
319 end 395 end
320 for name, data in pairs(lib.notCreated) do 396 end
321 if not data.db or not data.db.hide then 397 end
322 createButton(name, data[1], data[2]) 398 local function OnMinimapLeave()
323 lib.notCreated[name] = nil 399 if isDraggingButton then return end
324 end 400 for _, button in next, lib.objects do
325 end 401 if button.showOnMouseover then
326 end 402 button.fadeOut:Play()
327 403 end
328 function lib:DisableLibrary() 404 end
329 lib.disabled = true 405 end
330 for name, object in pairs(lib.objects) do 406 Minimap:HookScript("OnEnter", OnMinimapEnter)
331 object:Hide() 407 Minimap:HookScript("OnLeave", OnMinimapLeave)
332 end 408
333 end 409 function lib:ShowOnEnter(name, value)
334 410 local button = lib.objects[name]
411 if button then
412 if value then
413 button.showOnMouseover = true
414 button.fadeOut:Stop()
415 button:SetAlpha(0)
416 else
417 button.showOnMouseover = false
418 button.fadeOut:Stop()
419 button:SetAlpha(1)
420 end
421 end
422 end
423 end
424
425 function lib:GetButtonList()
426 local t = {}
427 for name in next, lib.objects do
428 t[#t+1] = name
429 end
430 return t
431 end
432
433 function lib:SetButtonRadius(radius)
434 if type(radius) == "number" then
435 lib.radius = radius
436 for _, button in next, lib.objects do
437 updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
438 end
439 end
440 end
441
442 function lib:SetButtonToPosition(button, position)
443 updatePosition(lib.objects[button] or button, position)
444 end
445
446 -- Upgrade!
447 for name, button in next, lib.objects do
448 local db = getDatabase(name)
449 if not db or not db.lock then
450 button:SetScript("OnDragStart", onDragStart)
451 button:SetScript("OnDragStop", onDragStop)
452 end
453 button:SetScript("OnEnter", onEnter)
454 button:SetScript("OnLeave", onLeave)
455 button:SetScript("OnClick", onClick)
456 button:SetScript("OnMouseDown", onMouseDown)
457 button:SetScript("OnMouseUp", onMouseUp)
458
459 if not button.fadeOut then -- Upgrade to 39
460 button.fadeOut = button:CreateAnimationGroup()
461 local animOut = button.fadeOut:CreateAnimation("Alpha")
462 animOut:SetOrder(1)
463 animOut:SetDuration(0.2)
464 animOut:SetFromAlpha(1)
465 animOut:SetToAlpha(0)
466 animOut:SetStartDelay(1)
467 button.fadeOut:SetToFinalAlpha(true)
468 end
469 end
470 lib:SetButtonRadius(lib.radius) -- Upgrade to 40