Skip to content

Text box UI component

benjackson45 edited this page Oct 8, 2021 · 11 revisions

Overview

Displays a text box at the bottom of the screen that prints out a given string.

Example:

Functionality

All of the functionality is stored in MainGameTextDisplay, which extends UIComponent. When create is called, the class creates an empty table but doesn't display any images or text. display can then be called by an event listener, for when create_textbox is triggered, with a single String argument. It then places a background image within the table from create, before printing the text to the screen. The positioning was handled by subdividing the screen into columns and rows, which can be seen below.

Screen layout:

This allows for the text box's position to be dynamic in regards to the size of the screen. If the size of the screen is changed during the game, subsequent text boxes will fit to the screen.

Implementation

In order to display the textbox from within another class in the game, the following code can be used:

String string = "This string will be printed by the textbox";
((MainGameScreen) ServiceLocator.getGame().getScreen()).getMainGameEntity().getEvents().trigger("create_textbox", string);

If you want to display the text box from the 'MainGameScreen' entity, you can just do the following:

String string = "This string will be printed by the textbox";
entity.getEvents().trigger("create_textbox", string);

Testing

Due to difficulties with simulating the game screen within the testing interface, the unit tests do not have complete coverage.

Test Targets

  • MainGameTextDisplay is created properly
    • Test that the display is only created once
  • Test that the display() works
  • Test that update() can be called multiple times without an error
  • Test that dispose() removes all of the assets
Clone this wiki locally