CUGL
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::ObstacleSelector Class Reference

#include <CUObstacleSelector.h>

Public Member Functions

 ObstacleSelector ()
 
 ~ObstacleSelector ()
 
void dispose ()
 
bool init (const std::shared_ptr< ObstacleWorld > &world)
 
bool init (const std::shared_ptr< ObstacleWorld > &world, const Size &mouseSize)
 
const Vec2getPosition ()
 
void setPosition (float x, float y)
 
void setPosition (const Vec2 &pos)
 
bool isSelected () const
 
bool select ()
 
void deselect ()
 
ObstaclegetObstacle ()
 
bool onQuery (b2Fixture *fixture)
 
float getFrequency () const
 
void setFrequency (float speed)
 
float getDamping () const
 
void setDamping (float ratio)
 
float getForce () const
 
void setForce (float force)
 
const SizegetMouseSize () const
 
void setMouseSize (const Size &size)
 
Color4 getDebugColor () const
 
void setDebugColor (Color4 color)
 
NodegetDebugScene () const
 
void setDebugScene (const std::shared_ptr< Node > &node)
 
void setVisible (bool flag)
 
bool isVisible ()
 
bool hasDebug ()
 

Static Public Member Functions

static std::shared_ptr< ObstacleSelectoralloc (const std::shared_ptr< ObstacleWorld > &world)
 
static std::shared_ptr< ObstacleSelectoralloc (const std::shared_ptr< ObstacleWorld > &world, const Size &mouseSize)
 

Protected Member Functions

void resetDebug ()
 
void updateDebug ()
 
void updateTarget (Obstacle *obstacle)
 
Poly2 hatchPoly () const
 

Protected Attributes

std::shared_ptr< ObstacleWorld_controller
 
Vec2 _position
 
Size _size
 
float _force
 
b2Fixture * _selection
 
b2Body * _ground
 
b2MouseJointDef _jointDef
 
b2MouseJoint * _mouseJoint
 
std::shared_ptr< Node_scene
 
std::shared_ptr< WireNode_hatch
 
std::shared_ptr< WireNode_connect
 
bool _dvisible
 
Color4 _dcolor
 

Detailed Description

Selection tool to move and drag physics obstacles

This class is essentially an instance of b2MouseJoint, but with an API that makes it a lot easier to use. It must be attached to a WorldController on creation, and this controller can never change. If you want a selector for a different ObstacleWorld, make a new instance.

As with all instances of b2MouseJoint, there will be some lag in the drag (though this is true on touch devices in general). You can adjust the degree of this lag by adjusting the force. However, larger forces can cause artifacts when dragging an obstacle through other obstacles.

Constructor & Destructor Documentation

cugl::ObstacleSelector::ObstacleSelector ( )
inline

Creates a new ObstacleSelector

The selector created is not usable. This constructor only initializes default values.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

cugl::ObstacleSelector::~ObstacleSelector ( )
inline

Disposes of this selector, releasing all resources.

Member Function Documentation

static std::shared_ptr<ObstacleSelector> cugl::ObstacleSelector::alloc ( const std::shared_ptr< ObstacleWorld > &  world)
inlinestatic

Returns a newly allocated ObstacleSelector for the given ObstacleWorld

This controller can never change. If you want a selector for a different ObstacleWorld, make a new instance.

This constructor uses the default mouse size.

Parameters
worldthe physics controller
Returns
a newly allocated ObstacleSelector for the given ObstacleWorld
static std::shared_ptr<ObstacleSelector> cugl::ObstacleSelector::alloc ( const std::shared_ptr< ObstacleWorld > &  world,
const Size mouseSize 
)
inlinestatic

Returns a newly allocated ObstacleSelector for the given world and mouse size.

This controller can never change. If you want a selector for a different ObstacleWorld, make a new instance. However, the mouse size can be changed at any time.

Parameters
worldthe physics controller
mouseSizethe mouse size
Returns
an autoreleased selector object
void cugl::ObstacleSelector::deselect ( )

Deselects the physics body, discontinuing any mouse movement.

The body may still continue to move of its own accord.

void cugl::ObstacleSelector::dispose ( )

Disposes all of the resources used by this selector.

A disposed selector can be safely reinitialized.

float cugl::ObstacleSelector::getDamping ( ) const
inline

Returns the damping ratio of the mouse joint

See the documentation of b2JointDef for more information on the damping ratio.

Returns
the damping ratio of the mouse joint
Color4 cugl::ObstacleSelector::getDebugColor ( ) const
inline

Returns the color of the debug wireframe.

The default color is white, which means that the objects will be shown with a white wireframe.

