tercio@0
|
1
|
tercio@0
|
2 -----------------------------------------------------------------------
|
Tercio@20
|
3 -- LibDBIcon-1.0
|
tercio@0
|
4 --
|
Tercio@20
|
5 -- Allows addons to easily create a lightweight minimap icon as an alternative to heavier LDB displays.
|
tercio@0
|
6 --
|
tercio@0
|
7
|
tercio@0
|
8 local DBICON10 = "LibDBIcon-1.0"
|
Tercio@20
|
9 local DBICON10_MINOR = 36 -- Bump on changes
|
tercio@0
|
10 if not LibStub then error(DBICON10 .. " requires LibStub.") end
|
tercio@0
|
11 local ldb = LibStub("LibDataBroker-1.1", true)
|
tercio@0
|
12 if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end
|
tercio@0
|
13 local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR)
|
tercio@0
|
14 if not lib then return end
|
tercio@0
|
15
|
tercio@0
|
16 lib.disabled = lib.disabled or nil
|
tercio@0
|
17 lib.objects = lib.objects or {}
|
tercio@0
|
18 lib.callbackRegistered = lib.callbackRegistered or nil
|
tercio@0
|
19 lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
|
tercio@0
|
20 lib.notCreated = lib.notCreated or {}
|
Tercio@20
|
21 lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibDBIconTooltip", UIParent, "GameTooltipTemplate")
|
tercio@0
|
22
|
Tercio@20
|
23 function lib:IconCallback(event, name, key, value)
|
tercio@0
|
24 if lib.objects[name] then
|
tercio@0
|
25 if key == "icon" then
|
tercio@0
|
26 lib.objects[name].icon:SetTexture(value)
|
tercio@0
|
27 elseif key == "iconCoords" then
|
tercio@0
|
28 lib.objects[name].icon:UpdateCoord()
|
tercio@0
|
29 elseif key == "iconR" then
|
tercio@0
|
30 local _, g, b = lib.objects[name].icon:GetVertexColor()
|
tercio@0
|
31 lib.objects[name].icon:SetVertexColor(value, g, b)
|
tercio@0
|
32 elseif key == "iconG" then
|
tercio@0
|
33 local r, _, b = lib.objects[name].icon:GetVertexColor()
|
tercio@0
|
34 lib.objects[name].icon:SetVertexColor(r, value, b)
|
tercio@0
|
35 elseif key == "iconB" then
|
tercio@0
|
36 local r, g = lib.objects[name].icon:GetVertexColor()
|
tercio@0
|
37 lib.objects[name].icon:SetVertexColor(r, g, value)
|
tercio@0
|
38 end
|
tercio@0
|
39 end
|
tercio@0
|
40 end
|
tercio@0
|
41 if not lib.callbackRegistered then
|
tercio@0
|
42 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__icon", "IconCallback")
|
tercio@0
|
43 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconCoords", "IconCallback")
|
tercio@0
|
44 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconR", "IconCallback")
|
tercio@0
|
45 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconG", "IconCallback")
|
tercio@0
|
46 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconB", "IconCallback")
|
tercio@0
|
47 lib.callbackRegistered = true
|
tercio@0
|
48 end
|
tercio@0
|
49
|
tercio@0
|
50 local function getAnchors(frame)
|
tercio@0
|
51 local x, y = frame:GetCenter()
|
tercio@0
|
52 if not x or not y then return "CENTER" end
|
tercio@0
|
53 local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
|
tercio@0
|
54 local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
|
tercio@0
|
55 return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
|
tercio@0
|
56 end
|
tercio@0
|
57
|
tercio@0
|
58 local function onEnter(self)
|
tercio@0
|
59 if self.isMoving then return end
|
tercio@0
|
60 local obj = self.dataObject
|
tercio@0
|
61 if obj.OnTooltipShow then
|
Tercio@20
|
62 lib.tooltip:SetOwner(self, "ANCHOR_NONE")
|
Tercio@20
|
63 lib.tooltip:SetPoint(getAnchors(self))
|
Tercio@20
|
64 obj.OnTooltipShow(lib.tooltip)
|
Tercio@20
|
65 lib.tooltip:Show()
|
tercio@0
|
66 elseif obj.OnEnter then
|
tercio@0
|
67 obj.OnEnter(self)
|
tercio@0
|
68 end
|
tercio@0
|
69 end
|
tercio@0
|
70
|
tercio@0
|
71 local function onLeave(self)
|
tercio@0
|
72 local obj = self.dataObject
|
Tercio@20
|
73 lib.tooltip:Hide()
|
tercio@0
|
74 if obj.OnLeave then obj.OnLeave(self) end
|
tercio@0
|
75 end
|
tercio@0
|
76
|
tercio@0
|
77 --------------------------------------------------------------------------------
|
tercio@0
|
78
|
Tercio@20
|
79 local onClick, onMouseUp, onMouseDown, onDragStart, onDragStop, updatePosition
|
tercio@0
|
80
|
tercio@0
|
81 do
|
tercio@0
|
82 local minimapShapes = {
|
tercio@0
|
83 ["ROUND"] = {true, true, true, true},
|
tercio@0
|
84 ["SQUARE"] = {false, false, false, false},
|
tercio@0
|
85 ["CORNER-TOPLEFT"] = {false, false, false, true},
|
tercio@0
|
86 ["CORNER-TOPRIGHT"] = {false, false, true, false},
|
tercio@0
|
87 ["CORNER-BOTTOMLEFT"] = {false, true, false, false},
|
tercio@0
|
88 ["CORNER-BOTTOMRIGHT"] = {true, false, false, false},
|
tercio@0
|
89 ["SIDE-LEFT"] = {false, true, false, true},
|
tercio@0
|
90 ["SIDE-RIGHT"] = {true, false, true, false},
|
tercio@0
|
91 ["SIDE-TOP"] = {false, false, true, true},
|
tercio@0
|
92 ["SIDE-BOTTOM"] = {true, true, false, false},
|
tercio@0
|
93 ["TRICORNER-TOPLEFT"] = {false, true, true, true},
|
tercio@0
|
94 ["TRICORNER-TOPRIGHT"] = {true, false, true, true},
|
tercio@0
|
95 ["TRICORNER-BOTTOMLEFT"] = {true, true, false, true},
|
tercio@0
|
96 ["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false},
|
tercio@0
|
97 }
|
tercio@0
|
98
|
tercio@0
|
99 function updatePosition(button)
|
tercio@0
|
100 local angle = math.rad(button.db and button.db.minimapPos or button.minimapPos or 225)
|
tercio@0
|
101 local x, y, q = math.cos(angle), math.sin(angle), 1
|
tercio@0
|
102 if x < 0 then q = q + 1 end
|
tercio@0
|
103 if y > 0 then q = q + 2 end
|
tercio@0
|
104 local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
|
tercio@0
|
105 local quadTable = minimapShapes[minimapShape]
|
tercio@0
|
106 if quadTable[q] then
|
tercio@0
|
107 x, y = x*80, y*80
|
tercio@0
|
108 else
|
tercio@0
|
109 local diagRadius = 103.13708498985 --math.sqrt(2*(80)^2)-10
|
tercio@0
|
110 x = math.max(-80, math.min(x*diagRadius, 80))
|
tercio@0
|
111 y = math.max(-80, math.min(y*diagRadius, 80))
|
tercio@0
|
112 end
|
tercio@0
|
113 button:SetPoint("CENTER", Minimap, "CENTER", x, y)
|
tercio@0
|
114 end
|
tercio@0
|
115 end
|
tercio@0
|
116
|
tercio@0
|
117 function onClick(self, b) if self.dataObject.OnClick then self.dataObject.OnClick(self, b) end end
|
tercio@0
|
118 function onMouseDown(self) self.isMouseDown = true; self.icon:UpdateCoord() end
|
tercio@0
|
119 function onMouseUp(self) self.isMouseDown = false; self.icon:UpdateCoord() end
|
tercio@0
|
120
|
tercio@0
|
121 do
|
tercio@0
|
122 local function onUpdate(self)
|
tercio@0
|
123 local mx, my = Minimap:GetCenter()
|
tercio@0
|
124 local px, py = GetCursorPosition()
|
tercio@0
|
125 local scale = Minimap:GetEffectiveScale()
|
tercio@0
|
126 px, py = px / scale, py / scale
|
tercio@0
|
127 if self.db then
|
tercio@0
|
128 self.db.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360
|
tercio@0
|
129 else
|
tercio@0
|
130 self.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360
|
tercio@0
|
131 end
|
tercio@0
|
132 updatePosition(self)
|
tercio@0
|
133 end
|
tercio@0
|
134
|
tercio@0
|
135 function onDragStart(self)
|
tercio@0
|
136 self:LockHighlight()
|
tercio@0
|
137 self.isMouseDown = true
|
tercio@0
|
138 self.icon:UpdateCoord()
|
tercio@0
|
139 self:SetScript("OnUpdate", onUpdate)
|
tercio@0
|
140 self.isMoving = true
|
Tercio@20
|
141 lib.tooltip:Hide()
|
tercio@0
|
142 end
|
tercio@0
|
143 end
|
tercio@0
|
144
|
tercio@0
|
145 function onDragStop(self)
|
tercio@0
|
146 self:SetScript("OnUpdate", nil)
|
tercio@0
|
147 self.isMouseDown = false
|
tercio@0
|
148 self.icon:UpdateCoord()
|
tercio@0
|
149 self:UnlockHighlight()
|
tercio@0
|
150 self.isMoving = nil
|
tercio@0
|
151 end
|
tercio@0
|
152
|
tercio@0
|
153 local defaultCoords = {0, 1, 0, 1}
|
tercio@0
|
154 local function updateCoord(self)
|
tercio@0
|
155 local coords = self:GetParent().dataObject.iconCoords or defaultCoords
|
tercio@0
|
156 local deltaX, deltaY = 0, 0
|
tercio@0
|
157 if not self:GetParent().isMouseDown then
|
tercio@0
|
158 deltaX = (coords[2] - coords[1]) * 0.05
|
tercio@0
|
159 deltaY = (coords[4] - coords[3]) * 0.05
|
tercio@0
|
160 end
|
tercio@0
|
161 self:SetTexCoord(coords[1] + deltaX, coords[2] - deltaX, coords[3] + deltaY, coords[4] - deltaY)
|
tercio@0
|
162 end
|
tercio@0
|
163
|
tercio@0
|
164 local function createButton(name, object, db)
|
tercio@0
|
165 local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap)
|
tercio@0
|
166 button.dataObject = object
|
tercio@0
|
167 button.db = db
|
tercio@0
|
168 button:SetFrameStrata("MEDIUM")
|
tercio@0
|
169 button:SetSize(31, 31)
|
tercio@0
|
170 button:SetFrameLevel(8)
|
tercio@0
|
171 button:RegisterForClicks("anyUp")
|
tercio@0
|
172 button:RegisterForDrag("LeftButton")
|
Tercio@20
|
173 button:SetHighlightTexture(136477) --"Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight"
|
tercio@0
|
174 local overlay = button:CreateTexture(nil, "OVERLAY")
|
tercio@0
|
175 overlay:SetSize(53, 53)
|
Tercio@20
|
176 overlay:SetTexture(136430) --"Interface\\Minimap\\MiniMap-TrackingBorder"
|
tercio@0
|
177 overlay:SetPoint("TOPLEFT")
|
tercio@0
|
178 local background = button:CreateTexture(nil, "BACKGROUND")
|
tercio@0
|
179 background:SetSize(20, 20)
|
Tercio@20
|
180 background:SetTexture(136467) --"Interface\\Minimap\\UI-Minimap-Background"
|
tercio@0
|
181 background:SetPoint("TOPLEFT", 7, -5)
|
tercio@0
|
182 local icon = button:CreateTexture(nil, "ARTWORK")
|
tercio@0
|
183 icon:SetSize(17, 17)
|
tercio@0
|
184 icon:SetTexture(object.icon)
|
tercio@0
|
185 icon:SetPoint("TOPLEFT", 7, -6)
|
tercio@0
|
186 button.icon = icon
|
tercio@0
|
187 button.isMouseDown = false
|
tercio@0
|
188
|
tercio@0
|
189 local r, g, b = icon:GetVertexColor()
|
tercio@0
|
190 icon:SetVertexColor(object.iconR or r, object.iconG or g, object.iconB or b)
|
tercio@0
|
191
|
tercio@0
|
192 icon.UpdateCoord = updateCoord
|
tercio@0
|
193 icon:UpdateCoord()
|
tercio@0
|
194
|
tercio@0
|
195 button:SetScript("OnEnter", onEnter)
|
tercio@0
|
196 button:SetScript("OnLeave", onLeave)
|
tercio@0
|
197 button:SetScript("OnClick", onClick)
|
tercio@0
|
198 if not db or not db.lock then
|
tercio@0
|
199 button:SetScript("OnDragStart", onDragStart)
|
tercio@0
|
200 button:SetScript("OnDragStop", onDragStop)
|
tercio@0
|
201 end
|
tercio@0
|
202 button:SetScript("OnMouseDown", onMouseDown)
|
tercio@0
|
203 button:SetScript("OnMouseUp", onMouseUp)
|
tercio@0
|
204
|
tercio@0
|
205 lib.objects[name] = button
|
tercio@0
|
206
|
tercio@0
|
207 if lib.loggedIn then
|
tercio@0
|
208 updatePosition(button)
|
tercio@0
|
209 if not db or not db.hide then button:Show()
|
tercio@0
|
210 else button:Hide() end
|
tercio@0
|
211 end
|
tercio@0
|
212 lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
|
tercio@0
|
213 end
|
tercio@0
|
214
|
tercio@0
|
215 -- We could use a metatable.__index on lib.objects, but then we'd create
|
tercio@0
|
216 -- the icons when checking things like :IsRegistered, which is not necessary.
|
tercio@0
|
217 local function check(name)
|
tercio@0
|
218 if lib.notCreated[name] then
|
tercio@0
|
219 createButton(name, lib.notCreated[name][1], lib.notCreated[name][2])
|
tercio@0
|
220 lib.notCreated[name] = nil
|
tercio@0
|
221 end
|
tercio@0
|
222 end
|
tercio@0
|
223
|
tercio@0
|
224 lib.loggedIn = lib.loggedIn or false
|
tercio@0
|
225 -- Wait a bit with the initial positioning to let any GetMinimapShape addons
|
tercio@0
|
226 -- load up.
|
tercio@0
|
227 if not lib.loggedIn then
|
tercio@0
|
228 local f = CreateFrame("Frame")
|
tercio@0
|
229 f:SetScript("OnEvent", function()
|
tercio@0
|
230 for _, object in pairs(lib.objects) do
|
tercio@0
|
231 updatePosition(object)
|
tercio@0
|
232 if not lib.disabled and (not object.db or not object.db.hide) then object:Show()
|
tercio@0
|
233 else object:Hide() end
|
tercio@0
|
234 end
|
tercio@0
|
235 lib.loggedIn = true
|
tercio@0
|
236 f:SetScript("OnEvent", nil)
|
tercio@0
|
237 f = nil
|
tercio@0
|
238 end)
|
tercio@0
|
239 f:RegisterEvent("PLAYER_LOGIN")
|
tercio@0
|
240 end
|
tercio@0
|
241
|
tercio@0
|
242 local function getDatabase(name)
|
tercio@0
|
243 return lib.notCreated[name] and lib.notCreated[name][2] or lib.objects[name].db
|
tercio@0
|
244 end
|
tercio@0
|
245
|
tercio@0
|
246 function lib:Register(name, object, db)
|
tercio@0
|
247 if not object.icon then error("Can't register LDB objects without icons set!") end
|
tercio@0
|
248 if lib.objects[name] or lib.notCreated[name] then error("Already registered, nubcake.") end
|
tercio@0
|
249 if not lib.disabled and (not db or not db.hide) then
|
tercio@0
|
250 createButton(name, object, db)
|
tercio@0
|
251 else
|
tercio@0
|
252 lib.notCreated[name] = {object, db}
|
tercio@0
|
253 end
|
tercio@0
|
254 end
|
tercio@0
|
255
|
tercio@0
|
256 function lib:Lock(name)
|
tercio@0
|
257 if not lib:IsRegistered(name) then return end
|
tercio@0
|
258 if lib.objects[name] then
|
tercio@0
|
259 lib.objects[name]:SetScript("OnDragStart", nil)
|
tercio@0
|
260 lib.objects[name]:SetScript("OnDragStop", nil)
|
tercio@0
|
261 end
|
tercio@0
|
262 local db = getDatabase(name)
|
tercio@0
|
263 if db then db.lock = true end
|
tercio@0
|
264 end
|
tercio@0
|
265
|
tercio@0
|
266 function lib:Unlock(name)
|
tercio@0
|
267 if not lib:IsRegistered(name) then return end
|
tercio@0
|
268 if lib.objects[name] then
|
tercio@0
|
269 lib.objects[name]:SetScript("OnDragStart", onDragStart)
|
tercio@0
|
270 lib.objects[name]:SetScript("OnDragStop", onDragStop)
|
tercio@0
|
271 end
|
tercio@0
|
272 local db = getDatabase(name)
|
tercio@0
|
273 if db then db.lock = nil end
|
tercio@0
|
274 end
|
tercio@0
|
275
|
tercio@0
|
276 function lib:Hide(name)
|
tercio@0
|
277 if not lib.objects[name] then return end
|
tercio@0
|
278 lib.objects[name]:Hide()
|
tercio@0
|
279 end
|
tercio@0
|
280 function lib:Show(name)
|
tercio@0
|
281 if lib.disabled then return end
|
tercio@0
|
282 check(name)
|
tercio@0
|
283 lib.objects[name]:Show()
|
tercio@0
|
284 updatePosition(lib.objects[name])
|
tercio@0
|
285 end
|
tercio@0
|
286 function lib:IsRegistered(name)
|
tercio@0
|
287 return (lib.objects[name] or lib.notCreated[name]) and true or false
|
tercio@0
|
288 end
|
tercio@0
|
289 function lib:Refresh(name, db)
|
tercio@0
|
290 if lib.disabled then return end
|
tercio@0
|
291 check(name)
|
tercio@0
|
292 local button = lib.objects[name]
|
tercio@0
|
293 if db then button.db = db end
|
tercio@0
|
294 updatePosition(button)
|
tercio@0
|
295 if not button.db or not button.db.hide then
|
tercio@0
|
296 button:Show()
|
tercio@0
|
297 else
|
tercio@0
|
298 button:Hide()
|
tercio@0
|
299 end
|
tercio@0
|
300 if not button.db or not button.db.lock then
|
tercio@0
|
301 button:SetScript("OnDragStart", onDragStart)
|
tercio@0
|
302 button:SetScript("OnDragStop", onDragStop)
|
tercio@0
|
303 else
|
tercio@0
|
304 button:SetScript("OnDragStart", nil)
|
tercio@0
|
305 button:SetScript("OnDragStop", nil)
|
tercio@0
|
306 end
|
tercio@0
|
307 end
|
tercio@0
|
308 function lib:GetMinimapButton(name)
|
tercio@0
|
309 return lib.objects[name]
|
tercio@0
|
310 end
|
tercio@0
|
311
|
tercio@0
|
312 function lib:EnableLibrary()
|
tercio@0
|
313 lib.disabled = nil
|
tercio@0
|
314 for name, object in pairs(lib.objects) do
|
tercio@0
|
315 if not object.db or not object.db.hide then
|
tercio@0
|
316 object:Show()
|
tercio@0
|
317 updatePosition(object)
|
tercio@0
|
318 end
|
tercio@0
|
319 end
|
tercio@0
|
320 for name, data in pairs(lib.notCreated) do
|
tercio@0
|
321 if not data.db or not data.db.hide then
|
tercio@0
|
322 createButton(name, data[1], data[2])
|
tercio@0
|
323 lib.notCreated[name] = nil
|
tercio@0
|
324 end
|
tercio@0
|
325 end
|
tercio@0
|
326 end
|
tercio@0
|
327
|
tercio@0
|
328 function lib:DisableLibrary()
|
tercio@0
|
329 lib.disabled = true
|
tercio@0
|
330 for name, object in pairs(lib.objects) do
|
tercio@0
|
331 object:Hide()
|
tercio@0
|
332 end
|
tercio@0
|
333 end
|
tercio@0
|
334
|