Return values and callbacks
A control reports a change in two ways; use either:
- The callback is a function you pass to the control. It runs in the input step of the frame that received the input.
- The return value is true in the next frame's OnGUI.
Button("Greet", [this] { greeting = "Hello, " + name + "!"; });
if (Button("Reset", nullptr))
ResetSettings();
Value editors take the value by reference:
- They write to it directly.
- They return whether it changed.
Field(name);
Field(enabled, "Enabled");
if (Field(scale))
ApplyScale(scale);
Overloads per control: Controls.
GUIContent
A control shows a GUIContent:
- text, an icon, or both
- an optional tooltip
Strings and icons convert to it:
Button("Save", onSave);
Button(saveIcon, onSave);
Button({ saveIcon, "Save", "Write the file to disk" }, onSave);
Button({ "Save", "Write the file to disk" }, onSave);
Its strings are std::string_view, so they only need to live for the duration of the call. A local string is fine:
const std::string title = "Save " + fileName;
Button(title, onSave);
Units
- Sizes you pass in are CSS px, scaled by the UI scale like stylesheet sizes.
- Positions, rects and sizes the library measured or computed are whole device pixels.
Per-element overrides of style layout properties.
Definition gui_constraints.h:150
LayoutOverride & Width(CssPx w) &
Size, min and max size, in CSS px (scaled by the UI scale) or Pixels (as they are).
Definition gui_constraints.h:232
A size in device pixels, used as is (not scaled by the UI scale).
Definition gui_units.h:28
- A plain int converts to CssPx; Pixels must be written explicitly.
- int2 and IntRect are always pixels.
The UI scale is the main window's display scale times the zoom:
- set the zoom with SetUIScale(scale)
- a zoom of 1 follows the OS
styleManager.Px(n) converts CSS px to pixels, for example for custom drawing:
const int margin = styleManager.Px(8);
Previous: Begin and End | Overview | Next: Errors and saved state