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