VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::VexUI Class Reference

Class defining VexUI, it initializes the UI system, loads, renders, converts ui data, essentially managing ui from file to reneering. More...

#include <VexUI.hpp>

Public Member Functions

 VexUI (VulkanContext &ctx, VirtualFileSystem *vfs, VulkanResources *res, ResolutionManager *resMgr)
 Constructor for VexUI.
bool init ()
 Initialize the UI system, components needed later on.
void load (const std::string &path)
 Load UI data from a JSON file.
void render (VkCommandBuffer cmd, VkPipeline pipeline, VkPipelineLayout pipelineLayout, int currentFrame)
 Render the UI. It is called by main rendering function.
void processEvent (const SDL_Event &ev)
 Process mouse and keyboard events.
WidgetgetFocusedWidget () const
 Get the currently focused widget.
void setText (const std::string &id, const std::string &txt)
 Set text of a UI element.
void setOnClick (const std::string &id, std::function< void()> cb)
 Set on-click callback of a UI element.
WidgetgetWidget (const std::string &id)
 Get a UI element.
UIStylegetStyle (const std::string &id)
 Get a UI element's style.
void setStyle (const std::string &id, const UIStyle &style)
 Set a UI element's style.
void setRotation (const std::string &id, float degrees)
 Set rotation of a widget in degrees.
void setPosition (const std::string &id, UIUnitValue x, UIUnitValue y)
 Move a widget (sets Left/Top yoga properties). Works best with position: absolute, or relative offsets.
void setSize (const std::string &id, UIUnitValue w, UIUnitValue h)
 Resize a widget.
void setImage (const std::string &id, const std::string &path)
 Change the image of an image widget.
void setFont (const std::string &id, const std::string &fontPath, UIUnitValue fontSize)
 Change font properties.
void setColor (const std::string &id, glm::vec4 color)
 Set text/tint color.
void setBackgroundColor (const std::string &id, glm::vec4 color)
 Set background color.
void setBorder (const std::string &id, UIUnitValue width, glm::vec4 color)
 Set border properties.
bool isInitialized ()
 Check if the UI system is initialized.
int getZIndex ()
 Get the current z-index of the UI system.
void setZIndex (int zIndex)
 Set the z-index of the UI system.
void update (float deltaTime=0.016f)
 Update the UI system.
float getPspMultiplier () const
glm::uvec2 getRenderResolution () const

Private Member Functions

void loadFonts (Widget *w)
 Loads fonts for the UI.
void loadImages (Widget *w)
 Loads images for the UI.
void layout (glm::uvec2 res)
 Layouts the UI using yoga.
void batch (Widget *w, std::vector< float > &verts, glm::vec2 parentOffset={0.f, 0.f})
 Batches the UI for rendering.
void uploadVerts (const std::vector< float > &verts)
 Uploads the vertex buffer to the GPU.
WidgetparseNode (const nlohmann::json &j)
 Parses a node from json to widgets.
void freeTree (Widget *w)
 Frees the widget tree.
WidgetfindWidgetAt (Widget *w, glm::vec2 pos, glm::vec2 parentOffset={0.f, 0.f})
 Finds a widget at a position.
WidgetfindById (Widget *w, const std::string &id)
 Finds a widget by its id.
std::vector< std::string > wrapText (const std::string &text, const FontAtlas &a, float maxWidth)
 Wraps text to fit within a given width.
glm::vec2 calculateTextSize (Widget *w, float maxWidth=FLT_MAX)
 Calculates the size of the text.
void safeUpdate (const std::string &id, std::function< void(Widget *)> action)
 Updates a widget safely.
void navigateToWidget (float dirX, float dirY)
 Navigate to widget in a given direction.
void getNavigableWidgets (std::vector< Widget * > &out)
 Get all navigable widgets (buttons).
void collectNavigableWidgets (Widget *w, std::vector< Widget * > &out)
 Recursively collect navigable widgets.
bool isWidgetNavigable (Widget *w) const
 Check if widget is navigable (button type).
glm::vec2 getWidgetCenter (Widget *w)
 Get widget center position in screen space.
WidgetfindClosestNavigableWidget (const glm::vec2 &fromPos, const glm::vec2 &direction, const std::vector< Widget * > &candidates)
 Find closest navigable widget in a direction.
void setFocusedWidget (Widget *w)
 Set focus to a widget and trigger focus callbacks.

Static Private Member Functions

static void measureTextNode (LayoutNode *node, float width, float height)
 Measures a text node.

