CUGL 1.1
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/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 ButtonListener
59 
78 typedef std::function<void(const std::string& name, bool down)> ButtonListener;
79 
80 #pragma mark -
81 #pragma mark Button
82 
100 class Button : public Node {
101 protected:
103  bool _down;
104 
106  std::shared_ptr<Node> _upnode;
108  std::shared_ptr<Node> _downnode;
110  std::shared_ptr<JsonValue> _upform;
112  std::shared_ptr<JsonValue> _downform;
117 
120 
122  bool _active;
124  bool _mouse;
126  Uint32 _inputkey;
128  ButtonListener _listener;
129 
130 
131 public:
132 #pragma mark Constructors
133 
141  Button();
142 
146  ~Button() { dispose(); }
147 
158  virtual void dispose() override;
159 
168  virtual bool init() override {
169  CUAssertLog(false,"This node does not support the empty initializer");
170  return false;
171  }
172 
183  bool init(const std::shared_ptr<Node>& up) {
184  return init(up,up->getColor()*Color4::GRAY);
185  }
186 
198  bool init(const std::shared_ptr<Node>& up, Color4 down);
199 
212  bool init(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down);
213 
239  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
240 
241 #pragma mark Static Constructors
242 
252  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up) {
253  std::shared_ptr<Button> node = std::make_shared<Button>();
254  return (node->init(up) ? node : nullptr);
255  }
256 
268  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, Color4 down) {
269  std::shared_ptr<Button> node = std::make_shared<Button>();
270  return (node->init(up,down) ? node : nullptr);
271  }
272 
285  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down) {
286  std::shared_ptr<Button> node = std::make_shared<Button>();
287  return (node->init(up,down) ? node : nullptr);
288  }
289 
315  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
316  const std::shared_ptr<JsonValue> data) {
317  std::shared_ptr<Button> result = std::make_shared<Button>();
318  if (!result->initWithData(loader,data)) { result = nullptr; }
319  return std::dynamic_pointer_cast<Node>(result);
320  }
321 
322 #pragma mark Button Attributes
323 
334  virtual void setColor(Color4 color) override;
335 
345  const Poly2& getPushable() const { return _bounds; }
346 
356  void setPushable(const Poly2& bounds);
357 
368  void setPushable(const std::vector<Vec2>& vertices);
369 
377  virtual void doLayout() override;
378 
379 #pragma mark Button State
380 
391  bool containsScreen(const Vec2& point);
392 
405  bool containsScreen(float x, float y) {
406  return containsScreen(Vec2(x,y));
407  }
408 
420  bool isDown() const { return _down; }
421 
433  void setDown(bool down);
434 
435 
436 #pragma mark Listeners
437 
446  bool hasListener() const { return _listener != nullptr; }
447 
458  const ButtonListener getListener() const { return _listener; }
459 
470  void setListener(ButtonListener listener) { _listener = listener; }
471 
482  bool removeListener();
483 
502  bool activate(Uint32 key);
503 
519  bool deactivate();
520 
526  bool isActive() const { return _active; }
527 };
528 
529 }
530 
531 #endif /* __CU_BUTTON_H__ */
bool removeListener()
virtual void doLayout() override
std::shared_ptr< JsonValue > _downform
Definition: CUButton.h:112
std::shared_ptr< Node > _downnode
Definition: CUButton.h:108
virtual void setColor(Color4 color) override
bool isActive() const
Definition: CUButton.h:526
Definition: CUPoly2.h:115
Color4 _upcolor
Definition: CUButton.h:114
Definition: CUVec2.h:61
bool hasListener() const
Definition: CUButton.h:446
~Button()
Definition: CUButton.h:146
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, Color4 down)
Definition: CUButton.h:268
bool init(const std::shared_ptr< Node > &up)
Definition: CUButton.h:183
void setListener(ButtonListener listener)
Definition: CUButton.h:470
bool containsScreen(float x, float y)
Definition: CUButton.h:405
bool _mouse
Definition: CUButton.h:124
virtual bool init() override
Definition: CUButton.h:168
bool _down
Definition: CUButton.h:103
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:285
const Poly2 & getPushable() const
Definition: CUButton.h:345
ButtonListener _listener
Definition: CUButton.h:128
void setPushable(const Poly2 &bounds)
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUButton.h:315
void setDown(bool down)
bool containsScreen(const Vec2 &point)
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up)
Definition: CUButton.h:252
bool isDown() const
Definition: CUButton.h:420
Definition: CUButton.h:100
std::shared_ptr< JsonValue > _upform
Definition: CUButton.h:110
bool deactivate()
bool activate(Uint32 key)
const ButtonListener getListener() const
Definition: CUButton.h:458
Color4 _downcolor
Definition: CUButton.h:116
std::function< void(const std::string &name, bool down)> ButtonListener
Definition: CUButton.h:78
bool _active
Definition: CUButton.h:122
static const Color4 GRAY
Definition: CUColor4.h:1124
virtual void dispose() override
Poly2 _bounds
Definition: CUButton.h:119
Definition: CUSceneLoader.h:77
Uint32 _inputkey
Definition: CUButton.h:126
Definition: CUColor4.h:1084
Definition: CUAction.h:51
std::shared_ptr< Node > _upnode
Definition: CUButton.h:106
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override