CUGL 1.1
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 zlib 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 SliderListener
64 
83 typedef std::function<void(const std::string& name, float value)> SliderListener;
84 
85 
86 #pragma mark -
87 #pragma mark Slider
88 
114 class Slider : public Node {
115 protected:
116 #pragma mark -
117 #pragma mark Values
118 
119  float _value;
122 
124  std::shared_ptr<Button> _knob;
126  std::shared_ptr<Node> _path;
131 
133  float _tick;
135  bool _snap;
136 
138  bool _active;
140  bool _mouse;
144  Uint32 _inputkey;
146  SliderListener _listener;
147 
148 public:
149 #pragma mark -
150 #pragma mark Constructors
151 
157  Slider();
158 
165  ~Slider() { dispose(); }
166 
176  virtual void dispose() override;
177 
186  virtual bool init() override {
187  return init(Vec2(DEFAULT_MIN,DEFAULT_MAX),Rect(DEFAULT_MIN,DEFAULT_RADIUS,DEFAULT_MAX,0));
188  }
189 
208  bool init(const Vec2& range, const Rect& bounds);
209 
230  bool initWithUI(const Vec2& range, const Rect& bounds,
231  const std::shared_ptr<Node>& path,
232  const std::shared_ptr<Button>& knob);
233 
262  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
263 
264 
265 #pragma mark -
266 #pragma mark Static Constructors
267 
276  static std::shared_ptr<Slider> alloc() {
277  std::shared_ptr<Slider> node = std::make_shared<Slider>();
278  return (node->init() ? node : nullptr);
279  }
280 
299  static std::shared_ptr<Slider> alloc(const Vec2& range, const Rect& bounds) {
300  std::shared_ptr<Slider> node = std::make_shared<Slider>();
301  return (node->init(range,bounds) ? node : nullptr);
302  }
303 
324  static std::shared_ptr<Slider> allocWithUI(const Vec2& range, const Rect& bounds,
325  const std::shared_ptr<Node>& path,
326  const std::shared_ptr<Button>& knob) {
327  std::shared_ptr<Slider> node = std::make_shared<Slider>();
328  return (node->initWithUI(range,bounds,path,knob) ? node : nullptr);
329  }
330 
359  static std::shared_ptr<Slider> allocWithData(const SceneLoader* loader,
360  const std::shared_ptr<JsonValue> data) {
361  std::shared_ptr<Slider> node = std::make_shared<Slider>();
362  return (node->initWithData(loader,data) ? node : nullptr);
363  }
364 
365 
366 #pragma mark -
367 #pragma mark Slider State
368 
376  float getMinValue() const { return _range.x; }
377 
386  void setMinValue(float value) { _range.x = value; reposition(); }
387 
396  float getMaxValue() const { return _range.y; }
397 
406  void setMaxValue(float value) { _range.y = value; reposition(); }
407 
416  const Vec2& getRange() const { return _range; }
417 
426  void setRange(const Vec2& range) { _range = range; reposition(); }
427 
438  void setRange(float min, float max) { setRange(Vec2(min,max)); }
439 
445  float getValue() const { return _value; }
446 
456  void setValue(float value) { _value = validate(value); reposition(); }
457 
458 
459 #pragma mark -
460 #pragma mark Appearance
461 
469  const std::shared_ptr<Button>& getKnob() const { return _knob; }
470 
483  void setKnob(const std::shared_ptr<Button>& knob) { placeKnob(knob); reconfigure(); }
484 
493  const std::shared_ptr<Node>& getPath() const { return _path; }
494 
506  void setPath(const std::shared_ptr<Node>& path) { placePath(path); reconfigure(); }
507 
521  const Rect& getBounds() const { return _bounds; }
522 
536  void getBounds(const Rect& value) { _bounds = value; reconfigure(); }
537 
538 #pragma mark -
539 #pragma mark Tick Support
540 
556  float getTick() const { return _tick; }
557 
574  void setTick(float value) { _tick = value; reposition(); }
575 
585  bool hasSnap() const { return _snap; }
586 
596  void snapTick(bool value) { _snap = value; reposition(); }
597 
598 #pragma mark -
599 #pragma mark Listeners
600 
610  bool hasListener() const { return _listener != nullptr; }
611 
623  const SliderListener getListener() const { return _listener; }
624 
636  void setListener(SliderListener listener) { _listener = listener; }
637 
649  bool removeListener();
650 
669  bool activate(Uint32 key);
670 
684  bool deactivate();
685 
691  bool isActive() const { return _active; }
692 
693 
694 protected:
695 #pragma mark -
696 #pragma mark Internal Helpers
697 
707  float validate(float value) const;
708 
714  void reconfigure();
715 
721  void reposition();
722 
732  void dragKnob(const Vec2& pos);
733 
744  void placeKnob(const std::shared_ptr<Button>& knob);
745 
756  void placePath(const std::shared_ptr<Node>& path);
757 
758 };
759 
760 }
761 
762 #endif /* __CU_SLIDER_H__ */
float _tick
Definition: CUSlider.h:133
float getMinValue() const
Definition: CUSlider.h:376
float x
Definition: CUVec2.h:66
void reconfigure()
float y
Definition: CUVec2.h:68
bool hasSnap() const
Definition: CUSlider.h:585
virtual void dispose() override
SliderListener _listener
Definition: CUSlider.h:146
const Rect & getBounds() const
Definition: CUSlider.h:521
void setRange(const Vec2 &range)
Definition: CUSlider.h:426
static std::shared_ptr< Slider > alloc(const Vec2 &range, const Rect &bounds)
Definition: CUSlider.h:299
Definition: CUVec2.h:61
Vec2 _range
Definition: CUSlider.h:121
virtual bool init() override
Definition: CUSlider.h:186
std::shared_ptr< Button > _knob
Definition: CUSlider.h:124
static std::shared_ptr< Slider > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUSlider.h:359
float getMaxValue() const
Definition: CUSlider.h:396
float _value
Definition: CUSlider.h:119
Definition: CUNode.h:92
bool _mouse
Definition: CUSlider.h:140
void snapTick(bool value)
Definition: CUSlider.h:596
void getBounds(const Rect &value)
Definition: CUSlider.h:536
const Vec2 & getRange() const
Definition: CUSlider.h:416
bool deactivate()
bool _snap
Definition: CUSlider.h:135
const SliderListener getListener() const
Definition: CUSlider.h:623
float validate(float value) const
float getValue() const
Definition: CUSlider.h:445
bool initWithUI(const Vec2 &range, const Rect &bounds, const std::shared_ptr< Node > &path, const std::shared_ptr< Button > &knob)
void setPath(const std::shared_ptr< Node > &path)
Definition: CUSlider.h:506
void setMinValue(float value)
Definition: CUSlider.h:386
void placeKnob(const std::shared_ptr< Button > &knob)
bool hasListener() const
Definition: CUSlider.h:610
void dragKnob(const Vec2 &pos)
void setKnob(const std::shared_ptr< Button > &knob)
Definition: CUSlider.h:483
void placePath(const std::shared_ptr< Node > &path)
void setListener(SliderListener listener)
Definition: CUSlider.h:636
bool _active
Definition: CUSlider.h:138
Rect _adjust
Definition: CUSlider.h:130
std::function< void(const std::string &name, float value)> SliderListener
Definition: CUSlider.h:83
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:324
void reposition()
Definition: CURect.h:45
void setRange(float min, float max)
Definition: CUSlider.h:438
Definition: CUSceneLoader.h:77
static std::shared_ptr< Slider > alloc()
Definition: CUSlider.h:276
~Slider()
Definition: CUSlider.h:165
bool activate(Uint32 key)
const std::shared_ptr< Node > & getPath() const
Definition: CUSlider.h:493
const std::shared_ptr< Button > & getKnob() const
Definition: CUSlider.h:469
void setValue(float value)
Definition: CUSlider.h:456
std::shared_ptr< Node > _path
Definition: CUSlider.h:126
Definition: CUSlider.h:114
void setTick(float value)
Definition: CUSlider.h:574
Uint32 _inputkey
Definition: CUSlider.h:144
bool isActive() const
Definition: CUSlider.h:691
Definition: CUAction.h:51
bool removeListener()
float getTick() const
Definition: CUSlider.h:556
Rect _bounds
Definition: CUSlider.h:128
void setMaxValue(float value)
Definition: CUSlider.h:406
Vec2 _dragpos
Definition: CUSlider.h:142
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override