Private Attributes

VulkanContextm_ctx
VirtualFileSystemm_vfs
VulkanResourcesm_res
ResolutionManagerm_resMgr
Widgetm_root = nullptr
bool initialized = false
int zIndex = 0
glm::uvec2 m_lastRenderRes {0, 0}
float m_lastPspMult = 0.f
std::vector< std::function< void()> > pendingSetters
bool loadPending = false
std::string loadPath = ""
std::unordered_map< std::string, FontAtlasm_fontAtlases
VkBuffer m_vb = VK_NULL_HANDLE
VmaAllocation m_vbAlloc = VK_NULL_HANDLE
size_t m_vbSize = 0
VkSampler m_uiSampler = VK_NULL_HANDLE
Widgetm_focusedWidget = nullptr
float m_gamepadAxisX = 0.0f
float m_gamepadAxisY = 0.0f
Widgetm_previousFocusedWidget = nullptr
float m_gamepadNavigationCooldown = 0.0f

Static Private Attributes

static constexpr float GAMEPAD_NAV_COOLDOWN = 0.2f
static constexpr float GAMEPAD_AXIS_THRESHOLD = 0.5f

Detailed Description

Class defining VexUI, it initializes the UI system, loads, renders, converts ui data, essentially managing ui from file to reneering.

Definition at line 139 of file VexUI.hpp.

Constructor & Destructor Documentation

◆ VexUI()

vex::VexUI::VexUI ( VulkanContext & ctx,
VirtualFileSystem * vfs,
VulkanResources * res,
ResolutionManager * resMgr )

Constructor for VexUI.

Parameters
VulkanContext&ctx - Vulkan context.
VirtualFileSystem*vfs - Virtual file system.
VulkanResources*res -Vulkan resources.
ResolutionManager*resMgr - Resolution manager.

Definition at line 462 of file VexUI.cpp.

◆ ~VexUI()

vex::VexUI::~VexUI ( )

Definition at line 465 of file VexUI.cpp.

Member Function Documentation

◆ batch()

void vex::VexUI::batch ( Widget * w,
std::vector< float > & verts,
glm::vec2 parentOffset = {0.f, 0.f} )
private

Batches the UI for rendering.

Parameters
Widget*w The widget to batch.
std::vector<float>&verts The vertex buffer to batch into.
glm::vec2parentOffset The offset of the parent widget.

Definition at line 851 of file VexUI.cpp.

◆ calculateTextSize()

glm::vec2 vex::VexUI::calculateTextSize ( Widget * w,
float maxWidth = FLT_MAX )
private

Calculates the size of the text.

Parameters
Widget*w The widget to calculate the size of.
floatmaxWidth The maximum width of the text.
Returns
The size of the text.

Definition at line 816 of file VexUI.cpp.

◆ collectNavigableWidgets()

void vex::VexUI::collectNavigableWidgets ( Widget * w,
std::vector< Widget * > & out )
private

Recursively collect navigable widgets.

Definition at line 754 of file VexUI.cpp.

◆ findById()

Widget * vex::VexUI::findById ( Widget * w,
const std::string & id )
private

Finds a widget by its id.

Parameters
Widget*w The widget to search.
conststd::string& id The id to search for.
Returns
The widget with the id.

Definition at line 673 of file VexUI.cpp.

◆ findClosestNavigableWidget()

Widget * vex::VexUI::findClosestNavigableWidget ( const glm::vec2 & fromPos,
const glm::vec2 & direction,
const std::vector< Widget * > & candidates )
private

Find closest navigable widget in a direction.

Definition at line 782 of file VexUI.cpp.

◆ findWidgetAt()

Widget * vex::VexUI::findWidgetAt ( Widget * w,
glm::vec2 pos,
glm::vec2 parentOffset = {0.f, 0.f} )
private

Finds a widget at a position.

Parameters
Widget*w The widget to search.
glm::vec2pos The position to search at.
glm::vec2parentOffset The offset of the parent widget.
Returns
The widget at the position.

Definition at line 647 of file VexUI.cpp.

◆ freeTree()

void vex::VexUI::freeTree ( Widget * w)
private

Frees the widget tree.

Parameters
Widget*w The widget to free.

Definition at line 1028 of file VexUI.cpp.

◆ getFocusedWidget()

Widget * vex::VexUI::getFocusedWidget ( ) const
inline

Get the currently focused widget.

Returns
Widget* - Pointer to focused widget, or nullptr if none focused.

