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