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

#include <CUPathNode.h>

Inheritance diagram for cugl::PathNode:
cugl::TexturedNode cugl::Node

Public Member Functions

 PathNode ()
 
 ~PathNode ()
 
bool initWithVertices (const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
 
bool initWithPoly (const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
 
void setStroke (float stroke)
 
float getStroke () const
 
void setClosed (bool closed)
 
bool getClosed () const
 
void setJoint (PathJoint joint)
 
PathJoint getJoint () const
 
void setCap (PathCap cap)
 
PathCap getCap () const
 
virtual void setPolygon (const std::vector< Vec2 > &vertices) override
 
virtual void setPolygon (const Poly2 &poly) override
 
virtual void setPolygon (const Rect &rect) override
 
float getExtrudedContentWidth () const
 
float getExtrudedContentHeight () const
 
const SizegetExtrudedContentSize () const
 
const RectgetExtrudedContentBounds () const
 
virtual void draw (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
 
- Public Member Functions inherited from cugl::TexturedNode
 TexturedNode ()
 
virtual ~TexturedNode ()
 
virtual void dispose () override
 
virtual bool init () override
 
bool init (const std::vector< Vec2 > &vertices)
 
bool init (const Poly2 &poly)
 
bool init (const Rect &rect)
 
bool initWithFile (const std::string &filename)
 
bool initWithFile (const std::string &filename, const std::vector< Vec2 > &vertices)
 
bool initWithFile (const std::string &filename, const Poly2 &poly)
 
bool initWithFile (const std::string &filename, const Rect &rect)
 
bool initWithTexture (const std::shared_ptr< Texture > &texture)
 
bool initWithTexture (const std::shared_ptr< Texture > &texture, const std::vector< Vec2 > &vertices)
 
bool initWithTexture (const std::shared_ptr< Texture > &texture, const Poly2 &poly)
 
bool initWithTexture (const std::shared_ptr< Texture > &texture, const Rect &rect)
 
void setTexture (const std::string &filename)
 
void setTexture (const std::shared_ptr< Texture > &texture)
 
std::shared_ptr< Texture > & getTexture ()
 
const std::shared_ptr< Texture > & getTexture () const
 
const Poly2getPolygon () const
 
virtual void shiftPolygon (float dx, float dy)
 
const RectgetBoundingRect () const
 
void setBlendFunc (GLenum srcFactor, GLenum dstFactor)
 
GLenum getSourceBlendFactor () const
 
GLenum getDestinationBlendFactor () const
 
void setBlendEquation (GLenum equation)
 
GLenum getBlendEquation () const
 
void flipHorizontal (bool flag)
 
bool isFlipHorizontal () const
 
void flipVertical (bool flag)
 
bool isFlipVertical () const
 
virtual std::string toString (bool verbose=false) const override
 
bool isAbsolute () const
 
void setAbsolute (bool flag)
 
virtual void setAnchor (const Vec2 &anchor) override
 
void refresh ()
 
- Public Member Functions inherited from cugl::Node
 Node ()
 
virtual ~Node ()
 
virtual bool initWithPosition (const Vec2 &pos)
 
bool initWithPosition (float x, float y)
 
virtual bool initWithBounds (const Size &size)
 
bool initWithBounds (float width, float height)
 
virtual bool initWithBounds (const Rect &rect)
 
bool initWithBounds (float x, float y, float width, float height)
 
virtual Nodecopy (Node *dst)
 
unsigned int getTag () const
 
void setTag (unsigned int tag)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
 operator std::string () const
 
const Vec2getPosition () const
 
void setPosition (const Vec2 &position)
 
void setPosition (float x, float y)
 
float getPositionX (void) const
 
void setPositionX (float x)
 
float getPositionY (void) const
 
void setPositionY (float y)
 
Vec2 getWorldPosition () const
 
const SizegetContentSize () const
 
virtual void setContentSize (const Size &size)
 
virtual void setContentSize (float width, float height)
 
float getContentWidth () const
 
void setContentWidth (float width)
 
float getContentHeight () const
 
void setContentHeight (float height)
 
Size getSize () const
 
float getWidth () const
 
float getHeight () const
 
Rect getBoundingBox () const
 
void setAnchor (float x, float y)
 
const Vec2getAnchor () const
 
Vec2 getAnchorInPixels ()
 
Color4 getColor () const
 
void setColor (Color4 color)
 
Color4 getAbsoluteColor ()
 
bool isVisible () const
 
void setVisible (bool visible)
 
bool hasRelativeColor ()
 
void setRelativeColor (bool flag)
 
const Vec2getScale () const
 
float getScaleX () const
 
float getScaleY () const
 
void setScale (float scale)
 
void setScale (const Vec2 &vec)
 
void setScale (float sx, float sy)
 
float getAngle ()
 
void setAngle (float angle)
 
const Mat4getAlternateTransform ()
 
void setAlternateTransform (const Mat4 &transform)
 
bool withAlternateTransform ()
 
void chooseAlternateTransform (bool active)
 
const Mat4getNodeToParentTransform () const
 
Mat4 getParentToNodeTransform () const
 
Mat4 getNodeToWorldTransform () const
 
Mat4 getWorldToNodeTransform () const
 
Vec2 screenToNodeCoords (const Vec2 &screenPoint) const
 
Vec2 worldToNodeCoords (const Vec2 &worldPoint) const
 
Vec2 nodeToScreenCoords (const Vec2 &nodePoint) const
 
Vec2 nodeToWorldCoords (const Vec2 &nodePoint) const
 
Vec2 parentToNodeCoords (const Vec2 &parentPoint) const
 
Vec2 nodeToParentCoords (const Vec2 &nodePoint) const
 
void setZOrder (int z)
 
int getZOrder () const
 
bool isZDirty () const
 
void sortZOrder ()
 
size_t getChildCount () const
 
std::shared_ptr< NodegetChild (unsigned int pos)
 
const std::shared_ptr< Node > & getChild (unsigned int pos) const
 
template<typename T >
std::shared_ptr< TgetChild (unsigned int pos) const
 
std::shared_ptr< NodegetChildByTag (unsigned int tag) const
 
template<typename T >
std::shared_ptr< TgetChildByTag (unsigned int tag) const
 
std::shared_ptr< NodegetChildByName (const std::string &name) const
 
template<typename T >
std::shared_ptr< TgetChildByName (const std::string &name) const
 
std::vector< std::shared_ptr< Node > > getChildren ()
 
const std::vector< std::shared_ptr< Node > > & getChildren () const
 
void addChild (std::shared_ptr< Node > child)
 
virtual void addChild (const std::shared_ptr< Node > &child, int zval)
 
void addChildWithTag (const std::shared_ptr< Node > &child, unsigned int tag)
 
void addChildWithTag (const std::shared_ptr< Node > &child, unsigned int tag, int zval)
 
void addChildWithName (const std::shared_ptr< Node > &child, const std::string &name)
 
void addChildWithName (const std::shared_ptr< Node > &child, const std::string &name, int zval)
 
void swapChild (const std::shared_ptr< Node > &child1, const std::shared_ptr< Node > &child2, bool inherit=false)
 
NodegetParent ()
 
const NodegetParent () const
 
ScenegetScene ()
 
const ScenegetScene () const
 
void removeFromParent ()
 
virtual void removeChild (unsigned int pos)
 
void removeChild (const std::shared_ptr< Node > &child)
 
void removeChildByTag (unsigned int tag)
 
void removeChildByName (const std::string &name)
 
virtual void removeAllChildren ()
 
void render (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint)
 
void render (const std::shared_ptr< SpriteBatch > &batch)
 

Static Public Member Functions

static std::shared_ptr< PathNodealloc ()
 
static std::shared_ptr< PathNodeallocWithVertices (const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
 
static std::shared_ptr< PathNodeallocWithPoly (const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
 
static std::shared_ptr< PathNodeallocWithRect (const Rect &rect, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
 
static std::shared_ptr< PathNodeallocWithLine (const Vec2 &origin, const Vec2 &dest, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
 
static std::shared_ptr< PathNodeallocWithEllipse (const Vec2 &center, const Size &size, float stroke, unsigned int segments=PATH_SEGMENTS, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
 
- Static Public Member Functions inherited from cugl::Node
static std::shared_ptr< Nodealloc ()
 
static std::shared_ptr< NodeallocWithPosition (const Vec2 &pos)
 
static std::shared_ptr< NodeallocWithPosition (float x, float y)
 
static std::shared_ptr< NodeallocWithBounds (const Size &size)
 
static std::shared_ptr< NodeallocWithBounds (float width, float height)
 
static std::shared_ptr< NodeallocWithBounds (const Rect &rect)
 
static std::shared_ptr< NodeallocWithBounds (float x, float y, float width, float height)
 

Protected Attributes

Poly2 _extrusion
 
Rect _extrbounds
 
bool _closed
 
float _stroke
 
PathJoint _joint
 
PathCap _endcap
 
- Protected Attributes inherited from cugl::TexturedNode
std::string _classname
 
std::shared_ptr< Texture_texture
 
Poly2 _polygon
 
bool _absolute
 
bool _rendered
 
std::vector< Vertex2_vertices
 
GLenum _blendEquation
 
GLenum _srcFactor
 
GLenum _dstFactor
 
bool _flipHorizontal
 
bool _flipVertical
 
- Protected Attributes inherited from cugl::Node
Vec2 _position
 
Vec2 _anchor
 
Size _contentSize
 
Color4 _tintColor
 
bool _hasParentColor
 
bool _isVisible
 
Vec2 _scale
 
float _angle
 
Mat4 _transform
 
bool _useTransform
 
Mat4 _combined
 
std::vector< std::shared_ptr< Node > > _children
 
Node_parent
 
Scene_graph
 
int _childOffset
 
unsigned int _tag
 
std::string _name
 
size_t _hashOfName
 
int _zOrder
 
bool _zDirty
 

Static Protected Attributes

static PathExtruder _extruder
 
static PathOutliner _outliner
 

Additional Inherited Members

- Protected Member Functions inherited from cugl::TexturedNode
void clearRenderData ()
 
void updateTextureCoords ()
 
 CU_DISALLOW_COPY_AND_ASSIGN (TexturedNode)
 

Detailed Description

This is a scene graph node to represent a path with width.

At first glance, it would appear that this class is unnecessary. A path with width, produced by PathExtruder is a solid polygon. This polygon can, in turn, be used in conjunction with PolygonNode.

However, there are some subtle issues. In particular, mitres and joints may mean that a PathNode and WireNode at the same position may not line up with one another. This is undesirable. Hence this is a special polygon node that takes into account that it is an extruded path.

One of the side effects of this is that the content size of the node is defined by the wireframe path, NOT the extruded path. If you want the bounds of the extruded path (relative to Node space), you should use the method getExtrudedContentBounds(). Additionally, the anchor point is relative to the content size not the extruded size. This means that the extruded path may be to the left of the origin even when the anchor is (0,0).

Because paths have width, it is natural to texture them. However, generally you will only want to create a path with the degenerate texture (to draw a solid, colored path). Hence, none of the static constructors take a texture. You are free to update the texture after creation, if you wish.

The polygon is specified in image coordinates. Image coordinates are different from texture coordinates. Their origin is at the bottom-left corner of the file, and each pixel is one unit. This makes specifying to polygon more natural for irregular shapes.

This means that a polygon with vertices (0,0), (width,0), (width,height), and (0,height) would be identical to a sprite node. However, a polygon with vertices (0,0), (2*width,0), (2*width,2*height), and (0,2*height) would tile the sprite (given the wrap settings) twice both horizontally and vertically. A uniform translation of the polygon (in contrast to the node itself) will not move the shape on the the screen. Instead, it will just change the part or the texture it uses.

For example, suppose the texture has given width and height. We have one polygon with vertices (0,0), (width/2,0), (width/2,height/2), and (0,height/2). We have another polygon with vertices (width/2,height/2), (width,height/2), (width,height), and (width/2,height). Both polygons would create a rectangle of size (width/2,height/2). centered at the node position. However, the first would use the bottom left part of the texture, while the second would use the top right.

You can disable these features at any time by setting the attribute absolute to true. Do this will place the polygon vertices in their absolute positions in Node space. This will also disable anchor functions (setting the anchor as the bottom left corner), since anchors do not make sense when we are drawing vertices directly into the coordinate space.

Constructor & Destructor Documentation

cugl::PathNode::PathNode ( )

Creates an empty path node.

You must initialize this PathNode before use.

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

cugl::PathNode::~PathNode ( )
inline

Releases all resources allocated with this node.

This will release, but not necessarily delete the associated texture. However, the polygon and drawing commands will be deleted and no longer safe to use.

Member Function Documentation

static std::shared_ptr<PathNode> cugl::PathNode::alloc ( )
inlinestatic

Returns an empty path node.

The underlying polygon is empty, and must be set via setPolygon().

Returns
an empty path node.
static std::shared_ptr<PathNode> cugl::PathNode::allocWithEllipse ( const Vec2 center,
const Size size,
float  stroke,
unsigned int  segments = PATH_SEGMENTS,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE 
)
inlinestatic

Returns a path node that is an ellipse with given the center and dimensions.

The path node will draw around the boundary of the ellipse, and will be CLOSED. PathNode objects share a single extruder, so this constructor is not thread safe.

Parameters
centerThe ellipse center point
sizeThe size of the ellipse
strokeThe width of the path
segmentsThe number of segments to use
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
Returns
a path node that is an ellipse with given the center and dimensions.
static std::shared_ptr<PathNode> cugl::PathNode::allocWithLine ( const Vec2 origin,
const Vec2 dest,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE 
)
inlinestatic

Returns a path node that is a line from origin to destination.

The path will be OPEN. PathNode objects share a single extruder, so this constructor is not thread safe.

Parameters
originThe line origin
destThe line destination
strokeThe width of the path
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
Returns
An autoreleased path node
static std::shared_ptr<PathNode> cugl::PathNode::allocWithPoly ( const Poly2 poly,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE 
)
inlinestatic

Returns a path node with the given polygon and stroke width.

The polygon will be extruded using the given polygon, assuming that it is a (connected) path. It will extrude that polygon with the given joint and cap. It will assume the polygon is closed if the number of indices is twice the number of vertices. PathNode objects share a single extruder, so this constructor is not thread safe.

Parameters
polyThe polygon to texture (expressed in image space)
strokeThe stroke width of the extruded path.
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
Returns
a path node with the given polygon and stroke width.
static std::shared_ptr<PathNode> cugl::PathNode::allocWithRect ( const Rect rect,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE 
)
inlinestatic

Returns a path node with the given rect and stroke width.

The rectangle will be converted into a Poly2, using the standard outline. This is the same as passing Poly2(rect,false). The traversal will be CLOSED. It will then be extruded with the current joint and cap. PathNode objects share a single extruder, so this constructor is not thread safe.

Parameters
rectThe rectangle for to texture.
strokeThe stroke width of the extruded path.
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
Returns
a path with the given rect and stroke width.
static std::shared_ptr<PathNode> cugl::PathNode::allocWithVertices ( const std::vector< Vec2 > &  vertices,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE,
bool  closed = true 
)
inlinestatic

Returns a path node with the given vertices and stroke width.

The polygon will be extruded using the given sequence of vertices. First it will traverse the vertices using either a closed or open traveral. Then it will extrude that polygon with the given joint and cap. PathNode objects share a single extruder, so this constructor is not thread safe.

Parameters
verticesThe vertices to texture (expressed in image space)
strokeThe stroke width of the extruded path.
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
closedThe whether the vertex path is open or closed.
Returns
a path node with the given vertices and stroke width.
virtual void cugl::PathNode::draw ( const std::shared_ptr< SpriteBatch > &  batch,
const Mat4 transform,
Color4  tint 
)
overridevirtual

Draws this Node via the given SpriteBatch.

This method only worries about drawing the current node. It does not attempt to render the children.

This is the method that you should override to implement your custom drawing code. You are welcome to use any OpenGL commands that you wish. You can even skip use of the SpriteBatch. However, if you do so, you must flush the SpriteBatch by calling end() at the start of the method. in addition, you should remember to call begin() at the start of the method.

This method provides the correct transformation matrix and tint color. You do not need to worry about whether the node uses relative color. This method is called by render() and these values are guaranteed to be correct. In addition, this method does not need to check for visibility, as it is guaranteed to only be called when the node is visible.

Parameters
batchThe SpriteBatch to draw with.
transformThe global transformation matrix.
tintThe tint to blend with the Node color.

Implements cugl::TexturedNode.

PathCap cugl::PathNode::getCap ( ) const
inline

Returns the cap shape at the ends of the path.

Returns
the cap shape at the ends of the path.
bool cugl::PathNode::getClosed ( ) const
inline

Returns whether the path is closed.

Returns
whether the path is closed.
const Rect& cugl::PathNode::getExtrudedContentBounds ( ) const
inline

Returns the bounding box of the extruded content.

This method is similar to getExtrudedContentSize() in that it returns the extra content size created by the stroke width, mitres, and caps. In addition, it returns the actual bounds of the path within Node space.

Unlike this method is unaffected by any transforms on this node. the bounding box of the extruded content.

float cugl::PathNode::getExtrudedContentHeight ( ) const
inline

Returns the height of the extruded content.

This method is an alternative to getContentHeight(). That method only returns the content height of the path polygon; it does not include the stroke width, mitres, and caps. This method includes the extra height of the extruded path.

Returns
the height of the extruded content.
const Size& cugl::PathNode::getExtrudedContentSize ( ) const
inline

Returns the size of the extruded content.

This method is an alternative to getContentSize(). That method only returns the content size of the path polygon; it does not include the stroke width, mitres, and caps. This method includes the extra size of the extruded path.

Returns
the size of the extruded content.
float cugl::PathNode::getExtrudedContentWidth ( ) const
inline

Returns the width of the extruded content.

This method is an alternative to getContentWidth(). That method only returns the content width of the path polygon; it does not include the stroke width, mitres, and caps. This method includes the extra width of the extruded path.

Returns
the width of the extruded content.
PathJoint cugl::PathNode::getJoint ( ) const
inline

Returns the joint type between path segments.

Returns
the joint type between path segments.
float cugl::PathNode::getStroke ( ) const
inline

Returns the stroke width of the path.

Returns
the stroke width of the path.
bool cugl::PathNode::initWithPoly ( const Poly2 poly,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE 
)

Intializes a path node with the given polygon and stroke width.

You do not need to set the texture; rendering this into a SpriteBatch will simply use the blank texture. Hence the wireframe will have a solid color.

The polygon will be extruded using the given polygon, assuming that it is a (connected) path. It will extrude that polygon with the given joint and cap. It will assume the polygon is closed if the number of indices is twice the number of vertices. PathNode objects share a single extruder, so this initializer is not thread safe.

Parameters
polyThe polygon to texture (expressed in image space)
strokeThe stroke width of the extruded path.
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
Returns
true if the path node is initialized properly, false otherwise.
bool cugl::PathNode::initWithVertices ( const std::vector< Vec2 > &  vertices,
float  stroke,
PathJoint  joint = PathJoint::NONE,
PathCap  cap = PathCap::NONE,
bool  closed = true 
)

Intializes a path with the given vertices and stroke width.

You do not need to set the texture; rendering this into a SpriteBatch will simply use the blank texture. Hence the wireframe will have a solid color.

The polygon will be extruded using the given sequence of vertices. First it will traverse the vertices using either a closed or open traveral. Then it will extrude that polygon with the given joint and cap. PathNode objects share a single extruder, so this initializer is not thread safe.

Parameters
verticesThe vertices to texture (expressed in image space)
strokeThe stroke width of the extruded path.
jointThe joint between extrusion line segments.
capThe end caps of the extruded paths.
closedThe whether the vertex path is open or closed.
Returns
true if the path node is initialized properly, false otherwise.
void cugl::PathNode::setCap ( PathCap  cap)

Sets the cap shape at the ends of the path.

This method affects the extruded polygon, but not the original path polygon.

Parameters
capThe cap shape at the ends of the path.
void cugl::PathNode::setClosed ( bool  closed)

Sets whether the path is closed.

This method affects both the extruded polygon and the original path polygon.

Parameters
closedWhether the path is closed.
void cugl::PathNode::setJoint ( PathJoint  joint)

Sets the joint type between path segments.

This method affects the extruded polygon, but not the original path polygon.

Parameters
jointThe joint type between path segments
virtual void cugl::PathNode::setPolygon ( const std::vector< Vec2 > &  vertices)
overridevirtual

Sets the polgon to the vertices expressed in texture space.

The polygon will be extruded using the given sequence of vertices. First it will traverse the vertices using the current traversal. Then it will extrude that polygon with the current joint and cap. PathNode objects share a single extruder, so this method is not thread safe.

Parameters
verticesThe vertices to texture

Implements cugl::TexturedNode.

virtual void cugl::PathNode::setPolygon ( const Poly2 poly)
overridevirtual

Sets the polygon to the given one in texture space.

This method will extrude that polygon with the current joint and cap. The polygon is assumed to be closed if the number of indices is twice the number of vertices. PathNode objects share a single extruder, so this method is not thread safe.

Parameters
polyThe polygon to texture

Reimplemented from cugl::TexturedNode.

virtual void cugl::PathNode::setPolygon ( const Rect rect)
overridevirtual

Sets the texture polygon to one equivalent to the given rect.

The rectangle will be converted into a Poly2, using the standard outline. This is the same as passing Poly2(rect,false). It will then be extruded with the current joint and cap. PathNode objects share a single extruder, so this method is not thread safe.

Parameters
rectThe rectangle to texture

Implements cugl::TexturedNode.

void cugl::PathNode::setStroke ( float  stroke)

Sets the stroke width of the path.

This method affects the extruded polygon, but not the original path polygon.

Parameters
strokeThe stroke width of the path

Member Data Documentation

bool cugl::PathNode::_closed
protected

Whether or not the polygon path is closed.

PathCap cugl::PathNode::_endcap
protected

The shape of the two end caps of the path.

Rect cugl::PathNode::_extrbounds
protected

The bounds of the extruded shape

PathExtruder cugl::PathNode::_extruder
staticprotected

An extruder for those incomplete polygons

Poly2 cugl::PathNode::_extrusion
protected

The extrusion polygon, when the stroke > 0

PathJoint cugl::PathNode::_joint
protected

The joint between segments of the path.

PathOutliner cugl::PathNode::_outliner
staticprotected

An outliner for those incomplete polygons

float cugl::PathNode::_stroke
protected

The stroke width of this path.


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