Chapter 6. GUI with Nifty GUI

First of all, what is Nifty GUI? It's not the only GUI available in jMonkeyEngine, but it is the one that is officially supported. It is not developed by the jMonkeyEngine team but is an independent open source effort that has implementations in other engines as well.

In this chapter, we'll cover the following topics:

  • Initializing Nifty and managing an options menu
  • Loading the screen
  • Creating an RPG dialog screen
  • Implementing a game console
  • Handling a game message queue
  • Creating an inventory screen
  • Customizing the input and settings page
  • Using offscreen rendering for a minimap

Introduction

Nifty GUI is operated using screens. A screen could be, for example, an in-game (HUD) heads-up display or the same game's main menu. Screens are built using XML and Nifty's own set of tags. On each screen, there can be layers that are drawn on top of each other according to their order.

On a screen, objects cascade similarly as on a web page, that is, from top to bottom or left to right, depending on the settings. The following code is an example of what a simple screen might look like:

<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" />
    
  <registerSound id="showWindow" filename="Sound/Effects/Beep.ogg" />
    
  <screen id="main" controller="gui.controller.MainScreenController">
    <layer id="layer0" childLayout="absolute" backgroundColor="#000f">
      <!-- add more content -->
    </layer>
  </screen>
</nifty>

Each screen has a Controller class tied to it. This is the link between the XML and Java that allows Nifty to control functions in the code and the other way around.

Another important concept is Controls (not to be confused with Controller classes or jMonkeyEngine's Control interface). Using Controls is a very convenient way to make screen files smaller and create reusable components. Anyone familiar with, for example, components in JSF will see the similarities. It's highly recommended that you become accustomed to using these early on, or screen files will quickly become unmanageable.

A UI's implementation is often very specific to the game in question. This chapter will try to show and explain the different functions and effects available in Nifty GUI. Even if the title of a recipe does not appeal to you, it could still be worth to glance through the content to see whether it covers some features that could be suitable for your project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.123