CUGL
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 zlib 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 "../math/CUPoly2.h"
50 #include "CUTexturedNode.h"
51 #include "../math/polygon/CUPathExtruder.h"
52 #include "../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;
131 
132 public:
133 #pragma mark -
134 #pragma mark Constructors
135 
143  PathNode();
144 
152  ~PathNode() { }
153 
175  bool initWithVertices(const std::vector<Vec2>& vertices, float stroke,
177  bool closed=true);
178 
199  bool initWithPoly(const Poly2& poly, float stroke,
201 
202 
203 #pragma mark Static Constructors
204 
211  static std::shared_ptr<PathNode> alloc() {
212  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
213  return (node->init() ? node : nullptr);
214  }
215 
233  static std::shared_ptr<PathNode> allocWithVertices(const std::vector<Vec2>& vertices, float stroke,
234  PathJoint joint = PathJoint::NONE,
235  PathCap cap = PathCap::NONE, bool closed = true) {
236  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
237  return (node->initWithVertices(vertices,stroke,joint,cap,closed) ? node : nullptr);
238  }
239 
256  static std::shared_ptr<PathNode> allocWithPoly(const Poly2& poly, float stroke,
257  PathJoint joint = PathJoint::NONE,
258  PathCap cap = PathCap::NONE) {
259  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
260  return (node->initWithPoly(poly,stroke,joint,cap) ? node : nullptr);
261  }
262 
278  static std::shared_ptr<PathNode> allocWithRect(const Rect& rect, float stroke,
279  PathJoint joint = PathJoint::NONE,
280  PathCap cap = PathCap::NONE) {
281  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
282  return (node->initWithPoly(Poly2(rect,false),stroke,joint,cap) ? node : nullptr);
283  }
284 
299  static std::shared_ptr<PathNode> allocWithLine(const Vec2 &origin, const Vec2 &dest, float stroke,
300  PathJoint joint = PathJoint::NONE,
301  PathCap cap = PathCap::NONE) {
302  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
303  return (node->initWithPoly(Poly2::createLine(origin, dest),stroke,joint,cap) ? node : nullptr);
304  }
305 
322  static std::shared_ptr<PathNode> allocWithEllipse( const Vec2& center, const Size& size, float stroke,
323  unsigned int segments = PATH_SEGMENTS,
324  PathJoint joint = PathJoint::NONE,
325  PathCap cap = PathCap::NONE) {
326  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
327  return (node->initWithPoly(Poly2::createEllipse(center,size,segments,false),stroke,joint,cap) ?
328  node : nullptr);
329  }
330 
331 
332 #pragma mark -
333 #pragma mark Attributes
334 
342  void setStroke(float stroke);
343 
349  float getStroke() const { return _stroke; }
350 
359  void setClosed(bool closed);
360 
366  bool getClosed() const { return _closed; }
367 
376  void setJoint(PathJoint joint);
377 
383  PathJoint getJoint() const { return _joint; }
384 
393  void setCap(PathCap cap);
394 
400  PathCap getCap() const { return _endcap; }
401 
402 #pragma mark -
403 #pragma mark Polygons
404 
414  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
415 
426  virtual void setPolygon(const Poly2& poly) override;
427 
438  virtual void setPolygon(const Rect& rect) override;
439 
450  float getExtrudedContentWidth() const { return _extrbounds.size.width; }
451 
462  float getExtrudedContentHeight() const { return _extrbounds.size.height; }
463 
474  const Size& getExtrudedContentSize() const { return _extrbounds.size; }
475 
489  const Rect& getExtrudedContentBounds() const { return _extrbounds; }
490 
491 
492 #pragma mark Rendering
493 
516  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
517 
518 
519 
520 #pragma mark Internal Helpers
521 private:
525  virtual void generateRenderData() override;
526 
530  void updateExtrusion();
531 
533  CU_DISALLOW_COPY_AND_ASSIGN(PathNode);
534 };
535 
536 }
537 
538 #endif /* __CU_PATH_NODE_H__ */
Definition: CUSize.h:57
Definition: CUTexturedNode.h:83
PathJoint
Definition: CUPathExtruder.h:65
Definition: CUPoly2.h:115
static PathOutliner _outliner
Definition: CUPathNode.h:116
Definition: CUVec2.h:61
const Size & getExtrudedContentSize() const
Definition: CUPathNode.h:474
static std::shared_ptr< PathNode > allocWithPoly(const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:256
static std::shared_ptr< PathNode > alloc()
Definition: CUPathNode.h:211
const Rect & getExtrudedContentBounds() const
Definition: CUPathNode.h:489
void setStroke(float stroke)
bool initWithVertices(const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
~PathNode()
Definition: CUPathNode.h:152
Poly2 _extrusion
Definition: CUPathNode.h:119
float _stroke
Definition: CUPathNode.h:126
float getExtrudedContentHeight() const
Definition: CUPathNode.h:462
Rect _extrbounds
Definition: CUPathNode.h:121
Definition: CUPathNode.h:110
PathCap
Definition: CUPathExtruder.h:85
float width
Definition: CUSize.h:61
static Poly2 createLine(const Vec2 &origin, const Vec2 &dest)
PathJoint getJoint() const
Definition: CUPathNode.h:383
PathCap _endcap
Definition: CUPathNode.h:130
Definition: CUPathExtruder.h:129
void setClosed(bool closed)
float height
Definition: CUSize.h:63
PathJoint _joint
Definition: CUPathNode.h:128
Definition: CURect.h:45
static PathExtruder _extruder
Definition: CUPathNode.h:114
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:299
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:322
bool _closed
Definition: CUPathNode.h:124
bool initWithPoly(const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
Size size
Definition: CURect.h:51
void setJoint(PathJoint joint)
float getStroke() const
Definition: CUPathNode.h:349
Definition: CUColor4.h:1104
Definition: CUPathOutliner.h:78
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:233
Definition: CUAnimationNode.h:52
bool getClosed() const
Definition: CUPathNode.h:366
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
float getExtrudedContentWidth() const
Definition: CUPathNode.h:450
Definition: CUMat4.h:92
PathCap getCap() const
Definition: CUPathNode.h:400
void setCap(PathCap cap)
static Poly2 createEllipse(const Vec2 &center, const Size &size, unsigned int segments, bool solid=true)
static std::shared_ptr< PathNode > allocWithRect(const Rect &rect, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:278