CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUProgressBar.h
1 //
2 // CUProgressBar.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a simple progress bar, which is useful
6 // for displaying things such as asset loading.
7 //
8 // This class uses our standard shared-pointer architecture.
9 //
10 // 1. The constructor does not perform any initialization; it just sets all
11 // attributes to their defaults.
12 //
13 // 2. All initialization takes place via init methods, which can fail if an
14 // object is initialized more than once.
15 //
16 // 3. All allocation takes place via static constructors which return a shared
17 // pointer.
18 //
19 // CUGL MIT License:
20 // This software is provided 'as-is', without any express or implied
21 // warranty. In no event will the authors be held liable for any damages
22 // arising from the use of this software.
23 //
24 // Permission is granted to anyone to use this software for any purpose,
25 // including commercial applications, and to alter it and redistribute it
26 // freely, subject to the following restrictions:
27 //
28 // 1. The origin of this software must not be misrepresented; you must not
29 // claim that you wrote the original software. If you use this software
30 // in a product, an acknowledgment in the product documentation would be
31 // appreciated but is not required.
32 //
33 // 2. Altered source versions must be plainly marked as such, and must not
34 // be misrepresented as being the original software.
35 //
36 // 3. This notice may not be removed or altered from any source distribution.
37 //
38 // Author: Walker White
39 // Version: 1/8/17
40 //
41 #ifndef __CU_PROGRESS_BAR_H__
42 #define __CU_PROGRESS_BAR_H__
43 #include <cugl/2d/CUNode.h>
44 #include <cugl/2d/CUPolygonNode.h>
45 
46 namespace cugl {
47 
60 class ProgressBar: public Node {
61 protected:
63  float _progress;
66 
68  std::shared_ptr<PolygonNode> _background;
70  std::shared_ptr<PolygonNode> _foreground;
72  std::shared_ptr<PolygonNode> _begincap;
74  std::shared_ptr<PolygonNode> _finalcap;
75 
76 public:
77 #pragma mark -
78 #pragma mark Constructors
79 
87  ProgressBar() : _progress(1.0f) {}
88 
93 
104  virtual void dispose() override;
105 
114  virtual bool init() override {
115  CUAssertLog(false,"This node does not support the empty initializer");
116  return false;
117  }
118 
130  bool init(const Size& size) {
131  return initWithCaps(nullptr,nullptr,nullptr,nullptr,size);
132  }
133 
146  bool init(const std::shared_ptr<Texture>& background) {
147  return initWithCaps(background,nullptr,nullptr,nullptr);
148  }
149 
163  bool init(const std::shared_ptr<Texture>& background, const Size& size) {
164  return initWithCaps(background,nullptr,nullptr,nullptr,size);
165  }
166 
179  bool init(const std::shared_ptr<Texture>& background,
180  const std::shared_ptr<Texture>& foreground) {
181  return initWithCaps(background,foreground,nullptr,nullptr);
182  }
183 
197  bool init(const std::shared_ptr<Texture>& background,
198  const std::shared_ptr<Texture>& foreground,
199  const Size& size) {
200  return initWithCaps(background,foreground,nullptr,nullptr,size);
201  }
202 
217  bool initWithCaps(const std::shared_ptr<Texture>& background,
218  const std::shared_ptr<Texture>& foreground,
219  const std::shared_ptr<Texture>& beginCap,
220  const std::shared_ptr<Texture>& finalCap);
221 
237  bool initWithCaps(const std::shared_ptr<Texture>& background,
238  const std::shared_ptr<Texture>& foreground,
239  const std::shared_ptr<Texture>& beginCap,
240  const std::shared_ptr<Texture>& finalCap,
241  const Size& size);
242 
263  bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
264 
265 #pragma mark -
266 #pragma mark Static Constructors
267 
278  static std::shared_ptr<ProgressBar> alloc(const Size& size) {
279  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
280  return (node->init(size) ? node : nullptr);
281  }
282 
295  static std::shared_ptr<ProgressBar> alloc(const std::shared_ptr<Texture>& background) {
296  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
297  return (node->init(background) ? node : nullptr);
298  }
299 
313  static std::shared_ptr<ProgressBar> alloc(const std::shared_ptr<Texture>& background, const Size& size) {
314  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
315  return (node->init(background,size) ? node : nullptr);
316  }
317 
330  static std::shared_ptr<ProgressBar> alloc(const std::shared_ptr<Texture>& background,
331  const std::shared_ptr<Texture>& foreground) {
332  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
333  return (node->init(background,foreground) ? node : nullptr);
334  }
335 
349  static std::shared_ptr<ProgressBar> alloc(const std::shared_ptr<Texture>& background,
350  const std::shared_ptr<Texture>& foreground,
351  const Size& size) {
352  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
353  return (node->init(background,foreground,size) ? node : nullptr);
354  }
355 
370  static std::shared_ptr<ProgressBar> allocWithCaps(const std::shared_ptr<Texture>& background,
371  const std::shared_ptr<Texture>& foreground,
372  const std::shared_ptr<Texture>& beginCap,
373  const std::shared_ptr<Texture>& finalCap) {
374  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
375  return (node->initWithCaps(background,foreground,beginCap,finalCap) ? node : nullptr);
376  }
377 
393  static std::shared_ptr<ProgressBar> allocWithCaps(const std::shared_ptr<Texture>& background,
394  const std::shared_ptr<Texture>& foreground,
395  const std::shared_ptr<Texture>& beginCap,
396  const std::shared_ptr<Texture>& finalCap,
397  const Size& size) {
398  std::shared_ptr<ProgressBar> node = std::make_shared<ProgressBar>();
399  return (node->initWithCaps(background,foreground,beginCap,finalCap,size) ? node : nullptr);
400  }
401 
422  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
423  const std::shared_ptr<JsonValue>& data) {
424  std::shared_ptr<ProgressBar> result = std::make_shared<ProgressBar>();
425  if (!result->initWithData(loader,data)) { result = nullptr; }
426  return std::dynamic_pointer_cast<Node>(result);
427  }
428 
429 #pragma mark -
430 #pragma mark Properties
431 
439  float getProgress() const { return _progress; }
440 
449  void setProgress(float progress);
450 
459  Color4 getBackgroundColor() const { return _background->getColor(); }
460 
469  void setBackgroundColor(Color4 color) { _background->setColor(color); }
470 
480  Color4 getForegroundColor() const { return _foreground->getColor(); }
481 
491  void setForegroundColor(Color4 color);
492 };
493 
494 }
495 
496 #endif /* __CU_PROGRESS_BAR_H__ */
cugl::ProgressBar::_begincap
std::shared_ptr< PolygonNode > _begincap
Definition: CUProgressBar.h:72
cugl::ProgressBar::_background
std::shared_ptr< PolygonNode > _background
Definition: CUProgressBar.h:68
cugl::ProgressBar::init
bool init(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground, const Size &size)
Definition: CUProgressBar.h:197
cugl::ProgressBar::_foresize
Size _foresize
Definition: CUProgressBar.h:65
cugl::ProgressBar::alloc
static std::shared_ptr< ProgressBar > alloc(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground, const Size &size)
Definition: CUProgressBar.h:349
cugl::ProgressBar::init
bool init(const std::shared_ptr< Texture > &background, const Size &size)
Definition: CUProgressBar.h:163
cugl::ProgressBar::init
bool init(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground)
Definition: CUProgressBar.h:179
cugl::ProgressBar::_progress
float _progress
Definition: CUProgressBar.h:63
cugl::ProgressBar::initWithCaps
bool initWithCaps(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground, const std::shared_ptr< Texture > &beginCap, const std::shared_ptr< Texture > &finalCap)
cugl::ProgressBar::getBackgroundColor
Color4 getBackgroundColor() const
Definition: CUProgressBar.h:459
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::ProgressBar::getForegroundColor
Color4 getForegroundColor() const
Definition: CUProgressBar.h:480
cugl::ProgressBar::getProgress
float getProgress() const
Definition: CUProgressBar.h:439
cugl::ProgressBar::alloc
static std::shared_ptr< ProgressBar > alloc(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground)
Definition: CUProgressBar.h:330
cugl::ProgressBar::allocWithCaps
static std::shared_ptr< ProgressBar > allocWithCaps(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground, const std::shared_ptr< Texture > &beginCap, const std::shared_ptr< Texture > &finalCap, const Size &size)
Definition: CUProgressBar.h:393
cugl::ProgressBar::init
bool init(const std::shared_ptr< Texture > &background)
Definition: CUProgressBar.h:146
cugl::ProgressBar::alloc
static std::shared_ptr< ProgressBar > alloc(const std::shared_ptr< Texture > &background, const Size &size)
Definition: CUProgressBar.h:313
cugl::ProgressBar::initWithData
bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::ProgressBar::alloc
static std::shared_ptr< ProgressBar > alloc(const std::shared_ptr< Texture > &background)
Definition: CUProgressBar.h:295
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::ProgressBar::_foreground
std::shared_ptr< PolygonNode > _foreground
Definition: CUProgressBar.h:70
cugl::ProgressBar::_finalcap
std::shared_ptr< PolygonNode > _finalcap
Definition: CUProgressBar.h:74
cugl::Node
Definition: CUNode.h:92
cugl::ProgressBar::allocWithCaps
static std::shared_ptr< ProgressBar > allocWithCaps(const std::shared_ptr< Texture > &background, const std::shared_ptr< Texture > &foreground, const std::shared_ptr< Texture > &beginCap, const std::shared_ptr< Texture > &finalCap)
Definition: CUProgressBar.h:370
cugl::ProgressBar::~ProgressBar
~ProgressBar()
Definition: CUProgressBar.h:92
cugl::ProgressBar::init
bool init(const Size &size)
Definition: CUProgressBar.h:130
cugl::ProgressBar::setBackgroundColor
void setBackgroundColor(Color4 color)
Definition: CUProgressBar.h:469
cugl::ProgressBar::init
virtual bool init() override
Definition: CUProgressBar.h:114
cugl::ProgressBar::setProgress
void setProgress(float progress)
cugl::ProgressBar::setForegroundColor
void setForegroundColor(Color4 color)
cugl::ProgressBar::ProgressBar
ProgressBar()
Definition: CUProgressBar.h:87
cugl::ProgressBar::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUProgressBar.h:422
cugl::ProgressBar::dispose
virtual void dispose() override
cugl::ProgressBar
Definition: CUProgressBar.h:60
cugl::ProgressBar::alloc
static std::shared_ptr< ProgressBar > alloc(const Size &size)
Definition: CUProgressBar.h:278