WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Right mouse button clicks on menu buttons (https://www.wowinterface.com/forums/showthread.php?t=52368)

Niatue 06-14-15 12:42 PM

Right mouse button clicks on menu buttons
 
Hello, I have an EasyMenu DropDownMenu and I want to be able to both right and left click on the buttons and handle each button click differently. However, right button clicks are not registered at all and there's no way to even tell the left button has been clicked.

My code is:
Code:

local menuFrame = CreateFrame("Frame", "MenuFrame", DropDownButton, "UIDropDownMenuTemplate")

local menuList = {       
        { text = "Menu 1", hasArrow = true, notCheckable = true,
        menuList = {
            { text = "First option", notCheckable = true,
                func = function() print("1st option clicked") end },
                       
            { text = "Second option", notCheckable = true,
                func = function() print("2nd option clicked") end }
        }
    }
}

local function ShowMenu(self, button)
        EasyMenu(menuList, menuFrame, "cursor", 0, 0, "MENU")
end

local button = CreateFrame("Button", "DropDownButton", UIParent, "UIPanelButtonTemplate")
button:SetSize(100, 50)
button:SetText("Menu")
button:SetPoint("CENTER")
button:RegisterForClicks("RightButtonDown", "LeftButtonDown")
button:SetScript("OnClick", ShowMenu)
button:Show()


Is there any way to register both buttons and distinguish between the two via one of the 2 custom arguments menuList takes? Other than having to implement my own DropDownMenu. Is there perhaps a function than can be hooked to "force" it to recognize both buttons?

I'd like to do something like this:

Code:

local menuList = {       
    { text = "Menu 1", hasArrow = true, notCheckable = true,
        menuList = {
          { text = "First option", arg1 = "1st option clicked", arg2 = button,
            notCheckable = true, func = function()
                if button == "RightButton" then
                    print(arg1)
                else
                    print("Left button clicked!")
                end
          end},                       
          }
    }
}


Seerah 06-14-15 04:29 PM

Try this to see if any arguments are passed through to your function maybe?
Lua Code:
  1. func = function(...) print(...) end
If there are, and one of them is which button was used, then it's easy. Otherwise, not so much. ;)

Niatue 06-14-15 11:31 PM

The signature of the function is func(self, arg1, arg2, checked). Arg1 and arg2 are user defined variables and can be literally anything but unlike most OnClick functions, the button state is not passed!

In fact, right button clicks are not recognized at all, meaning, when I right-click a button nothing happens. It only recognizes left-clicks and I'm trying to change that.

Each menuList table has a specific signature more on which can be found here: UI Object UIDropDownMenu and EasyMenu

Lombra 06-16-15 06:47 AM

It can be done, but it's a bit hacky.

First hook the function that creates menu buttons to make sure they're registered for right clicks. (unless you have a reason to use *Down, I'd use *Up instead, as that's what the rest of the UI uses)
Code:

local function createFramesHook(numLevels, numButtons)
        for level = 1, numLevels do
                for i = 1, numButtons do
                        _G["DropDownList"..level.."Button"..i]:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                end
        end
end

createFramesHook(UIDROPDOWNMENU_MAXLEVELS, UIDROPDOWNMENU_MAXBUTTONS)
hooksecurefunc("UIDropDownMenu_CreateFrames", createFramesHook)

Then use GetMouseButtonClicked() in func to determine which button was clicked.

Niatue 06-16-15 02:59 PM

That's exactly what I wanted! Thank you, Lombra.


All times are GMT -6. The time now is 07:49 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI