Skip to content

Radio buttons

The radio buttons is an input element and displays up to three mutually exclusive options.

Configure

Ensure the GUI library is included and initialised according to the configuration procedure.

radioButtons myRadioButtons;

The constructor creates a radio-button.

1
2
3
4
5
myRadioButtons.dDefine(10, 10, 80, 60,
     setItem(0x0101, "Radio 1"),
     setItem(0x0102, "Radio 2"),
     setItem(0x0103, "Radio 3"),
     fontSizeAutomatic);

dDefine() defines the radio-button with vector coordinates.

The required parameters are

  • The first line specifies the vector coordinates: top-left coordinates x-y then width and height in pixels;

  • The second line contains the item of the first option, here built with setItem() with an index and a text;

  • The third line contains the item of the second option, here built with setItem() with an index and a text.

The optional parameters are

  • The fourth line contains the item of the third option, here built with setItem() with an index and a text. The value can be itemEmpty to get access to the fifth line;

  • The fifth line is optional: the size of the font, by default fontSizeAutomatic for automatic.

By default, the element is enabled according to the GUI setting and no option is selected.

As an alternative,

radioButtons myRadioButtons;
myRadioButtons.dStringDefine(10, 10, 80, 60,
     "Radio 1",
     "Radio 2",
     "Radio 3",
     fontSizeAutomatic);

The required parameters are

  • The first line specifies the vector coordinates: top-left coordinates x-y then width and height in pixels;

  • The second line contains the text of the first option, with index set to 0x0001;

  • The third line contains the text of the second option, with index set to 0x0002;

The optional parameters are

  • The fourth line contains the text of the third option, with index set to 0x0003;

  • The fifth line is optional: the size of the font, by default fontSizeAutomatic for automatic.

By default, the element is oriented horizontally, not enabled, and with no option selected.

Then,

myRadioButtons.setOption(optionVertical);
myRadioButtons.setState(stateEnabled);
myRadioButtons.setValue(0);

setOption() defines the orientation of the bar-graph:

  • optionHorizontal or false for horizontal orientation;
  • optionVertical or true for vertical orientation.

Default is false for horizontal orientation.

setState() defines whether touch is enabled for the element.

Select

  • stateDisabled or false for touch disabled;
  • stateEnabled or true for touch enabled.

Default is false for touch disabled.

setValue() sets the initial selected option of the radio-buttons element:

  • 0 for no option selected;
  • 1, 2 or 3 for the first, second or third option selected.

Default is 0 for no option selected.

Use

myRadioButtons.draw();
bool result = myRadioButtons.check();
uint8_t value = myRadioButtons.getValue();
uint32_t index = myRadioButtons.getIndex();

draw() displays the check-box.

check() polls the touch controller and returns true is the button is pressed.

An optional parameter defines the mode of how the element is checked:

  • checkNormal or false for normal check mode;
  • checkInstant or true for instant check mode.

Default mode is normal check mode.

In normal check mode, the element raises the event when the finger is released from the element. Additionally, the element goes through a cinematic sequence.

Going from option 2 to 3: start, press, hold, release

In instant check mode, the element raises the event when the finger touches the element. No cinematic sequence is performed.

Going from option 1 to 2: start, press

getValue() gets the value of the selected option:

  • 0 for no option selected;
  • 1, 2 or 3 for the first, second or third option selected.

getIndex() returns the index of the selected option, set at dDefine().

Example

This is the core of the code from example GUI_Radio.ino.

void displayRadioButtons()
{
    myScreen.setOrientation(myOrientation);
    uint16_t x, y, dx, dy;

    x = myScreen.screenSizeX();
    y = myScreen.screenSizeY();
    dx = x / 11;
    dy = y / 5;

    myGUI.delegate(false);

    dLabel(0, 0, x, dy, "Radio buttons", myColours.black, myColours.white, -1, 1, fontLarge);

    text myText1;
    myText1.dDefine(dx, dy, dx * 4, dy, 0, 0, fontMedium);
    myText1.setOption(optionWithoutFrame);
    myText1.draw("None");

    text myText2;
    myText2.dDefine(dx * 6, dy, dx * 4, dy, 0, 0, fontMedium);
    myText2.setOption(optionWithoutFrame);
    myText2.draw("None");

    radioButtons myRadioButtons1;
    myRadioButtons1.dStringDefine(dx, dy * 2, dx * 4, dy * 2, "Radio 1", "Radio 2", "Radio 3", fontMedium);
    myRadioButtons1.setOption(optionVertical);
    myRadioButtons1.setState(stateEnabled);
    myRadioButtons1.setValue(0);
    myRadioButtons1.draw();

    radioButtons myRadioButtons2;
    myRadioButtons2.dStringDefine(dx * 6, dy * 2, dx * 4, dy * 2, "Instant 1", "Instant 2", "Instant 3", fontMedium);
    myRadioButtons2.setOption(optionVertical);
    myRadioButtons2.setState(stateEnabled);
    myRadioButtons2.setValue(0);
    myRadioButtons2.draw();

    drawNext();
    myScreen.flushFast();

    myGUI.delegate(true);
    while (!checkNext())
    {
        if (myRadioButtons1.check())
        {
            myText1.draw(formatString("Radio %i", myRadioButtons1.getValue()));
        }
        else if (myRadioButtons2.check(checkInstant))
        {
            myText2.draw(formatString("Instant %i", myRadioButtons2.getValue()));
        }

        hV_HAL_delayMilliseconds(10);
    }
}