CUGL 4.0
Cornell University Game Library
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::scene2::FigmaLayout Class Reference

#include <CUFigmaLayout.h>

Inheritance diagram for cugl::scene2::FigmaLayout:
cugl::scene2::Layout

Classes

class  Entry
 

Public Member Functions

 FigmaLayout ()
 
 ~FigmaLayout ()
 
virtual bool initWithData (const std::shared_ptr< JsonValue > &data) override
 
virtual void dispose () override
 
virtual bool add (const std::string key, const std::shared_ptr< JsonValue > &data) override
 
bool addMixed (const std::string key, Anchor anchor, float left, float right, float top, float bottom, bool x_absolute, bool y_absolute)
 
virtual bool remove (const std::string key) override
 
virtual void layout (scene2::SceneNode *node) override
 
- Public Member Functions inherited from cugl::scene2::Layout
 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 std::shared_ptr< FigmaLayoutalloc ()
 
static std::shared_ptr< FigmaLayoutallocWithData (const std::shared_ptr< JsonValue > &data)
 
static void placeFigmaNode (SceneNode *node, Anchor anchor, const Rect &bounds, const Rect &offset)
 
- 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 placeNode (SceneNode *node, Anchor anchor, const Rect &bounds, const Vec2 &offbl, const Vec2 &offtr)
 
static void reanchor (SceneNode *node, Anchor anchor)
 

Protected Attributes

std::unordered_map< std::string, Entry_entries
 

Additional Inherited Members

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

Detailed Description

This class provides a Figma layout manager.

A Figma layout attaches a child node to one of nine "anchors" in the parent (corners, sides or middle), together with a percentage (or absolute) offset. As the parent grows or shinks, the child will move according to its anchor. For example, nodes in the center will stay centered, while nodes on the left side will move to keep the appropriate distance from the left side. In fact, the stretching behavior is very similar to that of a NinePatch.

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

◆ FigmaLayout()

cugl::scene2::FigmaLayout::FigmaLayout ( )
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.

◆ ~FigmaLayout()

cugl::scene2::FigmaLayout::~FigmaLayout ( )
inline

Deletes this layout manager, disposing of all resources.

Member Function Documentation

◆ add()

virtual bool cugl::scene2::FigmaLayout::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_anchor": One of 'left', 'center', 'right', or 'left+right'
 "y_anchor": One of 'bottom', 'middle', 'top', or 'top+bottom'
 "x_absolute": Whether to use absolute instead of relative (percentage) offsets in x
 "y_absolute": Whether to use absolute instead of relative (percentage) offsets in y
 "l_offset": A number indicating the left offset from the anchor.
             If "x_absolute" is true, this is the distance in coordinate space.
             Otherwise it is a percentage of the width.
 "r_offset": A number indicating the right offset from the anchor.
             If "x_absolute" is true, this is the distance in coordinate space.
             Otherwise it is a percentage of the width.
 "t_offset": A number indicating the top offset from the anchor.
             If "y_absolute" is true, this is the distance in coordinate space.
             Otherwise it is a percentage of the height.
 "b_offset": A number indicating the bottom offset from the anchor.
             If "y_absolute" is true, this is the distance in coordinate space.
             Otherwise it is a percentage of the height.

All attributes are optional. There are no required attributes.

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.

◆ addMixed()

bool cugl::scene2::FigmaLayout::addMixed ( const std::string  key,
Anchor  anchor,
float  left,
float  right,
float  top,
float  bottom,
bool  x_absolute,
bool  y_absolute 
)

Assigns layout information for a given key.

This method allows for independent specification of absolute or relative offsets along the x and y axes. If an axis is marked as absolute, its offset is treated as a distance in Node coordinate space. Otherwise, the offset is interpreted as a percentage of the parent node's size in that dimension.

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
anchorThe anchor point to use
leftThe offset from the left edge
rightThe offset from the right edge
topThe offset from the top edge
bottomThe offset from the bottom edge
x_absoluteWhether the x offset is absolute (in coordinate space)
y_absoluteWhether the y offset is absolute (in coordinate space)
Returns
true if the layout information was assigned to that key

◆ alloc()

static std::shared_ptr< FigmaLayout > cugl::scene2::FigmaLayout::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< FigmaLayout > cugl::scene2::FigmaLayout::allocWithData ( const std::shared_ptr< JsonValue > &  data)
inlinestatic

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

The JSON specification format is simple. It only supports one (required) attribute: 'type'. The type should specify "figma".

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

◆ dispose()

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

Deletes the layout resources and resets all attributes.

A disposed layout manager can be safely reinitialized.

Reimplemented from cugl::scene2::Layout.

◆ initWithData()

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

Initializes a new layout manager with the given JSON specificaton.

The JSON specification format is simple. It only supports one (required) attribute: 'type'. The type should specify "figma".

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

Reimplemented from cugl::scene2::Layout.

◆ layout()

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

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.

This manager attaches a child node to one of nine "anchors" in the parent (corners, sides or middle), together with a percentage (or absolute) offset. As the parent grows or shinks, the child will move according to its anchor. For example, nodes in the center will stay centered, while nodes on the left side will move to keep the appropriate distance from the left side. In fact, the stretching behavior is very similar to that of a NinePatch.

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.

◆ placeFigmaNode()

static void cugl::scene2::FigmaLayout::placeFigmaNode ( SceneNode node,
Anchor  anchor,
const Rect bounds,
const Rect offset 
)
static

Repositions the given Figma 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 of the parent, like we do in GridLayout.

This function is different from Layout#placeNode in that it follows the conventions of Figma layouts. For Figma nodes, the offset is measured from the top-left corner of the parent. The size of the offset is the total distance from the edges. So the width is the sum of the distance from the left and right edges while the height is the sum of the distance from the top and bottom edges

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

◆ remove()

virtual bool cugl::scene2::FigmaLayout::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.

Member Data Documentation

◆ _entries

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

The map of keys to layout information


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