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