Tramway SDK
Classes | Typedefs | Enumerations | Functions | Variables
tram::GUI Namespace Reference

Immediate mode GUI system. More...

Classes

struct  FrameObject
 

Typedefs

typedef uint32_t font_t
 
typedef uint32_t glyph_t
 

Enumerations

enum  orientation : uint32_t {
  TEXT_LEFT , TEXT_CENTER , TEXT_RIGHT , TEXT_JUSTIFIED ,
  LINE_LOW , LINE_NORMAL , LINE_HIGH , FRAME_LEFT ,
  FRAME_RIGHT , FRAME_TOP , FRAME_BOTTOM , FRAME_LEFT_INV ,
  FRAME_RIGHT_INV , FRAME_TOP_INV , FRAME_BOTTOM_INV , FRAME_INSET ,
  FRAME_CENTER_VERTICAL , FRAME_CENTER_HORIZONTAL
}
 
enum  GlyphType { TEXT , WIDGET }
 
enum  widget : glyph_t {
  WIDGET_TOP_LEFT = 0 , WIDGET_TOP_MIDDLE = 1 , WIDGET_TOP_RIGHT = 2 , WIDGET_MIDDLE_LEFT = 3 ,
  WIDGET_MIDDLE_MIDDLE = 4 , WIDGET_MIDDLE_RIGHT = 5 , WIDGET_BOTTOM_LEFT = 6 , WIDGET_BOTTOM_MIDDLE = 7 ,
  WIDGET_BOTTOM_RIGHT = 8 , WIDGET_LEFT = 0 , WIDGET_MIDDLE = 1 , WIDGET_RIGHT = 2 ,
  WIDGET_BUTTON = 0 , WIDGET_BUTTON_PRESSED = 9 , WIDGET_BUTTON_SELECTED_ENABLED = 18 , WIDGET_BUTTON_SELECTED_DISABLED = 27 ,
  WIDGET_BUTTON_DISABLED = 36 , WIDGET_BUTTON_PRESSED_DISABLED = 45 , WIDGET_WINDOW = 54 , WIDGET_REVERSE_WINDOW = 63 ,
  WIDGET_SELECT_BOX = 72 , WIDGET_SELECT_BOX_PRESSED = 81 , WIDGET_SELECT_BOX_SELECTED = 90 , WIDGET_TEXT_BOX = 99 ,
  WIDGET_TEXT_BOX_DISABLED = 108 , WIDGET_BORDER = 117 , WIDGET_SCROLL_BAR_TRACK_VERTICAL = 126 , WIDGET_SCROLL_BAR_TRACK_HORIZONTAL = 129 ,
  WIDGET_DIVIDER_HORIZONTAL = 132 , WIDGET_DIVIDER_VERTICAL = 135 , WIDGET_SLIDER_TRACK_VERTICAL = 138 , WIDGET_SLIDER_TRACK_HORIZONTAL = 138 ,
  WIDGET_SCROLL_BAR_VERTICAL = 144 , WIDGET_SCROLL_BAR_HORIZONTAL = 147 , WIDGET_TAB = 150 , WIDGET_PROGESS_BAR = 153 ,
  WIDGET_RADIO_BUTTON = 156 , WIDGET_CHECK_BUTTON = 162 , WIDGET_SLIDER_VERTICAL = 168 , WIDGET_SLIDER_HORIZONTAL = 174 ,
  WIDGET_BUTTON_UP = 180 , WIDGET_BUTTON_DOWN = 186 , WIDGET_BUTTON_LEFT = 192 , WIDGET_BUTTON_RIGHT = 198 ,
  WIDGET_BUTTON_EXIT = 204 , WIDGET_BUTTON_HELP = 210 , WIDGET_BUTTON_2_WINDOW = 216 , WIDGET_BUTTON_1_WINDOW = 222 ,
  WIDGET_DEFAULT = 255
}
 

Functions

void SetSelectedText (char *text)
 Sets the text which is selected in a textbox. More...
 
