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

#include <CUButton.h>

Inheritance diagram for cugl::Button:
cugl::Node

Public Member Functions

 Button ()
 
 ~Button ()
 
virtual void dispose () override
 
virtual bool init () override
 
bool init (const std::shared_ptr< Node > &up)
 
bool init (const std::shared_ptr< Node > &up, Color4 down)
 
bool init (const std::shared_ptr< Node > &up, const std::shared_ptr< Node > &down)
 
bool containsScreen (const Vec2 &point)
 
bool contains (float x, float y)
 
bool isDown () const
 
void setDown (bool down)
 
bool hasListener () const
 
const ButtonListener getListener () const
 
void setListener (ButtonListener listener)
 
bool removeListener ()
 
bool activate (Uint32 key)
 
bool deactivate ()
 
- Public Member Functions inherited from cugl::Node
 Node ()
 
virtual ~Node ()
 
virtual bool initWithPosition (const Vec2 &pos)
 
bool initWithPosition (float x, float y)
 
virtual bool initWithBounds (const Size &size)
 
bool initWithBounds (float width, float height)
 
virtual bool initWithBounds (const Rect &rect)
 
bool initWithBounds (float x, float y, float width, float height)
 
virtual Nodecopy (Node *dst)
 
unsigned int getTag () const
 
void setTag (unsigned int tag)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
virtual std::string toString (bool verbose=false) const
 
 operator std::string () const
 
const Vec2getPosition () const
 
void setPosition (const Vec2 &position)
 
void setPosition (float x, float y)
 
float getPositionX (void) const
 
void setPositionX (float x)
 
float getPositionY (void) const
 
void setPositionY (float y)
 
Vec2 getWorldPosition () const
 
const SizegetContentSize () const
 
virtual void setContentSize (const Size &size)
 
virtual void setContentSize (float width, float height)
 
float getContentWidth () const
 
void setContentWidth (float width)
 
float getContentHeight () const
 
void setContentHeight (float height)
 
Size getSize () const
 
float getWidth () const
 
float getHeight () const
 
Rect getBoundingBox () const
 
virtual void setAnchor (const Vec2 &anchor)
 
void setAnchor (float x, float y)
 
const Vec2getAnchor () const
 
Vec2 getAnchorInPixels ()
 
Color4 getColor () const
 
void setColor (Color4 color)
 
Color4 getAbsoluteColor ()
 
bool isVisible () const
 
void setVisible (bool visible)
 
bool hasRelativeColor ()
 
void setRelativeColor (bool flag)
 
const Vec2getScale () const
 
float getScaleX () const
 
float getScaleY () const
 
void setScale (float scale)
 
void setScale (const Vec2 &vec)
 
void setScale (float sx, float sy)
 
float getAngle ()
 
void setAngle (float angle)
 
const Mat4getAlternateTransform ()
 
void setAlternateTransform (const Mat4 &transform)
 
bool withAlternateTransform ()
 
void chooseAlternateTransform (bool active)
 
const Mat4getNodeToParentTransform () const
 
Mat4 getParentToNodeTransform () const
 
Mat4 getNodeToWorldTransform () const
 
Mat4 getWorldToNodeTransform () const
 
Vec2 screenToNodeCoords (const Vec2 &screenPoint) const
 
Vec2 worldToNodeCoords (const Vec2 &worldPoint) const
 
Vec2 nodeToScreenCoords (const Vec2 &nodePoint) const
 
Vec2 nodeToWorldCoords (const Vec2 &nodePoint) const
 
Vec2 parentToNodeCoords (const Vec2 &parentPoint) const
 
Vec2 nodeToParentCoords (const Vec2 &nodePoint) const
 
void setZOrder (int z)
 
int getZOrder () const
 
bool isZDirty () const
 
void sortZOrder ()
 
size_t getChildCount () const
 
std::shared_ptr< NodegetChild (unsigned int pos)
 
const std::shared_ptr< Node > & getChild (unsigned int pos) const
 
template<typename T >
std::shared_ptr< TgetChild (unsigned int pos) const
 
std::shared_ptr< NodegetChildByTag (unsigned int tag) const
 
template<typename T >
std::shared_ptr< TgetChildByTag (unsigned int tag) const
 
std::shared_ptr< NodegetChildByName (const std::string &name) const
 
template<typename T >
std::shared_ptr< TgetChildByName (const std::string &name) const
 
std::vector< std::shared_ptr< Node > > getChildren ()
 
const std::vector< std::shared_ptr< Node > > & getChildren () const
 
void addChild (std::shared_ptr< Node > child)
 
virtual void addChild (const std::shared_ptr< Node > &child, int zval)
 
void addChildWithTag (const std::shared_ptr< Node > &child, unsigned int tag)
 
void addChildWithTag (const std::shared_ptr< Node > &child, unsigned int tag, int zval)
 
void addChildWithName (const std::shared_ptr< Node > &child, const std::string &name)
 
void addChildWithName (const std::shared_ptr< Node > &child, const std::string &name, int zval)
 
