Function not executing

I made a UI with nifty in Java. Currently I have the problem that my function is not executing when called (on click). My code:

[java]/*

package dae.repest.gui;

import dae.repest.level.event.BadgeEventPart;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.PopupBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.Properties;

public class BadgePopupUI implements Controller {

private BadgeEventPart oep;
private Nifty nifty;
private Element popupElement;
private Screen screen;
private String _name;

public BadgePopupUI(String name) {
    this._name = name;
}

public void buildUI(Nifty nifty, BadgeEventPart part) {
    nifty.loadStyleFile("nifty-default-styles.xml");
    nifty.loadControlFile("nifty-default-controls.xml");
    oep = part;

    // <screen>

    PopupBuilder builder = new PopupBuilder("choicepopup") {
        {
            id("choicepopup");
            controller(BadgePopupUI.this); // Screen properties       
            childLayoutAbsolute(); // layer properties, add more...
            // <panel>
            panel(new PanelBuilder("Panel_ID") {
                {
                    onShowEffect(
                            new EffectBuilder("fade") {
                        {
                            length(2000);
                            inherit(true);
                            post(false);

                            effectParameter("start", "#00");
                            effectParameter("end", "#ff");
                        }
                    });
                    backgroundImage("gui/HUD/PanelBackground.png");
                    imageMode("resize:36,34,36,40,36,34,36,34,36,34,36,40");
                    paddingLeft("20px");
                    paddingRight("20px");
                    paddingTop("20px");
                    paddingBottom("20px");
                    width("550px");
                    height("200px");
                    x("25%");
                    y("25%");
                    childLayoutCenter(); // panel properties, add more...

                    panel(
                            new PanelBuilder("innerPanel") {
                        {
                            childLayoutHorizontal();
                            image(new ImageBuilder() {
                                {
                                    filename("gui_FA/HUD/badges/" + _name + ".png");
                                }
                            });
                            text(new TextBuilder() {
                                {
                                    text("Proficiat met je badge ! "
                                            + "Bekijk je badges in het badge menu (duw op '1')");
                                    wrap(true);
                                    font("fonts/comicbook_32.fnt");
                                    height("100%");
                                    width("70%");
                                    interactOnClick("buttonClicked()");
                                }
                            });

                            text(new TextBuilder() {
                                {
                                    text("X");
                                    wrap(true);
                                    font("fonts/comicbook_32.fnt");
                                    height("100%");
                                    width("5%");
                                    interactOnClick("buttonClicked()");
                                }
                            });
                            control(new ButtonBuilder("Button_ID", "Hello Nifty") {
                                {
                                    alignCenter();
                                    valignCenter();
                                    height("15%");
                                    width("15%");
                                    interactOnClick("buttonClicked()");
                                }
                            });
                        }
                    });
                    //.. add more GUI elements here              

                }
            });
            // </panel>
        }
    };
    builder.registerPopup(nifty);
    Element popup = nifty.createPopup("choicepopup");
    Controller c = popup.getControl(Controller.class);
    if (c instanceof BadgePopupUI) {
        BadgePopupUI cui = (BadgePopupUI) c;
        cui.oep = oep;
    }
    nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);
}

@Override
public void bind(Nifty nifty, Screen screen, Element elmnt, Properties prprts, Attributes atrbts) {
    this.nifty = nifty;
    this.screen = screen;
    this.popupElement = elmnt;
}

@Override
public void init(Properties parameter, Attributes controlDefinitionAttributes) {
}

@Override
public void onStartScreen() {
    System.out.println("Badge screen started");
}

@Override
public void onFocus(boolean getFocus) {
}

@Override
public boolean inputEvent(NiftyInputEvent inputEvent) {
    return true;
}

public void buttonClicked() {
    System.out.println("CLICKED ON THE X");
    //nifty.removeScreen("OptionScreen");
    nifty.closePopup(popupElement.getId());

}

}
[/java]

As you can see I put [java]interactOnClick(“buttonClicked()”);[/java] to see if it would make a difference, but it doensn’t. Anyone know why the function is not executing ?

try add visibleToMouse() in the builders and see if that does anything

Didn’t change anything. When I used an external class for my controller instead of the ui itself it works. Though I need it to work like this because I need the variable [java]oep [/java]in the function [java]buttonClicked()[/java]

Maybe this one helps

[java]
private BadgePopupUI me;

public BadgePopupUI(String name) {
    this._name = name;
    me = this
}

public void buildUI(Nifty nifty, BadgeEventPart part) {
    nifty.loadStyleFile(“nifty-default-styles.xml”);
    nifty.loadControlFile(“nifty-default-controls.xml”);
    oep = part;

    // <screen>

    PopupBuilder builder = new PopupBuilder(“choicepopup”) {
        {
            id(“choicepopup”);
            controller(me); // Screen properties

[/java]

or try

[java]
controller(BadgePopupUI.class);
[/java]

I’m not sure if it works, just play’n around a bit.

Thx, tried it but it doesn’t work.

Something different I tried:

My UI class:

[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package dae.repest.gui;

import dae.repest.gui.controllers.BadgeUIController;
import dae.repest.level.event.BadgeEventPart;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.PopupBuilder;
import de.lessvoid.nifty.builder.TextBuilder;

import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;

/**
*

  • @author Koen
    */
    public class BadgePopupUI {

    private Nifty nifty;
    private Screen screen;
    private BadgeEventPart oep;
    private Element popupElement;
    private String _name;
    private BadgeUIController cntrl;

    public BadgePopupUI(String name) {
    this._name = name;
    }

    public void buildUI(Nifty nifty, BadgeEventPart part) {
    nifty.loadStyleFile(“nifty-default-styles.xml”);
    nifty.loadControlFile(“nifty-default-controls.xml”);
    oep = part;
    cntrl = new BadgeUIController();
    cntrl.setBep(oep);

     // <screen>
     PopupBuilder builder = new PopupBuilder("badgepopup") {
         {
             visibleToMouse();
             id("badgepopup");
             controller(cntrl); // Screen properties       
             childLayoutAbsolute(); // layer properties, add more...
             //<panel>
             panel(new PanelBuilder("Panel_ID") {
                 {
                     visibleToMouse();
                     onShowEffect(
                             new EffectBuilder("fade") {
                         {
                             length(2000);
                             inherit(true);
                             post(false);
    
                             effectParameter("start", "#00");
                             effectParameter("end", "#ff");
                         }
                     });
                     backgroundImage("gui/HUD/PanelBackground.png");
                     imageMode("resize:36,34,36,40,36,34,36,34,36,34,36,40");
                     paddingLeft("20px");
                     paddingRight("20px");
                     paddingTop("20px");
                     paddingBottom("20px");
                     width("550px");
                     height("200px");
                     x("25%");
                     y("25%");
                     childLayoutCenter(); // panel properties, add more...
    
                     panel(
                             new PanelBuilder("innerPanel") {
                         {
                             visibleToMouse();
                             childLayoutHorizontal();
                             image(new ImageBuilder() {
                                 {
                                     filename("gui_FA/HUD/badges/" + _name + ".png");
                                 }
                             });
                             text(new TextBuilder() {
                                 {
                                     text("Proficiat met je badge ! "
                                             + "Bekijk je badges in het badge menu (duw op '1')");
                                     wrap(true);
                                     font("fonts/comicbook_32.fnt");
                                     height("100%");
                                     width("70%");
                                     interactOnClick("buttonClicked()");
                                     visibleToMouse();
                                 }
                             });
    
                             text(new TextBuilder() {
                                 {
                                     text("X");
                                     wrap(true);
                                     font("fonts/comicbook_32.fnt");
                                     height("100%");
                                     width("5%");
                                     interactOnClick("buttonClicked()");
                                     visibleToMouse();
                                 }
                             });
                         }
                     });
                     //.. add more GUI elements here              
    
                 }
             });
             // </panel>
         }
     };
     builder.registerPopup(nifty);
     Element popup = nifty.createPopup("badgepopup");
     //Controller c = popup.getControl(Controller.class);
     //if (c instanceof BadgePopupUI) {
     //    BadgePopupUI cui = (BadgePopupUI) c;
     //    cui.oep = oep;
     //}
     nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);
    

    }
    }
    [/java]