void SetColor (Render::color_t color, GlyphType type)
 Sets the color. More...
 
void SetFont (font_t font, GlyphType type)
 Sets the font. More...
 
void RestoreColor (GlyphType type)
 Restores previous glyph color. More...
 
void RestoreFont (GlyphType type)
 Restores previous glyph font. More...
 
void SetGlyphDefaults (Render::color_t color, font_t font, GlyphType type)
 Overrides glyph default colors and fonts. More...
 
void SetScaling (uint32_t scale)
 Sets the scaling factor. More...
 
uint32_t GetScaling ()
 Returns the scaling factor. More...
 
void SetCursorDelayed (UI::CursorType cursor)
 Sets the cursor. More...
 
void Init ()
 Performs initialization of the GUI system. More...
 
void UpdateDrawListFonts ()
 Submits registered fonts to the renderer. More...
 
void Update ()
 Submits all of the glyphs for rendering. More...
 
font_t RegisterFont (Render::Sprite *sprite)
 Registers a font. More...
 
void SetGlyph (float x, float y, float z, float w, float h, float tex_x, float tex_y, float tex_w, float tex_h, const vec3 &color, font_t font)
 Adds a glyph to rendering list. More...
 
uint32_t GlyphWidth (font_t font, glyph_t glyph)
 
uint32_t GlyphHeight (font_t font, glyph_t glyph)
 
uint32_t GlyphBorderV (font_t font, glyph_t glyph)
 
void DrawGlyph (font_t font, glyph_t glyph, const vec3 &color, uint32_t x, uint32_t y, uint32_t w=0, uint32_t h=0)
 
void Glyph (font_t font, glyph_t glyph)
 Draws a glyph from a font. More...
 
