WoW:API EasyMenu: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(a note about naming frames)
(→‎Example: single block of code)
Line 1: Line 1:
{{framexmlfunc|FrameXML/EasyMenu.lua}}
{{framexmlfunc|FrameXML/EasyMenu.lua}} __NOTOC__
Easily create context menus on the fly with clickable items.
Populate a context menu with options described in a provided table.
  EasyMenu(menuList, menuFrame, anchor, x, y, displayMode, autoHideDelay)
  EasyMenu(menuList, menuFrame, anchor, x, y, displayMode, autoHideDelay)


==Arguments==
==Arguments==
;menuList : Table - see [[#Details|Details]] for key/value descriptions.
;menuList : Table - an array of tables describing the entries in the menu to be created. The descriptions may use any key-value tables used in a [[UI_Object_UIDropDownMenu#The_info_table|UIDropDownMenu info table]]; as a bare minimum, each option should specify the "text" key.
;menuFrame : Frame - the UI frame to populate.
;menuFrame : Frame - a Frame object that will be used to store some information about the menu.
;anchor : String - Just like in the [[UIOBJECT_GameTooltip|GameTooltip]] this is the anchor point.
;anchor : String/Region - Specify what to anchor the menu relative to: either "cursor", a region name, or a region (frame) reference.
;x : Number - x offset.
;x : Number - x offset from the anchor.
;y : Number - y offset
;y : Number - y offset from the anchor.
;displayMode : String - "MENU" enables a tooltip-styled context menu, any other value the dropdown style.
;displayMode : String - "MENU" enables a tooltip-styled context menu, any other value the dropdown style.
;autoHideDelay : Number - Automatically hide the menu after this many seconds.
;autoHideDelay : Number - Automatically hide the menu after this many seconds.


==Details==
== Details ==
;menuList : This needs to be a table in the following format.
{
    {
        text = "Menu Item 1", -- string. This is the text to put on this menu item.
        func = function() DoStuff() end, -- function. This is the function that will fire when you click on this menu item.
    },
    {
        text = "Menu Item 2",
        func = function() DoOtherStuff() end,
    },
    -- ...
}
 
:See "List of button attributes" in the [[FrameXML]] file [http://wdn.wowinterface.com/code/live/FrameXML/UIDropDownMenu.lua ''UIDropDownMenu.lua''] for the full list of available table elements.
 
== Notes ==
* The menu becomes visible as soon as you call the function and goes away after you click a menu item unless keepShownOnClick in '''menuList''' was set to 1.
* The menu becomes visible as soon as you call the function and goes away after you click a menu item unless keepShownOnClick in '''menuList''' was set to 1.
* Fine-grained control over the positioning of the menu is possible by setting the menuFrame.point and menuFrame.relativePoint values to specific anchor points to use when anchoring the frame to a non-cursor region.


==Examples==
==Example==
 
The following example creates a context menu based on a table description:
  local menu_frame = CreateFrame("Frame", "ExampleMenuFrame", UIParent, "UIDropDownMenuTemplate")
  local menu = {
  { text = "Select an Option", isTitle = true},
  { text = "Option 1", func = function() print("You've chosen option 1"); end },
  { text = "Option 2", func = function() print("You've chosen option 2"); end },
  { text = "More Options", hasArrow = true,
    menuList = {
    { text = "Option 3", func = function() print("You've chosen option 3"); end }
    }
  }
};
local menuFrame = CreateFrame("Frame", "ExampleMenuFrame", UIParent, "UIDropDownMenuTemplate")
   
   
  local function onOption1()
  -- Make the menu appear at the cursor:
  -- do stuff
  EasyMenu(menuList, menuFrame, "cursor", 0 , 0, "MENU");
end
-- Or make the menu appear at the frame:
local function onOption2()
  menu_frame:SetPoint("Center", UIParent, "Center")
  -- do stuff
  EasyMenu(menuList, menuFrame, menuFrame, 0 , 0, "MENU");
end
local menuList = {
  { text = "Option 1", func = onOption1 },
  { text = "Option 2", func = onOption2 }
}
menu_frame:SetPoint("Center", UIParent, "Center") -- setup position of menu. Not needed if 'cursor' is used as anchor in EasyMenu
  EasyMenu(menuList, menuFrame, "ExampleMenuFrame", 0 , 0, "MENU")
 
Above example creates "MENU" type poupup menu.
To crate standard menu:
  local menu_frame = CreateFrame("Frame", "ExampleMenuFrame", UIParent)
...
  EasyMenu(menuList, menuFrame, "ExampleMenuFrame", 0 , 0)
 
<b>Note:</b> You <i>must</i> give the frame a name (argument 2 to CreateFrame()) for menus to work.


== See Also ==  
== See Also ==  
* [[UI Object UIDropDownMenu]]
* [[UI Object UIDropDownMenu]]
* [[Using UIDropDownMenu]]
* [[Using UIDropDownMenu]]

Revision as of 01:06, 8 April 2010

WoW API < EasyMenu

"I" iconThis function is implemented in Lua here FrameXML/EasyMenu.lua.

Populate a context menu with options described in a provided table.

EasyMenu(menuList, menuFrame, anchor, x, y, displayMode, autoHideDelay)

Arguments

menuList
Table - an array of tables describing the entries in the menu to be created. The descriptions may use any key-value tables used in a UIDropDownMenu info table; as a bare minimum, each option should specify the "text" key.
menuFrame
Frame - a Frame object that will be used to store some information about the menu.
anchor
String/Region - Specify what to anchor the menu relative to: either "cursor", a region name, or a region (frame) reference.
x
Number - x offset from the anchor.
y
Number - y offset from the anchor.
displayMode
String - "MENU" enables a tooltip-styled context menu, any other value the dropdown style.
autoHideDelay
Number - Automatically hide the menu after this many seconds.

Details

  • The menu becomes visible as soon as you call the function and goes away after you click a menu item unless keepShownOnClick in menuList was set to 1.
  • Fine-grained control over the positioning of the menu is possible by setting the menuFrame.point and menuFrame.relativePoint values to specific anchor points to use when anchoring the frame to a non-cursor region.

Example

The following example creates a context menu based on a table description:

local menu = {
 { text = "Select an Option", isTitle = true},
 { text = "Option 1", func = function() print("You've chosen option 1"); end },
 { text = "Option 2", func = function() print("You've chosen option 2"); end },
 { text = "More Options", hasArrow = true,
   menuList = {
    { text = "Option 3", func = function() print("You've chosen option 3"); end }
   } 
 }
};
local menuFrame = CreateFrame("Frame", "ExampleMenuFrame", UIParent, "UIDropDownMenuTemplate")

-- Make the menu appear at the cursor: 
EasyMenu(menuList, menuFrame, "cursor", 0 , 0, "MENU");
-- Or make the menu appear at the frame:
menu_frame:SetPoint("Center", UIParent, "Center")
EasyMenu(menuList, menuFrame, menuFrame, 0 , 0, "MENU");

See Also