CUGL 2.1
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< SceneNode > &path, const std::shared_ptr< Button > &knob) |
bool | initWithData (const Scene2Loader *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< SceneNode > & | getPath () const |
void | setPath (const std::shared_ptr< SceneNode > &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 (Uint32 key) const |
const std::vector< Listener > | getListeners () 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 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 std::shared_ptr< SceneNode > | copy (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 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< Scissor > | getScissor () 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 Affine2 & | getTransform () const |
const Affine2 & | getAlternateTransform () const |
void | setAlternateTransform (const Affine2 &transform) |
bool | withAlternateTransform () |
void | chooseAlternateTransform (bool active) |
const Affine2 & | getNodeToParentTransform () const |
Affine2 | getParentToNodeTransform () const |
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< SceneNode > | getChild (unsigned int pos) |
const std::shared_ptr< SceneNode > & | getChild (unsigned int pos) const |
template<typename T > | |
std::shared_ptr< T > | getChild (unsigned int pos) const |
std::shared_ptr< SceneNode > | getChildByTag (unsigned int tag) const |
template<typename T > | |
std::shared_ptr< T > | getChildByTag (unsigned int tag) const |
std::shared_ptr< SceneNode > | getChildByName (const std::string name) const |
template<typename T > | |
std::shared_ptr< T > | getChildByName (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) |
SceneNode * | getParent () |
const SceneNode * | getParent () const |
Scene2 * | getScene () |
const Scene2 * | getScene () 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< 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< SceneNode > &path, const std::shared_ptr< Button > &knob) |
static std::shared_ptr< SceneNode > | allocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data) |
Static Public Member Functions inherited from cugl::scene2::SceneNode | |
static std::shared_ptr< SceneNode > | alloc () |
static std::shared_ptr< SceneNode > | allocWithPosition (const Vec2 pos) |
static std::shared_ptr< SceneNode > | allocWithPosition (float x, float y) |
static std::shared_ptr< SceneNode > | allocWithBounds (const Size size) |
static std::shared_ptr< SceneNode > | allocWithBounds (float width, float height) |
static std::shared_ptr< SceneNode > | allocWithBounds (const Rect rect) |
static std::shared_ptr< SceneNode > | allocWithBounds (float x, float y, float width, float height) |
static std::shared_ptr< SceneNode > | allocWithData (const Scene2Loader *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< SceneNode > &path) |
Protected Attributes | |
float | _value |
Vec2 | _range |
std::shared_ptr< Button > | _knob |
std::shared_ptr< SceneNode > | _path |
Rect | _bounds |
Rect | _adjust |
float | _tick |
bool | _snap |
bool | _active |
bool | _mouse |
Vec2 | _dragpos |
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 |
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() 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::scene2::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::scene2::Slider::activate | ( | ) |
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.
Uint32 cugl::scene2::Slider::addListener | ( | Listener | listener | ) |
Returns a key for a listener after adding it to this button.
This listener is invoked when the slider value changes.
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.
listener | The listener to add |
|
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 Scene2Loader. 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 |
void cugl::scene2::Slider::clearListeners | ( | ) |
Clears all listeners for this slider.
These listeners are invoked when the slider value changes. This method does not require you to remember the keys assigned to the individual listeners.
bool cugl::scene2::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::scene2::SceneNode.
|
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.
const Listener cugl::scene2::Slider::getListener | ( | Uint32 | key | ) | const |
Returns the listener for the given key
This listener is invoked when the slider value changes.
If there is no listener for the given key, it returns nullptr.
key | The identifier for the listener |
const std::vector<Listener> cugl::scene2::Slider::getListeners | ( | ) | const |
Returns all listeners for this slider
These listeners are invoked when the slider value changes.
|
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 the slider value changes.
|
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::scene2::SceneNode.
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 Scene2Loader. 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::scene2::SceneNode.
bool cugl::scene2::Slider::initWithUI | ( | const Vec2 | range, |
const Rect | bounds, | ||
const std::shared_ptr< SceneNode > & | 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::scene2::Slider::removeListener | ( | Uint32 | key | ) |
Removes a listener from this slider.
This listener is invoked when the button state changes (up or down).
Listeners must be identified by the key returned by the addListener method. If this slider does not have a listener for the given key, this method will fail.
key | The key of the listener to remove |
|
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 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 next available key for a listener
|
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