]> git.tdb.fi Git - r2c2.git/blob - source/engineer/clockwidget.cpp
Better graphics for the clock
[r2c2.git] / source / engineer / clockwidget.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include <msp/gl/renderer.h>
3 #include <msp/gltk/graphic.h>
4 #include <msp/gltk/part.h>
5 #include "clockwidget.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 ClockWidget::ClockWidget(R2C2::Clock &c):
11         clock(c),
12         hour_hand((GL::VERTEX2, GL::TEXCOORD2, GL::COLOR4_UBYTE)),
13         minute_hand((GL::VERTEX2, GL::TEXCOORD2, GL::COLOR4_UBYTE))
14 { }
15
16 void ClockWidget::rebuild_special(const GLtk::Part &part)
17 {
18         if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
19         {
20                 bool minute = (part.get_name()=="minutehand");
21                 GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
22
23                 const GLtk::Graphic *graphic = part.get_graphic(state);
24                 const GLtk::Alignment &align = part.get_alignment();
25
26                 mesh.clear();
27                 GL::MeshBuilder bld(mesh);
28                 bld.matrix() *= GL::Matrix::translation(GL::Vector3(graphic->get_width()*-align.x, graphic->get_height()*-align.y, 0));
29                 graphic->build(graphic->get_width(), graphic->get_height(), bld);
30
31                 part_cache.insert_special(part);
32         }
33 }
34
35 void ClockWidget::render_special(const GLtk::Part &part, GL::Renderer &renderer) const
36 {
37         if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
38         {
39                 bool minute = (part.get_name()=="minutehand");
40                 const GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
41
42                 float orientation = clock.get_current_time()/((minute ? 1 : 12)*Time::hour);
43                 Geometry::Angle<float> angle = Geometry::Angle<float>::from_turns(orientation);
44
45                 GL::Renderer::Push push(renderer);
46                 renderer.matrix_stack() *= GL::Matrix::translation(GL::Vector3(geom.w*0.5f, geom.h*0.5f, 0.0f));
47                 renderer.matrix_stack() *= GL::Matrix::rotation(angle, GL::Vector3(0, 0, -1));
48                 mesh.draw(renderer);
49         }
50 }