CUGL 2.5
Cornell University Game Library
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::scene2::ButtonGroup Class Reference

#include <CUButtonGroup.h>

Inheritance diagram for cugl::scene2::ButtonGroup:
cugl::scene2::SceneNode

Public Types

typedef std::function< void(const std::string name, int curBtn, int nextBtn)> Listener
 

Public Member Functions

 ButtonGroup ()
 
 ~ButtonGroup ()
 
virtual void dispose () override
 
virtual bool init () override
 
bool init (const std::shared_ptr< Button > &button, int initialButton=-1)
 
bool init (const std::vector< std::shared_ptr< Button > > &buttons, int initialButton=-1)
 
bool initWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data) override
 
const int getCurrent () const
 
void addButton (const std::shared_ptr< Button > &btn)
 
void removeButton (const std::shared_ptr< Button > &btn)
 
int screenToIndex (const Vec2 point)
 
int screenToIndex (float x, float y)
 
void setDown (int btn)
 
virtual void doLayout () override
 
bool hasListener () const
 
const Listener getListener (Uint32 key) const
 
const std::vector< ListenergetListeners () const
 
Uint32 addListener (Listener listener)
 
bool removeListener (Uint32 key)
 
void clearListeners ()
 
bool activate ()
 
bool deactivate ()
 
bool isActive () const
 
- Public Member Functions inherited from cugl::scene2::SceneNode
 SceneNode ()
 
 ~SceneNode ()
 
virtual void dispose ()
 
virtual bool init ()
 
virtual bool initWithPosition (const Vec2 pos)
 
bool initWithPosition (float x, float y)
 
virtual bool initWithBounds (const Size size)
 
virtual bool initWithBounds (float width, float height)
 
virtual bool initWithBounds (const Rect rect)
 
virtual bool initWithBounds (float x, float y, float width, float height)
 
virtual bool initWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data)
 
virtual std::shared_ptr< SceneNodecopy (const std::shared_ptr< SceneNode > &dst) const
 
unsigned int getTag () const
 
void setTag (unsigned int tag)
 
const std::string getName () const
 
void setName (const std::string name)
 
const std::string getClassName () const
 
virtual std::string toString (bool verbose=false) const
 
 operator std::string () const
 
const Vec2 getPosition () 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 Size getContentSize () 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)
 
virtual Rect getLayoutBounds () const
 
Size getSize () const
 
float getWidth () const
 
float getHeight () const
 
Rect getBoundingBox () const
 
virtual bool inContentBounds (const Vec2 point)
 
bool inContentBounds (float x, float y)
 
virtual void setAnchor (const Vec2 anchor)
 
virtual void setAnchor (float x, float y)
 
const Vec2 getAnchor () const
 
Vec2 getAnchorInPixels ()
 
Color4 getColor () const
 
virtual void setColor (Color4 color)
 
Color4 getAbsoluteColor ()
 
bool isVisible () const
 
void setVisible (bool visible)
 
bool hasRelativeColor () const
 
void setRelativeColor (bool flag)
 
std::shared_ptr< ScissorgetScissor () const
 
void setScissor (const std::shared_ptr< Scissor > &scissor)
 
void setScissor ()
 
const Vec2 getScale () 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 Affine2getTransform () const
 
const Affine2getAlternateTransform () const
 
void setAlternateTransform (const Affine2 &transform)
 
bool withAlternateTransform ()
 
void chooseAlternateTransform (bool active)
 
const Affine2getNodeToParentTransform () const
 
Affine2 getParentToNodeTransform () const
 
virtual Affine2 getNodeToWorldTransform () const
 
Affine2 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
 
size_t getChildCount () const
 
std::shared_ptr< SceneNodegetChild (unsigned int pos)
 
const std::shared_ptr< SceneNode > & getChild (unsigned int pos) const
 
template<typename T >
std::shared_ptr< TgetChild (unsigned int pos) const
 
std::shared_ptr< SceneNodegetChildByTag (unsigned int tag) const
 
template<typename T >
std::shared_ptr< TgetChildByTag (unsigned int tag) const
 
std::shared_ptr< SceneNodegetChildByName (const std::string name) const
 
template<typename T >
std::shared_ptr< TgetChildByName (const std::string name) const
 
std::vector< std::shared_ptr< SceneNode > > getChildren ()
 
const std::vector< std::shared_ptr< SceneNode > > & getChildren () const
 
void addChild (const std::shared_ptr< SceneNode > &child)
 
void addChildWithTag (const std::shared_ptr< SceneNode > &child, unsigned int tag)
 
void addChildWithName (const std::shared_ptr< SceneNode > &child, const std::string name)
 
