CUGL
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 zlib 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 "../math/CUPoly2.h"
49 #include "CUTexturedNode.h"
50 #include "../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 
115 
116 public:
117 #pragma mark -
118 #pragma mark Constructors
119 
128  _classname = "WireNode";
129  _name = "WireNode";
130  }
131 
139  ~WireNode() { }
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 
182 #pragma mark -
183 #pragma mark Static Constructors
184 
191  static std::shared_ptr<WireNode> alloc() {
192  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
193  return (node->init() ? node : nullptr);
194  }
195 
208  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices) {
209  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
210  return (node->init(vertices) ? node : nullptr);
211  }
212 
225  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices,
226  PathTraversal traversal) {
227  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
228  return (node->initWithVertices(vertices,traversal) ? node : nullptr);
229  }
230 
242  static std::shared_ptr<WireNode> allocWithPoly(const Poly2& poly) {
243  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
244  return (node->init(poly) ? node : nullptr);
245  }
246 
256  static std::shared_ptr<WireNode> allocWithRect(const Rect& rect) {
257  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
258  return (node->init(rect) ? node : nullptr);
259  }
260 
269  static std::shared_ptr<WireNode> allocWithLine(const Vec2 &origin, const Vec2 &dest) {
270  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
271  return (node->initWithLine(origin,dest) ? node : nullptr);
272  }
273 
285  static std::shared_ptr<WireNode> allocWithEllipse(const Vec2& center, const Size& size,
286  unsigned int segments = WIRE_SEGMENTS) {
287  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
288  return (node->initWithEllipse(center,size,segments) ? node : nullptr);
289  }
290 
291 #pragma mark -
292 #pragma mark Attributes
293 
302  void setTraversal(PathTraversal traversal);
303 
313 
324  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
325 
335  void setPolygon(const std::vector<Vec2>& vertices, PathTraversal traversal);
336 
345  virtual void setPolygon(const Poly2& poly) override;
346 
356  virtual void setPolygon(const Rect& rect) override;
357 
366  void setLine(const Vec2 &origin, const Vec2 &dest);
367 
378  void setEllipse(const Vec2& center, const Size& size, unsigned int segments = WIRE_SEGMENTS);
379 
380 #pragma mark -
381 #pragma mark Rendering
382 
405  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
406 
407 
408 private:
410  CU_DISALLOW_COPY_AND_ASSIGN(WireNode);
411 };
412 
413 }
414 
415 
416 #endif /* __CU_WIRE_NODE_H__ */
Definition: CUSize.h:57
Definition: CUWireNode.h:107
static std::shared_ptr< WireNode > allocWithRect(const Rect &rect)
Definition: CUWireNode.h:256
bool initWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
Definition: CUTexturedNode.h:83
void setLine(const Vec2 &origin, const Vec2 &dest)
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
Definition: CUPoly2.h:115
Definition: CUVec2.h:61
std::string _classname
Definition: CUTexturedNode.h:87
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
Definition: CUWireNode.h:225
~WireNode()
Definition: CUWireNode.h:139
PathTraversal getTraversal() const
Definition: CUWireNode.h:312
void setTraversal(PathTraversal traversal)
static PathOutliner _outliner
Definition: CUWireNode.h:111
void setEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
static std::shared_ptr< WireNode > allocWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
Definition: CUWireNode.h:285
Definition: CURect.h:45
PathTraversal _traversal
Definition: CUWireNode.h:114
static std::shared_ptr< WireNode > allocWithLine(const Vec2 &origin, const Vec2 &dest)
Definition: CUWireNode.h:269
WireNode()
Definition: CUWireNode.h:127
static std::shared_ptr< WireNode > alloc()
Definition: CUWireNode.h:191
bool initWithLine(const Vec2 &origin, const Vec2 &dest)
std::string _name
Definition: CUNode.h:187
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
Definition: CUColor4.h:1104
PathTraversal
Definition: CUPathOutliner.h:49
Definition: CUPathOutliner.h:78
static std::shared_ptr< WireNode > allocWithPoly(const Poly2 &poly)
Definition: CUWireNode.h:242
Definition: CUAnimationNode.h:52
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices)
Definition: CUWireNode.h:208
bool initWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
Definition: CUMat4.h:92