Mercurial > hg > screen-ui
changeset 0:2db9be889344
Initial Commit
author | Henrik Ekeus <hekeus@eecs.qmul.ac.uk> |
---|---|
date | Mon, 23 Jan 2012 16:46:37 +0000 |
parents | |
children | 3b6ab3b28808 |
files | Voice.cpp Voice.h main.cpp melodyTriangle.cpp melodyTriangle.h testApp.cpp testApp.h |
diffstat | 7 files changed, 720 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Voice.cpp Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,50 @@ +/* + * Voice.cpp + * MelodyTriangle + * + * Created by Henrik Ekeus on 12/01/2012. + * Copyright 2012 Queen Mary University of London. All rights reserved. + * + */ + +#include "Voice.h" + +Voice::Voice(int id, int x, int y){ + this->id=id; + posx=x; + posy=y; + isActive=true; + radius=15; + inTriangle=false; + octave=0; + highlight=false; +} + +Voice::Voice(){} + +void Voice::draw(){ + ofSetColor(255,0,0); + + if (isActive){ + + ofFill(); + }else { + ofNoFill(); + } + ofCircle(posx, posy, radius); + if (highlight){ + ofNoFill(); + ofSetColor(255, 255, 255); + ofCircle(posx, posy, radius); + } +} + +bool Voice::isInVoice(int x, int y){ + if (ofDist(x, y, posx, posy)<=radius){ + return true; + }else { + return false; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Voice.h Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,27 @@ +/* + * Voice.h + * MelodyTriangle + * + * Created by Henrik Ekeus on 12/01/2012. + * Copyright 2012 Queen Mary University of London. All rights reserved. + * + */ +#include "ofMain.h" +class Voice { +public: + + void draw(); + Voice(int id, int x, int y); + Voice(); + bool isInVoice(int x, int y); + + int id; + int posx,posy; + bool isActive; + bool inTriangle; + int radius; + int octave; + bool highlight; + + +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,26 @@ +#include "ofMain.h" +#include "melodyTriangle.h" +#include "ofAppGlutWindow.h" + +#define HOST "localhost" +#define PORT 7770 +#define NUMVOICES 3 + +//======================================================================== +int main(int argc, const char **argv ){ + + ofAppGlutWindow window; + + if (argc>4 ? atoi(argv[4]) : false){ + ofSetupOpenGL(&window, 1280,800, OF_FULLSCREEN); // <-------- setup the GL context + }else{ + ofSetupOpenGL(&window, 800,600, OF_WINDOW); + } + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp( new melodyTriangle(argc>1 ? argv[1] : HOST, + argc>2 ? atoi(argv[2]) : PORT, + argc>3 ? atoi(argv[3]) : NUMVOICES)); + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/melodyTriangle.cpp Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,424 @@ +#include "melodyTriangle.h" +#include <GLUT/GLUT.h> + +/* + /birth id + /death id + /start id + /stop id + /track id x y left right top bottom area + /tempo + + + */ +melodyTriangle::melodyTriangle(const char *host, int port, int numVoices){ + printf("in constructor: %s %i %i\n",host,port,numVoices); + this->numVoices=numVoices; + + //voices=*Voice[numVoices]; + sender.setup( host,port ); +} + +//-------------------------------------------------------------- +void melodyTriangle::setup(){ + //voices = new Voice[NUMVOICES]; + + ofSetCircleResolution(100); + ofBackground(0,0,0); + ofSetWindowTitle("Melody Triangle"); + triangleHeight=ofGetHeight()*0.75; + ofSetFrameRate(40); // if vertical sync is off, we can go a bit fast... this caps the framerate at 60fps. + ofEnableSmoothing(); + x1=ofGetWidth()/2; + y1=(ofGetHeight()-triangleHeight)/2; + x2=ofGetWidth()/2-triangleHeight/sqrt(3); + y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2; + x3=ofGetWidth()/2+triangleHeight/sqrt(3); + y3=y2; + + ofxOscMessage m; + m.setAddress( "/calibrate" ); + m.addIntArg( x1 ); + m.addIntArg( y1 ); + m.addIntArg( x2 ); + m.addIntArg( y2 ); + m.addIntArg( x3 ); + m.addIntArg( y3 ); + sender.sendMessage( m ); + printf("sent /calibrate %i %i %i %i %i %i\n",x1,y1,x2,y2,x3,y3); + + for (int i=0;i<numVoices;i++){ + + voices[i]=new Voice(i+1,x2+15,y1+20+i*30); + } + voiceGrabbed=-1; +} + +//-------------------------------------------------------------- +void melodyTriangle::update(){ +} + +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); + + if (dx/dy < tan(30*PI/180)){ + + //printf("in triangle \n"); + return true; + }else { + //printf("not in triangle \n"); + return false; + } + + }else{ + //printf("not in bounding box \n"); + return false; + } +} + +void melodyTriangle::sendStatus(Voice v){ + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/track2d" ); + m.addIntArg( v.id ); + m.addIntArg( v.posx ); + m.addIntArg( v.posy ); + sender.sendMessage( m ); + printf("sent - /track2d %i %i %i\n",v.id,v.posx,v.posy); + +} + +//-------------------------------------------------------------- +void melodyTriangle::draw(){ + + + //let's draw our triangle + ofSetColor(0,0,255); + + ofFill(); + ofTriangle(x1, y1, x2, y2, x3, y3); + bool sendStart=false; + if (voiceGrabbed!=-1){ + if (mouseX!=(*voices[voiceGrabbed]).posx || mouseY!=(*voices[voiceGrabbed]).posy){ + //(*voices[voiceGrabbed]).posx=mouseX; + //(*voices[voiceGrabbed]).posy=mouseY; + if ((*voices[voiceGrabbed]).inTriangle && !isInTriangle(mouseX,mouseY)){ + ///death id + + if (ofDist((*voices[voiceGrabbed]).posx, (*voices[voiceGrabbed]).posy, mouseX, mouseY) > (*voices[voiceGrabbed]).radius*2){ + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/death" ); + m.addIntArg( (*voices[voiceGrabbed]).id ); + sender.sendMessage( m ); + + printf("sent /death %i \n",(*voices[voiceGrabbed]).id); + (*voices[voiceGrabbed]).posx=(*voices[voiceGrabbed]).posx*0.9+mouseX*0.1; + (*voices[voiceGrabbed]).posy=(*voices[voiceGrabbed]).posy*0.9+mouseY*0.1; + + } else { + //printf("e"); + //On Edge + } + + }else{ + (*voices[voiceGrabbed]).posx=mouseX; + (*voices[voiceGrabbed]).posy=mouseY; + //(*voices[voiceGrabbed]).posx=(*voices[voiceGrabbed]).posx*0.9+mouseX*0.1; + //(*voices[voiceGrabbed]).posy=(*voices[voiceGrabbed]).posy*0.9+mouseY*0.1; + + + } + if (!(*voices[voiceGrabbed]).inTriangle && isInTriangle(mouseX,mouseY)){ + //birth id + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/birth" ); + m.addIntArg( (*voices[voiceGrabbed]).id ); + sender.sendMessage( m ); + + printf("sent /birth %i \n",(*voices[voiceGrabbed]).id); + sendStart=true; + } + + + //(*voices[voiceGrabbed]).inTriangle = isInTriangle(mouseX,mouseY); + + (*voices[voiceGrabbed]).inTriangle = isInTriangle((*voices[voiceGrabbed]).posx,(*voices[voiceGrabbed]).posy); + + if ((*voices[voiceGrabbed]).inTriangle){ + sendStatus(*voices[voiceGrabbed]); + if (sendStart){ + if ((*voices[voiceGrabbed]).isActive){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/start" ); + m.addIntArg( (*voices[voiceGrabbed]).id ); + sender.sendMessage( m ); + printf("sent /start %i \n",(*voices[voiceGrabbed]).id); + } + } + } + + } + }; + for (int i=0; i<numVoices; i++){ + (*voices[i]).draw(); + } + + +} + + +//-------------------------------------------------------------- +void melodyTriangle::keyPressed (int key){ + //printf("key %i",key); + if (key == 'a'){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + (*voices[i]).isActive=!(*voices[i]).isActive; + ///start id + ///stop id + if ((*voices[i]).isActive){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/start" ); + m.addIntArg( (*voices[i]).id ); + sender.sendMessage( m ); + printf("sent /start %i \n",(*voices[i]).id); + }else { + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/stop" ); + m.addIntArg( (*voices[i]).id ); + sender.sendMessage( m ); + printf("sent /stop %i \n",(*voices[i]).id); + + } + + } + } + } + if (key == OF_KEY_UP){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/period" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(1); + m.addIntArg(2); + sender.sendMessage( m ); + printf("sent /period %i %i %i\n",(*voices[i]).id,1,2); + + } + } + } + if (key == OF_KEY_DOWN){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/period" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(2); + m.addIntArg(1); + sender.sendMessage( m ); + printf("sent /period %i %i %i\n",(*voices[i]).id,2,1); + } + } + } + + if (key == '.'){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/period" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(1); + m.addIntArg(3); + sender.sendMessage( m ); + printf("sent /period %i %i %i\n",(*voices[i]).id,1,3); + } + } + + } + if (key == ','){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/period" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(3); + m.addIntArg(1); + sender.sendMessage( m ); + printf("sent /period %i %i %i\n",(*voices[i]).id,3,1); + } + } + + } + if (key == OF_KEY_LEFT){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/shift" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(-1); + m.addIntArg(2); + sender.sendMessage( m ); + printf("sent /shift %i %i %i\n",(*voices[i]).id,-1,2); + + } + } + } + if (key == OF_KEY_RIGHT){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/shift" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg(1); + m.addIntArg(2); + sender.sendMessage( m ); + printf("sent /shift %i %i %i\n",(*voices[i]).id,1,2); + + } + } + } + if (key == '1'){ + + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/tempo" ); + m.addIntArg(30); + sender.sendMessage( m ); + printf("sent /tempo 30\n"); + + } + if (key == '2'){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/tempo" ); + m.addIntArg(60); + sender.sendMessage( m ); + printf("sent /tempo 60\n"); + } + if (key == '3'){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/tempo" ); + m.addIntArg(90); + sender.sendMessage( m ); + printf("sent /tempo 90\n"); + } + if (key == '4'){ + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/tempo" ); + m.addIntArg(120); + sender.sendMessage( m ); + printf("sent /tempo 120\n"); + } + if (key == '+'){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + (*voices[i]).octave++; + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/octave" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg((*voices[i]).octave); + + sender.sendMessage( m ); + printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave); + + } + } + } + + if (key == '-'){ + for (int i=0; i<numVoices; i++){ + if ((*voices[i]).isInVoice(mouseX,mouseY)){ + + (*voices[i]).octave--; + ofxOscMessage m; + ///track id x y left right top bottom area + m.setAddress( "/octave" ); + m.addIntArg( (*voices[i]).id ); + m.addIntArg((*voices[i]).octave); + + sender.sendMessage( m ); + printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave); + + } + } + } + +} + +//-------------------------------------------------------------- +void melodyTriangle::keyReleased (int key){ + +} + +//-------------------------------------------------------------- +void melodyTriangle::mouseMoved(int x, int y ){ + for (int i=0; i<numVoices;i++){ + if ((*voices[i]).isInVoice(x,y)){ + (*voices[i]).highlight=true; + }else { + (*voices[i]).highlight=false; + } + + } + + +} + +//-------------------------------------------------------------- +void melodyTriangle::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void melodyTriangle::mousePressed(int x, int y, int button){ + + for (int i=0; i<numVoices;i++){ + if ((*voices[i]).isInVoice(x,y)){ + voiceGrabbed=i; + //printf("grabbed %i",voiceGrabbed); + }else{ + //printf("didnt grab %i",i); + } + } +} + + + +//-------------------------------------------------------------- +void melodyTriangle::mouseReleased(int x, int y, int button){ + //printf("released %i",voiceGrabbed); + voiceGrabbed=-1; +} + +//-------------------------------------------------------------- +void melodyTriangle::windowResized(int w, int h){ + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/melodyTriangle.h Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,43 @@ + + +#include "ofMain.h" +#include "Voice.h" + +#include "ofxOsc.h" + + + + + +class melodyTriangle : public ofBaseApp{ + + public: + melodyTriangle(const char *host, int port, int numVoices); + + void setup(); + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + int numVoices; + float counter; + bool bSmooth; + //Voice *voices[NUMVOICES]; + Voice *voices[10]; + + int x1,y1,x2,y2,x3,y3;//Triangle Coords + int triangleHeight; + int voiceGrabbed; + void sendStatus(Voice v); + bool isInTriangle(int x, int y); + + + private: + ofxOscSender sender; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testApp.cpp Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,122 @@ +#include "testApp.h" + + +//-------------------------------------------------------------- +void testApp::setup(){ + counter = 0; + ofSetCircleResolution(50); + ofBackground(255,255,255); + bSmooth = false; + ofSetWindowTitle("graphics example"); + + ofSetFrameRate(60); // if vertical sync is off, we can go a bit fast... this caps the framerate at 60fps. +} + +//-------------------------------------------------------------- +void testApp::update(){ + counter = counter + 0.033f; +} + +//-------------------------------------------------------------- +void testApp::draw(){ + + //--------------------------- circles + //let's draw a circle: + ofSetColor(255,130,0); + float radius = 50 + 10 * sin(counter); + ofFill(); // draw "filled shapes" + ofCircle(100,400,radius); + + // now just an outline + ofNoFill(); + ofSetColor(0xCCCCCC); + ofCircle(100,400,80); + + // use the bitMap type + // note, this can be slow on some graphics cards + // because it is using glDrawPixels which varies in + // speed from system to system. try using ofTrueTypeFont + // if this bitMap type slows you down. + ofSetColor(0x000000); + ofDrawBitmapString("circle", 75,500); + + + //--------------------------- rectangles + ofFill(); + for (int i = 0; i < 200; i++){ + ofSetColor((int)ofRandom(0,255),(int)ofRandom(0,255),(int)ofRandom(0,255)); + ofRect(ofRandom(250,350),ofRandom(350,450),ofRandom(10,20),ofRandom(10,20)); + } + ofSetColor(0x000000); + ofDrawBitmapString("rectangles", 275,500); + + //--------------------------- transparency + ofSetColor(0x00FF33); + ofRect(400,350,100,100); + // alpha is usually turned off - for speed puposes. let's turn it on! + ofEnableAlphaBlending(); + ofSetColor(255,0,0,127); // red, 50% transparent + ofRect(450,430,100,33); + ofSetColor(255,0,0,(int)(counter * 10.0f) % 255); // red, variable transparent + ofRect(450,370,100,33); + ofDisableAlphaBlending(); + + ofSetColor(0x000000); + ofDrawBitmapString("transparency", 410,500); + + //--------------------------- lines + // a bunch of red lines, make them smooth if the flag is set + + if (bSmooth){ + ofEnableSmoothing(); + } + + ofSetColor(0xFF0000); + for (int i = 0; i < 20; i++){ + ofLine(600,300 + (i*5),800, 250 + (i*10)); + } + + if (bSmooth){ + ofDisableSmoothing(); + } + + ofSetColor(0x000000); + ofDrawBitmapString("lines\npress 's' to toggle smoothness", 600,500); + +} + + +//-------------------------------------------------------------- +void testApp::keyPressed (int key){ + if (key == 's'){ + bSmooth = !bSmooth; + } +} + +//-------------------------------------------------------------- +void testApp::keyReleased (int key){ + +} + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ +} + + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testApp.h Mon Jan 23 16:46:37 2012 +0000 @@ -0,0 +1,28 @@ +#ifndef _TEST_APP +#define _TEST_APP + + +#include "ofMain.h" + +class testApp : public ofBaseApp{ + + public: + + void setup(); + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + + float counter; + bool bSmooth; +}; + +#endif +