Returns
the color of the debug wireframe.
Node* cugl::ObstacleSelector::getDebugScene ( ) const
inline

Returns the parent scene graph node for the debug wireframe

The returned node is the parent coordinate space for drawing physics. All debug nodes for physics objects are drawn within this coordinate space. Setting the visibility of this node to false will disable any debugging.

The wireframe will be drawn using physics coordinates, which is possibly much smaller than your drawing coordinates (e.g. 1 Box2D unit = 1 pixel). If you want the wireframes to be larger, you should scale the parent parent coordinate space to match the rest of the application.

This scene graph node is intended for debugging purposes only. If you want a physics body to update a proper texture image, you should either use the method update(float) for subclasses or setListener for decoupled classes.

Returns
the scene graph node for the debug wireframe
float cugl::ObstacleSelector::getForce ( ) const
inline

Returns the force multiplier of the mouse joint

The mouse joint will move the attached fixture with a force of this value times the object mass.

Returns
the force multiplier of the mouse joint
float cugl::ObstacleSelector::getFrequency ( ) const
inline

Returns the response speed of the mouse joint

See the documentation of b2JointDef for more information on the response speed.

Returns
the response speed of the mouse joint
const Size& cugl::ObstacleSelector::getMouseSize ( ) const
inline

Returns the size of the mouse pointer

When a selection is made, this selector will create an axis-aligned bounding box centered at the mouse position. Any fixture overlapping this box will be selected. The size of this box is determined by this value.

Returns
the size of the mouse pointer
Obstacle* cugl::ObstacleSelector::getObstacle ( )

Returns a (weak) reference to the Obstacle selected (if any)

Just because a physics body was selected does not mean that an Obstacle was selected. The body could be a basic Box2d body generated by other means. If the body is not an Obstacle, this method returns nullptr.

Returns
a (weak) reference to the Obstacle selected (if any)
const Vec2& cugl::ObstacleSelector::getPosition ( )
inline

Returns the current position of this selector (in World space)

Returns
the current position of this selector (in World space)
bool cugl::ObstacleSelector::hasDebug ( )
inline

Returns true if the obstacle has a wireframe for debugging.

This method will return false if there is no active parent scene for the wireframe.

Returns
true if the obstacle has a wireframe for debugging.
Poly2 cugl::ObstacleSelector::hatchPoly ( ) const
protected

Returns a new polygon for the mouse hatch

Returns
a new polygon for the mouse hatch
bool cugl::ObstacleSelector::init ( const std::shared_ptr< ObstacleWorld > &  world)
inline

Initializes a new selector for the given ObstacleWorld

This controller can never change. If you want a selector for a different ObstacleWorld, make a new instance.

This initializer uses the default mouse size.

Parameters
worldthe physics controller
Returns
true if the obstacle is initialized properly, false otherwise.
bool cugl::ObstacleSelector::init ( const std::shared_ptr< ObstacleWorld > &  world,
const Size mouseSize 
)

Initializes a new selector for the given ObstacleWorld and mouse size.

This controller can never change. If you want a selector for a different ObstacleWorld, make a new instance. However, the mouse size can be changed at any time.

Parameters
worldthe physics controller
mouseSizethe mouse size
Returns
true if the obstacle is initialized properly, false otherwise.
bool cugl::ObstacleSelector::isSelected ( ) const
inline

Returns true if a physics body is currently selected

Returns
true if a physics body is currently selected
bool cugl::ObstacleSelector::isVisible ( )
inline

Returns true if the debug wireframe for this object is visible

This method is necessary for touch screen devices, where we cannot track the selector if there is no active touch.

Returns
true if the debug wireframe for this object is visible
bool cugl::ObstacleSelector::onQuery ( b2Fixture *  fixture)

Callback function for mouse selection.

This is the callback function used by the method queryAABB to select a physics body at the current mouse location.

Parameters
fixturethe fixture selected
Returns
false to terminate the query.
void cugl::ObstacleSelector::resetDebug ( )
protected

Creates the outline of the physics fixtures in the debug wireframe

The debug wireframe is use to outline the fixtures attached to this selector. It is useful when you want to visualize the relationship between the mouse and the selected shape.

bool cugl::ObstacleSelector::select ( )

Returns true if a physics body was selected at the current position.

This method contructs and AABB the size of the mouse pointer, centered at the current position. If any part of the AABB overlaps a fixture, it is selected.

Returns
true if a physics body was selected at the given position.
void cugl::ObstacleSelector::setDamping ( float  ratio)
inline

Sets the damping ratio of the mouse joint

See the documentation of b2JointDef for more information on the damping ratio.

