Console - relu91/nifty-gui GitHub Wiki

Table of Contents

Console

Description

The Console control displays a command prompt similiar to the ones found in a terminal window. It allows input and output of text. An additional class is available that adds more advanced features like command line completion, command history and command parsing.

Java API

This is the basic Java API for the console control.

Console

Additionally you can use the ConsoleCommands class that adds more features to the console control. The way this works is that you can register commands with the ConsoleCommands class and then connect the ConsoleCommands instance to the actual console control. This way the ConsoleCommands class can intercept and process commands. This way features like command line completion (for all registered commands) as well as command history are implemented.

ConsoleCommands

Standard Parameters

All parameters for the Console control.

Name Datatype Default Description
lines integer 2 Defines the number of lines the history and output window of the console above the text input field displays.

EventBus Notification

The Console supports EventBus notification when a command is being submitted with the return key. In that case a ConsoleExecuteCommandEvent is published using the id of the Console as the Topic as well as the actual command line that should be executed by you. The event class gives access to the already parsed command line.

EventBus Notification is only used for the basic console. When you use the ConsoleCommands then the ConsoleExecuteCommandEvent is only published when an unknown command has been found. When a previously registered command has been detected the registered ConsoleCommand will directly be called.

ConsoleExecuteCommandEvent

Java Builder Example

// this creates a simple console with 25 lines that is 80% width (of the parent element) and for demonstration purpose there is an effect added
control(new ConsoleBuilder("console") {{
  width("80%");
  lines(25);
  alignCenter();
  valignCenter();
  onStartScreenEffect(new EffectBuilder("move") {{
    length(150);
    inherit();
    neverStopRendering(true);
    effectParameter("mode", "in");
    effectParameter("direction", "top");
  }});
}});

XML Example

<!-- simple default console control that displays 25 lines. please note: event notification requires an id -->
<control id="console" name="nifty-console" lines="25" align="center" valign="center">

Command Usage Example

// get the console control (this assumes that there is a console in the current screen with the id="console"
Console console = screen.findNiftyControl("console", Console.class);

// output hello to the console
console.output("Hello :)");

// create the console commands class and attach it to the console
ConsoleCommands consoleCommands = new ConsoleCommands(nifty, console);

// create a simple command (see below for implementation) this class will be called when the command is detected
// and register the command as a command with the console
ConsoleCommand simpleCommand = new SimpleCommand();
consoleCommands.registerCommand("simple", simpleCommand);

// create another command (this time we can even register arguments with nifty so that the command completion will work with arguments too)
ConsoleCommand showCommand = new ShowCommand();
consoleCommands.registerCommand("show a", showCommand);
consoleCommands.registerCommand("show b", showCommand);
consoleCommands.registerCommand("show c", showCommand);

// finally enable command completion
consoleCommands.enableCommandCompletion(true);

////////////////////////

private class SimpleCommand implements ConsoleCommand {
  @Override
  public void execute(final String[] args) {
    System.out.println(args[0]); // this is always the command (in this case 'simple')
    if (args.length > 1) {
      for (String a : args) {
        System.out.println(a);
      }
    }
  }
}

private class ShowCommand implements ConsoleCommand {
  @Override
  public void execute(final String[] args) {
    System.out.println(args[0] + " " args[1]);
  }
}

Back to Nifty Standard Controls (Nifty 1.3)

⚠️ **GitHub.com Fallback** ⚠️