1

我有一个自定义按钮,可以在单击时切换图像,因此我需要处理 mouseEvent 单击。但是,它只对函数“inputEvent(NiftyInputEvent inputEvent)”上的keyEvent做出反应......

我将不得不传递一个 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>
4

1 回答 1

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()”处理程序方法添加到控制器实现中:

public class ImageButtonControl implements Controller {
    ...

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

Nifty 将自下而上解析交互方法,从最里面的 Controller 开始向上到 ScreenController,第一个匹配获胜并被调用。

于 2014-05-24T15:59:25.627 回答