]> git.tdb.fi Git - r2c2.git/blob - source/designer/measure.cpp
69ada814e110ff53935e8151e70462762cf8e059
[r2c2.git] / source / designer / measure.cpp
1 #include <cmath>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/renderer.h>
4 #include "designer.h"
5 #include "3d/layout.h"
6 #include "measure.h"
7
8 using namespace std;
9 using namespace R2C2;
10 using namespace Msp;
11
12 Measure::Measure(Designer &d):
13         designer(d),
14         state(NONE),
15         mesh((GL::COLOR4_UBYTE, GL::VERTEX3))
16 { }
17
18 void Measure::start()
19 {
20         state = STARTING;
21         update_mesh();
22 }
23
24 void Measure::button_press(float gx, float gy, unsigned btn)
25 {
26         if(!state)
27                 return;
28
29         if(btn==1)
30         {
31                 ssnap.position = Vector(gx, gy, 0);
32                 ssnap.rotation = 0;
33                 snap_to_tracks(ssnap);
34
35                 state = ACTIVE;
36         }
37         else if(btn==3)
38         {
39                 if(state==ACTIVE)
40                 {
41                         state = STARTING;
42                         update_mesh();
43                 }
44                 else
45                 {
46                         state = NONE;
47                         signal_done.emit();
48                 }
49         }
50 }
51
52 void Measure::pointer_motion(float gx, float gy)
53 {
54         if(!state)
55                 return;
56
57         Snap sn = ssnap;
58         sn.position = Vector(gx, gy, 0);
59         snap_to_tracks(sn);
60         pointer = sn.position;
61
62         if(state!=STARTING)
63         {
64                 Vector delta(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
65                 float c = cos(ssnap.rotation);
66                 float s = sin(ssnap.rotation);
67
68                 par_dist = delta.x*c+delta.y*s;
69                 perp_dist = delta.x*s-delta.y*c;
70
71                 adiff = sn.rotation-ssnap.rotation+M_PI;
72                 while(adiff<-M_PI)
73                         adiff += M_PI*2;
74                 while(adiff>M_PI)
75                         adiff -= M_PI*2;
76
77                 update_mesh();
78
79                 signal_changed.emit();
80         }
81 }
82
83 void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
84 {
85         if(state==NONE)
86                 return;
87
88         GL::Renderer::Push push(renderer);
89         const Vector &pos = (state==ACTIVE ? ssnap.position : pointer);
90         renderer.matrix_stack() *= GL::Matrix::translation(pos.x, pos.y, pos.z);
91
92         mesh.draw(renderer);
93 }
94
95 void Measure::update_mesh()
96 {
97         mesh.clear();
98         GL::MeshBuilder bld(mesh);
99         bld.color(1.0f, 1.0f, 1.0f, 1.0f);
100         bld.begin(GL::QUAD_STRIP);
101         for(unsigned i=0; i<=16; ++i)
102         {
103                 float x = cos(i*M_PI/8)*0.005;
104                 float y = sin(i*M_PI/8)*0.005;
105                 bld.vertex(x, y, 0);
106                 bld.vertex(x, y, 0.01);
107         }
108         bld.end();
109
110         if(state==ACTIVE)
111         {
112                 float c = cos(ssnap.rotation);
113                 float s = sin(ssnap.rotation);
114                 bld.begin(GL::QUAD_STRIP);
115                 bld.vertex(0, 0, 0);
116                 bld.vertex(0, 0, 0.01);
117                 bld.vertex(c*par_dist, s*par_dist, 0);
118                 bld.vertex(c*par_dist, s*par_dist, 0.01);
119                 bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
120                 bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0.01);
121                 bld.vertex(0, 0, 0);
122                 bld.vertex(0, 0, 0.01);
123                 bld.end();
124         }
125 }
126
127 void Measure::snap_to_tracks(Snap &sn)
128 {
129         const set<Track *> &ltracks = designer.get_layout().get_tracks();
130         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
131                 if((*i)->snap(sn, 0.01, SNAP_NODE))
132                         return;
133 }