Game Development Reference
In-Depth Information
11. We add
<useControls filename="nifty-default-con-
trols.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:
1. We should define a class that implements the
ScreenController
interface,
which will become the link between the GUI and the code. We can make it ab-
stract and call it
NiftyController
.
2. It should have two protected fields, namely,
Nifty nifty
and
Screen
screen
, which will be set from the values provided in the
bind
method.
3. We also need a Boolean field called
optionsMenuVisible
.
4. We need to add methods for each of the methods specified in the
option-
sMenu.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");
5. Then, we can call either
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:
1. Start by deleting the related mapping by adding the following line to the
NiftyAppState
initialization method:
app.getInputManager().deleteMapping(SimpleApplication.INPUT_MAPPING_EXIT);
2. Now, we need to add our own mapping for the Esc key, as shown in the following
code:
app.getInputManager().addMapping("TOGGLE_OPTIONS",
new KeyTrigger(KeyInput.KEY_ESCAPE));
app.getInputManager().addListener(this,
"TOGGLE_OPTIONS");