[SOLVED]Nifty Menu doesnt capture mouseEvent

Im having a problem with Nifty Menu where the mouseEvent is not consumed when using the left mouse button to select the menu item.

I have not found any reports of this problem anywhere on the forums so I assume its something I am doing to make this happen.

Has anyone else seen this behavior?

I have tried adding a custom control definition (hacked version of the default definition that uses the nifty default controllers ) edited to use “onPrimaryRelease” for the mouse and made the panels also “visibleToMouse” as a work around.

<?xml version="1.0" encoding="UTF-8"?>
<nifty-controls xmlns="http://nifty-gui.lessvoid.com/nifty-gui">
    <!-- the menu control -->
    <controlDefinition name="popupMenu" controller="de.lessvoid.nifty.controls.menu.MenuControl" >
        <panel style="nifty-panel-bright" width="150px" childLayout="vertical" paddingLeft="0px" paddingRight="0px" visibleToMouse="true">
        </panel>
    </controlDefinition>

    <!-- the menu item control -->
    <controlDefinition name="popupMenuItem" controller="de.lessvoid.nifty.controls.MenuItemControl"
                       inputMapping="de.lessvoid.nifty.input.mapping.MenuInputMapping">
        <panel style="niftyMenuItemStyle" visibleToMouse="true">
            <interact onPrimaryRelease="$menuOnClick" onSecondaryRelease="$menuOnClick" onSecondaryClick="$menuOnClick"/>
            <panel width="10px" height="23px"/>
            <image id="#icon" visible="$menuIconVisible" width="23px" height="23px" filename="$menuIcon"/>
            <panel width="5px" height="23px"/>
            <control id="#text" style="#text" name="label" text="$menuText"/>
        </panel>
    </controlDefinition>

    <!-- the menu item separator control -->
    <controlDefinition name="popupMenuItemSeparator">
        <panel style="niftyMenuItemSeparatorStyle">
            <panel style="#line"/>
        </panel>
    </controlDefinition>

    <!-- and the popup -->
    <popup id="customPopupMenu" childLayout="absolute-inside"
           controller="de.lessvoid.nifty.controls.menu.PopupMenuControl">
        <interact onPrimaryRelease="closePopup()" onSecondaryClick="closePopup()" />
        <control id="#popupmenu" name="popupMenu"/>
    </popup>
</nifty-controls>

This definition loads with no errors and I use it in the menu creation code but all changes to the definition are ignored.

What am I doing wrong that allows this to be ignored?

    private void createPopup() {
        this.popup = nifty.createPopup("customPopupMenu");
        
        popupMenu = popup.findNiftyControl("#popupmenu", Menu.class);
        
        popupMenu.setWidth(new SizeValue("250px"));
        popupMenu.addMenuItem("Lock/Unlock MiniMap Drag", new DragNDrop("minimap_drag"));
        popupMenu.addMenuItemSeparator();
        popupMenu.addMenuItem("Exit", new DragNDrop("exit"));
    }

de.lessvoid.nifty.controls.MenuControl has a call to the original definition inside it.

Changed that call to point to my definition and now all is right with the world.

Weird how you can spend days on things and within an hour of posting for help you figure it out.

A final question.

Is there a way to find an <interact> element in a default control definition and change its mouse events?

All that was needed to fix this was to change this line

in the default controls “niftyMenuItemStyle” panel to use “onPrimaryRelease”.

<interact onPrimaryRelease="$menuOnClick" onSecondaryRelease="$menuOnClick" onSecondaryClick="$menuOnClick"/>

Yes, you can get set interactions directly on an element by calling “getElementInteraction()” and then settings the appropriate callback.

Wouldn’t that only work for,

Finding the line 13 interact, which is wrapped inside a definition and in a panel with no id, doesn’t seem to be working for me.

Add break point in your code and look at the popup element and dig into it to find the right children. You don’t necessarily need an ID to find the right elements in the nifty DOM, but you do need to understand how it builds things. Ultimately, nifty elements form a tree very similar to how to the jmonkey nodes work. You can get access to pretty much any element or setting in nifty as long as you know how to traverse that tree.

Excuse my continued ignorance.

I figured out how to get the interact element I am after but I don’t see how to change it from onClick to onPrimaryRelease using getElementInteraction().

I see how to change the method onClick calls but that’s about it.

  • getElementInteraction().getPrimary().setOnClickMethod(onClickMethod);

Can you be more specific on the setter for onPrimaryRelease?

Finally figured it out.

    private void createPopup() {
        this.popup = nifty.createPopup("niftyPopupMenu");
        
        Menu<DragNDrop> popupMenu = popup.findNiftyControl("#menu", Menu.class);
        
        popupMenu.setWidth(new SizeValue("250px"));
        popupMenu.addMenuItem("Lock/Unlock MiniMap Drag", new DragNDrop("minimap_drag"));
        popupMenu.addMenuItemSeparator();
        popupMenu.addMenuItem("Exit", new DragNDrop("exit"));
        
        //grab all elements of the menu
        for(Element e: popupMenu.getElement().getChildren()) { 
            //skip item separator(null element id)
            if (e.getId() != null) {
                //null out onClick for primary
                NiftyMethodInvoker onClickedMethod = new NiftyMethodInvoker(nifty);
                e.getElementInteraction().getPrimary().setOnClickMethod(onClickedMethod);
                //set onRelease for primary
                NiftyMethodInvoker onReleaseMethod = new NiftyMethodInvoker(nifty, "activateItem(" + e.getId() + ")", popupMenu);
                e.getElementInteraction().getPrimary().setOnReleaseMethod(onReleaseMethod);
            } 
        }
    }

Thanks for the nudge in the right direction.

Would have figured it out sooner if the documentation for the methods was complete. Way to much trial and error plus reading sources required just to do something like this.