Definition at line 166 of file VexUI.hpp.

◆ getNavigableWidgets()

void vex::VexUI::getNavigableWidgets ( std::vector< Widget * > & out)
private

Get all navigable widgets (buttons).

Definition at line 752 of file VexUI.cpp.

◆ getPspMultiplier()

float vex::VexUI::getPspMultiplier ( ) const

Definition at line 493 of file VexUI.cpp.

◆ getRenderResolution()

glm::uvec2 vex::VexUI::getRenderResolution ( ) const
inline

Definition at line 283 of file VexUI.hpp.

◆ getStyle()

UIStyle * vex::VexUI::getStyle ( const std::string & id)
inline

Get a UI element's style.

Parameters
conststd::string& id - ID of the UI element.
Returns
UIStyle* - Pointer to the UI element's style.

Definition at line 190 of file VexUI.hpp.

◆ getWidget()

Widget * vex::VexUI::getWidget ( const std::string & id)
inline

Get a UI element.

Parameters
conststd::string& id - ID of the UI element.
Returns
Widget* - Pointer to the UI element.

Definition at line 181 of file VexUI.hpp.

◆ getWidgetCenter()

glm::vec2 vex::VexUI::getWidgetCenter ( Widget * w)
private

Get widget center position in screen space.

Definition at line 765 of file VexUI.cpp.

◆ getZIndex()

int vex::VexUI::getZIndex ( )
inline

Get the current z-index of the UI system.

Returns
int - Current z-index.

Definition at line 259 of file VexUI.hpp.

◆ init()

bool vex::VexUI::init ( )

Initialize the UI system, components needed later on.

Definition at line 475 of file VexUI.cpp.

◆ isInitialized()

bool vex::VexUI::isInitialized ( )
inline

Check if the UI system is initialized.

Returns
bool - True if initialized, false otherwise.

Definition at line 255 of file VexUI.hpp.

◆ isWidgetNavigable()

bool vex::VexUI::isWidgetNavigable ( Widget * w) const
private

Check if widget is navigable (button type).

Definition at line 760 of file VexUI.cpp.

◆ layout()

void vex::VexUI::layout ( glm::uvec2 res)
private

Layouts the UI using yoga.

Parameters
glm::uvec2res The resolution of the UI.

Definition at line 643 of file VexUI.cpp.

◆ load()

void vex::VexUI::load ( const std::string & path)

Load UI data from a JSON file.

Parameters
conststd::string& path - Path to the JSON file.

Definition at line 608 of file VexUI.cpp.

◆ loadFonts()

void vex::VexUI::loadFonts ( Widget * w)
private

Loads fonts for the UI.

Parameters
Widget*w The widget to load fonts for.

Definition at line 514 of file VexUI.cpp.

◆ loadImages()

void vex::VexUI::loadImages ( Widget * w)
private

Loads images for the UI.

Parameters
Widget*w The widget to load images for.

Definition at line 567 of file VexUI.cpp.

◆ measureTextNode()

void vex::VexUI::measureTextNode ( LayoutNode * node,
float width,
float height )
staticprivate

Measures a text node.

Parameters
LayoutNode*node The node to measure.
floatwidth The width of the node.
floatheight The height of the node.

Definition at line 840 of file VexUI.cpp.

◆ navigateToWidget()

void vex::VexUI::navigateToWidget ( float dirX,
float dirY )
private

Navigate to widget in a given direction.

Parameters
floatdirX Direction X component (-1, 0, or 1).
floatdirY Direction Y component (-1, 0, or 1).

Definition at line 732 of file VexUI.cpp.

◆ parseNode()

Widget * vex::VexUI::parseNode ( const nlohmann::json & j)
private

Parses a node from json to widgets.

Parameters
constnlohmann::json& j The json node to parse.
Returns
The parsed widget.

Definition at line 578 of file VexUI.cpp.

◆ processEvent()

void vex::VexUI::processEvent ( const SDL_Event & ev)

Process mouse and keyboard events.

Parameters
constSDL_Event& ev - SDL event.

Definition at line 695 of file VexUI.cpp.

◆ render()

void vex::VexUI::render ( VkCommandBuffer cmd,
VkPipeline pipeline,
VkPipelineLayout pipelineLayout,
int currentFrame )

Render the UI. It is called by main rendering function.

Parameters
VkCommandBuffercmd - Command buffer.
VkPipelinepipeline - Pipeline.
VkPipelineLayoutpipelineLayout - Pipeline layout.
intcurrentFrame - Current frame.