void swapChild (const std::shared_ptr< SceneNode > &child1, const std::shared_ptr< SceneNode > &child2, bool inherit=false)
 
SceneNodegetParent ()
 
const SceneNodegetParent () const
 
Scene2getScene ()
 
const Scene2getScene () const
 
void removeFromParent ()
 
virtual void removeChild (unsigned int pos)
 
void removeChild (const std::shared_ptr< SceneNode > &child)
 
void removeChildByTag (unsigned int tag)
 
void removeChildByName (const std::string name)
 
virtual void removeAllChildren ()
 
void setPriority (float priority)
 
float getPriority ()
 
virtual void render (const std::shared_ptr< SpriteBatch > &batch, const Affine2 &transform, Color4 tint)
 
virtual void render (const std::shared_ptr< SpriteBatch > &batch)
 
virtual void draw (const std::shared_ptr< SpriteBatch > &batch, const Affine2 &transform, Color4 tint)
 
const std::shared_ptr< Layout > & getLayout () const
 
void setLayout (const std::shared_ptr< Layout > &layout)
 
virtual void doLayout ()
 

Static Public Member Functions

static std::shared_ptr< ButtonGroupalloc (const std::shared_ptr< Button > &button, int initialButton=-1)
 
static std::shared_ptr< ButtonGroupalloc (const std::vector< std::shared_ptr< Button > > &buttons, int initialButton=-1)
 
static std::shared_ptr< SceneNodeallocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data)
 
- Static Public Member Functions inherited from cugl::scene2::SceneNode
static std::shared_ptr< SceneNodealloc ()
 
static std::shared_ptr< SceneNodeallocWithPosition (const Vec2 pos)
 
static std::shared_ptr< SceneNodeallocWithPosition (float x, float y)
 
static std::shared_ptr< SceneNodeallocWithBounds (const Size size)
 
static std::shared_ptr< SceneNodeallocWithBounds (float width, float height)
 
static std::shared_ptr< SceneNodeallocWithBounds (const Rect rect)
 
static std::shared_ptr< SceneNodeallocWithBounds (float x, float y, float width, float height)
 
static std::shared_ptr< SceneNodeallocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data)
 

Protected Attributes

int _curButton
 
std::vector< std::shared_ptr< Button > > _buttons
 
std::vector< std::string > _keyset
 
bool _active
 
bool _mouse
 
Uint32 _inputkey
 
Uint32 _nextKey
 
std::unordered_map< Uint32, Listener_listeners
 
- Protected Attributes inherited from cugl::scene2::SceneNode
Vec2 _position
 
Vec2 _anchor
 
Size _contentSize
 
Color4 _tintColor
 
bool _hasParentColor
 
bool _isVisible
 
std::shared_ptr< Scissor_scissor
 
Vec2 _scale
 
float _angle
 
Affine2 _transform
 
bool _useTransform
 
Affine2 _combined
 
std::vector< std::shared_ptr< SceneNode > > _children
 
SceneNode_parent
 
Scene2_graph
 
std::shared_ptr< Layout_layout
 
int _childOffset
 
unsigned int _tag
 
std::string _name
 
size_t _hashOfName
 
std::string _classname
 
float _priority
 
std::shared_ptr< JsonValue_json
 

Detailed Description

This class represents a button group

A button group is a bunch of toggle buttons, of which at most one can be selected at a time. If a single button is clicked that button's listener will be called if it has one, as well as the button group's listener. The previously toggled button will be untoggled and its listener will also be called.

Button groups can be used to make a multiple choice quiz scenes and tabs

Member Typedef Documentation

◆ Listener

This type represents a listener for a ButtonGroup state change.

In CUGL, listeners are implemented as a set of callback functions, not as objects. This allows each listener to implement as much or as little functionality as it wants. For simplicity, ButtonGroup nodes only support a single listener. If you wish for more than one listener, then your listener should handle its own dispatch.

The function type is equivalent to

 std::function<void(const std::string name, bool down)>
Parameters
nameThe button name
curBtnThe currently toggled button in the group
nextBtnThe button to be toggled once the listeners have finished

Constructor & Destructor Documentation

◆ ButtonGroup()

cugl::scene2::ButtonGroup::ButtonGroup ( )

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.

◆ ~ButtonGroup()

cugl::scene2::ButtonGroup::~ButtonGroup ( )
inline

Deletes this button group, disposing all resources

Member Function Documentation

◆ activate()

bool cugl::scene2::ButtonGroup::activate ( )

Activates this button group 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 group 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 group will change the state of the appropriate button on its own, without requiring the user to use setDown(int). If there is a Listener attached, it will call that function upon any state changes.

