KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
KTech::UI Class Reference

World structure that contains and renders Widgets. More...

#include <ui.hpp>

Public Member Functions

 UI (Engine &engine, UPoint resolution=UPoint(10, 10), std::string name="")
 Construct a UI.
 
virtual ~UI ()
 Remove all Widgets from itself, and itself from Memory.
 
auto AddWidget (const ID< Widget > &widget) -> bool
 Add a Widget.
 
auto RemoveWidget (const ID< Widget > &widget) -> bool
 Remove a Widget.
 
auto RemoveAllWidgets () -> bool
 Remove all contained Widget.
 
void Resize (UPoint resolution)
 Resize the image's resolution.
 
void Render ()
 Render all contained Widgets.
 
void Draw (Point position=Point(0, 0), UPoint start=UPoint(0, 0), UPoint end=UPoint(0, 0), uint8_t alpha=std::numeric_limits< uint8_t >::max())
 Draw the rendered image (UI::m_image) to Output so it can be printed to the terminal.
 
void RenderClearDrawPrint ()
 Shortcut for UI::Render(), Output::Clear(), UI::Draw() and Output::Print().
 

Public Attributes

Engineengine
 Parent Engine.
 
const ID< UIm_id {ID<UI>::Unique()}
 Personal ID.
 
std::string m_name
 String name.
 
std::vector< ID< Widget > > m_widgets
 Contained Widgets.
 
UPoint m_res
 Image's resolution.
 
CellA m_background = CellA(' ', RGBA(0, 0, 0, 0), RGBA(0, 0, 0, 0))
 The background to render upon.
 
uint8_t m_alpha = std::numeric_limits<uint8_t>::max()
 Opacity for all rendered Widgets.
 
RGBA m_frgba = RGBAColors::transparent
 Foreground color added after rendering Widgets.
 
RGBA m_brgba = RGBAColors::transparent
 Background color added after rendering Widgets.
 
std::vector< CellAm_image
 CellA-based rendered image.
 

Protected Member Functions

virtual auto OnTick () -> bool
 Virtual function called once each tick.
 

Friends

class KTech::Memory
 

Detailed Description

World structure that contains and renders Widgets.

This class is like a combined Layer and Camera, but for Widgets.

Unlike how Objects are rendered, rendering Widgets doesn't involve multiple "layers" (UI is a single layer itself).

Also, UI's image is CellA-based in contrary to Camera's Cell-based image. The alpha channels maintain the total opacity of the rendered image. This allows UI's image to be drawn like a HUD on top of Camera's image in Output::Draw().

Constructor & Destructor Documentation

◆ UI()

KTech::UI::UI ( Engine & engine,
UPoint resolution = UPoint(10, 10),
std::string name = "" )

Construct a UI.

Parameters
engineParent Engine.
resolutionImage's resolution.
nameString name.

Member Function Documentation

◆ AddWidget()

auto KTech::UI::AddWidget ( const ID< Widget > & widget) -> bool

Add a Widget.

Parameters
widgetThe Widget to add.
Returns
true if added Widget. false if Widget doesn't exist in Memory or already contained by this UI.

◆ Draw()

void KTech::UI::Draw ( Point position = Point(0, 0),
UPoint start = UPoint(0, 0),
UPoint end = UPoint(0, 0),
uint8_t alpha = std::numeric_limits<uint8_t>::max() )

Draw the rendered image (UI::m_image) to Output so it can be printed to the terminal.

This function redirects to Output::Draw() and passes the given parameters verbatim.

See also
Output::Draw() for parameters explanation.

◆ OnTick()

auto KTech::UI::OnTick ( ) -> bool
protectedvirtual

Virtual function called once each tick.

You can override this in your inherited class to add whatever functionality you want.

Called by Memory::CallOnTicks().

Returns
bool value, which is explained in Output::ShouldRenderThisTick().
See also
Memory::CallOnTicks()
Output::ShouldRenderThisTick()

◆ RemoveAllWidgets()

auto KTech::UI::RemoveAllWidgets ( ) -> bool

Remove all contained Widget.

Returns
true if removed all Widgets. false there are no Widgets in this UI.

◆ RemoveWidget()

auto KTech::UI::RemoveWidget ( const ID< Widget > & widget) -> bool

Remove a Widget.

Parameters
widgetThe Widget to remove.
Returns
true if removed Widget. false if Widget is not contained by this UI.

◆ RenderClearDrawPrint()

void KTech::UI::RenderClearDrawPrint ( )

Shortcut for UI::Render(), Output::Clear(), UI::Draw() and Output::Print().

This function calls the above functions with respect to "render on demand" (by checking Output::ShouldRenderThisTick() and Output::ShouldPrintThisTick()). So, you can use this function in your game loop to avoid boilerplate code while still maintaining good performance, unless you want more functionality in your graphics portion of your game loop. This function is especially convenient for testing in no-game-loop mode.

See also
UI::Render()
Output::Clear()
UI::Draw()
Output::Print()
Engine::noGameLoopMode

◆ Resize()

void KTech::UI::Resize ( UPoint resolution)

Resize the image's resolution.

Parameters
resolutionThe new size of the image.

The documentation for this class was generated from the following files: