annotate Libs/LibDBIcon-1.0/LibDBIcon-1.0.lua @ 23:52973d00a183

- framework update.
author Tercio
date Tue, 08 Sep 2015 13:23:31 -0300
parents
children 0682d738499b
rev   line source
Tercio@23 1 --[[
Tercio@23 2 Name: DBIcon-1.0
Tercio@23 3 Revision: $Rev: 34 $
Tercio@23 4 Author(s): Rabbit (rabbit.magtheridon@gmail.com)
Tercio@23 5 Description: Allows addons to register to recieve a lightweight minimap icon as an alternative to more heavy LDB displays.
Tercio@23 6 Dependencies: LibStub
Tercio@23 7 License: GPL v2 or later.
Tercio@23 8 ]]
Tercio@23 9
Tercio@23 10 --[[
Tercio@23 11 Copyright (C) 2008-2011 Rabbit
Tercio@23 12
Tercio@23 13 This program is free software; you can redistribute it and/or
Tercio@23 14 modify it under the terms of the GNU General Public License
Tercio@23 15 as published by the Free Software Foundation; either version 2
Tercio@23 16 of the License, or (at your option) any later version.
Tercio@23 17
Tercio@23 18 This program is distributed in the hope that it will be useful,
Tercio@23 19 but WITHOUT ANY WARRANTY; without even the implied warranty of
Tercio@23 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Tercio@23 21 GNU General Public License for more details.
Tercio@23 22
Tercio@23 23 You should have received a copy of the GNU General Public License
Tercio@23 24 along with this program; if not, write to the Free Software
Tercio@23 25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Tercio@23 26 ]]
Tercio@23 27
Tercio@23 28 -----------------------------------------------------------------------
Tercio@23 29 -- DBIcon-1.0
Tercio@23 30 --
Tercio@23 31 -- Disclaimer: Most of this code was ripped from Barrel but fixed, streamlined
Tercio@23 32 -- and cleaned up a lot so that it no longer sucks.
Tercio@23 33 --
Tercio@23 34
Tercio@23 35 local DBICON10 = "LibDBIcon-1.0"
Tercio@23 36 local DBICON10_MINOR = tonumber(("$Rev: 34 $"):match("(%d+)"))
Tercio@23 37 if not LibStub then error(DBICON10 .. " requires LibStub.") end
Tercio@23 38 local ldb = LibStub("LibDataBroker-1.1", true)
Tercio@23 39 if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end
Tercio@23 40 local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR)
Tercio@23 41 if not lib then return end
Tercio@23 42
Tercio@23 43 lib.disabled = lib.disabled or nil
Tercio@23 44 lib.objects = lib.objects or {}
Tercio@23 45 lib.callbackRegistered = lib.callbackRegistered or nil
Tercio@23 46 lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
Tercio@23 47 lib.notCreated = lib.notCreated or {}
Tercio@23 48
Tercio@23 49 function lib:IconCallback(event, name, key, value, dataobj)
Tercio@23 50 if lib.objects[name] then
Tercio@23 51 if key == "icon" then
Tercio@23 52 lib.objects[name].icon:SetTexture(value)
Tercio@23 53 elseif key == "iconCoords" then
Tercio@23 54 lib.objects[name].icon:UpdateCoord()
Tercio@23 55 elseif key == "iconR" then
Tercio@23 56 local _, g, b = lib.objects[name].icon:GetVertexColor()
Tercio@23 57 lib.objects[name].icon:SetVertexColor(value, g, b)
Tercio@23 58 elseif key == "iconG" then
Tercio@23 59 local r, _, b = lib.objects[name].icon:GetVertexColor()
Tercio@23 60 lib.objects[name].icon:SetVertexColor(r, value, b)
Tercio@23 61 elseif key == "iconB" then
Tercio@23 62 local r, g = lib.objects[name].icon:GetVertexColor()
Tercio@23 63 lib.objects[name].icon:SetVertexColor(r, g, value)
Tercio@23 64 end
Tercio@23 65 end
Tercio@23 66 end
Tercio@23 67 if not lib.callbackRegistered then
Tercio@23 68 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__icon", "IconCallback")
Tercio@23 69 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconCoords", "IconCallback")
Tercio@23 70 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconR", "IconCallback")
Tercio@23 71 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconG", "IconCallback")
Tercio@23 72 ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconB", "IconCallback")
Tercio@23 73 lib.callbackRegistered = true
Tercio@23 74 end
Tercio@23 75
Tercio@23 76 -- Tooltip code ripped from StatBlockCore by Funkydude
Tercio@23 77 local function getAnchors(frame)
Tercio@23 78 local x, y = frame:GetCenter()
Tercio@23 79 if not x or not y then return "CENTER" end
Tercio@23 80 local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
Tercio@23 81 local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
Tercio@23 82 return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
Tercio@23 83 end
Tercio@23 84
Tercio@23 85 local function onEnter(self)
Tercio@23 86 if self.isMoving then return end
Tercio@23 87 local obj = self.dataObject
Tercio@23 88 if obj.OnTooltipShow then
Tercio@23 89 GameTooltip:SetOwner(self, "ANCHOR_NONE")
Tercio@23 90 GameTooltip:SetPoint(getAnchors(self))
Tercio@23 91 obj.OnTooltipShow(GameTooltip)
Tercio@23 92 GameTooltip:Show()
Tercio@23 93 elseif obj.OnEnter then
Tercio@23 94 obj.OnEnter(self)
Tercio@23 95 end
Tercio@23 96 end
Tercio@23 97
Tercio@23 98 local function onLeave(self)
Tercio@23 99 local obj = self.dataObject
Tercio@23 100 GameTooltip:Hide()
Tercio@23 101 if obj.OnLeave then obj.OnLeave(self) end
Tercio@23 102 end
Tercio@23 103
Tercio@23 104 --------------------------------------------------------------------------------
Tercio@23 105
Tercio@23 106 local onClick, onMouseUp, onMouseDown, onDragStart, onDragStop, onDragEnd, updatePosition
Tercio@23 107
Tercio@23 108 do
Tercio@23 109 local minimapShapes = {
Tercio@23 110 ["ROUND"] = {true, true, true, true},
Tercio@23 111 ["SQUARE"] = {false, false, false, false},
Tercio@23 112 ["CORNER-TOPLEFT"] = {false, false, false, true},
Tercio@23 113 ["CORNER-TOPRIGHT"] = {false, false, true, false},
Tercio@23 114 ["CORNER-BOTTOMLEFT"] = {false, true, false, false},
Tercio@23 115 ["CORNER-BOTTOMRIGHT"] = {true, false, false, false},
Tercio@23 116 ["SIDE-LEFT"] = {false, true, false, true},
Tercio@23 117 ["SIDE-RIGHT"] = {true, false, true, false},
Tercio@23 118 ["SIDE-TOP"] = {false, false, true, true},
Tercio@23 119 ["SIDE-BOTTOM"] = {true, true, false, false},
Tercio@23 120 ["TRICORNER-TOPLEFT"] = {false, true, true, true},
Tercio@23 121 ["TRICORNER-TOPRIGHT"] = {true, false, true, true},
Tercio@23 122 ["TRICORNER-BOTTOMLEFT"] = {true, true, false, true},
Tercio@23 123 ["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false},
Tercio@23 124 }
Tercio@23 125
Tercio@23 126 function updatePosition(button)
Tercio@23 127 local angle = math.rad(button.db and button.db.minimapPos or button.minimapPos or 225)
Tercio@23 128 local x, y, q = math.cos(angle), math.sin(angle), 1
Tercio@23 129 if x < 0 then q = q + 1 end
Tercio@23 130 if y > 0 then q = q + 2 end
Tercio@23 131 local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
Tercio@23 132 local quadTable = minimapShapes[minimapShape]
Tercio@23 133 if quadTable[q] then
Tercio@23 134 x, y = x*80, y*80
Tercio@23 135 else
Tercio@23 136 local diagRadius = 103.13708498985 --math.sqrt(2*(80)^2)-10
Tercio@23 137 x = math.max(-80, math.min(x*diagRadius, 80))
Tercio@23 138 y = math.max(-80, math.min(y*diagRadius, 80))
Tercio@23 139 end
Tercio@23 140 button:SetPoint("CENTER", Minimap, "CENTER", x, y)
Tercio@23 141 end
Tercio@23 142 end
Tercio@23 143
Tercio@23 144 function onClick(self, b) if self.dataObject.OnClick then self.dataObject.OnClick(self, b) end end
Tercio@23 145 function onMouseDown(self) self.isMouseDown = true; self.icon:UpdateCoord() end
Tercio@23 146 function onMouseUp(self) self.isMouseDown = false; self.icon:UpdateCoord() end
Tercio@23 147
Tercio@23 148 do
Tercio@23 149 local function onUpdate(self)
Tercio@23 150 local mx, my = Minimap:GetCenter()
Tercio@23 151 local px, py = GetCursorPosition()
Tercio@23 152 local scale = Minimap:GetEffectiveScale()
Tercio@23 153 px, py = px / scale, py / scale
Tercio@23 154 if self.db then
Tercio@23 155 self.db.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360
Tercio@23 156 else
Tercio@23 157 self.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360
Tercio@23 158 end
Tercio@23 159 updatePosition(self)
Tercio@23 160 end
Tercio@23 161
Tercio@23 162 function onDragStart(self)
Tercio@23 163 self:LockHighlight()
Tercio@23 164 self.isMouseDown = true
Tercio@23 165 self.icon:UpdateCoord()
Tercio@23 166 self:SetScript("OnUpdate", onUpdate)
Tercio@23 167 self.isMoving = true
Tercio@23 168 GameTooltip:Hide()
Tercio@23 169 end
Tercio@23 170 end
Tercio@23 171
Tercio@23 172 function onDragStop(self)
Tercio@23 173 self:SetScript("OnUpdate", nil)
Tercio@23 174 self.isMouseDown = false
Tercio@23 175 self.icon:UpdateCoord()
Tercio@23 176 self:UnlockHighlight()
Tercio@23 177 self.isMoving = nil
Tercio@23 178 end
Tercio@23 179
Tercio@23 180 local defaultCoords = {0, 1, 0, 1}
Tercio@23 181 local function updateCoord(self)
Tercio@23 182 local coords = self:GetParent().dataObject.iconCoords or defaultCoords
Tercio@23 183 local deltaX, deltaY = 0, 0
Tercio@23 184 if not self:GetParent().isMouseDown then
Tercio@23 185 deltaX = (coords[2] - coords[1]) * 0.05
Tercio@23 186 deltaY = (coords[4] - coords[3]) * 0.05
Tercio@23 187 end
Tercio@23 188 self:SetTexCoord(coords[1] + deltaX, coords[2] - deltaX, coords[3] + deltaY, coords[4] - deltaY)
Tercio@23 189 end
Tercio@23 190
Tercio@23 191 local function createButton(name, object, db)
Tercio@23 192 local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap)
Tercio@23 193 button.dataObject = object
Tercio@23 194 button.db = db
Tercio@23 195 button:SetFrameStrata("MEDIUM")
Tercio@23 196 button:SetSize(31, 31)
Tercio@23 197 button:SetFrameLevel(8)
Tercio@23 198 button:RegisterForClicks("anyUp")
Tercio@23 199 button:RegisterForDrag("LeftButton")
Tercio@23 200 button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
Tercio@23 201 local overlay = button:CreateTexture(nil, "OVERLAY")
Tercio@23 202 overlay:SetSize(53, 53)
Tercio@23 203 overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
Tercio@23 204 overlay:SetPoint("TOPLEFT")
Tercio@23 205 local background = button:CreateTexture(nil, "BACKGROUND")
Tercio@23 206 background:SetSize(20, 20)
Tercio@23 207 background:SetTexture("Interface\\Minimap\\UI-Minimap-Background")
Tercio@23 208 background:SetPoint("TOPLEFT", 7, -5)
Tercio@23 209 local icon = button:CreateTexture(nil, "ARTWORK")
Tercio@23 210 icon:SetSize(17, 17)
Tercio@23 211 icon:SetTexture(object.icon)
Tercio@23 212 icon:SetPoint("TOPLEFT", 7, -6)
Tercio@23 213 button.icon = icon
Tercio@23 214 button.isMouseDown = false
Tercio@23 215
Tercio@23 216 local r, g, b = icon:GetVertexColor()
Tercio@23 217 icon:SetVertexColor(object.iconR or r, object.iconG or g, object.iconB or b)
Tercio@23 218
Tercio@23 219 icon.UpdateCoord = updateCoord
Tercio@23 220 icon:UpdateCoord()
Tercio@23 221
Tercio@23 222 button:SetScript("OnEnter", onEnter)
Tercio@23 223 button:SetScript("OnLeave", onLeave)
Tercio@23 224 button:SetScript("OnClick", onClick)
Tercio@23 225 if not db or not db.lock then
Tercio@23 226 button:SetScript("OnDragStart", onDragStart)
Tercio@23 227 button:SetScript("OnDragStop", onDragStop)
Tercio@23 228 end
Tercio@23 229 button:SetScript("OnMouseDown", onMouseDown)
Tercio@23 230 button:SetScript("OnMouseUp", onMouseUp)
Tercio@23 231
Tercio@23 232 lib.objects[name] = button
Tercio@23 233
Tercio@23 234 if lib.loggedIn then
Tercio@23 235 updatePosition(button)
Tercio@23 236 if not db or not db.hide then button:Show()
Tercio@23 237 else button:Hide() end
Tercio@23 238 end
Tercio@23 239 lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
Tercio@23 240 end
Tercio@23 241
Tercio@23 242 -- We could use a metatable.__index on lib.objects, but then we'd create
Tercio@23 243 -- the icons when checking things like :IsRegistered, which is not necessary.
Tercio@23 244 local function check(name)
Tercio@23 245 if lib.notCreated[name] then
Tercio@23 246 createButton(name, lib.notCreated[name][1], lib.notCreated[name][2])
Tercio@23 247 lib.notCreated[name] = nil
Tercio@23 248 end
Tercio@23 249 end
Tercio@23 250
Tercio@23 251 lib.loggedIn = lib.loggedIn or false
Tercio@23 252 -- Wait a bit with the initial positioning to let any GetMinimapShape addons
Tercio@23 253 -- load up.
Tercio@23 254 if not lib.loggedIn then
Tercio@23 255 local f = CreateFrame("Frame")
Tercio@23 256 f:SetScript("OnEvent", function()
Tercio@23 257 for _, object in pairs(lib.objects) do
Tercio@23 258 updatePosition(object)
Tercio@23 259 if not lib.disabled and (not object.db or not object.db.hide) then object:Show()
Tercio@23 260 else object:Hide() end
Tercio@23 261 end
Tercio@23 262 lib.loggedIn = true
Tercio@23 263 f:SetScript("OnEvent", nil)
Tercio@23 264 f = nil
Tercio@23 265 end)
Tercio@23 266 f:RegisterEvent("PLAYER_LOGIN")
Tercio@23 267 end
Tercio@23 268
Tercio@23 269 local function getDatabase(name)
Tercio@23 270 return lib.notCreated[name] and lib.notCreated[name][2] or lib.objects[name].db
Tercio@23 271 end
Tercio@23 272
Tercio@23 273 function lib:Register(name, object, db)
Tercio@23 274 if not object.icon then error("Can't register LDB objects without icons set!") end
Tercio@23 275 if lib.objects[name] or lib.notCreated[name] then error("Already registered, nubcake.") end
Tercio@23 276 if not lib.disabled and (not db or not db.hide) then
Tercio@23 277 createButton(name, object, db)
Tercio@23 278 else
Tercio@23 279 lib.notCreated[name] = {object, db}
Tercio@23 280 end
Tercio@23 281 end
Tercio@23 282
Tercio@23 283 function lib:Lock(name)
Tercio@23 284 if not lib:IsRegistered(name) then return end
Tercio@23 285 if lib.objects[name] then
Tercio@23 286 lib.objects[name]:SetScript("OnDragStart", nil)
Tercio@23 287 lib.objects[name]:SetScript("OnDragStop", nil)
Tercio@23 288 end
Tercio@23 289 local db = getDatabase(name)
Tercio@23 290 if db then db.lock = true end
Tercio@23 291 end
Tercio@23 292
Tercio@23 293 function lib:Unlock(name)
Tercio@23 294 if not lib:IsRegistered(name) then return end
Tercio@23 295 if lib.objects[name] then
Tercio@23 296 lib.objects[name]:SetScript("OnDragStart", onDragStart)
Tercio@23 297 lib.objects[name]:SetScript("OnDragStop", onDragStop)
Tercio@23 298 end
Tercio@23 299 local db = getDatabase(name)
Tercio@23 300 if db then db.lock = nil end
Tercio@23 301 end
Tercio@23 302
Tercio@23 303 function lib:Hide(name)
Tercio@23 304 if not lib.objects[name] then return end
Tercio@23 305 lib.objects[name]:Hide()
Tercio@23 306 end
Tercio@23 307 function lib:Show(name)
Tercio@23 308 if lib.disabled then return end
Tercio@23 309 check(name)
Tercio@23 310 lib.objects[name]:Show()
Tercio@23 311 updatePosition(lib.objects[name])
Tercio@23 312 end
Tercio@23 313 function lib:IsRegistered(name)
Tercio@23 314 return (lib.objects[name] or lib.notCreated[name]) and true or false
Tercio@23 315 end
Tercio@23 316 function lib:Refresh(name, db)
Tercio@23 317 if lib.disabled then return end
Tercio@23 318 check(name)
Tercio@23 319 local button = lib.objects[name]
Tercio@23 320 if db then button.db = db end
Tercio@23 321 updatePosition(button)
Tercio@23 322 if not button.db or not button.db.hide then
Tercio@23 323 button:Show()
Tercio@23 324 else
Tercio@23 325 button:Hide()
Tercio@23 326 end
Tercio@23 327 if not button.db or not button.db.lock then
Tercio@23 328 button:SetScript("OnDragStart", onDragStart)
Tercio@23 329 button:SetScript("OnDragStop", onDragStop)
Tercio@23 330 else
Tercio@23 331 button:SetScript("OnDragStart", nil)
Tercio@23 332 button:SetScript("OnDragStop", nil)
Tercio@23 333 end
Tercio@23 334 end
Tercio@23 335 function lib:GetMinimapButton(name)
Tercio@23 336 return lib.objects[name]
Tercio@23 337 end
Tercio@23 338
Tercio@23 339 function lib:EnableLibrary()
Tercio@23 340 lib.disabled = nil
Tercio@23 341 for name, object in pairs(lib.objects) do
Tercio@23 342 if not object.db or not object.db.hide then
Tercio@23 343 object:Show()
Tercio@23 344 updatePosition(object)
Tercio@23 345 end
Tercio@23 346 end
Tercio@23 347 for name, data in pairs(lib.notCreated) do
Tercio@23 348 if not data.db or not data.db.hide then
Tercio@23 349 createButton(name, data[1], data[2])
Tercio@23 350 lib.notCreated[name] = nil
Tercio@23 351 end
Tercio@23 352 end
Tercio@23 353 end
Tercio@23 354
Tercio@23 355 function lib:DisableLibrary()
Tercio@23 356 lib.disabled = true
Tercio@23 357 for name, object in pairs(lib.objects) do
Tercio@23 358 object:Hide()
Tercio@23 359 end
Tercio@23 360 end
Tercio@23 361