vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Bongos Bar
3 A customizable container frame object
4  
5 BongosBar.sets = {
6 scale (a float ranging from 0 to <1, or nil)
7 the bar's scale, relative to UIParent
8 alpha (a float ranging from 0 to <1, or nil)
9 the bar's opacity
10 vis (flag)
11 is the bar shown or not
12 anchor (string or nil)
13 what bar we're attached to, and the point
14 --any other bar settings
15 }
16 --]]
17  
18 local STICKY_TOLERANCE = 16; --how close one bar has to be to another in order to attempt auto anchoring
19  
20 local barList = {}; --indexed by barID, any bongos bars currently in use
21 local deletedBars = {}; --indexed by name, any bongos bars we've deleted
22  
23 --[[ Drag Button Functions ]]--
24  
25 local function DragButton_OnMouseDown()
26 this:GetParent():StartMoving();
27 GameTooltip:Hide();
28 end
29  
30 local function DragButton_OnMouseUp()
31 this:GetParent():StopMovingOrSizing();
32 BBar.TryToStick(BBar.IDToBar(this:GetText()));
33 end
34  
35 local function DragButton_OnEnter()
36 if this:GetScript("OnClick") then
37 GameTooltip:SetOwner(this, "ANCHOR_LEFT")
38 if not tonumber(this:GetText()) then
39 GameTooltip:SetText(this:GetText() .. " bar", 1, 1, 1);
40 else
41 GameTooltip:SetText("actionbar " .. this:GetText(), 1, 1, 1);
42 end
43 GameTooltip:AddLine("<Right Click> to Configure");
44 GameTooltip:Show();
45 end
46 end
47  
48 local function DragButton_OnLeave()
49 GameTooltip:Hide();
50 end
51  
52 local function AddDragButton(parent, menuFunc)
53 local button = CreateFrame("Button", parent:GetName() .. "DragButton", parent);
54  
55 button:SetPoint("TOPLEFT", parent, "TOPLEFT", -2, 2);
56 button:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 2, -2);
57 button:SetFrameLevel(parent:GetFrameLevel() + 3);
58 button:SetClampedToScreen(true);
59  
60 button:SetTextFontObject(GameFontNormalLarge);
61 button:SetHighlightTextColor(1, 1, 1);
62 button:SetText(parent.id);
63  
64 local normalTexture = button:CreateTexture();
65 normalTexture:SetTexture(0, 0, 0, 0.4);
66 normalTexture:SetAllPoints(button);
67  
68 local highlightTexture = button:CreateTexture();
69 highlightTexture:SetTexture(0.2, 0.4, 0.8, 0.2);
70 highlightTexture:SetAllPoints(button);
71 button:SetHighlightTexture(highlightTexture);
72  
73 button:SetScale(1/parent:GetScale());
74 button:RegisterForClicks("LeftButtonDown", "LeftButtonUp", "RightButtonUp");
75 button:SetScript("OnMouseDown", DragButton_OnMouseDown);
76 button:SetScript("OnMouseUp", DragButton_OnMouseUp);
77 button:SetScript("OnEnter", DragButton_OnEnter);
78 button:SetScript("OnLeave", DragButton_OnLeave);
79  
80 if menuFunc then
81 button:SetScript("OnClick", function()
82 if arg1 == "RightButton" then
83 menuFunc(button:GetParent())
84 end
85 end);
86 end
87  
88 button:Hide();
89 end
90  
91 --updates the drag button color of a given bar if its attached to another bar
92 local function UpdateDragButtonColor(bar)
93 if bar.sets.anchor then
94 getglobal(bar:GetName() .. "DragButton"):SetTextColor(0.5, 0.5, 1);
95 else
96 getglobal(bar:GetName() .. "DragButton"):SetTextColor(1, 0.82, 0);
97 end
98 end
99  
100 --[[ Bar Retrieval ]]--
101  
102 local function GetDeletedBar(barName)
103 deletedBars[barName] = nil;
104 local bar = getglobal(barName);
105 bar:SetParent(UIParent);
106 return bar;
107 end
108  
109 --[[ Usable BBar Functions ]]--
110  
111 BBar = {
112 --[[ Constructor]]--
113  
114 Create = function(barID, name, sets, menuFunc, alwaysShow, onDeleteFunc)
115 assert(barID and name, "Usage: BBar.Create(barID, \"name\" [, \"settings\"] [, menuFunc] [,alwaysShow])");
116 assert(not barList[barID], "BarID '" .. barID .. "' is already in use.");
117  
118 local bar;
119 if getglobal(name) then
120 --we're reusing a previously created bongos bar
121 if deletedBars[name] then
122 bar = GetDeletedBar(name);
123 bar.id = barID;
124 if menuFunc then
125 getglobal(bar:GetName() .. "DragButton"):SetScript("OnClick", function()
126 if arg1 == "RightButton" then
127 menuFunc(this:GetParent())
128 end
129 end);
130 end
131 else
132 error("Attempted to create preexisting frame '" .. name .. "'.");
133 end
134 --we're creating a new bar
135 else
136 bar = CreateFrame("Frame", name, UIParent);
137 bar.id = barID;
138 AddDragButton(bar, menuFunc);
139 end
140  
141 bar:SetClampedToScreen(true);
142 bar:SetMovable(true);
143 if alwaysShow then
144 bar.alwaysShow = 1;
145 end
146 bar.OnDelete = onDeleteFunc;
147  
148 local settings;
149 if sets then
150 settings = getfield(sets);
151 bar.setsGlobal = sets;
152 end
153 if not settings then
154 local defaults = BProfile.GetDefaultValue(sets);
155 if defaults then
156 bar.sets = defaults;
157 elseif not bar.alwaysShow then
158 bar.sets = {vis = 1};
159 else
160 bar.sets = {};
161 end
162  
163 if sets then
164 setfield(sets, bar.sets);
165 end
166 else
167 bar.sets = settings;
168 end
169 BBar.LoadSettings(bar);
170 barList[barID] = bar;
171  
172 return bar;
173 end,
174  
175 --[[ Destructor ]]--
176  
177 Delete = function(barID)
178 assert(barID, "Usage: BBar.Create(barID)");
179  
180 local bar = barList[barID];
181 if bar then
182 if bar.OnDelete then
183 bar:OnDelete()
184 end
185 --delete all bar saved settings, remove it from the list of used IDs
186 barList[barID] = nil;
187 setfield(bar.setsGlobal, nil)
188 bar.sets = nil;
189 bar.setsGlobal = nil;
190 bar.id = nil;
191 bar.alwaysShow = nil;
192 bar:UnregisterAllEvents();
193  
194 --hide the bar, then reanchor all bars
195 bar:SetParent(nil);
196 bar:ClearAllPoints();
197 bar:SetUserPlaced(false);
198 bar:Hide();
199 BBar.ForAll(BBar.Reanchor);
200  
201 --add the bar to the deleted bars list
202 deletedBars[bar:GetName()] = true;
203 end
204 end,
205  
206 --[[ Visibility ]]--
207  
208 Show = function(bar, save)
209 if not bar.alwaysShow then
210 bar:Show();
211 if save then
212 bar.sets.vis = 1;
213 end
214 end
215 end,
216  
217 Hide = function(bar, save)
218 if not bar.alwaysShow then
219 bar:Hide();
220 if save then
221 bar.sets.vis = nil;
222 end
223 end
224 end,
225  
226 Toggle = function(bar, save)
227 if bar:IsShown() then
228 BBar.Hide(bar, save);
229 else
230 BBar.Show(bar, save);
231 end
232 end,
233  
234 --[[ Movement ]]--
235  
236 Lock = function(bar)
237 getglobal(bar:GetName() .. "DragButton"):Hide();
238 end,
239  
240 Unlock = function(bar)
241 getglobal(bar:GetName() .. "DragButton"):Show();
242 end,
243  
244 --[[ Configuration ]]--
245  
246 --set bar scale
247 SetScale = function(bar, scale, save)
248 Infield.Scale(bar, scale or 1);
249 getglobal(bar:GetName() .. "DragButton"):SetScale(1/bar:GetScale());
250 BBar.Reanchor(bar);
251 if save then
252 if scale == 1 then
253 bar.sets.scale = nil;
254 else
255 bar.sets.scale = scale;
256 end
257 end
258 end,
259  
260 --set bar opacity
261 SetAlpha = function(bar, alpha, save)
262 bar:SetAlpha(alpha or 1);
263 getglobal(bar:GetName() .. "DragButton"):SetAlpha(1);
264 if save then
265 if alpha == 1 then
266 bar.sets.alpha = nil
267 else
268 bar.sets.alpha = alpha;
269 end
270 end
271 end,
272  
273 --try to anchor the bar to any bar its near
274 TryToStick = function(bar)
275 if BongosSets.sticky then
276 bar.sets.anchor = nil;
277 for _, otherBar in pairs(barList) do
278 if otherBar:IsShown() then
279 local point = FlyPaper.Stick(bar, otherBar, STICKY_TOLERANCE, 2, 2);
280 if point then
281 bar.sets.anchor = otherBar.id .. point;
282 break;
283 end
284 end
285 end
286 end
287 UpdateDragButtonColor(bar);
288 end,
289  
290 --[[ Load Settings ]]--
291  
292 --load all default bar settings
293 LoadSettings = function(bar)
294 --set visibility
295 if not(bar.sets.vis or bar.alwaysShow) then
296 bar:Hide();
297 else
298 bar:Show();
299 end
300 --set opacity
301 if bar.sets.alpha then
302 BBar.SetAlpha(bar, bar.sets.alpha);
303 end
304 --set position
305 if bar.sets.x and bar.sets.y then
306 --this handles rescaling, so we don't rescale if we're loading the bar's position
307 BBar.Reposition(bar);
308 --set scale
309 elseif bar.sets.scale then
310 BBar.Rescale(bar, bar.sets.scale);
311 end
312 if not BongosSets.locked then
313 BBar.Unlock(bar);
314 end
315 end,
316  
317 --rescale the bar, but without auto repositioning
318 Rescale = function(bar)
319 bar:SetScale(bar.sets.scale or 1);
320 getglobal(bar:GetName() .. "DragButton"):SetScale(1/bar:GetScale());
321 end,
322  
323 --place the bar at it's save'd position. This is ment to be used in combination with BProfile
324 Reposition = function(bar)
325 BBar.Rescale(bar);
326  
327 bar:ClearAllPoints();
328 bar:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", bar.sets.x, bar.sets.y);
329 bar:SetUserPlaced(true);
330 bar.sets.x = nil;
331 bar.sets.y = nil;
332 end,
333  
334 --try to reanchor the bar
335 Reanchor = function(bar)
336 local otherBar, point = BBar.GetAnchor(bar);
337 if BongosSets.sticky and otherBar then
338 if not FlyPaper.StickToPoint(bar, otherBar, point, 2, 2) then
339 bar.sets.anchor = nil;
340 end
341 else
342 bar.sets.anchor = nil;
343 end
344 UpdateDragButtonColor(bar);
345 end,
346  
347 --[[ Conversions ]]--
348  
349 GetID = function(bar)
350 return bar.id;
351 end,
352  
353 GetAnchor = function(bar)
354 local anchorString = bar.sets.anchor;
355 if anchorString then
356 local pointStart = strlen(anchorString) - 1;
357 return BBar.IDToBar(strsub(anchorString, 1, pointStart - 1)), strsub(anchorString, pointStart);
358 end
359 end,
360  
361 --takes a barID, and returns
362 IDToBar = function(barID)
363 if tonumber(barID) then
364 return barList[tonumber(barID)];
365 end
366 return barList[barID];
367 end,
368  
369 --[[ Higher Order Functions ]]--
370  
371 --performs action(bar, arg1, arg2, ...) to every bongos bar
372 ForAll = function(action, ...)
373 for _,bar in pairs(barList) do
374 action(bar, unpack(arg));
375 end
376 end,
377  
378 --performs action(barID, arg1, arg2, ...) to every bongos bar ID
379 ForAllIDs = function(action, ...)
380 for id in barList do
381 action(id, unpack(arg));
382 end
383 end,
384  
385 GetAll = function()
386 local list = {};
387 for _, bar in pairs(barList) do
388 table.insert(list, bar)
389 end
390 return list;
391 end,
392 }
393  
394 Infield.AddRescaleAction(function()
395 BBar.ForAll(BBar.Rescale);
396 BBar.ForAll(BBar.Reanchor);
397 end)