annotate Libs/LibDBIcon-1.0/LibDBIcon-1.0.lua @ 63:3552946c0b9a tip

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