Skip to content

Alert Box

Shaivika Anand edited this page Oct 2, 2024 · 1 revision

AlertBox Class Documentation

Overview

The AlertBox class, part of the com.csse3200.game.ui package, is a customizable pop-up dialog that displays a message to the user and provides an "OK" button to dismiss the alert. The class is designed to match the template of PopupDialogBox for consistency with other UI components in the game.

Key Features:

  • Customizable Message: Allows setting a custom message to be displayed in the alert box.
  • Dismissable Popup: Includes a simple "OK" button to close the alert.
  • Background Image: Custom background image can be set for the dialog box.
  • UI Consistency: The design is intended to match other pop-up dialogs in the game for consistent user experience.

Key Components

1. Fields

  • messageLabel: A Label that holds and displays the message to the user.
  • okButton: A TextButton that, when clicked, dismisses the alert box.
  • dialogWidth and dialogHeight: Custom width and height of the dialog box.

2. Constructor

public AlertBox(String message, Skin skin, float dialogWidth, float dialogHeight)

This constructor initializes the AlertBox by setting up the background, message, and the "OK" button. The layout of the alert box is configured using the createDialogLayout() method.

  • message: The message to display in the alert box.
  • skin: The Skin object used to style the UI elements (label and button).
  • dialogWidth and dialogHeight: Specify the dimensions of the dialog box.

During construction, the class sets a background image for the alert box and adds the message and the button into a content table.

3. Methods

  • createDialogLayout(): This method sets up the layout of the alert box by:

    • Adding the message label to the content table.
    • Placing the "OK" button below the message label.
    • Ensuring proper padding and alignment for visual aesthetics.

    The dialog box is centered on the screen using the set size and positioning based on the dimensions of the screen and dialog.

  • display(Stage stage): This method adds the alert box to a specified Stage, making it visible in the game. The dialog is positioned at the center of the screen for optimal user interaction.

    Example:

    alertBox.display(stage);
  • hide(): This method is inherited from the Dialog class and is called to close the alert when the "OK" button is pressed.


UI Customization

  • Background Image: The dialog box uses a light blue background image (lightblue.png) located in the images/animal directory. This image is loaded as a Texture and applied as the background of the content table.

  • Message Styling: The message text is styled using the provided Skin and colored cyan. It wraps within the dialog if the message is too long to fit in a single line.

  • Button: The "OK" button dismisses the dialog when clicked, triggering the hide() method.


Example Usage

// Creating a Skin instance for the dialog elements
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));

// Creating and displaying an alert box
AlertBox alertBox = new AlertBox("Game Over! Try again.", skin, 400, 200);
alertBox.display(stage);

In this example, the AlertBox will show the message "Game Over! Try again." on a 400x200 dialog box, centered on the screen.


Use Cases

  • Game Notifications: This dialog can be used to notify players of important game events such as errors, success messages, or warnings.
  • Confirmation Messages: The class could be extended to include additional buttons for confirmations (e.g., "Yes", "No").

Conclusion

The AlertBox class is a simple, customizable solution for displaying alert messages in a game. It offers flexibility with respect to the message, styling, and background while maintaining a consistent UI experience. The easy-to-use interface ensures that game developers can integrate alert dialogs with minimal effort.

Clone this wiki locally