Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | Static Protected Attributes
gui::widget::Widget Class Referenceabstract

#include <widget.hpp>

Inheritance diagram for gui::widget::Widget:
gui::widget::DynText gui::widget::Empty gui::widget::Flexbox gui::widget::StaticImg gui::widget::TextInput

Public Types

using Ptr = std::unique_ptr< Widget >
 

Public Member Functions

 Widget (Type type, glm::vec2 origin)
 Sets the type and origin position of the widget, and assigns a new unique handle. More...
 
virtual ~Widget ()=default
 
void setOrigin (glm::vec2 origin)
 Overrides the current origin position with the newly specified one. More...
 
virtual void lock ()
 Function that is run after all of the widgets have been added to the screee but before any input handling has occurred. More...
 
CallbackHandle addOnClick (Callback callback)
 ====================================================================================== More...
 
CallbackHandle addOnHover (Callback callback)
 Register a new onHover callback function for this widget. More...
 
void removeOnClick (CallbackHandle handle)
 Removes the onClick callback function associated with the given handle. More...
 
void removeOnHover (CallbackHandle handle)
 Removes the onHover callback function associated with the given handle. More...
 
virtual void doClick (float x, float y)
 Performs a click at the specified (x,y) position in the GUI coordinate frame. More...
 
virtual void doHover (float x, float y)
 Performs a hover action at the specified (x,y) position in the GUI coordinate frame. More...
 
virtual void render ()=0
 ====================================================================================== More...
 
Type getType () const
 ====================================================================================== More...
 
const glm::vec2 & getOrigin () const
 Queries the origin position of the widget. More...
 
std::pair< std::size_t, std::size_t > getSize () const
 Queries the size of the widget in pixels. More...
 
Handle getHandle () const
 Queries the handle for this specific widget. More...
 
virtual bool hasHandle (Handle handle) const
 ====================================================================================== More...
 
virtual Widgetborrow (Handle handle)
 Gives a pointer to the subwidget specified by the handle. More...
 

Protected Attributes

Handle handle
 ====================================================================================== More...
 
Type type
 Type of the widget. More...
 
glm::vec2 origin
 Origin position (bottom left) of the widget in GUI coordinates. More...
 
std::size_t width {0}
 Width of the widget, in pixels. More...
 
std::size_t height {0}
 Height of the widget, in pixels. More...
 
std::unordered_map< CallbackHandle, Callbackon_clicks
 all of the onClick handlers, indexable by handle More...
 
std::unordered_map< CallbackHandle, Callbackon_hovers
 all of the onHover handlers, indexable by handle More...
 

Static Protected Attributes

static std::size_t num_widgets = 0
 Static counter for the number of widgets that have been created, so a new identifier can be assigned to each widget as they are created. More...
 

Detailed Description

Abstract base class for any arbitrary widget.

Any Widget implementation must override the following pure virtual functions:

And certain widget implementations may also wish to override these virtual functions

You can see the documentation for these functions for explanations of why you may or may not need to override this functions for a specific derived widget.

In addition, any derived class must also set width and height once these values are known.

Member Typedef Documentation

◆ Ptr

using gui::widget::Widget::Ptr = std::unique_ptr<Widget>

All widgets are passed around and manipulated as unique ptrs so this is a helpful alias to reduce characters typed

Constructor & Destructor Documentation

◆ Widget()

gui::widget::Widget::Widget ( Type  type,
glm::vec2  origin 
)
explicit

Sets the type and origin position of the widget, and assigns a new unique handle.

=<SETUP>==============================================================================

These functions are necessary to setup and position a widget on the screen.

Parameters
typeType of the widget
originOrigin position of the widget (bottom left x,y coordinate in the GUI coordinate frame)

◆ ~Widget()

virtual gui::widget::Widget::~Widget ( )
virtualdefault

Member Function Documentation

◆ addOnClick()

CallbackHandle gui::widget::Widget::addOnClick ( Callback  callback)

======================================================================================

=<ACTION HANDLING>====================================================================

These functions are used to register, remove, and activate action callbacks on widgets

Register a new onClick callback function for this widget.

Parameters
callbackCallback function to run on click
Returns
handle to the callback function, which can be passed into removeOnClick to unregister.

◆ addOnHover()

CallbackHandle gui::widget::Widget::addOnHover ( Callback  callback)

Register a new onHover callback function for this widget.

Parameters
callbackCallback function to run on hover
Returns
handle to the callback function, which can be passed into removeOnHover to unregister.

◆ borrow()

Widget * gui::widget::Widget::borrow ( Handle  handle)
virtual

Gives a pointer to the subwidget specified by the handle.

NOTE: if handle is not a valid handle for either (1) this widget or (2) any subwidgets, then this function will terminate the program and print out an error message.

Returns
a pointer to the widget specified by handle

Reimplemented in gui::widget::Flexbox, and gui::widget::TextInput.

◆ doClick()

void gui::widget::Widget::doClick ( float  x,
float  y 
)
virtual

Performs a click at the specified (x,y) position in the GUI coordinate frame.

NOTE: This function's main role is to determine if a click at the specified (x,y) coordinate should trigger the onClick handlers for this widget. The default implementation of this checks to see if the (x,y) coordinate is within the bounds specified by origin and width and height, and if so calls all of the onClick handlers.

NOTE: One reason for a derived class to override this function is if the widget itself contains other widgets, because in that case doClick will not be called on those "subwidgets" unless this function passes the call down the chain

Parameters
xx coordinate of the click in GUI coordinates
yy coordinate of the click in GUI coordinates

Reimplemented in gui::widget::Flexbox, and gui::widget::TextInput.

◆ doHover()

void gui::widget::Widget::doHover ( float  x,
float  y 
)
virtual

Performs a hover action at the specified (x,y) position in the GUI coordinate frame.

NOTE: see the documentation for doClick, as everything said there also applies here, but just for the hover actions.

Reimplemented in gui::widget::Flexbox, and gui::widget::TextInput.

◆ getHandle()

Handle gui::widget::Widget::getHandle ( ) const

Queries the handle for this specific widget.

Returns
the handle for this widget

◆ getOrigin()

const glm::vec2 & gui::widget::Widget::getOrigin ( ) const

Queries the origin position of the widget.

Returns
origin position of the widget in GUI coordinates

◆ getSize()

std::pair< std::size_t, std::size_t > gui::widget::Widget::getSize ( ) const

Queries the size of the widget in pixels.

Returns
the width and height of the widget

◆ getType()

Type gui::widget::Widget::getType ( ) const

======================================================================================

=<SIMPLE GETTERS>=====================================================================

Queries the widget::Type of the widget

Returns
Type of the widget

◆ hasHandle()

bool gui::widget::Widget::hasHandle ( Handle  handle) const
virtual

======================================================================================

=<WIDGET BORROWING>===================================================================

The following functions are incredibly important for GUI manipulation, especially in event handlers. They essentially define how outside users can gain internal access to widgets that are inside of the GUI

hasHandle should always be called before calling borrow if you aren't sure whether or not the widget actually has the specified handle, because borrow will terminate the program if the specified widget does not have the handle.

By default hasHandle and borrow only check the widget itself to see if the handle matches. This means that if some derived Widget acts as a container for some set of internal subwidgets, then these functions should be overridden to provide access to those subwidgets.

Checks to see whether or not this widget "has" the specified handle, whether because the handle is this widgets handle, or because this widget internally contains some subwidget that has that handle.

Parameters
handleHandle of the widget for which you want to query
Returns
True if this widget is or contains the specified widget, false otherwise

Reimplemented in gui::widget::Flexbox, and gui::widget::TextInput.

◆ lock()

virtual void gui::widget::Widget::lock ( )
inlinevirtual

Function that is run after all of the widgets have been added to the screee but before any input handling has occurred.

This by default does nothing, but for certain widgets (flexbox) this can be helpful

Reimplemented in gui::widget::Flexbox.

◆ removeOnClick()

void gui::widget::Widget::removeOnClick ( CallbackHandle  handle)

Removes the onClick callback function associated with the given handle.

Parameters
handleHandle to the onClick callback function you want to remove

◆ removeOnHover()

void gui::widget::Widget::removeOnHover ( CallbackHandle  handle)

Removes the onHover callback function associated with the given handle.

Parameters
handleHandle to the onHover callback function you want to remove

◆ render()

virtual void gui::widget::Widget::render ( )
pure virtual

======================================================================================

=<DISPLAY>============================================================================

Renders the widget to the screen

This is the only function that must be overridden by derived classes.

Implemented in gui::widget::DynText, gui::widget::Empty, gui::widget::Flexbox, gui::widget::StaticImg, and gui::widget::TextInput.

◆ setOrigin()

void gui::widget::Widget::setOrigin ( glm::vec2  origin)

Overrides the current origin position with the newly specified one.

NOTE: This is helpful when you may not know the exact origin position of a widget at construction time (i.e. when inserting a widget inside of a flexbox, as the flexbox) then becomes responsible for positioning the widget).

Parameters
originBottom left (x,y) coordinate of the widget in the GUI coordinate frame.

Field Documentation

◆ handle

Handle gui::widget::Widget::handle
protected

======================================================================================

Handle for this widget

◆ height

std::size_t gui::widget::Widget::height {0}
protected

Height of the widget, in pixels.

◆ num_widgets

std::size_t gui::widget::Widget::num_widgets = 0
staticprotected

Static counter for the number of widgets that have been created, so a new identifier can be assigned to each widget as they are created.

◆ on_clicks

std::unordered_map<CallbackHandle, Callback> gui::widget::Widget::on_clicks
protected

all of the onClick handlers, indexable by handle

◆ on_hovers

std::unordered_map<CallbackHandle, Callback> gui::widget::Widget::on_hovers
protected

all of the onHover handlers, indexable by handle

◆ origin

glm::vec2 gui::widget::Widget::origin
protected

Origin position (bottom left) of the widget in GUI coordinates.

◆ type

Type gui::widget::Widget::type
protected

Type of the widget.

◆ width

std::size_t gui::widget::Widget::width {0}
protected

Width of the widget, in pixels.

NOTE: Both the widget and the height of the widget are initialized to 0 and are not set anywhere inside of this base class. Instead, derived classes must set these values themselves once they are known.


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