]> git.tdb.fi Git - libs/gl.git/blobdiff - source/text.h
Add a class for easier text rendering
[libs/gl.git] / source / text.h
diff --git a/source/text.h b/source/text.h
new file mode 100644 (file)
index 0000000..ab1bfe9
--- /dev/null
@@ -0,0 +1,87 @@
+#ifndef MSP_GL_TEXT_H_
+#define MSP_GL_TEXT_H_
+
+#include "font.h"
+#include "mesh.h"
+#include "object.h"
+#include "technique.h"
+
+namespace Msp {
+namespace GL {
+
+/**
+Creates an object consisting of the visual representation of a string.  Can be
+used with an ObjectInstance to further customize the appearance.
+*/
+class Text: public Renderable
+{
+public:
+       enum HorizontalAlign
+       {
+               LEFT,
+               CENTER,
+               RIGHT
+       };
+
+       enum VerticalAlign
+       {
+               DESCENT,
+               BASELINE,
+               MIDLINE,
+               ASCENT
+       };
+
+private:
+       const Font &font;
+       Mesh mesh;
+       Object object;
+       Technique technique;
+       float horz_align;
+       float vert_offset;
+       float width;
+
+public:
+       Text(const Font &, const Technique * = 0);
+
+       const Mesh *get_mesh() const { return &mesh; }
+
+       /** Sets technique to render with.  It should have a texture slot named
+       "diffusemap", which will be replaced with the font's texture. */
+       void set_technique(const Technique *);
+
+       const Technique *get_technique() const { return object.get_technique(); }
+
+       /// Sets the string to be displayed.
+       void set_text(const std::string &, StringCodec::Decoder &);
+
+       template<typename C>
+       void set_text(const std::string &t)
+       {
+               typename C::Decoder dec;
+               set_text(t, dec);
+       }
+
+       void set_text(const std::string &t)
+       { set_text<StringCodec::Utf8>(t); }
+
+       /// Clears the object's contents.
+       void clear();
+
+       /// Sets horizontal and vertical alignment with predefined anchors.
+       void set_alignment(HorizontalAlign, VerticalAlign = BASELINE);
+
+       /** Sets horizontal and vertical alignment.  0.0 means left or baseline,
+       1.0 means right or top. */
+       void set_alignment(float, float = 0.0f);
+
+       float get_width() const { return width; }
+
+       virtual void render(Renderer &, const Tag &) const;
+
+       operator const Object &() const { return object; }
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif