CUGL
Cornell University Game Library
CUObstacleSelector.h
1 //
2 // CUObstacleSelector.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class implements a selection tool for dragging physics objects with a
6 // mouse. It is essentially an instance of b2MouseJoint, but with an API that
7 // makes it a lot easier to use. As with all instances of b2MouseJoint, there
8 // will be some lag in the drag (though this is true on touch devices in general).
9 // You can adjust the degree of this lag by adjusting the force. However,
10 // larger forces can cause artifacts when dragging an obstacle through other
11 // obstacles.
12 //
13 // This class uses our standard shared-pointer architecture.
14 //
15 // 1. The constructor does not perform any initialization; it just sets all
16 // attributes to their defaults.
17 //
18 // 2. All initialization takes place via init methods, which can fail if an
19 // object is initialized more than once.
20 //
21 // 3. All allocation takes place via static constructors which return a shared
22 // pointer.
23 //
24 // CUGL zlib License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
44 //
45 // Author: Walker White
46 // Version: 11/6/16
47 //
48 #ifndef __CU_OBSTACLE_SELECTOR_H__
49 #define __CU_OBSTACLE_SELECTOR_H__
50 
51 #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
52 #include <Box2D/Dynamics/b2Fixture.h>
53 //#include "CUObstacleWorld.h"
54 
56 #define DEFAULT_MSIZE 0.2f
57 
58 #define DEFAULT_FREQUENCY 10.0f
59 
60 #define DEFAULT_DAMPING 0.7f
61 
62 #define DEFAULT_FORCE 1000.0f
63 
64 namespace cugl {
65 
66 // Forward reference to the ObstacleWorld
67 class ObstacleWorld;
68 
69 #pragma mark -
70 #pragma mark Obstacle Selector
71 
85 protected:
87  std::shared_ptr<ObstacleWorld> _controller;
88 
94  float _force;
95 
97  b2Fixture* _selection;
99  b2Body* _ground;
100 
102  b2MouseJointDef _jointDef;
104  b2MouseJoint* _mouseJoint;
105 
107  std::shared_ptr<Node> _scene;
109  std::shared_ptr<WireNode> _hatch;
111  std::shared_ptr<WireNode> _connect;
113  //Vec2 _drawScale;
115  bool _dvisible;
118 
119 #pragma mark -
120 #pragma mark Scene Graph Internals
121 
128  void resetDebug();
129 
137  void updateDebug();
138 
147  void updateTarget(Obstacle* obstacle);
148 
154  Poly2 hatchPoly() const;
155 
156 public:
157 #pragma mark Constructors
158 
168  _controller(nullptr), _selection(nullptr), _ground(nullptr),
169  _mouseJoint(nullptr), _scene(nullptr), _hatch(nullptr),
170  _connect(nullptr) {}
171 
176 
182  void dispose();
183 
196  bool init(const std::shared_ptr<ObstacleWorld>& world) {
197  return init(world, Size(DEFAULT_MSIZE, DEFAULT_MSIZE));
198  }
199 
212  bool init(const std::shared_ptr<ObstacleWorld>& world, const Size& mouseSize);
213 
214 
215 #pragma mark Static Constructors
216 
228  static std::shared_ptr<ObstacleSelector> alloc(const std::shared_ptr<ObstacleWorld>& world) {
229  std::shared_ptr<ObstacleSelector> result = std::make_shared<ObstacleSelector>();
230  return (result->init(world) ? result : nullptr);
231  }
232 
245  static std::shared_ptr<ObstacleSelector> alloc(const std::shared_ptr<ObstacleWorld>& world,
246  const Size& mouseSize) {
247  std::shared_ptr<ObstacleSelector> result = std::make_shared<ObstacleSelector>();
248  return (result->init(world,mouseSize) ? result : nullptr);
249  }
250 
251 #pragma mark Positional Methods
252 
257  const Vec2& getPosition() { return _position; }
258 
265  void setPosition(float x, float y);
266 
272  void setPosition(const Vec2& pos) { setPosition(pos.x,pos.y); }
273 
274 
275 
276 #pragma mark Selection Methods
277 
282  bool isSelected() const { return _selection != nullptr; }
283 
293  bool select();
294 
300  void deselect();
301 
312 
323  bool onQuery(b2Fixture* fixture);
324 
325 
326 #pragma mark Attribute Properties
327 
335  float getFrequency() const { return _jointDef.frequencyHz; }
336 
345  void setFrequency(float speed) { _jointDef.frequencyHz = speed; }
346 
355  float getDamping() const { return _jointDef.dampingRatio; }
356 
365  void setDamping(float ratio) { _jointDef.dampingRatio = ratio; }
366 
375  float getForce() const { return _force; }
376 
385  void setForce(float force) { _force = force; }
386 
397  const Size& getMouseSize() const { return _size; }
398 
409  void setMouseSize(const Size& size) { _size = size; resetDebug(); }
410 
411 
412 #pragma mark -
413 #pragma mark Scene Graph Methods
414 
422  Color4 getDebugColor() const { return _dcolor; }
423 
432  void setDebugColor(Color4 color) {
433  _dcolor = color;
434  if (_hatch) { _hatch->setColor(color); }
435  if (_connect) { _connect->setColor(color); }
436  }
437 
458  Node* getDebugScene() const { return _scene.get(); }
459 
481  void setDebugScene(const std::shared_ptr<Node>& node);
482 
491  void setVisible(bool flag) {
492  _dvisible = flag;
493  if (_hatch) { _hatch->setVisible(flag); }
494  if (_connect) { _connect->setVisible(flag); }
495  }
496 
505  bool isVisible() { return _dvisible; }
506 
515  bool hasDebug() { return _hatch != nullptr; }
516 };
517 
518 }
519 #endif /* __CU_OBSTACLE_SELECTOR_H__ */
Definition: CUSize.h:57
Color4 _dcolor
Definition: CUObstacleSelector.h:117
float x
Definition: CUVec2.h:66
float y
Definition: CUVec2.h:68
Definition: CUPoly2.h:115
void setDebugColor(Color4 color)
Definition: CUObstacleSelector.h:432
Definition: CUVec2.h:61
Size _size
Definition: CUObstacleSelector.h:92
Obstacle * getObstacle()
b2Fixture * _selection
Definition: CUObstacleSelector.h:97
std::shared_ptr< WireNode > _connect
Definition: CUObstacleSelector.h:111
static std::shared_ptr< ObstacleSelector > alloc(const std::shared_ptr< ObstacleWorld > &world)
Definition: CUObstacleSelector.h:228
Definition: CUNode.h:92
void setForce(float force)
Definition: CUObstacleSelector.h:385
static std::shared_ptr< ObstacleSelector > alloc(const std::shared_ptr< ObstacleWorld > &world, const Size &mouseSize)
Definition: CUObstacleSelector.h:245
b2Body * _ground
Definition: CUObstacleSelector.h:99
float _force
Definition: CUObstacleSelector.h:94
b2MouseJoint * _mouseJoint
Definition: CUObstacleSelector.h:104
std::shared_ptr< ObstacleWorld > _controller
Definition: CUObstacleSelector.h:87
bool _dvisible
Definition: CUObstacleSelector.h:115
Color4 getDebugColor() const
Definition: CUObstacleSelector.h:422
bool isVisible()
Definition: CUObstacleSelector.h:505
void setVisible(bool flag)
Definition: CUObstacleSelector.h:491
void setFrequency(float speed)
Definition: CUObstacleSelector.h:345
std::shared_ptr< Node > _scene
Definition: CUObstacleSelector.h:107
Vec2 _position
Definition: CUObstacleSelector.h:90
bool onQuery(b2Fixture *fixture)
Poly2 hatchPoly() const
bool init(const std::shared_ptr< ObstacleWorld > &world)
Definition: CUObstacleSelector.h:196
float getDamping() const
Definition: CUObstacleSelector.h:355
Definition: CUObstacleSelector.h:84
float getForce() const
Definition: CUObstacleSelector.h:375
Node * getDebugScene() const
Definition: CUObstacleSelector.h:458
b2MouseJointDef _jointDef
Definition: CUObstacleSelector.h:102
void updateTarget(Obstacle *obstacle)
Definition: CUObstacle.h:76
bool hasDebug()
Definition: CUObstacleSelector.h:515
void setPosition(float x, float y)
std::shared_ptr< WireNode > _hatch
Definition: CUObstacleSelector.h:109
void setMouseSize(const Size &size)
Definition: CUObstacleSelector.h:409
Definition: CUColor4.h:1104
ObstacleSelector()
Definition: CUObstacleSelector.h:167
void setPosition(const Vec2 &pos)
Definition: CUObstacleSelector.h:272
float getFrequency() const
Definition: CUObstacleSelector.h:335
Definition: CUAnimationNode.h:52
const Vec2 & getPosition()
Definition: CUObstacleSelector.h:257
void setDebugScene(const std::shared_ptr< Node > &node)
void setDamping(float ratio)
Definition: CUObstacleSelector.h:365
bool isSelected() const
Definition: CUObstacleSelector.h:282
const Size & getMouseSize() const
Definition: CUObstacleSelector.h:397
~ObstacleSelector()
Definition: CUObstacleSelector.h:175