Definition at line 954 of file VexUI.cpp.

◆ safeUpdate()

void vex::VexUI::safeUpdate ( const std::string & id,
std::function< void(Widget *)> action )
private

Updates a widget safely.

Parameters
conststd::string& id The id of the widget to update.
std::function<void(Widget*)>action The action to perform on the widget.

Definition at line 500 of file VexUI.cpp.

◆ setBackgroundColor()

void vex::VexUI::setBackgroundColor ( const std::string & id,
glm::vec4 color )

Set background color.

Parameters
conststd::string& id The id of the widget to change the background color of.
glm::vec4color The new background color.

Definition at line 1081 of file VexUI.cpp.

◆ setBorder()

void vex::VexUI::setBorder ( const std::string & id,
UIUnitValue width,
glm::vec4 color )

Set border properties.

Parameters
conststd::string& id The id of the widget to change the border of.
UIUnitValuewidth The new border width.
glm::vec4color The new border color.

Definition at line 1088 of file VexUI.cpp.

◆ setColor()

void vex::VexUI::setColor ( const std::string & id,
glm::vec4 color )

Set text/tint color.

Parameters
conststd::string& id The id of the widget to change the color of.
glm::vec4color The new color.

Definition at line 1074 of file VexUI.cpp.

◆ setFocusedWidget()

void vex::VexUI::setFocusedWidget ( Widget * w)
private

Set focus to a widget and trigger focus callbacks.

Parameters
Widget*w - Widget to focus.

Definition at line 808 of file VexUI.cpp.

◆ setFont()

void vex::VexUI::setFont ( const std::string & id,
const std::string & fontPath,
UIUnitValue fontSize )

Change font properties.

Parameters
conststd::string& id The id of the widget to change the font of.
conststd::string& fontPath The path to the new font.
UIUnitValuefontSize The new font size.

Definition at line 1065 of file VexUI.cpp.

◆ setImage()

void vex::VexUI::setImage ( const std::string & id,
const std::string & path )

Change the image of an image widget.

Parameters
conststd::string& id The id of the widget to change the image of.
conststd::string& path The path to the new image.

Definition at line 1057 of file VexUI.cpp.

◆ setOnClick()

void vex::VexUI::setOnClick ( const std::string & id,
std::function< void()> cb )

Set on-click callback of a UI element.

Parameters
conststd::string& id - ID of the UI element.
std::function<void()>cb - Callback function.

Definition at line 689 of file VexUI.cpp.

◆ setPosition()

void vex::VexUI::setPosition ( const std::string & id,
UIUnitValue x,
UIUnitValue y )

Move a widget (sets Left/Top yoga properties). Works best with position: absolute, or relative offsets.

Parameters
conststd::string& id The id of widget to Move
UIUnitValuex The x-coordinate of the widget's new position.
UIUnitValuey The y-coordinate of the widget's new position.

Definition at line 1041 of file VexUI.cpp.

◆ setRotation()

void vex::VexUI::setRotation ( const std::string & id,
float degrees )

Set rotation of a widget in degrees.

Parameters
conststd::string& id The id of the widget to rotate.
floatdegrees The rotation angle in degrees.

Definition at line 1034 of file VexUI.cpp.

◆ setSize()

void vex::VexUI::setSize ( const std::string & id,
UIUnitValue w,
UIUnitValue h )

Resize a widget.

Parameters
conststd::string& id The id of the widget to resize.
UIUnitValuew The new width of the widget.
UIUnitValueh The new height of the widget.

Definition at line 1050 of file VexUI.cpp.

◆ setStyle()

void vex::VexUI::setStyle ( const std::string & id,
const UIStyle & style )
inline

Set a UI element's style.

Parameters
conststd::string& id - ID of the UI element.
constUIStyle& style - New style for the UI element.

Definition at line 199 of file VexUI.hpp.

◆ setText()

void vex::VexUI::setText ( const std::string & id,
const std::string & txt )

Set text of a UI element.

Parameters
conststd::string& id - ID of the UI element.
conststd::string& txt - Text to set.

Definition at line 680 of file VexUI.cpp.

◆ setZIndex()

void vex::VexUI::setZIndex ( int zIndex)
inline

Set the z-index of the UI system.

Parameters
zIndex- New z-index value.

Definition at line 263 of file VexUI.hpp.

◆ update()

void vex::VexUI::update ( float deltaTime = 0.016f)
inline

Update the UI system.

This function should not be called directly. Its called every frame by the engine right before rendering to update pending operations.

