annotate Overlay.lua @ 75:06cd74bdc7da

- Cleaned up Bar interface - Move all attribute setting from Bar into State - Separated Moonkin and Tree of Life - Removed PossessBar module - Added some infrastructure for paged/mind control support to Action
author Flick <flickerstreak@gmail.com>
date Mon, 16 Jun 2008 18:46:08 +0000
parents dd01feae0d89
children da8ba8783924
rev   line source
flickerstreak@73 1 local ReAction = ReAction
flickerstreak@73 2 local L = ReAction.L
flickerstreak@73 3 local CreateFrame = CreateFrame
flickerstreak@73 4 local InCombatLockdown = InCombatLockdown
flickerstreak@73 5 local floor = math.floor
flickerstreak@73 6 local min = math.min
flickerstreak@73 7 local format = string.format
flickerstreak@73 8 local GameTooltip = GameTooltip
flickerstreak@73 9
flickerstreak@73 10 -- Looking for a lightweight AceConfig3-struct-compatible
flickerstreak@73 11 -- replacement for Dewdrop (e.g. forthcoming AceConfigDropdown-3.0?).
flickerstreak@73 12 -- Considering Blizzard's EasyMenu/UIDropDownMenu, but that's
flickerstreak@73 13 -- a bit tricky to convert from AceConfig3-struct
flickerstreak@73 14 local Dewdrop = AceLibrary("Dewdrop-2.0")
flickerstreak@73 15
flickerstreak@73 16 local function OpenMenu (frame, opts)
flickerstreak@73 17 Dewdrop:Open(frame, "children", opts, "cursorX", true, "cursorY", true)
flickerstreak@73 18 end
flickerstreak@73 19
flickerstreak@73 20 local function CloseMenu(frame)
flickerstreak@73 21 if Dewdrop:GetOpenedParent() == frame then
flickerstreak@73 22 Dewdrop:Close()
flickerstreak@73 23 end
flickerstreak@73 24 end
flickerstreak@73 25
flickerstreak@73 26 local function ShowMenu(bar)
flickerstreak@73 27 if not bar.menuOpts then
flickerstreak@73 28 bar.menuOpts = {
flickerstreak@73 29 type = "group",
flickerstreak@73 30 args = {
flickerstreak@73 31 openConfig = {
flickerstreak@73 32 type = "execute",
flickerstreak@73 33 name = L["Settings..."],
flickerstreak@73 34 desc = L["Open the editor for this bar"],
flickerstreak@73 35 func = function() CloseMenu(bar.controlFrame); ReAction:ShowEditor(bar) end,
flickerstreak@73 36 disabled = InCombatLockdown,
flickerstreak@73 37 order = 1
flickerstreak@73 38 },
flickerstreak@73 39 delete = {
flickerstreak@73 40 type = "execute",
flickerstreak@73 41 name = L["Delete Bar"],
flickerstreak@73 42 desc = L["Remove the bar from the current profile"],
flickerstreak@73 43 confirm = L["Are you sure you want to remove this bar?"],
flickerstreak@73 44 func = function() ReAction:EraseBar(bar) end,
flickerstreak@73 45 order = 2
flickerstreak@73 46 },
flickerstreak@73 47 }
flickerstreak@73 48 }
flickerstreak@73 49 end
flickerstreak@73 50 OpenMenu(bar.controlFrame, bar.menuOpts)
flickerstreak@73 51 end
flickerstreak@73 52
flickerstreak@73 53
flickerstreak@73 54 --
flickerstreak@73 55 -- Bar config overlay
flickerstreak@73 56 --
flickerstreak@73 57 -- localize some of these for small OnUpdate performance boost
flickerstreak@73 58 local Bar = ReAction.Bar.prototype
flickerstreak@73 59 local GetSize = Bar.GetSize
flickerstreak@73 60 local GetButtonSize = Bar.GetButtonSize
flickerstreak@73 61 local GetButtonGrid = Bar.GetButtonGrid
flickerstreak@73 62 local SetSize = Bar.SetSize
flickerstreak@73 63 local SetButtonSize = Bar.SetButtonSize
flickerstreak@73 64 local SetButtonGrid = Bar.SetButtonGrid
flickerstreak@73 65 local ApplyAnchor = Bar.ApplyAnchor
flickerstreak@73 66
flickerstreak@73 67 local function StoreExtents(bar)
flickerstreak@75 68 local f = bar:GetFrame()
flickerstreak@73 69 local point, relativeTo, relativePoint, x, y = f:GetPoint(1)
flickerstreak@73 70 relativeTo = relativeTo or f:GetParent()
flickerstreak@73 71 local anchorTo
flickerstreak@73 72 for name, b in ReAction:IterateBars() do
flickerstreak@73 73 if b and b:GetFrame() == relativeTo then
flickerstreak@73 74 anchorTo = name
flickerstreak@73 75 break
flickerstreak@73 76 end
flickerstreak@73 77 end
flickerstreak@73 78 anchorTo = anchorTo or relativeTo:GetName()
flickerstreak@73 79 local c = bar.config
flickerstreak@73 80 c.anchor = point
flickerstreak@73 81 c.anchorTo = anchorTo
flickerstreak@73 82 c.relativePoint = relativePoint
flickerstreak@73 83 c.x = x
flickerstreak@73 84 c.y = y
flickerstreak@73 85 c.width, c.height = f:GetWidth(), f:GetHeight()
flickerstreak@73 86 end
flickerstreak@73 87
flickerstreak@73 88 local function StoreSize(bar)
flickerstreak@75 89 local f = bar:GetFrame()
flickerstreak@73 90 local c = bar.config
flickerstreak@73 91 c.width, c.height = f:GetWidth(), f:GetHeight()
flickerstreak@73 92 end
flickerstreak@73 93
flickerstreak@73 94 local function RecomputeButtonSize(bar)
flickerstreak@73 95 local w, h = GetSize(bar)
flickerstreak@73 96 local bw, bh = GetButtonSize(bar)
flickerstreak@73 97 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 98
flickerstreak@73 99 local scaleW = (floor(w/c) - s) / bw
flickerstreak@73 100 local scaleH = (floor(h/r) - s) / bh
flickerstreak@73 101 local scale = min(scaleW, scaleH)
flickerstreak@73 102
flickerstreak@73 103 SetButtonSize(bar, scale * bw, scale * bh, s)
flickerstreak@73 104 end
flickerstreak@73 105
flickerstreak@73 106 local function RecomputeButtonSpacing(bar)
flickerstreak@73 107 local w, h = GetSize(bar)
flickerstreak@73 108 local bw, bh = GetButtonSize(bar)
flickerstreak@73 109 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 110
flickerstreak@73 111 SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh))
flickerstreak@73 112 end
flickerstreak@73 113
flickerstreak@73 114 local function RecomputeGrid(bar)
flickerstreak@73 115 local w, h = GetSize(bar)
flickerstreak@73 116 local bw, bh = GetButtonSize(bar)
flickerstreak@73 117 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 118
flickerstreak@73 119 SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s)
flickerstreak@73 120 end
flickerstreak@73 121
flickerstreak@73 122 local function ClampToButtons(bar)
flickerstreak@73 123 local bw, bh = GetButtonSize(bar)
flickerstreak@73 124 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 125 SetSize(bar, (bw+s)*c + 1, (bh+s)*r + 1)
flickerstreak@73 126 end
flickerstreak@73 127
flickerstreak@73 128 local function HideGameTooltip()
flickerstreak@73 129 GameTooltip:Hide()
flickerstreak@73 130 end
flickerstreak@73 131
flickerstreak@73 132 local anchorInside = { inside = true }
flickerstreak@73 133 local anchorOutside = { outside = true }
flickerstreak@73 134 local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" }
flickerstreak@73 135 local oppositeEdges = {
flickerstreak@73 136 TOP = "BOTTOM",
flickerstreak@73 137 BOTTOM = "TOP",
flickerstreak@73 138 LEFT = "RIGHT",
flickerstreak@73 139 RIGHT = "LEFT"
flickerstreak@73 140 }
flickerstreak@73 141 local pointsOnEdge = {
flickerstreak@73 142 BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", },
flickerstreak@73 143 TOP = { "TOP", "TOPLEFT", "TOPRIGHT", },
flickerstreak@73 144 RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", },
flickerstreak@73 145 LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", },
flickerstreak@73 146 }
flickerstreak@73 147 local edgeSelector = {
flickerstreak@73 148 BOTTOM = 1, -- select x of x,y
flickerstreak@73 149 TOP = 1, -- select x of x,y
flickerstreak@73 150 LEFT = 2, -- select y of x,y
flickerstreak@73 151 RIGHT = 2, -- select y of x,y
flickerstreak@73 152 }
flickerstreak@73 153 local snapPoints = {
flickerstreak@73 154 [anchorOutside] = {
flickerstreak@73 155 BOTTOMLEFT = {"BOTTOMRIGHT","TOPLEFT","TOPRIGHT"},
flickerstreak@73 156 BOTTOM = {"TOP"},
flickerstreak@73 157 BOTTOMRIGHT = {"BOTTOMLEFT","TOPRIGHT","TOPLEFT"},
flickerstreak@73 158 RIGHT = {"LEFT"},
flickerstreak@73 159 TOPRIGHT = {"TOPLEFT","BOTTOMRIGHT","BOTTOMLEFT"},
flickerstreak@73 160 TOP = {"BOTTOM"},
flickerstreak@73 161 TOPLEFT = {"TOPRIGHT","BOTTOMLEFT","BOTTOMRIGHT"},
flickerstreak@73 162 LEFT = {"RIGHT"},
flickerstreak@73 163 CENTER = {"CENTER"}
flickerstreak@73 164 },
flickerstreak@73 165 [anchorInside] = {
flickerstreak@73 166 BOTTOMLEFT = {"BOTTOMLEFT"},
flickerstreak@73 167 BOTTOM = {"BOTTOM"},
flickerstreak@73 168 BOTTOMRIGHT = {"BOTTOMRIGHT"},
flickerstreak@73 169 RIGHT = {"RIGHT"},
flickerstreak@73 170 TOPRIGHT = {"TOPRIGHT"},
flickerstreak@73 171 TOP = {"TOP"},
flickerstreak@73 172 TOPLEFT = {"TOPLEFT"},
flickerstreak@73 173 LEFT = {"LEFT"},
flickerstreak@73 174 CENTER = {"CENTER"}
flickerstreak@73 175 }
flickerstreak@73 176 }
flickerstreak@73 177 local insidePointOffsetFuncs = {
flickerstreak@73 178 BOTTOMLEFT = function(x, y) return x, y end,
flickerstreak@73 179 BOTTOM = function(x, y) return 0, y end,
flickerstreak@73 180 BOTTOMRIGHT = function(x, y) return -x, y end,
flickerstreak@73 181 RIGHT = function(x, y) return -x, 0 end,
flickerstreak@73 182 TOPRIGHT = function(x, y) return -x, -y end,
flickerstreak@73 183 TOP = function(x, y) return 0, -y end,
flickerstreak@73 184 TOPLEFT = function(x, y) return x, -y end,
flickerstreak@73 185 LEFT = function(x, y) return x, 0 end,
flickerstreak@73 186 CENTER = function(x, y) return 0, 0 end,
flickerstreak@73 187 }
flickerstreak@73 188 local pointCoordFuncs = {
flickerstreak@73 189 BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end,
flickerstreak@73 190 BOTTOM = function(f) return nil, f:GetBottom() end,
flickerstreak@73 191 BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end,
flickerstreak@73 192 RIGHT = function(f) return f:GetRight(), nil end,
flickerstreak@73 193 TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end,
flickerstreak@73 194 TOP = function(f) return nil, f:GetTop() end,
flickerstreak@73 195 TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end,
flickerstreak@73 196 LEFT = function(f) return f:GetLeft(), nil end,
flickerstreak@73 197 CENTER = function(f) return f:GetCenter() end,
flickerstreak@73 198 }
flickerstreak@73 199 local edgeBoundsFuncs = {
flickerstreak@73 200 BOTTOM = function(f) return f:GetLeft(), f:GetRight() end,
flickerstreak@73 201 LEFT = function(f) return f:GetBottom(), f:GetTop() end
flickerstreak@73 202 }
flickerstreak@73 203 edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM
flickerstreak@73 204 edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT
flickerstreak@73 205
flickerstreak@73 206
flickerstreak@73 207 -- Returns absolute coordinates x,y of the named point 'p' of frame 'f'
flickerstreak@73 208 local function GetPointCoords( f, p )
flickerstreak@73 209 local x, y = pointCoordFuncs[p](f)
flickerstreak@73 210 if not(x and y) then
flickerstreak@73 211 local cx, cy = f:GetCenter()
flickerstreak@73 212 x = x or cx
flickerstreak@73 213 y = y or cy
flickerstreak@73 214 end
flickerstreak@73 215 return x, y
flickerstreak@73 216 end
flickerstreak@73 217
flickerstreak@73 218
flickerstreak@73 219 -- Returns true if frame 'f1' can be anchored to frame 'f2'
flickerstreak@73 220 local function CheckAnchorable( f1, f2 )
flickerstreak@73 221 -- can't anchor a frame to itself or to nil
flickerstreak@73 222 if f1 == f2 or f2 == nil then
flickerstreak@73 223 return false
flickerstreak@73 224 end
flickerstreak@73 225
flickerstreak@73 226 -- can always anchor to UIParent
flickerstreak@73 227 if f2 == UIParent then
flickerstreak@73 228 return true
flickerstreak@73 229 end
flickerstreak@73 230
flickerstreak@73 231 -- also can't do circular anchoring of frames
flickerstreak@73 232 -- walk the anchor chain, which generally shouldn't be that expensive
flickerstreak@73 233 -- (who nests draggables that deep anyway?)
flickerstreak@73 234 for i = 1, f2:GetNumPoints() do
flickerstreak@73 235 local _, f = f2:GetPoint(i)
flickerstreak@73 236 if not f then f = f2:GetParent() end
flickerstreak@73 237 return CheckAnchorable(f1,f)
flickerstreak@73 238 end
flickerstreak@73 239
flickerstreak@73 240 return true
flickerstreak@73 241 end
flickerstreak@73 242
flickerstreak@73 243 -- Returns true if frames f1 and f2 specified edges overlap
flickerstreak@73 244 local function CheckEdgeOverlap( f1, f2, e )
flickerstreak@73 245 local l1, u1 = edgeBoundsFuncs[e](f1)
flickerstreak@73 246 local l2, u2 = edgeBoundsFuncs[e](f2)
flickerstreak@73 247 return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2
flickerstreak@73 248 end
flickerstreak@73 249
flickerstreak@73 250 -- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2
flickerstreak@73 251 local function CheckPointEdgeOverlap( f1, p1, f2, e2 )
flickerstreak@73 252 local l, u = edgeBoundsFuncs[e2](f2)
flickerstreak@73 253 local x, y = GetPointCoords(f1,p1)
flickerstreak@73 254 x = select(edgeSelector[e2], x, y)
flickerstreak@73 255 return l <= x and x <= u
flickerstreak@73 256 end
flickerstreak@73 257
flickerstreak@73 258 -- Returns the distance between corresponding edges. It is
flickerstreak@73 259 -- assumed that the passed in edges e1 and e2 are the same or opposites
flickerstreak@73 260 local function GetEdgeDistance( f1, f2, e1, e2 )
flickerstreak@73 261 local x1, y1 = pointCoordFuncs[e1](f1)
flickerstreak@73 262 local x2, y2 = pointCoordFuncs[e2](f2)
flickerstreak@73 263 return math.abs((x1 or y1) - (x2 or y2))
flickerstreak@73 264 end
flickerstreak@73 265
flickerstreak@73 266 local globalSnapTargets = { [UIParent] = anchorInside }
flickerstreak@73 267
flickerstreak@73 268 local function GetClosestFrameEdge(f1,f2,a)
flickerstreak@73 269 local dist, edge, opp
flickerstreak@73 270 if f2:IsVisible() and CheckAnchorable(f1,f2) then
flickerstreak@73 271 for _, e in pairs(edges) do
flickerstreak@73 272 local o = a.inside and e or oppositeEdges[e]
flickerstreak@73 273 if CheckEdgeOverlap(f1,f2,e) then
flickerstreak@73 274 local d = GetEdgeDistance(f1, f2, e, o)
flickerstreak@73 275 if not dist or (d < dist) then
flickerstreak@73 276 dist, edge, opp = d, e, o
flickerstreak@73 277 end
flickerstreak@73 278 end
flickerstreak@73 279 end
flickerstreak@73 280 end
flickerstreak@73 281 return dist, edge, opp
flickerstreak@73 282 end
flickerstreak@73 283
flickerstreak@73 284 local function GetClosestVisibleEdge( f )
flickerstreak@73 285 local r, o, e1, e2
flickerstreak@73 286 local a = anchorOutside
flickerstreak@73 287 for _, b in ReAction:IterateBars() do
flickerstreak@73 288 local d, e, opp = GetClosestFrameEdge(f,b:GetFrame(),a)
flickerstreak@73 289 if d and (not r or d < r) then
flickerstreak@73 290 r, o, e1, e2 = d, b:GetFrame(), e, opp
flickerstreak@73 291 end
flickerstreak@73 292 end
flickerstreak@73 293 for f2, a2 in pairs(globalSnapTargets) do
flickerstreak@73 294 local d, e, opp = GetClosestFrameEdge(f,f2,a2)
flickerstreak@73 295 if d and (not r or d < r) then
flickerstreak@73 296 r, o, e1, e2, a = d, f2, e, opp, a2
flickerstreak@73 297 end
flickerstreak@73 298 end
flickerstreak@73 299 return o, e1, e2, a
flickerstreak@73 300 end
flickerstreak@73 301
flickerstreak@73 302 local function GetClosestVisiblePoint(f1)
flickerstreak@73 303 local f2, e1, e2, a = GetClosestVisibleEdge(f1)
flickerstreak@73 304 if f2 then
flickerstreak@73 305 local rsq, p, rp, x, y
flickerstreak@73 306 -- iterate pointsOnEdge in order and use < to prefer edge centers to corners
flickerstreak@73 307 for _, p1 in ipairs(pointsOnEdge[e1]) do
flickerstreak@73 308 if CheckPointEdgeOverlap(f1,p1,f2,e2) then
flickerstreak@73 309 for _, p2 in pairs(snapPoints[a][p1]) do
flickerstreak@73 310 local x1, y1 = GetPointCoords(f1,p1)
flickerstreak@73 311 local x2, y2 = GetPointCoords(f2,p2)
flickerstreak@73 312 local dx = x1 - x2
flickerstreak@73 313 local dy = y1 - y2
flickerstreak@73 314 local rsq2 = dx*dx + dy*dy
flickerstreak@73 315 if not rsq or rsq2 < rsq then
flickerstreak@73 316 rsq, p, rp, x, y = rsq2, p1, p2, dx, dy
flickerstreak@73 317 end
flickerstreak@73 318 end
flickerstreak@73 319 end
flickerstreak@73 320 end
flickerstreak@73 321 return f2, p, rp, x, y
flickerstreak@73 322 end
flickerstreak@73 323 end
flickerstreak@73 324
flickerstreak@73 325 local function GetClosestPointSnapped(f1, rx, ry, xOff, yOff)
flickerstreak@73 326 local o, p, rp, x, y = GetClosestVisiblePoint(f1)
flickerstreak@73 327 local s = false
flickerstreak@73 328
flickerstreak@73 329 local sx, sy = insidePointOffsetFuncs[p](xOff or 0, yOff or 0)
flickerstreak@73 330 local xx, yy = pointCoordFuncs[p](f1)
flickerstreak@73 331 if xx and yy then
flickerstreak@73 332 if math.abs(x) <= rx then
flickerstreak@73 333 x = sx
flickerstreak@73 334 s = true
flickerstreak@73 335 end
flickerstreak@73 336 if math.abs(y) <= ry then
flickerstreak@73 337 y = sy
flickerstreak@73 338 s = true
flickerstreak@73 339 end
flickerstreak@73 340 elseif xx then
flickerstreak@73 341 if math.abs(x) <= rx then
flickerstreak@73 342 x = sx
flickerstreak@73 343 s = true
flickerstreak@73 344 if math.abs(y) <= ry then
flickerstreak@73 345 y = sy
flickerstreak@73 346 end
flickerstreak@73 347 end
flickerstreak@73 348 elseif yy then
flickerstreak@73 349 if math.abs(y) <= ry then
flickerstreak@73 350 y = sy
flickerstreak@73 351 s = true
flickerstreak@73 352 if math.abs(x) <= rx then
flickerstreak@73 353 x = sx
flickerstreak@73 354 end
flickerstreak@73 355 end
flickerstreak@73 356 end
flickerstreak@73 357
flickerstreak@73 358 if x == -0 then x = 0 end
flickerstreak@73 359 if y == -0 then y = 0 end
flickerstreak@73 360
flickerstreak@73 361 if s then
flickerstreak@73 362 return o, p, rp, math.floor(x), math.floor(y)
flickerstreak@73 363 end
flickerstreak@73 364 end
flickerstreak@73 365
flickerstreak@73 366 local function CreateSnapIndicator()
flickerstreak@73 367 local si = CreateFrame("Frame",nil,UIParent)
flickerstreak@73 368 si:SetFrameStrata("HIGH")
flickerstreak@73 369 si:SetHeight(8)
flickerstreak@73 370 si:SetWidth(8)
flickerstreak@73 371 local tex = si:CreateTexture()
flickerstreak@73 372 tex:SetAllPoints()
flickerstreak@73 373 tex:SetTexture(1.0, 0.82, 0, 0.8)
flickerstreak@73 374 tex:SetBlendMode("ADD")
flickerstreak@73 375 tex:SetDrawLayer("OVERLAY")
flickerstreak@73 376 return si
flickerstreak@73 377 end
flickerstreak@73 378
flickerstreak@73 379 local si1 = CreateSnapIndicator()
flickerstreak@73 380 local si2 = CreateSnapIndicator()
flickerstreak@73 381
flickerstreak@73 382 local function DisplaySnapIndicator( f, rx, ry, xOff, yOff )
flickerstreak@73 383 local o, p, rp, x, y, snap = GetClosestPointSnapped(f, rx, ry, xOff, yOff)
flickerstreak@73 384 if o then
flickerstreak@73 385 si1:ClearAllPoints()
flickerstreak@73 386 si2:ClearAllPoints()
flickerstreak@73 387 si1:SetPoint("CENTER", f, p, 0, 0)
flickerstreak@73 388 local xx, yy = pointCoordFuncs[rp](o)
flickerstreak@73 389 x = math.abs(x) <=rx and xx and 0 or x
flickerstreak@73 390 y = math.abs(y) <=ry and yy and 0 or y
flickerstreak@73 391 si2:SetPoint("CENTER", o, rp, x, y)
flickerstreak@73 392 si1:Show()
flickerstreak@73 393 si2:Show()
flickerstreak@73 394 else
flickerstreak@73 395 if si1:IsVisible() then
flickerstreak@73 396 si1:Hide()
flickerstreak@73 397 si2:Hide()
flickerstreak@73 398 end
flickerstreak@73 399 end
flickerstreak@73 400 end
flickerstreak@73 401
flickerstreak@73 402 local function HideSnapIndicator()
flickerstreak@73 403 if si1:IsVisible() then
flickerstreak@73 404 si1:Hide()
flickerstreak@73 405 si2:Hide()
flickerstreak@73 406 end
flickerstreak@73 407 end
flickerstreak@73 408
flickerstreak@73 409 local function CreateControls(bar)
flickerstreak@75 410 local f = bar:GetFrame()
flickerstreak@73 411
flickerstreak@73 412 f:SetMovable(true)
flickerstreak@73 413 f:SetResizable(true)
flickerstreak@73 414 f:SetClampedToScreen(true)
flickerstreak@73 415
flickerstreak@73 416 -- buttons on the bar should be direct children of the bar frame.
flickerstreak@73 417 -- The control elements need to float on top of this, which we could
flickerstreak@73 418 -- do with SetFrameLevel() or Raise(), but it's more reliable to do it
flickerstreak@73 419 -- via frame nesting, hence good old foo's appearance here.
flickerstreak@73 420 local foo = CreateFrame("Frame",nil,f)
flickerstreak@73 421 foo:SetAllPoints()
flickerstreak@73 422 foo:SetClampedToScreen(true)
flickerstreak@73 423
flickerstreak@73 424 local control = CreateFrame("Button", nil, foo)
flickerstreak@73 425 control:EnableMouse(true)
flickerstreak@73 426 control:SetToplevel(true)
flickerstreak@73 427 control:SetPoint("TOPLEFT", -4, 4)
flickerstreak@73 428 control:SetPoint("BOTTOMRIGHT", 4, -4)
flickerstreak@73 429 control:SetBackdrop({
flickerstreak@73 430 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
flickerstreak@73 431 tile = true,
flickerstreak@73 432 tileSize = 16,
flickerstreak@73 433 edgeSize = 16,
flickerstreak@73 434 insets = { left = 0, right = 0, top = 0, bottom = 0 },
flickerstreak@73 435 })
flickerstreak@73 436
flickerstreak@73 437 -- textures
flickerstreak@73 438 local bgTex = control:CreateTexture(nil,"BACKGROUND")
flickerstreak@73 439 bgTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@73 440 bgTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@73 441 bgTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@73 442 local hTex = control:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@73 443 hTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@73 444 hTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@73 445 hTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@73 446 hTex:SetBlendMode("ADD")
flickerstreak@73 447
flickerstreak@73 448 -- label
flickerstreak@73 449 local label = control:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@73 450 label:SetAllPoints()
flickerstreak@73 451 label:SetJustifyH("CENTER")
flickerstreak@73 452 label:SetShadowColor(0,0,0,1)
flickerstreak@73 453 label:SetShadowOffset(2,-2)
flickerstreak@73 454 label:SetTextColor(1,1,1,1)
flickerstreak@73 455 label:SetText(bar:GetName())
flickerstreak@73 456 label:Show()
flickerstreak@73 457 bar.controlLabelString = label -- so that bar:SetName() can update it
flickerstreak@73 458
flickerstreak@73 459 local function StopResize()
flickerstreak@73 460 f:StopMovingOrSizing()
flickerstreak@73 461 f.isMoving = false
flickerstreak@73 462 f:SetScript("OnUpdate",nil)
flickerstreak@73 463 StoreSize(bar)
flickerstreak@73 464 ClampToButtons(bar)
flickerstreak@73 465 ApplyAnchor(bar)
flickerstreak@73 466 ReAction:RefreshOptions()
flickerstreak@73 467 end
flickerstreak@73 468
flickerstreak@73 469 -- edge drag handles
flickerstreak@73 470 for _, point in pairs({"LEFT","TOP","RIGHT","BOTTOM"}) do
flickerstreak@73 471 local edge = CreateFrame("Frame",nil,control)
flickerstreak@73 472 edge:EnableMouse(true)
flickerstreak@73 473 edge:SetWidth(8)
flickerstreak@73 474 edge:SetHeight(8)
flickerstreak@73 475 if point == "TOP" or point == "BOTTOM" then
flickerstreak@73 476 edge:SetPoint(point.."LEFT")
flickerstreak@73 477 edge:SetPoint(point.."RIGHT")
flickerstreak@73 478 else
flickerstreak@73 479 edge:SetPoint("TOP"..point)
flickerstreak@73 480 edge:SetPoint("BOTTOM"..point)
flickerstreak@73 481 end
flickerstreak@73 482 local tex = edge:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@73 483 tex:SetTexture(1.0,0.82,0,0.7)
flickerstreak@73 484 tex:SetBlendMode("ADD")
flickerstreak@73 485 tex:SetAllPoints()
flickerstreak@73 486 edge:RegisterForDrag("LeftButton")
flickerstreak@73 487 edge:SetScript("OnMouseDown",
flickerstreak@73 488 function()
flickerstreak@73 489 local bw, bh = GetButtonSize(bar)
flickerstreak@73 490 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 491 f:SetMinResize( bw+s+1, bh+s+1 )
flickerstreak@73 492 f:StartSizing(point)
flickerstreak@73 493 f:SetScript("OnUpdate",
flickerstreak@73 494 function()
flickerstreak@73 495 RecomputeGrid(bar)
flickerstreak@73 496 end
flickerstreak@73 497 )
flickerstreak@73 498 end
flickerstreak@73 499 )
flickerstreak@73 500 edge:SetScript("OnMouseUp", StopResize)
flickerstreak@73 501 edge:SetScript("OnEnter",
flickerstreak@73 502 function()
flickerstreak@73 503 GameTooltip:SetOwner(f, "ANCHOR_"..point)
flickerstreak@73 504 GameTooltip:AddLine(L["Drag to add/remove buttons"])
flickerstreak@73 505 GameTooltip:Show()
flickerstreak@73 506 end
flickerstreak@73 507 )
flickerstreak@73 508 edge:SetScript("OnLeave", HideGameTooltip)
flickerstreak@73 509 edge:Show()
flickerstreak@73 510 end
flickerstreak@73 511
flickerstreak@73 512 -- corner drag handles, again nested in an anonymous frame so that they are on top
flickerstreak@73 513 local foo2 = CreateFrame("Frame",nil,control)
flickerstreak@73 514 foo2:SetAllPoints(true)
flickerstreak@73 515 for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do
flickerstreak@73 516 local corner = CreateFrame("Frame",nil,foo2)
flickerstreak@73 517 corner:EnableMouse(true)
flickerstreak@73 518 corner:SetWidth(12)
flickerstreak@73 519 corner:SetHeight(12)
flickerstreak@73 520 corner:SetPoint(point)
flickerstreak@73 521 local tex = corner:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@73 522 tex:SetTexture(1.0,0.82,0,0.7)
flickerstreak@73 523 tex:SetBlendMode("ADD")
flickerstreak@73 524 tex:SetAllPoints()
flickerstreak@73 525 corner:RegisterForDrag("LeftButton","RightButton")
flickerstreak@73 526 local function updateTooltip()
flickerstreak@73 527 local size, size2 = bar:GetButtonSize()
flickerstreak@73 528 local rows, cols, spacing = bar:GetButtonGrid()
flickerstreak@73 529 size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
flickerstreak@73 530 GameTooltipTextRight4:SetText(size)
flickerstreak@73 531 GameTooltipTextRight5:SetText(tostring(spacing))
flickerstreak@73 532 end
flickerstreak@73 533 corner:SetScript("OnMouseDown",
flickerstreak@73 534 function(_,btn)
flickerstreak@73 535 local bw, bh = GetButtonSize(bar)
flickerstreak@73 536 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 537 if btn == "LeftButton" then -- button resize
flickerstreak@73 538 f:SetMinResize( (s+12)*c+1, (s+12)*r+1 )
flickerstreak@73 539 f:SetScript("OnUpdate",
flickerstreak@73 540 function()
flickerstreak@73 541 RecomputeButtonSize(bar)
flickerstreak@73 542 updateTooltip()
flickerstreak@73 543 end
flickerstreak@73 544 )
flickerstreak@73 545 elseif btn == "RightButton" then -- spacing resize
flickerstreak@73 546 f:SetMinResize( bw*c, bh*r )
flickerstreak@73 547 f:SetScript("OnUpdate",
flickerstreak@73 548 function()
flickerstreak@73 549 RecomputeButtonSpacing(bar)
flickerstreak@73 550 updateTooltip()
flickerstreak@73 551 end
flickerstreak@73 552 )
flickerstreak@73 553 end
flickerstreak@73 554 f:StartSizing(point)
flickerstreak@73 555 end
flickerstreak@73 556 )
flickerstreak@73 557 corner:SetScript("OnMouseUp",StopResize)
flickerstreak@73 558 corner:SetScript("OnEnter",
flickerstreak@73 559 function()
flickerstreak@73 560 GameTooltip:SetOwner(f, "ANCHOR_"..point)
flickerstreak@73 561 GameTooltip:AddLine(L["Drag to resize buttons"])
flickerstreak@73 562 GameTooltip:AddLine(L["Right-click-drag"])
flickerstreak@73 563 GameTooltip:AddLine(L["to change spacing"])
flickerstreak@73 564 local size, size2 = bar:GetButtonSize()
flickerstreak@73 565 local rows, cols, spacing = bar:GetButtonGrid()
flickerstreak@73 566 size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
flickerstreak@73 567 GameTooltip:AddDoubleLine(L["Size:"], size)
flickerstreak@73 568 GameTooltip:AddDoubleLine(L["Spacing:"], tostring(spacing))
flickerstreak@73 569 GameTooltip:Show()
flickerstreak@73 570 end
flickerstreak@73 571 )
flickerstreak@73 572 corner:SetScript("OnLeave",
flickerstreak@73 573 function()
flickerstreak@73 574 GameTooltip:Hide()
flickerstreak@73 575 f:SetScript("OnUpdate",nil)
flickerstreak@73 576 end
flickerstreak@73 577 )
flickerstreak@73 578
flickerstreak@73 579 end
flickerstreak@73 580
flickerstreak@73 581 control:RegisterForDrag("LeftButton")
flickerstreak@73 582 control:RegisterForClicks("RightButtonUp")
flickerstreak@73 583
flickerstreak@73 584 control:SetScript("OnDragStart",
flickerstreak@73 585 function()
flickerstreak@73 586 f:StartMoving()
flickerstreak@73 587 f.isMoving = true
flickerstreak@73 588 local w,h = bar:GetButtonSize()
flickerstreak@73 589 f:ClearAllPoints()
flickerstreak@73 590 f:SetScript("OnUpdate", function()
flickerstreak@73 591 if IsShiftKeyDown() then
flickerstreak@73 592 DisplaySnapIndicator(f,w,h)
flickerstreak@73 593 else
flickerstreak@73 594 HideSnapIndicator()
flickerstreak@73 595 end
flickerstreak@73 596 end)
flickerstreak@73 597 end
flickerstreak@73 598 )
flickerstreak@73 599
flickerstreak@73 600 local function updateDragTooltip()
flickerstreak@73 601 GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT")
flickerstreak@73 602 GameTooltip:AddLine(bar.name)
flickerstreak@73 603 GameTooltip:AddLine(L["Drag to move"])
flickerstreak@73 604 GameTooltip:AddLine(("|cff00ff00%s|r %s"):format(L["Shift-drag"],L["to anchor to nearby frames"]))
flickerstreak@73 605 GameTooltip:AddLine(("|cff00cccc%s|r %s"):format(L["Right-click"],L["for options"]))
flickerstreak@73 606 local _, a = bar:GetAnchor()
flickerstreak@73 607 if a and a ~= "UIParent" then
flickerstreak@73 608 GameTooltip:AddLine(L["Currently anchored to <%s>"]:format(a))
flickerstreak@73 609 end
flickerstreak@73 610 GameTooltip:Show()
flickerstreak@73 611 end
flickerstreak@73 612
flickerstreak@73 613 control:SetScript("OnDragStop",
flickerstreak@73 614 function()
flickerstreak@73 615 f:StopMovingOrSizing()
flickerstreak@73 616 f.isMoving = false
flickerstreak@73 617 f:SetScript("OnUpdate",nil)
flickerstreak@73 618
flickerstreak@73 619 if IsShiftKeyDown() then
flickerstreak@73 620 local w, h = bar:GetButtonSize()
flickerstreak@73 621 local a, p, rp, x, y = GetClosestPointSnapped(f,w,h)
flickerstreak@73 622 if a then
flickerstreak@73 623 f:ClearAllPoints()
flickerstreak@73 624 f:SetPoint(p,a,rp,x,y)
flickerstreak@73 625 end
flickerstreak@73 626 HideSnapIndicator()
flickerstreak@73 627 end
flickerstreak@73 628
flickerstreak@73 629 StoreExtents(bar)
flickerstreak@73 630 ReAction:RefreshOptions()
flickerstreak@73 631 updateDragTooltip()
flickerstreak@73 632 end
flickerstreak@73 633 )
flickerstreak@73 634
flickerstreak@73 635 control:SetScript("OnEnter",
flickerstreak@73 636 function()
flickerstreak@73 637 -- TODO: add bar type and status information to name
flickerstreak@73 638 --[[
flickerstreak@73 639 local name = bar.name
flickerstreak@73 640 for _, m in ReAction:IterateModules() do
flickerstreak@73 641 local suffix = safecall(m,"GetBarNameModifier",bar)
flickerstreak@73 642 if suffix then
flickerstreak@73 643 name = ("%s %s"):format(name,suffix)
flickerstreak@73 644 end
flickerstreak@73 645 end
flickerstreak@73 646 ]]--
flickerstreak@73 647
flickerstreak@73 648 updateDragTooltip()
flickerstreak@73 649 end
flickerstreak@73 650 )
flickerstreak@73 651
flickerstreak@73 652 control:SetScript("OnLeave", HideGameTooltip)
flickerstreak@73 653
flickerstreak@73 654 control:SetScript("OnClick",
flickerstreak@73 655 function()
flickerstreak@73 656 ShowMenu(bar)
flickerstreak@73 657 end
flickerstreak@73 658 )
flickerstreak@73 659
flickerstreak@73 660 return control
flickerstreak@73 661 end
flickerstreak@73 662
flickerstreak@73 663
flickerstreak@73 664 -- export the ShowControls method to the Bar prototype
flickerstreak@73 665
flickerstreak@73 666 function Bar:ShowControls(show)
flickerstreak@73 667 if show then
flickerstreak@73 668 if not self.controlFrame then
flickerstreak@73 669 self.controlFrame = CreateControls(self)
flickerstreak@73 670 end
flickerstreak@73 671 self.controlFrame:Show()
flickerstreak@73 672 elseif self.controlFrame then
flickerstreak@73 673 CloseMenu(self.controlFrame)
flickerstreak@73 674 self.controlFrame:Hide()
flickerstreak@73 675 end
flickerstreak@73 676 end
flickerstreak@73 677
flickerstreak@73 678