vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | -- AddonMenu (Satrina@Stormrage) |
2 | -- An embedded library to support addon configuration menus in the SELF menu |
||
3 | -- See AddonMenu.txt for usage |
||
4 | |||
5 | local version = 1.6 |
||
6 | |||
7 | if not AddonMenu or (AddonMenu.version < version) then |
||
8 | |||
9 | local prevAddonMenu = AddonMenu |
||
10 | |||
11 | AddonMenu = { |
||
12 | version = version, |
||
13 | debug = nil, |
||
14 | |||
15 | -------------------- |
||
16 | -- User functions -- |
||
17 | -------------------- |
||
18 | |||
19 | -- |
||
20 | -- AddMenuItem |
||
21 | -- Add text as a menu option to the addon menu callback will be invoked when the user selects it |
||
22 | -- |
||
23 | AddMenuItem = function(self, name, callbackFunc, submenu) |
||
24 | self:Init() |
||
25 | if not submenu then |
||
26 | table.insert(UnitPopupMenus["ADDON_MENU"], 1, name) |
||
27 | else |
||
28 | if not UnitPopupMenus[submenu] then |
||
29 | if self.debug then |
||
30 | ChatFrame1:AddMessage("AddMenuItem: Adding submenu |cff00ff00"..submenu.."|r") |
||
31 | end |
||
32 | table.insert(UnitPopupMenus["ADDON_MENU"], 1, submenu) |
||
33 | UnitPopupButtons[submenu] = { text = TEXT(submenu), dist = 0, nested = 1 } |
||
34 | UnitPopupMenus[submenu] = { "CANCEL" } |
||
35 | end |
||
36 | table.insert(UnitPopupMenus[submenu], 1, name) |
||
37 | end |
||
38 | if self.debug then |
||
39 | ChatFrame1:AddMessage("AddMenuItem: Adding menu item |cff00ff00"..name.."|r") |
||
40 | end |
||
41 | UnitPopupButtons[name] = { text = TEXT(name), dist = 0 } |
||
42 | self:InsertCallback(name, callbackFunc) |
||
43 | end, |
||
44 | |||
45 | ------------------------------------------- |
||
46 | -- Users should not call these functions -- |
||
47 | ------------------------------------------- |
||
48 | |||
49 | -- |
||
50 | -- AddonMenuName |
||
51 | -- Return the localised text for the addon menu name |
||
52 | -- |
||
53 | AddonMenuName = function(self) |
||
54 | -- if (GetLocale() == "deDE") then |
||
55 | -- return "GermanText" |
||
56 | -- elseif (GetLocale() == "frFR") then |
||
57 | -- return "FrenchText" |
||
58 | -- else |
||
59 | return "Addons" |
||
60 | -- end |
||
61 | end, |
||
62 | |||
63 | -- |
||
64 | -- InsertCallback |
||
65 | -- Insert a callback into the hook table |
||
66 | -- |
||
67 | InsertCallback = function(self, item, callbackFunc) |
||
68 | for index,entry in pairs(self.callbacks) do |
||
69 | if entry.name == item then |
||
70 | return |
||
71 | end |
||
72 | end |
||
73 | table.insert(self.callbacks, {name = item, func = callbackFunc}) |
||
74 | end, |
||
75 | |||
76 | -- |
||
77 | -- UnitPopup_OnClick |
||
78 | -- If the menu item clicked is in the hook table, call its callback |
||
79 | -- Otherwise call the global UnitPopup_OnClick |
||
80 | -- |
||
81 | UnitPopup_OnClick = function() |
||
82 | for index,entry in AddonMenu.callbacks do |
||
83 | if (this.value == entry.name) then |
||
84 | entry.func() |
||
85 | return |
||
86 | end |
||
87 | end |
||
88 | AddonMenu.unitPopupOnClick() |
||
89 | end, |
||
90 | |||
91 | -- |
||
92 | -- UnitPopup_ShowMenu |
||
93 | -- Override the original function with a copy of itself, making two small changes to support a third level of nesting. |
||
94 | -- Very ugly. |
||
95 | -- Unless we're in the self menu and above the first level of nesting, though, just passthrough |
||
96 | -- |
||
97 | UnitPopup_ShowMenu = function(dropdownMenu, which, unit, name, userData) |
||
98 | if (which == "SELF") and (UIDROPDOWNMENU_MENU_LEVEL > 1) then |
||
99 | -- Init variables |
||
100 | dropdownMenu.which = which; |
||
101 | dropdownMenu.unit = unit; |
||
102 | if ( unit and not name ) then |
||
103 | name = UnitName(unit); |
||
104 | end |
||
105 | dropdownMenu.name = name; |
||
106 | dropdownMenu.userData = userData; |
||
107 | |||
108 | -- Determine which buttons should be shown or hidden |
||
109 | UnitPopup_HideButtons(); |
||
110 | |||
111 | -- If only one menu item (the cancel button) then don't show the menu |
||
112 | local count = 0; |
||
113 | for index, value in UnitPopupMenus[which] do |
||
114 | if( UnitPopupShown[index] == 1 and value ~= "CANCEL" ) then |
||
115 | count = count + 1; |
||
116 | end |
||
117 | end |
||
118 | if ( count < 1 ) then |
||
119 | return; |
||
120 | end |
||
121 | |||
122 | -- Determine which loot method and which loot threshold are selected and set the corresponding buttons to the same text |
||
123 | dropdownMenu.selectedLootMethod = UnitLootMethod[GetLootMethod()].text; |
||
124 | UnitPopupButtons["LOOT_METHOD"].text = dropdownMenu.selectedLootMethod; |
||
125 | UnitPopupButtons["LOOT_METHOD"].tooltipText = UnitLootMethod[GetLootMethod()].tooltipText; |
||
126 | dropdownMenu.selectedLootThreshold = getglobal("ITEM_QUALITY"..GetLootThreshold().."_DESC"); |
||
127 | UnitPopupButtons["LOOT_THRESHOLD"].text = dropdownMenu.selectedLootThreshold; |
||
128 | -- This allows player to view loot settings if he's not the leader |
||
129 | if ( ((GetNumPartyMembers() > 0) or (GetNumRaidMembers() > 0)) and IsPartyLeader() ) then |
||
130 | -- If this is true then player is the party leader |
||
131 | UnitPopupButtons["LOOT_METHOD"].nested = 1; |
||
132 | UnitPopupButtons["LOOT_THRESHOLD"].nested = 1; |
||
133 | else |
||
134 | UnitPopupButtons["LOOT_METHOD"].nested = nil; |
||
135 | UnitPopupButtons["LOOT_THRESHOLD"].nested = nil; |
||
136 | end |
||
137 | |||
138 | -- If level 2 dropdown |
||
139 | local info; |
||
140 | local color; |
||
141 | local icon; |
||
142 | if ( UIDROPDOWNMENU_MENU_LEVEL > 1 ) then |
||
143 | dropdownMenu.which = UIDROPDOWNMENU_MENU_VALUE; |
||
144 | for index, value in UnitPopupMenus[UIDROPDOWNMENU_MENU_VALUE] do |
||
145 | info = {}; |
||
146 | info.text = UnitPopupButtons[value].text; |
||
147 | info.owner = UIDROPDOWNMENU_MENU_VALUE; |
||
148 | -- Set the text color |
||
149 | color = UnitPopupButtons[value].color; |
||
150 | if ( color ) then |
||
151 | info.textR = color.r; |
||
152 | info.textG = color.g; |
||
153 | info.textB = color.b; |
||
154 | end |
||
155 | -- Icons |
||
156 | info.icon = UnitPopupButtons[value].icon; |
||
157 | info.tCoordLeft = UnitPopupButtons[value].tCoordLeft; |
||
158 | info.tCoordRight = UnitPopupButtons[value].tCoordRight; |
||
159 | info.tCoordTop = UnitPopupButtons[value].tCoordTop; |
||
160 | info.tCoordBottom = UnitPopupButtons[value].tCoordBottom; |
||
161 | -- Checked conditions |
||
162 | if ( info.text == dropdownMenu.selectedLootMethod ) then |
||
163 | info.checked = 1; |
||
164 | elseif ( info.text == dropdownMenu.selectedLootThreshold ) then |
||
165 | info.checked = 1; |
||
166 | elseif ( strsub(value, 1, 12) == "RAID_TARGET_" ) then |
||
167 | local raidTargetIndex = GetRaidTargetIndex(unit); |
||
168 | if ( raidTargetIndex == index ) then |
||
169 | info.checked = 1; |
||
170 | end |
||
171 | end |
||
172 | |||
173 | if ( UnitPopupButtons[value].nested ) then |
||
174 | info.hasArrow = 1; |
||
175 | end |
||
176 | |||
177 | info.value = value; |
||
178 | info.func = UnitPopup_OnClick; |
||
179 | -- Setup newbie tooltips |
||
180 | info.tooltipTitle = UnitPopupButtons[value].text; |
||
181 | info.tooltipText = getglobal("NEWBIE_TOOLTIP_UNIT_"..value); |
||
182 | UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL); |
||
183 | end |
||
184 | return; |
||
185 | end |
||
186 | |||
187 | -- Add dropdown title |
||
188 | if ( unit or name ) then |
||
189 | info = {}; |
||
190 | if ( name ) then |
||
191 | info.text = name; |
||
192 | else |
||
193 | info.text = TEXT(UNKNOWN); |
||
194 | end |
||
195 | info.isTitle = 1; |
||
196 | info.notCheckable = 1; |
||
197 | UIDropDownMenu_AddButton(info); |
||
198 | end |
||
199 | |||
200 | -- Show the buttons which are used by this menu |
||
201 | local tooltipText; |
||
202 | for index, value in UnitPopupMenus[which] do |
||
203 | if( UnitPopupShown[index] == 1 ) then |
||
204 | info = {}; |
||
205 | info.text = UnitPopupButtons[value].text; |
||
206 | info.value = value; |
||
207 | info.owner = which; |
||
208 | info.func = UnitPopup_OnClick; |
||
209 | if ( not UnitPopupButtons[value].checkable ) then |
||
210 | info.notCheckable = 1; |
||
211 | end |
||
212 | -- Text color |
||
213 | if ( value == "LOOT_THRESHOLD" ) then |
||
214 | -- Set the text color |
||
215 | color = ITEM_QUALITY_COLORS[GetLootThreshold()]; |
||
216 | info.textR = color.r; |
||
217 | info.textG = color.g; |
||
218 | info.textB = color.b; |
||
219 | else |
||
220 | color = UnitPopupButtons[value].color; |
||
221 | if ( color ) then |
||
222 | info.textR = color.r; |
||
223 | info.textG = color.g; |
||
224 | info.textB = color.b; |
||
225 | end |
||
226 | end |
||
227 | -- Icons |
||
228 | info.icon = UnitPopupButtons[value].icon; |
||
229 | info.tCoordLeft = UnitPopupButtons[value].tCoordLeft; |
||
230 | info.tCoordRight = UnitPopupButtons[value].tCoordRight; |
||
231 | info.tCoordTop = UnitPopupButtons[value].tCoordTop; |
||
232 | info.tCoordBottom = UnitPopupButtons[value].tCoordBottom; |
||
233 | -- Checked conditions |
||
234 | if ( strsub(value, 1, 12) == "RAID_TARGET_" ) then |
||
235 | local raidTargetIndex = GetRaidTargetIndex("target"); |
||
236 | if ( raidTargetIndex == index ) then |
||
237 | info.checked = 1; |
||
238 | end |
||
239 | end |
||
240 | if ( UnitPopupButtons[value].nested ) then |
||
241 | info.hasArrow = 1; |
||
242 | end |
||
243 | |||
244 | -- Setup newbie tooltips |
||
245 | info.tooltipTitle = UnitPopupButtons[value].text; |
||
246 | tooltipText = getglobal("NEWBIE_TOOLTIP_UNIT_"..value); |
||
247 | if ( not tooltipText ) then |
||
248 | tooltipText = UnitPopupButtons[value].tooltipText; |
||
249 | end |
||
250 | info.tooltipText = tooltipText; |
||
251 | UIDropDownMenu_AddButton(info); |
||
252 | end |
||
253 | end |
||
254 | PlaySound("igMainMenuOpen"); |
||
255 | else |
||
256 | AddonMenu.origShowMenu(dropdownMenu, which, unit, name, userData) |
||
257 | end |
||
258 | end, |
||
259 | |||
260 | -- |
||
261 | -- Initialisation |
||
262 | -- |
||
263 | Init = function(self) |
||
264 | if not self.initialised then |
||
265 | if self.debug then |
||
266 | ChatFrame1:AddMessage(string.format("AddMenuitem: Initializing version |cff00ff00%.1f|r", self.version)) |
||
267 | end |
||
268 | if prevAddonMenu then |
||
269 | if self.debug then |
||
270 | ChatFrame1:AddMessage(string.format("AddMenuitem: Migrating data from AddonMenu version %.1f", prevAddonMenu.version)) |
||
271 | end |
||
272 | if prevAddonMenu.unitPopupOnClick then |
||
273 | self.unitPopupOnClick = prevAddonMenu.unitPopupOnClick |
||
274 | else |
||
275 | self.unitPopupOnClick = getglobal("UnitPopup_OnClick") |
||
276 | end |
||
277 | if prevAddonMenu.origShowMenu then |
||
278 | self.origShowMenu = prevAddonMenu.origShowMenu |
||
279 | else |
||
280 | self.origShowMenu = getglobal("UnitPopup_ShowMenu") |
||
281 | end |
||
282 | self.callbacks = prevAddonMenu.callbacks |
||
283 | prevAddonMenu = {} |
||
284 | else |
||
285 | self.callbacks = {} |
||
286 | self.unitPopupOnClick = getglobal("UnitPopup_OnClick") |
||
287 | self.origShowMenu = getglobal("UnitPopup_ShowMenu") |
||
288 | UnitPopupButtons["ADDON_MENU"] = { text = self:AddonMenuName(), dist = 0, nested = 1 } |
||
289 | table.insert(UnitPopupMenus["SELF"], table.getn(UnitPopupMenus["SELF"]), "ADDON_MENU") |
||
290 | UnitPopupMenus["ADDON_MENU"] = {} |
||
291 | end |
||
292 | UnitPopup_OnClick = self.UnitPopup_OnClick |
||
293 | UnitPopup_ShowMenu = self.UnitPopup_ShowMenu |
||
294 | self.initialised = true |
||
295 | end |
||
296 | end |
||
297 | } |
||
298 | end |