CUGL
Cornell University Game Library
CULabel.h
1 //
2 // CULabel.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a scene graph node that displays a single line of text.
6 // We have moved multiline text support to a separate class.
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 zlib 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: 7/6/16
40 //
41 #ifndef __CU_LABEL_H__
42 #define __CU_LABEL_H__
43 
44 #include <string>
45 #include "CUNode.h"
46 #include "CUFont.h"
47 
48 namespace cugl {
49 
74 class Label : public Node {
75 #pragma mark Values
76 public:
85  enum class HAlign : int {
92  LEFT = 0,
99  CENTER = 1,
106  RIGHT = 2,
113  HARDLEFT = 3,
121  TRUECENTER = 4,
128  HARDRIGHT = 5
129  };
130 
137  enum class VAlign {
145  BOTTOM = 0,
154  MIDDLE = 1,
162  TOP = 2,
170  HARDBOTTOM= 3,
178  TRUEMIDDLE= 4,
186  HARDTOP = 5,
187  };
188 protected:
190  std::shared_ptr<Font> _font;
191 
193  std::string _text;
198 
205 
210 
212  bool _rendered;
214  std::vector<Vertex2> _vertices;
216  std::vector<unsigned short> _indices;
217  std::shared_ptr<Texture> _texture;
218 
219 public:
220 #pragma mark -
221 #pragma mark Constructors
222 
230  Label();
231 
235  ~Label() { clearRenderData(); _font = nullptr; }
236 
246  virtual void dispose() override;
247 
258  bool init(const Size& size, const std::shared_ptr<Font>& font);
259 
277  bool initWithText(const char* text, const std::shared_ptr<Font>& font) {
278  return initWithText(std::string(text), font);
279  }
280 
298  bool initWithText(const std::string& text, const std::shared_ptr<Font>& font);
299 
300 
301 #pragma mark -
302 #pragma mark Static Constructors
303 
313  static std::shared_ptr<Label> alloc(const Size& size, const std::shared_ptr<Font>& font) {
314  std::shared_ptr<Label> result = std::make_shared<Label>();
315  return (result->init(size,font) ? result : nullptr);
316  }
317 
335  static std::shared_ptr<Label> alloc(const std::string& text, const std::shared_ptr<Font>& font) {
336  std::shared_ptr<Label> result = std::make_shared<Label>();
337  return (result->initWithText(text,font) ? result : nullptr);
338  }
339 
360  static std::shared_ptr<Label> alloc(const char* text, const std::shared_ptr<Font>& font) {
361  std::shared_ptr<Label> result = std::make_shared<Label>();
362  return (result->initWithText(text,font) ? result : nullptr);
363  }
364 
365 
366 #pragma mark -
367 #pragma mark Text Attributes
368 
379  const std::string& getText() const { return _text; }
380 
400  void setText(const std::string& text, bool resize=false);
401 
418  const Vec2& getPadding() const { return _padding; }
419 
433  float getPaddingX() const { return _padding.x; }
434 
448  float getPaddingY() const { return _padding.y; }
449 
466  void setPadding(const Vec2& padding) {
467  setPadding(padding.x,padding.y);
468  }
469 
487  void setPadding(float padx, float pady);
488 
502  void setPaddingX(float padx) {
503  setPadding(padx,_padding.y);
504  }
505 
519  void setPaddingY(float pady) {
520  setPadding(_padding.x,pady);
521  }
522 
532 
541  void setHorizontalAlignment(HAlign halign);
542 
551  VAlign getVerticalAlignment() const { return _valign; }
552 
561  void setVerticalAlignment(VAlign valign);
562 
580  const Rect& getTextBounds() const { return _textbounds; }
581 
599  const Rect getTrueBounds() const {
600  return Rect(_textbounds.origin+_truebounds.origin,_truebounds.size);
601  }
602 
611  float getBaseLine() const;
612 
629  virtual void setContentSize(const Size& size) override;
630 
645  virtual void setContentSize(float width, float height) override {
646  setContentSize(Size(width, height));
647  }
648 
649 #pragma mark -
650 #pragma mark Other Attributes
651 
659  Color4 getForeground() const { return _foreground; }
660 
669  void setForeground(Color4 color) { _foreground = color; updateColor(); }
670 
680  Color4 getBackground() const { return _background; }
681 
691  void setBackground(Color4 color);
692 
698  std::shared_ptr<Font> getFont() const { return _font; }
699 
708  void setFont(const std::shared_ptr<Font>& font, bool resize=false);
709 
710 #pragma mark -
711 #pragma mark Rendering
712 
735  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
736 
737 private:
738 #pragma mark -
739 #pragma mark Internal Helpers
740 
750  void computeSize();
751 
755  void generateRenderData();
756 
760  void clearRenderData();
761 
768  void updateColor();
769 };
770 
771 }
772 #endif /* __CU_LABEL_H__ */
Definition: CUSize.h:57
virtual void setContentSize(const Size &size) override
float x
Definition: CUVec2.h:66
float y
Definition: CUVec2.h:68
void setText(const std::string &text, bool resize=false)
const Rect & getTextBounds() const
Definition: CULabel.h:580
bool _rendered
Definition: CULabel.h:212
Definition: CUVec2.h:61
Rect _textbounds
Definition: CULabel.h:195
bool initWithText(const char *text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:277
void setPaddingX(float padx)
Definition: CULabel.h:502
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
const std::string & getText() const
Definition: CULabel.h:379
VAlign _valign
Definition: CULabel.h:204
Definition: CUNode.h:92
~Label()
Definition: CULabel.h:235
VAlign getVerticalAlignment() const
Definition: CULabel.h:551
static std::shared_ptr< Label > alloc(const std::string &text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:335
Vec2 _padding
Definition: CULabel.h:200
HAlign _halign
Definition: CULabel.h:202
std::vector< Vertex2 > _vertices
Definition: CULabel.h:214
std::shared_ptr< Font > getFont() const
Definition: CULabel.h:698
Color4 getBackground() const
Definition: CULabel.h:680
Color4 getForeground() const
Definition: CULabel.h:659
const Vec2 & getPadding() const
Definition: CULabel.h:418
float getBaseLine() const
Color4 _foreground
Definition: CULabel.h:207
static std::shared_ptr< Label > alloc(const char *text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:360
void setPaddingY(float pady)
Definition: CULabel.h:519
HAlign
Definition: CULabel.h:85
void setBackground(Color4 color)
void setForeground(Color4 color)
Definition: CULabel.h:669
Color4 _background
Definition: CULabel.h:209
Rect _truebounds
Definition: CULabel.h:197
virtual void dispose() override
Definition: CURect.h:45
void setPadding(const Vec2 &padding)
Definition: CULabel.h:466
Vec2 origin
Definition: CURect.h:49
void setFont(const std::shared_ptr< Font > &font, bool resize=false)
std::vector< unsigned short > _indices
Definition: CULabel.h:216
float getPaddingX() const
Definition: CULabel.h:433
std::string _text
Definition: CULabel.h:193
std::shared_ptr< Font > _font
Definition: CULabel.h:190
Size size
Definition: CURect.h:51
Definition: CUColor4.h:1104
Definition: CULabel.h:74
float getPaddingY() const
Definition: CULabel.h:448
Definition: CUAnimationNode.h:52
static std::shared_ptr< Label > alloc(const Size &size, const std::shared_ptr< Font > &font)
Definition: CULabel.h:313
VAlign
Definition: CULabel.h:137
Definition: CUMat4.h:92
void setVerticalAlignment(VAlign valign)
virtual void setContentSize(float width, float height) override
Definition: CULabel.h:645
const Rect getTrueBounds() const
Definition: CULabel.h:599
virtual bool init()
Definition: CUNode.h:237
void setHorizontalAlignment(HAlign halign)
HAlign getHorizontalAlignment() const
Definition: CULabel.h:531