JME3 Nifty Popups - Lightnet/openwizcoder GitHub Wiki
There are two type of popup. One is the menu items and other is the popup menu that fade out the other menu.
UI_PopMenuItem.xml
<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
<useStyles filename="nifty-default-styles.xml" />
<useControls filename="nifty-default-controls.xml" />
<screen id="start" controller="org.openwizcoder.ui.UIPopMenuItemScreenController">
<layer childLayout="center">
<panel width="20%" height="25%" align="center" valign="center" childLayout="vertical" backgroundColor="#55a5" padding="10">
<panel height="21px" childLayout="horizontal">
<control id="BtMenuItem" name="button" label="Popup Menu" />
</panel>
<panel height="21px" childLayout="horizontal">
<control id="BtClose" name="button" label="Quit App" />
</panel>
</panel>
</layer>
</screen>
<popup id="niftyPopupMenu" childLayout="absolute-inside" width="10%">
<interact onClick="closePopup()" onSecondaryClick="closePopup()" onTertiaryClick="closePopup()" />
<control id="#menu" name="niftyMenu" />
</popup>
<popup id="PopupExitID" childLayout="center" backgroundColor="#000a">
<panel width="80%" height="25%" align="center" valign="center" childLayout="vertical" backgroundColor="#55a5" padding="10">
<panel width="*" />
<panel height="21px" childLayout="horizontal">
<panel width="*" />
<control id="BtYes" name="button" label="Yes"/>
<panel width="*" />
<control id="BtNo" name="button" label="No"/>
<panel width="*" />
</panel>
<panel width="*" />
</panel>
</popup>
<screen id="end">
</screen>
</nifty>
UIPopMenuItemScreenController.java
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ButtonClickedEvent;
import de.lessvoid.nifty.controls.Menu;
import de.lessvoid.nifty.controls.MenuItemActivatedEvent;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.SizeValue;
import de.lessvoid.nifty.screen.ScreenController;
/**
*
* @author Lightnet
*/
public class UIPopMenuItemScreenController extends ScreenController{
public Nifty nifty;
public Screen screen;
Element popup;
Element popupExit;
@Override
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
System.out.print("Pop Up Exit");
createExitPopupMenu();
createMyPopupMenu();
}
@Override
public void onStartScreen() {
}
@Override
public void onEndScreen() {
}
@NiftyEventSubscriber(id = "BtMenuItem")
public void BtMenuItemClicked(final String id, final ButtonClickedEvent event) {
System.out.print("\nButton press menu item");
showMenu();
}
@NiftyEventSubscriber(id = "BtClose")
public void BtCloseClicked(final String id, final ButtonClickedEvent event) {
System.out.print("\nButton press exit");
showpopupExit();
}
@NiftyEventSubscriber(id = "BtYes")
public void BtYesClicked(final String id, final ButtonClickedEvent event) {
System.out.print("\nButton press BtYes");
System.exit(0);
}
@NiftyEventSubscriber(id = "BtNo")
public void BtNoClicked(final String id, final ButtonClickedEvent event) {
System.out.print("\nButton press BtNo");
this.nifty.closePopup( popupExit.getId());
}
@NiftyEventSubscriber(id = "menuItemid")
public void MenuItemClicked(final String id, final MenuItemActivatedEvent event) {
System.out.print("\nItem menu[image]:"+event.getItem().toString());
if(event.getItem().toString().equalsIgnoreCase("close")){
this.nifty.closePopup( popup.getId());
}
}
//Menu Items
public void createMyPopupMenu(){
if(popup == null){
popup = this.nifty.createPopup("niftyPopupMenu");
Menu myMenu = popup.findNiftyControl("#menu", Menu.class);
myMenu.setWidth(new SizeValue("100px")); // must be set
myMenu.addMenuItem("Click me!", "clickme");
myMenu.addMenuItem("Close Me!", "close");
myMenu.setId("menuItemid");//remeber to assign NiftyEventSubscriber for this
}
}
public void showMenu() { // the method to trigger the menu
// call the popup to screen of your choice:
this.nifty.showPopup(this.nifty.getCurrentScreen(), popup.getId(), null);
}
public void hideMenu() { // the method to trigger the menu
// call the popup to screen of your choice:
this.nifty.closePopup( popup.getId());
}
//Popup Exit Menu
public void createExitPopupMenu(){
popupExit = this.nifty.createPopup("PopupExitID");
}
public void showpopupExit() { // the method to trigger the menu
this.nifty.showPopup(this.nifty.getCurrentScreen(), popupExit.getId(), null);
}
}
AppMenuPopUp.java < Main File Test
import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openwizcoder.ui.UIPopMenuItemScreenController;
public class AppMenuPopUp extends SimpleApplication implements ScreenController {
public Nifty nifty;
public static void main(final String[] args) throws Exception {
Logger.getLogger("").setLevel(Level.SEVERE);
AppMenuPopUp app = new AppMenuPopUp();
app.setShowSettings(false);
app.start();
}
public void Init_Nifty(){
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = niftyDisplay.getNifty();
UIPopMenuItemScreenController itemconrol = new UIPopMenuItemScreenController();
nifty.fromXml("Interface/UI_PopMenuItem.xml" ,"start",itemconrol);
// attach the nifty display to the gui view port as a processor
guiViewPort.addProcessor(niftyDisplay);
}
@Override
public void simpleInitApp() {
Init_Nifty();
setDisplayStatView(false);
mouseInput.setCursorVisible(true);
this.pauseOnFocus = false;
this.flyCam.setEnabled(false);
}
public void bind(Nifty nifty, Screen screen) {
System.out.println("bind( " + screen.getScreenId() + ")");
}
public void onStartScreen() {
System.out.println("onStartScreen");
}
public void onEndScreen() {
System.out.println("onEndScreen");
}
public void quit(){
nifty.gotoScreen("end");
}
}