CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 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
45 // Version: 10/28/18
46 //
47 #ifndef __CU_BUTTON_H__
48 #define __CU_BUTTON_H__
49 #include <cugl/assets/CUJsonValue.h>
50 #include <cugl/2d/CUNode.h>
51 #include <cugl/2d/CUPolygonNode.h>
52 #include <cugl/math/CUColor4.h>
53 #include <unordered_map>
54 
55 namespace cugl {
56 
57 #pragma mark -
58 #pragma mark Button
59 
82 class Button : public Node {
83 public:
84 #pragma mark Listener
85 
103  typedef std::function<void(const std::string& name, bool down)> Listener;
104 
105 protected:
107  bool _down;
108  bool _toggle;
109 
111  std::shared_ptr<Node> _upnode;
113  std::shared_ptr<Node> _downnode;
115  std::shared_ptr<JsonValue> _upform;
117  std::shared_ptr<JsonValue> _downform;
123  std::string _upchild;
124  std::string _downchild;
125 
128 
130  bool _active;
132  bool _mouse;
134  Uint32 _inputkey;
136  Listener _listener;
137 
138 public:
139 #pragma mark Constructors
140 
148  Button();
149 
153  ~Button() { dispose(); }
154 
165  virtual void dispose() override;
166 
175  virtual bool init() override {
176  CUAssertLog(false,"This node does not support the empty initializer");
177  return false;
178  }
179 
190  bool init(const std::shared_ptr<Node>& up) {
191  return init(up,up->getColor()*Color4::GRAY);
192  }
193 
205  bool init(const std::shared_ptr<Node>& up, Color4 down);
206 
219  bool init(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down);
220 
241  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
242 
243 #pragma mark Static Constructors
244 
254  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up) {
255  std::shared_ptr<Button> node = std::make_shared<Button>();
256  return (node->init(up) ? node : nullptr);
257  }
258 
270  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, Color4 down) {
271  std::shared_ptr<Button> node = std::make_shared<Button>();
272  return (node->init(up,down) ? node : nullptr);
273  }
274 
287  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down) {
288  std::shared_ptr<Button> node = std::make_shared<Button>();
289  return (node->init(up,down) ? node : nullptr);
290  }
291 
312  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
313  const std::shared_ptr<JsonValue>& data) {
314  std::shared_ptr<Button> result = std::make_shared<Button>();
315  if (!result->initWithData(loader,data)) { result = nullptr; }
316  return std::dynamic_pointer_cast<Node>(result);
317  }
318 
319 #pragma mark Button Attributes
320 
331  virtual void setColor(Color4 color) override;
332 
342  const Poly2& getPushable() const { return _bounds; }
343 
353  void setPushable(const Poly2& bounds);
354 
365  void setPushable(const std::vector<Vec2>& vertices);
366 
374  virtual void doLayout() override;
375 
376 #pragma mark Button State
377 
388  bool containsScreen(const Vec2& point);
389 
402  bool containsScreen(float x, float y) {
403  return containsScreen(Vec2(x,y));
404  }
405 
417  bool isDown() const { return _down; }
418 
430  void setDown(bool down);
431 
443  bool isToggle() const { return _toggle; }
444 
456  void setToggle(bool value) { _toggle = value; }
457 
458 #pragma mark Listeners
459 
468  bool hasListener() const { return _listener != nullptr; }
469 
480  const Listener getListener() const { return _listener; }
481 
492  void setListener(Listener listener) { _listener = listener; }
493 
504  bool removeListener();
505 
524  bool activate(Uint32 key);
525 
541  bool deactivate();
542 
548  bool isActive() const { return _active; }
549 };
550 
551 }
552 
553 #endif /* __CU_BUTTON_H__ */
bool removeListener()
virtual void doLayout() override
std::shared_ptr< JsonValue > _downform
Definition: CUButton.h:117
std::shared_ptr< Node > _downnode
Definition: CUButton.h:113
virtual void setColor(Color4 color) override
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
bool isActive() const
Definition: CUButton.h:548
Definition: CUPoly2.h:115
Color4 _upcolor
Definition: CUButton.h:119
std::function< void(const std::string &name, bool down)> Listener
Definition: CUButton.h:103
Definition: CUVec2.h:61
bool hasListener() const
Definition: CUButton.h:468
~Button()
Definition: CUButton.h:153
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, Color4 down)
Definition: CUButton.h:270
bool init(const std::shared_ptr< Node > &up)
Definition: CUButton.h:190
bool containsScreen(float x, float y)
Definition: CUButton.h:402
bool _mouse
Definition: CUButton.h:132
virtual bool init() override
Definition: CUButton.h:175
Listener _listener
Definition: CUButton.h:136
bool _down
Definition: CUButton.h:107
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:287
const Poly2 & getPushable() const
Definition: CUButton.h:342
std::string _upchild
Definition: CUButton.h:123
void setPushable(const Poly2 &bounds)
void setDown(bool down)
bool containsScreen(const Vec2 &point)
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up)
Definition: CUButton.h:254
bool isDown() const
Definition: CUButton.h:417
Definition: CUButton.h:82
std::shared_ptr< JsonValue > _upform
Definition: CUButton.h:115
bool deactivate()
bool activate(Uint32 key)
Color4 _downcolor
Definition: CUButton.h:121
bool _active
Definition: CUButton.h:130
static const Color4 GRAY
Definition: CUColor4.h:1124
virtual void dispose() override
Poly2 _bounds
Definition: CUButton.h:127
Definition: CUSceneLoader.h:77
bool isToggle() const
Definition: CUButton.h:443
Uint32 _inputkey
Definition: CUButton.h:134
void setListener(Listener listener)
Definition: CUButton.h:492
Definition: CUColor4.h:1084
const Listener getListener() const
Definition: CUButton.h:480
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUButton.h:312
Definition: CUAction.h:51
std::shared_ptr< Node > _upnode
Definition: CUButton.h:111
void setToggle(bool value)
Definition: CUButton.h:456