CUGL 2.1
Cornell University Game Library
|
#include <CUAnchoredLayout.h>
Classes | |
class | Entry |
Public Member Functions | |
AnchoredLayout () | |
~AnchoredLayout () | |
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 | addAbsolute (const std::string key, Anchor anchor, const Vec2 offset) |
bool | addRelative (const std::string key, Anchor anchor, const Vec2 offset) |
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 bool | init () |
Static Public Member Functions | |
static std::shared_ptr< AnchoredLayout > | alloc () |
static std::shared_ptr< AnchoredLayout > | allocWithData (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 Attributes | |
std::unordered_map< std::string, Entry > | _entries |
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 } |
This class provides an anchored layout manager.
An anchored 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.
|
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.
|
inline |
Deletes this layout manager, disposing of all resources.
|
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 'fill' "y_anchor": One of 'bottom', 'middle', 'top', or 'fill' "absolute": Whether to use absolute instead of relative (percentage) offsets "x_offset": A number indicating the horizontal offset from the anchor. If "absolute" is true, this is the distance in coordinate space. Otherwise it is a percentage of the width. "y_offset": A number indicating the vertical offset from the anchor. If "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.
key | The key identifying the layout information |
data | A JSON object with the layout information |
Reimplemented from cugl::scene2::Layout.
bool cugl::scene2::AnchoredLayout::addAbsolute | ( | const std::string | key, |
Anchor | anchor, | ||
const Vec2 | offset | ||
) |
Assigns layout information for a given key.
This method specifies the anchor offset in absolute terms. That is, offset is the distance from the anchor in Node coordinate space.
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.
key | The key identifying the layout information |
anchor | The anchor point to use |
offset | The offset from the anchor point |
bool cugl::scene2::AnchoredLayout::addRelative | ( | const std::string | key, |
Anchor | anchor, | ||
const Vec2 | offset | ||
) |
Assigns layout information for a given key.
This method specifies the anchor offset in relative terms. That is, offset is the percentage of the width and height of the Node.
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.
key | The key identifying the layout information |
anchor | The anchor point to use |
offset | The offset from the anchor point |
|
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.
|
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 "anchored".
data | The JSON object specifying the node |
|
inlineoverridevirtual |
Deletes the layout resources and resets all attributes.
A disposed layout manager can be safely reinitialized.
Reimplemented from cugl::scene2::Layout.
|
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 "anchored".
data | The JSON object specifying the node |
Reimplemented from cugl::scene2::Layout.
|
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.
node | The scene graph node to rearrange |
Reimplemented from cugl::scene2::Layout.
|
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.
key | The key identifying the layout information |
Reimplemented from cugl::scene2::Layout.
|
protected |
The map of keys to layout information