An alternative to using Blizzard's UIDropDownMenuTemplate and EasyMenu.
Example usage:
Code:
local menu = LibStub('LibMenuAssist-1.0'):New()
menu:SetAnchor(0, 0, 'TOP', myAddon.button, 'BOTTOM')
menu.initialize = function(self, level)
-- Do stuff
end
myAddon.button:SetScript('OnClick', function()
menu:Open()
end)
Example using the built-in initialize function:
Code:
local menu = LibStub('LibMenuAssist-1.0'):New()
menu:SetAnchor(0, 0, 'TOP', myAddon.button, 'BOTTOM')
menu[0] = function(self, level)
-- Called first each time menu.initialize is called
end
menu[1] = function(self, level)
-- Called when menu.initialize is called and level == 1 (*)
end
menu[2] = function(self, level)
-- Called when menu.initialize is called and level == 2 (*)
end
myAddon.button:SetScript('OnClick', function()
menu:Open()
end)
(*) - menu[1+] can be assigned a table instead, which will be processed via EasyMenu_Initialize.
API
menu = lib:New()
Create a new menu object.
Returns:
menu - (table) The menu object to be used by the calling code (see Menu Objects below).
Menu Objects
Any menu created by lib:New() is a normal table that emulates certain frame behaviors to allow it to work with the Blizzard's UIDropDownMenu code. A metatable provides the following frame-like methods: GetScript, HasScript, Hide, IsShown, IsVisible, SetScript, and Show. The only scripts supported by a menu are OnHide and OnShow. The metatable also defaults the field displayMode to 'MENU', set it to false to override. Menus also have the following additional methods:
menu:AddFocusFrame(frame)
Add a frame that is to be considered a part of the menu when calling the methods HasMouseFocus and UpdateAutoHide.
Arguments:
frame - (table) The frame to be added.
wasOpen = menu:Close()
Close the menu.
Returns:
wasOpen - (boolean or nil) True if the menu was open, nil otherwise.
hasFocus = menu:HasMouseFocus()
Determines if GetMouseFocus() returns any portion of the menu or a frame added with the method AddFocusFrame.
Returns:
hasFocus - (boolean or nil) True if the mouse focus is part of the menu, nil otherwise.
isOpen = menu:IsOpen()
Determines if the menu is open.
Returns:
isOpen - (boolean or nil) True if the menu is open, nil otherwise.
level = menu:Open([...])
Open the menu to level 1 and attempt to open additional menu levels with any values passed as arguements.
Arguments:
... - Optional values leading to sub-menu levels.
Returns:
level - (number or nil) The highest menu level that is open, nil otherwise.
menu:Recycle()
Removes the metatable from menu as well as removing all internal references so that it may be be garbage collected.
level = menu:Refresh()
Force a display update of menu.
Returns:
level - (number or nil) The highest menu level that is open, nil otherwise.
menu:RemoveAllFocusFrames()
Remove all frames previously added with AddFocusFrame.
menu:RemoveFocusFrame(frame)
Remove a frame previously added with AddFocusFrame.
Arguments:
frame - (table) The frame to be removed.
updated = menu:SetAnchor(xOffset, yOffset, point, relativeTo, relativePoint)
Similar to UIDropDownMenu_SetAnchor, it updates the menu values but also automatically updates the menu's position if any values have changed.
Arguments:
xOffset - (number or nil) The new value for menu.xOffset.
yOffset - (number or nil) The new value for menu.yOffset.
point - (string or nil) The new value for menu.point.
relativeTo - (string, table, or nil) The new value for menu.relativeTo.
relativePoint - (string or nil) The new value for menu.relativePoint.
Returns:
updated - (boolean or nil) True if the menu was updated, nil otherwise.
isOpen = menu:Toggle()
Close the menu if it is open, otherwise opens it.
Returns:
isOpen - (boolean or nil) True if the menu is open, nil otherwise.
isOpen = menu:UpdateAutoHide()
Starts, stops, or resets the menu's countdown to auto-hide based on the return value of the method HasMouseFocus.
Returns:
isOpen - (boolean or nil) True if the menu is open, nil otherwise.