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