vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Infield.lua
3 A library of functions to keep frames within the UIParent's bounds
4 --]]
5  
6 --constants
7 local VERSION = 60825;
8  
9 if Infield and Infield.version >= VERSION then return; end
10  
11 local UPDATE_INTERVAL = 1;
12  
13 --[[ Local Functions ]]--
14  
15 local function RegisterScaleEvents()
16 --set the onupdate function
17 InfieldUpdater:SetScript("OnEvent", function()
18 local uiScale = UIParent:GetScale();
19 if this.currentScale ~= uiScale then
20 for _, action in pairs(Infield.rescaleList) do
21 action();
22 end
23 this.currentScale = uiScale;
24 end
25 end);
26 InfieldUpdater:RegisterEvent("PLAYER_ENTERING_WORLD");
27 InfieldUpdater:RegisterEvent("CVAR_UPDATE");
28 end
29  
30 --returns the adjusted x and y coordinates for a frame when scaled, keeping the topleft of the frame in the same relative spot
31 local function GetAdjustedCoords(frame, nScale)
32 if not ( frame:GetLeft() and frame:GetTop() ) then
33 return;
34 end
35 return frame:GetLeft() * frame:GetScale() / nScale, frame:GetTop() * frame:GetScale() / nScale;
36 end
37  
38 --[[ Update or Load Infield's Functions ]]--
39  
40 --create a new infield instance
41 if not Infield then
42 Infield = { rescaleList = {} };
43 --do stuff needed to be done on a version change
44 else
45 local resList = Infield.rescaleList;
46 Infield = { rescaleList = resList };
47  
48 if InfieldUpdater and InfieldUpdater:GetScript("OnUpdate") then
49 InfieldUpdater:Hide();
50 InfieldUpdater:SetScript("OnUpdate", nil);
51 InfieldUpdater.elapsed = nil;
52 RegisterScaleEvents();
53 end
54 end
55 Infield.version = VERSION;
56  
57 --Add a function to execute when the UI scale changes
58 --This must be added before the PLAYER_LOGIN event takes place to detect the startup rescale.
59 Infield.AddRescaleAction = function(funct)
60 table.insert(Infield.rescaleList, funct);
61 end
62  
63 --Sets <frame>'s scale <scale>, and repositions the frame if its out of the viewable screen area
64 Infield.Scale = function(frame, scale)
65 local x, y = GetAdjustedCoords(frame, scale);
66  
67 frame:SetScale(scale);
68 if x and y then
69 Infield.Place(frame, "TOPLEFT", UIParent, "BOTTOMLEFT", x, y);
70 end
71 end
72  
73 --Places a frame at a location and repositions it so that it is not outside of the UIParent
74 --Syntax is similar to Frame:SetScale()
75 Infield.Place = function(frame, point, parent, relPoint, x, y)
76 frame:ClearAllPoints();
77 frame:SetPoint(point, parent, relPoint, x, y);
78 Infield.Reposition(frame, UIParent);
79 end
80  
81 --Moves a frame within the bounds of parent if its out of bounds
82 Infield.Reposition = function(frame, parent)
83 if frame:GetBottom() and frame:GetTop() and frame:GetLeft() and frame:GetRight() then
84 local xoff = 0;
85 local yoff = 0;
86 local ratio = frame:GetScale();
87  
88 --check and see if the frame is off the screen
89 --Y bounds checking
90 if frame:GetBottom() < 0 then
91 yoff = 0 - frame:GetBottom();
92 elseif frame:GetTop() > (parent:GetTop() / ratio) then
93 yoff = (parent:GetTop() / ratio) - frame:GetTop();
94 end
95  
96 --X bounds checking
97 if frame:GetLeft() < 0 then
98 xoff = 0 - frame:GetLeft();
99 elseif frame:GetRight() > (parent:GetRight() / ratio) then
100 xoff = (parent:GetRight() / ratio) - frame:GetRight();
101 end
102  
103 --Reposition if anything was out of bounds
104 if xoff ~= 0 or yoff ~= 0 then
105 local x = frame:GetLeft() + xoff;
106 local y = frame:GetTop() + yoff;
107  
108 frame:ClearAllPoints();
109 frame:SetPoint("TOPLEFT", parent, "BOTTOMLEFT", x , y);
110 end
111 end
112 end
113  
114 --create the frame
115 if not InfieldUpdater then
116 CreateFrame("Frame", "InfieldUpdater");
117 RegisterScaleEvents();
118 end