comparison src/melodyTriangle.cpp @ 15:9d7e139cd0a3

Voices can now be dragged along the edges of the triangle. Also added screenui.sh script to run executable.
author samer
date Tue, 31 Jan 2012 12:43:21 +0000
parents 578c1acf1cc4
children add71da95cb4
comparison
equal deleted inserted replaced
14:578c1acf1cc4 15:9d7e139cd0a3
1 #include "melodyTriangle.h" 1 #include "melodyTriangle.h"
2 #include <GLUT/GLUT.h> 2 #include <GLUT/GLUT.h>
3 3
4 #define BUFFER_ZONE 50 // have to drag this far to snap out of triange. 4 #define BUFFER_ZONE 80 // have to drag this far to snap out of triange.
5 /* 5 /*
6 /birth id 6 /birth id
7 /death id 7 /death id
8 /start id 8 /start id
9 /stop id 9 /stop id
37 y1=(ofGetHeight()-triangleHeight)/2; 37 y1=(ofGetHeight()-triangleHeight)/2;
38 x2=ofGetWidth()/2-triangleHeight/sqrt(3); 38 x2=ofGetWidth()/2-triangleHeight/sqrt(3);
39 y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2; 39 y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2;
40 x3=ofGetWidth()/2+triangleHeight/sqrt(3); 40 x3=ofGetWidth()/2+triangleHeight/sqrt(3);
41 y3=y2; 41 y3=y2;
42
43 // used for clipping
44 DX13=x3-x1; DY13=y3-y1;
45 SQLEN13=DX13*DX13+DY13*DY13;
42 46
43 sendCalibrate(); 47 sendCalibrate();
44 for (int i=0;i<numVoices;i++){ 48 for (int i=0;i<numVoices;i++){
45 voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30); 49 voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30);
46 } 50 }
126 cout<< msg_string << "\n"; 130 cout<< msg_string << "\n";
127 131
128 } 132 }
129 } 133 }
130 134
131 bool melodyTriangle::isInTriangle(int x, int y){ 135 bool melodyTriangle::clipToTriangle(int *x, int *y) {
132 if (x>x2 && x<x3 && y>y1 && y<y2){ 136 bool clipped;
133 //printf("in bounding box\n"); 137
134 float dx=abs(x-x1); 138 if (*y>y2) { // off the bottom
135 float dy=abs(y-y1); 139 clipped=true;
136 //printf("tan(30)- dx/dy: %f\n",tan(30*PI/180)-dx/dy); 140 *y=y2;
141 if (*x<x2) *x=x2;
142 else if (*x>x3) *x=x3;
143 } else { // have to be a bit cleverer
144 bool reflect=false;
145 if (*x<x1) { // work in reflected coordinates
146 reflect=true;
147 *x=2*x1-*x;
148 }
137 149
138 if (dx/dy < tan(30*PI/180)){ 150 int dx=(*x-x1), dy=(*y-y1); // deltas from top
139 151 if (dx*DY13 > dy*DX13) {
140 //printf("in triangle \n"); 152 // (x,y) must be somewhere right of triangle now
141 return true; 153 clipped=true;
142 }else { 154 int dp=dx*DX13 + dy*DY13;
143 //printf("not in triangle \n"); 155 if (dp<0) { *x=x1; *y=y1; } // off the top
144 return false; 156 else if (dp>SQLEN13) { *x=x3; *y=y3; } // off the bottom right
145 } 157 else { // project onto right edge
146 158 *x=x1+dp*DX13/SQLEN13;
147 }else{ 159 *y=y1+dp*DY13/SQLEN13;
148 //printf("not in bounding box \n"); 160 }
149 return false; 161 } else {
150 } 162 clipped=false;
151 } 163 }
164
165 if (reflect) *x=2*x1 - *x; // reflect back if necessary
166 }
167 return clipped;
168 }
169
152 170
153 void melodyTriangle::sendPosition(Voice v){ 171 void melodyTriangle::sendPosition(Voice v){
154 172
155 ofxOscMessage m; 173 ofxOscMessage m;
156 ///track id x y left right top bottom area 174 ///track id x y left right top bottom area
164 } 182 }
165 183
166 //-------------------------------------------------------------- 184 //--------------------------------------------------------------
167 void melodyTriangle::draw(){ 185 void melodyTriangle::draw(){
168 bool constrained=false; 186 bool constrained=false;
169
170 bool sendStart=false; 187 bool sendStart=false;
188
171 if (voiceGrabbed!=-1){ 189 if (voiceGrabbed!=-1){
172 Voice *vg=voices[voiceGrabbed]; 190 Voice *vg=voices[voiceGrabbed];
173 if (mouseX!=vg->posx || mouseY!=vg->posy){ 191 if (mouseX!=vg->posx || mouseY!=vg->posy){
174 //vg->posx=mouseX; 192 int clipx=mouseX, clipy=mouseY;
175 //vg->posy=mouseY; 193 bool clipped=clipToTriangle(&clipx,&clipy);
176 if (vg->inTriangle && !isInTriangle(mouseX,mouseY)){ 194
177 ///death id 195 if (vg->inTriangle) {
178 196
179 if (ofDist(vg->posx, vg->posy, mouseX, mouseY) 197 if (clipped) {
180 > BUFFER_ZONE) { 198 // check how far we clipped
199 if (ofDist(clipx, clipy, mouseX, mouseY) > BUFFER_ZONE) {
200 // if far enough, we pop out of triangle and send
201 // /death <id>
202 ofxOscMessage m;
203 m.setAddress( "/death" );
204 m.addIntArg( vg->id );
205 sender.sendMessage( m );
181 206
207 printf("sent /death %i \n",vg->id);
208 vg->posx=mouseX;
209 vg->posy=mouseY;
210 vg->inTriangle=false;
211 } else {
212 // otherwise, we move to clipped point
213 constrained=true;
214 vg->posx=clipx;
215 vg->posy=clipy;
216 }
217 } else { // not clipped; normal move
218 vg->posx=mouseX;
219 vg->posy=mouseY;
220 }
221 } else { // token was outside triangle
222 vg->posx=mouseX;
223 vg->posy=mouseY;
224 if (!clipped){ // ie mouse now in triangle
225 //birth id
182 226
183 ofxOscMessage m; 227 ofxOscMessage m;
184 ///track id x y left right top bottom area 228 m.setAddress( "/birth" );
185 m.setAddress( "/death" );
186 m.addIntArg( vg->id ); 229 m.addIntArg( vg->id );
187 sender.sendMessage( m ); 230 sender.sendMessage( m );
188 231
189 printf("sent /death %i \n",vg->id); 232 printf("sent /birth %i \n",vg->id);
190 vg->posx=mouseX; 233 sendOctave(vg->id,vg->octave);
191 vg->posy=mouseY; 234 sendAmplitude(vg->id,vg->amplitude);
192 } else { 235 sendStart=true;
193 //printf("e"); 236 vg->inTriangle=true;
194 //On Edge
195 constrained=true;
196 } 237 }
197 238 }
198 }else{
199 vg->posx=mouseX;
200 vg->posy=mouseY;
201 //vg->posx=vg->posx*0.9+mouseX*0.1;
202 //vg->posy=vg->posy*0.9+mouseY*0.1;
203
204
205 }
206 if (!vg->inTriangle && isInTriangle(mouseX,mouseY)){
207 //birth id
208
209 ofxOscMessage m;
210 ///track id x y left right top bottom area
211 m.setAddress( "/birth" );
212 m.addIntArg( vg->id );
213 sender.sendMessage( m );
214
215 printf("sent /birth %i \n",vg->id);
216 sendOctave(vg->id,vg->octave);
217 sendAmplitude(vg->id,vg->amplitude);
218 sendStart=true;
219 }
220
221
222 //vg->inTriangle = isInTriangle(mouseX,mouseY);
223
224 vg->inTriangle = isInTriangle(vg->posx,vg->posy);
225 239
226 if (vg->inTriangle){ 240 if (vg->inTriangle){
227 sendPosition(*vg); 241 sendPosition(*vg);
228 if (sendStart){ 242 if (sendStart && vg->isActive){
229 if (vg->isActive){ 243 ofxOscMessage m;
230 ofxOscMessage m; 244 ///track id x y left right top bottom area
231 ///track id x y left right top bottom area 245 m.setAddress( "/start" );
232 m.setAddress( "/start" ); 246 m.addIntArg( vg->id );
233 m.addIntArg( vg->id ); 247 sender.sendMessage( m );
234 sender.sendMessage( m ); 248 printf("sent /start %i \n",vg->id);
235 printf("sent /start %i \n",vg->id);
236 }
237 } 249 }
238 } 250 }
239
240 } 251 }
241 }; 252 };
242 253
243 //let's draw our triangle 254 //let's draw our triangle
244 ofSetLineWidth(2); 255 ofSetLineWidth(2);
255 (*voices[i]).draw(); 266 (*voices[i]).draw();
256 } 267 }
257 268
258 269
259 } 270 }
260
261 271
262 //-------------------------------------------------------------- 272 //--------------------------------------------------------------
263 void melodyTriangle::keyPressed (int key){ 273 void melodyTriangle::keyPressed (int key){
264 //printf("key %i",key); 274 //printf("key %i",key);
265 if (enableKeys){ 275 if (enableKeys){