CUGL 2.1
Cornell University Game Library
|
#include <CULayout.h>
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) |
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.
|
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.
|
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.
|
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.
key | The key identifying the layout information |
data | A JSON object with the layout information |
Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.
|
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.
|
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'.
x_anchor | The horizontal anchor setting |
y_anchor | The vertical anchor setting |
|
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.
|
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.
data | The JSON object specifying the node |
Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.
|
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.
node | The scene graph node to rearrange |
Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.
|
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.
node | The node to reposition |
anchor | The anchor rule for this node |
bounds | The area to compute the position from |
offset | The offset from the computed anchor position |
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.
node | The node to reanchor |
anchor | The layout anchor to use |
|
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.
key | The key identifying the layout information |
Reimplemented in cugl::scene2::FloatLayout, cugl::scene2::GridLayout, and cugl::scene2::AnchoredLayout.