void DrawBox (font_t font, glyph_t glyph, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
 Draws a glyph box. More...
 
uint32_t TextWidth (font_t font, const char *text)
 
void Text (const char *text, uint32_t orientation)
 Draws text on the screen. More...
 
void DrawBoxHorizontal (font_t font, glyph_t glyph, uint32_t x, uint32_t y, uint32_t w)
 Draws a horizontal bar. More...
 
void PushFrame (uint32_t x, uint32_t y, uint32_t w, uint32_t h)
 Pushes a frame. More...
 
void PushFrameRelative (uint32_t orientation, uint32_t offset)
 Pushes a frame releative to the previous. More...
 
void PopFrame ()
 Removes a frame from the frame stack. More...
 
void PushFrameRelativeKeepCursor (uint32_t orientation, uint32_t offset, bool keep_x, bool keep_y)
 Pushes a frame, but keeps the cursor in place. More...
 
void PopFrameKeepCursor (bool keep_x, bool keep_y)
 
bool CursorOver (uint32_t x, uint32_t y, uint32_t w, uint32_t h)
 
bool ClickHandled ()
 
bool ClickHandledLate ()
 
bool Clicked ()
 
bool Button (const char *text, bool enabled, uint32_t width)
 Draws a button. More...
 
bool RadioButton (uint32_t index, uint32_t &selected, const char *text, bool enabled)
 Draws a radio button. More...
 
bool CheckBox (bool &selected, const char *text, bool enabled)
 Draws a checkbox. More...
 
bool Slider (float &value, bool enabled, uint32_t width)
 Draws a horizontal slider. More...
 
void NewLine (uint32_t line)
 Draws a new line. More...
 
void HorizontalDivider ()
 Draws a horizontal divider. More...
 
void FillFrame (glyph_t glyph)
 Fills the frame with a glyph. More...
 
bool TextBox (char *text, uint32_t length, bool enabled, uint32_t w, uint32_t h)
 Draws a textbox. More...
 
void TextBox (const char *text, uint32_t w, uint32_t h)
 Draws a textbox. More...
 
void Begin ()
 Begins the GUI commands for the frame. More...
 
void End ()
 Ends the GUI commands for the frame. More...
 
void Glyph (glyph_t glyph)
 

Variables

uint32_t last_update_frame = 0
 
bool beginned = false
 
bool mouse_click_not_handled = true
 
bool mouse_click_not_handled_late = true
 

Detailed Description

Immediate mode GUI system.

If you don't like it, you can always integrate imgui, but I personally very much prefer this way of building GUIs.

First you need to register fonts, which are just Sprites. The glyphs are just sprite frame indices. There are some default fonts in the Ext::Menu system, if you don't want to make your own fonts.

To draw widgets, just call their respective functions once per frame.

To position the widgets, use the frame pushing and popping functions.

See also
https://racenis.github.io/tram-sdk/documentation/framework/gui.html

Typedef Documentation

◆ font_t

typedef uint32_t tram::GUI::font_t

◆ glyph_t

typedef uint32_t tram::GUI::glyph_t

Enumeration Type Documentation

◆ GlyphType

Enumerator
TEXT 
WIDGET 

◆ orientation

enum tram::GUI::orientation : uint32_t
Enumerator
TEXT_LEFT 
TEXT_CENTER 
TEXT_RIGHT 
TEXT_JUSTIFIED 
LINE_LOW 
LINE_NORMAL 
LINE_HIGH 
FRAME_LEFT 
FRAME_RIGHT 
FRAME_TOP 
FRAME_BOTTOM 
FRAME_LEFT_INV 
FRAME_RIGHT_INV 
FRAME_TOP_INV 
FRAME_BOTTOM_INV 
FRAME_INSET 
FRAME_CENTER_VERTICAL 
FRAME_CENTER_HORIZONTAL 

◆ widget

Enumerator
WIDGET_TOP_LEFT 
WIDGET_TOP_MIDDLE 
WIDGET_TOP_RIGHT 
WIDGET_MIDDLE_LEFT 
WIDGET_MIDDLE_MIDDLE 
WIDGET_MIDDLE_RIGHT 
WIDGET_BOTTOM_LEFT 
WIDGET_BOTTOM_MIDDLE 
WIDGET_BOTTOM_RIGHT 
WIDGET_LEFT 
WIDGET_MIDDLE 
WIDGET_RIGHT 
WIDGET_BUTTON 
WIDGET_BUTTON_PRESSED 
WIDGET_BUTTON_SELECTED_ENABLED 
WIDGET_BUTTON_SELECTED_DISABLED 
WIDGET_BUTTON_DISABLED 
WIDGET_BUTTON_PRESSED_DISABLED 
WIDGET_WINDOW 
WIDGET_REVERSE_WINDOW 
WIDGET_SELECT_BOX 
WIDGET_SELECT_BOX_PRESSED 
WIDGET_SELECT_BOX_SELECTED 
WIDGET_TEXT_BOX 
WIDGET_TEXT_BOX_DISABLED 
WIDGET_BORDER 
WIDGET_SCROLL_BAR_TRACK_VERTICAL 
WIDGET_SCROLL_BAR_TRACK_HORIZONTAL 
WIDGET_DIVIDER_HORIZONTAL 
WIDGET_DIVIDER_VERTICAL 
WIDGET_SLIDER_TRACK_VERTICAL 
WIDGET_SLIDER_TRACK_HORIZONTAL 
WIDGET_SCROLL_BAR_VERTICAL 
WIDGET_SCROLL_BAR_HORIZONTAL 
WIDGET_TAB 
WIDGET_PROGESS_BAR 
WIDGET_RADIO_BUTTON 
WIDGET_CHECK_BUTTON 
WIDGET_SLIDER_VERTICAL 
WIDGET_SLIDER_HORIZONTAL 
WIDGET_BUTTON_UP 
WIDGET_BUTTON_DOWN 
WIDGET_BUTTON_LEFT 
WIDGET_BUTTON_RIGHT 
WIDGET_BUTTON_EXIT 
WIDGET_BUTTON_HELP 
WIDGET_BUTTON_2_WINDOW 
WIDGET_BUTTON_1_WINDOW 
WIDGET_DEFAULT 

Function Documentation

◆ Begin()

void tram::GUI::Begin ( )

Begins the GUI commands for the frame.

Pushes the first frame to the frame stack. This function has to be called first, before the End() and the Update() functions. You can call Begin() as much as you want, you just have to call an End() before you can call Begin() again. This should help dealing a bit with the global state that the system has.

◆ Button()

bool tram::GUI::Button ( const char *  text,
bool  enabled,
uint32_t  width 
)

Draws a button.

Parameters
textText of the button.
enabledIf set to false, the button won't be clickable.
widthWidth of the button, in pixels.
Returns
True if the button was clicked on.

◆ CheckBox()

bool tram::GUI::CheckBox ( bool &  selected,
const char *  text,
bool  enabled 
)

Draws a checkbox.

Parameters
selectedIf set to true, the button will be drawn checked.
textText of the button.
enabledIf set to false, the button won't be clickable.
Returns
True if clicked on. The modified state of the button will be written out to the selected parameter.

◆ Clicked()

bool tram::GUI::Clicked ( )

◆ ClickHandled()

bool tram::GUI::ClickHandled ( )

◆ ClickHandledLate()

bool tram::GUI::ClickHandledLate ( )

◆ CursorOver()

bool tram::GUI::CursorOver ( uint32_t  x,
uint32_t  y,
uint32_t  w,
uint32_t  h 
)

◆ DrawBox()

void tram::GUI::DrawBox ( font_t  font,
glyph_t  glyph,
uint32_t  x,
uint32_t  y,
uint32_t  w,
uint32_t  h 
)

Draws a glyph box.

Glyph boxes are set up so that the first glyph, i.e. the one that you pass in the glyph parameter is the top-left corner. Then the numerically next glyph is take as the top-middle glpyh, then the top-right. This continues going left-to-right, top-to-bottom until all 9 squares are covered. If you are using a default widget layout font, you can use the enum values WIDGET_WINDOW, WIDGET_REVERSE_WINDOW, WIDGET_BUTTON, WIDGET_SELECT_BOX or WIDGET_BORDER for the glyph index.

◆ DrawBoxHorizontal()

void tram::GUI::DrawBoxHorizontal ( font_t  font,
glyph_t  glyph,
uint32_t  x,
uint32_t  y,
uint32_t  w 
)

Draws a horizontal bar.

Similar to how DrawBox() is used, except we only have 3 squares, the left, middle and right.

◆ DrawGlyph()

void tram::GUI::DrawGlyph ( font_t  font,
glyph_t  glyph,
const vec3 color,
uint32_t  x,
uint32_t  y,
uint32_t  w = 0,
uint32_t  h = 0 
)

◆ End()

void tram::GUI::End ( )

Ends the GUI commands for the frame.

This has to be called sometime after Begin(), but before Update().

◆ FillFrame()

void tram::GUI::FillFrame ( glyph_t  glyph)

Fills the frame with a glyph.

Uses the DrawBox() function internally, so it works just like it, except that it uses the topmost frame to determine the position and sixze of the box.

◆ GetScaling()

uint32_t tram::GUI::GetScaling ( )

Returns the scaling factor.

See SetScaling().

◆ Glyph() [1/2]

void tram::GUI::Glyph ( font_t  font,
glyph_t  glyph 
)

Draws a glyph from a font.

◆ Glyph() [2/2]

void tram::GUI::Glyph ( glyph_t  glyph)

◆ GlyphBorderV()

uint32_t tram::GUI::GlyphBorderV ( font_t  font,
glyph_t  glyph 
)

◆ GlyphHeight()

uint32_t tram::GUI::GlyphHeight ( font_t  font,
glyph_t  glyph 
)

◆ GlyphWidth()

uint32_t tram::GUI::GlyphWidth ( font_t  font,
glyph_t  glyph 
)

◆ HorizontalDivider()

void tram::GUI::HorizontalDivider ( )

Draws a horizontal divider.

◆ Init()

void tram::GUI::Init ( )

Performs initialization of the GUI system.

Render system must be initialized first.

◆ NewLine()

void tram::GUI::NewLine ( uint32_t  line)

Draws a new line.

Essentially just moves the cursor down a little bit and resets it to the left side of the frame. Sort of like a line break.

Parameters
lineLine type. Either LINE_LOW, LINE_NORMAL or LINE_HIGH.

◆ PopFrame()

void tram::GUI::PopFrame ( )

Removes a frame from the frame stack.

◆ PopFrameKeepCursor()

void tram::GUI::PopFrameKeepCursor ( bool  keep_x,
bool  keep_y 
)

◆ PushFrame()

void tram::GUI::PushFrame ( uint32_t  x,
uint32_t  y,
uint32_t  w,
uint32_t  h 
)

Pushes a frame.

The coordinates are in absolute screen cooordinates. This function could be useful for something, I just can't think of anything.

◆ PushFrameRelative()

void tram::GUI::PushFrameRelative ( uint32_t  orientation,
uint32_t  offset 
)

Pushes a frame releative to the previous.

Check the regulat HTML docs on the website for a graphical description with, i.e. with images, to see how this works. But essentially what this function does, is it takes the previous frame and measures out the offset from the specified orientation and then pushes a new frame with the are from the offset. Initially the first frame is pushed, and it takes up the whole screen. If you wanted to use make the topmost 200px part of the screen a frame, you would set the orientation to FRAME_TOP and offset to 200.

◆ PushFrameRelativeKeepCursor()

void tram::GUI::PushFrameRelativeKeepCursor ( uint32_t  orientation,
uint32_t  offset,
bool  keep_x,
bool  keep_y 
)

Pushes a frame, but keeps the cursor in place.

Identical to PushFrameRelative(), except this function will not reset the cursor to the top-left corner of the frame.

◆ RadioButton()

bool tram::GUI::RadioButton ( uint32_t  index,
uint32_t &  selected,
const char *  text,
bool  enabled 
)

Draws a radio button.

Parameters
indexIndex of this button.
selectedIndex of the selected button.
textText next to the button.
enableIf set to true, the button won't be clickable
Returns
True if the button was clicked on. The new selected button index will be written to the selected parameter.

◆ RegisterFont()

font_t tram::GUI::RegisterFont ( Render::Sprite sprite)

Registers a font.

Returns
Font handle that can be used with all of the GUI widget functions.

◆ RestoreColor()

void tram::GUI::RestoreColor ( GlyphType  type)

Restores previous glyph color.

See SetColor().

◆ RestoreFont()

void tram::GUI::RestoreFont ( GlyphType  type)

Restores previous glyph font.

See SetFont().

◆ SetColor()

void tram::GUI::SetColor ( Render::color_t  color,
GlyphType  type 
)

Sets the color.

Pushes color to color stack. Each glyph type gets its own color stack. The previously set color can be restored using RestoreColor(). GlyphType::TEXT will set the text rendering color of the Text() function, as well as the text that is drawn by the RadioButton() and CheckBox() functions, on top of all Button() buttons and in TextBox() textboxes. GlyphType::WIDGET will set the color that will be used when drawing non-text widget glyphs. By default it is Render::COLOR_WHITE. This stack is reset every time the GUI::Begin() function is called. The default values can be overriden with SetGlyphDefaults().

Parameters
colorRGB color that will be set. Take a look at the Render system's header or docs to see what kinds of predefined colors are available.
typeThe type of the glyph that this color will be applied to.

◆ SetCursorDelayed()

void tram::GUI::SetCursorDelayed ( UI::CursorType  cursor)

Sets the cursor.

The difference between using this function and the cursor setting function in the UI system, is that this function will only switch the cursor if it has been changed, i.e. it will issue the UI::SetCursor() command only if this function receives a new parameter. This means that GUI widgets can change the cursor, but the it is also possible to change it using the UI function and this function will not override it, except if the cursor is placed over a widget.

◆ SetFont()

void tram::GUI::SetFont ( font_t  font,
GlyphType  type 
)

Sets the font.

Works the same as SetColor(), except for fonts. A font needs to be registered with RegisterFont() first. Widget fonts need to use the widget font layout to be displayed properly.

◆ SetGlyph()

void tram::GUI::SetGlyph ( float  x,
float  y,
float  z,
float  w,
float  h,
float  tex_x,
float  tex_y,
float  tex_w,
float  tex_h,
const vec3 color,
font_t  font 
)

Adds a glyph to rendering list.

Triangularizes a glyph from its params and then it get sent off to rendering via the glyph rendering list.

◆ SetGlyphDefaults()

void tram::GUI::SetGlyphDefaults ( Render::color_t  color,
font_t  font,
GlyphType  type 
)

Overrides glyph default colors and fonts.

Whenever GUI::Begin() is called, the color and font passed to this function will be set as the color and font used for the glyph drawing functions. If you are using the Ext::Menu extension and its default fonts, you don't need to call this function.

◆ SetScaling()

void tram::GUI::SetScaling ( uint32_t  scale)

Sets the scaling factor.

If set to 1, the GUI will be drawn normally. If set to 2, then everything will be drawn twice as large. If set to 3, then everything will be drawn three times as large. I don't recommend going above that.

◆ SetSelectedText()

void tram::GUI::SetSelectedText ( char *  text)

Sets the text which is selected in a textbox.

The way the framework remembers which TextBox you have clicked on and are editing is that once a TextBox is clicked on, the framework remembers the pointer of the text that is being edited. The actual TextBox string is updated only when the TextBox() function is called. If you want to deselect any TextBox, call this function with a nullptr.

◆ Slider()

bool tram::GUI::Slider ( float &  value,
bool  enabled,
uint32_t  width 
)

Draws a horizontal slider.

Parameters
valueValue of the slider, between 0.0f and 1.0f.
enabledIf set to false, the slider won't be draggable.
widthWidth of the slider, in pixels.
Returns
True if the slider was clicked on. The slider's new value will be written out to the value variable.

◆ Text()

void tram::GUI::Text ( const char *  text,
uint32_t  orientation 
)

Draws text on the screen.

Parameters
orientationEither TEXT_LEFT, TEXT_CENTER, TEXT_RIGHT or TEXT_JUSTIFIED. The default is TEXT_LEFT.
textThe text that will drawn to screen.

◆ TextBox() [1/2]

bool tram::GUI::TextBox ( char *  text,
uint32_t  length,
bool  enabled,
uint32_t  w,
uint32_t  h 
)

Draws a textbox.

Parameters
textText in the textbox. This will be editable.
lengthLength of the text buffer.
enabledIf false, the textbox won't be editable.
w,hDimensions of the textbox, in pixels.

◆ TextBox() [2/2]

void tram::GUI::TextBox ( const char *  text,
uint32_t  w,
uint32_t  h 
)

Draws a textbox.

Essentially identical to the other TextBox() function, except this one accepts a const char* text. This also means that the text in the textbox won't be editable and it will be drawn as disabled.

◆ TextWidth()

uint32_t tram::GUI::TextWidth ( font_t  font,
const char *  text 
)

◆ Update()

void tram::GUI::Update ( )

Submits all of the glyphs for rendering.

Also updates cursor, if needed by SetCursorDelayed().

◆ UpdateDrawListFonts()

void tram::GUI::UpdateDrawListFonts ( )

Submits registered fonts to the renderer.

Variable Documentation

◆ beginned

bool tram::GUI::beginned = false

◆ last_update_frame

uint32_t tram::GUI::last_update_frame = 0

◆ mouse_click_not_handled

bool tram::GUI::mouse_click_not_handled = true

◆ mouse_click_not_handled_late

bool tram::GUI::mouse_click_not_handled_late = true