flickerstreak@18: --[[ flickerstreak@18: Name: ReAnchor-1.0 flickerstreak@18: Revision: $Rev: 1 $ flickerstreak@18: Author: Flick flickerstreak@18: Website: flickerstreak@18: Documentation: flickerstreak@18: SVN: flickerstreak@18: Description: Provides drag-placement facilities for frames flickerstreak@18: License: MIT flickerstreak@18: Dependencies: AceLibrary, AceOO-2.0 flickerstreak@18: ]] flickerstreak@18: flickerstreak@18: flickerstreak@18: local version_major, version_minor = "ReAnchor-1.0", "$Rev: 1 $" flickerstreak@18: flickerstreak@18: if not AceLibrary then error(version_major .. " requires AceLibrary.") end flickerstreak@18: if not AceLibrary:IsNewVersion(version_major, version_minor) then return end flickerstreak@18: if not AceLibrary:HasInstance("AceOO-2.0") then error(version_major .. " requires AceOO-2.0") end flickerstreak@18: flickerstreak@18: -- local constants flickerstreak@18: local AceOO = AceLibrary("AceOO-2.0") flickerstreak@18: flickerstreak@18: local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" } flickerstreak@18: flickerstreak@18: local pointsOnEdge = { flickerstreak@18: BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", }, flickerstreak@18: TOP = { "TOP", "TOPLEFT", "TOPRIGHT", }, flickerstreak@18: RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", }, flickerstreak@18: LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", }, flickerstreak@18: } flickerstreak@18: flickerstreak@18: local edgeSelector = { flickerstreak@18: BOTTOM = 1, -- select x of x,y flickerstreak@18: TOP = 1, -- select x of x,y flickerstreak@18: LEFT = 2, -- select y of x,y flickerstreak@18: RIGHT = 2, -- select y of x,y flickerstreak@18: } flickerstreak@18: flickerstreak@18: local oppositePoints = { flickerstreak@18: BOTTOMLEFT = "TOPRIGHT", flickerstreak@18: BOTTOM = "TOP", flickerstreak@18: BOTTOMRIGHT = "TOPLEFT", flickerstreak@18: RIGHT = "LEFT", flickerstreak@18: TOPRIGHT = "BOTTOMLEFT", flickerstreak@18: TOP = "BOTTOM", flickerstreak@18: TOPLEFT = "BOTTOMRIGHT", flickerstreak@18: LEFT = "RIGHT", flickerstreak@18: CENTER = "CENTER", flickerstreak@18: } flickerstreak@18: flickerstreak@18: local insidePointOffsetFuncs = { flickerstreak@18: BOTTOMLEFT = function(x, y) return x, y end, flickerstreak@18: BOTTOM = function(x, y) return 0, y end, flickerstreak@18: BOTTOMRIGHT = function(x, y) return -x, y end, flickerstreak@18: RIGHT = function(x, y) return -x, 0 end, flickerstreak@18: TOPRIGHT = function(x, y) return -x, -y end, flickerstreak@18: TOP = function(x, y) return 0, -y end, flickerstreak@18: TOPLEFT = function(x, y) return x, -y end, flickerstreak@18: LEFT = function(x, y) return x, 0 end, flickerstreak@18: CENTER = function(x, y) return 0, 0 end, flickerstreak@18: } flickerstreak@18: flickerstreak@18: local pointCoordFuncs = { flickerstreak@18: BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end, flickerstreak@18: BOTTOM = function(f) return nil, f:GetBottom() end, flickerstreak@18: BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end, flickerstreak@18: RIGHT = function(f) return f:GetRight(), nil end, flickerstreak@18: TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end, flickerstreak@18: TOP = function(f) return nil, f:GetTop() end, flickerstreak@18: TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end, flickerstreak@18: LEFT = function(f) return f:GetLeft(), nil end, flickerstreak@18: CENTER = function(f) return f:GetCenter() end, flickerstreak@18: } flickerstreak@18: flickerstreak@18: local edgeBoundsFuncs = { flickerstreak@18: BOTTOM = function(f) return f:GetLeft(), f:GetRight() end, flickerstreak@18: LEFT = function(f) return f:GetBottom(), f:GetTop() end flickerstreak@18: } flickerstreak@18: edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM flickerstreak@18: edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT flickerstreak@18: flickerstreak@18: flickerstreak@18: -- local utility functions flickerstreak@18: flickerstreak@18: -- Returns absolute coordinates x,y of the named point 'p' of frame 'f' flickerstreak@18: local function GetPointCoords( f, p ) flickerstreak@18: local x, y = pointCoordFuncs[p](f) flickerstreak@18: if not(x and y) then flickerstreak@18: local cx, cy = f:GetCenter() flickerstreak@18: x = x or cx flickerstreak@18: y = y or cy flickerstreak@18: end flickerstreak@18: return x, y flickerstreak@18: end flickerstreak@18: flickerstreak@18: flickerstreak@18: -- Returns true if frame 'f1' can be anchored to frame 'f2' flickerstreak@18: local function CheckAnchorable( f1, f2 ) flickerstreak@18: -- can't anchor a frame to itself or to nil flickerstreak@18: if f1 == f2 or f2 == nil then flickerstreak@18: return false flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- can always anchor to UIParent flickerstreak@18: if f2 == UIParent then flickerstreak@18: return true flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- also can't do circular anchoring of frames flickerstreak@18: -- walk the anchor chain, which generally shouldn't be that expensive flickerstreak@18: -- (who nests draggables that deep anyway?) flickerstreak@18: for i = 1, f2:GetNumPoints() do flickerstreak@18: local _, f = f2:GetPoint(i) flickerstreak@18: return CheckAnchorable(f1,f) flickerstreak@18: end flickerstreak@18: flickerstreak@18: return true flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- Returns true if frames f1 and f2 specified edges overlap flickerstreak@18: local function CheckEdgeOverlap( f1, f2, e ) flickerstreak@18: local l1, u1 = edgeBoundsFuncs[e](f1) flickerstreak@18: local l2, u2 = edgeBoundsFuncs[e](f2) flickerstreak@18: return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2 flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2 flickerstreak@18: local function CheckPointEdgeOverlap( f1, p1, f2, e2 ) flickerstreak@18: local l, u = edgeBoundsFuncs[e2](f2) flickerstreak@18: local x, y = GetPointCoords(f1,p1) flickerstreak@18: x = select(edgeSelector[e2], x, y) flickerstreak@18: return l <= x and x <= u flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- Returns the distance between corresponding edges. It is flickerstreak@18: -- assumed that the passed in edges e1 and e2 are the same or opposites flickerstreak@18: local function GetEdgeDistance( f1, f2, e1, e2 ) flickerstreak@18: local x1, y1 = pointCoordFuncs[e1](f1) flickerstreak@18: local x2, y2 = pointCoordFuncs[e2](f2) flickerstreak@18: return math.abs((x1 or y1) - (x2 or y2)) flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- Returns interior offsets (specified absolutely) from a point flickerstreak@18: local function GetInteriorOffsetsToPoint(p, x, y) flickerstreak@18: return insidePointOffsetFuncs[p](x,y) flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- creates a snap indicator square flickerstreak@18: local function createSnapIndicator() flickerstreak@18: local f = CreateFrame("Frame",nil,UIParent) flickerstreak@18: f:SetFrameStrata("HIGH") flickerstreak@18: local t = f:CreateTexture(nil,"OVERLAY") flickerstreak@18: t:SetBlendMode("ADD") flickerstreak@18: t:SetTexture(1,0.82,0,0.8) flickerstreak@18: t:SetPoint("CENTER") flickerstreak@18: f:SetWidth(8) flickerstreak@18: f:SetHeight(8) flickerstreak@18: f:Show() flickerstreak@18: return f flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- local objects flickerstreak@18: local snapIndicator1 flickerstreak@18: local snapIndicator2 flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: -- ReAnchor is a Mixin which provides some anchoring and flickerstreak@18: -- placement methods for frames. flickerstreak@18: -- An object with the ReAnchor mixin must support the flickerstreak@18: -- IAnchorable interface (implicitly or explicitly). flickerstreak@18: -- The mixin methods also require arguments to support flickerstreak@18: -- that interface. flickerstreak@18: flickerstreak@18: -- In the method prototypes, 'IRObjs' is used to refer to a flickerstreak@18: -- table of objects which support the IAnchorable interface. flickerstreak@18: flickerstreak@18: flickerstreak@18: local ReAnchor = AceOO.Mixin { flickerstreak@18: "GetClosestVisibleEdge", flickerstreak@18: "GetClosestVisiblePoint", flickerstreak@18: "GetClosestPointSnapped", flickerstreak@18: "DisplaySnapIndicator", flickerstreak@18: "HideSnapIndicator", flickerstreak@18: } flickerstreak@18: flickerstreak@18: --------------------------------------------------------- flickerstreak@18: -- Constants and classes that are not exported via mixin flickerstreak@18: --------------------------------------------------------- flickerstreak@18: ReAnchor.IAnchorable = AceOO.Interface { flickerstreak@18: GetFrame = "function", flickerstreak@18: GetAnchorage = "function", -- return ReAnchor.anchorInside or .anchorOutside flickerstreak@18: } flickerstreak@18: flickerstreak@18: ReAnchor.anchorInside = { inside = true } flickerstreak@18: ReAnchor.anchorOutside = { outside = true } flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: -------------------- flickerstreak@18: -- Mixin methods flickerstreak@18: -------------------- flickerstreak@18: flickerstreak@18: -- returns: flickerstreak@18: -- (1) o : the closest IRObj flickerstreak@18: -- (2) e1 : the point (edge) on self:GetFrame() flickerstreak@18: -- (3) e2 : the point (edge) on o:GetFrame() flickerstreak@18: function ReAnchor:GetClosestVisibleEdge( IRObjs ) flickerstreak@18: local f1 = self:GetFrame() flickerstreak@18: local r, o, e1, e2 flickerstreak@18: for _, o2 in pairs(IRObjs) do flickerstreak@18: local f2 = o2:GetFrame() flickerstreak@18: local a = o2:GetAnchorage() flickerstreak@18: if f2:IsVisible() and CheckAnchorable(f1,f2) then flickerstreak@18: for _, e in pairs(edges) do flickerstreak@18: local opp = a.inside and e or oppositePoints[e] flickerstreak@18: if CheckEdgeOverlap(f1,f2,e) then flickerstreak@18: local d = GetEdgeDistance(f1, f2, e, opp) flickerstreak@18: if not r or d < r then flickerstreak@18: r, o, e1, e2 = d, o2, e, opp flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: return o, e1, e2 flickerstreak@18: end flickerstreak@18: flickerstreak@18: -- returns: flickerstreak@18: -- (1) o: the closest IRObj flickerstreak@18: -- (1) p: the point on self:GetFrame() flickerstreak@18: -- (2) rp: the relativePoint on o:GetFrame() flickerstreak@18: -- (3) x: x offset flickerstreak@18: -- (4) y: y offset flickerstreak@18: -- such that self:GetFrame():SetPoint(p,o:GetFrame(),rp,x,y) preserves the current location flickerstreak@18: function ReAnchor:GetClosestVisiblePoint( IRObjs ) flickerstreak@18: local f1 = self:GetFrame() flickerstreak@18: local o, e1, e2 = self:GetClosestVisibleEdge( IRObjs ) flickerstreak@18: local f2 = o:GetFrame() flickerstreak@18: local rsq, p, rp, x, y flickerstreak@18: -- iterate pointsOnEdge in order and use < to prefer edge centers to corners flickerstreak@18: for _, p1 in ipairs(pointsOnEdge[e1]) do flickerstreak@18: if CheckPointEdgeOverlap(f1,p1,f2,e2) then flickerstreak@18: local p2 = o:GetAnchorage().outside and oppositePoints[p1] or p1 flickerstreak@18: local x1, y1 = GetPointCoords(f1,p1) flickerstreak@18: local x2, y2 = GetPointCoords(f2,p2) flickerstreak@18: local dx = x1 - x2 flickerstreak@18: local dy = y1 - y2 flickerstreak@18: local rsq2 = dx*dx + dy*dy flickerstreak@18: if not rsq or rsq2 < rsq then flickerstreak@18: rsq, p, rp, x, y = rsq2, p1, p2, dx, dy flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: return o, p, rp, x, y flickerstreak@18: end flickerstreak@18: flickerstreak@18: flickerstreak@18: -- Calls self:GetClosestVisiblePoint() and then snaps to the specified flickerstreak@18: -- offsets if within the given range in x and y (as appropriate) flickerstreak@18: -- Return semantic is the same as GetClosestVisiblePoint(). Returns nil flickerstreak@18: -- if no snap can be done. flickerstreak@18: function ReAnchor:GetClosestPointSnapped(IRObjs, r, xOff, yOff) flickerstreak@18: local f1 = self:GetFrame() flickerstreak@18: local o, p, rp, x, y = self:GetClosestVisiblePoint(IRObjs) flickerstreak@18: local s = false flickerstreak@18: flickerstreak@18: if r then flickerstreak@18: local sx, sy = GetInteriorOffsetsToPoint(p, xOff or 0, yOff or 0) flickerstreak@18: local xx, yy = pointCoordFuncs[p](f1) flickerstreak@18: if xx and yy then flickerstreak@18: if math.abs(x) <= r then flickerstreak@18: x = sx flickerstreak@18: s = true flickerstreak@18: end flickerstreak@18: if math.abs(y) <= r then flickerstreak@18: y = sy flickerstreak@18: s = true flickerstreak@18: end flickerstreak@18: elseif xx then flickerstreak@18: if math.abs(x) <= r then flickerstreak@18: x = sx flickerstreak@18: s = true flickerstreak@18: if math.abs(y) <= r then flickerstreak@18: y = sy flickerstreak@18: end flickerstreak@18: end flickerstreak@18: elseif yy then flickerstreak@18: if math.abs(y) <= r then flickerstreak@18: y = sy flickerstreak@18: s = true flickerstreak@18: if math.abs(x) <= r then flickerstreak@18: x = sx flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: flickerstreak@18: if s then flickerstreak@18: return o, p, rp, x, y flickerstreak@18: end flickerstreak@18: end flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: -- shows anchor-indicators on the associated frame and the target frame flickerstreak@18: -- when a snap is warranted. flickerstreak@18: function ReAnchor:DisplaySnapIndicator( IRObjs, r, xOff, yOff ) flickerstreak@18: local o, p, rp, x, y, snap = self:GetClosestPointSnapped(IRObjs, r, xOff, yOff) flickerstreak@18: local si1 = snapIndicator1 flickerstreak@18: local si2 = snapIndicator2 flickerstreak@18: if o then flickerstreak@18: si1:ClearAllPoints() flickerstreak@18: si2:ClearAllPoints() flickerstreak@18: si1:SetPoint("CENTER", self:GetFrame(), p, 0, 0) flickerstreak@18: local xx, yy = pointCoordFuncs[rp](o:GetFrame()) flickerstreak@18: x = math.abs(x) <=r and xx and 0 or x flickerstreak@18: y = math.abs(y) <=r and yy and 0 or y flickerstreak@18: si2:SetPoint("CENTER", o:GetFrame(), rp, x, y) flickerstreak@18: si1:Show() flickerstreak@18: si2:Show() flickerstreak@18: else flickerstreak@18: if si1:IsVisible() then flickerstreak@18: si1:Hide() flickerstreak@18: si2:Hide() flickerstreak@18: end flickerstreak@18: end flickerstreak@18: end flickerstreak@18: flickerstreak@18: flickerstreak@18: function ReAnchor:HideSnapIndicator() flickerstreak@18: if snapIndicator1:IsVisible() then flickerstreak@18: snapIndicator1:Hide() flickerstreak@18: snapIndicator2:Hide() flickerstreak@18: end flickerstreak@18: end flickerstreak@18: flickerstreak@18: flickerstreak@18: flickerstreak@18: -- library setup flickerstreak@18: flickerstreak@18: local function activate( self, oldLib, oldDeactivate ) flickerstreak@18: snapIndicator1 = createSnapIndicator() flickerstreak@18: snapIndicator2 = createSnapIndicator() flickerstreak@18: flickerstreak@18: if oldDeactivate then flickerstreak@18: oldDeactivate(oldLib) flickerstreak@18: end flickerstreak@18: end flickerstreak@18: flickerstreak@18: AceLibrary:Register(ReAnchor, version_major, version_minor, activate) flickerstreak@18: ReAnchor = nil