Returns
true if the button was successfully activated

◆ addButton()

void cugl::scene2::ButtonGroup::addButton ( const std::shared_ptr< Button > &  btn)

Add a button to the button group

Parameters
btnThe button to add to the group

◆ addListener()

Uint32 cugl::scene2::ButtonGroup::addListener ( Listener  listener)

Returns a key for a listener after adding it to this button group.

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

C++ cannot hash functions types. Therefore, the listener will be identified by a unique key, returned by this function. You should remember this key to remove the listener if necessary.

This also means that adding a listener twice will add it for an additional key, causing the listener to be called multiple times on a state change.

Parameters
listenerThe listener to add
Returns
the key for the listener

◆ alloc() [1/2]

static std::shared_ptr< ButtonGroup > cugl::scene2::ButtonGroup::alloc ( const std::shared_ptr< Button > &  button,
int  initialButton = -1 
)
inlinestatic

Returns a newly allocated button group with the given button and index

The new button group will have a single button, the one given, and will initially be set down if initialButton is 0. If initialButton is -1, no button is set down.

Parameters
buttonThe button in the button group
initialButtonThe index of the button to set down initially
Returns
a newly allocated button group with the given button and index

◆ alloc() [2/2]

static std::shared_ptr< ButtonGroup > cugl::scene2::ButtonGroup::alloc ( const std::vector< std::shared_ptr< Button > > &  buttons,
int  initialButton = -1 
)
inlinestatic

Returns a newly allocated button group with the given buttons and index

The new button group will have all of the buttons in the given list. If initialButton is a positive number the button at that index in the list will be set down initially. If initialButton is -1, no button is set down.

Parameters
buttonsThe buttons in the button group
initialButtonThe index of the button to set down initially
Returns
a newly allocated button group with the given buttons and index

◆ allocWithData()

static std::shared_ptr< SceneNode > cugl::scene2::ButtonGroup::allocWithData ( const Scene2Loader loader,
const std::shared_ptr< JsonValue > &  data 
)
inlinestatic

Returns a newly allocated a node with the given JSON specificaton.

This initializer is designed to receive the "data" object from the JSON passed to Scene2Loader. This JSON format supports all of the attribute values of its parent class. In addition, it supports the following additional attributes:

 "buttons":    A list of strings referencing the name of children nodes (must be buttons)
 "initial":    A number representing the index of the button to initially be set down

The attribute 'buttons' is REQUIRED. All other attributes are optional.

Parameters
loaderThe scene loader passing this JSON file
dataThe JSON object specifying the node
Returns
a newly allocated a node with the given JSON specificaton.

◆ clearListeners()

void cugl::scene2::ButtonGroup::clearListeners ( )

Clears all listeners for this button group.

These listeners are invoked when the button state changes (up or down). This method does not require you to remember the keys assigned to the individual listeners.

◆ deactivate()

bool cugl::scene2::ButtonGroup::deactivate ( )

Deactivates this button group, 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 buttons will no longer change their state on their own. However, the user can still change the state manually with the setDown(int) method. In addition, any Listener attached will still respond to manual state changes.

Returns
true if the button was successfully deactivated

◆ dispose()

virtual void cugl::scene2::ButtonGroup::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::scene2::SceneNode.

◆ doLayout()

virtual void cugl::scene2::ButtonGroup::doLayout ( )
overridevirtual

Arranges the child of this node using the layout manager.

This process occurs recursively and top-down. A layout manager may end up resizing the children. That is why the parent must finish its layout before we can apply a layout manager to the children.

Reimplemented from cugl::scene2::SceneNode.

◆ getCurrent()

const int cugl::scene2::ButtonGroup::getCurrent ( ) const
inline

Returns the index of the currently toggled button.

This method returns -1 if no button is toggled.

Returns
the index of the currently toggled button.

◆ getListener()

const Listener cugl::scene2::ButtonGroup::getListener ( Uint32  key) const

Returns the listener for the given key

This listener is invoked when the toggled button changes.

If there is no listener for the given key, it returns nullptr.

Parameters
keyThe identifier for the listener
Returns
the listener for the given key

◆ getListeners()

const std::vector< Listener > cugl::scene2::ButtonGroup::getListeners ( ) const

Returns all listeners for this button group

This listener is invoked when the toggled button changes.

Returns
the listener (if any) for this button

◆ hasListener()

bool cugl::scene2::ButtonGroup::hasListener ( ) const
inline

Returns true if this button group has a listener

This listener is invoked when the toggled button changes.

Returns
true if this button has a listener

◆ init() [1/3]

virtual bool cugl::scene2::ButtonGroup::init ( )
inlineoverridevirtual

