1 #include "meshbuilder.h"
13 Text::Text(const Font &f, const Technique *tech, Tag tex_slot):
14 ObjectInstance(object),
16 mesh((TEXCOORD2, VERTEX2))
18 object.set_mesh(&mesh);
20 set_technique(tech, tex_slot);
23 void Text::set_technique(const Technique *tech, Tag tex_slot)
27 if(!tex_slot.id && tech->has_method(Tag()))
28 if(const Program *shprog = tech->get_method(Tag()).get_shader_program())
30 if(shprog->get_uniform_location(Tag("font_tex"))>=0)
31 tex_slot = "font_tex";
32 else if(shprog->get_uniform_location(Tag("color_tex"))>=0)
33 tex_slot = "color_tex";
34 else if(shprog->get_uniform_location(Tag("diffuse_map"))>=0)
35 tex_slot = "diffuse_map";
36 else if(shprog->get_uniform_location(Tag("base_color_map"))>=0)
37 tex_slot = "base_color_map";
40 object.set_technique(tech);
41 texture_slot = tex_slot;
45 object.set_technique(0);
50 void Text::set_text(const string &text, StringCodec::Decoder &dec)
53 width = font.get_string_width(text, dec);
54 MeshBuilder bld(mesh);
55 bld.transform(Matrix::translation(Vector3(-horz_align*width, vert_offset, 0.0f)));
56 font.build_string(text, dec, bld);
65 void Text::set_alignment(HorizontalAlign ha, VerticalAlign va)
70 case LEFT: h = 0.0f; break;
71 case CENTER: h = 0.5f; break;
72 case RIGHT: h = 1.0f; break;
73 default: throw invalid_argument("Text::set_alignment");
79 case DESCENT: v = -font.get_descent(); break;
80 case BASELINE: v = 0.0f; break;
81 case MIDLINE: v = font.get_cap_height()/2; break;
82 case ASCENT: v = font.get_ascent(); break;
83 default: throw invalid_argument("Text::set_alignment");
89 void Text::set_alignment(float h, float v)
91 if(h==horz_align && -v==vert_offset)
94 float horz_adjust = (horz_align-h)*width;
95 float vert_adjust = -v-vert_offset;
99 unsigned pos_offset = mesh.get_vertices().get_format().offset(VERTEX2);
100 unsigned n_vertices = mesh.get_n_vertices();
101 for(unsigned i=0; i<n_vertices; ++i)
103 float *pos = reinterpret_cast<float *>(mesh.modify_vertex(i)+pos_offset);
104 pos[0] += horz_adjust;
105 pos[1] += vert_adjust;
109 void Text::setup_render(Renderer &renderer, Tag tag) const
111 ObjectInstance::setup_render(renderer, tag);
112 renderer.set_texture(texture_slot, &font.get_texture());