My Project
GLLabel.h
1 #pragma once
2 #include "GLType.h"
3 #include "ParaVector2.h"
4 #include <unordered_map>
5 #include <vector>
6 
7 
8 
9 namespace ParaEngine
10 {
11 
12 
13  enum class TextVAlignment
14  {
15  TOP,
16  CENTER,
17  BOTTOM
18  };
19 
20 
21  enum class TextHAlignment
22  {
23  LEFT,
24  CENTER,
25  RIGHT
26  };
27 
28 
29  class Texture2D;
30  class FontAtlas;
31 
32 
33 
34  class Label : public CRefCountedOne
35  {
36  public:
37 
38  static const int DistanceFieldFontSize;
39 
40  struct LetterInfo
41  {
43 
44  Vector2 position;
45  Size contentSize;
46  int atlasIndex;
47  };
48  enum class LabelType {
49 
50  TTF,
51  BMFONT,
52  CHARMAP,
53  STRING_TEXTURE
54  };
55 
56  Label(FontAtlas *atlas = nullptr, TextHAlignment hAlignment = TextHAlignment::LEFT,
57  TextVAlignment vAlignment = TextVAlignment::TOP, bool useDistanceField = false, bool useA8Shader = false);
58 
59  virtual ~Label();
60 
61  bool setTTFConfig(const TTFConfig& ttfConfig);
62 
63  FontAtlas* getFontAtlas() { return _fontAtlas; }
64 
65 
66 
67  virtual void setPosition(float x, float y);
68 
69  void setWidth(unsigned int width) { setDimensions(width, _labelHeight); }
70  unsigned int getWidth() const { return _labelWidth; }
71 
72  void setHeight(unsigned int height) { setDimensions(_labelWidth, height); }
73  unsigned int getHeight() const { return _labelHeight; }
74 
75  void setDimensions(unsigned int width, unsigned int height);
76  const Size& getDimensions() const { return _labelDimensions; }
77 
78 
79  int getStringLength() const;
80 
81 
82  virtual void setScale(float scale);
83  virtual void setScaleX(float scaleX);
84  virtual void setScaleY(float scaleY);
85  virtual float getScaleX() const;
86  virtual float getScaleY() const;
87 
88  void setContentSize(const Size & var);
89 
90  void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment, _vAlignment); }
91  TextHAlignment getTextAlignment() const { return _hAlignment; }
92 
93  void setAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment);
94 
95  void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment, _vAlignment); }
96  TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
97 
98  void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment, vAlignment); }
99  TextVAlignment getVerticalAlignment() const { return _vAlignment; }
100 
101 
102  bool computeHorizontalKernings(const std::u16string& stringToRender);
103 
104  void computeStringNumLines();
105 
106 
107  inline float getPositionX() const
108  {
109  return _position.x;
110  }
111 
112  inline float getPositionY() const
113  {
114  return _position.y;
115  }
116 
117  void setFontScale(float s);
118 
119  bool recordPlaceholderInfo(int spriteIndex);
120  bool recordLetterInfo(const Vector2& point, const FontLetterDefinition& letterDef, int spriteIndex);
121 
122 
123  protected:
124  std::string _bmFontPath;
125 
126  bool _isOpacityModifyRGB;
127  bool _contentDirty;
128 
129  bool _systemFontDirty;
130  std::string _systemFont;
131  float _systemFontSize;
132  LabelType _currentLabelType;
133 
134  FontAtlas * _fontAtlas;
135  std::vector<LetterInfo> _lettersInfo;
136 
137  TTFConfig _fontConfig;
138 
139  //compatibility with older LabelTTF
140  bool _compatibleMode;
141 
144  int _limitShowCount;
145 
146  float _additionalKerning;
147  float _commonLineHeight;
148  bool _lineBreakWithoutSpaces;
149  int * _horizontalKernings;
150 
151  unsigned int _maxLineWidth;
152  Size _labelDimensions;
153  unsigned int _labelWidth;
154  unsigned int _labelHeight;
155  TextHAlignment _hAlignment;
156  TextVAlignment _vAlignment;
157 
158  int _currNumLines;
159  std::u16string _currentUTF16String;
160  std::string _originalUTF8String;
161 
162  float _fontScale;
163 
164  bool _useDistanceField;
165  bool _useA8Shader;
166 
167  Color4B _effectColor;
168  Color4F _effectColorF;
169 
170  GLuint _uniformEffectColor;
171  GLuint _uniformTextColor;
172 
173  bool _shadowDirty;
174  bool _shadowEnabled;
175  Size _shadowOffset;
176  int _shadowBlurRadius;
177  Color3B _shadowColor;
178  float _shadowOpacity;
179 
180  int _outlineSize;
181 
182  Color4B _textColor;
183  Color4F _textColorF;
184 
185  bool _clipEnabled;
186  bool _blendFuncDirty;
187  bool _insideBounds;
188 
189 
190 
191  Size _contentSize;
192  Vector2 _position;
193 
194  float _scaleY;
195  float _scaleX;
196 
197  void reset();
198  void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false);
199 
200 
201  friend class LabelTextFormatter;
202  };
203 
204 
205 
207  {
208  public:
209  static bool createStringSprites(Label* label);
210  static bool multilineText(Label* label);
211  static bool alignText(Label* label);
212  };
213 }
RGB color composed of bytes 3 bytes.
Definition: GLType.h:91
Definition: GLType.h:217
Definition: GLFontAtlas.h:14
CCRect _reusedRect
used for optimization
Definition: GLLabel.h:143
Base class for a reference counted asset.
Definition: PERef.h:55
Definition: GLLabel.h:40
different physics engine has different winding order.
Definition: EventBinding.h:32
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
Definition: GLLabel.h:206
Definition: GLLabel.h:34
RGBA color composed of 4 floats.
Definition: GLType.h:162
RGBA color composed of 4 bytes.
Definition: GLType.h:128
Definition: GLType.h:237
Definition: GLType.h:68
Definition: GLType.h:31