CUGL 2.1
Cornell University Game Library
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::scene2::GridLayout Class Reference

#include <CUGridLayout.h>

Inheritance diagram for cugl::scene2::GridLayout:
cugl::scene2::Layout

Classes

class  Entry
 

Public Member Functions

 GridLayout ()
 
 ~GridLayout ()
 
virtual bool initWithData (const std::shared_ptr< JsonValue > &data) override
 
virtual void dispose () override
 
Size getGridSize () const
 
void setGridSize (const Size size)
 
void setGridSize (Uint32 width, Uint32 height)
 
virtual bool add (const std::string key, const std::shared_ptr< JsonValue > &data) override
 
bool addPosition (const std::string key, unsigned int x, unsigned int y, Anchor anchor)
 
virtual bool remove (const std::string key) override
 
virtual void layout (SceneNode *node) override
 
- Public Member Functions inherited from cugl::scene2::Layout
 Layout ()
 
 ~Layout ()
 
virtual bool init ()
 

Static Public Member Functions

static std::shared_ptr< GridLayoutalloc ()
 
static std::shared_ptr< GridLayoutallocWithData (const std::shared_ptr< JsonValue > &data)
 
- Static Public Member Functions inherited from cugl::scene2::Layout
static Anchor getAnchor (const std::string &x_anchor, const std::string &y_anchor)
 
static void placeNode (SceneNode *node, Anchor anchor, const Rect bounds, const Vec2 offset)
 
static void reanchor (SceneNode *node, Anchor anchor)
 

Protected Member Functions

bool validate (Uint32 width, Uint32 height)
 

Protected Attributes

std::unordered_map< std::string, Entry_entries
 
Uint32 _gwidth
 
Uint32 _gheight
 

Additional Inherited Members

- Public Types inherited from cugl::scene2::Layout
enum  Anchor : int {
  Anchor::BOTTOM_LEFT = 0, Anchor::MIDDLE_LEFT = 1, Anchor::TOP_LEFT = 2, Anchor::BOTTOM_CENTER = 3,
  Anchor::CENTER = 4, Anchor::TOP_CENTER = 5, Anchor::BOTTOM_RIGHT = 6, Anchor::MIDDLE_RIGHT = 7,
  Anchor::TOP_RIGHT = 8, Anchor::BOTTOM_FILL = 9, Anchor::MIDDLE_FILL = 10, Anchor::TOP_FILL = 11,
  Anchor::LEFT_FILL = 12, Anchor::CENTER_FILL = 13, Anchor::RIGHT_FILL = 14, Anchor::TOTAL_FILL = 15,
  Anchor::NONE = 16
}
 

Detailed Description

This class provides a grid layout manager.

A grid layout subdivides the node into equal sized grid regions. Each grid region may receive its own child (and can receive more than one). A grid region behaves like an AnchoredLayout for the rules of how to place the child. The result is a slightly more flexible layout manager than the grid layout in Java.

Layout information is indexed by key. To look up the layout information of a scene graph node, we use the name of the node. This requires all nodes to have unique names. The Scene2Loader prefixes all child names by the parent name, so this is the case in any well-defined JSON file.

Constructor & Destructor Documentation

◆ GridLayout()

cugl::scene2::GridLayout::GridLayout ( )

Creates a degenerate layout manager with no data.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

◆ ~GridLayout()

cugl::scene2::GridLayout::~GridLayout ( )
inline

Deletes this layout manager, disposing of all resources.

Member Function Documentation

◆ add()

virtual bool cugl::scene2::GridLayout::add ( const std::string  key,
const std::shared_ptr< JsonValue > &  data 
)
overridevirtual

Assigns layout information for a given key.

The JSON object may contain any of the following attribute values:

 "x_index":  An integer with the horizontal grid index
 "y_index":  An integer with the vertical grid index
 "x_anchor": One of 'left', 'center', 'right', or 'fill'
 "y_anchor": One of 'bottom', 'middle', 'top', or 'fill'

The specified grid region it treated like an AnchoredLayout when placing the child for the given key. There is no limit on the number of children that may share a grid region.

To look up the layout information of a scene graph node, we use the name of the node. This requires all nodes to have unique names. The Scene2Loader prefixes all child names by the parent name, so this is the case in any well-defined JSON file. If the key is already in use, this method will fail.

Parameters
keyThe key identifying the layout information
dataA JSON object with the layout information
Returns
true if the layout information was assigned to that key

Reimplemented from cugl::scene2::Layout.

◆ addPosition()

bool cugl::scene2::GridLayout::addPosition ( const std::string  key,
unsigned int  x,
unsigned int  y,
Anchor  anchor 
)

Assigns the layout position for a given key.

The specified grid region it treated like an AnchoredLayout when placing the child for the given key. There is no limit on the number of children that may share a grid region.

