]> git.tdb.fi Git - r2c2.git/blob - source/designer/measure.cpp
Refactor Designer::use_tool so tools don't need to take a set of objects
[r2c2.git] / source / designer / measure.cpp
1 #include <cmath>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/renderer.h>
4 #include <msp/strings/format.h>
5 #include "designer.h"
6 #include "3d/layout.h"
7 #include "measure.h"
8
9 using namespace std;
10 using namespace R2C2;
11 using namespace Msp;
12
13 Measure::Measure(Designer &d, Input::Mouse &m):
14         Tool(d, m),
15         start_pinned(false),
16         mesh((GL::COLOR4_UBYTE, GL::VERTEX3))
17 {
18         update_mesh();
19
20         designer.get_layout_3d().get_scene().add(*this);
21 }
22
23 Measure::~Measure()
24 {
25         designer.get_layout_3d().get_scene().remove(*this);
26 }
27
28 void Measure::button_press(unsigned btn)
29 {
30         if(btn==1)
31         {
32                 start.position = ground_pointer;
33                 start.rotation = Angle::zero();
34                 snap_to_tracks(start);
35
36                 start_pinned = true;
37         }
38         else if(btn==3)
39         {
40                 if(start_pinned)
41                 {
42                         start_pinned = false;
43                         update_mesh();
44                 }
45                 else
46                         set_done(true);
47         }
48 }
49
50 void Measure::pointer_motion()
51 {
52         Snap sn = start;
53         sn.position = ground_pointer;
54         snap_to_tracks(sn);
55         ground_pointer = sn.position;
56
57         if(start_pinned)
58         {
59                 Vector delta = rotated_vector(ground_pointer-start.position, -start.rotation);
60
61                 par_dist = delta.x;
62                 perp_dist = delta.y;
63
64                 adiff = wrap_balanced(sn.rotation-start.rotation+Angle::half_turn());
65
66                 update_mesh();
67
68                 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", par_dist*1000, perp_dist*1000, delta.norm()*1000, adiff.degrees());
69                 signal_status.emit(info);
70         }
71 }
72
73 void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
74 {
75         GL::Renderer::Push push(renderer);
76         const Vector &pos = (start_pinned ? start.position : ground_pointer);
77         renderer.matrix_stack() *= GL::Matrix::translation(pos);
78
79         mesh.draw(renderer);
80 }
81
82 void Measure::update_mesh()
83 {
84         mesh.clear();
85         GL::MeshBuilder bld(mesh);
86         bld.color(1.0f, 1.0f, 1.0f, 1.0f);
87         bld.begin(GL::QUAD_STRIP);
88         for(unsigned i=0; i<=16; ++i)
89         {
90                 float x = cos(i*M_PI/8)*0.005;
91                 float y = sin(i*M_PI/8)*0.005;
92                 bld.vertex(x, y, 0);
93                 bld.vertex(x, y, 0.01);
94         }
95         bld.end();
96
97         if(start_pinned)
98         {
99                 float c = cos(start.rotation);
100                 float s = sin(start.rotation);
101                 bld.begin(GL::QUAD_STRIP);
102                 bld.vertex(0, 0, 0);
103                 bld.vertex(0, 0, 0.01);
104                 bld.vertex(c*par_dist, s*par_dist, 0);
105                 bld.vertex(c*par_dist, s*par_dist, 0.01);
106                 bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0);
107                 bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0.01);
108                 bld.vertex(0, 0, 0);
109                 bld.vertex(0, 0, 0.01);
110                 bld.end();
111         }
112 }
113
114 void Measure::snap_to_tracks(Snap &sn)
115 {
116         const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
117         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
118                 if((*i)->snap(sn, 0.01, SNAP_NODE))
119                         return;
120 }