]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/clockwidget.cpp
Add a dialog to control the clock
[r2c2.git] / source / engineer / clockwidget.cpp
diff --git a/source/engineer/clockwidget.cpp b/source/engineer/clockwidget.cpp
new file mode 100644 (file)
index 0000000..31f5c39
--- /dev/null
@@ -0,0 +1,58 @@
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/renderer.h>
+#include <msp/gltk/part.h>
+#include "clockwidget.h"
+
+using namespace std;
+using namespace Msp;
+
+ClockWidget::ClockWidget(R2C2::Clock &c):
+       clock(c),
+       hour_hand((GL::VERTEX2, GL::COLOR4_UBYTE)),
+       minute_hand((GL::VERTEX2, GL::COLOR4_UBYTE))
+{ }
+
+void ClockWidget::autosize_special(const GLtk::Part &, GLtk::Geometry &ageom) const
+{
+       ageom.w = max(ageom.w, 200U);
+       ageom.h = max(ageom.h, 200U);
+}
+
+void ClockWidget::rebuild_special(const GLtk::Part &part)
+{
+       if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
+       {
+               bool minute = (part.get_name()=="minutehand");
+               GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
+               float length = min(geom.w, geom.h)*(minute ? 0.5f : 0.35f);
+               float width = length*(minute ? 0.05f : 0.1f);
+               mesh.clear();
+               GL::MeshBuilder bld(mesh);
+               bld.color(0.0f, 0.0f, 0.0f);
+               bld.begin(GL::TRIANGLES);
+               bld.vertex(width, -width);
+               bld.vertex(0.0f, length);
+               bld.vertex(-width, -width);
+               bld.end();
+
+               part_cache.insert_special(part);
+       }
+}
+
+void ClockWidget::render_special(const GLtk::Part &part, GL::Renderer &renderer) const
+{
+       if(part.get_name()=="hourhand" || part.get_name()=="minutehand")
+       {
+               bool minute = (part.get_name()=="minutehand");
+               const GL::Mesh &mesh = (minute ? minute_hand : hour_hand);
+
+               float orientation = clock.get_current_time()/((minute ? 1 : 12)*Time::hour);
+               Geometry::Angle<float> angle = Geometry::Angle<float>::from_turns(orientation);
+
+               GL::Renderer::Push push(renderer);
+               renderer.set_texture(0);
+               renderer.matrix_stack() *= GL::Matrix::translation(GL::Vector3(geom.w*0.5f, geom.h*0.5f, 0.0f));
+               renderer.matrix_stack() *= GL::Matrix::rotation(angle, GL::Vector3(0, 0, -1));
+               mesh.draw(renderer);
+       }
+}