To start things off, let's begin with a simple recipe that will provide us with the basics of setting up the application to use Nifty GUI and tell us how to manage the options menu. An options menu is usually found in games; it acts as a link between different screens. For this reason, it's suitable to create it using the control pattern so it can be easily handled across screens.
We'll initialize Nifty GUI inside AppState
to offset it from the main application code and then access the application from Nifty and control Nifty through code.
Let's look at how to initialize Nifty in an application. We start off by defining a new AppState
to handle our Nifty functions. We can call it NiftyAppState
and have it extend AbstractAppState
.
In the initialize
method, we need to create the Nifty display with the following line of code, giving Nifty access to various functionalities within the application and telling it to render o
in the GUI view:
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getRenderManager().getPostView("Gui Default"));
We should also store the Nifty instance in the class for use later, using niftyDisplay.getNifty()
. With this done, we need to add niftyDisplay
as a processor to the same view we just specified, using the following code:
app.getRenderManager().getPostView("Gui Default").addProcessor(niftyDisplay);
The last thing that needs to be done before Nifty could show anything is to tell it what to draw. We do this with nifty.fromXml
and pass the XML file to be used as well as the name of the screen (if several screens are stored in the same XML).
We start by defining the XML files for our options menu and the screen that will contain it. Perform the following steps to do this:
optionsMenu.xml
. It should reside in the Interface
/Controls
folder.<nifty-controls>
tag to let Nifty know that the elements inside should be parsed as controls.<controlDefinition name="options">
, which is the actual options menu instance.<panel>
element, as shown in the following code:<panel id="optionsPanel" childLayout="vertical" width="40%" height="60%" align="center" valign="center" backgroundColor="#333f">
<panel>
that will include <control name="label"> element with text="Options"
.Controller
class, as shown in the following code:<control name="button" id="closeButton" align="right" label="x" height="30px" width="30px" > <interact onClick="toggleOptionsMenu()"/> </control>
<control name="button">
elements we want for our options menu to work. There should at least be one that calls quit()
in the Controller
class to stop the application.<layer>
tags, and change the controller of the <screen>
element to gui.controls.NiftyController
.<useStyles>
tag, which should appear before the <screen>
element.<useControls filename="nifty-default-controls.xml" />
to include access to basic nifty controls such as buttons, and we should add another <useControls>
tag for our options menu. These should also be added before the <screen>
element.Now, we can start looking at the Controller
code for this. Perform the following five steps to do this:
ScreenController
interface, which will become the link between the GUI and the code. We can make it abstract and call it NiftyController
.Nifty nifty
and Screen screen
, which will be set from the values provided in the bind
method.optionsMenuVisible
.optionsMenu.xml
file, and toggleOptionsMenu()
should either show or hide the menu depending on whether optionsMenuVisible
is true or not. A handy way to get hold of an element is by using the following code:nifty.getCurrentScreen().findElementByName("options");
hide()
or show()
on the element to control visibility.Normally, the application is shut down when Esc is pressed. Let's make the options menu handle this instead; this consists of the following four steps:
NiftyAppState
initialization method:app.getInputManager().deleteMapping(SimpleApplication.INPUT_MAPPING_EXIT);
app.getInputManager().addMapping("TOGGLE_OPTIONS", new KeyTrigger(KeyInput.KEY_ESCAPE)); app.getInputManager().addListener(this, "TOGGLE_OPTIONS");
NiftyAppState
method also needs to implement ActionListener
and handle the key press:public void onAction(String name, boolean isPressed, float tpf) { if(name.equals(TOGGLE_OPTIONS) && isPressed){ ((NiftyController)nifty.getCurrentScreen().getScreenController()).toggleOptionsMenu(); } }
NiftyController
to handle this instead. Since this class will be shared by the screens, we provide the application with static access and a setter method. The quit
method just has to call app.stop()
to shut it down.Nifty was initialized inside AppState
to offset the code from the main application and make it more modular. This also made it easier to add some more general functionalities related to controlling the GUI.
Every nifty Controller
class must implement the ScreenController
interface for Nifty to be able to find it. Since some functions will be shared across the screens, we created an abstract class called NiftyController
to avoid duplicating the code. Apart from handling the generic Options menu, it was also given access to the application itself.
The link between the XML file and the Controller
class doesn't need to be specified beyond providing the qualified name of the controller in the screen. Likewise, Nifty will find methods automatically using the name provided in the interact
tag of ButtonControl
.
The <panel>
elements are versatile objects that can be used for many parts of the layout and can contain most other types of layout items.
It's OK to contain several <controlDefinition>
elements within a <nifty-controls>
tag.
It's very easy to use a properties
file to back a Nifty file for localization purposes, as given in the following points:
3.133.141.219