Skip to content

UICheckBox

A clickable checkbox UI element.

Description

UICheckBox is a clickable checkbox that can be toggled between checked and unchecked states. It supports both physical (keyboard/gamepad) and touch input. It can trigger a callback function when its state changes and integrates with UI layouts for automatic navigation.

Namespace

namespace pixelroot32::graphics::ui {
    class UICheckBox : public UIElement {
        // ...
    };
}

Inheritance

  • Inherits from: UIElement

Constructors

UICheckBox(string label, uint8_t index, float x, float y, float w, float h, bool checked = false, function callback = nullptr, int fontSize = 2)

Constructs a new UICheckBox.

Parameters: - label (std::string): Checkbox label text - index (uint8_t): Navigation index (for D-pad navigation in layouts) - x (float): X position - y (float): Y position - w (float): Width - h (float): Height - checked (bool, optional): Initial checked state. Default: false - callback (std::function, optional): Function to call when the state changes. Default: nullptr - fontSize (int, optional): Text size multiplier. Default: 2

Example:

#include "graphics/ui/UICheckBox.h"

void onCheckChanged(bool checked) {
    if (checked) {
        // Sound enabled
    } else {
        // Sound disabled
    }
}

// Create checkbox
pixelroot32::graphics::ui::UICheckBox* soundCheckbox = new pixelroot32::graphics::ui::UICheckBox(
    "Enable Sound",
    0,  // index
    64.0f, 64.0f,  // position
    120.0f, 20.0f, // size
    true,          // initial state
    onCheckChanged,
    1              // font size
);

Public Methods

void setStyle(Color textCol, Color bgCol, bool drawBg = false)

Configures the checkbox's visual style.

Parameters: - textCol (Color): Color of the text - bgCol (Color): Color of the background - drawBg (bool, optional): Whether to draw the background rectangle. Default: false

Returns: - void

void setChecked(bool checked)

Sets the checked state.

Parameters: - checked (bool): True if checked

Returns: - void

bool isChecked() const

Checks if the checkbox is currently checked.

Returns: - bool: true if checked

void toggle()

Toggles the checkbox state and triggers the callback.

Returns: - void

void setSelected(bool selected)

Sets the selection state (e.g., focused via D-pad).

Parameters: - selected (bool): True if selected

Returns: - void

bool getSelected() const

Checks if the checkbox is currently selected.

Returns: - bool: true if selected

Callbacks

onCheckChanged

The onCheckChanged callback is a std::function<void(bool)> that is triggered whenever the checkbox state changes via setChecked() or toggle().

checkbox->onCheckChanged = [](bool isChecked) {
    Serial.println(isChecked ? "Checked!" : "Unchecked!");
};

UICheckBox is designed to work seamlessly with UILayout containers (like UIVerticalLayout).

  • Focusable: Returns true for isFocusable(), allowing it to receive focus in a layout.
  • Input Handling: When selected (focused), it listens for the button index provided in the constructor (typically the 'A' button) to toggle its state.
  • Visual Feedback: When selected, it displays a selection indicator (usually a > character) if no background is drawn, or highlights its text/border.