annotate Bar.lua @ 63:768be7eb22a0

Converted several ReAction APIs to event-driven model instead of 'call-method-on-all-modules' model. Cleaned up a number of other architectural issues.
author Flick <flickerstreak@gmail.com>
date Thu, 22 May 2008 22:02:08 +0000
parents 20003239af0b
children fcb5dad031f9
rev   line source
flickerstreak@25 1 local ReAction = ReAction
flickerstreak@25 2 local L = ReAction.L
flickerstreak@25 3 local _G = _G
flickerstreak@25 4 local CreateFrame = CreateFrame
flickerstreak@33 5 local InCombatLockdown = InCombatLockdown
flickerstreak@33 6 local floor = math.floor
flickerstreak@33 7 local min = math.min
flickerstreak@33 8 local format = string.format
flickerstreak@33 9 local GameTooltip = GameTooltip
flickerstreak@33 10
flickerstreak@33 11
flickerstreak@25 12
flickerstreak@25 13 -- update ReAction revision if this file is newer
flickerstreak@33 14 local revision = tonumber(("$Revision$"):match("%d+"))
flickerstreak@25 15 if revision > ReAction.revision then
flickerstreak@52 16 ReAction.revision = revision
flickerstreak@25 17 end
flickerstreak@25 18
flickerstreak@28 19 ------ BAR CLASS ------
flickerstreak@28 20 local Bar = { _classID = {} }
flickerstreak@25 21
flickerstreak@28 22 local function Constructor( self, name, config )
flickerstreak@25 23 self.name, self.config = name, config
flickerstreak@25 24
flickerstreak@25 25 if type(config) ~= "table" then
flickerstreak@28 26 error("ReAction.Bar: config table required")
flickerstreak@25 27 end
flickerstreak@25 28
flickerstreak@54 29 local parent = config.parent and (ReAction:GetBar(config.parent) or _G[config.parent]) or UIParent
flickerstreak@54 30 local f = CreateFrame("Frame",nil,parent,"SecureStateDriverTemplate")
flickerstreak@25 31 f:SetFrameStrata("MEDIUM")
flickerstreak@30 32 config.width = config.width or 480
flickerstreak@30 33 config.height = config.height or 40
flickerstreak@25 34 f:SetWidth(config.width)
flickerstreak@25 35 f:SetWidth(config.height)
flickerstreak@25 36
flickerstreak@63 37 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@63 38
flickerstreak@25 39 self.frame = f
flickerstreak@25 40 self:RefreshLayout()
flickerstreak@25 41 self:ApplyAnchor()
flickerstreak@25 42 f:Show()
flickerstreak@25 43 end
flickerstreak@25 44
flickerstreak@25 45 function Bar:Destroy()
flickerstreak@25 46 local f = self.frame
flickerstreak@25 47 f:UnregisterAllEvents()
flickerstreak@25 48 f:Hide()
flickerstreak@25 49 f:SetParent(UIParent)
flickerstreak@25 50 f:ClearAllPoints()
flickerstreak@63 51 ReAction.UnregisterAllCallbacks(self)
flickerstreak@25 52 self.labelString = nil
flickerstreak@25 53 self.controlFrame = nil
flickerstreak@25 54 self.frame = nil
flickerstreak@25 55 self.config = nil
flickerstreak@25 56 end
flickerstreak@25 57
flickerstreak@63 58 function Bar:OnConfigModeChanged(event, mode)
flickerstreak@63 59 self:ShowControls(mode)
flickerstreak@63 60 end
flickerstreak@63 61
flickerstreak@25 62 function Bar:RefreshLayout()
flickerstreak@63 63 ReAction:RefreshBar(self)
flickerstreak@25 64 end
flickerstreak@25 65
flickerstreak@25 66 function Bar:ApplyAnchor()
flickerstreak@25 67 local f, config = self.frame, self.config
flickerstreak@25 68 f:SetWidth(config.width)
flickerstreak@25 69 f:SetHeight(config.height)
flickerstreak@25 70 local anchor = config.anchor
flickerstreak@51 71 f:ClearAllPoints()
flickerstreak@25 72 if anchor then
flickerstreak@52 73 local anchorTo = f:GetParent()
flickerstreak@25 74 if config.anchorTo then
flickerstreak@52 75 local bar = ReAction:GetBar(config.anchorTo)
flickerstreak@52 76 if bar then
flickerstreak@52 77 anchorTo = bar:GetFrame()
flickerstreak@52 78 else
flickerstreak@52 79 anchorTo = _G[config.anchorTo]
flickerstreak@52 80 end
flickerstreak@25 81 end
flickerstreak@52 82 f:SetPoint(anchor, anchorTo or f:GetParent(), config.relativePoint, config.x or 0, config.y or 0)
flickerstreak@25 83 else
flickerstreak@25 84 f:SetPoint("CENTER")
flickerstreak@25 85 end
flickerstreak@25 86 end
flickerstreak@25 87
flickerstreak@51 88 function Bar:SetAnchor(point, frame, relativePoint, x, y)
flickerstreak@51 89 local c = self.config
flickerstreak@51 90 c.anchor = point or c.anchor
flickerstreak@51 91 c.anchorTo = frame and frame:GetName() or c.anchorTo
flickerstreak@51 92 c.relativePoint = relativePoint or c.relativePoint
flickerstreak@51 93 c.x = x or c.x
flickerstreak@51 94 c.y = y or c.y
flickerstreak@51 95 self:ApplyAnchor()
flickerstreak@51 96 end
flickerstreak@51 97
flickerstreak@51 98 function Bar:GetAnchor()
flickerstreak@51 99 local c = self.config
flickerstreak@51 100 return (c.anchor or "CENTER"), (c.anchorTo or self.frame:GetParent():GetName()), (c.relativePoint or c.anchor or "CENTER"), (c.x or 0), (c.y or 0)
flickerstreak@51 101 end
flickerstreak@51 102
flickerstreak@25 103 function Bar:GetFrame()
flickerstreak@25 104 return self.frame
flickerstreak@25 105 end
flickerstreak@25 106
flickerstreak@25 107 function Bar:GetSize()
flickerstreak@25 108 return self.frame:GetWidth() or 200, self.frame:GetHeight() or 200
flickerstreak@25 109 end
flickerstreak@25 110
flickerstreak@25 111 function Bar:SetSize(w,h)
flickerstreak@25 112 self.config.width = w
flickerstreak@25 113 self.config.height = h
flickerstreak@25 114 end
flickerstreak@25 115
flickerstreak@25 116 function Bar:GetButtonSize()
flickerstreak@25 117 local w = self.config.btnWidth or 32
flickerstreak@25 118 local h = self.config.btnHeight or 32
flickerstreak@25 119 -- TODO: get from modules?
flickerstreak@25 120 return w,h
flickerstreak@25 121 end
flickerstreak@25 122
flickerstreak@25 123 function Bar:SetButtonSize(w,h)
flickerstreak@25 124 if w > 0 and h > 0 then
flickerstreak@25 125 self.config.btnWidth = w
flickerstreak@25 126 self.config.btnHeight = h
flickerstreak@25 127 end
flickerstreak@25 128 end
flickerstreak@25 129
flickerstreak@25 130 function Bar:GetButtonGrid()
flickerstreak@25 131 local cfg = self.config
flickerstreak@25 132 local r = cfg.btnRows or 1
flickerstreak@25 133 local c = cfg.btnColumns or 1
flickerstreak@25 134 local s = cfg.spacing or 4
flickerstreak@25 135 return r,c,s
flickerstreak@25 136 end
flickerstreak@25 137
flickerstreak@25 138 function Bar:SetButtonGrid(r,c,s)
flickerstreak@25 139 if r > 0 and c > 0 and s > 0 then
flickerstreak@25 140 local cfg = self.config
flickerstreak@25 141 cfg.btnRows = r
flickerstreak@25 142 cfg.btnColumns = c
flickerstreak@25 143 cfg.spacing = s
flickerstreak@25 144 end
flickerstreak@25 145 end
flickerstreak@25 146
flickerstreak@25 147 function Bar:GetName()
flickerstreak@25 148 return self.name
flickerstreak@25 149 end
flickerstreak@25 150
flickerstreak@33 151 function Bar:SetName(name)
flickerstreak@33 152 self.name = name
flickerstreak@33 153 if self.controlLabelString then
flickerstreak@33 154 self.controlLabelString:SetText(self.name)
flickerstreak@33 155 end
flickerstreak@33 156 end
flickerstreak@33 157
flickerstreak@25 158 function Bar:PlaceButton(f, idx, baseW, baseH)
flickerstreak@25 159 local r, c, s = self:GetButtonGrid()
flickerstreak@25 160 local bh, bw = self:GetButtonSize()
flickerstreak@25 161 local row, col = floor((idx-1)/c), mod((idx-1),c) -- zero-based
flickerstreak@25 162 local x, y = col*bw + (col+0.5)*s, row*bh + (row+0.5)*s
flickerstreak@25 163 local scale = bw/baseW
flickerstreak@25 164
flickerstreak@25 165 f:ClearAllPoints()
flickerstreak@25 166 f:SetPoint("TOPLEFT",x/scale,-y/scale)
flickerstreak@25 167 f:SetScale(scale)
flickerstreak@25 168 end
flickerstreak@25 169
flickerstreak@28 170
flickerstreak@28 171
flickerstreak@33 172
flickerstreak@33 173
flickerstreak@33 174
flickerstreak@33 175
flickerstreak@33 176 --
flickerstreak@33 177 -- Bar config overlay
flickerstreak@33 178 --
flickerstreak@52 179 local CreateControls
flickerstreak@33 180
flickerstreak@33 181 do
flickerstreak@33 182 -- upvalue some of these for small OnUpdate performance boost
flickerstreak@33 183 local GetSize = Bar.GetSize
flickerstreak@33 184 local GetButtonSize = Bar.GetButtonSize
flickerstreak@33 185 local GetButtonGrid = Bar.GetButtonGrid
flickerstreak@33 186 local SetSize = Bar.SetSize
flickerstreak@33 187 local SetButtonSize = Bar.SetButtonSize
flickerstreak@33 188 local SetButtonGrid = Bar.SetButtonGrid
flickerstreak@33 189 local ApplyAnchor = Bar.ApplyAnchor
flickerstreak@33 190
flickerstreak@52 191 local function StoreExtents(bar)
flickerstreak@33 192 local f = bar.frame
flickerstreak@33 193 local point, relativeTo, relativePoint, x, y = f:GetPoint(1)
flickerstreak@33 194 relativeTo = relativeTo or f:GetParent()
flickerstreak@33 195 local anchorTo
flickerstreak@63 196 for name, b in ReAction:IterateBars() do
flickerstreak@52 197 if b and b:GetFrame() == relativeTo then
flickerstreak@52 198 anchorTo = name
flickerstreak@52 199 break
flickerstreak@33 200 end
flickerstreak@33 201 end
flickerstreak@33 202 anchorTo = anchorTo or relativeTo:GetName()
flickerstreak@33 203 local c = bar.config
flickerstreak@33 204 c.anchor = point
flickerstreak@33 205 c.anchorTo = anchorTo
flickerstreak@33 206 c.relativePoint = relativePoint
flickerstreak@33 207 c.x = x
flickerstreak@33 208 c.y = y
flickerstreak@33 209 c.width, c.height = f:GetWidth(), f:GetHeight()
flickerstreak@33 210 end
flickerstreak@33 211
flickerstreak@52 212 local function StoreSize(bar)
flickerstreak@52 213 local f = bar.frame
flickerstreak@52 214 local c = bar.config
flickerstreak@52 215 c.width, c.height = f:GetWidth(), f:GetHeight()
flickerstreak@52 216 end
flickerstreak@52 217
flickerstreak@52 218 local function RecomputeButtonSize(bar)
flickerstreak@33 219 local w, h = GetSize(bar)
flickerstreak@33 220 local bw, bh = GetButtonSize(bar)
flickerstreak@33 221 local r, c, s = GetButtonGrid(bar)
flickerstreak@33 222
flickerstreak@33 223 local scaleW = (floor(w/c) - s) / bw
flickerstreak@33 224 local scaleH = (floor(h/r) - s) / bh
flickerstreak@33 225 local scale = min(scaleW, scaleH)
flickerstreak@33 226
flickerstreak@33 227 SetButtonSize(bar, scale * bw, scale * bh, s)
flickerstreak@33 228 end
flickerstreak@33 229
flickerstreak@52 230 local function RecomputeButtonSpacing(bar)
flickerstreak@33 231 local w, h = GetSize(bar)
flickerstreak@33 232 local bw, bh = GetButtonSize(bar)
flickerstreak@33 233 local r, c, s = GetButtonGrid(bar)
flickerstreak@33 234
flickerstreak@33 235 SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh))
flickerstreak@33 236 end
flickerstreak@33 237
flickerstreak@52 238 local function RecomputeGrid(bar)
flickerstreak@33 239 local w, h = GetSize(bar)
flickerstreak@33 240 local bw, bh = GetButtonSize(bar)
flickerstreak@33 241 local r, c, s = GetButtonGrid(bar)
flickerstreak@33 242
flickerstreak@33 243 SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s)
flickerstreak@33 244 end
flickerstreak@33 245
flickerstreak@52 246 local function ClampToButtons(bar)
flickerstreak@33 247 local bw, bh = GetButtonSize(bar)
flickerstreak@33 248 local r, c, s = GetButtonGrid(bar)
flickerstreak@50 249 SetSize(bar, (bw+s)*c + 1, (bh+s)*r + 1)
flickerstreak@33 250 end
flickerstreak@33 251
flickerstreak@52 252 local function HideGameTooltip()
flickerstreak@33 253 GameTooltip:Hide()
flickerstreak@33 254 end
flickerstreak@33 255
flickerstreak@52 256 local anchorInside = { inside = true }
flickerstreak@52 257 local anchorOutside = { outside = true }
flickerstreak@52 258 local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" }
flickerstreak@52 259 local oppositeEdges = {
flickerstreak@52 260 TOP = "BOTTOM",
flickerstreak@52 261 BOTTOM = "TOP",
flickerstreak@52 262 LEFT = "RIGHT",
flickerstreak@52 263 RIGHT = "LEFT"
flickerstreak@52 264 }
flickerstreak@52 265 local pointsOnEdge = {
flickerstreak@52 266 BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", },
flickerstreak@52 267 TOP = { "TOP", "TOPLEFT", "TOPRIGHT", },
flickerstreak@52 268 RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", },
flickerstreak@52 269 LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", },
flickerstreak@52 270 }
flickerstreak@52 271 local edgeSelector = {
flickerstreak@52 272 BOTTOM = 1, -- select x of x,y
flickerstreak@52 273 TOP = 1, -- select x of x,y
flickerstreak@52 274 LEFT = 2, -- select y of x,y
flickerstreak@52 275 RIGHT = 2, -- select y of x,y
flickerstreak@52 276 }
flickerstreak@52 277 local snapPoints = {
flickerstreak@52 278 [anchorOutside] = {
flickerstreak@52 279 BOTTOMLEFT = {"BOTTOMRIGHT","TOPLEFT","TOPRIGHT"},
flickerstreak@52 280 BOTTOM = {"TOP"},
flickerstreak@52 281 BOTTOMRIGHT = {"BOTTOMLEFT","TOPRIGHT","TOPLEFT"},
flickerstreak@52 282 RIGHT = {"LEFT"},
flickerstreak@52 283 TOPRIGHT = {"TOPLEFT","BOTTOMRIGHT","BOTTOMLEFT"},
flickerstreak@52 284 TOP = {"BOTTOM"},
flickerstreak@52 285 TOPLEFT = {"TOPRIGHT","BOTTOMLEFT","BOTTOMRIGHT"},
flickerstreak@52 286 LEFT = {"RIGHT"},
flickerstreak@52 287 CENTER = {"CENTER"}
flickerstreak@52 288 },
flickerstreak@52 289 [anchorInside] = {
flickerstreak@52 290 BOTTOMLEFT = {"BOTTOMLEFT"},
flickerstreak@52 291 BOTTOM = {"BOTTOM"},
flickerstreak@52 292 BOTTOMRIGHT = {"BOTTOMRIGHT"},
flickerstreak@52 293 RIGHT = {"RIGHT"},
flickerstreak@52 294 TOPRIGHT = {"TOPRIGHT"},
flickerstreak@52 295 TOP = {"TOP"},
flickerstreak@52 296 TOPLEFT = {"TOPLEFT"},
flickerstreak@52 297 LEFT = {"LEFT"},
flickerstreak@52 298 CENTER = {"CENTER"}
flickerstreak@52 299 }
flickerstreak@52 300 }
flickerstreak@52 301 local insidePointOffsetFuncs = {
flickerstreak@52 302 BOTTOMLEFT = function(x, y) return x, y end,
flickerstreak@52 303 BOTTOM = function(x, y) return 0, y end,
flickerstreak@52 304 BOTTOMRIGHT = function(x, y) return -x, y end,
flickerstreak@52 305 RIGHT = function(x, y) return -x, 0 end,
flickerstreak@52 306 TOPRIGHT = function(x, y) return -x, -y end,
flickerstreak@52 307 TOP = function(x, y) return 0, -y end,
flickerstreak@52 308 TOPLEFT = function(x, y) return x, -y end,
flickerstreak@52 309 LEFT = function(x, y) return x, 0 end,
flickerstreak@52 310 CENTER = function(x, y) return 0, 0 end,
flickerstreak@52 311 }
flickerstreak@52 312 local pointCoordFuncs = {
flickerstreak@52 313 BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end,
flickerstreak@52 314 BOTTOM = function(f) return nil, f:GetBottom() end,
flickerstreak@52 315 BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end,
flickerstreak@52 316 RIGHT = function(f) return f:GetRight(), nil end,
flickerstreak@52 317 TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end,
flickerstreak@52 318 TOP = function(f) return nil, f:GetTop() end,
flickerstreak@52 319 TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end,
flickerstreak@52 320 LEFT = function(f) return f:GetLeft(), nil end,
flickerstreak@52 321 CENTER = function(f) return f:GetCenter() end,
flickerstreak@52 322 }
flickerstreak@52 323 local edgeBoundsFuncs = {
flickerstreak@52 324 BOTTOM = function(f) return f:GetLeft(), f:GetRight() end,
flickerstreak@52 325 LEFT = function(f) return f:GetBottom(), f:GetTop() end
flickerstreak@52 326 }
flickerstreak@52 327 edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM
flickerstreak@52 328 edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT
flickerstreak@52 329
flickerstreak@52 330
flickerstreak@52 331 -- Returns absolute coordinates x,y of the named point 'p' of frame 'f'
flickerstreak@52 332 local function GetPointCoords( f, p )
flickerstreak@52 333 local x, y = pointCoordFuncs[p](f)
flickerstreak@52 334 if not(x and y) then
flickerstreak@52 335 local cx, cy = f:GetCenter()
flickerstreak@52 336 x = x or cx
flickerstreak@52 337 y = y or cy
flickerstreak@52 338 end
flickerstreak@52 339 return x, y
flickerstreak@52 340 end
flickerstreak@52 341
flickerstreak@52 342
flickerstreak@52 343 -- Returns true if frame 'f1' can be anchored to frame 'f2'
flickerstreak@52 344 local function CheckAnchorable( f1, f2 )
flickerstreak@52 345 -- can't anchor a frame to itself or to nil
flickerstreak@52 346 if f1 == f2 or f2 == nil then
flickerstreak@52 347 return false
flickerstreak@52 348 end
flickerstreak@52 349
flickerstreak@52 350 -- can always anchor to UIParent
flickerstreak@52 351 if f2 == UIParent then
flickerstreak@52 352 return true
flickerstreak@52 353 end
flickerstreak@52 354
flickerstreak@52 355 -- also can't do circular anchoring of frames
flickerstreak@52 356 -- walk the anchor chain, which generally shouldn't be that expensive
flickerstreak@52 357 -- (who nests draggables that deep anyway?)
flickerstreak@52 358 for i = 1, f2:GetNumPoints() do
flickerstreak@52 359 local _, f = f2:GetPoint(i)
flickerstreak@52 360 if not f then f = f2:GetParent() end
flickerstreak@52 361 return CheckAnchorable(f1,f)
flickerstreak@52 362 end
flickerstreak@52 363
flickerstreak@52 364 return true
flickerstreak@52 365 end
flickerstreak@52 366
flickerstreak@52 367 -- Returns true if frames f1 and f2 specified edges overlap
flickerstreak@52 368 local function CheckEdgeOverlap( f1, f2, e )
flickerstreak@52 369 local l1, u1 = edgeBoundsFuncs[e](f1)
flickerstreak@52 370 local l2, u2 = edgeBoundsFuncs[e](f2)
flickerstreak@52 371 return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2
flickerstreak@52 372 end
flickerstreak@52 373
flickerstreak@52 374 -- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2
flickerstreak@52 375 local function CheckPointEdgeOverlap( f1, p1, f2, e2 )
flickerstreak@52 376 local l, u = edgeBoundsFuncs[e2](f2)
flickerstreak@52 377 local x, y = GetPointCoords(f1,p1)
flickerstreak@52 378 x = select(edgeSelector[e2], x, y)
flickerstreak@52 379 return l <= x and x <= u
flickerstreak@52 380 end
flickerstreak@52 381
flickerstreak@52 382 -- Returns the distance between corresponding edges. It is
flickerstreak@52 383 -- assumed that the passed in edges e1 and e2 are the same or opposites
flickerstreak@52 384 local function GetEdgeDistance( f1, f2, e1, e2 )
flickerstreak@52 385 local x1, y1 = pointCoordFuncs[e1](f1)
flickerstreak@52 386 local x2, y2 = pointCoordFuncs[e2](f2)
flickerstreak@52 387 return math.abs((x1 or y1) - (x2 or y2))
flickerstreak@52 388 end
flickerstreak@52 389
flickerstreak@52 390 local globalSnapTargets = { [UIParent] = anchorInside }
flickerstreak@52 391
flickerstreak@52 392 local function GetClosestFrameEdge(f1,f2,a)
flickerstreak@52 393 local dist, edge, opp
flickerstreak@52 394 if f2:IsVisible() and CheckAnchorable(f1,f2) then
flickerstreak@52 395 for _, e in pairs(edges) do
flickerstreak@52 396 local o = a.inside and e or oppositeEdges[e]
flickerstreak@52 397 if CheckEdgeOverlap(f1,f2,e) then
flickerstreak@52 398 local d = GetEdgeDistance(f1, f2, e, o)
flickerstreak@52 399 if not dist or (d < dist) then
flickerstreak@52 400 dist, edge, opp = d, e, o
flickerstreak@52 401 end
flickerstreak@52 402 end
flickerstreak@52 403 end
flickerstreak@52 404 end
flickerstreak@52 405 return dist, edge, opp
flickerstreak@52 406 end
flickerstreak@52 407
flickerstreak@52 408 local function GetClosestVisibleEdge( f )
flickerstreak@52 409 local r, o, e1, e2
flickerstreak@52 410 local a = anchorOutside
flickerstreak@63 411 for _, b in ReAction:IterateBars() do
flickerstreak@52 412 local d, e, opp = GetClosestFrameEdge(f,b:GetFrame(),a)
flickerstreak@52 413 if d and (not r or d < r) then
flickerstreak@52 414 r, o, e1, e2 = d, b:GetFrame(), e, opp
flickerstreak@52 415 end
flickerstreak@52 416 end
flickerstreak@52 417 for f2, a2 in pairs(globalSnapTargets) do
flickerstreak@52 418 local d, e, opp = GetClosestFrameEdge(f,f2,a2)
flickerstreak@52 419 if d and (not r or d < r) then
flickerstreak@52 420 r, o, e1, e2, a = d, f2, e, opp, a2
flickerstreak@52 421 end
flickerstreak@52 422 end
flickerstreak@52 423 return o, e1, e2, a
flickerstreak@52 424 end
flickerstreak@52 425
flickerstreak@52 426 local function GetClosestVisiblePoint(f1)
flickerstreak@52 427 local f2, e1, e2, a = GetClosestVisibleEdge(f1)
flickerstreak@52 428 if f2 then
flickerstreak@52 429 local rsq, p, rp, x, y
flickerstreak@52 430 -- iterate pointsOnEdge in order and use < to prefer edge centers to corners
flickerstreak@52 431 for _, p1 in ipairs(pointsOnEdge[e1]) do
flickerstreak@52 432 if CheckPointEdgeOverlap(f1,p1,f2,e2) then
flickerstreak@52 433 for _, p2 in pairs(snapPoints[a][p1]) do
flickerstreak@52 434 local x1, y1 = GetPointCoords(f1,p1)
flickerstreak@52 435 local x2, y2 = GetPointCoords(f2,p2)
flickerstreak@52 436 local dx = x1 - x2
flickerstreak@52 437 local dy = y1 - y2
flickerstreak@52 438 local rsq2 = dx*dx + dy*dy
flickerstreak@52 439 if not rsq or rsq2 < rsq then
flickerstreak@52 440 rsq, p, rp, x, y = rsq2, p1, p2, dx, dy
flickerstreak@52 441 end
flickerstreak@52 442 end
flickerstreak@52 443 end
flickerstreak@52 444 end
flickerstreak@52 445 return f2, p, rp, x, y
flickerstreak@52 446 end
flickerstreak@52 447 end
flickerstreak@52 448
flickerstreak@52 449 local function GetClosestPointSnapped(f1, rx, ry, xOff, yOff)
flickerstreak@52 450 local o, p, rp, x, y = GetClosestVisiblePoint(f1)
flickerstreak@52 451 local s = false
flickerstreak@52 452
flickerstreak@52 453 local sx, sy = insidePointOffsetFuncs[p](xOff or 0, yOff or 0)
flickerstreak@52 454 local xx, yy = pointCoordFuncs[p](f1)
flickerstreak@52 455 if xx and yy then
flickerstreak@52 456 if math.abs(x) <= rx then
flickerstreak@52 457 x = sx
flickerstreak@52 458 s = true
flickerstreak@52 459 end
flickerstreak@52 460 if math.abs(y) <= ry then
flickerstreak@52 461 y = sy
flickerstreak@52 462 s = true
flickerstreak@52 463 end
flickerstreak@52 464 elseif xx then
flickerstreak@52 465 if math.abs(x) <= rx then
flickerstreak@52 466 x = sx
flickerstreak@52 467 s = true
flickerstreak@52 468 if math.abs(y) <= ry then
flickerstreak@52 469 y = sy
flickerstreak@52 470 end
flickerstreak@52 471 end
flickerstreak@52 472 elseif yy then
flickerstreak@52 473 if math.abs(y) <= ry then
flickerstreak@52 474 y = sy
flickerstreak@52 475 s = true
flickerstreak@52 476 if math.abs(x) <= rx then
flickerstreak@52 477 x = sx
flickerstreak@52 478 end
flickerstreak@52 479 end
flickerstreak@52 480 end
flickerstreak@52 481
flickerstreak@52 482 if x == -0 then x = 0 end
flickerstreak@52 483 if y == -0 then y = 0 end
flickerstreak@52 484
flickerstreak@52 485 if s then
flickerstreak@52 486 return o, p, rp, math.floor(x), math.floor(y)
flickerstreak@52 487 end
flickerstreak@52 488 end
flickerstreak@52 489
flickerstreak@52 490 local function CreateSnapIndicator()
flickerstreak@52 491 local si = CreateFrame("Frame",nil,UIParent)
flickerstreak@52 492 si:SetFrameStrata("HIGH")
flickerstreak@52 493 si:SetHeight(8)
flickerstreak@52 494 si:SetWidth(8)
flickerstreak@52 495 local tex = si:CreateTexture()
flickerstreak@52 496 tex:SetAllPoints()
flickerstreak@52 497 tex:SetTexture(1.0, 0.82, 0, 0.8)
flickerstreak@52 498 tex:SetBlendMode("ADD")
flickerstreak@52 499 tex:SetDrawLayer("OVERLAY")
flickerstreak@52 500 return si
flickerstreak@52 501 end
flickerstreak@52 502
flickerstreak@52 503 local si1 = CreateSnapIndicator()
flickerstreak@52 504 local si2 = CreateSnapIndicator()
flickerstreak@52 505
flickerstreak@52 506 local function DisplaySnapIndicator( f, rx, ry, xOff, yOff )
flickerstreak@52 507 local o, p, rp, x, y, snap = GetClosestPointSnapped(f, rx, ry, xOff, yOff)
flickerstreak@52 508 if o then
flickerstreak@52 509 si1:ClearAllPoints()
flickerstreak@52 510 si2:ClearAllPoints()
flickerstreak@52 511 si1:SetPoint("CENTER", f, p, 0, 0)
flickerstreak@52 512 local xx, yy = pointCoordFuncs[rp](o)
flickerstreak@52 513 x = math.abs(x) <=rx and xx and 0 or x
flickerstreak@52 514 y = math.abs(y) <=ry and yy and 0 or y
flickerstreak@52 515 si2:SetPoint("CENTER", o, rp, x, y)
flickerstreak@52 516 si1:Show()
flickerstreak@52 517 si2:Show()
flickerstreak@52 518 else
flickerstreak@52 519 if si1:IsVisible() then
flickerstreak@52 520 si1:Hide()
flickerstreak@52 521 si2:Hide()
flickerstreak@52 522 end
flickerstreak@52 523 end
flickerstreak@52 524 end
flickerstreak@52 525
flickerstreak@52 526 local function HideSnapIndicator()
flickerstreak@52 527 if si1:IsVisible() then
flickerstreak@52 528 si1:Hide()
flickerstreak@52 529 si2:Hide()
flickerstreak@52 530 end
flickerstreak@52 531 end
flickerstreak@52 532
flickerstreak@33 533 CreateControls = function(bar)
flickerstreak@33 534 local f = bar.frame
flickerstreak@33 535
flickerstreak@33 536 f:SetMovable(true)
flickerstreak@33 537 f:SetResizable(true)
flickerstreak@33 538 f:SetClampedToScreen(true)
flickerstreak@33 539
flickerstreak@33 540 -- buttons on the bar should be direct children of the bar frame.
flickerstreak@33 541 -- The control elements need to float on top of this, which we could
flickerstreak@33 542 -- do with SetFrameLevel() or Raise(), but it's more reliable to do it
flickerstreak@33 543 -- via frame nesting, hence good old foo's appearance here.
flickerstreak@33 544 local foo = CreateFrame("Frame",nil,f)
flickerstreak@33 545 foo:SetAllPoints()
flickerstreak@51 546 foo:SetClampedToScreen(true)
flickerstreak@33 547
flickerstreak@33 548 local control = CreateFrame("Button", nil, foo)
flickerstreak@33 549 control:EnableMouse(true)
flickerstreak@33 550 control:SetToplevel(true)
flickerstreak@33 551 control:SetPoint("TOPLEFT", -4, 4)
flickerstreak@33 552 control:SetPoint("BOTTOMRIGHT", 4, -4)
flickerstreak@33 553 control:SetBackdrop({
flickerstreak@33 554 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
flickerstreak@33 555 tile = true,
flickerstreak@33 556 tileSize = 16,
flickerstreak@33 557 edgeSize = 16,
flickerstreak@33 558 insets = { left = 0, right = 0, top = 0, bottom = 0 },
flickerstreak@33 559 })
flickerstreak@33 560
flickerstreak@33 561 -- textures
flickerstreak@33 562 local bgTex = control:CreateTexture(nil,"BACKGROUND")
flickerstreak@33 563 bgTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@33 564 bgTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@33 565 bgTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@33 566 local hTex = control:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@33 567 hTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@33 568 hTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@33 569 hTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@33 570 hTex:SetBlendMode("ADD")
flickerstreak@33 571
flickerstreak@33 572 -- label
flickerstreak@33 573 local label = control:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@33 574 label:SetAllPoints()
flickerstreak@33 575 label:SetJustifyH("CENTER")
flickerstreak@33 576 label:SetShadowColor(0,0,0,1)
flickerstreak@33 577 label:SetShadowOffset(2,-2)
flickerstreak@33 578 label:SetTextColor(1,1,1,1)
flickerstreak@33 579 label:SetText(bar:GetName())
flickerstreak@33 580 label:Show()
flickerstreak@33 581 bar.controlLabelString = label -- so that bar:SetName() can update it
flickerstreak@33 582
flickerstreak@33 583 local StopResize = function()
flickerstreak@33 584 f:StopMovingOrSizing()
flickerstreak@33 585 f.isMoving = false
flickerstreak@33 586 f:SetScript("OnUpdate",nil)
flickerstreak@52 587 StoreSize(bar)
flickerstreak@33 588 ClampToButtons(bar)
flickerstreak@33 589 ApplyAnchor(bar)
flickerstreak@63 590 ReAction:RefreshOptions()
flickerstreak@33 591 end
flickerstreak@33 592
flickerstreak@33 593 -- edge drag handles
flickerstreak@33 594 for _, point in pairs({"LEFT","TOP","RIGHT","BOTTOM"}) do
flickerstreak@33 595 local edge = CreateFrame("Frame",nil,control)
flickerstreak@33 596 edge:EnableMouse(true)
flickerstreak@33 597 edge:SetWidth(8)
flickerstreak@33 598 edge:SetHeight(8)
flickerstreak@33 599 if point == "TOP" or point == "BOTTOM" then
flickerstreak@33 600 edge:SetPoint(point.."LEFT")
flickerstreak@33 601 edge:SetPoint(point.."RIGHT")
flickerstreak@33 602 else
flickerstreak@33 603 edge:SetPoint("TOP"..point)
flickerstreak@33 604 edge:SetPoint("BOTTOM"..point)
flickerstreak@33 605 end
flickerstreak@33 606 local tex = edge:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@33 607 tex:SetTexture(1.0,0.82,0,0.7)
flickerstreak@33 608 tex:SetBlendMode("ADD")
flickerstreak@33 609 tex:SetAllPoints()
flickerstreak@33 610 edge:RegisterForDrag("LeftButton")
flickerstreak@33 611 edge:SetScript("OnMouseDown",
flickerstreak@33 612 function()
flickerstreak@33 613 local bw, bh = GetButtonSize(bar)
flickerstreak@33 614 local r, c, s = GetButtonGrid(bar)
flickerstreak@33 615 f:SetMinResize( bw+s+1, bh+s+1 )
flickerstreak@33 616 f:StartSizing(point)
flickerstreak@33 617 f:SetScript("OnUpdate",
flickerstreak@33 618 function()
flickerstreak@33 619 RecomputeGrid(bar)
flickerstreak@33 620 bar:RefreshLayout()
flickerstreak@33 621 end
flickerstreak@33 622 )
flickerstreak@33 623 end
flickerstreak@33 624 )
flickerstreak@33 625 edge:SetScript("OnMouseUp", StopResize)
flickerstreak@33 626 edge:SetScript("OnEnter",
flickerstreak@33 627 function()
flickerstreak@33 628 GameTooltip:SetOwner(f, "ANCHOR_"..point)
flickerstreak@33 629 GameTooltip:AddLine(L["Drag to add/remove buttons"])
flickerstreak@33 630 GameTooltip:Show()
flickerstreak@33 631 end
flickerstreak@33 632 )
flickerstreak@33 633 edge:SetScript("OnLeave", HideGameTooltip)
flickerstreak@33 634 edge:Show()
flickerstreak@33 635 end
flickerstreak@33 636
flickerstreak@33 637 -- corner drag handles, again nested in an anonymous frame so that they are on top
flickerstreak@33 638 local foo2 = CreateFrame("Frame",nil,control)
flickerstreak@33 639 foo2:SetAllPoints(true)
flickerstreak@33 640 for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do
flickerstreak@33 641 local corner = CreateFrame("Frame",nil,foo2)
flickerstreak@33 642 corner:EnableMouse(true)
flickerstreak@33 643 corner:SetWidth(12)
flickerstreak@33 644 corner:SetHeight(12)
flickerstreak@33 645 corner:SetPoint(point)
flickerstreak@33 646 local tex = corner:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@33 647 tex:SetTexture(1.0,0.82,0,0.7)
flickerstreak@33 648 tex:SetBlendMode("ADD")
flickerstreak@33 649 tex:SetAllPoints()
flickerstreak@33 650 corner:RegisterForDrag("LeftButton","RightButton")
flickerstreak@33 651 local updateTooltip = function()
flickerstreak@33 652 local size, size2 = bar:GetButtonSize()
flickerstreak@33 653 local rows, cols, spacing = bar:GetButtonGrid()
flickerstreak@33 654 size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
flickerstreak@33 655 GameTooltipTextRight4:SetText(size)
flickerstreak@33 656 GameTooltipTextRight5:SetText(tostring(spacing))
flickerstreak@33 657 end
flickerstreak@33 658 corner:SetScript("OnMouseDown",
flickerstreak@33 659 function(_,btn)
flickerstreak@33 660 local bw, bh = GetButtonSize(bar)
flickerstreak@33 661 local r, c, s = GetButtonGrid(bar)
flickerstreak@33 662 if btn == "LeftButton" then -- button resize
flickerstreak@33 663 f:SetMinResize( (s+12)*c+1, (s+12)*r+1 )
flickerstreak@33 664 f:SetScript("OnUpdate",
flickerstreak@33 665 function()
flickerstreak@33 666 RecomputeButtonSize(bar)
flickerstreak@33 667 bar:RefreshLayout()
flickerstreak@33 668 updateTooltip()
flickerstreak@33 669 end
flickerstreak@33 670 )
flickerstreak@33 671 elseif btn == "RightButton" then -- spacing resize
flickerstreak@33 672 f:SetMinResize( bw*c, bh*r )
flickerstreak@33 673 f:SetScript("OnUpdate",
flickerstreak@33 674 function()
flickerstreak@33 675 RecomputeButtonSpacing(bar)
flickerstreak@33 676 bar:RefreshLayout()
flickerstreak@33 677 updateTooltip()
flickerstreak@33 678 end
flickerstreak@33 679 )
flickerstreak@33 680 end
flickerstreak@33 681 f:StartSizing(point)
flickerstreak@33 682 end
flickerstreak@33 683 )
flickerstreak@33 684 corner:SetScript("OnMouseUp",StopResize)
flickerstreak@33 685 corner:SetScript("OnEnter",
flickerstreak@33 686 function()
flickerstreak@33 687 GameTooltip:SetOwner(f, "ANCHOR_"..point)
flickerstreak@33 688 GameTooltip:AddLine(L["Drag to resize buttons"])
flickerstreak@33 689 GameTooltip:AddLine(L["Right-click-drag"])
flickerstreak@33 690 GameTooltip:AddLine(L["to change spacing"])
flickerstreak@33 691 local size, size2 = bar:GetButtonSize()
flickerstreak@33 692 local rows, cols, spacing = bar:GetButtonGrid()
flickerstreak@33 693 size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
flickerstreak@33 694 GameTooltip:AddDoubleLine(L["Size:"], size)
flickerstreak@33 695 GameTooltip:AddDoubleLine(L["Spacing:"], tostring(spacing))
flickerstreak@33 696 GameTooltip:Show()
flickerstreak@33 697 end
flickerstreak@33 698 )
flickerstreak@33 699 corner:SetScript("OnLeave",
flickerstreak@33 700 function()
flickerstreak@33 701 GameTooltip:Hide()
flickerstreak@33 702 f:SetScript("OnUpdate",nil)
flickerstreak@33 703 end
flickerstreak@33 704 )
flickerstreak@33 705
flickerstreak@33 706 end
flickerstreak@33 707
flickerstreak@33 708 control:RegisterForDrag("LeftButton")
flickerstreak@33 709 control:RegisterForClicks("RightButtonDown")
flickerstreak@33 710
flickerstreak@33 711 control:SetScript("OnDragStart",
flickerstreak@33 712 function()
flickerstreak@33 713 f:StartMoving()
flickerstreak@33 714 f.isMoving = true
flickerstreak@52 715 local w,h = bar:GetButtonSize()
flickerstreak@52 716 f:ClearAllPoints()
flickerstreak@52 717 f:SetScript("OnUpdate", function()
flickerstreak@52 718 if IsShiftKeyDown() then
flickerstreak@52 719 DisplaySnapIndicator(f,w,h)
flickerstreak@52 720 else
flickerstreak@52 721 HideSnapIndicator()
flickerstreak@52 722 end
flickerstreak@52 723 end)
flickerstreak@33 724 end
flickerstreak@33 725 )
flickerstreak@33 726
flickerstreak@52 727 local function updateDragTooltip()
flickerstreak@52 728 GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT")
flickerstreak@52 729 GameTooltip:AddLine(bar.name)
flickerstreak@52 730 GameTooltip:AddLine(L["Drag to move"])
flickerstreak@52 731 GameTooltip:AddLine(("|cff00ff00%s|r %s"):format(L["Shift-drag"],L["to anchor to nearby frames"]))
flickerstreak@52 732 GameTooltip:AddLine(("|cff00cccc%s|r %s"):format(L["Right-click"],L["for options"]))
flickerstreak@52 733 local _, a = bar:GetAnchor()
flickerstreak@52 734 if a and a ~= "UIParent" then
flickerstreak@52 735 GameTooltip:AddLine(L["Currently anchored to <%s>"]:format(a))
flickerstreak@52 736 end
flickerstreak@52 737 GameTooltip:Show()
flickerstreak@52 738 end
flickerstreak@52 739
flickerstreak@33 740 control:SetScript("OnDragStop",
flickerstreak@33 741 function()
flickerstreak@33 742 f:StopMovingOrSizing()
flickerstreak@33 743 f.isMoving = false
flickerstreak@33 744 f:SetScript("OnUpdate",nil)
flickerstreak@52 745
flickerstreak@52 746 if IsShiftKeyDown() then
flickerstreak@52 747 local w, h = bar:GetButtonSize()
flickerstreak@52 748 local a, p, rp, x, y = GetClosestPointSnapped(f,w,h)
flickerstreak@52 749 if a then
flickerstreak@52 750 f:ClearAllPoints()
flickerstreak@52 751 f:SetPoint(p,a,rp,x,y)
flickerstreak@52 752 end
flickerstreak@52 753 HideSnapIndicator()
flickerstreak@52 754 end
flickerstreak@52 755
flickerstreak@33 756 StoreExtents(bar)
flickerstreak@63 757 ReAction:RefreshOptions()
flickerstreak@52 758 updateDragTooltip()
flickerstreak@33 759 end
flickerstreak@33 760 )
flickerstreak@33 761
flickerstreak@33 762 control:SetScript("OnEnter",
flickerstreak@33 763 function()
flickerstreak@63 764 -- TODO: add bar type and status information to name
flickerstreak@63 765 --[[
flickerstreak@33 766 local name = bar.name
flickerstreak@33 767 for _, m in ReAction:IterateModules() do
flickerstreak@33 768 local suffix = safecall(m,"GetBarNameModifier",bar)
flickerstreak@33 769 if suffix then
flickerstreak@33 770 name = ("%s %s"):format(name,suffix)
flickerstreak@33 771 end
flickerstreak@33 772 end
flickerstreak@63 773 ]]--
flickerstreak@52 774
flickerstreak@52 775 updateDragTooltip()
flickerstreak@33 776 end
flickerstreak@33 777 )
flickerstreak@33 778
flickerstreak@33 779 control:SetScript("OnLeave", HideGameTooltip)
flickerstreak@33 780
flickerstreak@33 781 control:SetScript("OnClick",
flickerstreak@33 782 function()
flickerstreak@33 783 bar:ShowMenu()
flickerstreak@33 784 end
flickerstreak@33 785 )
flickerstreak@33 786
flickerstreak@33 787 return control
flickerstreak@33 788 end
flickerstreak@33 789 end
flickerstreak@33 790
flickerstreak@33 791
flickerstreak@33 792 local OpenMenu, CloseMenu
flickerstreak@33 793 do
flickerstreak@33 794 -- Looking for a lightweight AceConfig3-struct-compatible
flickerstreak@33 795 -- replacement for Dewdrop, encapsulate here
flickerstreak@33 796 -- Considering Blizzard's EasyMenu/UIDropDownMenu, but that's
flickerstreak@33 797 -- a bit tricky to convert from AceConfig3-struct
flickerstreak@33 798 local Dewdrop = AceLibrary("Dewdrop-2.0")
flickerstreak@33 799 OpenMenu = function(frame, opts)
flickerstreak@33 800 Dewdrop:Open(frame, "children", opts, "cursorX", true, "cursorY", true)
flickerstreak@33 801 end
flickerstreak@33 802 CloseMenu = function(frame)
flickerstreak@33 803 if Dewdrop:GetOpenedParent() == frame then
flickerstreak@33 804 Dewdrop:Close()
flickerstreak@33 805 end
flickerstreak@33 806 end
flickerstreak@33 807 end
flickerstreak@33 808
flickerstreak@33 809
flickerstreak@33 810 function Bar:ShowControls(show)
flickerstreak@33 811 if show then
flickerstreak@33 812 if not self.controlFrame then
flickerstreak@33 813 self.controlFrame = CreateControls(self)
flickerstreak@33 814 end
flickerstreak@33 815 self.controlFrame:Show()
flickerstreak@33 816 elseif self.controlFrame then
flickerstreak@33 817 CloseMenu(self.controlFrame)
flickerstreak@33 818 self.controlFrame:Hide()
flickerstreak@33 819 end
flickerstreak@33 820 end
flickerstreak@33 821
flickerstreak@33 822 function Bar:ShowMenu()
flickerstreak@33 823 if not self.menuOpts then
flickerstreak@33 824 self.menuOpts = {
flickerstreak@33 825 type = "group",
flickerstreak@33 826 args = {
flickerstreak@33 827 openConfig = {
flickerstreak@33 828 type = "execute",
flickerstreak@58 829 name = L["Settings..."],
flickerstreak@58 830 desc = L["Open the editor for this bar"],
flickerstreak@63 831 func = function() CloseMenu(self.controlFrame); ReAction:ShowEditor(self) end,
flickerstreak@33 832 disabled = InCombatLockdown,
flickerstreak@33 833 order = 1
flickerstreak@33 834 },
flickerstreak@33 835 delete = {
flickerstreak@33 836 type = "execute",
flickerstreak@33 837 name = L["Delete Bar"],
flickerstreak@33 838 desc = L["Remove the bar from the current profile"],
flickerstreak@50 839 confirm = L["Are you sure you want to remove this bar?"],
flickerstreak@33 840 func = function() ReAction:EraseBar(self) end,
flickerstreak@33 841 order = 2
flickerstreak@33 842 },
flickerstreak@33 843 }
flickerstreak@33 844 }
flickerstreak@33 845 end
flickerstreak@33 846 OpenMenu(self.controlFrame, self.menuOpts)
flickerstreak@33 847 end
flickerstreak@33 848
flickerstreak@33 849
flickerstreak@33 850
flickerstreak@28 851 ------ Export as a class-factory ------
flickerstreak@28 852 ReAction.Bar = {
flickerstreak@28 853 new = function(self, ...)
flickerstreak@28 854 local x = { }
flickerstreak@28 855 for k,v in pairs(Bar) do
flickerstreak@28 856 x[k] = v
flickerstreak@28 857 end
flickerstreak@28 858 Constructor(x, ...)
flickerstreak@28 859 return x
flickerstreak@28 860 end
flickerstreak@28 861 }