To look up the layout information of a scene graph node, we use the name of the node. This requires all nodes to have unique names. The Scene2Loader prefixes all child names by the parent name, so this is the case in any well-defined JSON file. If the key is already in use, this method will fail.

Parameters
keyThe key identifying the layout information
xThe column of the grid region
yThe row of the grid region
anchorThe anchor rule for the grid region
Returns
true if the priority was assigned to that key

◆ alloc()

static std::shared_ptr<GridLayout> cugl::scene2::GridLayout::alloc ( )
inlinestatic

Returns a newly allocated layout manager.

The layout manager is initially empty. Before using it to perform a layout, layout information must be registered throught the add method interface.

Returns
a newly allocated layout manager.

◆ allocWithData()

static std::shared_ptr<GridLayout> cugl::scene2::GridLayout::allocWithData ( const std::shared_ptr< JsonValue > &  data)
inlinestatic

Returns a newly allocated layout manager with the given JSON specificaton.

In addition to the 'type' attribute (which must be "float"), the JSON specification supports the following attribute values:

 "width":    An integer with the number of columns in the grid
 "height":   An integer with the number of rows in the grid

All attributes other than 'type' are optional.

Parameters
dataThe JSON object specifying the node
Returns
a newly allocated layout manager with the given JSON specificaton.

◆ dispose()

virtual void cugl::scene2::GridLayout::dispose ( )
inlineoverridevirtual

Deletes the layout resources and resets all attributes.

A disposed layout manager can be safely reinitialized.

Reimplemented from cugl::scene2::Layout.

◆ getGridSize()

Size cugl::scene2::GridLayout::getGridSize ( ) const
inline

Returns the grid size of this layout

The size must have non-zero width and height. Despite the return type, the width and height must be integers.

Returns
the grid size of this layout

◆ initWithData()

virtual bool cugl::scene2::GridLayout::initWithData ( const std::shared_ptr< JsonValue > &  data)
overridevirtual

Initializes a new layout manager with the given JSON specificaton.

In addition to the 'type' attribute (which must be "float"), the JSON specification supports the following attribute values:

 "width":    An integer with the number of columns in the grid
 "height":   An integer with the number of rows in the grid

All attributes other than 'type' are optional.

Parameters
dataThe JSON object specifying the node
Returns
true if initialization was successful.

Reimplemented from cugl::scene2::Layout.

◆ layout()

virtual void cugl::scene2::GridLayout::layout ( SceneNode node)
overridevirtual

Performs a layout on the given node.

A grid layout subdivides the node into equal sized grid regions. Each grid region may receive its own child (and can receive more than one). A grid region behaves like an AnchoredLayout for the rules of how to place the child. The result is a slightly more flexible layout manager than the grid layout in Java.

To look up the layout information of a scene graph node, this method uses the name of the node. This requires all nodes to have unique names. The Scene2Loader prefixes all child names by the parent name, so this is the case in any well-defined JSON file.

Children not registered with this layout manager are not affected.

Parameters
nodeThe scene graph node to rearrange

Reimplemented from cugl::scene2::Layout.

◆ remove()

virtual bool cugl::scene2::GridLayout::remove ( const std::string  key)
overridevirtual

Removes the layout information for a given key.

To look up the layout information of a scene graph node, we use the name of the node. This requires all nodes to have unique names. The Scene2Loader prefixes all child names by the parent name, so this is the case in any well-defined JSON file.

If the key is not in use, this method will fail.

Parameters
keyThe key identifying the layout information
Returns
true if the layout information was removed for that key

Reimplemented from cugl::scene2::Layout.

◆ setGridSize() [1/2]

void cugl::scene2::GridLayout::setGridSize ( const Size  size)
inline

Sets the grid size of this layout

The size must have non-zero width and height. Despite the parameter type, the width and height must be integers. The values will be cast to the nearest integer.

Parameters
sizeThe grid size of this layout

◆ setGridSize() [2/2]

void cugl::scene2::GridLayout::setGridSize ( Uint32  width,
Uint32  height 
)

Sets the grid size of this layout

The size must have non-zero width and height.

Parameters
widthThe number of columns in the grid
heightThe number of rows in the grid

◆ validate()

bool cugl::scene2::GridLayout::validate ( Uint32  width,
Uint32  height 
)
protected

Returns true if (width, height) is a valid grid size

If the layout manager is resized, it must be done in such a way that none of the children are out of bounds. This method returns false if this happens. This method is used by setGridSize before changing the actual grid size.

Parameters
widthThe number of columns in the grid
heightThe number of rows in the grid
Returns
true if (width, height) is a valid grid size

Member Data Documentation

◆ _entries

std::unordered_map<std::string,Entry> cugl::scene2::GridLayout::_entries
protected

The map of keys to layout information

◆ _gheight

Uint32 cugl::scene2::GridLayout::_gheight
protected

The number of rows of grid regions

◆ _gwidth

Uint32 cugl::scene2::GridLayout::_gwidth
protected

The number of columns of grid regions


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