Quantcast Context Menu Maker - WoWWiki - Your guide to the World of Warcraft
Recent changes Random page
GAMING
Gaming
 
StarCraft Wiki
Super Smash Wiki
Halopedia
Diablo Wiki
FFXIclopedia
Grand Theft Wiki
See more...

Context Menu Maker

From WoWWiki

Jump to: navigation, search
This is a user-defined function that you can copy and paste into your addon. Replace <PREFIX> with your addon's prefix to avoid conflicts between different versions of these functions.

Create a drop down menu at your leisure and show it when you're ready. This is a very basic context menu, nothing advanced.

Contents

Usage

local menu = MenuClass:New()
menu:AddItem('Do Something', function()
    SendChatMessage("Hello Bob!", "WHISPER", "Common", "Bob")
end)
menu:AddItem('Close', function()
    -- do nothing, just close.
end)

local f = SomeClickableFrame
f:SetScript('OnClick', function(self, button)
    menu:Show()
end)

Properties

.menuItems 
Table. Stores the menu items.
.anchor 
String. The anchor.
.x 
The x offset
.y 
The y offset
.displayMode 
The display mode.
.autoHideDelay 
The time it takes for the menu to automatically hide.
.menuFrame 
The menu frame.
.uniqueID 
A self incrementing number used to ensure that the generic menu frame is unique.
.isTitle 
Is this menu item a header? 1 if yes, nil if no.

Methods

:New() 
Allocate memory for the new button and set the defaults. Required. Returns the menu.
:AddItem(<string> text, <function> func, <number> isTitle) 
Add a menu item with text text that does func. Requires at least 1.
:RemoveItem(<string> text) 
Remove the first menu item that matches text
:SetAnchor(<string> anchor) 
Set the anchor. Optional. Default: "cursor"
:SetDisplayMode(<string> displayMode) 
Set the displayMode. Optional. Default: "MENU"
:SetAutoHideDelay(<number> autoHideDelay) 
Set the autoHideDelay. Optional. Default: nil (does not auto close)
:SetMenuFrame(<menuFrame> menuFrame) 
Set the menuFrame. Optional. Default: self generating menuFrame. If set, this must be a frame that inherits UIDropDownMenuButtonTemplate.
:SetX(<number> x[, <boolean> save]) 
Set the x offset. Optional. Default: nil (0 if anchor is not "cursor"). If save resolves to true, x will be added to rather than replaced.
:SetY(<number> y[, <boolean> save]) 
Set the y offset. Optional. Default: nil (0 if anchor is not "cursor"). If save resolves to true, y will be added to rather than replaced.
:Show() 
Show the menu.

Code

local MenuClass = {}

function MenuClass:New()
    local ret = {}
    
    -- set the defaults
    ret.menuItems = {};
    ret.anchor = 'cursor'; -- default at the cursor
    ret.x = nil;
    ret.y = nil;
    ret.displayMode = 'MENU'; -- default
    ret.autoHideDelay = nil;
    ret.menuFrame = nil; -- If not defined, :Show() will create a generic menu frame
    ret.uniqueID = 1

    -- import the functions
    for k,v in pairs(self) do
        ret[k] = v
    end
    
    -- return a copy of the class
    return ret
end

--[[
    Add menu items
    ; text : The display text.
    ; func : The function to execute OnClick.
    ; isTitle : 1 if this is a header (usually the first one)
--]]
function MenuClass:AddItem(text, func, isTitle)
    table.insert(self.menuItems, {
        ["text"] = text,
        ["func"] = func,
        ["isTitle"] = isTitle,
    })
end

--[[
    Remove the first item matching "text"
    ; text : The text to search for.
--]]
function MenuClass:RemoveItem(text)
    for k,v in pairs(self.menuItems) do
        if v.text == text then
            table.remove(self.menuItems, k)
            return
        end
    end
end

--[[
    ; anchor : Set the anchor point. 
--]]
function MenuClass:SetAnchor(anchor)
    if anchor ~= 'cursor' then
        self.x = 0
        self.y = 0
    end
    self.anchor = anchor
end

--[[
    ; displayMode : "MENU"
--]]
function MenuClass:SetDisplayMode(displayMode)
    self.displayMode = displayMode
end

--[[
    ; autoHideDelay : How long, without a click, before the menu goes away.
--]]
function MenuClass:SetAutoHideDelay(autoHideDelay)
    self.autoHideDelay = tonumber(autoHideDelay)
end

--[[
    ; menuFrame : Should inherit a Drop Down Menu template.
--]]
function MenuClass:SetMenuFrame(menuFrame)
    self.menuFrame = menuFrame
end

--[[
    ; x : X position
    ; save : When not nil, will add to the current value rather than replace it
--]]
function MenuClass:SetX(x, save)
    if save then
        self.x = self.x + x
    else
        self.x = x
    end
end

--[[
    ; y : Y position
    ; save : When not nil, will add to the current value rather than replace it
--]]
function MenuClass:SetY(y, save)
    if save then
        self.y = self.y + y
    else
        self.y = y
    end
end

--[[
    Show the menu.
--]]
function MenuClass:Show()
    if not self.menuFrame then
        while _G['GenericMenuClassFrame'..self.uniqueID] do -- ensure that there's no namespace collisions
            self.uniqueID = self.uniqueID + 1
        end
        -- the frame must be named for some reason
        self.menuFrame = CreateFrame('Frame', 'GenericMenuClassFrame'..self.uniqueID, UIParent, "UIDropDownMenuTemplate")
    else
        EasyMenu(self.menuItems, self.menuFrame, self.anchor, self.x, self.y, self.displayMode, self.autoHideDelay)
    end
end
Rate this article:
Share this article: