annotate Overlay.lua @ 108:b2fb8f7dc780

ButtonFacade support
author Flick <flickerstreak@gmail.com>
date Wed, 07 Jan 2009 00:57:02 +0000
parents 890e4c4ab143
children
rev   line source
flickerstreak@99 1 local ReAction = ReAction
flickerstreak@99 2 local L = ReAction.L
flickerstreak@99 3 local CreateFrame = CreateFrame
flickerstreak@99 4 local InCombatLockdown = InCombatLockdown
flickerstreak@99 5 local floor = math.floor
flickerstreak@99 6 local min = math.min
flickerstreak@99 7 local format = string.format
flickerstreak@99 8 local GameTooltip = GameTooltip
flickerstreak@99 9 local Bar = ReAction.Bar
flickerstreak@99 10 local GetSize = Bar.GetSize
flickerstreak@103 11 local SetSize = Bar.SetSize
flickerstreak@99 12 local GetButtonSize = Bar.GetButtonSize
flickerstreak@99 13 local GetButtonGrid = Bar.GetButtonGrid
flickerstreak@99 14 local SetButtonSize = Bar.SetButtonSize
flickerstreak@99 15 local SetButtonGrid = Bar.SetButtonGrid
flickerstreak@99 16 local ApplyAnchor = Bar.ApplyAnchor
flickerstreak@99 17 local GameTooltipTextRight1 = GameTooltipTextRight1
flickerstreak@99 18 local GameTooltipTextRight2 = GameTooltipTextRight2
flickerstreak@99 19 local GameTooltipTextRight3 = GameTooltipTextRight3
flickerstreak@73 20
flickerstreak@92 21 local KB = LibStub("LibKeyBound-1.0")
flickerstreak@92 22
flickerstreak@80 23 ReAction:UpdateRevision("$Revision$")
flickerstreak@77 24
flickerstreak@103 25
flickerstreak@103 26 --
flickerstreak@103 27 -- Wrap some of the bar manipulators to make them state-aware
flickerstreak@103 28 --
flickerstreak@103 29 local function SetAnchor( bar, point, frame, relPoint, x, y )
flickerstreak@103 30 local state = bar:GetState()
flickerstreak@103 31 if state then
flickerstreak@103 32 local anchorstate = bar:GetStateProperty(state, "anchorEnable")
flickerstreak@103 33 if anchorstate then
flickerstreak@103 34 bar:SetStateProperty(state, "anchorFrame", frame)
flickerstreak@103 35 bar:SetStateProperty(state, "anchorPoint", point)
flickerstreak@103 36 bar:SetStateProperty(state, "anchorRelPoint", relPoint)
flickerstreak@103 37 bar:SetStateProperty(state, "anchorX", x or 0)
flickerstreak@103 38 bar:SetStateProperty(state, "anchorY", y or 0)
flickerstreak@103 39 bar:SetAnchor(bar:GetAnchor())
flickerstreak@103 40 return
flickerstreak@103 41 end
flickerstreak@103 42 end
flickerstreak@103 43 bar:SetAnchor(point, frame, relPoint, x, y)
flickerstreak@103 44 end
flickerstreak@103 45
flickerstreak@103 46 local function GetStateScale( bar )
flickerstreak@103 47 local state = bar:GetState()
flickerstreak@103 48 if state and bar:GetStateProperty(state, "enableScale") then
flickerstreak@103 49 return bar:GetStateProperty(state, "scale")
flickerstreak@103 50 end
flickerstreak@103 51 end
flickerstreak@103 52
flickerstreak@103 53 local function SetStateScale( bar, scale )
flickerstreak@103 54 local state = bar:GetState()
flickerstreak@103 55 if state and bar:GetStateProperty(state, "enableScale") then
flickerstreak@103 56 bar:SetStateProperty(state, "scale", scale)
flickerstreak@103 57 end
flickerstreak@103 58 end
flickerstreak@103 59
flickerstreak@103 60
flickerstreak@73 61 --
flickerstreak@73 62 -- Bar config overlay
flickerstreak@73 63 --
flickerstreak@90 64
flickerstreak@98 65 local function GetNormalTextColor()
flickerstreak@98 66 return 1.0, 1.0, 1.0, 1.0
flickerstreak@98 67 end
flickerstreak@98 68
flickerstreak@98 69 local function GetAnchoredTextColor()
flickerstreak@98 70 return 1.0, 1.0, 1.0, 1.0
flickerstreak@98 71 end
flickerstreak@98 72
flickerstreak@98 73 local function GetNormalBgColor()
flickerstreak@98 74 return 0.7, 0.7, 1.0, 0.3
flickerstreak@98 75 end
flickerstreak@98 76
flickerstreak@98 77 local function GetAnchoredBgColor()
flickerstreak@98 78 return 0.9, 0.2, 0.7, 0.3
flickerstreak@98 79 end
flickerstreak@98 80
flickerstreak@90 81 local function StoreSize(bar)
flickerstreak@90 82 local f = bar:GetFrame()
flickerstreak@90 83 SetSize( bar, f:GetWidth(), f:GetHeight() )
flickerstreak@90 84 end
flickerstreak@73 85
flickerstreak@73 86 local function StoreExtents(bar)
flickerstreak@75 87 local f = bar:GetFrame()
flickerstreak@92 88 local p, fr, rp, x, y = f:GetPoint(1)
flickerstreak@103 89 fr = fr and fr:GetName() or "UIParent"
flickerstreak@92 90 SetAnchor( bar, p, fr, rp, x, y )
flickerstreak@90 91 SetSize( bar, 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@103 106 local function ComputeBarScale(bar)
flickerstreak@103 107 local w, h = bar.controlFrame:GetWidth() - 8, bar.controlFrame:GetHeight() - 8
flickerstreak@103 108 local bw, bh = GetButtonSize(bar)
flickerstreak@103 109 local r, c, s = GetButtonGrid(bar)
flickerstreak@103 110
flickerstreak@103 111 local scaleW = w / (c*(bw+s))
flickerstreak@103 112 local scaleH = h / (r*(bh+s))
flickerstreak@103 113 local scale = min(scaleW, scaleH)
flickerstreak@103 114
flickerstreak@103 115 if scale > 2.5 then
flickerstreak@103 116 scale = 2.5
flickerstreak@103 117 elseif scale < 0.25 then
flickerstreak@103 118 scale = 0.25
flickerstreak@103 119 end
flickerstreak@103 120
flickerstreak@103 121 return scale
flickerstreak@103 122 end
flickerstreak@103 123
flickerstreak@73 124 local function RecomputeButtonSpacing(bar)
flickerstreak@73 125 local w, h = GetSize(bar)
flickerstreak@73 126 local bw, bh = GetButtonSize(bar)
flickerstreak@73 127 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 128
flickerstreak@73 129 SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh))
flickerstreak@73 130 end
flickerstreak@73 131
flickerstreak@73 132 local function RecomputeGrid(bar)
flickerstreak@73 133 local w, h = GetSize(bar)
flickerstreak@73 134 local bw, bh = GetButtonSize(bar)
flickerstreak@73 135 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 136
flickerstreak@73 137 SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s)
flickerstreak@73 138 end
flickerstreak@73 139
flickerstreak@73 140 local function ClampToButtons(bar)
flickerstreak@73 141 local bw, bh = GetButtonSize(bar)
flickerstreak@73 142 local r, c, s = GetButtonGrid(bar)
flickerstreak@73 143 SetSize(bar, (bw+s)*c + 1, (bh+s)*r + 1)
flickerstreak@73 144 end
flickerstreak@73 145
flickerstreak@73 146 local function HideGameTooltip()
flickerstreak@73 147 GameTooltip:Hide()
flickerstreak@73 148 end
flickerstreak@73 149
flickerstreak@73 150 local anchorInside = { inside = true }
flickerstreak@73 151 local anchorOutside = { outside = true }
flickerstreak@73 152 local edges = { "BOTTOM", "TOP", "LEFT", "RIGHT" }
flickerstreak@73 153 local oppositeEdges = {
flickerstreak@73 154 TOP = "BOTTOM",
flickerstreak@73 155 BOTTOM = "TOP",
flickerstreak@73 156 LEFT = "RIGHT",
flickerstreak@73 157 RIGHT = "LEFT"
flickerstreak@73 158 }
flickerstreak@73 159 local pointsOnEdge = {
flickerstreak@73 160 BOTTOM = { "BOTTOM", "BOTTOMLEFT", "BOTTOMRIGHT", },
flickerstreak@73 161 TOP = { "TOP", "TOPLEFT", "TOPRIGHT", },
flickerstreak@73 162 RIGHT = { "RIGHT", "BOTTOMRIGHT", "TOPRIGHT", },
flickerstreak@73 163 LEFT = { "LEFT", "BOTTOMLEFT", "TOPLEFT", },
flickerstreak@73 164 }
flickerstreak@73 165 local edgeSelector = {
flickerstreak@73 166 BOTTOM = 1, -- select x of x,y
flickerstreak@73 167 TOP = 1, -- select x of x,y
flickerstreak@73 168 LEFT = 2, -- select y of x,y
flickerstreak@73 169 RIGHT = 2, -- select y of x,y
flickerstreak@73 170 }
flickerstreak@73 171 local snapPoints = {
flickerstreak@73 172 [anchorOutside] = {
flickerstreak@73 173 BOTTOMLEFT = {"BOTTOMRIGHT","TOPLEFT","TOPRIGHT"},
flickerstreak@73 174 BOTTOM = {"TOP"},
flickerstreak@73 175 BOTTOMRIGHT = {"BOTTOMLEFT","TOPRIGHT","TOPLEFT"},
flickerstreak@73 176 RIGHT = {"LEFT"},
flickerstreak@73 177 TOPRIGHT = {"TOPLEFT","BOTTOMRIGHT","BOTTOMLEFT"},
flickerstreak@73 178 TOP = {"BOTTOM"},
flickerstreak@73 179 TOPLEFT = {"TOPRIGHT","BOTTOMLEFT","BOTTOMRIGHT"},
flickerstreak@73 180 LEFT = {"RIGHT"},
flickerstreak@73 181 CENTER = {"CENTER"}
flickerstreak@73 182 },
flickerstreak@73 183 [anchorInside] = {
flickerstreak@73 184 BOTTOMLEFT = {"BOTTOMLEFT"},
flickerstreak@73 185 BOTTOM = {"BOTTOM"},
flickerstreak@73 186 BOTTOMRIGHT = {"BOTTOMRIGHT"},
flickerstreak@73 187 RIGHT = {"RIGHT"},
flickerstreak@73 188 TOPRIGHT = {"TOPRIGHT"},
flickerstreak@73 189 TOP = {"TOP"},
flickerstreak@73 190 TOPLEFT = {"TOPLEFT"},
flickerstreak@73 191 LEFT = {"LEFT"},
flickerstreak@73 192 CENTER = {"CENTER"}
flickerstreak@73 193 }
flickerstreak@73 194 }
flickerstreak@73 195 local insidePointOffsetFuncs = {
flickerstreak@73 196 BOTTOMLEFT = function(x, y) return x, y end,
flickerstreak@73 197 BOTTOM = function(x, y) return 0, y end,
flickerstreak@73 198 BOTTOMRIGHT = function(x, y) return -x, y end,
flickerstreak@73 199 RIGHT = function(x, y) return -x, 0 end,
flickerstreak@73 200 TOPRIGHT = function(x, y) return -x, -y end,
flickerstreak@73 201 TOP = function(x, y) return 0, -y end,
flickerstreak@73 202 TOPLEFT = function(x, y) return x, -y end,
flickerstreak@73 203 LEFT = function(x, y) return x, 0 end,
flickerstreak@98 204 CENTER = function(x, y) return x, y end,
flickerstreak@73 205 }
flickerstreak@73 206 local pointCoordFuncs = {
flickerstreak@73 207 BOTTOMLEFT = function(f) return f:GetLeft(), f:GetBottom() end,
flickerstreak@73 208 BOTTOM = function(f) return nil, f:GetBottom() end,
flickerstreak@73 209 BOTTOMRIGHT = function(f) return f:GetRight(), f:GetBottom() end,
flickerstreak@73 210 RIGHT = function(f) return f:GetRight(), nil end,
flickerstreak@73 211 TOPRIGHT = function(f) return f:GetRight(), f:GetTop() end,
flickerstreak@73 212 TOP = function(f) return nil, f:GetTop() end,
flickerstreak@73 213 TOPLEFT = function(f) return f:GetLeft(), f:GetTop() end,
flickerstreak@73 214 LEFT = function(f) return f:GetLeft(), nil end,
flickerstreak@73 215 CENTER = function(f) return f:GetCenter() end,
flickerstreak@73 216 }
flickerstreak@73 217 local edgeBoundsFuncs = {
flickerstreak@73 218 BOTTOM = function(f) return f:GetLeft(), f:GetRight() end,
flickerstreak@73 219 LEFT = function(f) return f:GetBottom(), f:GetTop() end
flickerstreak@73 220 }
flickerstreak@73 221 edgeBoundsFuncs.TOP = edgeBoundsFuncs.BOTTOM
flickerstreak@73 222 edgeBoundsFuncs.RIGHT = edgeBoundsFuncs.LEFT
flickerstreak@99 223 local cornerTexCoords = {
flickerstreak@99 224 -- ULx, ULy, LLx, LLy, URx, URy, LRx, LRy
flickerstreak@99 225 TOPLEFT = { 1, 1, 1, 0, 0, 1, 0, 0 },
flickerstreak@99 226 TOPRIGHT = { 1, 0, 0, 0, 1, 1, 0, 1 },
flickerstreak@99 227 BOTTOMLEFT = { 0, 1, 1, 1, 0, 0, 1, 0 },
flickerstreak@99 228 BOTTOMRIGHT = { 0, 0, 0, 1, 1, 0, 1, 1 },
flickerstreak@99 229 }
flickerstreak@73 230
flickerstreak@73 231 -- Returns absolute coordinates x,y of the named point 'p' of frame 'f'
flickerstreak@73 232 local function GetPointCoords( f, p )
flickerstreak@73 233 local x, y = pointCoordFuncs[p](f)
flickerstreak@73 234 if not(x and y) then
flickerstreak@73 235 local cx, cy = f:GetCenter()
flickerstreak@73 236 x = x or cx
flickerstreak@73 237 y = y or cy
flickerstreak@73 238 end
flickerstreak@73 239 return x, y
flickerstreak@73 240 end
flickerstreak@73 241
flickerstreak@73 242
flickerstreak@73 243 -- Returns true if frame 'f1' can be anchored to frame 'f2'
flickerstreak@73 244 local function CheckAnchorable( f1, f2 )
flickerstreak@73 245 -- can't anchor a frame to itself or to nil
flickerstreak@73 246 if f1 == f2 or f2 == nil then
flickerstreak@73 247 return false
flickerstreak@73 248 end
flickerstreak@73 249
flickerstreak@73 250 -- can always anchor to UIParent
flickerstreak@73 251 if f2 == UIParent then
flickerstreak@73 252 return true
flickerstreak@73 253 end
flickerstreak@73 254
flickerstreak@73 255 -- also can't do circular anchoring of frames
flickerstreak@73 256 -- walk the anchor chain, which generally shouldn't be that expensive
flickerstreak@73 257 -- (who nests draggables that deep anyway?)
flickerstreak@73 258 for i = 1, f2:GetNumPoints() do
flickerstreak@73 259 local _, f = f2:GetPoint(i)
flickerstreak@73 260 if not f then f = f2:GetParent() end
flickerstreak@73 261 return CheckAnchorable(f1,f)
flickerstreak@73 262 end
flickerstreak@73 263
flickerstreak@73 264 return true
flickerstreak@73 265 end
flickerstreak@73 266
flickerstreak@73 267 -- Returns true if frames f1 and f2 specified edges overlap
flickerstreak@73 268 local function CheckEdgeOverlap( f1, f2, e )
flickerstreak@73 269 local l1, u1 = edgeBoundsFuncs[e](f1)
flickerstreak@73 270 local l2, u2 = edgeBoundsFuncs[e](f2)
flickerstreak@73 271 return l1 <= l2 and l2 <= u1 or l2 <= l1 and l1 <= u2
flickerstreak@73 272 end
flickerstreak@73 273
flickerstreak@73 274 -- Returns true if point p1 on frame f1 overlaps edge e2 on frame f2
flickerstreak@73 275 local function CheckPointEdgeOverlap( f1, p1, f2, e2 )
flickerstreak@73 276 local l, u = edgeBoundsFuncs[e2](f2)
flickerstreak@73 277 local x, y = GetPointCoords(f1,p1)
flickerstreak@73 278 x = select(edgeSelector[e2], x, y)
flickerstreak@73 279 return l <= x and x <= u
flickerstreak@73 280 end
flickerstreak@73 281
flickerstreak@73 282 -- Returns the distance between corresponding edges. It is
flickerstreak@73 283 -- assumed that the passed in edges e1 and e2 are the same or opposites
flickerstreak@73 284 local function GetEdgeDistance( f1, f2, e1, e2 )
flickerstreak@73 285 local x1, y1 = pointCoordFuncs[e1](f1)
flickerstreak@73 286 local x2, y2 = pointCoordFuncs[e2](f2)
flickerstreak@73 287 return math.abs((x1 or y1) - (x2 or y2))
flickerstreak@73 288 end
flickerstreak@73 289
flickerstreak@73 290 local globalSnapTargets = { [UIParent] = anchorInside }
flickerstreak@73 291
flickerstreak@73 292 local function GetClosestFrameEdge(f1,f2,a)
flickerstreak@73 293 local dist, edge, opp
flickerstreak@73 294 if f2:IsVisible() and CheckAnchorable(f1,f2) then
flickerstreak@73 295 for _, e in pairs(edges) do
flickerstreak@73 296 local o = a.inside and e or oppositeEdges[e]
flickerstreak@73 297 if CheckEdgeOverlap(f1,f2,e) then
flickerstreak@73 298 local d = GetEdgeDistance(f1, f2, e, o)
flickerstreak@73 299 if not dist or (d < dist) then
flickerstreak@73 300 dist, edge, opp = d, e, o
flickerstreak@73 301 end
flickerstreak@73 302 end
flickerstreak@73 303 end
flickerstreak@73 304 end
flickerstreak@73 305 return dist, edge, opp
flickerstreak@73 306 end
flickerstreak@73 307
flickerstreak@73 308 local function GetClosestVisibleEdge( f )
flickerstreak@73 309 local r, o, e1, e2
flickerstreak@73 310 local a = anchorOutside
flickerstreak@73 311 for _, b in ReAction:IterateBars() do
flickerstreak@73 312 local d, e, opp = GetClosestFrameEdge(f,b:GetFrame(),a)
flickerstreak@73 313 if d and (not r or d < r) then
flickerstreak@73 314 r, o, e1, e2 = d, b:GetFrame(), e, opp
flickerstreak@73 315 end
flickerstreak@73 316 end
flickerstreak@73 317 for f2, a2 in pairs(globalSnapTargets) do
flickerstreak@73 318 local d, e, opp = GetClosestFrameEdge(f,f2,a2)
flickerstreak@73 319 if d and (not r or d < r) then
flickerstreak@73 320 r, o, e1, e2, a = d, f2, e, opp, a2
flickerstreak@73 321 end
flickerstreak@73 322 end
flickerstreak@73 323 return o, e1, e2, a
flickerstreak@73 324 end
flickerstreak@73 325
flickerstreak@73 326 local function GetClosestVisiblePoint(f1)
flickerstreak@73 327 local f2, e1, e2, a = GetClosestVisibleEdge(f1)
flickerstreak@73 328 if f2 then
flickerstreak@73 329 local rsq, p, rp, x, y
flickerstreak@73 330 -- iterate pointsOnEdge in order and use < to prefer edge centers to corners
flickerstreak@73 331 for _, p1 in ipairs(pointsOnEdge[e1]) do
flickerstreak@73 332 if CheckPointEdgeOverlap(f1,p1,f2,e2) then
flickerstreak@73 333 for _, p2 in pairs(snapPoints[a][p1]) do
flickerstreak@73 334 local x1, y1 = GetPointCoords(f1,p1)
flickerstreak@73 335 local x2, y2 = GetPointCoords(f2,p2)
flickerstreak@73 336 local dx = x1 - x2
flickerstreak@73 337 local dy = y1 - y2
flickerstreak@73 338 local rsq2 = dx*dx + dy*dy
flickerstreak@73 339 if not rsq or rsq2 < rsq then
flickerstreak@73 340 rsq, p, rp, x, y = rsq2, p1, p2, dx, dy
flickerstreak@73 341 end
flickerstreak@73 342 end
flickerstreak@73 343 end
flickerstreak@73 344 end
flickerstreak@73 345 return f2, p, rp, x, y
flickerstreak@73 346 end
flickerstreak@73 347 end
flickerstreak@73 348
flickerstreak@73 349 local function GetClosestPointSnapped(f1, rx, ry, xOff, yOff)
flickerstreak@73 350 local o, p, rp, x, y = GetClosestVisiblePoint(f1)
flickerstreak@73 351 local s = false
flickerstreak@92 352
flickerstreak@92 353 local insideOffsetFunc = p and insidePointOffsetFuncs[p]
flickerstreak@92 354 local coordFunc = p and pointCoordFuncs[p]
flickerstreak@92 355 if not insideOffsetFunc or not coordFunc then
flickerstreak@92 356 return
flickerstreak@92 357 end
flickerstreak@73 358
flickerstreak@99 359 local sx, sy = insideOffsetFunc(xOff or 0, yOff or 0)
flickerstreak@99 360 local xx, yy = coordFunc(f1)
flickerstreak@73 361 if xx and yy then
flickerstreak@73 362 if math.abs(x) <= rx then
flickerstreak@99 363 if math.abs(y) <= ry then
flickerstreak@99 364 x = sx
flickerstreak@99 365 y = sy
flickerstreak@99 366 s = true
flickerstreak@99 367 elseif CheckEdgeOverlap(f1,o,"LEFT") then
flickerstreak@99 368 x = sx
flickerstreak@99 369 s = true
flickerstreak@99 370 end
flickerstreak@99 371 elseif math.abs(y) <= ry and CheckEdgeOverlap(f1,o,"TOP") then
flickerstreak@73 372 y = sy
flickerstreak@73 373 s = true
flickerstreak@73 374 end
flickerstreak@73 375 elseif xx then
flickerstreak@73 376 if math.abs(x) <= rx then
flickerstreak@73 377 x = sx
flickerstreak@73 378 s = true
flickerstreak@73 379 if math.abs(y) <= ry then
flickerstreak@73 380 y = sy
flickerstreak@73 381 end
flickerstreak@73 382 end
flickerstreak@73 383 elseif yy then
flickerstreak@73 384 if math.abs(y) <= ry then
flickerstreak@73 385 y = sy
flickerstreak@73 386 s = true
flickerstreak@73 387 if math.abs(x) <= rx then
flickerstreak@73 388 x = sx
flickerstreak@73 389 end
flickerstreak@73 390 end
flickerstreak@73 391 end
flickerstreak@73 392
flickerstreak@99 393 -- correct for some Lua oddities with doubles
flickerstreak@73 394 if x == -0 then x = 0 end
flickerstreak@73 395 if y == -0 then y = 0 end
flickerstreak@73 396
flickerstreak@73 397 if s then
flickerstreak@73 398 return o, p, rp, math.floor(x), math.floor(y)
flickerstreak@73 399 end
flickerstreak@73 400 end
flickerstreak@73 401
flickerstreak@73 402 local function CreateSnapIndicator()
flickerstreak@73 403 local si = CreateFrame("Frame",nil,UIParent)
flickerstreak@73 404 si:SetFrameStrata("HIGH")
flickerstreak@98 405 si:SetHeight(16)
flickerstreak@98 406 si:SetWidth(16)
flickerstreak@73 407 local tex = si:CreateTexture()
flickerstreak@73 408 tex:SetAllPoints()
flickerstreak@98 409 tex:SetTexture("Interface\\AddOns\\ReAction\\img\\lock")
flickerstreak@73 410 tex:SetBlendMode("ADD")
flickerstreak@73 411 tex:SetDrawLayer("OVERLAY")
flickerstreak@73 412 return si
flickerstreak@73 413 end
flickerstreak@73 414
flickerstreak@73 415 local si1 = CreateSnapIndicator()
flickerstreak@73 416 local si2 = CreateSnapIndicator()
flickerstreak@73 417
flickerstreak@73 418 local function DisplaySnapIndicator( f, rx, ry, xOff, yOff )
flickerstreak@73 419 local o, p, rp, x, y, snap = GetClosestPointSnapped(f, rx, ry, xOff, yOff)
flickerstreak@73 420 if o then
flickerstreak@73 421 si1:ClearAllPoints()
flickerstreak@73 422 si2:ClearAllPoints()
flickerstreak@73 423 si1:SetPoint("CENTER", f, p, 0, 0)
flickerstreak@73 424 local xx, yy = pointCoordFuncs[rp](o)
flickerstreak@73 425 x = math.abs(x) <=rx and xx and 0 or x
flickerstreak@73 426 y = math.abs(y) <=ry and yy and 0 or y
flickerstreak@73 427 si2:SetPoint("CENTER", o, rp, x, y)
flickerstreak@73 428 si1:Show()
flickerstreak@73 429 si2:Show()
flickerstreak@73 430 else
flickerstreak@73 431 if si1:IsVisible() then
flickerstreak@73 432 si1:Hide()
flickerstreak@73 433 si2:Hide()
flickerstreak@73 434 end
flickerstreak@73 435 end
flickerstreak@98 436 return o, p
flickerstreak@73 437 end
flickerstreak@73 438
flickerstreak@73 439 local function HideSnapIndicator()
flickerstreak@73 440 if si1:IsVisible() then
flickerstreak@73 441 si1:Hide()
flickerstreak@73 442 si2:Hide()
flickerstreak@73 443 end
flickerstreak@73 444 end
flickerstreak@73 445
flickerstreak@98 446 local function UpdateLabelString(bar)
flickerstreak@98 447 local label = bar.controlLabelString
flickerstreak@98 448 if label then
flickerstreak@98 449 local name = bar.labelName
flickerstreak@98 450 if name and bar.labelSubtext then
flickerstreak@98 451 name = format("%s (%s)", name, bar.labelSubtext)
flickerstreak@98 452 end
flickerstreak@98 453 label:SetText(name or "")
flickerstreak@98 454 end
flickerstreak@98 455 end
flickerstreak@98 456
flickerstreak@73 457 local function CreateControls(bar)
flickerstreak@75 458 local f = bar:GetFrame()
flickerstreak@73 459
flickerstreak@73 460 f:SetMovable(true)
flickerstreak@73 461 f:SetResizable(true)
flickerstreak@73 462
flickerstreak@103 463 -- child of UIParent so that alpha and scale doesn't propagate to it
flickerstreak@103 464 local overlay = CreateFrame("Button", nil, UIParent)
flickerstreak@92 465 overlay:EnableMouse(true)
flickerstreak@92 466 overlay:SetFrameLevel(3) -- set it above the buttons
flickerstreak@103 467 overlay:SetPoint("TOPLEFT", f, "TOPLEFT", -4, 4)
flickerstreak@103 468 overlay:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", 4, -4)
flickerstreak@92 469 overlay:SetBackdrop({
flickerstreak@73 470 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
flickerstreak@73 471 tile = true,
flickerstreak@73 472 tileSize = 16,
flickerstreak@73 473 edgeSize = 16,
flickerstreak@73 474 insets = { left = 0, right = 0, top = 0, bottom = 0 },
flickerstreak@73 475 })
flickerstreak@73 476
flickerstreak@73 477 -- textures
flickerstreak@92 478 local bgTex = overlay:CreateTexture(nil,"BACKGROUND")
flickerstreak@73 479 bgTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@73 480 bgTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@73 481 bgTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@92 482 local hTex = overlay:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@73 483 hTex:SetTexture(0.7,0.7,1.0,0.2)
flickerstreak@73 484 hTex:SetPoint("TOPLEFT",4,-4)
flickerstreak@73 485 hTex:SetPoint("BOTTOMRIGHT",-4,4)
flickerstreak@73 486 hTex:SetBlendMode("ADD")
flickerstreak@98 487 local aTex = overlay:CreateTexture(nil,"ARTWORK")
flickerstreak@98 488 aTex:SetTexture("Interface\\AddOns\\ReAction\\img\\lock")
flickerstreak@98 489 aTex:SetWidth(16)
flickerstreak@98 490 aTex:SetHeight(16)
flickerstreak@98 491 aTex:Hide()
flickerstreak@73 492
flickerstreak@73 493 -- label
flickerstreak@92 494 local label = overlay:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@73 495 label:SetAllPoints()
flickerstreak@73 496 label:SetJustifyH("CENTER")
flickerstreak@73 497 label:SetShadowColor(0,0,0,1)
flickerstreak@98 498 label:SetShadowOffset(3,-3)
flickerstreak@98 499 label:SetTextColor(GetNormalTextColor())
flickerstreak@73 500 label:SetText(bar:GetName())
flickerstreak@73 501 label:Show()
flickerstreak@90 502 bar.controlLabelString = label -- so that bar:SetLabel() can update it
flickerstreak@73 503
flickerstreak@98 504 local function UpdateAnchorDecoration()
flickerstreak@98 505 local point, anchor, relPoint, x, y = f:GetPoint(1)
flickerstreak@98 506 if point then
flickerstreak@98 507 local ofsx, ofsy = insidePointOffsetFuncs[point](x,y)
flickerstreak@98 508 if (anchor and anchor ~= UIParent) or (ofsx == 0 and ofsy == 0) then
flickerstreak@98 509 bgTex:SetTexture( GetAnchoredBgColor() )
flickerstreak@98 510 hTex:SetTexture( GetAnchoredBgColor() )
flickerstreak@98 511 label:SetTextColor( GetAnchoredTextColor() )
flickerstreak@98 512 aTex:ClearAllPoints()
flickerstreak@98 513 aTex:SetPoint(point)
flickerstreak@98 514 aTex:Show()
flickerstreak@98 515 return
flickerstreak@98 516 end
flickerstreak@98 517 end
flickerstreak@98 518 bgTex:SetTexture( GetNormalBgColor() )
flickerstreak@98 519 hTex:SetTexture( GetNormalBgColor() )
flickerstreak@98 520 label:SetTextColor( GetNormalTextColor() )
flickerstreak@98 521 aTex:Hide()
flickerstreak@98 522 end
flickerstreak@98 523
flickerstreak@73 524 local function StopResize()
flickerstreak@73 525 f:StopMovingOrSizing()
flickerstreak@73 526 f.isMoving = false
flickerstreak@73 527 f:SetScript("OnUpdate",nil)
flickerstreak@73 528 StoreSize(bar)
flickerstreak@73 529 ClampToButtons(bar)
flickerstreak@73 530 ReAction:RefreshOptions()
flickerstreak@73 531 end
flickerstreak@73 532
flickerstreak@99 533 local function CornerUpdate()
flickerstreak@99 534 local bw, bh = GetButtonSize(bar)
flickerstreak@99 535 local r, c, s = GetButtonGrid(bar)
flickerstreak@103 536 local ss = GetStateScale(bar)
flickerstreak@99 537 if IsShiftKeyDown() then
flickerstreak@103 538 if ss then
flickerstreak@103 539 f:SetMinResize( ((s+bw)*c*0.25)/ss, ((s+bh)*r*0.25)/ss )
flickerstreak@103 540 f:SetMaxResize( ((s+bw)*c*2.5 + 1)/ss, ((s+bh)*r*2.5 + 1)/ss )
flickerstreak@103 541 scale = ComputeBarScale(bar)
flickerstreak@103 542 else
flickerstreak@103 543 f:SetMinResize( (s+12)*c+1, (s+12)*r+1 )
flickerstreak@103 544 f:SetMaxResize( (s+128)*c+1, (s+128)*r+1 )
flickerstreak@103 545 RecomputeButtonSize(bar)
flickerstreak@103 546 end
flickerstreak@103 547 elseif not ss and IsAltKeyDown() then
flickerstreak@99 548 f:SetMinResize( bw*c, bh*r )
flickerstreak@103 549 f:SetMaxResize( 2*bw*c, 2*bh*r )
flickerstreak@99 550 RecomputeButtonSpacing(bar)
flickerstreak@73 551 else
flickerstreak@99 552 f:SetMinResize( bw+s+1, bh+s+1 )
flickerstreak@103 553 f:SetMaxResize( 50*(bw+s)+1, 50*(bh+s)+1 )
flickerstreak@99 554 RecomputeGrid(bar)
flickerstreak@73 555 end
flickerstreak@103 556 GameTooltipTextRight2:SetText(format("%d x %d",r,c))
flickerstreak@103 557
flickerstreak@103 558 local ss = GetStateScale(bar)
flickerstreak@103 559 local state = bar:GetState()
flickerstreak@103 560 if ss then
flickerstreak@103 561 GameTooltipTextRight4:SetText(format("%d%%", scale*100))
flickerstreak@103 562 else
flickerstreak@103 563 local size = (bw == bh) and tostring(bw) or format("%d x %d",bw,bh)
flickerstreak@103 564 GameTooltipTextRight3:SetText(size)
flickerstreak@103 565 GameTooltipTextRight4:SetText(tostring(s))
flickerstreak@103 566 end
flickerstreak@73 567 end
flickerstreak@73 568
flickerstreak@99 569 -- corner drag handles
flickerstreak@73 570 for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do
flickerstreak@99 571 local corner = CreateFrame("Frame",nil,overlay)
flickerstreak@73 572 corner:EnableMouse(true)
flickerstreak@99 573 corner:SetWidth(16)
flickerstreak@99 574 corner:SetHeight(16)
flickerstreak@73 575 corner:SetPoint(point)
flickerstreak@99 576
flickerstreak@73 577 local tex = corner:CreateTexture(nil,"HIGHLIGHT")
flickerstreak@99 578 tex:SetTexture("Interface\\AddOns\\ReAction\\img\\corner")
flickerstreak@99 579 tex:SetTexCoord(unpack(cornerTexCoords[point]))
flickerstreak@73 580 tex:SetBlendMode("ADD")
flickerstreak@99 581 tex:SetAlpha(0.6)
flickerstreak@73 582 tex:SetAllPoints()
flickerstreak@99 583
flickerstreak@73 584 corner:SetScript("OnMouseDown",
flickerstreak@73 585 function(_,btn)
flickerstreak@99 586 f:SetScript("OnUpdate", CornerUpdate)
flickerstreak@73 587 f:StartSizing(point)
flickerstreak@73 588 end
flickerstreak@73 589 )
flickerstreak@103 590 corner:SetScript("OnMouseUp",
flickerstreak@103 591 function()
flickerstreak@103 592 local ss = GetStateScale(bar)
flickerstreak@103 593 if ss then
flickerstreak@103 594 local state = bar:GetState()
flickerstreak@103 595 SetStateScale(bar, ComputeBarScale(bar))
flickerstreak@103 596 end
flickerstreak@103 597 StopResize()
flickerstreak@103 598 end)
flickerstreak@73 599 corner:SetScript("OnEnter",
flickerstreak@73 600 function()
flickerstreak@99 601 local bw, bh = GetButtonSize(bar)
flickerstreak@99 602 local r, c, s = bar:GetButtonGrid()
flickerstreak@99 603 local size = (bw == bh) and tostring(bw) or format("%d x %d",bw,bh)
flickerstreak@103 604 local ss = GetStateScale(bar)
flickerstreak@103 605 local state = bar:GetState()
flickerstreak@73 606 GameTooltip:SetOwner(f, "ANCHOR_"..point)
flickerstreak@103 607 if ss then
flickerstreak@103 608 GameTooltip:AddLine(format("%s (%s: %s)", bar:GetName(), L["State"], state))
flickerstreak@103 609 else
flickerstreak@103 610 GameTooltip:AddLine(bar:GetName())
flickerstreak@103 611 end
flickerstreak@99 612 GameTooltip:AddDoubleLine(format("|cffcccccc%s|r %s",L["Drag"],L["to add/remove buttons:"]), format("%d x %d",r,c))
flickerstreak@103 613 if ss then
flickerstreak@103 614 GameTooltip:AddLine(L["State Scale Override"])
flickerstreak@103 615 GameTooltip:AddDoubleLine(format("|cff00ff00%s|r %s",L["Hold Shift"],L["to change scale:"]), format("%d%%", bar:GetStateProperty(state,"scale")*100))
flickerstreak@103 616 else
flickerstreak@103 617 GameTooltip:AddDoubleLine(format("|cff00ff00%s|r %s",L["Hold Shift"],L["to resize buttons:"]), tostring(floor(size)))
flickerstreak@103 618 GameTooltip:AddDoubleLine(format("|cff0033cc%s|r %s",L["Hold Alt"], L["to change spacing:"]), tostring(floor(s)))
flickerstreak@103 619 end
flickerstreak@73 620 GameTooltip:Show()
flickerstreak@73 621 end
flickerstreak@73 622 )
flickerstreak@73 623 corner:SetScript("OnLeave",
flickerstreak@73 624 function()
flickerstreak@73 625 GameTooltip:Hide()
flickerstreak@73 626 f:SetScript("OnUpdate",nil)
flickerstreak@73 627 end
flickerstreak@73 628 )
flickerstreak@73 629 end
flickerstreak@73 630
flickerstreak@92 631 overlay:RegisterForDrag("LeftButton")
flickerstreak@92 632 overlay:RegisterForClicks("RightButtonUp")
flickerstreak@73 633
flickerstreak@92 634 overlay:SetScript("OnDragStart",
flickerstreak@73 635 function()
flickerstreak@73 636 f:StartMoving()
flickerstreak@73 637 f.isMoving = true
flickerstreak@73 638 local w,h = bar:GetButtonSize()
flickerstreak@73 639 f:ClearAllPoints()
flickerstreak@98 640 UpdateAnchorDecoration()
flickerstreak@73 641 f:SetScript("OnUpdate", function()
flickerstreak@73 642 if IsShiftKeyDown() then
flickerstreak@98 643 local f, p = DisplaySnapIndicator(f,w,h)
flickerstreak@73 644 else
flickerstreak@73 645 HideSnapIndicator()
flickerstreak@73 646 end
flickerstreak@73 647 end)
flickerstreak@73 648 end
flickerstreak@73 649 )
flickerstreak@73 650
flickerstreak@98 651 local function UpdateDragTooltip()
flickerstreak@73 652 GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT")
flickerstreak@103 653 local ss = GetStateScale(bar)
flickerstreak@103 654 local state = bar:GetState()
flickerstreak@103 655 if ss then
flickerstreak@103 656 GameTooltip:AddLine(format("%s (%s: %s)", bar:GetName(), L["State"], state))
flickerstreak@103 657 else
flickerstreak@103 658 GameTooltip:AddLine(bar:GetName())
flickerstreak@103 659 end
flickerstreak@99 660 GameTooltip:AddLine(format("|cffcccccc%s|r %s",L["Drag"],L["to move"]))
flickerstreak@99 661 GameTooltip:AddLine(format("|cff00ff00%s|r %s",L["Hold Shift"],L["to anchor to nearby frames"]))
flickerstreak@99 662 GameTooltip:AddLine(format("|cff00cccc%s|r %s",L["Right-click"],L["for options..."]))
flickerstreak@103 663 local point, frame, relpoint, x, y = bar:GetFrame():GetPoint(1)
flickerstreak@99 664 if point then
flickerstreak@99 665 local ofsx, ofsy = insidePointOffsetFuncs[point](x,y)
flickerstreak@103 666 if (frame and frame ~= UIParent) or (ofsx == 0 and ofsy == 0) then
flickerstreak@103 667 frame = frame or UIParent
flickerstreak@103 668 GameTooltip:AddLine(format("%s <%s>",L["Currently anchored to"],frame:GetName()))
flickerstreak@99 669 end
flickerstreak@73 670 end
flickerstreak@73 671 GameTooltip:Show()
flickerstreak@73 672 end
flickerstreak@73 673
flickerstreak@92 674 overlay:SetScript("OnDragStop",
flickerstreak@73 675 function()
flickerstreak@73 676 f:StopMovingOrSizing()
flickerstreak@73 677 f.isMoving = false
flickerstreak@73 678 f:SetScript("OnUpdate",nil)
flickerstreak@73 679
flickerstreak@73 680 if IsShiftKeyDown() then
flickerstreak@73 681 local w, h = bar:GetButtonSize()
flickerstreak@73 682 local a, p, rp, x, y = GetClosestPointSnapped(f,w,h)
flickerstreak@73 683 if a then
flickerstreak@73 684 f:ClearAllPoints()
flickerstreak@73 685 f:SetPoint(p,a,rp,x,y)
flickerstreak@73 686 end
flickerstreak@73 687 HideSnapIndicator()
flickerstreak@73 688 end
flickerstreak@73 689
flickerstreak@73 690 StoreExtents(bar)
flickerstreak@73 691 ReAction:RefreshOptions()
flickerstreak@98 692 UpdateDragTooltip()
flickerstreak@98 693 UpdateAnchorDecoration()
flickerstreak@73 694 end
flickerstreak@73 695 )
flickerstreak@73 696
flickerstreak@92 697 overlay:SetScript("OnEnter",
flickerstreak@73 698 function()
flickerstreak@98 699 UpdateDragTooltip()
flickerstreak@73 700 end
flickerstreak@73 701 )
flickerstreak@73 702
flickerstreak@92 703 overlay:SetScript("OnLeave", HideGameTooltip)
flickerstreak@73 704
flickerstreak@92 705 overlay:SetScript("OnClick",
flickerstreak@73 706 function()
flickerstreak@90 707 ReAction:ShowEditor(bar)
flickerstreak@73 708 end
flickerstreak@73 709 )
flickerstreak@73 710
flickerstreak@92 711 function overlay:LIBKEYBOUND_ENABLED(evt)
flickerstreak@92 712 self:SetFrameLevel(1)
flickerstreak@92 713 end
flickerstreak@90 714
flickerstreak@92 715 function overlay:LIBKEYBOUND_DISABLED(evt)
flickerstreak@92 716 self:SetFrameLevel(3)
flickerstreak@92 717 end
flickerstreak@92 718
flickerstreak@103 719 function overlay:RefreshControls()
flickerstreak@103 720 UpdateAnchorDecoration()
flickerstreak@103 721 end
flickerstreak@103 722
flickerstreak@103 723 overlay:SetScript("OnShow", overlay.RefreshControls)
flickerstreak@103 724
flickerstreak@92 725 KB.RegisterCallback(overlay,"LIBKEYBOUND_ENABLED")
flickerstreak@92 726 KB.RegisterCallback(overlay,"LIBKEYBOUND_DISABLED")
flickerstreak@92 727
flickerstreak@92 728 if ReAction:GetKeybindMode() then
flickerstreak@93 729 overlay:SetFrameLevel(1)
flickerstreak@92 730 end
flickerstreak@92 731
flickerstreak@98 732 bar:SetLabel(bar:GetName())
flickerstreak@98 733 UpdateLabelString(bar)
flickerstreak@98 734 UpdateAnchorDecoration()
flickerstreak@98 735
flickerstreak@92 736 return overlay
flickerstreak@73 737 end
flickerstreak@73 738
flickerstreak@73 739
flickerstreak@90 740 -- export methods to the Bar prototype
flickerstreak@73 741
flickerstreak@73 742 function Bar:ShowControls(show)
flickerstreak@92 743 local f = self.controlFrame
flickerstreak@73 744 if show then
flickerstreak@92 745 if not f then
flickerstreak@92 746 f = CreateControls(self)
flickerstreak@92 747 self.controlFrame = f
flickerstreak@73 748 end
flickerstreak@92 749 f:Show()
flickerstreak@92 750 elseif f then
flickerstreak@92 751 f:Hide()
flickerstreak@73 752 end
flickerstreak@73 753 end
flickerstreak@73 754
flickerstreak@103 755 function Bar:RefreshControls()
flickerstreak@103 756 if self.controlFrame and self.controlFrame:IsShown() then
flickerstreak@103 757 self.controlFrame:RefreshControls()
flickerstreak@103 758 end
flickerstreak@103 759 end
flickerstreak@103 760
flickerstreak@90 761 function Bar:SetLabel(name)
flickerstreak@98 762 self.labelName = name
flickerstreak@98 763 UpdateLabelString(self)
flickerstreak@90 764 end
flickerstreak@98 765
flickerstreak@98 766 function Bar:SetLabelSubtext(text)
flickerstreak@98 767 self.labelSubtext = text
flickerstreak@98 768 UpdateLabelString(self)
flickerstreak@98 769 end