Interface HUD

All Known Implementing Classes:
CameraControlledGameScreen, GameScreenWithHUD, ScalableGameScreen

public interface HUD
Represents a Heads-Up Display (HUD) system for rendering overlay elements such as UI components, score counters, or health bars on top of the main game view.

This interface provides access to the HUD's rendering camera, viewport, and dimensions, as well as utility methods for switching between world and HUD rendering contexts and obtaining mouse coordinates relative to the HUD.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    enableHUD(int width, int height)
    Initializes or recreates the HUD with the specified dimensions.
    com.badlogic.gdx.graphics.OrthographicCamera
    Returns the OrthographicCamera used for rendering the HUD.
    float
    Returns the height of the HUD in world units or pixels, depending on the implementation.
    com.badlogic.gdx.utils.viewport.FitViewport
    Returns the FitViewport used by the HUD.
    float
    Returns the width of the HUD in world units or pixels, depending on the implementation.
    com.badlogic.gdx.math.Vector2
    Returns the current mouse position in HUD coordinates.
    float
    Returns the mouse's X coordinate in HUD space.
    float
    Returns the mouse's Y coordinate in HUD space.
    com.badlogic.gdx.scenes.scene2d.Stage
    Get the UI stage for adding UI elements
    com.badlogic.gdx.math.Vector2
    hudToWorld(com.badlogic.gdx.math.Vector2 hudPos)
    Converts a position in HUD coordinates to world coordinates.
    float
    hudXtoWorldX(float hudX)
    Converts a HUD X coordinate to world X coordinate.
    float
    hudYtoWorldY(float hudY)
    Converts a HUD Y coordinate to world Y coordinate.
    boolean
    Returns true if the HUD is currently enabled.
    void
    Called when there is interaction with one of the UI elements on the screen
    void
    Call this method in your render method to render a User Interface.
    void
    Switches the rendering context to the HUD.
    void
    Switches the rendering context from the HUD to the main game world.
    com.badlogic.gdx.math.Vector2
    worldToHUD(com.badlogic.gdx.math.Vector2 worldPos)
    Converts a position in world coordinates to HUD coordinates.
    float
    worldXtoHUDX(float worldX)
    Converts a world X coordinate to HUD X coordinate.
    float
    worldYtoHUDY(float worldY)
    Converts a world Y coordinate to HUD Y coordinate.
  • Method Details

    • enableHUD

      void enableHUD(int width, int height)
      Initializes or recreates the HUD with the specified dimensions.

      This method should set up the HUD's camera, viewport, and any other required rendering components. It is typically called when the game is first created or when the window or screen is resized.

      Parameters:
      width - the width of the HUD area, usually in pixels
      height - the height of the HUD area, usually in pixels
    • getHUDWidth

      float getHUDWidth()
      Returns the width of the HUD in world units or pixels, depending on the implementation.
      Returns:
      the HUD width
    • getHUDHeight

      float getHUDHeight()
      Returns the height of the HUD in world units or pixels, depending on the implementation.
      Returns:
      the HUD height
    • getHUDViewport

      com.badlogic.gdx.utils.viewport.FitViewport getHUDViewport()
      Returns the FitViewport used by the HUD.

      The viewport ensures consistent scaling and aspect ratio across different screen resolutions.

      Returns:
      the HUD's FitViewport
    • getHUDCamera

      com.badlogic.gdx.graphics.OrthographicCamera getHUDCamera()
      Returns the OrthographicCamera used for rendering the HUD.

      The camera typically operates in an orthographic projection to simplify 2D rendering of HUD elements.

      Returns:
      the HUD's OrthographicCamera
    • switchToWorldRendering

      void switchToWorldRendering()
      Switches the rendering context from the HUD to the main game world.

      Implementations typically call batch.setProjectionMatrix(worldCamera.combined) or equivalent logic.

    • switchToHudRendering

      void switchToHudRendering()
      Switches the rendering context to the HUD.

      Implementations typically call batch.setProjectionMatrix(hudCamera.combined) or equivalent logic before drawing HUD elements.

    • getMouseHUDPosition

      com.badlogic.gdx.math.Vector2 getMouseHUDPosition()
      Returns the current mouse position in HUD coordinates.

      This can be useful for detecting interactions with UI components or buttons within the HUD.

      Returns:
      a Vector2 representing the mouse position in HUD space
    • getMouseHUDX

      float getMouseHUDX()
      Returns the mouse's X coordinate in HUD space.
      Returns:
      the X coordinate of the mouse in HUD coordinates
    • getMouseHUDY

      float getMouseHUDY()
      Returns the mouse's Y coordinate in HUD space.
      Returns:
      the Y coordinate of the mouse in HUD coordinates
    • worldToHUD

      com.badlogic.gdx.math.Vector2 worldToHUD(com.badlogic.gdx.math.Vector2 worldPos)
      Converts a position in world coordinates to HUD coordinates.
      Parameters:
      worldPos - the position in world coordinates
      Returns:
      a new Vector2 representing the same point in HUD coordinates
    • hudToWorld

      com.badlogic.gdx.math.Vector2 hudToWorld(com.badlogic.gdx.math.Vector2 hudPos)
      Converts a position in HUD coordinates to world coordinates.
      Parameters:
      hudPos - the position in HUD coordinates
      Returns:
      a new Vector2 representing the same point in world coordinates
    • worldXtoHUDX

      float worldXtoHUDX(float worldX)
      Converts a world X coordinate to HUD X coordinate.
      Parameters:
      worldX - the X coordinate in world space
      Returns:
      the corresponding X coordinate in HUD space
    • worldYtoHUDY

      float worldYtoHUDY(float worldY)
      Converts a world Y coordinate to HUD Y coordinate.
      Parameters:
      worldY - the Y coordinate in world space
      Returns:
      the corresponding Y coordinate in HUD space
    • hudXtoWorldX

      float hudXtoWorldX(float hudX)
      Converts a HUD X coordinate to world X coordinate.
      Parameters:
      hudX - the X coordinate in HUD space
      Returns:
      the corresponding X coordinate in world space
    • hudYtoWorldY

      float hudYtoWorldY(float hudY)
      Converts a HUD Y coordinate to world Y coordinate.
      Parameters:
      hudY - the Y coordinate in HUD space
      Returns:
      the corresponding Y coordinate in world space
    • renderUI

      void renderUI()
      Call this method in your render method to render a User Interface. Can only be done when the HUD is enabled.
    • getUIStage

      com.badlogic.gdx.scenes.scene2d.Stage getUIStage()
      Get the UI stage for adding UI elements
      Returns:
      Stage for UI elements
    • onUIInteraction

      void onUIInteraction(String key, Object value)
      Called when there is interaction with one of the UI elements on the screen
      Parameters:
      key - Name of the element
      value - Value of the element
    • isHUDEnabled

      boolean isHUDEnabled()
      Returns true if the HUD is currently enabled.