]> git.tdb.fi Git - r2c2.git/blob - source/engineer/clockwidget.cpp
31f5c3988a33a41954579d536fa651b2e65cbef6
[r2c2.git] / source / engineer / clockwidget.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include <msp/gl/renderer.h>
3 #include <msp/gltk/part.h>
4 #include "clockwidget.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 ClockWidget::ClockWidget(R2C2::Clock &c):
10         clock(c),
11         hour_hand((GL::VERTEX2, GL::COLOR4_UBYTE)),
12         minute_hand((GL::VERTEX2, GL::COLOR4_UBYTE))
13 { }
14
15 void ClockWidget::autosize_special(const GLtk::Part &, GLtk::Geometry &ageom) const
16 {
17         ageom.w = max(ageom.w, 200U);
18         ageom.h = max(ageom.h, 200U);
19 }
20
21 void ClockWidget::rebuild_special(const GLtk::Part &part)
22 {
23         if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
24         {
25                 bool minute = (part.get_name()=="minutehand");
26                 GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
27                 float length = min(geom.w, geom.h)*(minute ? 0.5f : 0.35f);
28                 float width = length*(minute ? 0.05f : 0.1f);
29                 mesh.clear();
30                 GL::MeshBuilder bld(mesh);
31                 bld.color(0.0f, 0.0f, 0.0f);
32                 bld.begin(GL::TRIANGLES);
33                 bld.vertex(width, -width);
34                 bld.vertex(0.0f, length);
35                 bld.vertex(-width, -width);
36                 bld.end();
37
38                 part_cache.insert_special(part);
39         }
40 }
41
42 void ClockWidget::render_special(const GLtk::Part &part, GL::Renderer &renderer) const
43 {
44         if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
45         {
46                 bool minute = (part.get_name()=="minutehand");
47                 const GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
48
49                 float orientation = clock.get_current_time()/((minute ? 1 : 12)*Time::hour);
50                 Geometry::Angle<float> angle = Geometry::Angle<float>::from_turns(orientation);
51
52                 GL::Renderer::Push push(renderer);
53                 renderer.set_texture(0);
54                 renderer.matrix_stack() *= GL::Matrix::translation(GL::Vector3(geom.w*0.5f, geom.h*0.5f, 0.0f));
55                 renderer.matrix_stack() *= GL::Matrix::rotation(angle, GL::Vector3(0, 0, -1));
56                 mesh.draw(renderer);
57         }
58 }