TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
controls/controls.cpp

The common controls as label/value rows: buttons, toggles, text and number fields, a slider, a color, a menu and tabs.

The common controls as label/value rows: buttons, toggles, text and number fields, a slider, a color, a menu and tabs.Walkthrough: Controls.

// Controls overview: the common controls, laid out as label/value rows.
#include <cstdio>
#include <string>
class ControlsApp : public tachyon::GUIApplication
{
public:
tachyon::Result<void> Initialize(const InitializeOptions& options = {}) override
{
if (tachyon::Result<void> result = GUIApplication::Initialize(options); !result)
return result;
windowID = idManager.CreateStaticID();
menuID = idManager.CreateStaticID();
return {};
}
void OnGUI() override
{
// The scope ends the window when it leaves, open or not.
auto window = Window(windowID, "Controls", tachyon::LayoutOverride{}.Size(480, 560));
if (!window)
return;
BeginVertical(tachyon::LayoutOverride{}.FlexGrow(1));
// A label group lines up Label + control pairs as rows with a shared,
// resizable label column. DragLabel also drags the value it labels.
BeginLabelGroup(140);
Label("Button");
Button("Click me", [this] { ++clicks; });
Label("Clicks");
Text(std::to_string(clicks));
Label("Checkbox");
Field(enabled, enabled ? "On" : "Off");
Label("Toggles");
BeginHorizontal();
Toggle(bold, "B");
Toggle(italic, "I");
Toggle(underline, "U");
EndHorizontal();
Label("Text");
Field(text);
Label("Multiline");
DragLabel("Float");
Field(floatValue);
DragLabel("Int [0, 10]");
Field(intValue, tachyon::NumericFieldConfig<int>{ .minVal = 0, .maxVal = 10 });
DragLabel("Vector");
Field(position);
Label("Slider");
SliderField(volume, 0.0f, 100.0f, "Volume");
Label("Color");
ColorField(color);
Label("Menu");
if (BeginMenu(menuID, "Choose...", tachyon::LayoutOverride{}.NoGrow().NoShrink()))
{
for (const char* item : { "Apple", "Banana", "Cherry" })
MenuItem(item, [this, item] { choice = item; });
}
EndMenu();
Label("Chosen");
Text(choice);
EndLabelGroup();
// Tabs select which content to show; the content is up to the app.
BeginTabBar(idManager.AllocateDynamicID("tabs"), &selectedTab, nullptr);
Tab("First");
Tab("Second");
EndTabBar();
Text(selectedTab == 0 ? "Content of the first tab" : "Content of the second tab");
EndVertical();
}
private:
int clicks = 0;
bool enabled = true;
bool bold = false, italic = false, underline = false;
std::string text = "Edit me";
std::string notes = "Line one\nLine two";
float floatValue = 1.5f;
int intValue = 5;
tachyon::float3 position { 1.0f, 2.0f, 3.0f };
float volume = 75.0f;
tachyon::color4f color { 0.2f, 0.6f, 1.0f, 1.0f };
std::string choice = "(none)";
int selectedTab = 0;
};
int main()
{
ControlsApp app;
tachyon::Result<void> result = app.Initialize();
if (result)
result = app.Run();
app.Cleanup();
if (!result)
{
std::fprintf(stderr, "%s\n", result.GetError().message.c_str());
return 1;
}
return 0;
}
The application: owns the windows, the systems and the frame loop.
Definition gui_application.h:185
A value, or the Error saying why there is none.
Definition gui_result.h:36
const Error & GetError() const
The error; only valid when !HasValue().
Definition gui_result.h:61
@ Multiline
Enter inserts a newline instead of committing.
Definition gui_text_system.h:102
@ ContinuousUpdate
update the text on every keystroke, not only on commit
Definition gui_text_system.h:101
std::string message
what went wrong, for logs and users
Definition gui_result.h:28
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
Per-element overrides of style layout properties.
Definition gui_constraints.h:150
LayoutOverride & FlexGrow(int16_t v) &
Overrides the style's flex-grow / flex-shrink.
Definition gui_constraints.h:317
int2 Size() const
The explicit sizes; kAuto on axes without one.
Definition gui_constraints.h:205
LayoutOverride & NoGrow() &
Shorthands for FlexGrow(0) / FlexShrink(0).
Definition gui_constraints.h:322
Drag speed and range of a numeric Field, specialized per value type.
Definition gui_numeric_field_config.h:19
Definition gui_base_types.h:395
Definition gui_base_types.h:150