Gamedev Framework (gf)  0.2.0
A C++11 framework for 2D games
Classes | Public Member Functions | List of all members
gf::UI Class Reference

An immediate mode user interface class. More...

#include <gf/UI.h>

Inheritance diagram for gf::UI:
Inheritance graph
[legend]

Public Member Functions

 UI (const UIRenderer &renderer, const UILayout &layout)
 Constructor. More...
 
void update (const Event &event)
 Update the internal state with an event. More...
 
void clear ()
 Clear the internal state for this frame. More...
 
bool beginScrollArea (const std::string &name, const RectF &area, float *scroll)
 Define the main widget. More...
 
void endScrollArea ()
 End the main widget. More...
 
void indent ()
 Increase the indent size. More...
 
void unindent ()
 Decrease the indent size. More...
 
void separator ()
 Add an invisible separator. More...
 
void separatorLine ()
 Add a separator line. More...
 
bool button (const std::string &text, bool enabled=true)
 Add a button. More...
 
bool item (const std::string &text, bool enabled=true)
 Add an item. More...
 
bool check (const std::string &text, bool checked, bool enabled=true)
 Add a checkbox. More...
 
bool collapse (const std::string &text, bool checked, bool enabled=true)
 Add a disclosure widget. More...
 
void label (const std::string &text)
 Add a label. More...
 
void value (const std::string &text)
 Add a value. More...
 
bool slider (const std::string &text, float *val, float vmin, float vmax, float vinc, bool enabled=true)
 Add a slider. More...
 
bool cycle (const std::vector< std::string > &choices, std::size_t choice, bool enabled=true)
 Add a cycle. More...
 
virtual void draw (RenderTarget &target, RenderStates states) override
 Draw the object to a render target. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 

Detailed Description

An immediate mode user interface class.

// initialisation
gf::DefaultUIRenderer uiRenderer(font);
gf::UILayout uiLayout;
gf::UI ui(uiRenderer, uiLayout);
// state
bool checked1 = false;
bool checked2 = false;
bool checked3 = true;
bool checked4 = false;
float scrollArea = 0;
float value1 = 50.0f;
float value2 = 30.0f;
std::vector<std::string> choices = { "First Choice", "Next Choice", "Last Choice" };
std::size_t choice = 0;
// ...
// in the main loop
ui.beginScrollArea("Scroll area", { 10.0f, 10.0f, 200.0f, 400.0f }, &scrollArea);
ui.separatorLine();
ui.separator();
ui.button("Button");
ui.button("Disabled Button", false);
ui.item("Item");
ui.item("Disabled item", false);
if (ui.check("Checkbox", checked1)) {
checked1 = !checked1;
}
if (ui.check("Disabled checkbox", checked2, false)) {
checked2 = !checked2;
}
if (ui.collapse("Collapse", checked3)) {
checked3 = !checked3;
}
if (checked3) {
ui.indent();
ui.label("Collapsible element");
ui.unindent();
}
if (ui.collapse("Disabled collapse", checked4, false)) {
checked4 = !checked4;
}
ui.label("Label");
ui.value("Value");
ui.slider("Slider", &value1, 0.0f, 100.0f, 1.0f);
ui.slider("Disabled slider", &value2, 0.0f, 100.0f, 1.0f, false);
ui.indent();
ui.label("Indented");
ui.unindent();
ui.label("Unindented");
if (ui.cycle(choices, choice)) {
choice = (choice + 1) % choices.size();
}
ui.endScrollArea();

The result of the previous code is given in the following figure.

ui.png
UI example
See also
gf::UIRenderer, gf::UILayout, gf::UIFlags, gf::UIIcon

Constructor & Destructor Documentation

gf::UI::UI ( const UIRenderer renderer,
const UILayout layout 
)

Constructor.

Parameters
rendererA renderer for user interface
layoutA layout for user interface

Member Function Documentation

bool gf::UI::beginScrollArea ( const std::string &  name,
const RectF area,
float *  scroll 
)

Define the main widget.

A scroll area is the main widget. It is the container for the other widgets. It may have a scrollbar on the right side if the widgets are too high.

Parameters
nameThe name of the scroll area
areaThe area of the scroll area
scrollA pointer to the value of the scroll
See also
endScrollArea()
Scrollbar - Wikipedia
bool gf::UI::button ( const std::string &  text,
bool  enabled = true 
)

Add a button.

Parameters
textThe text on the button
enabledTrue if the button can be pressed
Returns
True if the button has been activated
See also
Button - Wikipedia
bool gf::UI::check ( const std::string &  text,
bool  checked,
bool  enabled = true 
)

Add a checkbox.

Parameters
textThe text of the checkbox
checkedTrue if the checkbox is checked
enabledTrue if the checkbox can be checked
Returns
True if the checkbox has been activated
See also
Checkbox - Wikipedia
void gf::UI::clear ( )

Clear the internal state for this frame.

This function must be called at the beginning of every frame, before any other call to any function.

bool gf::UI::collapse ( const std::string &  text,
bool  checked,
bool  enabled = true 
)

Add a disclosure widget.

Parameters
textThe text of the widget
checkedTrue if the widget is expanded
enabledTrue if the widget if enabled
Returns
True if the widget has been activated
See also
Disclosure widget - Wikipedia
bool gf::UI::cycle ( const std::vector< std::string > &  choices,
std::size_t  choice,
bool  enabled = true 
)

Add a cycle.

Parameters
choicesThe possible values of the cycles
choiceThe current value of the cycle
enabledTrue if the cycle is enabled
Returns
True if the cycle has been activated
See also
Cycle button - Wikipedia
virtual void gf::UI::draw ( RenderTarget target,
RenderStates  states 
)
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
targetRender target to draw to
statesCurrent render states

Implements gf::Drawable.

void gf::UI::endScrollArea ( )

End the main widget.

See also
beginScrollArea()
void gf::UI::indent ( )

Increase the indent size.

After this call, all the widgets will be indented. It is possible to call this function multiple times.

See also
unindent()
bool gf::UI::item ( const std::string &  text,
bool  enabled = true 
)

Add an item.

Parameters
textThe text on the item
enabledTrue if the item can be pressed
Returns
True if the item has been activated
void gf::UI::label ( const std::string &  text)

Add a label.

A label is a left aligned text

Parameters
textThe text of the label
See also
value()
Label - Wikipedia
void gf::UI::separator ( )

Add an invisible separator.

void gf::UI::separatorLine ( )

Add a separator line.

bool gf::UI::slider ( const std::string &  text,
float *  val,
float  vmin,
float  vmax,
float  vinc,
bool  enabled = true 
)

Add a slider.

Parameters
textThe text of the slider
valA pointer to the value of the slider
vminThe minimum value of the slider
vmaxThe maximum value of the slider
vincThe increment of the slider
enabledTrue if the slider is enabled
Returns
True if the slider has been activated
See also
Slider - Wikipedia
void gf::UI::unindent ( )

Decrease the indent size.

After this call, all the widgets will be unindented. It is possible to call this function multiple times.

See also
indent()
void gf::UI::update ( const Event event)

Update the internal state with an event.

This function must be called for every event that occur in a frame.

Parameters
eventAn event
void gf::UI::value ( const std::string &  text)

Add a value.

A value is a right aligned text

Parameters
textThe text of the value
See also
label()