CUGL 1.3
Cornell University Game Library
CUWireNode.h
1 //
2 // CUWireNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a scene graph node that supports wireframes. The
6 // primary use case is to have a node that outlines physics bodies.
7 //
8 // This class is loosely coupled with PathOutliner. You can use PathOutliner
9 // independent of the WireNode, but all functionality is present in this class.
10 //
11 // This class uses our standard shared-pointer architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 // CUGL MIT License:
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // Author: Walker White
42 // Version: 6/27/16
43 
44 #ifndef __CU_WIRE_NODE_H__
45 #define __CU_WIRE_NODE_H__
46 
47 #include <string>
48 #include <cugl/2d/CUTexturedNode.h>
49 #include <cugl/math/CUPoly2.h>
50 #include <cugl/math/polygon/CUPathOutliner.h>
51 
53 #define WIRE_SEGMENTS 8
54 
55 namespace cugl {
56 
107 class WireNode : public TexturedNode {
108 #pragma mark Values
109 protected:
112 
114  PathTraversal _traversal;
115 
116 public:
117 #pragma mark -
118 #pragma mark Constructors
119 
127  WireNode() : TexturedNode(), _traversal(PathTraversal::CLOSED) {
128  _classname = "WireNode";
129  _name = "WireNode";
130  }
131 
140 
157  bool initWithVertices(const std::vector<Vec2>& vertices, PathTraversal traversal);
158 
167  bool initWithLine(const Vec2 &origin, const Vec2 &dest);
168 
180  bool initWithEllipse(const Vec2& center, const Size& size, unsigned int segments = WIRE_SEGMENTS);
181 
205  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
206 
207 
208 #pragma mark -
209 #pragma mark Static Constructors
210 
217  static std::shared_ptr<WireNode> alloc() {
218  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
219  return (node->init() ? node : nullptr);
220  }
221 
234  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices) {
235  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
236  return (node->init(vertices) ? node : nullptr);
237  }
238 
251  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices,
252  PathTraversal traversal) {
253  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
254  return (node->initWithVertices(vertices,traversal) ? node : nullptr);
255  }
256 
268  static std::shared_ptr<WireNode> allocWithPoly(const Poly2& poly) {
269  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
270  return (node->init(poly) ? node : nullptr);
271  }
272 
282  static std::shared_ptr<WireNode> allocWithRect(const Rect& rect) {
283  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
284  return (node->init(rect) ? node : nullptr);
285  }
286 
295  static std::shared_ptr<WireNode> allocWithLine(const Vec2 &origin, const Vec2 &dest) {
296  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
297  return (node->initWithLine(origin,dest) ? node : nullptr);
298  }
299 
311  static std::shared_ptr<WireNode> allocWithEllipse(const Vec2& center, const Size& size,
312  unsigned int segments = WIRE_SEGMENTS) {
313  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
314  return (node->initWithEllipse(center,size,segments) ? node : nullptr);
315  }
316 
340  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
341  const std::shared_ptr<JsonValue>& data) {
342  std::shared_ptr<WireNode> result = std::make_shared<WireNode>();
343  if (!result->initWithData(loader,data)) { result = nullptr; }
344  return std::dynamic_pointer_cast<Node>(result);
345  }
346 
347 #pragma mark -
348 #pragma mark Attributes
349 
358  void setTraversal(PathTraversal traversal);
359 
368  PathTraversal getTraversal() const { return _traversal; }
369 
380  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
381 
392  void setPolygon(const std::vector<Vec2>& vertices, PathTraversal traversal);
393 
402  virtual void setPolygon(const Poly2& poly) override;
403 
413  virtual void setPolygon(const Rect& rect) override;
414 
423  void setLine(const Vec2 &origin, const Vec2 &dest);
424 
435  void setEllipse(const Vec2& center, const Size& size, unsigned int segments = WIRE_SEGMENTS);
436 
437 #pragma mark -
438 #pragma mark Rendering
439 
462  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
463 
464 
465 private:
467  CU_DISALLOW_COPY_AND_ASSIGN(WireNode);
468 };
469 
470 }
471 
472 
473 #endif /* __CU_WIRE_NODE_H__ */
cugl::Node::_name
std::string _name
Definition: CUNode.h:189
cugl::WireNode::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUWireNode.h:340
cugl::PathOutliner
Definition: CUPathOutliner.h:79
cugl::WireNode::getTraversal
PathTraversal getTraversal() const
Definition: CUWireNode.h:368
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::WireNode::allocWithLine
static std::shared_ptr< WireNode > allocWithLine(const Vec2 &origin, const Vec2 &dest)
Definition: CUWireNode.h:295
cugl::WireNode::setEllipse
void setEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
cugl::WireNode::allocWithVertices
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
Definition: CUWireNode.h:251
cugl::TexturedNode::dispose
virtual void dispose() override
cugl::WireNode::_outliner
static PathOutliner _outliner
Definition: CUWireNode.h:111
cugl::TexturedNode::_classname
std::string _classname
Definition: CUTexturedNode.h:88
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::WireNode::setLine
void setLine(const Vec2 &origin, const Vec2 &dest)
cugl::WireNode::initWithEllipse
bool initWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::WireNode::allocWithRect
static std::shared_ptr< WireNode > allocWithRect(const Rect &rect)
Definition: CUWireNode.h:282
cugl::WireNode::WireNode
WireNode()
Definition: CUWireNode.h:127
cugl::WireNode::_traversal
PathTraversal _traversal
Definition: CUWireNode.h:114
cugl::WireNode::alloc
static std::shared_ptr< WireNode > alloc()
Definition: CUWireNode.h:217
cugl::WireNode::setPolygon
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
cugl::Poly2
Definition: CUPoly2.h:109
cugl::WireNode::allocWithEllipse
static std::shared_ptr< WireNode > allocWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
Definition: CUWireNode.h:311
cugl::Vec2
Definition: CUVec2.h:61
cugl::WireNode::initWithLine
bool initWithLine(const Vec2 &origin, const Vec2 &dest)
cugl::WireNode::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::WireNode::~WireNode
~WireNode()
Definition: CUWireNode.h:139
cugl::WireNode::initWithVertices
bool initWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
cugl::WireNode
Definition: CUWireNode.h:107
cugl::WireNode::initWithData
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::TexturedNode
Definition: CUTexturedNode.h:84
cugl::WireNode::setTraversal
void setTraversal(PathTraversal traversal)
cugl::WireNode::allocWithPoly
static std::shared_ptr< WireNode > allocWithPoly(const Poly2 &poly)
Definition: CUWireNode.h:268
cugl::WireNode::allocWithVertices
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices)
Definition: CUWireNode.h:234