Interface HUD
- All Known Implementing Classes:
CameraControlledGameScreen,GameScreenWithHUD,ScalableGameScreen
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 TypeMethodDescriptionvoidenableHUD(int width, int height) Initializes or recreates the HUD with the specified dimensions.com.badlogic.gdx.graphics.OrthographicCameraReturns theOrthographicCameraused for rendering the HUD.floatReturns the height of the HUD in world units or pixels, depending on the implementation.com.badlogic.gdx.utils.viewport.FitViewportReturns theFitViewportused by the HUD.floatReturns the width of the HUD in world units or pixels, depending on the implementation.com.badlogic.gdx.math.Vector2Returns the current mouse position in HUD coordinates.floatReturns the mouse's X coordinate in HUD space.floatReturns the mouse's Y coordinate in HUD space.com.badlogic.gdx.scenes.scene2d.StageGet the UI stage for adding UI elementscom.badlogic.gdx.math.Vector2hudToWorld(com.badlogic.gdx.math.Vector2 hudPos) Converts a position in HUD coordinates to world coordinates.floathudXtoWorldX(float hudX) Converts a HUD X coordinate to world X coordinate.floathudYtoWorldY(float hudY) Converts a HUD Y coordinate to world Y coordinate.booleanReturns true if the HUD is currently enabled.voidonUIInteraction(String key, Object value) Called when there is interaction with one of the UI elements on the screenvoidrenderUI()Call this method in your render method to render a User Interface.voidSwitches the rendering context to the HUD.voidSwitches the rendering context from the HUD to the main game world.com.badlogic.gdx.math.Vector2worldToHUD(com.badlogic.gdx.math.Vector2 worldPos) Converts a position in world coordinates to HUD coordinates.floatworldXtoHUDX(float worldX) Converts a world X coordinate to HUD X coordinate.floatworldYtoHUDY(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 pixelsheight- 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 theFitViewportused 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 theOrthographicCameraused 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
Vector2representing 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
Vector2representing 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
Vector2representing 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
Called when there is interaction with one of the UI elements on the screen- Parameters:
key- Name of the elementvalue- Value of the element
-
isHUDEnabled
boolean isHUDEnabled()Returns true if the HUD is currently enabled.
-