Mercurial > wow > reaction
changeset 73:dd01feae0d89
Split the bar overlay out into its own file
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Thu, 05 Jun 2008 18:52:53 +0000 |
parents | aa88aed52124 |
children | 00e28094e1a3 |
files | Bar.lua Overlay.lua ReAction.xml |
diffstat | 3 files changed, 684 insertions(+), 678 deletions(-) [+] |
line wrap: on
line diff
--- a/Bar.lua Thu Jun 05 18:34:36 2008 +0000 +++ b/Bar.lua Thu Jun 05 18:52:53 2008 +0000 @@ -2,11 +2,7 @@ local L = ReAction.L local _G = _G local CreateFrame = CreateFrame -local InCombatLockdown = InCombatLockdown local floor = math.floor -local min = math.min -local format = string.format -local GameTooltip = GameTooltip local SecureStateHeader_Refresh = SecureStateHeader_Refresh @@ -61,7 +57,7 @@ end function Bar:OnConfigModeChanged(event, mode) - self:ShowControls(mode) + self:ShowControls(mode) -- ShowControls() defined in Overlay.lua end function Bar:RefreshLayout() @@ -324,683 +320,10 @@ end --- --- Bar config overlay --- -local CreateControls - -do - -- upvalue some of these for small OnUpdate performance boost - local GetSize = Bar.GetSize - local GetButtonSize = Bar.GetButtonSize - local GetButtonGrid = Bar.GetButtonGrid - local SetSize = Bar.SetSize - local SetButtonSize = Bar.SetButtonSize - local SetButtonGrid = Bar.SetButtonGrid - local ApplyAnchor = Bar.ApplyAnchor - - local function StoreExtents(bar) - local f = bar.frame - local point, relativeTo, relativePoint, x, y = f:GetPoint(1) - relativeTo = relativeTo or f:GetParent() - local anchorTo - for name, b in ReAction:IterateBars() do - if b and b:GetFrame() == relativeTo then - anchorTo = name - break - end - end - anchorTo = anchorTo or relativeTo:GetName() - local c = bar.config - c.anchor = point - c.anchorTo = anchorTo - c.relativePoint = relativePoint - c.x = x - c.y = y - c.width, c.height = f:GetWidth(), f:GetHeight() - end - - local function StoreSize(bar) - local f = bar.frame - local c = bar.config - c.width, c.height = f:GetWidth(), f:GetHeight() - end - - local function RecomputeButtonSize(bar) - local w, h = GetSize(bar) - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - - local scaleW = (floor(w/c) - s) / bw - local scaleH = (floor(h/r) - s) / bh - local scale = min(scaleW, scaleH) - - SetButtonSize(bar, scale * bw, scale * bh, s) - end - - local function RecomputeButtonSpacing(bar) - local w, h = GetSize(bar) - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - - SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh)) - end - - local function RecomputeGrid(bar) - local w, h = GetSize(bar) - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - - SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s) - end - - local function ClampToButtons(bar) - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - SetSize(bar, (bw+s)*c + 1, (bh+s)*r + 1) - end - - local function HideGameTooltip() - GameTooltip:Hide() - end - - local anchorInside = { inside = true } - local anchorOutside = { outside = true } - local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" } - local oppositeEdges = { - TOP = "BOTTOM", - BOTTOM = "TOP", - LEFT = "RIGHT", - RIGHT = "LEFT" - } - local pointsOnEdge = { - BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", }, - TOP = { "TOP", "TOPLEFT", "TOPRIGHT", }, - RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", }, - LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", }, - } - local edgeSelector = { - BOTTOM = 1, -- select x of x,y - TOP = 1, -- select x of x,y - LEFT = 2, -- select y of x,y - RIGHT = 2, -- select y of x,y - } - local snapPoints = { - [anchorOutside] = { - BOTTOMLEFT = {"BOTTOMRIGHT","TOPLEFT","TOPRIGHT"}, - BOTTOM = {"TOP"}, - BOTTOMRIGHT = {"BOTTOMLEFT","TOPRIGHT","TOPLEFT"}, - RIGHT = {"LEFT"}, - TOPRIGHT = {"TOPLEFT","BOTTOMRIGHT","BOTTOMLEFT"}, - TOP = {"BOTTOM"}, - TOPLEFT = {"TOPRIGHT","BOTTOMLEFT","BOTTOMRIGHT"}, - LEFT = {"RIGHT"}, - CENTER = {"CENTER"} - }, - [anchorInside] = { - BOTTOMLEFT = {"BOTTOMLEFT"}, - BOTTOM = {"BOTTOM"}, - BOTTOMRIGHT = {"BOTTOMRIGHT"}, - RIGHT = {"RIGHT"}, - TOPRIGHT = {"TOPRIGHT"}, - TOP = {"TOP"}, - TOPLEFT = {"TOPLEFT"}, - LEFT = {"LEFT"}, - CENTER = {"CENTER"} - } - } - local insidePointOffsetFuncs = { - BOTTOMLEFT = function(x, y) return x, y end, - BOTTOM = function(x, y) return 0, y end, - BOTTOMRIGHT = function(x, y) return -x, y end, - RIGHT = function(x, y) return -x, 0 end, - TOPRIGHT = function(x, y) return -x, -y end, - TOP = function(x, y) return 0, -y end, - TOPLEFT = function(x, y) return x, -y end, - LEFT = function(x, y) return x, 0 end, - CENTER = function(x, y) return 0, 0 end, - } - local pointCoordFuncs = { - BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end, - BOTTOM = function(f) return nil, f:GetBottom() end, - BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end, - RIGHT = function(f) return f:GetRight(), nil end, - TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end, - TOP = function(f) return nil, f:GetTop() end, - TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end, - LEFT = function(f) return f:GetLeft(), nil end, - CENTER = function(f) return f:GetCenter() end, - } - local edgeBoundsFuncs = { - BOTTOM = function(f) return f:GetLeft(), f:GetRight() end, - LEFT = function(f) return f:GetBottom(), f:GetTop() end - } - edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM - edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT - - - -- Returns absolute coordinates x,y of the named point 'p' of frame 'f' - local function GetPointCoords( f, p ) - local x, y = pointCoordFuncs[p](f) - if not(x and y) then - local cx, cy = f:GetCenter() - x = x or cx - y = y or cy - end - return x, y - end - - - -- Returns true if frame 'f1' can be anchored to frame 'f2' - local function CheckAnchorable( f1, f2 ) - -- can't anchor a frame to itself or to nil - if f1 == f2 or f2 == nil then - return false - end - - -- can always anchor to UIParent - if f2 == UIParent then - return true - end - - -- also can't do circular anchoring of frames - -- walk the anchor chain, which generally shouldn't be that expensive - -- (who nests draggables that deep anyway?) - for i = 1, f2:GetNumPoints() do - local _, f = f2:GetPoint(i) - if not f then f = f2:GetParent() end - return CheckAnchorable(f1,f) - end - - return true - end - - -- Returns true if frames f1 and f2 specified edges overlap - local function CheckEdgeOverlap( f1, f2, e ) - local l1, u1 = edgeBoundsFuncs[e](f1) - local l2, u2 = edgeBoundsFuncs[e](f2) - return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2 - end - - -- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2 - local function CheckPointEdgeOverlap( f1, p1, f2, e2 ) - local l, u = edgeBoundsFuncs[e2](f2) - local x, y = GetPointCoords(f1,p1) - x = select(edgeSelector[e2], x, y) - return l <= x and x <= u - end - - -- Returns the distance between corresponding edges. It is - -- assumed that the passed in edges e1 and e2 are the same or opposites - local function GetEdgeDistance( f1, f2, e1, e2 ) - local x1, y1 = pointCoordFuncs[e1](f1) - local x2, y2 = pointCoordFuncs[e2](f2) - return math.abs((x1 or y1) - (x2 or y2)) - end - - local globalSnapTargets = { [UIParent] = anchorInside } - - local function GetClosestFrameEdge(f1,f2,a) - local dist, edge, opp - if f2:IsVisible() and CheckAnchorable(f1,f2) then - for _, e in pairs(edges) do - local o = a.inside and e or oppositeEdges[e] - if CheckEdgeOverlap(f1,f2,e) then - local d = GetEdgeDistance(f1, f2, e, o) - if not dist or (d < dist) then - dist, edge, opp = d, e, o - end - end - end - end - return dist, edge, opp - end - - local function GetClosestVisibleEdge( f ) - local r, o, e1, e2 - local a = anchorOutside - for _, b in ReAction:IterateBars() do - local d, e, opp = GetClosestFrameEdge(f,b:GetFrame(),a) - if d and (not r or d < r) then - r, o, e1, e2 = d, b:GetFrame(), e, opp - end - end - for f2, a2 in pairs(globalSnapTargets) do - local d, e, opp = GetClosestFrameEdge(f,f2,a2) - if d and (not r or d < r) then - r, o, e1, e2, a = d, f2, e, opp, a2 - end - end - return o, e1, e2, a - end - - local function GetClosestVisiblePoint(f1) - local f2, e1, e2, a = GetClosestVisibleEdge(f1) - if f2 then - local rsq, p, rp, x, y - -- iterate pointsOnEdge in order and use < to prefer edge centers to corners - for _, p1 in ipairs(pointsOnEdge[e1]) do - if CheckPointEdgeOverlap(f1,p1,f2,e2) then - for _, p2 in pairs(snapPoints[a][p1]) do - local x1, y1 = GetPointCoords(f1,p1) - local x2, y2 = GetPointCoords(f2,p2) - local dx = x1 - x2 - local dy = y1 - y2 - local rsq2 = dx*dx + dy*dy - if not rsq or rsq2 < rsq then - rsq, p, rp, x, y = rsq2, p1, p2, dx, dy - end - end - end - end - return f2, p, rp, x, y - end - end - - local function GetClosestPointSnapped(f1, rx, ry, xOff, yOff) - local o, p, rp, x, y = GetClosestVisiblePoint(f1) - local s = false - - local sx, sy = insidePointOffsetFuncs[p](xOff or 0, yOff or 0) - local xx, yy = pointCoordFuncs[p](f1) - if xx and yy then - if math.abs(x) <= rx then - x = sx - s = true - end - if math.abs(y) <= ry then - y = sy - s = true - end - elseif xx then - if math.abs(x) <= rx then - x = sx - s = true - if math.abs(y) <= ry then - y = sy - end - end - elseif yy then - if math.abs(y) <= ry then - y = sy - s = true - if math.abs(x) <= rx then - x = sx - end - end - end - - if x == -0 then x = 0 end - if y == -0 then y = 0 end - - if s then - return o, p, rp, math.floor(x), math.floor(y) - end - end - - local function CreateSnapIndicator() - local si = CreateFrame("Frame",nil,UIParent) - si:SetFrameStrata("HIGH") - si:SetHeight(8) - si:SetWidth(8) - local tex = si:CreateTexture() - tex:SetAllPoints() - tex:SetTexture(1.0, 0.82, 0, 0.8) - tex:SetBlendMode("ADD") - tex:SetDrawLayer("OVERLAY") - return si - end - - local si1 = CreateSnapIndicator() - local si2 = CreateSnapIndicator() - - local function DisplaySnapIndicator( f, rx, ry, xOff, yOff ) - local o, p, rp, x, y, snap = GetClosestPointSnapped(f, rx, ry, xOff, yOff) - if o then - si1:ClearAllPoints() - si2:ClearAllPoints() - si1:SetPoint("CENTER", f, p, 0, 0) - local xx, yy = pointCoordFuncs[rp](o) - x = math.abs(x) <=rx and xx and 0 or x - y = math.abs(y) <=ry and yy and 0 or y - si2:SetPoint("CENTER", o, rp, x, y) - si1:Show() - si2:Show() - else - if si1:IsVisible() then - si1:Hide() - si2:Hide() - end - end - end - - local function HideSnapIndicator() - if si1:IsVisible() then - si1:Hide() - si2:Hide() - end - end - - function CreateControls(bar) - local f = bar.frame - - f:SetMovable(true) - f:SetResizable(true) - f:SetClampedToScreen(true) - - -- buttons on the bar should be direct children of the bar frame. - -- The control elements need to float on top of this, which we could - -- do with SetFrameLevel() or Raise(), but it's more reliable to do it - -- via frame nesting, hence good old foo's appearance here. - local foo = CreateFrame("Frame",nil,f) - foo:SetAllPoints() - foo:SetClampedToScreen(true) - - local control = CreateFrame("Button", nil, foo) - control:EnableMouse(true) - control:SetToplevel(true) - control:SetPoint("TOPLEFT", -4, 4) - control:SetPoint("BOTTOMRIGHT", 4, -4) - control:SetBackdrop({ - edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", - tile = true, - tileSize = 16, - edgeSize = 16, - insets = { left = 0, right = 0, top = 0, bottom = 0 }, - }) - - -- textures - local bgTex = control:CreateTexture(nil,"BACKGROUND") - bgTex:SetTexture(0.7,0.7,1.0,0.2) - bgTex:SetPoint("TOPLEFT",4,-4) - bgTex:SetPoint("BOTTOMRIGHT",-4,4) - local hTex = control:CreateTexture(nil,"HIGHLIGHT") - hTex:SetTexture(0.7,0.7,1.0,0.2) - hTex:SetPoint("TOPLEFT",4,-4) - hTex:SetPoint("BOTTOMRIGHT",-4,4) - hTex:SetBlendMode("ADD") - - -- label - local label = control:CreateFontString(nil,"OVERLAY","GameFontNormalLarge") - label:SetAllPoints() - label:SetJustifyH("CENTER") - label:SetShadowColor(0,0,0,1) - label:SetShadowOffset(2,-2) - label:SetTextColor(1,1,1,1) - label:SetText(bar:GetName()) - label:Show() - bar.controlLabelString = label -- so that bar:SetName() can update it - - local function StopResize() - f:StopMovingOrSizing() - f.isMoving = false - f:SetScript("OnUpdate",nil) - StoreSize(bar) - ClampToButtons(bar) - ApplyAnchor(bar) - ReAction:RefreshOptions() - end - - -- edge drag handles - for _, point in pairs({"LEFT","TOP","RIGHT","BOTTOM"}) do - local edge = CreateFrame("Frame",nil,control) - edge:EnableMouse(true) - edge:SetWidth(8) - edge:SetHeight(8) - if point == "TOP" or point == "BOTTOM" then - edge:SetPoint(point.."LEFT") - edge:SetPoint(point.."RIGHT") - else - edge:SetPoint("TOP"..point) - edge:SetPoint("BOTTOM"..point) - end - local tex = edge:CreateTexture(nil,"HIGHLIGHT") - tex:SetTexture(1.0,0.82,0,0.7) - tex:SetBlendMode("ADD") - tex:SetAllPoints() - edge:RegisterForDrag("LeftButton") - edge:SetScript("OnMouseDown", - function() - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - f:SetMinResize( bw+s+1, bh+s+1 ) - f:StartSizing(point) - f:SetScript("OnUpdate", - function() - RecomputeGrid(bar) - bar:RefreshLayout() - end - ) - end - ) - edge:SetScript("OnMouseUp", StopResize) - edge:SetScript("OnEnter", - function() - GameTooltip:SetOwner(f, "ANCHOR_"..point) - GameTooltip:AddLine(L["Drag to add/remove buttons"]) - GameTooltip:Show() - end - ) - edge:SetScript("OnLeave", HideGameTooltip) - edge:Show() - end - - -- corner drag handles, again nested in an anonymous frame so that they are on top - local foo2 = CreateFrame("Frame",nil,control) - foo2:SetAllPoints(true) - for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do - local corner = CreateFrame("Frame",nil,foo2) - corner:EnableMouse(true) - corner:SetWidth(12) - corner:SetHeight(12) - corner:SetPoint(point) - local tex = corner:CreateTexture(nil,"HIGHLIGHT") - tex:SetTexture(1.0,0.82,0,0.7) - tex:SetBlendMode("ADD") - tex:SetAllPoints() - corner:RegisterForDrag("LeftButton","RightButton") - local function updateTooltip() - local size, size2 = bar:GetButtonSize() - local rows, cols, spacing = bar:GetButtonGrid() - size = (size == size2) and tostring(size) or format("%dx%d",size,size2) - GameTooltipTextRight4:SetText(size) - GameTooltipTextRight5:SetText(tostring(spacing)) - end - corner:SetScript("OnMouseDown", - function(_,btn) - local bw, bh = GetButtonSize(bar) - local r, c, s = GetButtonGrid(bar) - if btn == "LeftButton" then -- button resize - f:SetMinResize( (s+12)*c+1, (s+12)*r+1 ) - f:SetScript("OnUpdate", - function() - RecomputeButtonSize(bar) - bar:RefreshLayout() - updateTooltip() - end - ) - elseif btn == "RightButton" then -- spacing resize - f:SetMinResize( bw*c, bh*r ) - f:SetScript("OnUpdate", - function() - RecomputeButtonSpacing(bar) - bar:RefreshLayout() - updateTooltip() - end - ) - end - f:StartSizing(point) - end - ) - corner:SetScript("OnMouseUp",StopResize) - corner:SetScript("OnEnter", - function() - GameTooltip:SetOwner(f, "ANCHOR_"..point) - GameTooltip:AddLine(L["Drag to resize buttons"]) - GameTooltip:AddLine(L["Right-click-drag"]) - GameTooltip:AddLine(L["to change spacing"]) - local size, size2 = bar:GetButtonSize() - local rows, cols, spacing = bar:GetButtonGrid() - size = (size == size2) and tostring(size) or format("%dx%d",size,size2) - GameTooltip:AddDoubleLine(L["Size:"], size) - GameTooltip:AddDoubleLine(L["Spacing:"], tostring(spacing)) - GameTooltip:Show() - end - ) - corner:SetScript("OnLeave", - function() - GameTooltip:Hide() - f:SetScript("OnUpdate",nil) - end - ) - - end - - control:RegisterForDrag("LeftButton") - control:RegisterForClicks("RightButtonDown") - - control:SetScript("OnDragStart", - function() - f:StartMoving() - f.isMoving = true - local w,h = bar:GetButtonSize() - f:ClearAllPoints() - f:SetScript("OnUpdate", function() - if IsShiftKeyDown() then - DisplaySnapIndicator(f,w,h) - else - HideSnapIndicator() - end - end) - end - ) - - local function updateDragTooltip() - GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT") - GameTooltip:AddLine(bar.name) - GameTooltip:AddLine(L["Drag to move"]) - GameTooltip:AddLine(("|cff00ff00%s|r %s"):format(L["Shift-drag"],L["to anchor to nearby frames"])) - GameTooltip:AddLine(("|cff00cccc%s|r %s"):format(L["Right-click"],L["for options"])) - local _, a = bar:GetAnchor() - if a and a ~= "UIParent" then - GameTooltip:AddLine(L["Currently anchored to <%s>"]:format(a)) - end - GameTooltip:Show() - end - - control:SetScript("OnDragStop", - function() - f:StopMovingOrSizing() - f.isMoving = false - f:SetScript("OnUpdate",nil) - - if IsShiftKeyDown() then - local w, h = bar:GetButtonSize() - local a, p, rp, x, y = GetClosestPointSnapped(f,w,h) - if a then - f:ClearAllPoints() - f:SetPoint(p,a,rp,x,y) - end - HideSnapIndicator() - end - - StoreExtents(bar) - ReAction:RefreshOptions() - updateDragTooltip() - end - ) - - control:SetScript("OnEnter", - function() - -- TODO: add bar type and status information to name - --[[ - local name = bar.name - for _, m in ReAction:IterateModules() do - local suffix = safecall(m,"GetBarNameModifier",bar) - if suffix then - name = ("%s %s"):format(name,suffix) - end - end - ]]-- - - updateDragTooltip() - end - ) - - control:SetScript("OnLeave", HideGameTooltip) - - control:SetScript("OnClick", - function() - bar:ShowMenu() - end - ) - - return control - end -end - - -local OpenMenu, CloseMenu -do - -- Looking for a lightweight AceConfig3-struct-compatible - -- replacement for Dewdrop, encapsulate here - -- Considering Blizzard's EasyMenu/UIDropDownMenu, but that's - -- a bit tricky to convert from AceConfig3-struct - local Dewdrop = AceLibrary("Dewdrop-2.0") - function OpenMenu (frame, opts) - Dewdrop:Open(frame, "children", opts, "cursorX", true, "cursorY", true) - end - function CloseMenu(frame) - if Dewdrop:GetOpenedParent() == frame then - Dewdrop:Close() - end - end -end - - -function Bar:ShowControls(show) - if show then - if not self.controlFrame then - self.controlFrame = CreateControls(self) - end - self.controlFrame:Show() - elseif self.controlFrame then - CloseMenu(self.controlFrame) - self.controlFrame:Hide() - end -end - -function Bar:ShowMenu() - if not self.menuOpts then - self.menuOpts = { - type = "group", - args = { - openConfig = { - type = "execute", - name = L["Settings..."], - desc = L["Open the editor for this bar"], - func = function() CloseMenu(self.controlFrame); ReAction:ShowEditor(self) end, - disabled = InCombatLockdown, - order = 1 - }, - delete = { - type = "execute", - name = L["Delete Bar"], - desc = L["Remove the bar from the current profile"], - confirm = L["Are you sure you want to remove this bar?"], - func = function() ReAction:EraseBar(self) end, - order = 2 - }, - } - } - end - OpenMenu(self.controlFrame, self.menuOpts) -end - - ------ Export as a class-factory ------ ReAction.Bar = { + prototype = Bar, new = function(self, ...) local x = { } for k,v in pairs(Bar) do
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Overlay.lua Thu Jun 05 18:52:53 2008 +0000 @@ -0,0 +1,681 @@ +local ReAction = ReAction +local L = ReAction.L +local CreateFrame = CreateFrame +local InCombatLockdown = InCombatLockdown +local floor = math.floor +local min = math.min +local format = string.format +local GameTooltip = GameTooltip + +-- Looking for a lightweight AceConfig3-struct-compatible +-- replacement for Dewdrop (e.g. forthcoming AceConfigDropdown-3.0?). +-- Considering Blizzard's EasyMenu/UIDropDownMenu, but that's +-- a bit tricky to convert from AceConfig3-struct +local Dewdrop = AceLibrary("Dewdrop-2.0") + +local function OpenMenu (frame, opts) + Dewdrop:Open(frame, "children", opts, "cursorX", true, "cursorY", true) +end + +local function CloseMenu(frame) + if Dewdrop:GetOpenedParent() == frame then + Dewdrop:Close() + end +end + +local function ShowMenu(bar) + if not bar.menuOpts then + bar.menuOpts = { + type = "group", + args = { + openConfig = { + type = "execute", + name = L["Settings..."], + desc = L["Open the editor for this bar"], + func = function() CloseMenu(bar.controlFrame); ReAction:ShowEditor(bar) end, + disabled = InCombatLockdown, + order = 1 + }, + delete = { + type = "execute", + name = L["Delete Bar"], + desc = L["Remove the bar from the current profile"], + confirm = L["Are you sure you want to remove this bar?"], + func = function() ReAction:EraseBar(bar) end, + order = 2 + }, + } + } + end + OpenMenu(bar.controlFrame, bar.menuOpts) +end + + +-- +-- Bar config overlay +-- +-- localize some of these for small OnUpdate performance boost +local Bar = ReAction.Bar.prototype +local GetSize = Bar.GetSize +local GetButtonSize = Bar.GetButtonSize +local GetButtonGrid = Bar.GetButtonGrid +local SetSize = Bar.SetSize +local SetButtonSize = Bar.SetButtonSize +local SetButtonGrid = Bar.SetButtonGrid +local ApplyAnchor = Bar.ApplyAnchor + +local function StoreExtents(bar) + local f = bar.frame + local point, relativeTo, relativePoint, x, y = f:GetPoint(1) + relativeTo = relativeTo or f:GetParent() + local anchorTo + for name, b in ReAction:IterateBars() do + if b and b:GetFrame() == relativeTo then + anchorTo = name + break + end + end + anchorTo = anchorTo or relativeTo:GetName() + local c = bar.config + c.anchor = point + c.anchorTo = anchorTo + c.relativePoint = relativePoint + c.x = x + c.y = y + c.width, c.height = f:GetWidth(), f:GetHeight() +end + +local function StoreSize(bar) + local f = bar.frame + local c = bar.config + c.width, c.height = f:GetWidth(), f:GetHeight() +end + +local function RecomputeButtonSize(bar) + local w, h = GetSize(bar) + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + + local scaleW = (floor(w/c) - s) / bw + local scaleH = (floor(h/r) - s) / bh + local scale = min(scaleW, scaleH) + + SetButtonSize(bar, scale * bw, scale * bh, s) +end + +local function RecomputeButtonSpacing(bar) + local w, h = GetSize(bar) + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + + SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh)) +end + +local function RecomputeGrid(bar) + local w, h = GetSize(bar) + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + + SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s) +end + +local function ClampToButtons(bar) + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + SetSize(bar, (bw+s)*c + 1, (bh+s)*r + 1) +end + +local function HideGameTooltip() + GameTooltip:Hide() +end + +local anchorInside = { inside = true } +local anchorOutside = { outside = true } +local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" } +local oppositeEdges = { + TOP = "BOTTOM", + BOTTOM = "TOP", + LEFT = "RIGHT", + RIGHT = "LEFT" +} +local pointsOnEdge = { + BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", }, + TOP = { "TOP", "TOPLEFT", "TOPRIGHT", }, + RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", }, + LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", }, +} +local edgeSelector = { + BOTTOM = 1, -- select x of x,y + TOP = 1, -- select x of x,y + LEFT = 2, -- select y of x,y + RIGHT = 2, -- select y of x,y +} +local snapPoints = { + [anchorOutside] = { + BOTTOMLEFT = {"BOTTOMRIGHT","TOPLEFT","TOPRIGHT"}, + BOTTOM = {"TOP"}, + BOTTOMRIGHT = {"BOTTOMLEFT","TOPRIGHT","TOPLEFT"}, + RIGHT = {"LEFT"}, + TOPRIGHT = {"TOPLEFT","BOTTOMRIGHT","BOTTOMLEFT"}, + TOP = {"BOTTOM"}, + TOPLEFT = {"TOPRIGHT","BOTTOMLEFT","BOTTOMRIGHT"}, + LEFT = {"RIGHT"}, + CENTER = {"CENTER"} + }, + [anchorInside] = { + BOTTOMLEFT = {"BOTTOMLEFT"}, + BOTTOM = {"BOTTOM"}, + BOTTOMRIGHT = {"BOTTOMRIGHT"}, + RIGHT = {"RIGHT"}, + TOPRIGHT = {"TOPRIGHT"}, + TOP = {"TOP"}, + TOPLEFT = {"TOPLEFT"}, + LEFT = {"LEFT"}, + CENTER = {"CENTER"} + } +} +local insidePointOffsetFuncs = { + BOTTOMLEFT = function(x, y) return x, y end, + BOTTOM = function(x, y) return 0, y end, + BOTTOMRIGHT = function(x, y) return -x, y end, + RIGHT = function(x, y) return -x, 0 end, + TOPRIGHT = function(x, y) return -x, -y end, + TOP = function(x, y) return 0, -y end, + TOPLEFT = function(x, y) return x, -y end, + LEFT = function(x, y) return x, 0 end, + CENTER = function(x, y) return 0, 0 end, +} +local pointCoordFuncs = { + BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end, + BOTTOM = function(f) return nil, f:GetBottom() end, + BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end, + RIGHT = function(f) return f:GetRight(), nil end, + TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end, + TOP = function(f) return nil, f:GetTop() end, + TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end, + LEFT = function(f) return f:GetLeft(), nil end, + CENTER = function(f) return f:GetCenter() end, +} +local edgeBoundsFuncs = { + BOTTOM = function(f) return f:GetLeft(), f:GetRight() end, + LEFT = function(f) return f:GetBottom(), f:GetTop() end +} +edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM +edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT + + +-- Returns absolute coordinates x,y of the named point 'p' of frame 'f' +local function GetPointCoords( f, p ) + local x, y = pointCoordFuncs[p](f) + if not(x and y) then + local cx, cy = f:GetCenter() + x = x or cx + y = y or cy + end + return x, y +end + + +-- Returns true if frame 'f1' can be anchored to frame 'f2' +local function CheckAnchorable( f1, f2 ) + -- can't anchor a frame to itself or to nil + if f1 == f2 or f2 == nil then + return false + end + + -- can always anchor to UIParent + if f2 == UIParent then + return true + end + + -- also can't do circular anchoring of frames + -- walk the anchor chain, which generally shouldn't be that expensive + -- (who nests draggables that deep anyway?) + for i = 1, f2:GetNumPoints() do + local _, f = f2:GetPoint(i) + if not f then f = f2:GetParent() end + return CheckAnchorable(f1,f) + end + + return true +end + +-- Returns true if frames f1 and f2 specified edges overlap +local function CheckEdgeOverlap( f1, f2, e ) + local l1, u1 = edgeBoundsFuncs[e](f1) + local l2, u2 = edgeBoundsFuncs[e](f2) + return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2 +end + +-- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2 +local function CheckPointEdgeOverlap( f1, p1, f2, e2 ) + local l, u = edgeBoundsFuncs[e2](f2) + local x, y = GetPointCoords(f1,p1) + x = select(edgeSelector[e2], x, y) + return l <= x and x <= u +end + +-- Returns the distance between corresponding edges. It is +-- assumed that the passed in edges e1 and e2 are the same or opposites +local function GetEdgeDistance( f1, f2, e1, e2 ) + local x1, y1 = pointCoordFuncs[e1](f1) + local x2, y2 = pointCoordFuncs[e2](f2) + return math.abs((x1 or y1) - (x2 or y2)) +end + +local globalSnapTargets = { [UIParent] = anchorInside } + +local function GetClosestFrameEdge(f1,f2,a) + local dist, edge, opp + if f2:IsVisible() and CheckAnchorable(f1,f2) then + for _, e in pairs(edges) do + local o = a.inside and e or oppositeEdges[e] + if CheckEdgeOverlap(f1,f2,e) then + local d = GetEdgeDistance(f1, f2, e, o) + if not dist or (d < dist) then + dist, edge, opp = d, e, o + end + end + end + end + return dist, edge, opp +end + +local function GetClosestVisibleEdge( f ) + local r, o, e1, e2 + local a = anchorOutside + for _, b in ReAction:IterateBars() do + local d, e, opp = GetClosestFrameEdge(f,b:GetFrame(),a) + if d and (not r or d < r) then + r, o, e1, e2 = d, b:GetFrame(), e, opp + end + end + for f2, a2 in pairs(globalSnapTargets) do + local d, e, opp = GetClosestFrameEdge(f,f2,a2) + if d and (not r or d < r) then + r, o, e1, e2, a = d, f2, e, opp, a2 + end + end + return o, e1, e2, a +end + +local function GetClosestVisiblePoint(f1) + local f2, e1, e2, a = GetClosestVisibleEdge(f1) + if f2 then + local rsq, p, rp, x, y + -- iterate pointsOnEdge in order and use < to prefer edge centers to corners + for _, p1 in ipairs(pointsOnEdge[e1]) do + if CheckPointEdgeOverlap(f1,p1,f2,e2) then + for _, p2 in pairs(snapPoints[a][p1]) do + local x1, y1 = GetPointCoords(f1,p1) + local x2, y2 = GetPointCoords(f2,p2) + local dx = x1 - x2 + local dy = y1 - y2 + local rsq2 = dx*dx + dy*dy + if not rsq or rsq2 < rsq then + rsq, p, rp, x, y = rsq2, p1, p2, dx, dy + end + end + end + end + return f2, p, rp, x, y + end +end + +local function GetClosestPointSnapped(f1, rx, ry, xOff, yOff) + local o, p, rp, x, y = GetClosestVisiblePoint(f1) + local s = false + + local sx, sy = insidePointOffsetFuncs[p](xOff or 0, yOff or 0) + local xx, yy = pointCoordFuncs[p](f1) + if xx and yy then + if math.abs(x) <= rx then + x = sx + s = true + end + if math.abs(y) <= ry then + y = sy + s = true + end + elseif xx then + if math.abs(x) <= rx then + x = sx + s = true + if math.abs(y) <= ry then + y = sy + end + end + elseif yy then + if math.abs(y) <= ry then + y = sy + s = true + if math.abs(x) <= rx then + x = sx + end + end + end + + if x == -0 then x = 0 end + if y == -0 then y = 0 end + + if s then + return o, p, rp, math.floor(x), math.floor(y) + end +end + +local function CreateSnapIndicator() + local si = CreateFrame("Frame",nil,UIParent) + si:SetFrameStrata("HIGH") + si:SetHeight(8) + si:SetWidth(8) + local tex = si:CreateTexture() + tex:SetAllPoints() + tex:SetTexture(1.0, 0.82, 0, 0.8) + tex:SetBlendMode("ADD") + tex:SetDrawLayer("OVERLAY") + return si +end + +local si1 = CreateSnapIndicator() +local si2 = CreateSnapIndicator() + +local function DisplaySnapIndicator( f, rx, ry, xOff, yOff ) + local o, p, rp, x, y, snap = GetClosestPointSnapped(f, rx, ry, xOff, yOff) + if o then + si1:ClearAllPoints() + si2:ClearAllPoints() + si1:SetPoint("CENTER", f, p, 0, 0) + local xx, yy = pointCoordFuncs[rp](o) + x = math.abs(x) <=rx and xx and 0 or x + y = math.abs(y) <=ry and yy and 0 or y + si2:SetPoint("CENTER", o, rp, x, y) + si1:Show() + si2:Show() + else + if si1:IsVisible() then + si1:Hide() + si2:Hide() + end + end +end + +local function HideSnapIndicator() + if si1:IsVisible() then + si1:Hide() + si2:Hide() + end +end + +local function CreateControls(bar) + local f = bar.frame + + f:SetMovable(true) + f:SetResizable(true) + f:SetClampedToScreen(true) + + -- buttons on the bar should be direct children of the bar frame. + -- The control elements need to float on top of this, which we could + -- do with SetFrameLevel() or Raise(), but it's more reliable to do it + -- via frame nesting, hence good old foo's appearance here. + local foo = CreateFrame("Frame",nil,f) + foo:SetAllPoints() + foo:SetClampedToScreen(true) + + local control = CreateFrame("Button", nil, foo) + control:EnableMouse(true) + control:SetToplevel(true) + control:SetPoint("TOPLEFT", -4, 4) + control:SetPoint("BOTTOMRIGHT", 4, -4) + control:SetBackdrop({ + edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", + tile = true, + tileSize = 16, + edgeSize = 16, + insets = { left = 0, right = 0, top = 0, bottom = 0 }, + }) + + -- textures + local bgTex = control:CreateTexture(nil,"BACKGROUND") + bgTex:SetTexture(0.7,0.7,1.0,0.2) + bgTex:SetPoint("TOPLEFT",4,-4) + bgTex:SetPoint("BOTTOMRIGHT",-4,4) + local hTex = control:CreateTexture(nil,"HIGHLIGHT") + hTex:SetTexture(0.7,0.7,1.0,0.2) + hTex:SetPoint("TOPLEFT",4,-4) + hTex:SetPoint("BOTTOMRIGHT",-4,4) + hTex:SetBlendMode("ADD") + + -- label + local label = control:CreateFontString(nil,"OVERLAY","GameFontNormalLarge") + label:SetAllPoints() + label:SetJustifyH("CENTER") + label:SetShadowColor(0,0,0,1) + label:SetShadowOffset(2,-2) + label:SetTextColor(1,1,1,1) + label:SetText(bar:GetName()) + label:Show() + bar.controlLabelString = label -- so that bar:SetName() can update it + + local function StopResize() + f:StopMovingOrSizing() + f.isMoving = false + f:SetScript("OnUpdate",nil) + StoreSize(bar) + ClampToButtons(bar) + ApplyAnchor(bar) + ReAction:RefreshOptions() + end + + -- edge drag handles + for _, point in pairs({"LEFT","TOP","RIGHT","BOTTOM"}) do + local edge = CreateFrame("Frame",nil,control) + edge:EnableMouse(true) + edge:SetWidth(8) + edge:SetHeight(8) + if point == "TOP" or point == "BOTTOM" then + edge:SetPoint(point.."LEFT") + edge:SetPoint(point.."RIGHT") + else + edge:SetPoint("TOP"..point) + edge:SetPoint("BOTTOM"..point) + end + local tex = edge:CreateTexture(nil,"HIGHLIGHT") + tex:SetTexture(1.0,0.82,0,0.7) + tex:SetBlendMode("ADD") + tex:SetAllPoints() + edge:RegisterForDrag("LeftButton") + edge:SetScript("OnMouseDown", + function() + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + f:SetMinResize( bw+s+1, bh+s+1 ) + f:StartSizing(point) + f:SetScript("OnUpdate", + function() + RecomputeGrid(bar) + bar:RefreshLayout() + end + ) + end + ) + edge:SetScript("OnMouseUp", StopResize) + edge:SetScript("OnEnter", + function() + GameTooltip:SetOwner(f, "ANCHOR_"..point) + GameTooltip:AddLine(L["Drag to add/remove buttons"]) + GameTooltip:Show() + end + ) + edge:SetScript("OnLeave", HideGameTooltip) + edge:Show() + end + + -- corner drag handles, again nested in an anonymous frame so that they are on top + local foo2 = CreateFrame("Frame",nil,control) + foo2:SetAllPoints(true) + for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do + local corner = CreateFrame("Frame",nil,foo2) + corner:EnableMouse(true) + corner:SetWidth(12) + corner:SetHeight(12) + corner:SetPoint(point) + local tex = corner:CreateTexture(nil,"HIGHLIGHT") + tex:SetTexture(1.0,0.82,0,0.7) + tex:SetBlendMode("ADD") + tex:SetAllPoints() + corner:RegisterForDrag("LeftButton","RightButton") + local function updateTooltip() + local size, size2 = bar:GetButtonSize() + local rows, cols, spacing = bar:GetButtonGrid() + size = (size == size2) and tostring(size) or format("%dx%d",size,size2) + GameTooltipTextRight4:SetText(size) + GameTooltipTextRight5:SetText(tostring(spacing)) + end + corner:SetScript("OnMouseDown", + function(_,btn) + local bw, bh = GetButtonSize(bar) + local r, c, s = GetButtonGrid(bar) + if btn == "LeftButton" then -- button resize + f:SetMinResize( (s+12)*c+1, (s+12)*r+1 ) + f:SetScript("OnUpdate", + function() + RecomputeButtonSize(bar) + bar:RefreshLayout() + updateTooltip() + end + ) + elseif btn == "RightButton" then -- spacing resize + f:SetMinResize( bw*c, bh*r ) + f:SetScript("OnUpdate", + function() + RecomputeButtonSpacing(bar) + bar:RefreshLayout() + updateTooltip() + end + ) + end + f:StartSizing(point) + end + ) + corner:SetScript("OnMouseUp",StopResize) + corner:SetScript("OnEnter", + function() + GameTooltip:SetOwner(f, "ANCHOR_"..point) + GameTooltip:AddLine(L["Drag to resize buttons"]) + GameTooltip:AddLine(L["Right-click-drag"]) + GameTooltip:AddLine(L["to change spacing"]) + local size, size2 = bar:GetButtonSize() + local rows, cols, spacing = bar:GetButtonGrid() + size = (size == size2) and tostring(size) or format("%dx%d",size,size2) + GameTooltip:AddDoubleLine(L["Size:"], size) + GameTooltip:AddDoubleLine(L["Spacing:"], tostring(spacing)) + GameTooltip:Show() + end + ) + corner:SetScript("OnLeave", + function() + GameTooltip:Hide() + f:SetScript("OnUpdate",nil) + end + ) + + end + + control:RegisterForDrag("LeftButton") + control:RegisterForClicks("RightButtonUp") + + control:SetScript("OnDragStart", + function() + f:StartMoving() + f.isMoving = true + local w,h = bar:GetButtonSize() + f:ClearAllPoints() + f:SetScript("OnUpdate", function() + if IsShiftKeyDown() then + DisplaySnapIndicator(f,w,h) + else + HideSnapIndicator() + end + end) + end + ) + + local function updateDragTooltip() + GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT") + GameTooltip:AddLine(bar.name) + GameTooltip:AddLine(L["Drag to move"]) + GameTooltip:AddLine(("|cff00ff00%s|r %s"):format(L["Shift-drag"],L["to anchor to nearby frames"])) + GameTooltip:AddLine(("|cff00cccc%s|r %s"):format(L["Right-click"],L["for options"])) + local _, a = bar:GetAnchor() + if a and a ~= "UIParent" then + GameTooltip:AddLine(L["Currently anchored to <%s>"]:format(a)) + end + GameTooltip:Show() + end + + control:SetScript("OnDragStop", + function() + f:StopMovingOrSizing() + f.isMoving = false + f:SetScript("OnUpdate",nil) + + if IsShiftKeyDown() then + local w, h = bar:GetButtonSize() + local a, p, rp, x, y = GetClosestPointSnapped(f,w,h) + if a then + f:ClearAllPoints() + f:SetPoint(p,a,rp,x,y) + end + HideSnapIndicator() + end + + StoreExtents(bar) + ReAction:RefreshOptions() + updateDragTooltip() + end + ) + + control:SetScript("OnEnter", + function() + -- TODO: add bar type and status information to name + --[[ + local name = bar.name + for _, m in ReAction:IterateModules() do + local suffix = safecall(m,"GetBarNameModifier",bar) + if suffix then + name = ("%s %s"):format(name,suffix) + end + end + ]]-- + + updateDragTooltip() + end + ) + + control:SetScript("OnLeave", HideGameTooltip) + + control:SetScript("OnClick", + function() + ShowMenu(bar) + end + ) + + return control +end + + +-- export the ShowControls method to the Bar prototype + +function Bar:ShowControls(show) + if show then + if not self.controlFrame then + self.controlFrame = CreateControls(self) + end + self.controlFrame:Show() + elseif self.controlFrame then + CloseMenu(self.controlFrame) + self.controlFrame:Hide() + end +end + +