Lesson 4: Nifty GUI

Env3D integrates with the Nifty GUI project to give users the ability to put GUI elements directly into the 3D screen itself.  To use this feature, you must use the EnvAdvanced class to create your environment.  You can then call the EnvAdvanced.getNiftyGUI() method to create a Nifty object that you can use to manipulate the UI. 

Below is a simple example of the GUI:

 
import env3d.advanced.EnvAdvanced;
import env3d.advanced.EnvNode;

import de.lessvoid.nifty.*;
import de.lessvoid.nifty.screen.*;
import de.lessvoid.nifty.builder.*;

public class Game
{
    public void play() {
        EnvAdvanced env = new EnvAdvanced();
        
        // Create a nifty object to manipulate the UI
        Nifty nifty = env.getNiftyGUI();
        
        // We build a screen using the *Builder classes.  
        // We are using inner classes to create the screen, makes
        // the code much more readable.
        new ScreenBuilder("HelloScreen") {
            // This is a bit of Java magic.  We are defining the screen as an 
            // anonymous inner class, but we need to put some code inside the
            // constructor of this anonymous class.  The use of {} without any
            // declarations tells java that the code inside the block is to used
            // as the constructor
            {
                // Each screen has one or more layers
                layer(new LayerBuilder() {
                    {
                        // Layout the child elements in the middle
                        childLayoutCenter();
                        // Each layer can have one or more panel
                        panel(new PanelBuilder() {
                            {
                                // We simply have an empty panel with a 
                                // translucent background
                                alignCenter();
                                valignCenter();
                                childLayoutCenter();
                                width(percentage(100));
                                height(percentage(100));        
                                
                                // color is expressed using the RGBA notation
                                backgroundColor("#ffffff88");
                            }                            
                        });
                    }
                });
            }
            // Build this screen and let the nifty object manage it
        }.build(nifty);
        
        // Activate the screen
        nifty.gotoScreen("HelloScreen");
        
        // put a simple object in our environment.  
        EnvNode node = new EnvNode();
        env.addObject(node);
        node.setX(5); node.setY(1); node.setZ(0);
        node.setScale(3);
        // Render the screen until escape key is pressed
        while (env.getKey() != 1) {
            // Rotate our object
            node.setRotateY(node.getRotateY()+1);
            env.advanceOneFrame();
        }
        
        env.exit();
    }
    
    public static void main(String args[]) {
        (new Game()).play();
    }
}

The most important thing to keep in mind when using Nifty GUI is that it is built around the idea of "screens".  Only one screen can be shown at one time, but you can transition between multiple screens using the Nifty.gotoScreen() method. 

Within a screen, you can have multiple "layers".  Layers are overlapping and is shown on top of each other. 

Inside each layer is one or more "panels".  Each panel can contain actual UI elements or other panels.

To understand how layout works in Nifty, check out the layout section of the Nifty Wiki.