And an exteral controller:

[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package dae.repest.gui.controllers;

import dae.repest.level.event.BadgeEventPart;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.Properties;

/**
*

  • @author odjansse
    */
    public class BadgeUIController implements Controller {

    private Nifty nifty;
    private Screen screen;
    private Element popupElement;
    private BadgeEventPart bep;

    public BadgeUIController() {
    }

    @Override
    public void onStartScreen() {
    System.out.println(“Option screen started”);
    }

    public void buttonClicked() {
    System.out.println(“CLICKED THE X”);
    bep.setCloseOptionUI(true);
    nifty.closePopup(popupElement.getId());
    }

    @Override
    public void bind(Nifty nifty, Screen screen, Element elmnt, Properties prprts, Attributes atrbts) {
    this.nifty = nifty;
    this.screen = screen;
    this.popupElement = elmnt;
    }

    @Override
    public void init(Properties prprts, Attributes atrbts) {
    }

    @Override
    public void onFocus(boolean bln) {
    }

    @Override
    public boolean inputEvent(NiftyInputEvent nie) {
    return true;
    }

    /**

    • @return the bep
      */
      public BadgeEventPart getBep() {
      return bep;
      }

    /**

    • @param bep the bep to set
      */
      public void setBep(BadgeEventPart bep) {
      this.bep = bep;
      }
      }
      [/java]

In order to have the BadgeEventPart variable in my controller when the button is clicked i use a setter to set the variable.

Everything works fine, so the popup closes and the variable is set, though when I click the button the badgeventpart variable is null. Which is strange because is has been set with the setter.

Fixed it, just made the bep variable a public static variable.

1 Like