Skip to content

Commit

Permalink
feat: readd support for free drag
Browse files Browse the repository at this point in the history
  • Loading branch information
nulian committed May 6, 2024
1 parent 01d30f7 commit 99b7688
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
--@curseforge-project-slug: libdbicon-1-0@
-----------------------------------------------------------------------
-- LibDBIcon-1.0
-- LibWithFreeDragDBIcon-1.0
--
-- Allows addons to easily create a lightweight minimap icon as an alternative to heavier LDB displays.
-- Customized version of LibDBIcon to support free drag mode.
--

local DBICON10 = "LibDBIcon-1.0"
local DBICON10 = "LibWithFreeDragDBIcon-1.0"
local DBICON10_MINOR = 52 -- Bump on changes
if not LibStub then error(DBICON10 .. " requires LibStub.") end
local ldb = LibStub("LibDataBroker-1.1", true)
Expand All @@ -18,7 +18,7 @@ lib.callbackRegistered = lib.callbackRegistered or nil
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.radius = lib.radius or 5
local next, Minimap, CreateFrame, AddonCompartmentFrame = next, Minimap, CreateFrame, AddonCompartmentFrame
lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibDBIconTooltip", UIParent, "GameTooltipTemplate")
lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibWithFreeDragDBIconTooltip", UIParent, "GameTooltipTemplate")
local isDraggingButton = false

function lib:IconCallback(event, name, key, value)
Expand Down Expand Up @@ -145,23 +145,27 @@ do

local rad, cos, sin, sqrt, max, min = math.rad, math.cos, math.sin, math.sqrt, math.max, math.min
function updatePosition(button, position)
local angle = rad(position or 225)
local x, y, q = cos(angle), sin(angle), 1
if x < 0 then q = q + 1 end
if y > 0 then q = q + 2 end
local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
local quadTable = minimapShapes[minimapShape]
local w = (Minimap:GetWidth() / 2) + lib.radius
local h = (Minimap:GetHeight() / 2) + lib.radius
if quadTable[q] then
x, y = x*w, y*h
if type(position) == "number" then
local angle = rad(position or 225)
local x, y, q = cos(angle), sin(angle), 1
if x < 0 then q = q + 1 end
if y > 0 then q = q + 2 end
local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
local quadTable = minimapShapes[minimapShape]
local w = (Minimap:GetWidth() / 2) + lib.radius
local h = (Minimap:GetHeight() / 2) + lib.radius
if quadTable[q] then
x, y = x*w, y*h
else
local diagRadiusW = sqrt(2*(w)^2)-10
local diagRadiusH = sqrt(2*(h)^2)-10
x = max(-w, min(x*diagRadiusW, w))
y = max(-h, min(y*diagRadiusH, h))
end
button:SetPoint("CENTER", Minimap, "CENTER", x, y)
else
local diagRadiusW = sqrt(2*(w)^2)-10
local diagRadiusH = sqrt(2*(h)^2)-10
x = max(-w, min(x*diagRadiusW, w))
y = max(-h, min(y*diagRadiusH, h))
button:SetPoint("CENTER", Minimap, "CENTER", position['x'], position['y'])
end
button:SetPoint("CENTER", Minimap, "CENTER", x, y)
end
end

Expand All @@ -173,6 +177,17 @@ end

local function onMouseDown(self)
self.isMouseDown = true
self.enableFreeDrag = IsModifierKeyDown()
local px, py = GetCursorPosition()
local scale = Minimap:GetEffectiveScale()
px, py = px / scale, py / scale
self.cursorStartX = px
self.cursorStartY = py
local vCenterX, vCenterY = self:GetCenter()
local vMinimapCenterX, vMinimapCenterY = Minimap:GetCenter()

self.centerStartX = vCenterX - vMinimapCenterX
self.centerStartY = vCenterY - vMinimapCenterY
self.icon:UpdateCoord()
end

Expand All @@ -184,19 +199,37 @@ end
do
local deg, atan2 = math.deg, math.atan2
local function onUpdate(self)
local mx, my = Minimap:GetCenter()
local px, py = GetCursorPosition()
local scale = Minimap:GetEffectiveScale()
px, py = px / scale, py / scale
local pos = 225
if self.db then
pos = deg(atan2(py - my, px - mx)) % 360
self.db.minimapPos = pos
if self.enableFreeDrag then
local scale = Minimap:GetEffectiveScale()
px, py = px / scale, py / scale
local vCursorDeltaX = px - self.cursorStartX
local vCursorDeltaY = py - self.cursorStartY


local vCenterX = self.centerStartX + vCursorDeltaX
local vCenterY = self.centerStartY + vCursorDeltaY
local coords = { x = vCenterX, y = vCenterY }
if self.db then
self.db.minimapPos = coords
else
self.minimapPos = coords
end
updatePosition(self, coords)
else
pos = deg(atan2(py - my, px - mx)) % 360
self.minimapPos = pos
local mx, my = Minimap:GetCenter()
local scale = Minimap:GetEffectiveScale()
px, py = px / scale, py / scale
local pos = 225
if self.db then
pos = deg(atan2(py - my, px - mx)) % 360
self.db.minimapPos = pos
else
pos = deg(atan2(py - my, px - mx)) % 360
self.minimapPos = pos
end
updatePosition(self, pos)
end
updatePosition(self, pos)
end

function onDragStart(self)
Expand Down Expand Up @@ -240,7 +273,7 @@ local function updateCoord(self)
end

local function createButton(name, object, db, customCompartmentIcon)
local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap)
local button = CreateFrame("Button", "LibWithFreeDragDBIcon10_"..name, Minimap)
button.dataObject = object
button.db = db
button:SetFrameStrata("MEDIUM")
Expand Down Expand Up @@ -321,7 +354,7 @@ local function createButton(name, object, db, customCompartmentIcon)
if db and db.showInCompartment then
lib:AddButtonToCompartment(name, customCompartmentIcon)
end
lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
lib.callbacks:Fire("LibWithFreeDragDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
end

-- Wait a bit with the initial positioning to let any GetMinimapShape addons
Expand Down
4 changes: 2 additions & 2 deletions Outfitter.toc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Interface: 100206
## Author: John Stephen
## Title: Outfitter
## Version: 10.2.6.2
## Version: 10.2.6.3
## Notes: Clothing and weapon management and automated equipment changes
## OptionalDeps:
## RequiredDeps:
Expand All @@ -19,7 +19,7 @@ Libraries/UTF8/utf8.lua
Libraries/LibStub.lua
Libraries/CallbackHandler-1.0.lua
Libraries/LibDataBroker-1.1.lua
Libraries/LibDBIcon-1.0.lua
Libraries/LibWithFreeDragDBIcon-1.0.lua
Libraries/LibBabble-3.0.lua
Libraries/LibBabble-SubZone-3.0.lua
Libraries/LibBabble-Inventory-3.0.lua
Expand Down
2 changes: 1 addition & 1 deletion OutfitterLDB.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Outfitter.LDB:Initialize()
end

function Outfitter.LDB:CreateIcon(minimapButton)
self.icon = LibStub("LibDBIcon-1.0")
self.icon = LibStub("LibWithFreeDragDBIcon-1.0")

self.icon:Register(Outfitter.cTitle, self.DataObj, minimapButton)
end
Expand Down

0 comments on commit 99b7688

Please sign in to comment.