Deactivates the default initializer.

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

Returns
false

Reimplemented from cugl::scene2::SceneNode.

◆ init() [2/3]

bool cugl::scene2::ButtonGroup::init ( const std::shared_ptr< Button > &  button,
int  initialButton = -1 
)

Initializes a button group with the given button and initial index

The new button group will have a single button, the one given, and will initially be set down if initialButton is 0. If initialButton is -1, no button is set down.

Parameters
buttonThe button in the button group
initialButtonThe index of the button to set down initially
Returns
true if the button group is initialized properly, false otherwise.

◆ init() [3/3]

bool cugl::scene2::ButtonGroup::init ( const std::vector< std::shared_ptr< Button > > &  buttons,
int  initialButton = -1 
)

Initializes a button group with the given buttons and initial index

The new button group will have all of the buttons in the given list. If initialButton is a positive number the button at that index in the list will be set down initially. If initialButton is -1, no button is set down.

Parameters
buttonsThe buttons in the button group
initialButtonThe index of the button to set down initially
Returns
true if the button group is initialized properly, false otherwise.

◆ initWithData()

bool cugl::scene2::ButtonGroup::initWithData ( const Scene2Loader loader,
const std::shared_ptr< JsonValue > &  data 
)
overridevirtual

Initializes a node with the given JSON specificaton.

This initializer is designed to receive the "data" object from the JSON passed to Scene2Loader. This JSON format supports all of the attribute values of its parent class. In addition, it supports the following additional attributes:

 "buttons":    A list of strings referencing the name of children nodes (must be buttons)
 "initial":    A number representing the index of the button to initially be set down

The attribute 'buttons' is REQUIRED. All other attributes are optional.

Parameters
loaderThe scene loader passing this JSON file
dataThe JSON object specifying the node
Returns
true if initialization was successful.

Reimplemented from cugl::scene2::SceneNode.

◆ isActive()

bool cugl::scene2::ButtonGroup::isActive ( ) const
inline

Returns true if this button group has been activated.

Returns
true if this button group has been activated.

◆ removeButton()

void cugl::scene2::ButtonGroup::removeButton ( const std::shared_ptr< Button > &  btn)

Remove a button from the button group

Parameters
btnThe button to remove from the group

◆ removeListener()

bool cugl::scene2::ButtonGroup::removeListener ( Uint32  key)

Removes a listener from this button group.

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

Listeners must be identified by the key returned by the addListener method. If this button does not have a listener for the given key, this method will fail.

Parameters
keyThe key of the listener to remove
Returns
true if the listener was succesfully removed

◆ screenToIndex() [1/2]

int cugl::scene2::ButtonGroup::screenToIndex ( const Vec2  point)

Returns the index of the button that containing the point in screen space

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.

If the click is not within a button in the group this function will return -1.

Parameters
pointThe point in screen coordinates
Returns
the index of the button that containing the point in screen space

◆ screenToIndex() [2/2]

int cugl::scene2::ButtonGroup::screenToIndex ( float  x,
float  y 
)
inline

Returns the index of the button that containing the point in screen space

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.

If the click is not within a button in the group this function will return -1.

Parameters
xThe x-value in screen coordinates
yThe y-value in screen coordinates
Returns
index of button clicked, or -1 if no button is clicked

◆ setDown()

void cugl::scene2::ButtonGroup::setDown ( int  btn)

Invokes setDown on the button at index btn

This method invokes setDown on the button at index btn with the appropriate parameter. If btn is -1, no button is down.

Parameters
btnThe index of the button to be set down

Member Data Documentation

◆ _active

bool cugl::scene2::ButtonGroup::_active
protected

Whether the button is actively checking for state changes

◆ _buttons

std::vector<std::shared_ptr<Button> > cugl::scene2::ButtonGroup::_buttons
protected

The list of buttons representing the buttons states.

This cannot be null and must have length > 0.

◆ _curButton

int cugl::scene2::ButtonGroup::_curButton
protected

The index of the current button in _buttons list.

This value tarts as -1 (none selected)

◆ _inputkey

Uint32 cugl::scene2::ButtonGroup::_inputkey
protected

The listener key when the button is checking for state changes

◆ _keyset

std::vector<std::string> cugl::scene2::ButtonGroup::_keyset
protected

Keyset to access the children (may be empty)

◆ _listeners

std::unordered_map<Uint32, Listener> cugl::scene2::ButtonGroup::_listeners
protected

The listener callbacks for state changes

◆ _mouse

bool cugl::scene2::ButtonGroup::_mouse
protected

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

◆ _nextKey

Uint32 cugl::scene2::ButtonGroup::_nextKey
protected

The next available key for a listener


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