void swapChild (const std::shared_ptr< Node > &child1, const std::shared_ptr< Node > &child2, bool inherit=false)
 
NodegetParent ()
 
const NodegetParent () const
 
ScenegetScene ()
 
const ScenegetScene () const
 
void removeFromParent ()
 
virtual void removeChild (unsigned int pos)
 
void removeChild (const std::shared_ptr< Node > &child)
 
void removeChildByTag (unsigned int tag)
 
void removeChildByName (const std::string &name)
 
virtual void removeAllChildren ()
 
void render (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint)
 
void render (const std::shared_ptr< SpriteBatch > &batch)
 
virtual void draw (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint)
 

Static Public Member Functions

static std::shared_ptr< Buttonalloc (const std::shared_ptr< Node > &up)
 
static std::shared_ptr< Buttonalloc (const std::shared_ptr< Node > &up, Color4 down)
 
static std::shared_ptr< Buttonalloc (const std::shared_ptr< Node > &up, const std::shared_ptr< Node > &down)
 
- Static Public Member Functions inherited from cugl::Node
static std::shared_ptr< Nodealloc ()
 
static std::shared_ptr< NodeallocWithPosition (const Vec2 &pos)
 
static std::shared_ptr< NodeallocWithPosition (float x, float y)
 
static std::shared_ptr< NodeallocWithBounds (const Size &size)
 
static std::shared_ptr< NodeallocWithBounds (float width, float height)
 
static std::shared_ptr< NodeallocWithBounds (const Rect &rect)
 
static std::shared_ptr< NodeallocWithBounds (float x, float y, float width, float height)
 

Protected Attributes

bool _down
 
std::shared_ptr< Node_upnode
 
std::shared_ptr< Node_downnode
 
Color4 _upcolor
 
Color4 _downcolor
 
bool _active
 
bool _mouse
 
Uint32 _inputkey
 
ButtonListener _listener
 
- Protected Attributes inherited from cugl::Node
Vec2 _position
 
Vec2 _anchor
 
Size _contentSize
 
Color4 _tintColor
 
bool _hasParentColor
 
bool _isVisible
 
Vec2 _scale
 
float _angle
 
Mat4 _transform
 
bool _useTransform
 
Mat4 _combined
 
std::vector< std::shared_ptr< Node > > _children
 
Node_parent
 
Scene_graph
 
int _childOffset
 
unsigned int _tag
 
std::string _name
 
size_t _hashOfName
 
int _zOrder
 
bool _zDirty
 

Detailed Description

This class represents a simple clickable button

