]> git.tdb.fi Git - r2c2.git/blob - source/designer/measure.cpp
Split the Manipulator class into several Tools
[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, const set<Object *> &):
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                 {
47                         done = true;
48                         signal_done.emit();
49                 }
50         }
51 }
52
53 void Measure::axis_motion(unsigned axis, float x, float y)
54 {
55         Tool::axis_motion(axis, x, y);
56
57         Snap sn = start;
58         sn.position = ground_pointer;
59         snap_to_tracks(sn);
60         ground_pointer = sn.position;
61
62         if(start_pinned)
63         {
64                 Vector delta = rotated_vector(ground_pointer-start.position, -start.rotation);
65
66                 par_dist = delta.x;
67                 perp_dist = delta.y;
68
69                 adiff = wrap_balanced(sn.rotation-start.rotation+Angle::half_turn());
70
71                 update_mesh();
72
73                 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", par_dist*1000, perp_dist*1000, delta.norm()*1000, adiff.degrees());
74                 signal_status.emit(info);
75         }
76 }
77
78 void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
79 {
80         GL::Renderer::Push push(renderer);
81         const Vector &pos = (start_pinned ? start.position : ground_pointer);
82         renderer.matrix_stack() *= GL::Matrix::translation(pos);
83
84         mesh.draw(renderer);
85 }
86
87 void Measure::update_mesh()
88 {
89         mesh.clear();
90         GL::MeshBuilder bld(mesh);
91         bld.color(1.0f, 1.0f, 1.0f, 1.0f);
92         bld.begin(GL::QUAD_STRIP);
93         for(unsigned i=0; i<=16; ++i)
94         {
95                 float x = cos(i*M_PI/8)*0.005;
96                 float y = sin(i*M_PI/8)*0.005;
97                 bld.vertex(x, y, 0);
98                 bld.vertex(x, y, 0.01);
99         }
100         bld.end();
101
102         if(start_pinned)
103         {
104                 float c = cos(start.rotation);
105                 float s = sin(start.rotation);
106                 bld.begin(GL::QUAD_STRIP);
107                 bld.vertex(0, 0, 0);
108                 bld.vertex(0, 0, 0.01);
109                 bld.vertex(c*par_dist, s*par_dist, 0);
110                 bld.vertex(c*par_dist, s*par_dist, 0.01);
111                 bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0);
112                 bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0.01);
113                 bld.vertex(0, 0, 0);
114                 bld.vertex(0, 0, 0.01);
115                 bld.end();
116         }
117 }
118
119 void Measure::snap_to_tracks(Snap &sn)
120 {
121         const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
122         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
123                 if((*i)->snap(sn, 0.01, SNAP_NODE))
124                         return;
125 }