CUGL
Cornell University Game Library
CUButton.h
1 //
2 // CUButton.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a simple clickable button. The button may
6 // either be represented by two images (one up and one down), or a single
7 // image and two different color tints.
8 //
9 // The button can track its own state, relieving you of having to manually
10 // check mouse presses. However, it can only do this when the button 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
45 // Version: 1/8/17
46 //
47 #ifndef __CU_BUTTON_H__
48 #define __CU_BUTTON_H__
49 #include <cugl/2d/CUNode.h>
50 #include <cugl/2d/CUPolygonNode.h>
51 #include <cugl/math/CUColor4.h>
52 #include <unordered_map>
53 
54 namespace cugl {
55 
56 #pragma mark -
57 #pragma mark ButtonListener
58 
77 typedef std::function<void(const std::string& name, bool down)> ButtonListener;
78 
79 #pragma mark -
80 #pragma mark Button
81 
96 class Button : public Node {
97 protected:
99  bool _down;
101  std::shared_ptr<Node> _upnode;
103  std::shared_ptr<Node> _downnode;
108 
110  bool _active;
112  bool _mouse;
114  Uint32 _inputkey;
116  ButtonListener _listener;
117 
118 
119 public:
120 #pragma mark Constructors
121 
129  Button() : _down(false), _active(false), _mouse(false), _listener(nullptr) {}
130 
134  ~Button() {}
135 
146  virtual void dispose() override;
147 
156  virtual bool init() override {
157  CUAssertLog(false,"This node does not support the empty initializer");
158  return false;
159  }
160 
171  bool init(const std::shared_ptr<Node>& up) {
172  return init(up,up->getColor()*Color4::GRAY);
173  }
174 
186  bool init(const std::shared_ptr<Node>& up, Color4 down);
187 
200  bool init(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down);
201 
202 #pragma mark Static Constructors
203 
213  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up) {
214  std::shared_ptr<Button> node = std::make_shared<Button>();
215  return (node->init(up) ? node : nullptr);
216  }
217 
229  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, Color4 down) {
230  std::shared_ptr<Button> node = std::make_shared<Button>();
231  return (node->init(up,down) ? node : nullptr);
232  }
233 
246  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down) {
247  std::shared_ptr<Button> node = std::make_shared<Button>();
248  return (node->init(up,down) ? node : nullptr);
249  }
250 
251 #pragma mark Button State
252 
263  bool containsScreen(const Vec2& point) {
264  Vec2 local = screenToNodeCoords(point);
265  return Rect(Vec2::ZERO, getContentSize()).contains(local);
266  }
267 
280  bool contains(float x, float y) {
281  Vec2 local = screenToNodeCoords(Vec2(x,y));
282  return Rect(Vec2::ZERO, getContentSize()).contains(local);
283  }
284 
296  bool isDown() const { return _down; }
297 
309  void setDown(bool down);
310 
311 
312 #pragma mark Listeners
313 
322  bool hasListener() const { return _listener != nullptr; }
323 
334  const ButtonListener getListener() const { return _listener; }
335 
346  void setListener(ButtonListener listener) { _listener = listener; }
347 
358  bool removeListener();
359 
378  bool activate(Uint32 key);
379 
395  bool deactivate();
396 };
397 
398 }
399 
400 #endif /* __CU_BUTTON_H__ */
bool removeListener()
bool contains(const Rect &rect) const
std::shared_ptr< Node > _downnode
Definition: CUButton.h:103
Vec2 screenToNodeCoords(const Vec2 &screenPoint) const
Color4 _upcolor
Definition: CUButton.h:105
Definition: CUVec2.h:61
bool hasListener() const
Definition: CUButton.h:322
~Button()
Definition: CUButton.h:134
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, Color4 down)
Definition: CUButton.h:229
bool init(const std::shared_ptr< Node > &up)
Definition: CUButton.h:171
void setListener(ButtonListener listener)
Definition: CUButton.h:346
bool _mouse
Definition: CUButton.h:112
virtual bool init() override
Definition: CUButton.h:156
bool _down
Definition: CUButton.h:99
Definition: CUNode.h:92
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, const std::shared_ptr< Node > &down)
Definition: CUButton.h:246
ButtonListener _listener
Definition: CUButton.h:116
void setDown(bool down)
bool containsScreen(const Vec2 &point)
Definition: CUButton.h:263
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up)
Definition: CUButton.h:213
bool isDown() const
Definition: CUButton.h:296
Definition: CUButton.h:96
const Size & getContentSize() const
Definition: CUNode.h:652
bool deactivate()
bool activate(Uint32 key)
const ButtonListener getListener() const
Definition: CUButton.h:334
Color4 _downcolor
Definition: CUButton.h:107
std::function< void(const std::string &name, bool down)> ButtonListener
Definition: CUButton.h:77
bool _active
Definition: CUButton.h:110
static const Color4 GRAY
Definition: CUColor4.h:1144
virtual void dispose() override
bool contains(float x, float y)
Definition: CUButton.h:280
Definition: CURect.h:45
Uint32 _inputkey
Definition: CUButton.h:114
Definition: CUColor4.h:1104
Definition: CUAnimationNode.h:52
static const Vec2 ZERO
Definition: CUVec2.h:71
std::shared_ptr< Node > _upnode
Definition: CUButton.h:101
Button()
Definition: CUButton.h:129