CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 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: 7/6/16
40 //
41 #ifndef __CU_LABEL_H__
42 #define __CU_LABEL_H__
43 
44 #include <string>
45 #include <cugl/2d/CUNode.h>
46 #include <cugl/2d/CUFont.h>
47 
48 namespace cugl {
49 
74 class Label : public Node {
75 #pragma mark Values
76 public:
86  enum class HAlign : int {
93  LEFT = 0,
100  CENTER = 1,
107  RIGHT = 2,
114  HARDLEFT = 3,
122  TRUECENTER = 4,
129  HARDRIGHT = 5
130  };
131 
138  enum class VAlign {
146  BOTTOM = 0,
155  MIDDLE = 1,
163  TOP = 2,
171  HARDBOTTOM= 3,
179  TRUEMIDDLE= 4,
187  HARDTOP = 5,
188  };
189 protected:
191  std::shared_ptr<Font> _font;
192 
194  std::string _text;
199 
206 
211 
215  GLenum _srcFactor;
217  GLenum _dstFactor;
218 
220  bool _rendered;
222  std::vector<Vertex2> _vertices;
224  std::vector<unsigned short> _indices;
226  std::shared_ptr<Texture> _texture;
227 
228 public:
229 #pragma mark -
230 #pragma mark Constructors
231 
239  Label();
240 
244  ~Label() { dispose(); }
245 
255  virtual void dispose() override;
256 
264  virtual bool init() override {
265  CUAssertLog(false,"This node does not support the empty initializer");
266  return false;
267  }
268 
279  bool init(const Size& size, const std::shared_ptr<Font>& font);
280 
298  bool initWithText(const char* text, const std::shared_ptr<Font>& font) {
299  return initWithText(std::string(text), font);
300  }
301 
319  bool initWithText(const std::string& text, const std::shared_ptr<Font>& font);
320 
346  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
347 
348 #pragma mark -
349 #pragma mark Static Constructors
350 
360  static std::shared_ptr<Label> alloc(const Size& size, const std::shared_ptr<Font>& font) {
361  std::shared_ptr<Label> result = std::make_shared<Label>();
362  return (result->init(size,font) ? result : nullptr);
363  }
364 
382  static std::shared_ptr<Label> alloc(const std::string& text, const std::shared_ptr<Font>& font) {
383  std::shared_ptr<Label> result = std::make_shared<Label>();
384  return (result->initWithText(text,font) ? result : nullptr);
385  }
386 
407  static std::shared_ptr<Label> alloc(const char* text, const std::shared_ptr<Font>& font) {
408  std::shared_ptr<Label> result = std::make_shared<Label>();
409  return (result->initWithText(text,font) ? result : nullptr);
410  }
411 
437  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
438  const std::shared_ptr<JsonValue>& data) {
439  std::shared_ptr<Label> result = std::make_shared<Label>();
440  if (!result->initWithData(loader,data)) { result = nullptr; }
441  return std::dynamic_pointer_cast<Node>(result);
442  }
443 
444 #pragma mark -
445 #pragma mark Text Attributes
446 
457  const std::string& getText() const { return _text; }
458 
478  virtual void setText(const std::string& text, bool resize=false);
479 
496  const Vec2& getPadding() const { return _padding; }
497 
511  float getPaddingX() const { return _padding.x; }
512 
526  float getPaddingY() const { return _padding.y; }
527 
544  void setPadding(const Vec2& padding) {
545  setPadding(padding.x,padding.y);
546  }
547 
565  void setPadding(float padx, float pady);
566 
580  void setPaddingX(float padx) {
581  setPadding(padx,_padding.y);
582  }
583 
597  void setPaddingY(float pady) {
598  setPadding(_padding.x,pady);
599  }
600 
610 
619  void setHorizontalAlignment(HAlign halign);
620 
629  VAlign getVerticalAlignment() const { return _valign; }
630 
639  void setVerticalAlignment(VAlign valign);
640 
658  const Rect& getTextBounds() const { return _textbounds; }
659 
677  const Rect getTrueBounds() const {
679  }
680 
689  float getBaseLine() const;
690 
707  virtual void setContentSize(const Size& size) override;
708 
723  virtual void setContentSize(float width, float height) override {
724  setContentSize(Size(width, height));
725  }
726 
727 #pragma mark -
728 #pragma mark Other Attributes
729 
737  Color4 getForeground() const { return _foreground; }
738 
747  void setForeground(Color4 color) { _foreground = color; updateColor(); }
748 
758  Color4 getBackground() const { return _background; }
759 
769  void setBackground(Color4 color);
770 
776  std::shared_ptr<Font> getFont() const { return _font; }
777 
787  void setFont(const std::shared_ptr<Font>& font, bool resize=false);
788 
789 #pragma mark -
790 #pragma mark Rendering
791 
809  void setBlendFunc(GLenum srcFactor, GLenum dstFactor) { _srcFactor = srcFactor; _dstFactor = dstFactor; }
810 
823  GLenum getSourceBlendFactor() const { return _srcFactor; }
824 
837  GLenum getDestinationBlendFactor() const { return _srcFactor; }
838 
854  void setBlendEquation(GLenum equation) { _blendEquation = equation; }
855 
868  GLenum getBlendEquation() const { return _blendEquation; }
869 
893  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
894 
895 private:
896 #pragma mark -
897 #pragma mark Internal Helpers
898 
908  void computeSize();
909 
913  void generateRenderData();
914 
918  void clearRenderData();
919 
926  void updateColor();
927 };
928 
929 }
930 #endif /* __CU_LABEL_H__ */
cugl::Label::_textbounds
Rect _textbounds
Definition: CULabel.h:196
cugl::Label::_vertices
std::vector< Vertex2 > _vertices
Definition: CULabel.h:222
cugl::Label::_font
std::shared_ptr< Font > _font
Definition: CULabel.h:191
cugl::Label::getTrueBounds
const Rect getTrueBounds() const
Definition: CULabel.h:677
cugl::Label::VAlign::BOTTOM
cugl::Label::setBackground
void setBackground(Color4 color)
cugl::Label::VAlign::MIDDLE
cugl::Label::_text
std::string _text
Definition: CULabel.h:194
cugl::Label::alloc
static std::shared_ptr< Label > alloc(const Size &size, const std::shared_ptr< Font > &font)
Definition: CULabel.h:360
cugl::Label::getFont
std::shared_ptr< Font > getFont() const
Definition: CULabel.h:776
cugl::Label::setBlendEquation
void setBlendEquation(GLenum equation)
Definition: CULabel.h:854
cugl::Label::getPadding
const Vec2 & getPadding() const
Definition: CULabel.h:496
cugl::Label::VAlign
VAlign
Definition: CULabel.h:138
cugl::Label::getDestinationBlendFactor
GLenum getDestinationBlendFactor() const
Definition: CULabel.h:837
cugl::Label::_rendered
bool _rendered
Definition: CULabel.h:220
cugl::Label::getSourceBlendFactor
GLenum getSourceBlendFactor() const
Definition: CULabel.h:823
cugl::Label::setPadding
void setPadding(const Vec2 &padding)
Definition: CULabel.h:544
cugl::Label::dispose
virtual void dispose() override
cugl::Label::_texture
std::shared_ptr< Texture > _texture
Definition: CULabel.h:226
cugl::Label::getPaddingY
float getPaddingY() const
Definition: CULabel.h:526
cugl::Label::VAlign::HARDTOP
cugl::Label::getVerticalAlignment
VAlign getVerticalAlignment() const
Definition: CULabel.h:629
cugl::Label::getForeground
Color4 getForeground() const
Definition: CULabel.h:737
cugl::Label::HAlign::TRUECENTER
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::Label::HAlign::CENTER
cugl::Label::setVerticalAlignment
void setVerticalAlignment(VAlign valign)
cugl::Label::setPaddingY
void setPaddingY(float pady)
Definition: CULabel.h:597
cugl::Label
Definition: CULabel.h:74
cugl::Label::HAlign::RIGHT
cugl::Label::alloc
static std::shared_ptr< Label > alloc(const char *text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:407
cugl::Label::_srcFactor
GLenum _srcFactor
Definition: CULabel.h:215
cugl::Vec2::x
float x
Definition: CUVec2.h:66
cugl::Label::_foreground
Color4 _foreground
Definition: CULabel.h:208
cugl::Label::_background
Color4 _background
Definition: CULabel.h:210
cugl::Label::_truebounds
Rect _truebounds
Definition: CULabel.h:198
cugl::Label::getTextBounds
const Rect & getTextBounds() const
Definition: CULabel.h:658
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::Rect::origin
Vec2 origin
Definition: CURect.h:49
cugl::Label::_blendEquation
GLenum _blendEquation
Definition: CULabel.h:213
cugl::Label::alloc
static std::shared_ptr< Label > alloc(const std::string &text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:382
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::Label::HAlign::LEFT
cugl::Label::setContentSize
virtual void setContentSize(float width, float height) override
Definition: CULabel.h:723
cugl::Vec2::y
float y
Definition: CUVec2.h:68
cugl::Label::setText
virtual void setText(const std::string &text, bool resize=false)
cugl::Label::getHorizontalAlignment
HAlign getHorizontalAlignment() const
Definition: CULabel.h:609
cugl::Label::_padding
Vec2 _padding
Definition: CULabel.h:201
cugl::Label::getBlendEquation
GLenum getBlendEquation() const
Definition: CULabel.h:868
cugl::Node
Definition: CUNode.h:92
cugl::Label::getBaseLine
float getBaseLine() const
cugl::Label::_valign
VAlign _valign
Definition: CULabel.h:205
cugl::Label::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CULabel.h:437
cugl::Label::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::Label::Label
Label()
cugl::Vec2
Definition: CUVec2.h:61
cugl::Label::initWithText
bool initWithText(const char *text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:298
cugl::Label::_halign
HAlign _halign
Definition: CULabel.h:203
cugl::Label::_dstFactor
GLenum _dstFactor
Definition: CULabel.h:217
cugl::Label::HAlign::HARDLEFT
cugl::Label::init
virtual bool init() override
Definition: CULabel.h:264
cugl::Label::setContentSize
virtual void setContentSize(const Size &size) override
cugl::Label::VAlign::TRUEMIDDLE
cugl::Label::HAlign
HAlign
Definition: CULabel.h:86
cugl::Label::setHorizontalAlignment
void setHorizontalAlignment(HAlign halign)
cugl::Label::VAlign::TOP
cugl::Label::setPaddingX
void setPaddingX(float padx)
Definition: CULabel.h:580
cugl::Label::getText
const std::string & getText() const
Definition: CULabel.h:457
cugl::Rect::size
Size size
Definition: CURect.h:51
cugl::Label::HAlign::HARDRIGHT
cugl::Label::getBackground
Color4 getBackground() const
Definition: CULabel.h:758
cugl::Label::setBlendFunc
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CULabel.h:809
cugl::Label::~Label
~Label()
Definition: CULabel.h:244
cugl::Label::VAlign::HARDBOTTOM
cugl::Label::setForeground
void setForeground(Color4 color)
Definition: CULabel.h:747
cugl::Label::setFont
void setFont(const std::shared_ptr< Font > &font, bool resize=false)
cugl::Label::_indices
std::vector< unsigned short > _indices
Definition: CULabel.h:224
cugl::Label::getPaddingX
float getPaddingX() const
Definition: CULabel.h:511
cugl::Label::initWithData
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override