CUGL 1.1
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 <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:
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 
214  GLenum _srcFactor;
216  GLenum _dstFactor;
217 
219  bool _rendered;
221  std::vector<Vertex2> _vertices;
223  std::vector<unsigned short> _indices;
224  std::shared_ptr<Texture> _texture;
225 
226 public:
227 #pragma mark -
228 #pragma mark Constructors
229 
237  Label();
238 
242  ~Label() { dispose(); }
243 
253  virtual void dispose() override;
254 
262  virtual bool init() override {
263  CUAssertLog(false,"This node does not support the empty initializer");
264  return false;
265  }
266 
277  bool init(const Size& size, const std::shared_ptr<Font>& font);
278 
296  bool initWithText(const char* text, const std::shared_ptr<Font>& font) {
297  return initWithText(std::string(text), font);
298  }
299 
317  bool initWithText(const std::string& text, const std::shared_ptr<Font>& font);
318 
349  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
350 
351 #pragma mark -
352 #pragma mark Static Constructors
353 
363  static std::shared_ptr<Label> alloc(const Size& size, const std::shared_ptr<Font>& font) {
364  std::shared_ptr<Label> result = std::make_shared<Label>();
365  return (result->init(size,font) ? result : nullptr);
366  }
367 
385  static std::shared_ptr<Label> alloc(const std::string& text, const std::shared_ptr<Font>& font) {
386  std::shared_ptr<Label> result = std::make_shared<Label>();
387  return (result->initWithText(text,font) ? result : nullptr);
388  }
389 
410  static std::shared_ptr<Label> alloc(const char* text, const std::shared_ptr<Font>& font) {
411  std::shared_ptr<Label> result = std::make_shared<Label>();
412  return (result->initWithText(text,font) ? result : nullptr);
413  }
414 
445  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
446  const std::shared_ptr<JsonValue> data) {
447  std::shared_ptr<Label> result = std::make_shared<Label>();
448  if (!result->initWithData(loader,data)) { result = nullptr; }
449  return std::dynamic_pointer_cast<Node>(result);
450  }
451 
452 #pragma mark -
453 #pragma mark Text Attributes
454 
465  const std::string& getText() const { return _text; }
466 
486  virtual void setText(const std::string& text, bool resize=false);
487 
504  const Vec2& getPadding() const { return _padding; }
505 
519  float getPaddingX() const { return _padding.x; }
520 
534  float getPaddingY() const { return _padding.y; }
535 
552  void setPadding(const Vec2& padding) {
553  setPadding(padding.x,padding.y);
554  }
555 
573  void setPadding(float padx, float pady);
574 
588  void setPaddingX(float padx) {
589  setPadding(padx,_padding.y);
590  }
591 
605  void setPaddingY(float pady) {
606  setPadding(_padding.x,pady);
607  }
608 
618 
627  void setHorizontalAlignment(HAlign halign);
628 
637  VAlign getVerticalAlignment() const { return _valign; }
638 
647  void setVerticalAlignment(VAlign valign);
648 
666  const Rect& getTextBounds() const { return _textbounds; }
667 
685  const Rect getTrueBounds() const {
686  return Rect(_textbounds.origin+_truebounds.origin,_truebounds.size);
687  }
688 
697  float getBaseLine() const;
698 
715  virtual void setContentSize(const Size& size) override;
716 
731  virtual void setContentSize(float width, float height) override {
732  setContentSize(Size(width, height));
733  }
734 
735 #pragma mark -
736 #pragma mark Other Attributes
737 
745  Color4 getForeground() const { return _foreground; }
746 
755  void setForeground(Color4 color) { _foreground = color; updateColor(); }
756 
766  Color4 getBackground() const { return _background; }
767 
777  void setBackground(Color4 color);
778 
784  std::shared_ptr<Font> getFont() const { return _font; }
785 
794  void setFont(const std::shared_ptr<Font>& font, bool resize=false);
795 
796 #pragma mark -
797 #pragma mark Rendering
798 
816  void setBlendFunc(GLenum srcFactor, GLenum dstFactor) { _srcFactor = srcFactor; _dstFactor = dstFactor; }
817 
830  GLenum getSourceBlendFactor() const { return _srcFactor; }
831 
844  GLenum getDestinationBlendFactor() const { return _srcFactor; }
845 
861  void setBlendEquation(GLenum equation) { _blendEquation = equation; }
862 
875  GLenum getBlendEquation() const { return _blendEquation; }
876 
900  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
901 
902 private:
903 #pragma mark -
904 #pragma mark Internal Helpers
905 
915  void computeSize();
916 
920  void generateRenderData();
921 
925  void clearRenderData();
926 
933  void updateColor();
934 };
935 
936 }
937 #endif /* __CU_LABEL_H__ */
Definition: CUSize.h:57
virtual void setContentSize(const Size &size) override
float x
Definition: CUVec2.h:66
GLenum _srcFactor
Definition: CULabel.h:214
float y
Definition: CUVec2.h:68
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CULabel.h:445
const Rect & getTextBounds() const
Definition: CULabel.h:666
bool _rendered
Definition: CULabel.h:219
Definition: CUVec2.h:61
Rect _textbounds
Definition: CULabel.h:195
bool initWithText(const char *text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:296
void setPaddingX(float padx)
Definition: CULabel.h:588
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
const std::string & getText() const
Definition: CULabel.h:465
VAlign _valign
Definition: CULabel.h:204
Definition: CUNode.h:92
~Label()
Definition: CULabel.h:242
VAlign getVerticalAlignment() const
Definition: CULabel.h:637
GLenum _blendEquation
Definition: CULabel.h:212
GLenum getDestinationBlendFactor() const
Definition: CULabel.h:844
static std::shared_ptr< Label > alloc(const std::string &text, const std::shared_ptr< Font > &font)
Definition: CULabel.h:385
Vec2 _padding
Definition: CULabel.h:200
GLenum getSourceBlendFactor() const
Definition: CULabel.h:830
HAlign _halign
Definition: CULabel.h:202
std::vector< Vertex2 > _vertices
Definition: CULabel.h:221
virtual void setText(const std::string &text, bool resize=false)
std::shared_ptr< Font > getFont() const
Definition: CULabel.h:784
Color4 getBackground() const
Definition: CULabel.h:766
Color4 getForeground() const
Definition: CULabel.h:745
const Vec2 & getPadding() const
Definition: CULabel.h:504
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:410
void setPaddingY(float pady)
Definition: CULabel.h:605
HAlign
Definition: CULabel.h:85
void setBackground(Color4 color)
void setForeground(Color4 color)
Definition: CULabel.h:755
Color4 _background
Definition: CULabel.h:209
virtual bool init() override
Definition: CULabel.h:262
void setBlendEquation(GLenum equation)
Definition: CULabel.h:861
GLenum getBlendEquation() const
Definition: CULabel.h:875
Rect _truebounds
Definition: CULabel.h:197
virtual void dispose() override
GLenum _dstFactor
Definition: CULabel.h:216
Definition: CURect.h:45
void setPadding(const Vec2 &padding)
Definition: CULabel.h:552
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:223
float getPaddingX() const
Definition: CULabel.h:519
Definition: CUSceneLoader.h:77
std::string _text
Definition: CULabel.h:193
std::shared_ptr< Font > _font
Definition: CULabel.h:190
Size size
Definition: CURect.h:51
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CULabel.h:816
Definition: CUColor4.h:1084
Definition: CULabel.h:74
float getPaddingY() const
Definition: CULabel.h:534
Definition: CUAction.h:51
static std::shared_ptr< Label > alloc(const Size &size, const std::shared_ptr< Font > &font)
Definition: CULabel.h:363
VAlign
Definition: CULabel.h:137
Definition: CUMat4.h:92
void setVerticalAlignment(VAlign valign)
virtual void setContentSize(float width, float height) override
Definition: CULabel.h:731
const Rect getTrueBounds() const
Definition: CULabel.h:685
void setHorizontalAlignment(HAlign halign)
HAlign getHorizontalAlignment() const
Definition: CULabel.h:617