Game Development Reference
In-Depth Information
◦ The settingsControl class we just created should be added inside a
layer element
That's all with regards to XML. To create the Controller class, perform the following
steps:
1. As usual, we create a new class that extends NiftyController . We call it
SettingsController .
2. We'll have Element fields for each of the key bindings we would like to track
and one Element field for the current selectedElement .
3. In addition, we should add Map<Integer, String> called mappings
where we can keep the relations between key inputs and input bindings.
4. From here, we should call a bindElements method, which we'll define as
well.
5. Inside this, we'll add the current key bindings to the mappings map using the
key code as key and the actual binding as the value. This can usually be found in
the class that handles the input.
6. Next, for each of the keys we would like to handle, we find the reference in the
settings screen and populate their values accordingly. For example, for the for-
ward key use the following code:
forwardMapping =
screen.findElementByName("forwardKey");
forwardMapping.findNiftyControl("#command",
Label.class).setText("MoveForward");
forwardMapping.findNiftyControl("#key",
Button.class).setText(Keyboard.getKeyName(KeyInput.KEY_W));
7. Next, we define a new inner class called KeyEventListener that implements
RawInputListener .
8. In onKeyEvent , add an if statement for if the incoming KeyInputEvent is
pressed and selectedElement is not null.
9. Here, we add a reference to the yet-to-be-created changeMapping method and
add the following line:
selectedElement.findNiftyControl("#key",
Button.class).setText(Keyboard.getKeyName(evt.getKeyCode()));
10. Finally, we should set selectedElement to null .
Search WWH ::




Custom Search