FabGL offers two levels of user interface:
- fabgl::InputBox: ready-made modal dialogs (messages, text input, lists, menus, progress, file selection), usable with a few lines of code and no event handling;
- fabgl::uiApp: a complete windowing system with overlapping frames, controls, keyboard focus, mouse and timers.
Quick dialogs with InputBox
extern "C" void app_main()
{
PS2Controller.
begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
char name[32] = "";
if (ib.
textInput(
"Setup",
"Your name", name,
sizeof(name) - 1) == InputResult::Enter) {
int choice = ib.
select(
"Setup",
"Favorite color",
"Red;Green;Blue");
ib.
messageFmt(
"Setup",
nullptr,
"OK",
"Hello %s, you chose %d", name, choice);
}
}
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes the PS/2 device controller.
InputBox can also use an existing display controller (ib.begin(&DisplayController)) or run inside a uiApp.
Structure of a uiApp application
An application derives from fabgl::uiApp, creates its windows and controls in init() and is started with run():
class MyApp :
public uiApp {
void init() override {
rootWindow()->frameStyle().backgroundColor =
RGB888(0, 0, 64);
auto frame =
new uiFrame(rootWindow(),
"My window",
Point(50, 50),
Size(300, 150));
label =
new uiLabel(frame,
"Press the button",
Point(10, 30));
button->onClick = [&]() {
};
}
} app;
extern "C" void app_main()
{
PS2Controller.
begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
DisplayController.
begin();
app.run(&DisplayController);
}
VGA bitmapped display controller with 16 colors.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
Represents the whole application base class.
uiFrame(uiWindow *parent, char const *title, const Point &pos, const Size &size, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
uiLabel(uiWindow *parent, char const *text, const Point &pos, const Size &size=Size(0, 0), bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
void update()
Updates the label content.
void setText(char const *value)
Sets label text.
A label is a static text UI element.
Represents the coordinates of a point.
Represents a 24-bit RGB color.
Represents a two-dimensional size.
Key points:
- Windows and controls are created with new and owned by their parent. They are destroyed automatically when the parent is destroyed or when the application ends: never delete them yourself (use uiApp::destroyWindow() to remove one at run time).
- Every window has a parent. The root window (rootWindow()) covers the whole screen; frames (fabgl::uiFrame) are movable, resizable windows with a title bar; controls (buttons, labels, ...) live inside frames.
- Positions are relative to the parent window.
- run() executes the event loop in the calling task. runAsync() runs it in a new task.
Events
Events are delivered through delegates: public members to which you assign a function or a lambda.
| Delegate | Available on | Called when |
| onClick | uiButton, uiCheckBox, uiLabel, uiListBox | the control is clicked (or activated with the keyboard) |
| onChange | uiTextEdit, uiCheckBox, uiSlider, uiComboBox, uiListBox, uiButton (switch kind), uiKnob, uiToggleSwitch, uiLevelBar, uiTileSelector | the value changes |
| onSelect | uiSimpleMenu, uiSplitButton | an item is chosen |
| onStep | uiStepper | an arrow button is pressed (-1 or +1) |
| onNoteOn, onNoteOff | uiPianoKeyboard | a key is pressed or released (MIDI note number) |
| onKeyDown, onKeyUp | uiFrame and derived windows | a key is pressed or released while the frame is active |
| onPaint | uiFrame (no parameters), uiPaintBox, uiGraph (receive an area) | the window must be repainted |
| onShow, onHide, onResize | uiFrame | the frame is shown, hidden or resized |
| onTimer | uiFrame, uiApp | a timer created with setTimer() expires |
edit->onChange = [=]() { printf("text: %s\n", edit->text()); };
quit(0);
};
uiTextEdit(uiWindow *parent, char const *text, const Point &pos, const Size &size, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Contains details about the key event.
Event handlers run in the GUI task, so they can safely create, modify and destroy windows. From other tasks, send an event with uiApp::postEvent() instead of touching windows directly.
Updating the screen
Changing a property (text, color, items...) does not always repaint the control: call repaint() on the control (or update() on labels, which also recalculates their size). Repainting is queued and performed by the GUI task.
To draw custom content, use a fabgl::uiPaintBox or the onPaint delegate of a frame, and draw with the frame canvas:
frame->onPaint = [=]() {
auto cv = frame->canvas();
cv->setPenColor(Color::BrightRed);
cv->drawLine(0, 0, 100, 100);
};
Timers and modal windows
auto timer = setTimer(frame, 1000);
frame->onTimer = [&](uiTimerHandle t) { };
killTimer(timer);
A modal window blocks the rest of the interface until it is closed:
auto dlg =
new uiFrame(rootWindow(),
"Question", UIWINDOW_PARENTCENTER,
Size(200, 100),
false);
ok->onClick = [=]() { dlg->exitModal(1); };
int result = showModalWindow(dlg);
destroyWindow(dlg);
For simple questions use uiApp::messageBox(), uiApp::inputBox() and uiApp::fileDialog().
Styles
Every window and control has style structures (frameStyle(), windowStyle(), buttonStyle(), labelStyle(), ...) that can be changed directly. To style many controls consistently, derive from fabgl::uiStyle, assign a style class ID to each control (last constructor parameter) and install the style with uiApp::setStyle():
enum { STYLE_NONE, STYLE_BUTTON };
void setStyle(
uiObject *
object, uint32_t styleClassID)
override {
if (styleClassID == STYLE_BUTTON)
((
uiButton *)
object)->buttonStyle().backgroundColor =
RGB888(64, 64, 128);
}
} myStyle;
setStyle(&myStyle);
new uiButton(frame,
"Styled",
Point(10, 10),
Size(80, 20), uiButtonKind::Button,
true, STYLE_BUTTON);
Base class of all UI elements like windows and controls.
Base class for application styles.
Synth and instrument controls
These controls are custom painted and fully styleable. They were designed for audio applications, but are generic:
| Control | Description |
| fabgl::uiKnob | rotary knob with a ring of dots: drag up/down, mouse wheel, keyboard; double click restores the default value. The text below the knob is set with setValueText() / setValueTextFmt() inside onChange |
| fabgl::uiToggleSwitch | on/off switch with an optional "ON" / "OFF" text |
| fabgl::uiLevelBar | horizontal segmented bar (led meter look), read-only or editable with the mouse and the keyboard |
| fabgl::uiLedBar | row of leds, each with a state (off or one of three colors) |
| fabgl::uiStepper | a text between "previous" and "next" arrow buttons |
| fabgl::uiTileSelector | row of selectable tiles painted by the application (onPaintTile), with optional captions |
| fabgl::uiGraph | plots a sequence of values, optionally filling the area below the curve; onPaintBackground and onPaint add markers and labels |
| fabgl::uiPianoKeyboard | on-screen piano keyboard played with the mouse (with glissando) or by the application (press() / release()) |
cutoff->onChange = [=]() {
cutoff->setValueTextFmt("%d Hz", synth.filter().cutoff());
};
cutoff->setup(0, 100, 70);
piano->onNoteOn = [&](int note) { synth.noteOn(note); };
piano->onNoteOff = [&](int note) { synth.noteOff(note); };
static int cutoffFromPosition(int position)
Maps a linear position (for example a knob) to a cutoff frequency on a logarithmic scale.
uiKnob(uiWindow *parent, char const *label, const Point &pos, const Size &size, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
uiPianoKeyboard(uiWindow *parent, const Point &pos, const Size &size, int keysCount=25, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
The Synth example uses all of them, themed with a fabgl::uiStyle.