The UI System provides functionality for creating and managing user interface elements in the game engine.
The UISystem class is the central manager for all UI elements. It handles:
- Creation of UI elements (factory methods)
- Event handling (mouse clicks, movement, dragging)
- Managing element states (hover, focus, drag)
- Rendering delegation
// Create a UISystem instance
UISystem uiSystem = new UISystem(gameWindow, ecsInstance);
// Register with a scene
uiSystem.setCurrentRegistrar(sceneRegistrar);Buttons can be clicked to trigger actions.
// Create a button
Entity button = uiSystem.createButton("Click Me", 100, 100, 150, 40);
// Add click handler
Button buttonComponent = button.get(UIComponent.class).getUi();
buttonComponent.setOnClick(() -> System.out.println("Button clicked!"));Labels display static text.
// Create a label
Entity label = uiSystem.createLabel("Hello World", 100, 100);Panels are container elements that can hold other UI elements.
// Create a panel
Entity panel = uiSystem.createPanel(50, 50, 300, 200);
// Add elements to the panel
Entity button = uiSystem.createButton("Panel Button", 10, 10, 100, 30);
uiSystem.addToPanel(panel, button);Sliders allow the user to select a value from a range.
// Create a slider
Entity slider = uiSystem.createSlider("Volume", 100, 150, 200, 30, 0.0f, 1.0f, 0.5f);
// Add value change callback
uiSystem.setSliderCallback(slider, value -> System.out.println("New value: " + value));Debug overlays display performance metrics and debug information.
// Create a debug overlay
Entity debugOverlay = uiSystem.createDebugOverlay(10, 10);The UISystem automatically handles mouse events for all UI elements. This includes:
- Hover effects for buttons
- Focus management
- Slider dragging
- Click events
You can create custom UI elements by extending AbstractUIElement and implementing the required methods.
public class CustomElement extends AbstractUIElement {
public CustomElement(float x, float y, float width, float height) {
super(x, y, width, height);
}
@Override
public void render(Graphics2D g) {
// Custom rendering code
}
@Override
public void update(double deltaTime) {
// Custom update logic
}
}