CUGL 1.3
Cornell University Game Library
CUSlider.h
1 //
2 // CUSlider.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a slider, which allows the user to drag
6 // a knob to select a value. The slider can be spartan (a circle on a line),
7 // or it can have custom images.
8 //
9 // The slider can track its own state, relieving you of having to manually
10 // check mouse presses. However, it can only do this when the slider is part
11 // of a scene graph, as the scene graph maps mouse coordinates to screen
12 // coordinates.
13 //
14 // This class uses our standard shared-pointer architecture.
15 //
16 // 1. The constructor does not perform any initialization; it just sets all
17 // attributes to their defaults.
18 //
19 // 2. All initialization takes place via init methods, which can fail if an
20 // object is initialized more than once.
21 //
22 // 3. All allocation takes place via static constructors which return a shared
23 // pointer.
24 //
25 // CUGL MIT License:
26 // This software is provided 'as-is', without any express or implied
27 // warranty. In no event will the authors be held liable for any damages
28 // arising from the use of this software.
29 //
30 // Permission is granted to anyone to use this software for any purpose,
31 // including commercial applications, and to alter it and redistribute it
32 // freely, subject to the following restrictions:
33 //
34 // 1. The origin of this software must not be misrepresented; you must not
35 // claim that you wrote the original software. If you use this software
36 // in a product, an acknowledgment in the product documentation would be
37 // appreciated but is not required.
38 //
39 // 2. Altered source versions must be plainly marked as such, and must not
40 // be misrepresented as being the original software.
41 //
42 // 3. This notice may not be removed or altered from any source distribution.
43 //
44 // Author: Walker White and Enze Zhou
45 // Version: 1/8/18
46 //
47 #ifndef __CU_SLIDER_H__
48 #define __CU_SLIDER_H__
49 
50 #include <cugl/2d/CUNode.h>
51 #include <cugl/2d/CUButton.h>
52 
53 namespace cugl {
54 
56 #define DEFAULT_MIN 0
57 
58 #define DEFAULT_MAX 100
59 
60 #define DEFAULT_RADIUS 20
61 
62 #pragma mark -
63 #pragma mark Slider
64 
90 class Slider : public Node {
91 public:
92 #pragma mark Listener
93 
112  typedef std::function<void(const std::string& name, float value)> Listener;
113 
114 protected:
115 #pragma mark -
116 #pragma mark Values
117 
118  float _value;
121 
123  std::shared_ptr<Button> _knob;
125  std::shared_ptr<Node> _path;
130 
132  float _tick;
134  bool _snap;
135 
137  bool _active;
139  bool _mouse;
143  Uint32 _inputkey;
146 
147 public:
148 #pragma mark -
149 #pragma mark Constructors
150 
156  Slider();
157 
164  ~Slider() { dispose(); }
165 
175  virtual void dispose() override;
176 
185  virtual bool init() override {
186  return init(Vec2(DEFAULT_MIN,DEFAULT_MAX),Rect(DEFAULT_MIN,DEFAULT_RADIUS,DEFAULT_MAX,0));
187  }
188 
207  bool init(const Vec2& range, const Rect& bounds);
208 
229  bool initWithUI(const Vec2& range, const Rect& bounds,
230  const std::shared_ptr<Node>& path,
231  const std::shared_ptr<Button>& knob);
232 
256  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
257 
258 
259 #pragma mark -
260 #pragma mark Static Constructors
261 
270  static std::shared_ptr<Slider> alloc() {
271  std::shared_ptr<Slider> node = std::make_shared<Slider>();
272  return (node->init() ? node : nullptr);
273  }
274 
293  static std::shared_ptr<Slider> alloc(const Vec2& range, const Rect& bounds) {
294  std::shared_ptr<Slider> node = std::make_shared<Slider>();
295  return (node->init(range,bounds) ? node : nullptr);
296  }
297 
318  static std::shared_ptr<Slider> allocWithUI(const Vec2& range, const Rect& bounds,
319  const std::shared_ptr<Node>& path,
320  const std::shared_ptr<Button>& knob) {
321  std::shared_ptr<Slider> node = std::make_shared<Slider>();
322  return (node->initWithUI(range,bounds,path,knob) ? node : nullptr);
323  }
324 
348  static std::shared_ptr<Slider> allocWithData(const SceneLoader* loader,
349  const std::shared_ptr<JsonValue>& data) {
350  std::shared_ptr<Slider> node = std::make_shared<Slider>();
351  return (node->initWithData(loader,data) ? node : nullptr);
352  }
353 
354 
355 #pragma mark -
356 #pragma mark Slider State
357 
365  float getMinValue() const { return _range.x; }
366 
375  void setMinValue(float value) { _range.x = value; reposition(); }
376 
385  float getMaxValue() const { return _range.y; }
386 
395  void setMaxValue(float value) { _range.y = value; reposition(); }
396 
405  const Vec2& getRange() const { return _range; }
406 
415  void setRange(const Vec2& range) { _range = range; reposition(); }
416 
427  void setRange(float min, float max) { setRange(Vec2(min,max)); }
428 
434  float getValue() const { return _value; }
435 
445  void setValue(float value) { _value = validate(value); reposition(); }
446 
447 
448 #pragma mark -
449 #pragma mark Appearance
450 
458  const std::shared_ptr<Button>& getKnob() const { return _knob; }
459 
472  void setKnob(const std::shared_ptr<Button>& knob) { placeKnob(knob); reconfigure(); }
473 
482  const std::shared_ptr<Node>& getPath() const { return _path; }
483 
495  void setPath(const std::shared_ptr<Node>& path) { placePath(path); reconfigure(); }
496 
510  const Rect& getBounds() const { return _bounds; }
511 
525  void getBounds(const Rect& value) { _bounds = value; reconfigure(); }
526 
527 #pragma mark -
528 #pragma mark Tick Support
529 
545  float getTick() const { return _tick; }
546 
563  void setTick(float value) { _tick = value; reposition(); }
564 
574  bool hasSnap() const { return _snap; }
575 
585  void snapTick(bool value) { _snap = value; reposition(); }
586 
587 #pragma mark -
588 #pragma mark Listeners
589 
599  bool hasListener() const { return _listener != nullptr; }
600 
612  const Listener getListener() const { return _listener; }
613 
625  void setListener(Listener listener) { _listener = listener; }
626 
638  bool removeListener();
639 
658  bool activate(Uint32 key);
659 
673  bool deactivate();
674 
680  bool isActive() const { return _active; }
681 
682 
683 protected:
684 #pragma mark -
685 #pragma mark Internal Helpers
686 
696  float validate(float value) const;
697 
703  void reconfigure();
704 
710  void reposition();
711 
721  void dragKnob(const Vec2& pos);
722 
733  void placeKnob(const std::shared_ptr<Button>& knob);
734 
745  void placePath(const std::shared_ptr<Node>& path);
746 
747 };
748 
749 }
750 
751 #endif /* __CU_SLIDER_H__ */
cugl::Slider::alloc
static std::shared_ptr< Slider > alloc()
Definition: CUSlider.h:270
cugl::Slider::_snap
bool _snap
Definition: CUSlider.h:134
cugl::Slider::_range
Vec2 _range
Definition: CUSlider.h:120
cugl::Slider::placePath
void placePath(const std::shared_ptr< Node > &path)
cugl::Slider::dispose
virtual void dispose() override
cugl::Slider::setRange
void setRange(float min, float max)
Definition: CUSlider.h:427
cugl::Slider::_active
bool _active
Definition: CUSlider.h:137
cugl::Slider::Listener
std::function< void(const std::string &name, float value)> Listener
Definition: CUSlider.h:112
cugl::Slider::reconfigure
void reconfigure()
cugl::Slider::isActive
bool isActive() const
Definition: CUSlider.h:680
cugl::Slider::getValue
float getValue() const
Definition: CUSlider.h:434
cugl::Slider::hasListener
bool hasListener() const
Definition: CUSlider.h:599
cugl::Slider::placeKnob
void placeKnob(const std::shared_ptr< Button > &knob)
cugl::Slider::activate
bool activate(Uint32 key)
cugl::Slider::getMaxValue
float getMaxValue() const
Definition: CUSlider.h:385
cugl::Slider::getRange
const Vec2 & getRange() const
Definition: CUSlider.h:405
cugl::Slider::_listener
Listener _listener
Definition: CUSlider.h:145
cugl::Slider::_dragpos
Vec2 _dragpos
Definition: CUSlider.h:141
cugl::Slider::setPath
void setPath(const std::shared_ptr< Node > &path)
Definition: CUSlider.h:495
cugl::Slider::_tick
float _tick
Definition: CUSlider.h:132
cugl::Slider::getBounds
void getBounds(const Rect &value)
Definition: CUSlider.h:525
cugl::Vec2::x
float x
Definition: CUVec2.h:66
cugl::Slider::_knob
std::shared_ptr< Button > _knob
Definition: CUSlider.h:123
cugl::Slider::_mouse
bool _mouse
Definition: CUSlider.h:139
cugl::Slider::setMinValue
void setMinValue(float value)
Definition: CUSlider.h:375
cugl::Slider::removeListener
bool removeListener()
cugl::Rect
Definition: CURect.h:45
cugl::Slider::getTick
float getTick() const
Definition: CUSlider.h:545
cugl::Slider::alloc
static std::shared_ptr< Slider > alloc(const Vec2 &range, const Rect &bounds)
Definition: CUSlider.h:293
cugl::Slider::setListener
void setListener(Listener listener)
Definition: CUSlider.h:625
cugl::Slider::Slider
Slider()
cugl::Slider::init
virtual bool init() override
Definition: CUSlider.h:185
cugl::Slider::initWithData
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::Slider::getPath
const std::shared_ptr< Node > & getPath() const
Definition: CUSlider.h:482
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::Slider
Definition: CUSlider.h:90
cugl::Vec2::y
float y
Definition: CUVec2.h:68
cugl::Slider::hasSnap
bool hasSnap() const
Definition: CUSlider.h:574
cugl::Node
Definition: CUNode.h:92
cugl::Slider::dragKnob
void dragKnob(const Vec2 &pos)
cugl::Slider::_bounds
Rect _bounds
Definition: CUSlider.h:127
cugl::Vec2
Definition: CUVec2.h:61
cugl::Slider::validate
float validate(float value) const
cugl::Slider::~Slider
~Slider()
Definition: CUSlider.h:164
cugl::Slider::allocWithUI
static std::shared_ptr< Slider > allocWithUI(const Vec2 &range, const Rect &bounds, const std::shared_ptr< Node > &path, const std::shared_ptr< Button > &knob)
Definition: CUSlider.h:318
cugl::Slider::setKnob
void setKnob(const std::shared_ptr< Button > &knob)
Definition: CUSlider.h:472
cugl::Slider::_adjust
Rect _adjust
Definition: CUSlider.h:129
cugl::Slider::_value
float _value
Definition: CUSlider.h:118
cugl::Slider::reposition
void reposition()
cugl::Slider::getListener
const Listener getListener() const
Definition: CUSlider.h:612
cugl::Slider::getBounds
const Rect & getBounds() const
Definition: CUSlider.h:510
cugl::Slider::_inputkey
Uint32 _inputkey
Definition: CUSlider.h:143
cugl::Slider::setRange
void setRange(const Vec2 &range)
Definition: CUSlider.h:415
cugl::Slider::allocWithData
static std::shared_ptr< Slider > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUSlider.h:348
cugl::Slider::setTick
void setTick(float value)
Definition: CUSlider.h:563
cugl::Slider::snapTick
void snapTick(bool value)
Definition: CUSlider.h:585
cugl::Slider::setMaxValue
void setMaxValue(float value)
Definition: CUSlider.h:395
cugl::Slider::deactivate
bool deactivate()
cugl::Slider::setValue
void setValue(float value)
Definition: CUSlider.h:445
cugl::Slider::_path
std::shared_ptr< Node > _path
Definition: CUSlider.h:125
cugl::Slider::getMinValue
float getMinValue() const
Definition: CUSlider.h:365
cugl::Slider::getKnob
const std::shared_ptr< Button > & getKnob() const
Definition: CUSlider.h:458
cugl::Slider::initWithUI
bool initWithUI(const Vec2 &range, const Rect &bounds, const std::shared_ptr< Node > &path, const std::shared_ptr< Button > &knob)