Parameters
ratiothe damping ratio of the mouse joint
void cugl::ObstacleSelector::setDebugColor ( Color4  color)
inline

Sets the color of the debug wireframe.

The default color is white, which means that the objects will be shown with a white wireframe.

Parameters
colorthe color of the debug wireframe.
void cugl::ObstacleSelector::setDebugScene ( const std::shared_ptr< Node > &  node)

Sets the parent scene graph node for the debug wireframe

The given node is the parent coordinate space for drawing physics. All debug nodes for physics objects are drawn within this coordinate space. Setting the visibility of this node to false will disable any debugging. Similarly, setting this value to nullptr will disable any debugging.

The wireframe will be drawn using physics coordinates, which is possibly much smaller than your drawing coordinates (e.g. 1 Box2D unit = 1 pixel). If you want the wireframes to be larger, you should scale the parent parent coordinate space to match the rest of the application.

This scene graph node is intended for debugging purposes only. If you want a physics body to update a proper texture image, you should either use the method update(float) for subclasses or setListener for decoupled classes.

Parameters
nodehe parent scene graph node for the debug wireframe
void cugl::ObstacleSelector::setForce ( float  force)
inline

Sets the force multiplier of the mouse joint

The mouse joint will move the attached fixture with a force of this value times the object mass.

Parameters
forcethe force multiplier of the mouse joint
void cugl::ObstacleSelector::setFrequency ( float  speed)
inline

Sets the response speed of the mouse joint

See the documentation of b2JointDef for more information on the response speed.

Parameters
speedthe response speed of the mouse joint
void cugl::ObstacleSelector::setMouseSize ( const Size size)
inline

Sets the size of the mouse pointer

When a selection is made, this selector will create an axis-aligned bounding box centered at the mouse position. Any fixture overlapping this box will be selected. The size of this box is determined by this value.

Parameters
sizethe size of the mouse pointer
void cugl::ObstacleSelector::setPosition ( float  x,
float  y 
)

Sets the current position of this selector (in World space)

Parameters
xthe x-coordinate of the selector
ythe y-coordinate of the selector
void cugl::ObstacleSelector::setPosition ( const Vec2 pos)
inline

Sets the current position of this selector (in World space)

Parameters
posthe position of the selector
void cugl::ObstacleSelector::setVisible ( bool  flag)
inline

Sets whether the debug wireframe for this object is visible

This method is necessary for touch screen devices, where we cannot track the selector if there is no active touch.

Parameters
flagwhether the debug wireframe for this object is visible
void cugl::ObstacleSelector::updateDebug ( )
protected

Repositions the debug wireframe so that it agrees with the physics object.

The debug wireframe is use to outline the fixtures attached to this selector. It is useful when you want to visualize the relationship between the mouse and the selected shape.

void cugl::ObstacleSelector::updateTarget ( Obstacle obstacle)
protected

Repositions the scene node so that it agrees with the physics object.

By default, the position of a node should be the body position times the draw scale. However, for some obstacles (particularly complex obstacles), it may be desirable to turn the default functionality off. Hence we have made this virtual.

Member Data Documentation

std::shared_ptr<WireNode> cugl::ObstacleSelector::_connect
protected

The wireframe node for the connection

std::shared_ptr<ObstacleWorld> cugl::ObstacleSelector::_controller
protected

The ObstacleWorld associated with this selection

Color4 cugl::ObstacleSelector::_dcolor
protected

The wireframe color for debugging

bool cugl::ObstacleSelector::_dvisible
protected

The conversion rate between physics units and drawing units Whether or not to display the debug wireframe.

float cugl::ObstacleSelector::_force
protected

The amount to multiply by the mass to move the object

b2Body* cugl::ObstacleSelector::_ground
protected

A default body used as the other half of the mouse joint

std::shared_ptr<WireNode> cugl::ObstacleSelector::_hatch
protected

The wireframe node for debugging.

b2MouseJointDef cugl::ObstacleSelector::_jointDef
protected

A reusable definition for creating a mouse joint

b2MouseJoint* cugl::ObstacleSelector::_mouseJoint
protected

The current mouse joint, if an item is selected

Vec2 cugl::ObstacleSelector::_position
protected

The location in world space of this selector

std::shared_ptr<Node> cugl::ObstacleSelector::_scene
protected

The wireframe parent for debugging.

b2Fixture* cugl::ObstacleSelector::_selection
protected

The current fixture selected by this tool (may be nullptr)

Size cugl::ObstacleSelector::_size
protected

The size of the selection region (for accuracy)


The documentation for this class was generated from the following file: