DynamicHUD Dev Guide
DownloadNeed Support?
  • Dynamic HUD
  • Import using Gradle
  • Integrating DynamicHUD
    • Integrating DynamicHUD
    • Adding Widgets
    • Adding your own WidgetRenderer
    • Passing AbstractMoveableScreen instance
    • Changing default key bind
    • Changing save and load file
    • Registering Custom Widgets
  • Widget
    • Widget class
    • How to create a custom widget
    • Widget Renderer
    • WidgetData
    • DynamicValueRegistry
    • Scaling
  • Screens
    • AbstractMoveableScreen
  • ContextMenu
    • ContextMenu class
    • Using context menu
    • Option<T> class
      • Color Option
      • Boolean Option
      • Double Option
      • Runnable Option
      • Enum Option
      • List Option
      • SubMenu Option
Powered by GitBook
On this page
  • Introduction
  • Details
  • Interactivity
  • Example Usage

Was this helpful?

  1. ContextMenu
  2. Option<T> class

Boolean Option

Introduction

The BooleanOption class inherits from the Option<Boolean> class and allows users to switch between two states, typically represented as "on" and "off".

Details

Constructor

The Boolean Option class has two constructors that takes the following parameters:

  • One of the constructors uses the BooleanPool class to store and manage the Boolean values. This is when you don't want the hassle to create a new Boolean for every BooleanOption you create.

  • Supplier Requirement: The Supplier<Boolean> parameter is necessary to obtain the current state of the Boolean value.

  • Consumer Provision: The Consumer<Boolean> parameter updates the Boolean value when the option is interacted with.

  • Name Parameter: The name parameter labels the option within the HUD, enhancing user experience and clarity.

BooleanPool

The BooleanPool class is a thread-safe cache that stores and retrieves Boolean values. It provides a fast and efficient way to access and update the Boolean values. This is what the BooleanPool class looks like

public class BooleanPool {
    private static final Map<String, Boolean> pool = new ConcurrentHashMap<>();

    public static void put(String key, boolean value) {
        pool.put(key, value);
    }
    public static void remove(String key) {
        pool.remove(key);
    }

    public static boolean get(String key) {
        return pool.getOrDefault(key, false);
    }
}

Interactivity

  • Click Response: Clicking the option toggles its state and triggers the associated setter action.

  • Render Behavior: The option is visually represented with a label, changing color to indicate its state (green for "on", red for "off").

  • Mouse Interaction: The class includes methods to handle mouse clicks, ensuring the option responds to user input.

Example Usage

// Create a BooleanOption for sound settings
BooleanOption soundOption = new BooleanOption(
    "Sound Enabled",
    () -> this.isSoundEnabled(), // Getter: Fetches the current sound setting
    value -> this.setSoundEnabled(value) // Setter: Updates the sound setting
);    

Ensure that the getter is synchronized with the setter; otherwise, the option will enter a soft lock, and the value will not update. (Meaning the getter and setter should be updating the same variables or referencing the same variable)

PreviousColor OptionNextDouble Option

Last updated 9 months ago

Was this helpful?