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