CUGL 2.1
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cugl::scene2::Layout Class Reference

#include <CULayout.h>

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

Public Types

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
}
 

Public Member Functions

 Layout ()
 
 ~Layout ()
 
virtual void dispose ()
 
virtual bool init ()
 
virtual bool initWithData (const std::shared_ptr< JsonValue > &data)
 
virtual bool add (const std::string key, const std::shared_ptr< JsonValue > &data)
 
virtual bool remove (const std::string key)
 
virtual void layout (SceneNode *node)
 

Static Public Member Functions

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)
 

Detailed Description

This class is an abstract interface for a layout manager.

A layout manager associates layout information with scene graph nodes, much like a map interface. When a layout manager is asked to layout a Node, it searches for those children that are registered with the layout manager. For those children, it repositions and/or resizes them according to the layout information.

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.

All layout managers extend this class, providing implementations for the add, remove, and layout methods.

Several layout managers, such as AnchoredLayout and GridLayout make use of anchors. Therefore, we provide support for them in this class in order to consolidate code.

Member Enumeration Documentation

◆ Anchor

enum cugl::scene2::Layout::Anchor : int
strong

This enum represents the nine possible anchors.

The anchor positions are similar to that of a NinePatch. The anchor positions are relative to the Node bounding box. In addition, there are "fill" anchors, which stretch the node to fill the available space.

Enumerator
BOTTOM_LEFT 

The bottom left corner, or position (0,0) in Node coordinate space.

MIDDLE_LEFT 

The left side, or position (0,height/2) in Node coordinate space.

TOP_LEFT 

The top left corner, or position (0,height) in Node coordinate space.

BOTTOM_CENTER 

The bottom side, or position (width/2,0) in Node coordinate space.

CENTER 

The middle region, or position (width/2,height/2) in Node coordinate space.

TOP_CENTER 

The top side, or position (width/2,height) in Node coordinate space.

BOTTOM_RIGHT 

The bottom right corner, or position (width,0) in Node coordinate space.

MIDDLE_RIGHT 

The right side, or position (width,height/2) in Node coordinate space.

TOP_RIGHT 

The top left corner, or position (width,height) in Node coordinate space.

BOTTOM_FILL 

Anchors at y=0, but stretches the width to fill the parent.

MIDDLE_FILL 

Anchors at y=height/2, but stretches the width to fill the parent.

TOP_FILL 

Anchors at y=height, but stretches the width to fill the parent.

LEFT_FILL 

Anchors at x=0, but stretches the height to fill the parent.

CENTER_FILL 

Anchors at x=width/2, but stretches the height to fill the parent.

RIGHT_FILL 

Anchors at x=width, but stretches the height to fill the parent.

TOTAL_FILL 

Stretches the width and height to fill the entire parent

NONE 

No anchor. The layout will not adjust this child

Constructor & Destructor Documentation

◆ Layout()

cugl::scene2::Layout::Layout ( )
inline

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.

◆ ~Layout()

cugl::scene2::Layout::~Layout ( )
inline

Deletes this layout manager, disposing of all resources.

Member Function Documentation

◆ add()

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

Assigns 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.

The format of the JSON object is layout manager specific.

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 in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.

◆ dispose()

virtual void cugl::scene2::Layout::dispose ( )
inlinevirtual

Deletes the layout resources and resets all attributes.

A disposed layout manager can be safely reinitialized.

Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.

◆ getAnchor()

static Anchor cugl::scene2::Layout::getAnchor ( const std::string &  x_anchor,
const std::string &  y_anchor 
)
static

Returns the anchor for the given text values

This method is used to get an anchor object from a JSON directory. The x_anchor should be one of 'left', 'center', 'right', or 'fill'. The y_anchor should be one of 'bottom', 'middle', 'top', or 'fill'.

Parameters
x_anchorThe horizontal anchor setting
y_anchorThe vertical anchor setting
Returns
the anchor for the given text values

◆ init()

virtual bool cugl::scene2::Layout::init ( )
inlinevirtual

Initializes a new 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
true if initialization is successful.

◆ initWithData()

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

Initializes a new layout manager with the given JSON specificaton.

The JSON specification format is implementation specific. However, all layout managers require a 'type' attribute that specifies the name of the layout manager.

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

Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.

◆ layout()

virtual void cugl::scene2::Layout::layout ( SceneNode node)
inlinevirtual

Performs a layout on the given node.

This layout manager will searches for those children that are registered with it. For those children, it repositions and/or resizes them according to the layout information.

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 in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.

◆ placeNode()

static void cugl::scene2::Layout::placeNode ( SceneNode node,
Anchor  anchor,
const Rect  bounds,
const Vec2  offset 
)
static

Repositions the given node according the rules of its anchor.

The repositioning is done relative to bounds, not the parent node. This allows us to apply anchors to a subregion, like we do in GridLayout. The value offset should be in coordinates, and not percentages.

Parameters
nodeThe node to reposition
anchorThe anchor rule for this node
boundsThe area to compute the position from
offsetThe offset from the computed anchor position

◆ reanchor()

static void cugl::scene2::Layout::reanchor ( SceneNode node,
Anchor  anchor 
)
static

Resets the node anchor to agree with the layout anchor

For some layout managers, the layout anchor (which is an enum) may disagree with the node anchor (which is a percentage vector). This method allows a layout manager to "disable" the node anchor in favor of the layout anchor.

Parameters
nodeThe node to reanchor
anchorThe layout anchor to use

◆ remove()

virtual bool cugl::scene2::Layout::remove ( const std::string  key)
inlinevirtual

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 in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.


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