Definition at line 270 of file VexUI.hpp.

◆ uploadVerts()

void vex::VexUI::uploadVerts ( const std::vector< float > & verts)
private

Uploads the vertex buffer to the GPU.

Parameters
conststd::vector<float>& verts The vertex buffer to upload.

Definition at line 1020 of file VexUI.cpp.

◆ wrapText()

std::vector< std::string > vex::VexUI::wrapText ( const std::string & text,
const FontAtlas & a,
float maxWidth )
private

Wraps text to fit within a given width.

Parameters
conststd::string& text The text to wrap.
constFontAtlas& a The font atlas to use.
floatmaxWidth The maximum width of the text.
Returns
A vector of wrapped lines of text.

Definition at line 209 of file VexUI.cpp.

Member Data Documentation

◆ GAMEPAD_AXIS_THRESHOLD

float vex::VexUI::GAMEPAD_AXIS_THRESHOLD = 0.5f
staticconstexprprivate

Definition at line 316 of file VexUI.hpp.

◆ GAMEPAD_NAV_COOLDOWN

float vex::VexUI::GAMEPAD_NAV_COOLDOWN = 0.2f
staticconstexprprivate

Definition at line 315 of file VexUI.hpp.

◆ initialized

bool vex::VexUI::initialized = false
private

Definition at line 292 of file VexUI.hpp.

◆ loadPath

std::string vex::VexUI::loadPath = ""
private

Definition at line 301 of file VexUI.hpp.

◆ loadPending

bool vex::VexUI::loadPending = false
private

Definition at line 300 of file VexUI.hpp.

◆ m_ctx

VulkanContext& vex::VexUI::m_ctx
private

Definition at line 286 of file VexUI.hpp.

◆ m_focusedWidget

Widget* vex::VexUI::m_focusedWidget = nullptr
private

Definition at line 310 of file VexUI.hpp.

◆ m_fontAtlases

std::unordered_map<std::string, FontAtlas> vex::VexUI::m_fontAtlases
private

Definition at line 303 of file VexUI.hpp.

◆ m_gamepadAxisX

float vex::VexUI::m_gamepadAxisX = 0.0f
private

Definition at line 311 of file VexUI.hpp.

◆ m_gamepadAxisY

float vex::VexUI::m_gamepadAxisY = 0.0f
private

Definition at line 312 of file VexUI.hpp.

◆ m_gamepadNavigationCooldown

float vex::VexUI::m_gamepadNavigationCooldown = 0.0f
private

Definition at line 314 of file VexUI.hpp.

◆ m_lastPspMult

float vex::VexUI::m_lastPspMult = 0.f
private

Definition at line 296 of file VexUI.hpp.

◆ m_lastRenderRes

glm::uvec2 vex::VexUI::m_lastRenderRes {0, 0}
private

Definition at line 295 of file VexUI.hpp.

◆ m_previousFocusedWidget

Widget* vex::VexUI::m_previousFocusedWidget = nullptr
private

Definition at line 313 of file VexUI.hpp.

◆ m_res

VulkanResources* vex::VexUI::m_res
private

Definition at line 288 of file VexUI.hpp.

◆ m_resMgr

ResolutionManager* vex::VexUI::m_resMgr
private

Definition at line 289 of file VexUI.hpp.

◆ m_root

Widget* vex::VexUI::m_root = nullptr
private

Definition at line 291 of file VexUI.hpp.

◆ m_uiSampler

VkSampler vex::VexUI::m_uiSampler = VK_NULL_HANDLE
private

Definition at line 308 of file VexUI.hpp.

◆ m_vb

VkBuffer vex::VexUI::m_vb = VK_NULL_HANDLE
private

Definition at line 305 of file VexUI.hpp.

◆ m_vbAlloc

VmaAllocation vex::VexUI::m_vbAlloc = VK_NULL_HANDLE
private

Definition at line 306 of file VexUI.hpp.

◆ m_vbSize

size_t vex::VexUI::m_vbSize = 0
private

Definition at line 307 of file VexUI.hpp.

◆ m_vfs

VirtualFileSystem* vex::VexUI::m_vfs
private

Definition at line 287 of file VexUI.hpp.

◆ pendingSetters

std::vector<std::function<void()> > vex::VexUI::pendingSetters
private

Definition at line 298 of file VexUI.hpp.

◆ zIndex

int vex::VexUI::zIndex = 0
private

Definition at line 293 of file VexUI.hpp.


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