javaxmluser-interfacejmonkeyenginenifty-gui

Custom controller with NiftyGUI


I have a custom button that switche image when clicked so I need to handle a mouseEvent click. But, it only react to keyEvent on the function "inputEvent(NiftyInputEvent inputEvent)"...

I would have to pass a NiftyMousePrimaryClickedEvent but then, it would'nt implements the Controller class...Am I doing this right?

Here my custom image button:

   public class ImageButtonControl implements Controller{
private Nifty nifty;
private Screen screen;
private Element img1;
private Element img2;
private Properties properties;
private boolean isPressed = false;
private ImageRenderer render1;
private NiftyImage imageNifty1;
private NiftyImage imageNifty2;
private FocusHandler focusHandler;

@Override
public void bind(
  final Nifty nifty,
  final Screen screenParam,
  final Element element,
  final Properties parameter,
  final Attributes controlDefinitionAttributes) {
    this.nifty = nifty;
    this.screen = screenParam;
    this.properties = parameter;
    img1 = element.findElementByName("image-1");
    img2 = element.findElementByName("image-2");
    initImages(this.nifty, this.screen, properties.getProperty("img-1"),properties.getProperty("img-2"));
    //focusHandler = screenParam.getFocusHandler();
    System.out.println("Initialisation completed.");}
@Override
public void init(Properties prprts, Attributes atrbts) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void onStartScreen() {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void onFocus(final boolean getFocus) {
    //super.onFocus(getFocus);
     System.out.println("---Clicked event focus---");
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean inputEvent(NiftyInputEvent inputEvent) {
    System.out.println("---Clicked event focus---");
    return false;
}


public void switchImage(){
   //ImageRenderer imageRenderer = img1.getRenderer(ImageRenderer.class);
    if(isPressed){
        isPressed = false;
        //img1.show();
        //img2.hide();
        System.out.println("Clicked event");
    }else{
        isPressed = true;
        //img1.hide();
        //img2.show();
        System.out.println("Clicked event");
    }
}

private void initImages(Nifty nifty, Screen screen, String imgPath1, String imgPath2) {
    NiftyRenderEngine renderEngine = nifty.getRenderEngine();
    imageNifty1 = renderEngine.createImage(screen,imgPath1,false);
    imageNifty2 = renderEngine.createImage(screen,imgPath2,false);
}}

Here is my XML:

  <controlDefinition name="imagebutton" style="nifty-panel-style" controller="de.lessvoid.nifty.customs.imagebutton.ImageButtonControl">
    <panel style="#panel" childLayout="overlay" focusable="true" visibleToMouse="true" width="$width" heigth="$height">
      <image id="img-1" name="image-1" style="#select" filename="$img1" visibleToMouse="true"/>
      <image id="img-2" name="image-2" style="#select" filename="$img2" visibleToMouse="true"/>
    </panel>
  </controlDefinition>

Solution

  • Best way would be to add an 'interact' element to the panel:

    <controlDefinition name="imagebutton" style="nifty-panel-style" controller="de.lessvoid.nifty.customs.imagebutton.ImageButtonControl">
        <panel style="#panel" childLayout="overlay" focusable="true" visibleToMouse="true" width="$width" heigth="$height">
    
            <!-- add this line to the panel -->
            <interact onClick="onClick()" />
    
            <!-- ... your other stuff -->
    

    and add the corresponding 'onClick()' handler method to the Controller implementation:

    public class ImageButtonControl implements Controller {
        ...
    
        public boolean onClick() {
           // TODO add mouse click handling here
        }
    

    Nifty will resolve interact methods bottom-up beginning at the inner most Controller upwards to the ScreenController and the first match wins and is called.