Пользовательский контроллер с NiftyGUI

У меня есть пользовательская кнопка, которая переключает изображение при нажатии, поэтому мне нужно обработать щелчок мышью. Но он реагирует только на keyEvent в функции "inputEvent(NiftyInputEvent inputEvent)"...

Я должен был бы передать NiftyMousePrimaryClickedEvent, но тогда он не реализовал бы класс Controller... Я делаю это правильно?

Вот моя пользовательская кнопка изображения:

   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);
}}

Вот мой 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>

1 ответ

Решение

Лучше всего добавить на панель элемент "взаимодействовать":

<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 -->

и добавьте соответствующий метод-обработчик onClick() в реализацию Controller:

public class ImageButtonControl implements Controller {
    ...

    public boolean onClick() {
       // TODO add mouse click handling here
    }

Nifty разрешит методы взаимодействия снизу вверх, начиная с самого внутреннего контроллера и заканчивая ScreenController, и первое совпадение выигрывает и вызывается.

Другие вопросы по тегам