CUGL 1.3
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;
109  bool _toggle;
110 
112  std::shared_ptr<Node> _upnode;
114  std::shared_ptr<Node> _downnode;
116  std::shared_ptr<JsonValue> _upform;
118  std::shared_ptr<JsonValue> _downform;
124  std::string _upchild;
126  std::string _downchild;
127 
130 
132  bool _active;
134  bool _mouse;
136  Uint32 _inputkey;
139 
140 public:
141 #pragma mark Constructors
142 
150  Button();
151 
155  ~Button() { dispose(); }
156 
167  virtual void dispose() override;
168 
177  virtual bool init() override {
178  CUAssertLog(false,"This node does not support the empty initializer");
179  return false;
180  }
181 
192  bool init(const std::shared_ptr<Node>& up) {
193  return init(up,up->getColor()*Color4::GRAY);
194  }
195 
207  bool init(const std::shared_ptr<Node>& up, Color4 down);
208 
221  bool init(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down);
222 
243  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
244 
245 #pragma mark Static Constructors
246 
256  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up) {
257  std::shared_ptr<Button> node = std::make_shared<Button>();
258  return (node->init(up) ? node : nullptr);
259  }
260 
272  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, Color4 down) {
273  std::shared_ptr<Button> node = std::make_shared<Button>();
274  return (node->init(up,down) ? node : nullptr);
275  }
276 
289  static std::shared_ptr<Button> alloc(const std::shared_ptr<Node>& up, const std::shared_ptr<Node>& down) {
290  std::shared_ptr<Button> node = std::make_shared<Button>();
291  return (node->init(up,down) ? node : nullptr);
292  }
293 
314  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
315  const std::shared_ptr<JsonValue>& data) {
316  std::shared_ptr<Button> result = std::make_shared<Button>();
317  if (!result->initWithData(loader,data)) { result = nullptr; }
318  return std::dynamic_pointer_cast<Node>(result);
319  }
320 
321 #pragma mark Button Attributes
322 
333  virtual void setColor(Color4 color) override;
334 
344  const Poly2& getPushable() const { return _bounds; }
345 
355  void setPushable(const Poly2& bounds);
356 
367  void setPushable(const std::vector<Vec2>& vertices);
368 
376  virtual void doLayout() override;
377 
378 #pragma mark Button State
379 
390  bool containsScreen(const Vec2& point);
391 
404  bool containsScreen(float x, float y) {
405  return containsScreen(Vec2(x,y));
406  }
407 
419  bool isDown() const { return _down; }
420 
432  void setDown(bool down);
433 
445  bool isToggle() const { return _toggle; }
446 
458  void setToggle(bool value) { _toggle = value; }
459 
460 #pragma mark Listeners
461 
470  bool hasListener() const { return _listener != nullptr; }
471 
482  const Listener getListener() const { return _listener; }
483 
494  void setListener(Listener listener) { _listener = listener; }
495 
506  bool removeListener();
507 
526  bool activate(Uint32 key);
527 
543  bool deactivate();
544 
550  bool isActive() const { return _active; }
551 };
552 
553 }
554 
555 #endif /* __CU_BUTTON_H__ */
cugl::Button::_upnode
std::shared_ptr< Node > _upnode
Definition: CUButton.h:112
cugl::Button::_upcolor
Color4 _upcolor
Definition: CUButton.h:120
cugl::Button::Listener
std::function< void(const std::string &name, bool down)> Listener
Definition: CUButton.h:103
cugl::Button::dispose
virtual void dispose() override
cugl::Button::containsScreen
bool containsScreen(float x, float y)
Definition: CUButton.h:404
cugl::Button
Definition: CUButton.h:82
cugl::Button::_active
bool _active
Definition: CUButton.h:132
cugl::Button::isToggle
bool isToggle() const
Definition: CUButton.h:445
cugl::Button::setToggle
void setToggle(bool value)
Definition: CUButton.h:458
cugl::Button::_upform
std::shared_ptr< JsonValue > _upform
Definition: CUButton.h:116
cugl::Color4
Definition: CUColor4.h:1084
cugl::Button::doLayout
virtual void doLayout() override
cugl::Button::setPushable
void setPushable(const Poly2 &bounds)
cugl::Button::removeListener
bool removeListener()
cugl::Button::alloc
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, Color4 down)
Definition: CUButton.h:272
cugl::Color4::GRAY
static const Color4 GRAY
Definition: CUColor4.h:1124
cugl::Button::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUButton.h:314
cugl::Button::containsScreen
bool containsScreen(const Vec2 &point)
cugl::Button::_downchild
std::string _downchild
Definition: CUButton.h:126
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::Button::setDown
void setDown(bool down)
cugl::Button::_mouse
bool _mouse
Definition: CUButton.h:134
cugl::Button::hasListener
bool hasListener() const
Definition: CUButton.h:470
cugl::Button::_downform
std::shared_ptr< JsonValue > _downform
Definition: CUButton.h:118
cugl::Button::_listener
Listener _listener
Definition: CUButton.h:138
cugl::Node
Definition: CUNode.h:92
cugl::Button::getListener
const Listener getListener() const
Definition: CUButton.h:482
cugl::Button::getPushable
const Poly2 & getPushable() const
Definition: CUButton.h:344
cugl::Button::setColor
virtual void setColor(Color4 color) override
cugl::Poly2
Definition: CUPoly2.h:109
cugl::Button::_downcolor
Color4 _downcolor
Definition: CUButton.h:122
cugl::Vec2
Definition: CUVec2.h:61
cugl::Button::init
bool init(const std::shared_ptr< Node > &up)
Definition: CUButton.h:192
cugl::Button::isDown
bool isDown() const
Definition: CUButton.h:419
cugl::Button::_inputkey
Uint32 _inputkey
Definition: CUButton.h:136
cugl::Button::Button
Button()
cugl::Button::~Button
~Button()
Definition: CUButton.h:155
cugl::Button::init
virtual bool init() override
Definition: CUButton.h:177
cugl::Button::_downnode
std::shared_ptr< Node > _downnode
Definition: CUButton.h:114
cugl::Button::_down
bool _down
Definition: CUButton.h:107
cugl::Button::activate
bool activate(Uint32 key)
cugl::Button::_bounds
Poly2 _bounds
Definition: CUButton.h:129
cugl::Button::initWithData
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::Button::isActive
bool isActive() const
Definition: CUButton.h:550
cugl::Button::_upchild
std::string _upchild
Definition: CUButton.h:124
cugl::Button::_toggle
bool _toggle
Definition: CUButton.h:109
cugl::Button::setListener
void setListener(Listener listener)
Definition: CUButton.h:494
cugl::Button::deactivate
bool deactivate()
cugl::Button::alloc
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up)
Definition: CUButton.h:256
cugl::Button::alloc
static std::shared_ptr< Button > alloc(const std::shared_ptr< Node > &up, const std::shared_ptr< Node > &down)
Definition: CUButton.h:289