TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
  1. Buttons

Panels as classes

Each panel is a class:

  • It holds the panel's values.
  • Its Draw function builds the panel's controls.
  • Controls are members of tachyon::GUIApplication, so Draw takes the app.

In my_app.h, after #include <gui_controls/gui_application.h>, #include <gui_controls/gui_dock_node.h> and #include <string>:

namespace my_app
{
// A count, and buttons that change it.
class CounterPanel
{
public:
void Draw(tachyon::GUIApplication& ui);
int count = 0;
};
// A few fields, and a greeting made from them.
class GreetingPanel
{
public:
void Draw(tachyon::GUIApplication& ui);
std::string name = "World";
int times = 1;
bool excited = false;
};
The application: owns the windows, the systems and the frame loop.
Definition gui_application.h:185

The counter panel

In my_app.cpp:

#include "my_app.h"
namespace my_app
{
void CounterPanel::Draw(tachyon::GUIApplication& ui)
{
ui.Text("Count: " + std::to_string(count));
ui.Button("Add one", [this] { ++count; });
ui.Button("Reset", [this] { count = 0; });
void Text(ID id, std::string_view text, StyleRef style, LayoutOverride &&constraints={})
Static text.
bool Button(ID id, const GUIContent &content, std::function< void()> onClick, StyleRef style, LayoutOverride &&constraints={})
Controls take (id, icon, text, ..., style, constraints), each part optional.
bool BeginHorizontal(ID id, StyleRef style, LayoutOverride &&constraints={})
Undecorated flex containers; the Wrap variants wrap onto further lines/columns.
void EndHorizontal()
Closes BeginHorizontal.

Close Draw with }.

  • Draw runs every frame and describes the panel as it is now, so the text always shows the current count.
  • No IDs are needed:
    • The library keeps a control's state between frames under its ID.
    • Each control here takes its ID from its call site, the file and line of the call.
    • The call site stays the same across frames, so the ID does too.
  • BeginHorizontal/EndHorizontal put the buttons in a row.
    • Every Begin* needs its End*, whether or not the Begin* returned true.
  • A button's callback runs after all controls are built.
    • So it can change the panel's values freely.
    • The next frame shows the result.
  • [this] is safe because the app owns the panel (step 4).

Previous: 1. Set up the project | Tutorial | Next: 3. Fields