]> git.tdb.fi Git - libs/gl.git/blob - source/render/text.h
Pass Tag by value, not by reference
[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 "technique.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Creates an object consisting of the visual representation of a string.  Can be
14 used with an ObjectInstance to further customize the appearance.
15 */
16 class Text: public Renderable
17 {
18 public:
19         enum HorizontalAlign
20         {
21                 LEFT,
22                 CENTER,
23                 RIGHT
24         };
25
26         enum VerticalAlign
27         {
28                 DESCENT,
29                 BASELINE,
30                 MIDLINE,
31                 ASCENT
32         };
33
34 private:
35         const Font &font;
36         Mesh mesh;
37         Object object;
38         Technique technique;
39         float horz_align;
40         float vert_offset;
41         float width;
42
43 public:
44         Text(const Font &, const Technique * = 0, const std::string & = std::string());
45
46         const Mesh *get_mesh() const { return &mesh; }
47
48         /** Sets technique to render with, replacing the given texture slot with
49         the font texture.  If no texture slot is specified, heuristics are used to
50         choose a suitable one. */
51         void set_technique(const Technique *, const std::string & = std::string());
52
53         const Technique *get_technique() const { return object.get_technique(); }
54
55         /// Sets the string to be displayed.
56         void set_text(const std::string &, StringCodec::Decoder &);
57
58         template<typename C>
59         void set_text(const std::string &t)
60         {
61                 typename C::Decoder dec;
62                 set_text(t, dec);
63         }
64
65         void set_text(const std::string &t)
66         { set_text<StringCodec::Utf8>(t); }
67
68         /// Clears the object's contents.
69         void clear();
70
71         /// Sets horizontal and vertical alignment with predefined anchors.
72         void set_alignment(HorizontalAlign, VerticalAlign = BASELINE);
73
74         /** Sets horizontal and vertical alignment.  0.0 means left or baseline,
75         1.0 means right or top. */
76         void set_alignment(float, float = 0.0f);
77
78         float get_width() const { return width; }
79
80         virtual void render(Renderer &, Tag = Tag()) const;
81
82         operator const Object &() const { return object; }
83 };
84
85 } // namespace GL
86 } // namespace Msp
87
88 #endif