FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
Graphical user interface

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

fabgl::PS2Controller PS2Controller;
extern "C" void app_main()
{
PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
ib.begin(VGA_640x480_60Hz, 500, 400, 4); // creates its own 4-color VGA controller
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);
}
ib.end();
}
InputResult textInput(char const *titleText, char const *labelText, char *inOutString, int maxLength, char const *buttonCancelText="Cancel", char const *buttonOKText="OK", bool passwordMode=false)
Shows a dialog with a label and a text edit box.
Definition inputbox.cpp:149
InputResult messageFmt(char const *titleText, char const *buttonCancelText, char const *buttonOKText, const char *format,...)
Shows a dialog with just a label, using printf-like formatted text.
Definition inputbox.cpp:182
void end()
Releases resources and, if it was created by InputBox, disables the display output.
Definition inputbox.cpp:105
int select(char const *titleText, char const *messageText, char const *itemsText, char separator=';', char const *buttonCancelText="Cancel", char const *buttonOKText="OK")
Shows a dialog with a label and a list box.
Definition inputbox.cpp:200
void begin(char const *modeline=nullptr, int viewPortWidth=-1, int viewPortHeight=-1, int displayColors=16)
Initializes InputBox from a VGA modeline, creating a VGA display controller.
Definition inputbox.cpp:74
InputBox is a helper class to build simple user interfaces, such as wizards and input boxes.
Definition inputbox.h:328
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.
PS/2 device controller.
#define VGA_640x480_60Hz
Definition fabglconf.h:246

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():

fabgl::VGA16Controller DisplayController;
fabgl::PS2Controller PS2Controller;
class MyApp : public uiApp {
uiLabel * label;
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));
auto button = new uiButton(frame, "Click me", Point(10, 60), Size(80, 22));
button->onClick = [&]() {
label->setText("Clicked!");
label->update();
};
}
} app;
extern "C" void app_main()
{
PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
DisplayController.begin();
DisplayController.setResolution(VGA_640x480_60Hz);
app.run(&DisplayController); // returns when quit() is called
}
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.
Definition fabui.h:4323
uiButton(uiWindow *parent, char const *text, const Point &pos, const Size &size, uiButtonKind kind=uiButtonKind::Button, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:2537
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.
Definition fabui.cpp:1915
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.
Definition fabui.cpp:3188
void update()
Updates the label content.
Definition fabui.cpp:3240
void setText(char const *value)
Sets label text.
Definition fabui.cpp:3215
A label is a static text UI element.
Definition fabui.h:1606
Represents the coordinates of a point.
Definition fabutils.h:243
Represents a 24-bit RGB color.
Represents a two-dimensional size.
Definition fabutils.h:263

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
auto edit = new uiTextEdit(frame, "", Point(10, 30), Size(150, 20));
edit->onChange = [=]() { printf("text: %s\n", edit->text()); };
frame->onKeyUp = [&](uiKeyEventInfo const & key) {
if (key.VK == fabgl::VK_ESCAPE)
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.
Definition fabui.cpp:2699
@ VK_ESCAPE
Definition fabutils.h:1506
Contains details about the key event.
Definition fabui.h:165

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); // fires frame->onTimer every second
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);
auto ok = new uiButton(dlg, "OK", Point(70, 60), Size(60, 20));
ok->onClick = [=]() { dlg->exitModal(1); };
int result = showModalWindow(dlg); // returns the value passed to exitModal()
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 };
struct MyStyle : uiStyle {
void setStyle(uiObject * object, uint32_t styleClassID) override {
if (styleClassID == STYLE_BUTTON)
((uiButton *) object)->buttonStyle().backgroundColor = RGB888(64, 64, 128);
}
} myStyle;
// in init():
setStyle(&myStyle);
new uiButton(frame, "Styled", Point(10, 10), Size(80, 20), uiButtonKind::Button, true, STYLE_BUTTON);
Represents a button control. A button can have text and optionally a bitmap.
Definition fabui.h:1299
Base class of all UI elements like windows and controls.
Definition fabui.h:301
Base class for application styles.
Definition fabui.h:4244

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())
auto cutoff = new uiKnob(frame, "CUTOFF", Point(10, 30), Size(60, 76));
cutoff->onChange = [=]() {
synth.filter().setCutoff(ResonantLowPassFilter::cutoffFromPosition(cutoff->position()));
cutoff->setValueTextFmt("%d Hz", synth.filter().cutoff());
};
cutoff->setup(0, 100, 70); // range and default position (calls onChange)
auto piano = new uiPianoKeyboard(frame, Point(10, 120), Size(400, 120));
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.
Definition fabui.cpp:5429
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.
Definition fabui.cpp:6401

The Synth example uses all of them, themed with a fabgl::uiStyle.