Text.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages 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 freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_TEXT_HPP
26 #define CPP3DS_TEXT_HPP
27 
29 // Headers
31 #include <cpp3ds/Graphics/Drawable.hpp>
32 #include <cpp3ds/Graphics/Transformable.hpp>
33 #include <cpp3ds/Graphics/Font.hpp>
34 #include <cpp3ds/Graphics/Rect.hpp>
35 #include <cpp3ds/Graphics/VertexArray.hpp>
36 #include <cpp3ds/System/String.hpp>
37 #include <string>
38 #include <vector>
39 
40 
41 namespace cpp3ds
42 {
47 class Text : public Drawable, public Transformable
48 {
49 public :
50 
55  enum Style
56  {
57  Regular = 0,
58  Bold = 1 << 0,
59  Italic = 1 << 1,
60  Underlined = 1 << 2
61  };
62 
69  Text();
70 
79  Text(const String& string, const Font& font, unsigned int characterSize = 30);
80 
100  void setString(const String& string);
101 
117  void setFont(const Font& font);
118 
129  void setCharacterSize(unsigned int size);
130 
143  void setStyle(Uint32 style);
144 
155  void setColor(const Color& color);
156 
174  const String& getString() const;
175 
188  const Font* getFont() const;
189 
198  unsigned int getCharacterSize() const;
199 
208  Uint32 getStyle() const;
209 
218  const Color& getColor() const;
219 
235  Vector2f findCharacterPos(std::size_t index) const;
236 
249  FloatRect getLocalBounds() const;
250 
263  FloatRect getGlobalBounds() const;
264 
265  // Override position functions from Transformable.
266  // The 3DS text renders poorly without rounded coordinates.
267  void setPosition(float x, float y);
268  void setPosition(const Vector2f& position);
269 
270 private :
271 
279  virtual void draw(RenderTarget& target, RenderStates states) const;
280 
288  void ensureGeometryUpdate() const;
289 
291  // Member data
293  String m_string;
294  const Font* m_font;
295  unsigned int m_characterSize;
296  Uint32 m_style;
297  Color m_color;
298  mutable VertexArray m_vertices;
299  mutable FloatRect m_bounds;
300  mutable bool m_geometryNeedUpdate;
301 };
302 
303 } // namespace cpp3ds
304 
305 
306 #endif
307 
308 
Class for loading and manipulating character fonts.
Definition: Font.hpp:49
Italic characters.
Definition: Text.hpp:59
Decomposed transform defined by a position, a rotation and a scale.
const Font * getFont() const
Get the text's font.
void setColor(const Color &color)
Set the global color of the text.
Utility string class that automatically handles conversions between types and encodings.
Definition: String.hpp:42
FloatRect getLocalBounds() const
Get the local bounding rectangle of the entity.
Style
Enumeration of the string drawing styles.
Definition: Text.hpp:55
void setFont(const Font &font)
Set the text's font.
const Color & getColor() const
Get the global color of the text.
Utility class for manpulating RGBA colors.
Definition: Color.hpp:36
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
Define a set of one or more 2D primitives.
Definition: VertexArray.hpp:47
void setCharacterSize(unsigned int size)
Set the character size.
void setString(const String &string)
Set the text's string.
Bold characters.
Definition: Text.hpp:58
Graphical text that can be drawn to a render target.
Definition: Text.hpp:47
Abstract base class for objects that can be drawn to a render target.
Definition: Drawable.hpp:43
Utility template class for manipulating 2-dimensional vectors.
Definition: Vector2.hpp:37
void setStyle(Uint32 style)
Set the text's style.
Base class for all render targets (window, texture, ...)
Underlined characters.
Definition: Text.hpp:60
Define the states used for drawing to a RenderTarget.
const String & getString() const
Get the text's string.
FloatRect getGlobalBounds() const
Get the global bounding rectangle of the entity.
Uint32 getStyle() const
Get the text's style.
Regular characters, no style.
Definition: Text.hpp:57
unsigned int getCharacterSize() const
Get the character size.
Text()
Default constructor.