]> git.tdb.fi Git - r2c2.git/blob - source/designer/measure.cpp
Reduce interface clutter in Layout by storing Objects in a uniform way
[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 = Angle::zero();
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 = rotated_vector(pointer-ssnap.position, -ssnap.rotation);
65
66                 par_dist = delta.x;
67                 perp_dist = delta.y;
68
69                 adiff = wrap_balanced(sn.rotation-ssnap.rotation+Angle::half_turn());
70
71                 update_mesh();
72
73                 signal_changed.emit();
74         }
75 }
76
77 void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
78 {
79         if(state==NONE)
80                 return;
81
82         GL::Renderer::Push push(renderer);
83         const Vector &pos = (state==ACTIVE ? ssnap.position : pointer);
84         renderer.matrix_stack() *= GL::Matrix::translation(pos);
85
86         mesh.draw(renderer);
87 }
88
89 void Measure::update_mesh()
90 {
91         mesh.clear();
92         GL::MeshBuilder bld(mesh);
93         bld.color(1.0f, 1.0f, 1.0f, 1.0f);
94         bld.begin(GL::QUAD_STRIP);
95         for(unsigned i=0; i<=16; ++i)
96         {
97                 float x = cos(i*M_PI/8)*0.005;
98                 float y = sin(i*M_PI/8)*0.005;
99                 bld.vertex(x, y, 0);
100                 bld.vertex(x, y, 0.01);
101         }
102         bld.end();
103
104         if(state==ACTIVE)
105         {
106                 float c = cos(ssnap.rotation);
107                 float s = sin(ssnap.rotation);
108                 bld.begin(GL::QUAD_STRIP);
109                 bld.vertex(0, 0, 0);
110                 bld.vertex(0, 0, 0.01);
111                 bld.vertex(c*par_dist, s*par_dist, 0);
112                 bld.vertex(c*par_dist, s*par_dist, 0.01);
113                 bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
114                 bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0.01);
115                 bld.vertex(0, 0, 0);
116                 bld.vertex(0, 0, 0.01);
117                 bld.end();
118         }
119 }
120
121 void Measure::snap_to_tracks(Snap &sn)
122 {
123         const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
124         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
125                 if((*i)->snap(sn, 0.01, SNAP_NODE))
126                         return;
127 }