]> git.tdb.fi Git - libs/gl.git/blob - source/render/text.h
Check the flat qualifier from the correct member
[libs/gl.git] / source / render / text.h
1 #ifndef MSP_GL_TEXT_H_
2 #define MSP_GL_TEXT_H_
3
4 #include "font.h"
5 #include "mesh.h"
6 #include "object.h"
7 #include "objectinstance.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Creates a Mesh and an Object for rendering text.
14
15 The mesh is created with a font size of 1 unit.  Set the Text's model matrix to
16 scale it to the desired size.
17
18 If you derive from Text to customize it, make sure you call Text::setup_render
19 or otherwise set the appropriate texture to the Renderer.
20 */
21 class Text: public ObjectInstance
22 {
23 public:
24         enum HorizontalAlign
25         {
26                 LEFT,
27                 CENTER,
28                 RIGHT
29         };
30
31         enum VerticalAlign
32         {
33                 DESCENT,
34                 BASELINE,
35                 MIDLINE,
36                 ASCENT
37         };
38
39 private:
40         const Font &font;
41         Mesh mesh;
42         Object object;
43         Tag texture_slot;
44         float horz_align = 0.0f;
45         float vert_offset = 0.0f;
46         float width = 0.0f;
47
48 public:
49         Text(const Font &, const Technique * = 0, Tag = Tag());
50
51         const Mesh *get_mesh() const { return &mesh; }
52
53         /** Sets technique to render with, along with the texture slot to use for
54         the font texture.  If no texture slot is specified, heuristics are used to
55         choose a suitable one. */
56         void set_technique(const Technique *, Tag = Tag());
57
58         const Technique *get_technique() const { return object.get_technique(); }
59
60         /** Sets the string to be displayed, with an explicit character encoding. */
61         void set_text(const std::string &, StringCodec::Decoder &);
62
63         template<typename C>
64         void set_text(const std::string &t)
65         {
66                 typename C::Decoder dec;
67                 set_text(t, dec);
68         }
69
70         /** Sets the string to be displayed, with UTF-8 encoding. */
71         void set_text(const std::string &t)
72         { set_text<StringCodec::Utf8>(t); }
73
74         /** Clears any previous contents. */
75         void clear();
76
77         /** Sets horizontal and vertical alignment with predefined anchors. */
78         void set_alignment(HorizontalAlign, VerticalAlign = BASELINE);
79
80         /** Sets horizontal and vertical alignment.  0.0 means left or baseline,
81         1.0 means right or top. */
82         void set_alignment(float, float = 0.0f);
83
84         float get_width() const { return width; }
85
86         virtual void setup_render(Renderer &, Tag) const;
87
88         DEPRECATED operator const Object &() const { return object; }
89 };
90
91 } // namespace GL
92 } // namespace Msp
93
94 #endif