summaryrefslogtreecommitdiff
path: root/include/SFML/Graphics/String.hpp
blob: a7f471842f9b4ee2cc83ba3cb3b1224e00f43406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2008 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

#ifndef SFML_STRING_HPP
#define SFML_STRING_HPP

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Resource.hpp>
#include <SFML/System/Unicode.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <string>


namespace sf
{
////////////////////////////////////////////////////////////
/// String defines a graphical 2D text, that can be drawn on screen
////////////////////////////////////////////////////////////
class SFML_API String : public Drawable
{
public :

    ////////////////////////////////////////////////////////////
    /// Enumerate the string drawing styles
    ////////////////////////////////////////////////////////////
    enum Style
    {
        Regular    = 0,      ///< Regular characters, no style
        Bold       = 1 << 0, ///< Characters are bold
        Italic     = 1 << 1, ///< Characters are in italic
        Underlined = 1 << 2  ///< Characters are underlined
    };

    ////////////////////////////////////////////////////////////
    /// Default constructor
    ///
    ////////////////////////////////////////////////////////////
    String();

    ////////////////////////////////////////////////////////////
    /// Construct the string from any kind of text
    ///
    /// \param Text : Text assigned to the string
    /// \param Font : Font used to draw the string (SFML built-in font by default)
    /// \param Size : Characters size (30 by default)
    ///
    ////////////////////////////////////////////////////////////
    explicit String(const Unicode::Text& Text, const Font& CharFont = Font::GetDefaultFont(), float Size = 30.f);

    ////////////////////////////////////////////////////////////
    /// Set the text (from any kind of string)
    ///
    /// \param Text : New text
    ///
    ////////////////////////////////////////////////////////////
    void SetText(const Unicode::Text& Text);

    ////////////////////////////////////////////////////////////
    /// Set the font of the string
    ///
    /// \param Font : Font to use
    ///
    ////////////////////////////////////////////////////////////
    void SetFont(const Font& CharFont);

    ////////////////////////////////////////////////////////////
    /// Set the size of the string
    /// The default size is 30
    ///
    /// \param Size : New size, in pixels
    ///
    ////////////////////////////////////////////////////////////
    void SetSize(float Size);

    ////////////////////////////////////////////////////////////
    /// Set the style of the text
    /// The default style is Regular
    ///
    /// \param TextStyle : New text style, (combination of Style enum values)
    ///
    ////////////////////////////////////////////////////////////
    void SetStyle(unsigned long TextStyle);

    ////////////////////////////////////////////////////////////
    /// Get the text (the returned text can be converted implicitely to any kind of string)
    ///
    /// \return String's text
    ///
    ////////////////////////////////////////////////////////////
    const Unicode::Text& GetText() const;

    ////////////////////////////////////////////////////////////
    /// Get the font used by the string
    ///
    /// \return Font used
    ///
    ////////////////////////////////////////////////////////////
    const Font& GetFont() const;

    ////////////////////////////////////////////////////////////
    /// Get the size of the characters
    ///
    /// \return Size of the characters
    ///
    ////////////////////////////////////////////////////////////
    float GetSize() const;

    ////////////////////////////////////////////////////////////
    /// Get the style of the text
    ///
    /// \return Current string style (combination of Style enum values)
    ///
    ////////////////////////////////////////////////////////////
    unsigned long GetStyle() const;

    ////////////////////////////////////////////////////////////
    /// Return the visual position of the Index-th character of the string,
    /// in coordinates relative to the string
    /// (note : translation, center, rotation and scale are not applied)
    ///
    /// \param Index : Index of the character
    ///
    /// \return Position of the Index-th character (end of string if Index is out of range)
    ///
    ////////////////////////////////////////////////////////////
    sf::Vector2f GetCharacterPos(std::size_t Index) const;

    ////////////////////////////////////////////////////////////
    /// Get the string rectangle on screen
    ///
    /// \return Rectangle contaning the string in screen coordinates
    ///
    ////////////////////////////////////////////////////////////
    FloatRect GetRect() const;

protected :

    ////////////////////////////////////////////////////////////
    /// /see Drawable::Render
    ///
    ////////////////////////////////////////////////////////////
    virtual void Render(RenderTarget& Target) const;

private :

    ////////////////////////////////////////////////////////////
    /// Recompute the bounding rectangle of the text
    ///
    ////////////////////////////////////////////////////////////
    void RecomputeRect();

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    Unicode::Text     myText;           ///< Text to display
    ResourcePtr<Font> myFont;           ///< Font used to display the string
    float             mySize;           ///< Size of the characters
    unsigned long     myStyle;          ///< Text style (see Style enum)
    FloatRect         myBaseRect;       ///< Bounding rectangle of the text in object coordinates
    bool              myNeedRectUpdate; ///< Does the bounding rect need an update ?
};

} // namespace sf


#endif // SFML_STRING_HPP