CUGL 1.1
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 <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 
115 
116 public:
117 #pragma mark -
118 #pragma mark Constructors
119 
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 
210  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
211 
212 
213 #pragma mark -
214 #pragma mark Static Constructors
215 
222  static std::shared_ptr<WireNode> alloc() {
223  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
224  return (node->init() ? node : nullptr);
225  }
226 
239  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices) {
240  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
241  return (node->init(vertices) ? node : nullptr);
242  }
243 
256  static std::shared_ptr<WireNode> allocWithVertices(const std::vector<Vec2>& vertices,
257  PathTraversal traversal) {
258  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
259  return (node->initWithVertices(vertices,traversal) ? node : nullptr);
260  }
261 
273  static std::shared_ptr<WireNode> allocWithPoly(const Poly2& poly) {
274  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
275  return (node->init(poly) ? node : nullptr);
276  }
277 
287  static std::shared_ptr<WireNode> allocWithRect(const Rect& rect) {
288  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
289  return (node->init(rect) ? node : nullptr);
290  }
291 
300  static std::shared_ptr<WireNode> allocWithLine(const Vec2 &origin, const Vec2 &dest) {
301  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
302  return (node->initWithLine(origin,dest) ? node : nullptr);
303  }
304 
316  static std::shared_ptr<WireNode> allocWithEllipse(const Vec2& center, const Size& size,
317  unsigned int segments = WIRE_SEGMENTS) {
318  std::shared_ptr<WireNode> node = std::make_shared<WireNode>();
319  return (node->initWithEllipse(center,size,segments) ? node : nullptr);
320  }
321 
350  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
351  const std::shared_ptr<JsonValue> data) {
352  std::shared_ptr<WireNode> result = std::make_shared<WireNode>();
353  if (!result->initWithData(loader,data)) { result = nullptr; }
354  return std::dynamic_pointer_cast<Node>(result);
355  }
356 
357 #pragma mark -
358 #pragma mark Attributes
359 
368  void setTraversal(PathTraversal traversal);
369 
379 
390  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
391 
401  void setPolygon(const std::vector<Vec2>& vertices, PathTraversal traversal);
402 
411  virtual void setPolygon(const Poly2& poly) override;
412 
422  virtual void setPolygon(const Rect& rect) override;
423 
432  void setLine(const Vec2 &origin, const Vec2 &dest);
433 
444  void setEllipse(const Vec2& center, const Size& size, unsigned int segments = WIRE_SEGMENTS);
445 
446 #pragma mark -
447 #pragma mark Rendering
448 
471  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
472 
473 
474 private:
476  CU_DISALLOW_COPY_AND_ASSIGN(WireNode);
477 };
478 
479 }
480 
481 
482 #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:287
bool initWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
Definition: CUTexturedNode.h:84
void setLine(const Vec2 &origin, const Vec2 &dest)
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUWireNode.h:350
Definition: CUPoly2.h:115
Definition: CUVec2.h:61
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override
std::string _classname
Definition: CUTexturedNode.h:88
Definition: CUNode.h:92
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
Definition: CUWireNode.h:256
~WireNode()
Definition: CUWireNode.h:139
PathTraversal getTraversal() const
Definition: CUWireNode.h:378
void setTraversal(PathTraversal traversal)
static PathOutliner _outliner
Definition: CUWireNode.h:111
void setEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
virtual void dispose() override
static std::shared_ptr< WireNode > allocWithEllipse(const Vec2 &center, const Size &size, unsigned int segments=WIRE_SEGMENTS)
Definition: CUWireNode.h:316
Definition: CURect.h:45
PathTraversal _traversal
Definition: CUWireNode.h:114
Definition: CUSceneLoader.h:77
static std::shared_ptr< WireNode > allocWithLine(const Vec2 &origin, const Vec2 &dest)
Definition: CUWireNode.h:300
WireNode()
Definition: CUWireNode.h:127
static std::shared_ptr< WireNode > alloc()
Definition: CUWireNode.h:222
bool initWithLine(const Vec2 &origin, const Vec2 &dest)
std::string _name
Definition: CUNode.h:189
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
Definition: CUColor4.h:1084
PathTraversal
Definition: CUPathOutliner.h:49
Definition: CUPathOutliner.h:78
static std::shared_ptr< WireNode > allocWithPoly(const Poly2 &poly)
Definition: CUWireNode.h:273
Definition: CUAction.h:51
static std::shared_ptr< WireNode > allocWithVertices(const std::vector< Vec2 > &vertices)
Definition: CUWireNode.h:239
bool initWithVertices(const std::vector< Vec2 > &vertices, PathTraversal traversal)
Definition: CUMat4.h:92