Language:
Lua     Change language:
Pastebin: 132192
Author: Anonymous
Subject: Untitled
Created: 2010-01-29 10:21:53
Download and save
Toggle line numbers
1--[[  
2    Copyright (c) 2010, Eyal Shilony (eyal.pro@msn.com), All rights reserved. 
3 
4    Thanks to Gotai for the heads up and the great feedback. :) 
5    ----------------------------------------------------------------------------- 
6     
7    A library that defines shortcuts for the game user interface. 
8     
9]]-- 
10assert(LibStub, "LibShortcuts-2.0 requires LibStub"
11 
12local lib, minor = LibStub:NewLibrary("LibShortcuts-2.0", 0
13if not lib then return end 
14minor = minor or 0 
15 
16-- START LOCALIZATION -- 
17local CALENDAR = "Calendar" 
18-- END -- 
19 
20local GetUIPanel = GetUIPanel 
21local HideUIPanel = HideUIPanel 
22local ShowUIPanel = ShowUIPanel 
23local UIPanelWindows = UIPanelWindows 
24 
25-- The following frames cannot be opened or hidden while in-combat. 
26RestrictedFrames = { 
27    ["PaperDollFrame"] = true
28    ["SpellBookFrame"] = true
29} 
30 
31-- When we click on a shortcut, make sure to hide the GameMenuFrame for the following frames.  
32GameMenu_OptionFrames = { 
33    ["VideoOptionsFrame"] = true
34    ["AudioOptionsFrame"] = true
35    ["InterfaceOptionsFrame"] = true
36    ["MacOptionsFrame"] = true
37    ["KeyBindingFrame"] = true
38} 
39 
40-- Load these beforehand when a shortcut is executed. 
41LoD_AddonFrames = { 
42    ["PlayerTalentFrame"] = TalentFrame_LoadUI, 
43    ["AchievementFrame"] = AchievementFrame_LoadUI, 
44    ["CalendarFrame"] = Calendar_LoadUI, 
45    ["KeyBindingFrame"] = KeyBindingFrame_LoadUI, 
46    ["MacroFrame"] = MacroFrame_LoadUI, 
47    ["TimeManagerFrame"] = TimeManager_LoadUI, 
48} 
49 
50--[[ 
51    name - (string) The name of the frame the shortcut handles. 
52    text - (string) A localized name of the shortcut. 
53    func - (function) A function to execute when the shortcut is triggered. 
54    shorthand - (string) A shorthand for the shortcut. (if one exists.) 
55]]-- 
56local shortcuts = { 
57    character = { 
58        name = "CharacterFrame"
59        text = CHARACTER_BUTTON, 
60        shorthand = "char"
61        func = function() ToggleCharacter("PaperDollFrame") end
62    }
63    spellbook = { 
64        name = "SpellBookFrame"
65        text = SPELLBOOK_ABILITIES_BUTTON, 
66        shorthand = "sb"
67        func = function() ToggleFrame(SpellBookFrame) end
68    }
69    talents = { 
70        name = "PlayerTalentFrame"
71        text = TALENTS_BUTTON, 
72        shorthand = "tal"
73        func = function() ToggleTalentFrame() end
74    }
75    achievements = { 
76        name = "AchievementFrame"
77        text = ACHIEVEMENT_BUTTON, 
78        shorthand = "achi"
79        func = function() ToggleAchievementFrame() end
80    }
81    calendar = { 
82        name = "CalendarFrame"
83        text = CALENDAR, 
84        shorthand = "cal"
85        func = function() ToggleCalendar() end
86    }
87    questlog = { 
88        name = "QuestLogFrame"
89        text = QUESTLOG_BUTTON, 
90        shorthand = "ql"
91        func = function() ToggleFrame(QuestLogFrame) end
92    }
93    social = { 
94        name = "FriendsFrame"
95        text = SOCIAL_BUTTON, 
96        shorthand = "soc"
97        func = function() ToggleFriendsFrame() end
98    }
99    pvp = { 
100        name = "PVPParentFrame"
101        text = PLAYER_V_PLAYER, 
102        func = function() ToggleFrame(PVPParentFrame) end
103    }
104    lfd = { 
105        name = "LFDParentFrame"
106        text = DUNGEONS_BUTTON, 
107        func = function() ToggleLFDParentFrame() end
108    }
109    lfr = { 
110        name = "LFRParentFrame"
111        text = LOOKING_FOR_RAID, 
112        func = function() ToggleLFRParentFrame() end
113    }
114    time = { 
115        name = "TimeManagerFrame"
116        text = TIMEMANAGER_TITLE, 
117        func = function() ToggleTimeManager() end
118    }
119    help = { 
120        name = "HelpFrame"
121        text = HELP_BUTTON, 
122        func = function() ToggleHelpFrame() end
123    }
124    video = { 
125        name = "VideoOptionsFrame"
126        text = VIDEOOPTIONS_MENU, 
127        shorthand = "vid"
128        func = function() 
129            if not VideoOptionsFrame:IsVisible() then 
130                ShowUIPanel(VideoOptionsFrame) 
131                VideoOptionsFrame.lastFrame = GameMenuFrame 
132                VideoOptionsFrame.ignoreMenu = true 
133            else 
134                HideUIPanel(VideoOptionsFrame) 
135            end 
136        end
137    }
138    sound = { 
139        name = "AudioOptionsFrame"
140        text = SOUNDOPTIONS_MENU, 
141        shorthand = "snd"
142        func = function() 
143            if not AudioOptionsFrame:IsVisible() then 
144                ShowUIPanel(AudioOptionsFrame) 
145                AudioOptionsFrame.lastFrame = GameMenuFrame 
146                AudioOptionsFrame.ignoreMenu = true 
147            else 
148                HideUIPanel(AudioOptionsFrame) 
149            end 
150        end
151    }
152    interface = { 
153        name = "InterfaceOptionsFrame"
154        text = UIOPTIONS_MENU, 
155        shorthand = "ui"
156        func = function() 
157            if not InterfaceOptionsFrame:IsVisible() then 
158                ShowUIPanel(InterfaceOptionsFrame) 
159                InterfaceOptionsFrame.lastFrame = GameMenuFrame 
160                InterfaceOptionsFrame.ignoreMenu = true 
161            else 
162                HideUIPanel(InterfaceOptionsFrame) 
163            end 
164        end
165    }
166    mac = { 
167        name = "MacOptionsFrame"
168        text = MAC_OPTIONS, 
169        func = function() 
170            if not MacOptionsFrame:IsVisible() then 
171                ShowUIPanel(MacOptionsFrame) 
172                MacOptionsFrame.ignoreMenu = true 
173            else 
174                HideUIPanel(MacOptionsFrame) 
175            end 
176        end
177    }
178    bindings = { 
179        name = "KeyBindingFrame"
180        text = KEY_BINDINGS, 
181        shorthand = "binds"
182        func = function() 
183            if not KeyBindingFrame:IsVisible() then 
184                ShowUIPanel(KeyBindingFrame) 
185                KeyBindingFrame.ignoreMenu = true 
186            else 
187                HideUIPanel(KeyBindingFrame) 
188            end 
189        end
190    }
191    macro = { 
192        name = "MacroFrame"
193        text = MACROS, 
194        func = function() 
195            if not MacroFrame:IsVisible() then 
196                ShowUIPanel(MacroFrame) 
197            else 
198                HideUIPanel(MacroFrame) 
199            end 
200        end
201    }
202} 
203 
204-- Reference any shortcut to its shorthand 
205local shorthands = { } 
206for k, v in pairs(shortcuts) do 
207    if v.shorthand then 
208        shorthands[v.shorthand] = v 
209    end 
210end 
211 
212-- if we're not using a mac client, remove the shortcut. 
213if IsMacClient() ~= 1 then 
214    shortcuts["mac"] = nil 
215end 
216 
217local function message(value) 
218    print(("|cffFFFF00<LibShortcuts> The shortcut '%s' cannot be executed in combat due to Blizzard in-combat restrictions.|r"):format(value)) 
219end 
220 
221--[[ RunShortcut(name) - Execute a shortcut. 
222 
223    Arguments. 
224    name - (string) The name of the shortcut to execute. 
225     
226]]-- 
227 
228local function hideUIPanel(self) 
229    if self.ignoreMenu then 
230        HideUIPanel(GameMenuFrame) 
231        self.ignoreMenu = nil 
232    end 
233end 
234 
235function lib:RunShortcut(name) 
236    local shortcut = name and type(name) == "string" and (shortcuts[name] or shorthands[name]) 
237    if shortcut and shortcut.func and type(shortcut.func) == "function" then 
238        local inCombat = InCombatLockdown() 
239        local isRestricted = RestrictedFrames[shortcut.name] 
240        if not isRestricted or not inCombat and isRestricted then 
241            local loadLoD = LoD_AddonFrames[shortcut.name] 
242            if loadLoD then loadLoD() end 
243            local panel = UIPanelWindows[shortcut.name] 
244            local centerFrame = not (GetUIPanel("left") or GetUIPanel("right") or GetUIPanel("doublewide")) and GetUIPanel("center"
245            if panel and panel.area ~= "center" and centerFrame and centerFrame:IsShown() then 
246                HideUIPanel(centerFrame) 
247            end 
248            shortcut.func() 
249            if GameMenu_OptionFrames[shortcut.name] then 
250                local frame = _G[shortcut.name] 
251                if frame then 
252                    frame:HookScript("OnHide", hideUIPanel) 
253                end 
254                GameMenu_OptionFrames[shortcut.name] = nil 
255            end 
256        else 
257            message(name) 
258        end 
259    end 
260end 
261 
262--[[ GetShortcut(name) - Gets a table that contains the given shortcut. 
263 
264    Arguments. 
265    name - (string) The name of the shortcut to retrieve. 
266     
267    Returns. 
268    shortcut - (table) The shortcut. 
269     
270]]-- 
271function lib:GetShortcut(name) 
272    if name and type(name) == "string" then 
273        return shortcuts[name] 
274    end 
275end 
276 
277--[[ NumOfShortcuts() - Gets the number of shortcuts available. 
278 
279    Returns. 
280    num - (number) The number of shortcuts available. 
281 
282]]-- 
283 
284local num = 0 
285local key = next(shortcuts) 
286while key do 
287    num = num + 1 
288    key = next(shortcuts, key) 
289end 
290 
291function lib:NumOfShortcuts() 
292    return num 
293end 
294 
295--[[ IterateShortcuts([mode]) - Traverse through the shortcuts table.  
296 
297    Arguments. 
298    mode - (string) The mode of which the function may operate to return the values in an alphabetical order. 
299         
300        * keys - Returns a key, value pairs. 
301        * index - Returns an index, value pairs. 
302        * all - Returns an index, key and value. 
303     
304    Returns. 
305    iterator - (function) An iterator to traverse through the shortcuts table. 
306     
307        * index - (number) The position of the key. 
308        * key - (string) The name of the shortcut. 
309        * value - (table) The shortcut. 
310 
311]]-- 
312 
313local names = { } 
314for k in pairs(shortcuts) do 
315    tinsert(names, k:lower()) 
316end 
317table.sort(names) 
318 
319local function iterator(mode) 
320    local key 
321    local i = 0 
322    local iter = function() 
323        i = i + 1 
324        key = names[i] 
325        if key == nil then 
326            return nil 
327        elseif mode == "keys" then 
328            return key, shortcuts[key] 
329        elseif mode == "index" then 
330            return i, shortcuts[key] 
331        elseif mode == "all" then 
332            return i, key, shortcuts[key] 
333        end 
334    end 
335    return iter 
336end 
337 
338function lib:IterateShortcuts(mode) 
339    if mode == "keys" 
340    or mode == "index" 
341    or mode == "all" then 
342        return iterator(mode) 
343    else 
344        return pairs(shortcuts) 
345    end 
346end 
Download and save
Toggle line numbers
Thread:
[132192] Untitled by Anonymous at 2010-01-29 10:21:53
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.