CUGL 1.3
Cornell University Game Library
|
#include <CUSlider.h>
Public Types | |
typedef std::function< void(const std::string &name, float value)> | Listener |
Public Member Functions | |
Slider () | |
~Slider () | |
virtual void | dispose () override |
virtual bool | init () override |
bool | init (const Vec2 &range, const Rect &bounds) |
bool | initWithUI (const Vec2 &range, const Rect &bounds, const std::shared_ptr< Node > &path, const std::shared_ptr< Button > &knob) |
bool | initWithData (const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override |
float | getMinValue () const |
void | setMinValue (float value) |
float | getMaxValue () const |
void | setMaxValue (float value) |
const Vec2 & | getRange () const |
void | setRange (const Vec2 &range) |
void | setRange (float min, float max) |
float | getValue () const |
void | setValue (float value) |
const std::shared_ptr< Button > & | getKnob () const |
void | setKnob (const std::shared_ptr< Button > &knob) |
const std::shared_ptr< Node > & | getPath () const |
void | setPath (const std::shared_ptr< Node > &path) |
const Rect & | getBounds () const |
void | getBounds (const Rect &value) |
float | getTick () const |
void | setTick (float value) |
bool | hasSnap () const |
void | snapTick (bool value) |
bool | hasListener () const |
const Listener | getListener () const |
void | setListener (Listener listener) |
bool | removeListener () |
bool | activate (Uint32 key) |
bool | deactivate () |
bool | isActive () const |
Public Member Functions inherited from cugl::Node | |
Node () | |
~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 Node * | copy (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 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) |
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 Vec2 & | getAnchor () const |
Vec2 | getAnchorInPixels () |
Color4 | getColor () const |
virtual void | setColor (Color4 color) |
Color4 | getAbsoluteColor () |
bool | isVisible () const |
void | setVisible (bool visible) |
bool | hasRelativeColor () |
void | setRelativeColor (bool flag) |
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 Mat4 & | getAlternateTransform () |
void | setAlternateTransform (const Mat4 &transform) |
bool | withAlternateTransform () |
void | chooseAlternateTransform (bool active) |
const Mat4 & | getNodeToParentTransform () 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< Node > | getChild (unsigned int pos) |
const std::shared_ptr< Node > & | getChild (unsigned int pos) const |
template<typename T > | |
std::shared_ptr< T > | getChild (unsigned int pos) const |
std::shared_ptr< Node > | getChildByTag (unsigned int tag) const |
template<typename T > | |
std::shared_ptr< T > | getChildByTag (unsigned int tag) const |
std::shared_ptr< Node > | getChildByName (const std::string &name) const |
template<typename T > | |
std::shared_ptr< T > | getChildByName (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) |
Node * | getParent () |
const Node * | getParent () const |
Scene * | getScene () |
const Scene * | getScene () 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 () |
virtual void | render (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) |
virtual void | render (const std::shared_ptr< SpriteBatch > &batch) |
virtual void | draw (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &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< Slider > | alloc () |
static std::shared_ptr< Slider > | alloc (const Vec2 &range, const Rect &bounds) |
static std::shared_ptr< Slider > | allocWithUI (const Vec2 &range, const Rect &bounds, const std::shared_ptr< Node > &path, const std::shared_ptr< Button > &knob) |
static std::shared_ptr< Slider > | allocWithData (const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) |
Static Public Member Functions inherited from cugl::Node | |
static std::shared_ptr< Node > | alloc () |
static std::shared_ptr< Node > | allocWithPosition (const Vec2 &pos) |
static std::shared_ptr< Node > | allocWithPosition (float x, float y) |
static std::shared_ptr< Node > | allocWithBounds (const Size &size) |
static std::shared_ptr< Node > | allocWithBounds (float width, float height) |
static std::shared_ptr< Node > | allocWithBounds (const Rect &rect) |
static std::shared_ptr< Node > | allocWithBounds (float x, float y, float width, float height) |
static std::shared_ptr< Node > | allocWithData (const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) |
Protected Member Functions | |
float | validate (float value) const |
void | reconfigure () |
void | reposition () |
void | dragKnob (const Vec2 &pos) |
void | placeKnob (const std::shared_ptr< Button > &knob) |
void | placePath (const std::shared_ptr< Node > &path) |
Protected Attributes | |
float | _value |
Vec2 | _range |
std::shared_ptr< Button > | _knob |
std::shared_ptr< Node > | _path |
Rect | _bounds |
Rect | _adjust |
float | _tick |
bool | _snap |
bool | _active |
bool | _mouse |
Vec2 | _dragpos |
Uint32 | _inputkey |
Listener | _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 |
std::shared_ptr< Layout > | _layout |
int | _childOffset |
unsigned int | _tag |
std::string | _name |
size_t | _hashOfName |
int | _zOrder |
bool | _zDirty |
This class represents a slider, allowing the user to drag a knob to select a value.
A slider is defined by a knob and path. Each of these are a distinct scene graph node. If these are not specified, the class constructs a simple slider with a circle on a line.
The most important attribute for a slider is the bounds attribute. This rectangle defines the slideable region inside of the path node. This allows us to have complex path nodes with tick marks and other features that prevent ths slider from being centered in the node. It also allows us to define sliders with any orientation. The bottom left corner of the bounds rectangle is the minimum value while the top right is the maximum.
The user can also specify a tick interval that allows the slider to snap to predefined values. This is useful in preventing the slider values from being resolution dependent. However, the class will not automatically disolay tick marks. If you wish to display tick marks, you must add them to the path node.
The slider can track its own state, via the activate(Uint32) method, relieving you of having to manually check presses and drags. However, the appropriate input device must be active before you can activate the slider, as it needs to attach internal listeners.
This type represents a listener for a value change in Slider.
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, Slider 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, float value)>
name | The alider name |
value | Changed value of slider |
cugl::Slider::Slider | ( | ) |
Creates an uninitialized slider. You must initialize it 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.
|
inline |
Deletes this slider, disposing all resources
It is unsafe to call this on a slider that is still currently inside of a scene graph.
bool cugl::Slider::activate | ( | Uint32 | key | ) |
Activates this slider to enable dragging.
This method attaches a listener to either the Mouse or Touchscreen inputs to monitor when the slider is dragged. The slider 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 slider will change its value on its own, without requiring the user to use setValue(float). If there is a Listener attached, it will call that function upon any state changes.
key | The listener key for the input device |
|
inlinestatic |
Returns a newly allocated slider with the default values.
This initializer will create a horizontal slider from 0 to 100. It will assign one pixel to each value. The knob radius will be 20.
|
inlinestatic |
Returns a newly allocated slider with given bounds.
The slider visuals will be interpretted from bounds. The knob will be a circle whose radius is the maximum of x and y, where (x,y) is the bounds origin. The path will be a simple line, but it will be surrounded by a transparent "track" which tightly fits the knob.
The range is the slider value range. The x value is the minimum value (corresponding to the bottom left corner of bounds) and the y value is the maximum value (corresponding to the top right corner of bounds). The slider will start at the middle value.
range | The slider value range |
bounds | The slider path |
|
inlinestatic |
Returns a newly allocated node with the given JSON specificaton.
This initializer is designed to receive the "data" object from the JSON passed to SceneLoader. This JSON format supports all of the attribute values of its parent class. In addition, it supports the following additional attributes:
"bounds": A 4-element array of numbers (x,y,width,height) "range": A 2-element array of numbers (min,max) "value': A number representing the initial value "tick': A number greater than 0, representing the tick period "snap": A boolean indicating whether to snap to a nearest tick "knob": A JSON object defining a scene graph node "path": A JSON object defining a scene graph node OR
The attribute 'bounds' is REQUIRED. All other attributes are optional.
loader | The scene loader passing this JSON file |
data | The JSON object specifying the node |
|
inlinestatic |
Returns a newly allocated slider with given scene graph nodes.
The slider visuals will be taken from the scene graph nodes knob and path. The rectangle bounds should define an interior region of path. The knob graph node can be slid from the origing of bounds to the top right corner.
The range is the slider value range. The x value is the minimum value (corresponding to the bottom left corner of bounds) and the y value is the maximum value (corresponding to the top right corner of bounds). The slider will start at the middle value.
range | The slider value range |
bounds | The slider path |
path | The scene graph node for the path |
knob | The scene graph node for the knob |
bool cugl::Slider::deactivate | ( | ) |
Deactivates this slider, unable to drag from then on.
This method removes its internal listener from either the Mouse or Touchscreen.
When deactivated, the slider will no longer change value on its own. However, the user can still change manually with the setValue(float) method. In addition, any Listener attached will still respond to manual state changes.
|
overridevirtual |
Disposes all of the resources used.
A disposed slider can be safely reinitialized. Any child will be released. They will be deleted if no other object owns them.
It is unsafe to call this on a slider that is still currently inside of a scene graph.
Reimplemented from cugl::Node.
|
protected |
Drags the knob to the given position.
This method is called by the touch listeners and assumes that an initial drag anchor has been set. The position defines a drag vector that is projected on to the sliding bounds.
pos | The position to drag to (if in the sliding bounds) |
|
inline |
Returns the sliding bounds
This rectangle defines the slideable region inside of the path node. The bottom left corner of the bounds rectangle is the minimum value while the top right is the maximum. While the origin should have positive values either the width or height may be negative.
The bounds should be inside of the bounding box of the path node. However, this is not enforced.
|
inline |
Sets the sliding bounds
This rectangle defines the slideable region inside of the path node. The bottom left corner of the bounds rectangle is the minimum value while the top right is the maximum. While the origin should have positive values either the width or height may be negative.
The bounds should be inside of the bounding box of the path node. However, this is not enforced.
value | The new sliding bounds |
|
inline |
Returns the scene graph node for the knob.
It is safe to make changes to the knob so long as you do not resize it it or scale it. Doing so will mess up the slider visuals.
|
inline |
Returns the listener (if any) for this slider
This listener is invoked when value changes, whether active or not.
A slider may only have one listener at a time. If there is no listener, this method returns nullptr.
|
inline |
Returns the maximum possible slider value.
This is the value when the slider is at the top right corner of the bounds.
|
inline |
Returns the minimum possible slider value.
This is the value when the slider is at the bottom left corner of the bounds.
|
inline |
Returns the scene graph node for the path.
It is safe to make changes to the path so long as you do not resize it it or scale it. Doing so will mess up the slider visuals.
|
inline |
Returns the range of possible slider values.
The x coordinate is the minimum value while the y coordinate is the maximum value.
|
inline |
Returns the tick period of this slider.
The tick period is used to set a course granularity on the slider. Without it, each pixel is essentially its own value. However, the tick period is irrelevant unless the slider is set to snap to a tick.
When the slider does snap to a tick, it snaps to the nearest value min + k * tick where k is an non-negative integer. If this value is greater than max, then it will snap to the max.
The tick period should be nonnegative, but this is not enforced. A negative tick value will always snap to the minimum value.
|
inline |
Returns the current slider value.
|
inline |
Returns true if this slider has a listener
This listener is invoked when value changes, whether active or not.
A slider may only have one listener at a time.
|
inline |
Returns whether the slider will snap to a tick mark.
When the slider does snap to a tick, it snaps to the nearest value min + k * tick where k is an non-negative integer. If this value is greater than max, then it will snap to the max.
|
inlineoverridevirtual |
Initializes a slider with the default values.
This initializer will create a horizontal slider from 0 to 100. It will assign one pixel to each value. The knob radius will be 20.
Reimplemented from cugl::Node.
Initializes a slider with given bounds.
The slider visuals will be interpretted from bounds. The knob will be a circle whose radius is the maximum of x and y, where (x,y) is the bounds origin. The path will be a simple line, but it will be surrounded by a transparent "track" which tightly fits the knob.
The range is the slider value range. The x value is the minimum value (corresponding to the bottom left corner of bounds) and the y value is the maximum value (corresponding to the top right corner of bounds). The slider will start at the middle value.
range | The slider value range |
bounds | The slider path |
|
overridevirtual |
Initializes a node with the given JSON specificaton.
This initializer is designed to receive the "data" object from the JSON passed to SceneLoader. This JSON format supports all of the attribute values of its parent class. In addition, it supports the following additional attributes:
"bounds": A 4-element array of numbers (x,y,width,height) "range": A 2-element array of numbers (min,max) "value': A number representing the initial value "tick': A number greater than 0, representing the tick period "snap": A boolean indicating whether to snap to a nearest tick "knob": A JSON object defining a scene graph node "path": A JSON object defining a scene graph node OR
The attribute 'bounds' is REQUIRED. All other attributes are optional.
loader | The scene loader passing this JSON file |
data | The JSON object specifying the node |
Reimplemented from cugl::Node.
bool cugl::Slider::initWithUI | ( | const Vec2 & | range, |
const Rect & | bounds, | ||
const std::shared_ptr< Node > & | path, | ||
const std::shared_ptr< Button > & | knob | ||
) |
Initializes a slider with given scene graph nodes.
The slider visuals will be taken from the scene graph nodes knob and path. The rectangle bounds should define an interior region of path. The knob graph node can be slid from the origing of bounds to the top right corner.
The range is the slider value range. The x value is the minimum value (corresponding to the bottom left corner of bounds) and the y value is the maximum value (corresponding to the top right corner of bounds). The slider will start at the middle value.
range | The slider value range |
bounds | The slider path |
path | The scene graph node for the path |
knob | The scene graph node for the knob |
|
inline |
Returns true if this slider has been activated.
|
protected |
Sets the scene graph node for the knob.
If this value is nullptr, the method will construct a default knob scene graph consisting of a simple circle.
Unlike setKnob(), this does not resize the bounding box.
knob | The new scene graph node for the knob. |
|
protected |
Sets the scene graph node for the path.
If this value is nullptr, the method will construct a default path scene graph consisting of a simple line and a semi-transparent track.
Unlike setPath(), this does not resize the bounding box.
path | The new scene graph node for the path. |
|
protected |
Resizes the node and arranges the position of the knob and path.
This method is called whenever the bounds or scene graph changes.
bool cugl::Slider::removeListener | ( | ) |
Removes the listener for this slider.
This listener is invoked when value changes, whether active or not.
A slider may only have one listener at a time. If slider does not have a listener, this method will fail.
|
protected |
Repositions the knob to reflect a change in value.
This method is called whenever the value or its range changes.
|
inline |
Sets the scene graph node for the knob.
If this value is nullptr, the method will construct a default knob scene graph consisting of a simple circle.
Changing the knob may resize the bounding box of the slider. The slider tries to ensure that the knob remains inside of the bounding box no matter its position.
knob | The new scene graph node for the knob. |
|
inline |
Sets the listener for this slider.
This listener is invoked when value changes, whether active or not.
A slider may only have one listener at a time. If this slider already has a listener, this method will replace it.
listener | The listener to use |
|
inline |
Sets the maximum possible slider value.
This is the value when the slider is at the top right corner of the bounds.
value | The maximum possible slider value. |
|
inline |
Sets the minimum possible slider value.
This is the value when the slider is at the bottom left corner of the bounds.
value | The minimum possible slider value. |
|
inline |
Sets the scene graph node for the path.
If this value is nullptr, the method will construct a default path scene graph consisting of a simple line and a semi-transparent track.
Changing the knob may resize the bounding box of the slider. The slider tries to ensure that the entire path remains inside of the bounding box.
path | The new scene graph node for the path. |
|
inline |
Sets the range of possible slider values.
The x coordinate is the minimum value while the y coordinate is the maximum value.
range | The range of possible slider values. |
|
inline |
Sets the range of possible slider values.
The minimum is the value when the slider is at the bottom left corner of the bounds and the maximum is when the slider is in the top right corner.
min | The minimum possible slider value. |
max | The maximum possible slider value. |
|
inline |
Sets the tick period of this slider.
The tick period is used to set a course granularity on the slider. Without it, each pixel is essentially its own value. However, the tick period is irrelevant unless the slider is set to snap to a tick.
When the slider does snap to a tick, it snaps to the nearest value min + k * tick where k is an non-negative integer. If this value is greater than max, then it will snap to the max.
The tick period should be nonnegative, but this is not enforced. A negative tick value will always snap to the minimum value.
value | The tick period of this slider. |
|
inline |
Sets the current slider value.
If the slider is set to snap to ticks, this will assign the value to the nearest tick. In addition, if the value is out of range, it will snap to the nearest value in range.
value | The slider value |
|
inline |
Sets whether the slider will snap to a tick mark.
When the slider does snap to a tick, it snaps to the nearest value min + k * tick where k is an non-negative integer. If this value is greater than max, then it will snap to the max.
value | Whether the slider will snap to a tick mark. |
|
protected |
Returns the correct value nearest the given one.
This method is used to snap values to the grid of ticks, as well as keep the value in range.
value | The candidate value |
|
protected |
Whether the slider is actively checking input
|
protected |
The adjusted slider path, if padding is necessary
|
protected |
The slider path, defined relative to the background widget
|
protected |
The anchoring touch or mouse position in a drag
|
protected |
The listener key when the text field is checking for events
|
protected |
The knob widget for this slider
|
protected |
Listener for this slider, which will be called when value is changed
|
protected |
Whether we are using the mouse (as opposed to the touch screen)
|
protected |
The background widget for this slider
|
protected |
The value range of the slider (x is min and y is max)
|
protected |
Whether to snap the slider to a tick value
|
protected |
The (optional) )tick period for this slider
|
protected |
The current value of the slider