Tercio@23: --[[ Tercio@23: Name: DBIcon-1.0 Tercio@23: Revision: $Rev: 34 $ Tercio@23: Author(s): Rabbit (rabbit.magtheridon@gmail.com) Tercio@23: Description: Allows addons to register to recieve a lightweight minimap icon as an alternative to more heavy LDB displays. Tercio@23: Dependencies: LibStub Tercio@23: License: GPL v2 or later. Tercio@23: ]] Tercio@23: Tercio@23: --[[ Tercio@23: Copyright (C) 2008-2011 Rabbit Tercio@23: Tercio@23: This program is free software; you can redistribute it and/or Tercio@23: modify it under the terms of the GNU General Public License Tercio@23: as published by the Free Software Foundation; either version 2 Tercio@23: of the License, or (at your option) any later version. Tercio@23: Tercio@23: This program is distributed in the hope that it will be useful, Tercio@23: but WITHOUT ANY WARRANTY; without even the implied warranty of Tercio@23: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Tercio@23: GNU General Public License for more details. Tercio@23: Tercio@23: You should have received a copy of the GNU General Public License Tercio@23: along with this program; if not, write to the Free Software Tercio@23: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Tercio@23: ]] Tercio@23: Tercio@23: ----------------------------------------------------------------------- Tercio@23: -- DBIcon-1.0 Tercio@23: -- Tercio@23: -- Disclaimer: Most of this code was ripped from Barrel but fixed, streamlined Tercio@23: -- and cleaned up a lot so that it no longer sucks. Tercio@23: -- Tercio@23: Tercio@23: local DBICON10 = "LibDBIcon-1.0" Tercio@23: local DBICON10_MINOR = tonumber(("$Rev: 34 $"):match("(%d+)")) Tercio@23: if not LibStub then error(DBICON10 .. " requires LibStub.") end Tercio@23: local ldb = LibStub("LibDataBroker-1.1", true) Tercio@23: if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end Tercio@23: local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR) Tercio@23: if not lib then return end Tercio@23: Tercio@23: lib.disabled = lib.disabled or nil Tercio@23: lib.objects = lib.objects or {} Tercio@23: lib.callbackRegistered = lib.callbackRegistered or nil Tercio@23: lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib) Tercio@23: lib.notCreated = lib.notCreated or {} Tercio@23: Tercio@23: function lib:IconCallback(event, name, key, value, dataobj) Tercio@23: if lib.objects[name] then Tercio@23: if key == "icon" then Tercio@23: lib.objects[name].icon:SetTexture(value) Tercio@23: elseif key == "iconCoords" then Tercio@23: lib.objects[name].icon:UpdateCoord() Tercio@23: elseif key == "iconR" then Tercio@23: local _, g, b = lib.objects[name].icon:GetVertexColor() Tercio@23: lib.objects[name].icon:SetVertexColor(value, g, b) Tercio@23: elseif key == "iconG" then Tercio@23: local r, _, b = lib.objects[name].icon:GetVertexColor() Tercio@23: lib.objects[name].icon:SetVertexColor(r, value, b) Tercio@23: elseif key == "iconB" then Tercio@23: local r, g = lib.objects[name].icon:GetVertexColor() Tercio@23: lib.objects[name].icon:SetVertexColor(r, g, value) Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: if not lib.callbackRegistered then Tercio@23: ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__icon", "IconCallback") Tercio@23: ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconCoords", "IconCallback") Tercio@23: ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconR", "IconCallback") Tercio@23: ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconG", "IconCallback") Tercio@23: ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconB", "IconCallback") Tercio@23: lib.callbackRegistered = true Tercio@23: end Tercio@23: Tercio@23: -- Tooltip code ripped from StatBlockCore by Funkydude Tercio@23: local function getAnchors(frame) Tercio@23: local x, y = frame:GetCenter() Tercio@23: if not x or not y then return "CENTER" end Tercio@23: local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or "" Tercio@23: local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM" Tercio@23: return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf Tercio@23: end Tercio@23: Tercio@23: local function onEnter(self) Tercio@23: if self.isMoving then return end Tercio@23: local obj = self.dataObject Tercio@23: if obj.OnTooltipShow then Tercio@23: GameTooltip:SetOwner(self, "ANCHOR_NONE") Tercio@23: GameTooltip:SetPoint(getAnchors(self)) Tercio@23: obj.OnTooltipShow(GameTooltip) Tercio@23: GameTooltip:Show() Tercio@23: elseif obj.OnEnter then Tercio@23: obj.OnEnter(self) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: local function onLeave(self) Tercio@23: local obj = self.dataObject Tercio@23: GameTooltip:Hide() Tercio@23: if obj.OnLeave then obj.OnLeave(self) end Tercio@23: end Tercio@23: Tercio@23: -------------------------------------------------------------------------------- Tercio@23: Tercio@23: local onClick, onMouseUp, onMouseDown, onDragStart, onDragStop, onDragEnd, updatePosition Tercio@23: Tercio@23: do Tercio@23: local minimapShapes = { Tercio@23: ["ROUND"] = {true, true, true, true}, Tercio@23: ["SQUARE"] = {false, false, false, false}, Tercio@23: ["CORNER-TOPLEFT"] = {false, false, false, true}, Tercio@23: ["CORNER-TOPRIGHT"] = {false, false, true, false}, Tercio@23: ["CORNER-BOTTOMLEFT"] = {false, true, false, false}, Tercio@23: ["CORNER-BOTTOMRIGHT"] = {true, false, false, false}, Tercio@23: ["SIDE-LEFT"] = {false, true, false, true}, Tercio@23: ["SIDE-RIGHT"] = {true, false, true, false}, Tercio@23: ["SIDE-TOP"] = {false, false, true, true}, Tercio@23: ["SIDE-BOTTOM"] = {true, true, false, false}, Tercio@23: ["TRICORNER-TOPLEFT"] = {false, true, true, true}, Tercio@23: ["TRICORNER-TOPRIGHT"] = {true, false, true, true}, Tercio@23: ["TRICORNER-BOTTOMLEFT"] = {true, true, false, true}, Tercio@23: ["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false}, Tercio@23: } Tercio@23: Tercio@23: function updatePosition(button) Tercio@23: local angle = math.rad(button.db and button.db.minimapPos or button.minimapPos or 225) Tercio@23: local x, y, q = math.cos(angle), math.sin(angle), 1 Tercio@23: if x < 0 then q = q + 1 end Tercio@23: if y > 0 then q = q + 2 end Tercio@23: local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND" Tercio@23: local quadTable = minimapShapes[minimapShape] Tercio@23: if quadTable[q] then Tercio@23: x, y = x*80, y*80 Tercio@23: else Tercio@23: local diagRadius = 103.13708498985 --math.sqrt(2*(80)^2)-10 Tercio@23: x = math.max(-80, math.min(x*diagRadius, 80)) Tercio@23: y = math.max(-80, math.min(y*diagRadius, 80)) Tercio@23: end Tercio@23: button:SetPoint("CENTER", Minimap, "CENTER", x, y) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: function onClick(self, b) if self.dataObject.OnClick then self.dataObject.OnClick(self, b) end end Tercio@23: function onMouseDown(self) self.isMouseDown = true; self.icon:UpdateCoord() end Tercio@23: function onMouseUp(self) self.isMouseDown = false; self.icon:UpdateCoord() end Tercio@23: Tercio@23: do Tercio@23: local function onUpdate(self) Tercio@23: local mx, my = Minimap:GetCenter() Tercio@23: local px, py = GetCursorPosition() Tercio@23: local scale = Minimap:GetEffectiveScale() Tercio@23: px, py = px / scale, py / scale Tercio@23: if self.db then Tercio@23: self.db.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360 Tercio@23: else Tercio@23: self.minimapPos = math.deg(math.atan2(py - my, px - mx)) % 360 Tercio@23: end Tercio@23: updatePosition(self) Tercio@23: end Tercio@23: Tercio@23: function onDragStart(self) Tercio@23: self:LockHighlight() Tercio@23: self.isMouseDown = true Tercio@23: self.icon:UpdateCoord() Tercio@23: self:SetScript("OnUpdate", onUpdate) Tercio@23: self.isMoving = true Tercio@23: GameTooltip:Hide() Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: function onDragStop(self) Tercio@23: self:SetScript("OnUpdate", nil) Tercio@23: self.isMouseDown = false Tercio@23: self.icon:UpdateCoord() Tercio@23: self:UnlockHighlight() Tercio@23: self.isMoving = nil Tercio@23: end Tercio@23: Tercio@23: local defaultCoords = {0, 1, 0, 1} Tercio@23: local function updateCoord(self) Tercio@23: local coords = self:GetParent().dataObject.iconCoords or defaultCoords Tercio@23: local deltaX, deltaY = 0, 0 Tercio@23: if not self:GetParent().isMouseDown then Tercio@23: deltaX = (coords[2] - coords[1]) * 0.05 Tercio@23: deltaY = (coords[4] - coords[3]) * 0.05 Tercio@23: end Tercio@23: self:SetTexCoord(coords[1] + deltaX, coords[2] - deltaX, coords[3] + deltaY, coords[4] - deltaY) Tercio@23: end Tercio@23: Tercio@23: local function createButton(name, object, db) Tercio@23: local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap) Tercio@23: button.dataObject = object Tercio@23: button.db = db Tercio@23: button:SetFrameStrata("MEDIUM") Tercio@23: button:SetSize(31, 31) Tercio@23: button:SetFrameLevel(8) Tercio@23: button:RegisterForClicks("anyUp") Tercio@23: button:RegisterForDrag("LeftButton") Tercio@23: button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight") Tercio@23: local overlay = button:CreateTexture(nil, "OVERLAY") Tercio@23: overlay:SetSize(53, 53) Tercio@23: overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder") Tercio@23: overlay:SetPoint("TOPLEFT") Tercio@23: local background = button:CreateTexture(nil, "BACKGROUND") Tercio@23: background:SetSize(20, 20) Tercio@23: background:SetTexture("Interface\\Minimap\\UI-Minimap-Background") Tercio@23: background:SetPoint("TOPLEFT", 7, -5) Tercio@23: local icon = button:CreateTexture(nil, "ARTWORK") Tercio@23: icon:SetSize(17, 17) Tercio@23: icon:SetTexture(object.icon) Tercio@23: icon:SetPoint("TOPLEFT", 7, -6) Tercio@23: button.icon = icon Tercio@23: button.isMouseDown = false Tercio@23: Tercio@23: local r, g, b = icon:GetVertexColor() Tercio@23: icon:SetVertexColor(object.iconR or r, object.iconG or g, object.iconB or b) Tercio@23: Tercio@23: icon.UpdateCoord = updateCoord Tercio@23: icon:UpdateCoord() Tercio@23: Tercio@23: button:SetScript("OnEnter", onEnter) Tercio@23: button:SetScript("OnLeave", onLeave) Tercio@23: button:SetScript("OnClick", onClick) Tercio@23: if not db or not db.lock then Tercio@23: button:SetScript("OnDragStart", onDragStart) Tercio@23: button:SetScript("OnDragStop", onDragStop) Tercio@23: end Tercio@23: button:SetScript("OnMouseDown", onMouseDown) Tercio@23: button:SetScript("OnMouseUp", onMouseUp) Tercio@23: Tercio@23: lib.objects[name] = button Tercio@23: Tercio@23: if lib.loggedIn then Tercio@23: updatePosition(button) Tercio@23: if not db or not db.hide then button:Show() Tercio@23: else button:Hide() end Tercio@23: end Tercio@23: lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback Tercio@23: end Tercio@23: Tercio@23: -- We could use a metatable.__index on lib.objects, but then we'd create Tercio@23: -- the icons when checking things like :IsRegistered, which is not necessary. Tercio@23: local function check(name) Tercio@23: if lib.notCreated[name] then Tercio@23: createButton(name, lib.notCreated[name][1], lib.notCreated[name][2]) Tercio@23: lib.notCreated[name] = nil Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: lib.loggedIn = lib.loggedIn or false Tercio@23: -- Wait a bit with the initial positioning to let any GetMinimapShape addons Tercio@23: -- load up. Tercio@23: if not lib.loggedIn then Tercio@23: local f = CreateFrame("Frame") Tercio@23: f:SetScript("OnEvent", function() Tercio@23: for _, object in pairs(lib.objects) do Tercio@23: updatePosition(object) Tercio@23: if not lib.disabled and (not object.db or not object.db.hide) then object:Show() Tercio@23: else object:Hide() end Tercio@23: end Tercio@23: lib.loggedIn = true Tercio@23: f:SetScript("OnEvent", nil) Tercio@23: f = nil Tercio@23: end) Tercio@23: f:RegisterEvent("PLAYER_LOGIN") Tercio@23: end Tercio@23: Tercio@23: local function getDatabase(name) Tercio@23: return lib.notCreated[name] and lib.notCreated[name][2] or lib.objects[name].db Tercio@23: end Tercio@23: Tercio@23: function lib:Register(name, object, db) Tercio@23: if not object.icon then error("Can't register LDB objects without icons set!") end Tercio@23: if lib.objects[name] or lib.notCreated[name] then error("Already registered, nubcake.") end Tercio@23: if not lib.disabled and (not db or not db.hide) then Tercio@23: createButton(name, object, db) Tercio@23: else Tercio@23: lib.notCreated[name] = {object, db} Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: function lib:Lock(name) Tercio@23: if not lib:IsRegistered(name) then return end Tercio@23: if lib.objects[name] then Tercio@23: lib.objects[name]:SetScript("OnDragStart", nil) Tercio@23: lib.objects[name]:SetScript("OnDragStop", nil) Tercio@23: end Tercio@23: local db = getDatabase(name) Tercio@23: if db then db.lock = true end Tercio@23: end Tercio@23: Tercio@23: function lib:Unlock(name) Tercio@23: if not lib:IsRegistered(name) then return end Tercio@23: if lib.objects[name] then Tercio@23: lib.objects[name]:SetScript("OnDragStart", onDragStart) Tercio@23: lib.objects[name]:SetScript("OnDragStop", onDragStop) Tercio@23: end Tercio@23: local db = getDatabase(name) Tercio@23: if db then db.lock = nil end Tercio@23: end Tercio@23: Tercio@23: function lib:Hide(name) Tercio@23: if not lib.objects[name] then return end Tercio@23: lib.objects[name]:Hide() Tercio@23: end Tercio@23: function lib:Show(name) Tercio@23: if lib.disabled then return end Tercio@23: check(name) Tercio@23: lib.objects[name]:Show() Tercio@23: updatePosition(lib.objects[name]) Tercio@23: end Tercio@23: function lib:IsRegistered(name) Tercio@23: return (lib.objects[name] or lib.notCreated[name]) and true or false Tercio@23: end Tercio@23: function lib:Refresh(name, db) Tercio@23: if lib.disabled then return end Tercio@23: check(name) Tercio@23: local button = lib.objects[name] Tercio@23: if db then button.db = db end Tercio@23: updatePosition(button) Tercio@23: if not button.db or not button.db.hide then Tercio@23: button:Show() Tercio@23: else Tercio@23: button:Hide() Tercio@23: end Tercio@23: if not button.db or not button.db.lock then Tercio@23: button:SetScript("OnDragStart", onDragStart) Tercio@23: button:SetScript("OnDragStop", onDragStop) Tercio@23: else Tercio@23: button:SetScript("OnDragStart", nil) Tercio@23: button:SetScript("OnDragStop", nil) Tercio@23: end Tercio@23: end Tercio@23: function lib:GetMinimapButton(name) Tercio@23: return lib.objects[name] Tercio@23: end Tercio@23: Tercio@23: function lib:EnableLibrary() Tercio@23: lib.disabled = nil Tercio@23: for name, object in pairs(lib.objects) do Tercio@23: if not object.db or not object.db.hide then Tercio@23: object:Show() Tercio@23: updatePosition(object) Tercio@23: end Tercio@23: end Tercio@23: for name, data in pairs(lib.notCreated) do Tercio@23: if not data.db or not data.db.hide then Tercio@23: createButton(name, data[1], data[2]) Tercio@23: lib.notCreated[name] = nil Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: function lib:DisableLibrary() Tercio@23: lib.disabled = true Tercio@23: for name, object in pairs(lib.objects) do Tercio@23: object:Hide() Tercio@23: end Tercio@23: end Tercio@23: