HoverRace  2.0
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
HoverRace::Display::FlexGrid Class Reference

A container that arranges components into a grid that is automatically sized to the contents. More...

#include <FlexGrid.h>

+ Inheritance diagram for HoverRace::Display::FlexGrid:

Classes

class  BasicCell
 
class  Cell
 
class  CellProxy
 This is used to reference a cell of the grid without directly accessing it; useful for adding new widgets to the grid. More...
 
class  DefaultCell
 
struct  Props
 

Public Member Functions

 FlexGrid (Display &display, uiLayoutFlags_t layoutFlags=0)
 Constructor. More...
 
virtual ~FlexGrid ()
 
CellProxy At (size_t row, size_t col)
 Access a cell of the grid. More...
 
void Clear () override
 Remove all child elements. More...
 
void ClearFocusHint (const Control::Nav &nav)
 Clears the focus hint for a navigation direction. More...
 
void DropFocus () override
 Force this widget to give up focus, if it is currently focused. More...
 
CellGetColumnDefault (size_t col)
 Set the default cell settings for a column. More...
 
boost::optional< std::pair< size_t, size_t > > GetFocusHint (const Control::Nav &nav)
 Get the focus hint for a given focus navigation direction. More...
 
const Vec2GetMargin () const
 
const Vec2GetPadding () const
 
bool IsFixedHeight () const
 
bool IsFixedWidth () const
 
Vec3 Measure () override
 Calculate the size of the component. More...
 
bool OnAction () override
 
bool OnNavigate (const Control::Nav &nav) override
 
void Reserve (size_t rows, size_t cols)
 Set a hint about how many rows and columns will be in the grid. More...
 
void SetFixedHeight (double h)
 
void SetFixedSize (double w, double h)
 
void SetFixedWidth (double w)
 
void SetFocusHint (const Control::Nav &nav, size_t row, size_t col)
 Set a hint about what cell to focus when this grid is focused. More...
 
void SetMargin (double width, double height)
 
void SetPadding (double width, double height)
 
bool TryFocus (const Control::Nav &nav=Control::Nav::NEUTRAL) override
 Attempts to give this widget focus. More...
 
- Public Member Functions inherited from HoverRace::Display::BaseContainer
 BaseContainer (Display &display, uiLayoutFlags_t layoutFlags=0)
 Constructor for an unsized container without clipping. More...
 
 BaseContainer (Display &display, const Vec2 &size, bool clip=true, uiLayoutFlags_t layoutFlags=0)
 Constructor for a sized container. More...
 
virtual ~BaseContainer ()
 
void AttachView (Display &disp) override
 
template<class Fn >
void ForEachChild (Fn fn) const
 Iterate over each child widget. More...
 
template<class Fn >
void ForEachVisibleChild (Fn fn) const
 Iterate over each visible child widget. More...
 
const Vec2GetChildOffset () const
 Retrieve the offset to the position of the child widgets. More...
 
double GetOpacity () const
 Retrieve the opacity. More...
 
const Vec2GetSize () const
 Retrieve the size of the container. More...
 
bool IsClip () const
 Check if child elements are clipped to the container bounds. More...
 
bool IsEmpty () const
 Check if this container is devoid of child widgets. More...
 
bool IsVisible () const
 Check if the children of this container are shown. More...
 
bool OnMouseMoved (const Vec2 &pos) override
 
bool OnMousePressed (const Control::Mouse::Click &click) override
 
bool OnMouseReleased (const Control::Mouse::Click &click) override
 
bool OnMouseScrolled (const Control::Mouse::Scroll &scroll) override
 
void SetClip (bool clip)
 Set whether child elements are clipped to the container bounds. More...
 
void SetOpacity (double opacity)
 Set the opacity of the container. More...
 
void SetSize (const Vec2 &size)
 Set the size of the container. More...
 
void SetSize (double w, double h)
 Convenience function for SetSize(const Vec2&). More...
 
void SetVisible (bool visible)
 Sets whether the widgets in this container are visible. More...
 
void ShrinkWrap ()
 Resize the container to the minimum size that will fit all of the child elements. More...
 
- Public Member Functions inherited from HoverRace::Display::UiViewModel
 UiViewModel (uiLayoutFlags_t layoutFlags=0)
 
virtual ~UiViewModel ()
 
Vec2 GetAlignedPos (const Vec2 &pos, double w, double h) const
 Retrieve an arbitrary position adjusted by the current alignment. More...
 
Vec2 GetAlignedPos (double w, double h) const
 Retrieve the position adjusted by the current alignment. More...
 
Alignment GetAlignment () const
 Retrieve the alignment of the component. More...
 
focusRelinquishedSignal_tGetFocusRelinquishedSignal ()
 
focusRequestedSignal_tGetFocusRequestedSignal ()
 
uiLayoutFlags_t GetLayoutFlags () const
 Retrieve the layout flags. More...
 
const Vec2GetPos () const
 Get the position of the component. More...
 
const Vec2GetTranslation () const
 Retrieves the position translation. More...
 
bool IsFocused () const
 Check if this widget currently has input focus. More...
 
uiLayoutFlags_t IsLayoutFloating () const
 
uiLayoutFlags_t IsLayoutUnscaled () const
 
void RequestFocus ()
 Attempt to hand over the input focus to this widget. More...
 
void SetAlignment (Alignment alignment)
 Set the alignment of the component. More...
 
void SetPos (const Vec2 &pos)
 Set the position of the component. More...
 
void SetPos (double x, double y)
 Convenience function for SetPos(const Vec2&). More...
 
void SetTranslation (const Vec2 &translation)
 Sets the position translation. More...
 
void SetTranslation (double x, double y)
 Convenience function for SetTranslation(const Vec2&). More...
 
- Public Member Functions inherited from HoverRace::Display::ViewModel
 ViewModel ()
 
virtual ~ViewModel ()
 
ViewGetView () const
 
void PrepareRender ()
 
void Render ()
 
void SetView (std::unique_ptr< View > &&view)
 

Static Public Member Functions

static Vec2 AlignCellContents (double x, double y, double w, double h, Alignment alignment)
 Adjust the cell origin position to the position of the contents, based on the contents' alignment. More...
 

Static Public Attributes

static const double AUTOSIZE = std::numeric_limits<double>::quiet_NaN()
 Indicator that the dimension of the column or table is automatic (instead of fixed). More...
 
static const size_t BOTTOM = std::numeric_limits<size_t>::max()
 
static const size_t RIGHT = std::numeric_limits<size_t>::max()
 

Protected Member Functions

boost::optional< std::pair< size_t, size_t > > FindChild (UiViewModel *child) const
 Find the coordinates of a child widget. More...
 
void Layout () override
 Adjust the size and position of any child elements. More...
 
template<class T , class... Args>
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< BasicCell< T > > >::type NewGridCell (size_t row, size_t col, Args &&...args)
 
void OnChildRelinquishedFocus (UiViewModel &child, const Control::Nav &nav) override
 Called when a child widget passes input focus to another widget. More...
 
void OnChildRequestedFocus (UiViewModel &child) override
 Called when a child widget requests input focus. More...
 
void Reserve (size_t capacity) override
 Increase the capacity of the of this container. More...
 
- Protected Member Functions inherited from HoverRace::Display::BaseContainer
children_tGetChildren ()
 
template<class T , class... Args>
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type NewChild (Args &&...args)
 Create and append a new child widget to the end of the list. More...
 
template<typename T >
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type RemoveChild (const std::shared_ptr< T > &child)
 Remove a child element. More...
 
template<class T >
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type ReorderChild (const std::shared_ptr< T > &child, size_t idx)
 Move a widget to a different position in the list. More...
 
void SetChildOffset (const Vec2 &offset)
 Sets the position offset of child widgets. More...
 
bool TestHit (const Vec2 &pos)
 Check if the coordinates of an event are within the bounds of this component. More...
 
- Protected Member Functions inherited from HoverRace::Display::UiViewModel
void RelinquishFocus (const Control::Nav &nav)
 Voluntarily give up input focus, passing it on to the next widget. More...
 
void SetFocused (bool focused)
 Set the focused state. More...
 
- Protected Member Functions inherited from HoverRace::Display::ViewModel
template<class T >
void AttachViewDynamic (Display &disp, T *self)
 
virtual void FireModelUpdate (int prop)
 Indicate that a model property has changed. More...
 
void RequestLayout ()
 Indicate that the current layout is out-of-date and needs to be adjusted. More...
 

Private Types

using cells_t = std::vector< std::shared_ptr< Cell >>
 
using SUPER = BaseContainer
 

Private Member Functions

bool FocusDownFrom (size_t row, size_t col, const Control::Nav &nav)
 
template<class Fn >
bool FocusFrom (size_t row, size_t col, const Control::Nav &nav, Fn nextFn)
 Find the next focusable widget in a given direction. More...
 
bool FocusLeftFrom (size_t row, size_t col, const Control::Nav &nav)
 
bool FocusNextFrom (size_t row, size_t col, const Control::Nav &nav)
 
bool FocusPrevFrom (size_t row, size_t col, const Control::Nav &nav)
 
bool FocusRightFrom (size_t row, size_t col, const Control::Nav &nav)
 
bool FocusUpFrom (size_t row, size_t col, const Control::Nav &nav)
 
void SetFocusedCell (size_t row, size_t col)
 

Private Attributes

size_t colReserve
 
std::vector< DefaultCelldefaultCols
 
Vec2 fixedSize
 
boost::optional< std::pair< size_t, size_t > > focusedCell
 
std::map< Control::Nav::dir_t, std::pair< size_t, size_t > > focusHints
 
Vec2 margin
 
Vec2 padding
 
std::vector< cells_trows
 
Vec2 size
 

Additional Inherited Members

- Public Types inherited from HoverRace::Display::UiViewModel
enum  Alignment {
  Alignment::NW, Alignment::N, Alignment::NE, Alignment::E,
  Alignment::SE, Alignment::S, Alignment::SW, Alignment::W,
  Alignment::CENTER
}
 Imagine the component pinned to the container with a thumbtack. More...
 
using focusRelinquishedSignal_t = boost::signals2::signal< void(UiViewModel &, const Control::Nav &)>
 
using focusRequestedSignal_t = boost::signals2::signal< void(UiViewModel &)>
 
- Protected Types inherited from HoverRace::Display::BaseContainer
using children_t = std::vector< std::unique_ptr< Child >>
 
- Protected Attributes inherited from HoverRace::Display::BaseContainer
Displaydisplay
 

Detailed Description

A container that arranges components into a grid that is automatically sized to the contents.

Author
Michael Imamura

Member Typedef Documentation

using HoverRace::Display::FlexGrid::cells_t = std::vector<std::shared_ptr<Cell>>
private

Constructor & Destructor Documentation

HoverRace::Display::FlexGrid::FlexGrid ( Display display,
uiLayoutFlags_t  layoutFlags = 0 
)

Constructor.

Parameters
displayThe display child elements will be attached to.
layoutFlagsOptional layout flags.
virtual HoverRace::Display::FlexGrid::~FlexGrid ( )
inlinevirtual

Member Function Documentation

Vec2 HoverRace::Display::FlexGrid::AlignCellContents ( double  x,
double  y,
double  w,
double  h,
Alignment  alignment 
)
static

Adjust the cell origin position to the position of the contents, based on the contents' alignment.

Parameters
xThe upper-left corner of the cell, relative to the grid.
yThe upper-left corner of the cell, relative to the grid.
wThe width of the cell, not including padding.
hThe height of the cell, not including padding.
alignmentThe alignment of the cell contents.
Returns
The adjusted position.
CellProxy HoverRace::Display::FlexGrid::At ( size_t  row,
size_t  col 
)
inline

Access a cell of the grid.

This is usually used to add new widgets to the grid, but can also be used to change attributes of the cell without accessing the contents. If adding new widgets, the row and column don't need to already exist.

Parameters
rowThe row index.
colThe column index.
Returns
A proxy to the cell.
void HoverRace::Display::FlexGrid::Clear ( )
overridevirtual

Remove all child elements.

Reimplemented from HoverRace::Display::BaseContainer.

void HoverRace::Display::FlexGrid::ClearFocusHint ( const Control::Nav nav)

Clears the focus hint for a navigation direction.

Parameters
navThe navigation.
void HoverRace::Display::FlexGrid::DropFocus ( )
overridevirtual

Force this widget to give up focus, if it is currently focused.

This is called by the parent container if focus is shifting away to another widget.

Reimplemented from HoverRace::Display::UiViewModel.

boost::optional< std::pair< size_t, size_t > > HoverRace::Display::FlexGrid::FindChild ( UiViewModel child) const
protected

Find the coordinates of a child widget.

Parameters
childThe widget to search for (may be nullptr).
Returns
The coordinates (row, col) if found.
bool HoverRace::Display::FlexGrid::FocusDownFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
template<class Fn >
bool HoverRace::Display::FlexGrid::FocusFrom ( size_t  row,
size_t  col,
const Control::Nav nav,
Fn  nextFn 
)
inlineprivate

Find the next focusable widget in a given direction.

The starting coordinates are not focused; instead, the next widget in the direction nav is the first to be focused.

The nextFn callback function will take the row and column as references, so it can advance them to the next coordinate. It returns true if it was successful, false if we advanced off the edge of the grid.

Parameters
rowThe starting row.
colThe starting column.
navThe navigation direction.
nextFnThe function to advance the coordinates.
Returns
true if it succeeded in focusing a widget, false if it failed (could not find a focusable widget).
See also
FocusUpFrom()
FocusDownFrom()
FocusLeftFrom()
FocusRightFrom()
FocusNextFrom()
FocusPrevFrom()
bool HoverRace::Display::FlexGrid::FocusLeftFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
bool HoverRace::Display::FlexGrid::FocusNextFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
bool HoverRace::Display::FlexGrid::FocusPrevFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
bool HoverRace::Display::FlexGrid::FocusRightFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
bool HoverRace::Display::FlexGrid::FocusUpFrom ( size_t  row,
size_t  col,
const Control::Nav nav 
)
private
Cell& HoverRace::Display::FlexGrid::GetColumnDefault ( size_t  col)
inline

Set the default cell settings for a column.

This does not retro-actively set the defaults for cells already added.

Parameters
colThe column coordinate (starting at zero).
Returns
A mutable cell that will be used as the default template.
boost::optional< std::pair< size_t, size_t > > HoverRace::Display::FlexGrid::GetFocusHint ( const Control::Nav nav)

Get the focus hint for a given focus navigation direction.

Parameters
navThe navigation direction.
Returns
The cell coordinates, if set.
const Vec2& HoverRace::Display::FlexGrid::GetMargin ( ) const
inline
const Vec2& HoverRace::Display::FlexGrid::GetPadding ( ) const
inline
bool HoverRace::Display::FlexGrid::IsFixedHeight ( ) const
bool HoverRace::Display::FlexGrid::IsFixedWidth ( ) const
void HoverRace::Display::FlexGrid::Layout ( )
overrideprotectedvirtual

Adjust the size and position of any child elements.

Subclasses with child elements should override this function.

This is called automatically during the PrepareRender() phase if RequestLayout() has been called. It is also called the first time PrepareRender() is invoked. After this function is called, it will not be called again until another call to RequestLayout().

Reimplemented from HoverRace::Display::ViewModel.

Vec3 HoverRace::Display::FlexGrid::Measure ( )
overridevirtual

Calculate the size of the component.

For UI components, the return value is affected by layout flags.

Returns
The size (may be zero if the size cannot be determined yet).
Warning
Depending on the view, this may be an expensive operation since the view may have to call View::PrepareRender to apply model changes. As such, it is recommended to call Measure in the PrepareRender phase itself.

Reimplemented from HoverRace::Display::BaseContainer.

template<class T , class... Args>
std::enable_if< std::is_base_of<UiViewModel, T>::value, std::shared_ptr<BasicCell<T> > >::type HoverRace::Display::FlexGrid::NewGridCell ( size_t  row,
size_t  col,
Args &&...  args 
)
inlineprotected
bool HoverRace::Display::FlexGrid::OnAction ( )
inlineoverridevirtual

Reimplemented from HoverRace::Display::UiViewModel.

void HoverRace::Display::FlexGrid::OnChildRelinquishedFocus ( UiViewModel child,
const Control::Nav nav 
)
overrideprotectedvirtual

Called when a child widget passes input focus to another widget.

Parameters
childThe child widget.
navThe navigation direction.

Reimplemented from HoverRace::Display::BaseContainer.

void HoverRace::Display::FlexGrid::OnChildRequestedFocus ( UiViewModel child)
overrideprotectedvirtual

Called when a child widget requests input focus.

Parameters
childThe child widget.

Reimplemented from HoverRace::Display::BaseContainer.

bool HoverRace::Display::FlexGrid::OnNavigate ( const Control::Nav nav)
inlineoverridevirtual

Reimplemented from HoverRace::Display::UiViewModel.

void HoverRace::Display::FlexGrid::Reserve ( size_t  capacity)
inlineoverrideprotectedvirtual

Increase the capacity of the of this container.

The internal capacity will be increased (never decreased). This is useful as a hint to prevent re-allocation due to resizing the internal storage. It's not required; the internal storage will be resized as necessary anyway, at the cost of re-allocation. Containers with a lot of child elements will benefit the most from specifying the capacity early.

Parameters
capacityThe new capacity.

Reimplemented from HoverRace::Display::BaseContainer.

void HoverRace::Display::FlexGrid::Reserve ( size_t  rowCapacity,
size_t  colCapacity 
)

Set a hint about how many rows and columns will be in the grid.

The internal capacity will be increased (never decreased). This is useful as a hint to prevent re-allocation due to resizing the internal storage.

This function assumes that every cell of the grid will be filled. If the grid is sparse, then it is better to not call this function and let the automatic resizing occur.

Parameters
rowCapacityThe estimated number of rows.
colCapacityThe estimated number of columns per row.
void HoverRace::Display::FlexGrid::SetFixedHeight ( double  h)
void HoverRace::Display::FlexGrid::SetFixedSize ( double  w,
double  h 
)
void HoverRace::Display::FlexGrid::SetFixedWidth ( double  w)
void HoverRace::Display::FlexGrid::SetFocusedCell ( size_t  row,
size_t  col 
)
private
void HoverRace::Display::FlexGrid::SetFocusHint ( const Control::Nav nav,
size_t  row,
size_t  col 
)

Set a hint about what cell to focus when this grid is focused.

When the grid takes focus, the grid searches for a focusable cell. This automatic search may not produce results which are "natural" for the grid position, so this provides a way to set a hint about what cell to start the search from.

Because the size of the grid can change, the constants BOTTOM and RIGHT can be used to refer to bottommost or rightmost row or column, respectively.

Parameters
navThe navigation direction.
rowThe row (may be BOTTOM to refer to the bottom row).
colThe col.
void HoverRace::Display::FlexGrid::SetMargin ( double  width,
double  height 
)
void HoverRace::Display::FlexGrid::SetPadding ( double  width,
double  height 
)
bool HoverRace::Display::FlexGrid::TryFocus ( const Control::Nav nav = Control::Nav::NEUTRAL)
overridevirtual

Attempts to give this widget focus.

Subclasses should override this with logic to determine if the widget is focusable.

This is called by the parent container if focus has been requested.

Parameters
navThe direction in which the focus is heading.
Returns
true if it succeeds (widget is now focused), false if it fails (widget did not take focus).

Reimplemented from HoverRace::Display::UiViewModel.

Member Data Documentation

const double HoverRace::Display::FlexGrid::AUTOSIZE = std::numeric_limits<double>::quiet_NaN()
static

Indicator that the dimension of the column or table is automatic (instead of fixed).

const size_t HoverRace::Display::FlexGrid::BOTTOM = std::numeric_limits<size_t>::max()
static
size_t HoverRace::Display::FlexGrid::colReserve
private
std::vector<DefaultCell> HoverRace::Display::FlexGrid::defaultCols
private
Vec2 HoverRace::Display::FlexGrid::fixedSize
private
boost::optional<std::pair<size_t, size_t> > HoverRace::Display::FlexGrid::focusedCell
private
std::map<Control::Nav::dir_t, std::pair<size_t, size_t> > HoverRace::Display::FlexGrid::focusHints
private
Vec2 HoverRace::Display::FlexGrid::margin
private
Vec2 HoverRace::Display::FlexGrid::padding
private
const size_t HoverRace::Display::FlexGrid::RIGHT = std::numeric_limits<size_t>::max()
static
std::vector<cells_t> HoverRace::Display::FlexGrid::rows
private
Vec2 HoverRace::Display::FlexGrid::size
private

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