CUGL 1.1
Cornell University Game Library
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 <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;
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,
177  bool closed=true);
178 
199  bool initWithPoly(const Poly2& poly, float stroke,
201 
232  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
233 
234 
235 #pragma mark -
236 #pragma mark Static Constructors
237 
244  static std::shared_ptr<PathNode> alloc() {
245  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
246  return (node->init() ? node : nullptr);
247  }
248 
266  static std::shared_ptr<PathNode> allocWithVertices(const std::vector<Vec2>& vertices, float stroke,
267  PathJoint joint = PathJoint::NONE,
268  PathCap cap = PathCap::NONE, bool closed = true) {
269  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
270  return (node->initWithVertices(vertices,stroke,joint,cap,closed) ? node : nullptr);
271  }
272 
289  static std::shared_ptr<PathNode> allocWithPoly(const Poly2& poly, float stroke,
290  PathJoint joint = PathJoint::NONE,
291  PathCap cap = PathCap::NONE) {
292  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
293  return (node->initWithPoly(poly,stroke,joint,cap) ? node : nullptr);
294  }
295 
311  static std::shared_ptr<PathNode> allocWithRect(const Rect& rect, float stroke,
312  PathJoint joint = PathJoint::NONE,
313  PathCap cap = PathCap::NONE) {
314  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
315  return (node->initWithPoly(Poly2(rect,false),stroke,joint,cap) ? node : nullptr);
316  }
317 
332  static std::shared_ptr<PathNode> allocWithLine(const Vec2 &origin, const Vec2 &dest, float stroke,
333  PathJoint joint = PathJoint::NONE,
334  PathCap cap = PathCap::NONE) {
335  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
336  return (node->initWithPoly(Poly2::createLine(origin, dest),stroke,joint,cap) ? node : nullptr);
337  }
338 
355  static std::shared_ptr<PathNode> allocWithEllipse( const Vec2& center, const Size& size, float stroke,
356  unsigned int segments = PATH_SEGMENTS,
357  PathJoint joint = PathJoint::NONE,
358  PathCap cap = PathCap::NONE) {
359  std::shared_ptr<PathNode> node = std::make_shared<PathNode>();
360  return (node->initWithPoly(Poly2::createEllipse(center,size,segments,false),stroke,joint,cap) ?
361  node : nullptr);
362  }
363 
394  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
395  const std::shared_ptr<JsonValue> data) {
396  std::shared_ptr<PathNode> result = std::make_shared<PathNode>();
397  if (!result->initWithData(loader,data)) { result = nullptr; }
398  return std::dynamic_pointer_cast<Node>(result);
399  }
400 
401 
402 #pragma mark -
403 #pragma mark Attributes
404 
412  void setStroke(float stroke);
413 
419  float getStroke() const { return _stroke; }
420 
429  void setClosed(bool closed);
430 
436  bool getClosed() const { return _closed; }
437 
446  void setJoint(PathJoint joint);
447 
453  PathJoint getJoint() const { return _joint; }
454 
463  void setCap(PathCap cap);
464 
470  PathCap getCap() const { return _endcap; }
471 
472 #pragma mark -
473 #pragma mark Polygons
474 
484  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
485 
496  virtual void setPolygon(const Poly2& poly) override;
497 
508  virtual void setPolygon(const Rect& rect) override;
509 
520  float getExtrudedContentWidth() const { return _extrbounds.size.width; }
521 
532  float getExtrudedContentHeight() const { return _extrbounds.size.height; }
533 
544  const Size& getExtrudedContentSize() const { return _extrbounds.size; }
545 
559  const Rect& getExtrudedContentBounds() const { return _extrbounds; }
560 
561 
562 #pragma mark Rendering
563 
586  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
587 
588 
589 
590 #pragma mark Internal Helpers
591 private:
595  virtual void generateRenderData() override;
596 
600  void updateExtrusion();
601 
603  CU_DISALLOW_COPY_AND_ASSIGN(PathNode);
604 };
605 
606 }
607 
608 #endif /* __CU_PATH_NODE_H__ */
Definition: CUSize.h:57
Definition: CUTexturedNode.h:84
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:544
static std::shared_ptr< PathNode > allocWithPoly(const Poly2 &poly, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE)
Definition: CUPathNode.h:289
static std::shared_ptr< PathNode > alloc()
Definition: CUPathNode.h:244
const Rect & getExtrudedContentBounds() const
Definition: CUPathNode.h:559
void setStroke(float stroke)
bool initWithVertices(const std::vector< Vec2 > &vertices, float stroke, PathJoint joint=PathJoint::NONE, PathCap cap=PathCap::NONE, bool closed=true)
Definition: CUNode.h:92
~PathNode()
Definition: CUPathNode.h:152
Poly2 _extrusion
Definition: CUPathNode.h:119
float _stroke
Definition: CUPathNode.h:126
float getExtrudedContentHeight() const
Definition: CUPathNode.h:532
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:453
virtual void dispose() override
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
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUPathNode.h:394
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:332
Definition: CUSceneLoader.h:77
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:355
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
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override
void setJoint(PathJoint joint)
float getStroke() const
Definition: CUPathNode.h:419
Definition: CUColor4.h:1084
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:266
Definition: CUAction.h:51
bool getClosed() const
Definition: CUPathNode.h:436
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
float getExtrudedContentWidth() const
Definition: CUPathNode.h:520
Definition: CUMat4.h:92
PathCap getCap() const
Definition: CUPathNode.h:470
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:311