CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPathNode.h
1 //
2 // CUPathNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a scene graph node that supports extruded paths. When
6 // extruding paths, this node is better than PolygonNode, because it will align
7 // the extruded path to the original wireframe.
8 //
9 // This class is loosely coupled with PathExtruder. You can use PathExtruder
10 // independent of the PathNode, but all functionality is present in this class.
11 //
12 // This class uses our standard shared-pointer architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // CUGL MIT License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Walker White
43 // Version: 6/27/16
44 
45 #ifndef __CU_PATH_NODE_H__
46 #define __CU_PATH_NODE_H__
47 
48 #include <string>
49 #include <cugl/2d/CUTexturedNode.h>
50 #include <cugl/math/CUPoly2.h>
51 #include <cugl/math/polygon/CUPathExtruder.h>
52 #include <cugl/math/polygon/CUPathOutliner.h>
53 
55 #define PATH_SEGMENTS 8
56 
57 namespace cugl {
58 
110 class PathNode : public TexturedNode {
111 #pragma mark Values
112 protected:
117 
122 
124  bool _closed;
126  float _stroke;
128  PathJoint _joint;
130  PathCap _endcap;
131 
132 public:
133 #pragma mark -
134 #pragma mark Constructors
135 
143  PathNode();
144 
153 
175  bool initWithVertices(const std::vector<Vec2>& vertices, float stroke,
176  PathJoint joint = PathJoint::NONE, PathCap cap = PathCap::NONE,
177  bool closed=true);
178 
199  bool initWithPoly(const Poly2& poly, float stroke,
200  PathJoint joint = PathJoint::NONE, PathCap cap = PathCap::NONE);
201 
227  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
228 
229 
230 #pragma mark -
231 #pragma mark Static Constructors
232 
239  static std::shared_ptr<PathNode> alloc() {
240  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
241  return (node->init() ? node : nullptr);
242  }
243 
261  static std::shared_ptr<PathNode> allocWithVertices(const std::vector<Vec2>& vertices, float stroke,
262  PathJoint joint = PathJoint::NONE,
263  PathCap cap = PathCap::NONE, bool closed = true) {
264  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
265  return (node->initWithVertices(vertices,stroke,joint,cap,closed) ? node : nullptr);
266  }
267 
284  static std::shared_ptr<PathNode> allocWithPoly(const Poly2& poly, float stroke,
285  PathJoint joint = PathJoint::NONE,
286  PathCap cap = PathCap::NONE) {
287  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
288  return (node->initWithPoly(poly,stroke,joint,cap) ? node : nullptr);
289  }
290 
306  static std::shared_ptr<PathNode> allocWithRect(const Rect& rect, float stroke,
307  PathJoint joint = PathJoint::NONE,
308  PathCap cap = PathCap::NONE) {
309  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
310  return (node->initWithPoly(Poly2(rect,false),stroke,joint,cap) ? node : nullptr);
311  }
312 
327  static std::shared_ptr<PathNode> allocWithLine(const Vec2 &origin, const Vec2 &dest, float stroke,
328  PathJoint joint = PathJoint::NONE,
329  PathCap cap = PathCap::NONE) {
330  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
331  return (node->initWithPoly(Poly2::createLine(origin, dest),stroke,joint,cap) ? node : nullptr);
332  }
333 
350  static std::shared_ptr<PathNode> allocWithEllipse( const Vec2& center, const Size& size, float stroke,
351  unsigned int segments = PATH_SEGMENTS,
352  PathJoint joint = PathJoint::NONE,
353  PathCap cap = PathCap::NONE) {
354  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
355  return (node->initWithPoly(Poly2::createEllipse(center,size,segments,false),stroke,joint,cap) ?
356  node : nullptr);
357  }
358 
384  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
385  const std::shared_ptr<JsonValue>& data) {
386  std::shared_ptr<PathNode> result = std::make_shared<PathNode>();
387  if (!result->initWithData(loader,data)) { result = nullptr; }
388  return std::dynamic_pointer_cast<Node>(result);
389  }
390 
391 
392 #pragma mark -
393 #pragma mark Attributes
394 
402  void setStroke(float stroke);
403 
409  float getStroke() const { return _stroke; }
410 
419  void setClosed(bool closed);
420 
426  bool getClosed() const { return _closed; }
427 
436  void setJoint(PathJoint joint);
437 
443  PathJoint getJoint() const { return _joint; }
444 
453  void setCap(PathCap cap);
454 
460  PathCap getCap() const { return _endcap; }
461 
462 #pragma mark -
463 #pragma mark Polygons
464 
474  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
475 
486  virtual void setPolygon(const Poly2& poly) override;
487 
498  virtual void setPolygon(const Rect& rect) override;
499 
510  float getExtrudedContentWidth() const { return _extrbounds.size.width; }
511 
523 
534  const Size& getExtrudedContentSize() const { return _extrbounds.size; }
535 
549  const Rect& getExtrudedContentBounds() const { return _extrbounds; }
550 
551 
552 #pragma mark Rendering
553 
576  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
577 
578 
579 
580 #pragma mark Internal Helpers
581 private:
585  virtual void generateRenderData() override;
586 
590  void updateExtrusion();
591 
593  CU_DISALLOW_COPY_AND_ASSIGN(PathNode);
594 };
595 
596 }
597 
598 #endif /* __CU_PATH_NODE_H__ */
cugl::PathOutliner
Definition: CUPathOutliner.h:79
cugl::PathNode::allocWithLine
static std::shared_ptr< PathNode > allocWithLine(const Vec2 &origin, const Vec2 &dest, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:327
cugl::PathNode::setStroke
void setStroke(float stroke)
cugl::PathNode::getClosed
bool getClosed() const
Definition: CUPathNode.h:426
cugl::PathNode::setPolygon
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
cugl::PathNode::setClosed
void setClosed(bool closed)
cugl::PathNode::setCap
void setCap(PathCap cap)
cugl::PathNode::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUPathNode.h:384
cugl::PathNode::getExtrudedContentSize
const Size & getExtrudedContentSize() const
Definition: CUPathNode.h:534
cugl::PathNode::getCap
PathCap getCap() const
Definition: CUPathNode.h:460
cugl::Poly2::createEllipse
static Poly2 createEllipse(const Vec2 &center, const Size &size, unsigned int segments, bool solid=true)
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::PathNode::PathNode
PathNode()
cugl::PathNode::_outliner
static PathOutliner _outliner
Definition: CUPathNode.h:116
cugl::PathNode::getJoint
PathJoint getJoint() const
Definition: CUPathNode.h:443
cugl::PathNode::getExtrudedContentHeight
float getExtrudedContentHeight() const
Definition: CUPathNode.h:522
cugl::TexturedNode::dispose
virtual void dispose() override
cugl::PathNode
Definition: CUPathNode.h:110
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::PathNode::allocWithVertices
static std::shared_ptr< PathNode > allocWithVertices(const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
Definition: CUPathNode.h:261
cugl::PathExtruder
Definition: CUPathExtruder.h:129
cugl::PathNode::getStroke
float getStroke() const
Definition: CUPathNode.h:409
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::PathNode::_extrusion
Poly2 _extrusion
Definition: CUPathNode.h:119
cugl::PathNode::_extrbounds
Rect _extrbounds
Definition: CUPathNode.h:121
cugl::PathNode::initWithPoly
bool initWithPoly(const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
cugl::PathNode::setJoint
void setJoint(PathJoint joint)
cugl::PathNode::_endcap
PathCap _endcap
Definition: CUPathNode.h:130
cugl::PathNode::alloc
static std::shared_ptr< PathNode > alloc()
Definition: CUPathNode.h:239
cugl::PathNode::_joint
PathJoint _joint
Definition: CUPathNode.h:128
cugl::Poly2
Definition: CUPoly2.h:109
cugl::Vec2
Definition: CUVec2.h:61
cugl::Poly2::createLine
static Poly2 createLine(const Vec2 &origin, const Vec2 &dest)
cugl::PathNode::_stroke
float _stroke
Definition: CUPathNode.h:126
cugl::PathNode::initWithData
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::PathNode::allocWithPoly
static std::shared_ptr< PathNode > allocWithPoly(const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:284
cugl::PathNode::getExtrudedContentWidth
float getExtrudedContentWidth() const
Definition: CUPathNode.h:510
cugl::Rect::size
Size size
Definition: CURect.h:51
cugl::PathNode::_extruder
static PathExtruder _extruder
Definition: CUPathNode.h:114
cugl::PathNode::_closed
bool _closed
Definition: CUPathNode.h:124
cugl::PathNode::~PathNode
~PathNode()
Definition: CUPathNode.h:152
cugl::TexturedNode
Definition: CUTexturedNode.h:84
cugl::PathNode::allocWithRect
static std::shared_ptr< PathNode > allocWithRect(const Rect &rect, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:306
cugl::PathNode::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::PathNode::initWithVertices
bool initWithVertices(const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
cugl::PathNode::allocWithEllipse
static std::shared_ptr< PathNode > allocWithEllipse(const Vec2 &center, const Size &size, float stroke, unsigned int segments=PATH_SEGMENTS, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:350
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::PathNode::getExtrudedContentBounds
const Rect & getExtrudedContentBounds() const
Definition: CUPathNode.h:549