A button is either two nodes (one for up, one for down) that swap whenever the button is pressed, or a single node that changes color on a press. The nodes are typically either images (e.g PolygonNode or instances of class Label. These nodes are stored as children of this button, which in turn should be part of a larger scene graph.

The button can track its own state, via the activate(Uint32) method, relieving you of having to manually check mouse presses/touches. However, the appropriate input device must be active before you can activate the button, as it needs to attach internal listeners.

Constructor & Destructor Documentation

cugl::Button::Button ( )
inline

Creates an uninitialized button with no size or texture information.

You must initialize this button before use.

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

cugl::Button::~Button ( )
inline

Deletes this button, disposing all resources

Member Function Documentation

bool cugl::Button::activate ( Uint32  key)

Activates this button to listen for mouse/touch events.

This method attaches a listener to either the Mouse or Touchscreen inputs to monitor when the button is pressed and/or released. The button will favor the mouse, but will use the touch screen if no mouse input is active. If neither input is active, this method will fail.

When active, the button will change its state on its own, without requiring the user to use setDown(bool). If there is a ButtonListener attached, it will call that function upon any state changes.

Parameters
keyThe listener key for the input device
Returns
true if the button was successfully activated
static std::shared_ptr<Button> cugl::Button::alloc ( const std::shared_ptr< Node > &  up)
inlinestatic

Returns a newly allocated button with the given up node.

The button will look exactly like the given node when it is not pressed. When pressed, it will darken the up node a bit.

Parameters
upThe button when it is not pressed
Returns
a newly allocated button with the given up node.
static std::shared_ptr<Button> cugl::Button::alloc ( const std::shared_ptr< Node > &  up,
Color4  down 
)
inlinestatic

Returns a newly allocated button with the given node and color

The button will look exactly like the given node when it is not pressed. When pressed, it will tint the up node by the specified color.

Parameters
upThe button when it is not pressed
downThe button tint when it is pressed
Returns
a newly allocated button with the given node and color
static std::shared_ptr<Button> cugl::Button::alloc ( const std::shared_ptr< Node > &  up,
const std::shared_ptr< Node > &  down 
)
inlinestatic

Returns a newly allocated button with the given nodes

The button will look exactly like the up node when it is not pressed. It will look like the down node when it is pressed. The size of this button is the size of the larger of the two nodes.

Parameters
upThe button when it is not pressed
downThe button when it is pressed
Returns
a newly allocated button with the given nodes
bool cugl::Button::contains ( float  x,
float  y 
)
inline

Returns true if this button contains the given screen point

This method is used to manually check for mouse presses/touches. It converts a point in screen coordinates to the node coordinates and checks if it is in the bounds of the button.

Parameters
xThe x-value in screen coordinates
yThe y-value in screen coordinates
Returns
true if this button contains the given screen point
bool cugl::Button::containsScreen ( const Vec2 point)
inline

Returns true if this button contains the given screen point

This method is used to manually check for mouse presses/touches. It converts a point in screen coordinates to the node coordinates and checks if it is in the bounds of the button.

Parameters
pointThe point in screen coordinates
Returns
true if this button contains the given screen point
bool cugl::Button::deactivate ( )

Deactivates this button, ignoring future mouse/touch events.

This method removes its internal listener from either the Mouse or Touchscreen inputs to monitor when the button is pressed and/or released. The input affected is the one that received the listener upon activation.

When deactivated, the button will no longer change its state on its own. However, the user can still change the state manually with the setDown(bool) method. In addition, any ButtonListener attached will still respond to manual state changes.

Returns
true if the button was successfully deactivated
virtual void cugl::Button::dispose ( )
overridevirtual

Disposes all of the resources used by this node.

A disposed button can be safely reinitialized. Any children owned by this node will be released. They will be deleted if no other object owns them.

It is unsafe to call this on a button that is still currently inside of a scene graph.

Reimplemented from cugl::Node.

const ButtonListener cugl::Button::getListener ( ) const
inline

Returns the listener (if any) for this button

This listener is invoked when the button state changes (up or down).

A button may only have one listener at a time. If there is no listener, this method returns nullptr.

Returns
the listener (if any) for this button
bool cugl::Button::hasListener ( ) const
inline

Returns true if this button has a listener

This listener is invoked when the button state changes (up or down).

A button may only have one listener at a time.

Returns
true if this button has a listener
virtual bool cugl::Button::init ( )
inlineoverridevirtual

Deactivates the default initializer.

This initializer may not be used for a button. A button must have a child node for the up state at the very minimum.

Returns
false

Reimplemented from cugl::Node.

bool cugl::Button::init ( const std::shared_ptr< Node > &  up)
inline

Initializes a button with the given up node.

The button will look exactly like the given node when it is not pressed. When pressed, it will darken the up node a bit.

Parameters
upThe button when it is not pressed
Returns
true if the button is initialized properly, false otherwise.
bool cugl::Button::init ( const std::shared_ptr< Node > &  up,
Color4  down 
)

Initializes a button with the given node and color

The button will look exactly like the given node when it is not pressed. When pressed, it will tint the up node by the specified color.

Parameters
upThe button when it is not pressed
downThe button tint when it is pressed
Returns
true if the button is initialized properly, false otherwise.
bool cugl::Button::init ( const std::shared_ptr< Node > &  up,
const std::shared_ptr< Node > &  down 
)

Initializes a button with the given nodes

The button will look exactly like the up node when it is not pressed. It will look like the down node when it is pressed. The size of this button is the size of the larger of the two nodes.

Parameters
upThe button when it is not pressed
downThe button when it is pressed
Returns
true if the button is initialized properly, false otherwise.
bool cugl::Button::isDown ( ) const
inline

Return true if this button is currently down.

Buttons only have two states: up and down. The default state is up.

Changing this value will change how the button is displayed on the screen. It will also invoke the ButtonListener if one is currently attached.

Returns
true if this button is currently down.
bool cugl::Button::removeListener ( )

Removes the listener for this button.

This listener is invoked when the button state changes (up or down).

A button may only have one listener at a time. If this button does not have a listener, this method will fail.

Returns
true if the listener was succesfully removed
void cugl::Button::setDown ( bool  down)

Sets whether this button is currently down.

Buttons only have two states: up and down. The default state is up.

Changing this value will change how the button is displayed on the screen. It will also invoke the ButtonListener if one is currently attached.

Parameters
downWhether this button is currently down.
void cugl::Button::setListener ( ButtonListener  listener)
inline

Sets the listener for this button.

This listener is invoked when the button state changes (up or down).

A button may only have one listener at a time. If this button already has a listener, this method will replace it for the once specified.

Parameters
listenerThe listener to use

Member Data Documentation

bool cugl::Button::_active
protected

Whether the button is actively checking for state changes

bool cugl::Button::_down
protected

Whether or not the button is currently down

Color4 cugl::Button::_downcolor
protected

The button color when the button is down

std::shared_ptr<Node> cugl::Button::_downnode
protected

The node representing the button when it is down (may be null)

Uint32 cugl::Button::_inputkey
protected

The listener key when the button is checking for state changes

ButtonListener cugl::Button::_listener
protected

The listener callback for state changes

bool cugl::Button::_mouse
protected

Whether we are using the mouse (as opposed to the touch screen)

Color4 cugl::Button::_upcolor
protected

The button color when the button is up

std::shared_ptr<Node> cugl::Button::_upnode
protected

The node representing the button when it is up (cannot be null)


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