changeset 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
files release/MelodyTriangle.app.tar.gz screenui.sh src/melodyTriangle.cpp src/melodyTriangle.h
diffstat 4 files changed, 87 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
Binary file release/MelodyTriangle.app.tar.gz has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/screenui.sh	Tue Jan 31 12:43:21 2012 +0000
@@ -0,0 +1,2 @@
+#!/bin/sh
+bin/MelodyTriangle.app/Contents/MacOS/MelodyTriangle localhost 7770 "$@" 
--- a/src/melodyTriangle.cpp	Mon Jan 30 23:31:39 2012 +0000
+++ b/src/melodyTriangle.cpp	Tue Jan 31 12:43:21 2012 +0000
@@ -1,7 +1,7 @@
 #include "melodyTriangle.h"
 #include <GLUT/GLUT.h> 
 
-#define BUFFER_ZONE 50 // have to drag this far to snap out of triange.
+#define BUFFER_ZONE 80 // have to drag this far to snap out of triange.
 /*
  /birth id
  /death id
@@ -40,6 +40,10 @@
 	x3=ofGetWidth()/2+triangleHeight/sqrt(3);
 	y3=y2;
 	
+	// used for clipping
+	DX13=x3-x1; DY13=y3-y1;
+	SQLEN13=DX13*DX13+DY13*DY13;
+	
 	sendCalibrate();
 	for (int i=0;i<numVoices;i++){
 		voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30);
@@ -128,28 +132,42 @@
 	}
 }
 
-bool melodyTriangle::isInTriangle(int x, int y){
-	if (x>x2 && x<x3 && y>y1 && y<y2){
-		//printf("in bounding box\n");
-		float dx=abs(x-x1);
-		float dy=abs(y-y1);
-		//printf("tan(30)- dx/dy: %f\n",tan(30*PI/180)-dx/dy);
+bool melodyTriangle::clipToTriangle(int *x, int *y) {
+	bool clipped;
+	
+	if (*y>y2) { // off the bottom
+		clipped=true;
+		*y=y2;
+		if (*x<x2) *x=x2; 
+		else if (*x>x3) *x=x3;
+	} else { // have to be a bit cleverer
+		bool reflect=false;
+		if (*x<x1) { // work in reflected coordinates
+			reflect=true;
+			*x=2*x1-*x;
+		}
 		
-		if (dx/dy < tan(30*PI/180)){
-			
-			//printf("in triangle \n");
-			return true;
-		}else {
-			//printf("not in triangle \n");
-			return false;
+		int dx=(*x-x1), dy=(*y-y1); // deltas from top
+		if (dx*DY13 > dy*DX13) {
+			// (x,y) must be somewhere right of triangle now
+			clipped=true;
+			int dp=dx*DX13 + dy*DY13;
+			if (dp<0) { *x=x1; *y=y1; } // off the top
+			else if (dp>SQLEN13) { *x=x3; *y=y3; } // off the bottom right
+			else { // project onto right edge
+				*x=x1+dp*DX13/SQLEN13;
+				*y=y1+dp*DY13/SQLEN13;
+			}
+		} else {
+			clipped=false;
 		}
 
-	}else{
-		//printf("not in bounding box \n");
-		return false;
+		if (reflect) *x=2*x1 - *x; // reflect back if necessary
 	}
+	return clipped;
 }
 
+
 void melodyTriangle::sendPosition(Voice v){
 	
 	ofxOscMessage m;
@@ -166,77 +184,70 @@
 //--------------------------------------------------------------
 void melodyTriangle::draw(){
 	bool constrained=false;
-	
 	bool sendStart=false;
+
 	if (voiceGrabbed!=-1){
 		Voice *vg=voices[voiceGrabbed];
 		if (mouseX!=vg->posx || mouseY!=vg->posy){
-			//vg->posx=mouseX;
-			//vg->posy=mouseY;
-			if (vg->inTriangle && !isInTriangle(mouseX,mouseY)){
-			    ///death id
+			int clipx=mouseX, clipy=mouseY;
+			bool clipped=clipToTriangle(&clipx,&clipy);
+			
+			if (vg->inTriangle) {
 				
-				if (ofDist(vg->posx, vg->posy, mouseX, mouseY)
-					> BUFFER_ZONE) { 
+				if (clipped) {
+					// check how far we clipped
+					if (ofDist(clipx, clipy, mouseX, mouseY) > BUFFER_ZONE) { 
+						// if far enough, we pop out of triangle and send
+						// /death <id>
+						ofxOscMessage m;
+						m.setAddress( "/death" );
+						m.addIntArg( vg->id );
+						sender.sendMessage( m );
 				
+						printf("sent /death %i \n",vg->id);
+						vg->posx=mouseX;
+						vg->posy=mouseY;
+						vg->inTriangle=false;
+					} else {
+						// otherwise, we move to clipped point
+						constrained=true;
+						vg->posx=clipx;
+						vg->posy=clipy;
+					}
+				} else { // not clipped; normal move
+					vg->posx=mouseX;
+					vg->posy=mouseY;
+				}
+			} else { // token was outside triangle
+				vg->posx=mouseX;
+				vg->posy=mouseY;
+				if (!clipped){ // ie mouse now in triangle
+					//birth id
 					
 					ofxOscMessage m;
-					///track id x y left right top bottom area  
-					m.setAddress( "/death" );
+					m.setAddress( "/birth" );
 					m.addIntArg( vg->id );
 					sender.sendMessage( m );
-				
-					printf("sent /death %i \n",vg->id);
-					vg->posx=mouseX;
-					vg->posy=mouseY;
-				} else {
-					//printf("e");
-					//On Edge
-					constrained=true;
+					
+					printf("sent /birth %i \n",vg->id);
+					sendOctave(vg->id,vg->octave);
+					sendAmplitude(vg->id,vg->amplitude);
+					sendStart=true;
+					vg->inTriangle=true;
 				}
-
-			}else{
-				vg->posx=mouseX;
-				vg->posy=mouseY;
-				//vg->posx=vg->posx*0.9+mouseX*0.1;
-				//vg->posy=vg->posy*0.9+mouseY*0.1;
-				
-				
 			}
-			if (!vg->inTriangle && isInTriangle(mouseX,mouseY)){
-				//birth id
-				
-				ofxOscMessage m;
-				///track id x y left right top bottom area  
-				m.setAddress( "/birth" );
-				m.addIntArg( vg->id );
-				sender.sendMessage( m );
-				
-				printf("sent /birth %i \n",vg->id);
-				sendOctave(vg->id,vg->octave);
-				sendAmplitude(vg->id,vg->amplitude);
-				sendStart=true;
-			}
-			
-			
-			//vg->inTriangle = isInTriangle(mouseX,mouseY);
-			
-			vg->inTriangle = isInTriangle(vg->posx,vg->posy);
 			
 			if (vg->inTriangle){
 				sendPosition(*vg);
-				if (sendStart){
-					if (vg->isActive){
-						ofxOscMessage m;
-						///track id x y left right top bottom area  
-						m.setAddress( "/start" );
-						m.addIntArg( vg->id );
-						sender.sendMessage( m );
-						printf("sent /start %i \n",vg->id);
-					}
+				if (sendStart && vg->isActive){
+					ofxOscMessage m;
+					///track id x y left right top bottom area  
+					m.setAddress( "/start" );
+					m.addIntArg( vg->id );
+					sender.sendMessage( m );
+					printf("sent /start %i \n",vg->id);
 				}
 			}
-			
 		}
 	};
 
@@ -258,7 +269,6 @@
 	
 }
 
-
 //--------------------------------------------------------------
 void melodyTriangle::keyPressed  (int key){
 	//printf("key %i",key);
--- a/src/melodyTriangle.h	Mon Jan 30 23:31:39 2012 +0000
+++ b/src/melodyTriangle.h	Tue Jan 31 12:43:21 2012 +0000
@@ -30,7 +30,6 @@
 		bool enableKeys;
 		float 	counter;
 		bool	bSmooth;
-		//Voice *voices[NUMVOICES];
 		Voice *voices[10];
 	
 		int x1,y1,x2,y2,x3,y3;//Triangle Coords
@@ -42,10 +41,12 @@
 		void sendShift(int id, int num, int den);
 		void sendOctave(int id, int oct);
 		void sendAmplitude(int id, float amp);
-	    bool isInTriangle(int x, int y);
 		
 	
 	private:
 		ofxOscSender sender;
 		ofxOscReceiver	receiver;
+		int DX13, DY13, SQLEN13;
+	
+		bool clipToTriangle(int *cx, int *cy);
 };