Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Text.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_TEXT_H
25 #define GF_TEXT_H
26 
27 #include <string>
28 
29 #include "Alignment.h"
30 #include "Portability.h"
31 #include "Transformable.h"
32 #include "Vector.h"
33 #include "VertexArray.h"
34 #include "VertexBuffer.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
41  class Font;
42 
91  class GF_API Text : public Transformable {
92  public:
98  Text();
99 
107  Text(std::string string, Font& font, unsigned characterSize = 30);
108 
119  void setString(std::string string);
120 
130  const std::string& getString() const {
131  return m_string;
132  }
133 
143  void setCharacterSize(unsigned characterSize);
144 
152  unsigned getCharacterSize() const {
153  return m_characterSize;
154  }
155 
170  void setFont(Font& font);
171 
183  const Font *getFont() const {
184  return m_font;
185  }
186 
198  void setColor(const Color4f& color);
199 
207  const Color4f& getColor() const {
208  return m_color;
209  }
210 
220  void setOutlineColor(const Color4f& color);
221 
229  const Color4f& getOutlineColor() const {
230  return m_outlineColor;
231  }
232 
242  void setOutlineThickness(float thickness);
243 
252  return m_outlineThickness;
253  }
254 
263  void setParagraphWidth(float paragraphWidth);
264 
271  float getParagraphWidth() const {
272  return m_paragraphWidth;
273  }
274 
283  void setAlignment(Alignment align);
284 
292  return m_align;
293  }
294 
307  return m_bounds;
308  }
309 
320  void setAnchor(Anchor anchor);
321 
330  VertexBuffer commitGeometry() const;
331 
340  VertexBuffer commitOutlineGeometry() const;
341 
342  virtual void draw(RenderTarget& target, RenderStates states) override;
343 
344  private:
345  void updateGeometry();
346 
347  private:
348  struct Line {
349  std::vector<std::u32string> words;
350  float indent = 0.0f;
351  float spacing = 0.0f;
352  };
353 
354  struct Paragraph {
355  std::vector<Line> lines;
356  };
357 
358  float getWordWidth(const std::u32string& word);
359 
360  std::vector<Paragraph> makeParagraphs(const std::string& str, float spaceWidth);
361 
362  private:
363  std::string m_string;
364  Font *m_font;
365  unsigned m_characterSize;
366  Color4f m_color;
367  VertexArray m_vertices;
368  RectF m_bounds;
369 
370  Color4f m_outlineColor;
371  float m_outlineThickness;
372  VertexArray m_outlineVertices;
373 
374  float m_paragraphWidth;
375  Alignment m_align;
376  };
377 
378 #ifndef DOXYGEN_SHOULD_SKIP_THIS
379 }
380 #endif
381 }
382 
383 #endif // GF_TEXT_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
float getOutlineThickness()
Get the outline thickness of the text.
Definition: Text.h:251
A set of primitives.
Definition: VertexArray.h:65
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
unsigned getCharacterSize() const
Get the character size.
Definition: Text.h:152
float getParagraphWidth() const
Get the paragraph width.
Definition: Text.h:271
Data in the graphics memory.
Definition: VertexBuffer.h:70
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Text.h:306
Graphical text that can be drawn to a render target.
Definition: Text.h:91
The namespace for gf classes.
Definition: Action.h:34
Alignment getAlignment() const
Get the alignment of the text.
Definition: Text.h:291
const Color4f & getOutlineColor() const
Get the outline color of the text.
Definition: Text.h:229
A character font.
Definition: Font.h:130
Anchor
An anchor of a box.
Definition: Anchor.h:38
const std::string & getString() const
Get the text&#39;s string.
Definition: Text.h:130
Alignment
The alignement of a text.
Definition: Alignment.h:33
const Color4f & getColor() const
Get the fill color of the text.
Definition: Text.h:207
const Font * getFont() const
Get the text&#39;s font.
Definition: Text.h:183