]> git.tdb.fi Git - r2c2.git/blob - measure.cpp
e5e5abb542c3a434a96795df13e8c7f6e1571c01
[r2c2.git] / measure.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <GL/gl.h>
10 #include "designer.h"
11 #include "3d/layout.h"
12 #include "measure.h"
13
14 using namespace std;
15 using namespace R2C2;
16 using namespace Msp;
17
18 Measure::Measure(Designer &d):
19         designer(d),
20         state(NONE)
21 { }
22
23 void Measure::start()
24 {
25         state = STARTING;
26 }
27
28 void Measure::snap_to_tracks(Vector &pt, float &dir)
29 {
30         const set<Track *> &ltracks = designer.get_layout().get_tracks();
31         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
32                 if((*i)->snap(pt, dir))
33                         return;
34 }
35
36 void Measure::button_press(int, int, float gx, float gy, unsigned btn)
37 {
38         if(!state)
39                 return;
40
41         if(btn==1)
42         {
43                 spoint = Vector(gx, gy, 0);
44                 sdir = 0;
45                 snap_to_tracks(spoint, sdir);
46
47                 state = ACTIVE;
48         }
49         else if(btn==3)
50         {
51                 if(state==ACTIVE)
52                         state = STARTING;
53                 else
54                 {
55                         state = NONE;
56                         signal_done.emit();
57                 }
58         }
59 }
60
61 void Measure::pointer_motion(int, int, float gx, float gy)
62 {
63         if(!state)
64                 return;
65
66         pointer = Vector(gx, gy, 0);
67         float dir = sdir;
68         snap_to_tracks(pointer, dir);
69
70         if(state!=STARTING)
71         {
72                 Vector delta(pointer.x-spoint.x, pointer.y-spoint.y, 0);
73                 float c = cos(sdir);
74                 float s = sin(sdir);
75
76                 par_dist = delta.x*c+delta.y*s;
77                 perp_dist = delta.x*s-delta.y*c;
78
79                 adiff = dir-sdir+M_PI;
80                 while(adiff<-M_PI)
81                         adiff += M_PI*2;
82                 while(adiff>M_PI)
83                         adiff -= M_PI*2;
84
85                 signal_changed.emit();
86         }
87 }
88
89 void Measure::render()
90 {
91         glPushMatrix();
92         if(state==ACTIVE)
93                 glTranslatef(spoint.x, spoint.y, spoint.z);
94         else if(state==STARTING)
95                 glTranslatef(pointer.x, pointer.y, pointer.z);
96
97         glDisable(GL_CULL_FACE);
98         glColor4f(1, 1, 1, 1);
99         glBegin(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                 glVertex3f(x, y, 0);
105                 glVertex3f(x, y, 0.01);
106         }
107         glEnd();
108
109         if(state==ACTIVE)
110         {
111                 float c = cos(sdir);
112                 float s = sin(sdir);
113                 glBegin(GL_QUAD_STRIP);
114                 glVertex3f(0, 0, 0);
115                 glVertex3f(0, 0, 0.01);
116                 glVertex3f(c*par_dist, s*par_dist, 0);
117                 glVertex3f(c*par_dist, s*par_dist, 0.01);
118                 glVertex3f(pointer.x-spoint.x, pointer.y-spoint.y, 0);
119                 glVertex3f(pointer.x-spoint.x, pointer.y-spoint.y, 0.01);
120                 /*glVertex3f(s*perp_dist, -c*perp_dist, 0);
121                 glVertex3f(s*perp_dist, -c*perp_dist, 0.01);*/
122                 glVertex3f(0, 0, 0);
123                 glVertex3f(0, 0, 0.01);
124                 glEnd();
125         }
126
127         glPopMatrix();
128 }