Advertisement
rbnrpi

StartBarSelector

Jan 23rd, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. import oscP5.*;
  2. import netP5.*;
  3.  
  4. OscP5 oscP5;
  5. NetAddress sonicPi;
  6.  
  7. float mx,my; //will hold mouse position
  8. int bs = 1; //current bar start value
  9. int tr = 0; //play stop control
  10. boolean flag; //controls OSC sending: once per click
  11. PFont f;
  12. int expand = 70;
  13.  
  14. void setup(){
  15.   background(204);
  16.   frameRate(60);
  17.   size(240,120); //x value is 100 + 2*expand
  18.   oscP5 = new OscP5(this, 8000);
  19.   sonicPi = new NetAddress("127.0.0.1",4559);//////INSERT ADDRESS REQUIRED////////
  20.   // add click rectangles
  21.   fill(255); //fill white
  22.   rect(45+expand,15,10,10);//bs+
  23.   rect(45+expand,55,10,10);//bs=1
  24.   rect(45+expand,90,10,10);//bs-
  25.   fill(255,0,0);//fill red
  26.   //stop rect initially semi-hidden
  27.   fill(204);
  28.   rect(10+expand,55,10,10);//stop
  29.   //redraw it with no strke to get effect
  30.   noStroke();
  31.   rect(10+expand,55,10,10);//stop
  32.   stroke(0);
  33.   fill(255+expand,0,0);//fill red
  34.   rect(80+expand,55,10,10);//play
  35.  
  36.   // Create the font
  37.   f = createFont("ArialNarrow-16.vlw", 12);
  38.   textFont(f);
  39.   textAlign(CENTER, CENTER);
  40.  
  41.   //add screen captions
  42.   fill(0);
  43.   text("play",85+expand,40);
  44.   text("bs+",50+expand,4);
  45.   text("bs-",50+expand,105);
  46.   text("bs1",50+expand,72);
  47.   text("1",48+expand,40); //print initial "bs" value
  48. }
  49.  
  50. void sendOscData(int tr,int bs) {
  51.   OscMessage toSend = new OscMessage("/transport");
  52.   toSend.add(tr);
  53.   toSend.add(bs);
  54.   oscP5.send(toSend,sonicPi);
  55.   //println(toSend);//for debugging
  56. }
  57.  
  58. void draw() {
  59.   mx=mouseX;
  60.   my=mouseY;
  61.   if (mousePressed) {
  62.     if ((mx > 44+expand) && (mx <56+expand )&&(my > 89) && (my < 101)){//bs- clicked
  63.       bs-=1;
  64.       if (bs<1){
  65.         bs=1;
  66.       }
  67.       fill(0,255,0); //set green
  68.       rect(45+expand,90,10,10);//bs-
  69.       //println(bs);//for debugging
  70.       }
  71.     if ((mx > 44+expand) && (mx < 56+expand)&&(my >14) && (my<26)) {//bs+ clicked
  72.       bs+=1;
  73.       fill(0,255,0); //green
  74.       rect(45+expand,15,10,10);//bs+
  75.       //println(bs);//for debugging
  76.      }    
  77.     if ((mx >44+expand)&&(mx<56+expand)&&(my>54) && (my<66)){//bs=1 clicked
  78.       bs=1;
  79.       fill(0,255,0); //set green
  80.       rect(45+expand,55,10,10);//bs=1
  81.       //println(bs);//for debugging
  82.     }
  83.  
  84.     if ((mx >79+expand)&&(mx<91+expand)&&(my>54) && (my<66)){//play clicked
  85.       if(flag==false){ //only send OSC once per click
  86.         tr=1;
  87.         sendOscData(tr,bs);
  88.         flag=true;
  89.  
  90.         //switch on stop rectangle in red
  91.         fill(255,0,0);
  92.         rect(10+expand,55,10,10);//stop
  93.         //switch off play rectangle
  94.         fill(204);
  95.         noStroke();
  96.         rect(80+expand,55,10,10);//play
  97.         //clear play text caption
  98.         rect(70+expand,34,28,16);//play text clear rect
  99.         //add stop text caption
  100.         fill(0);
  101.         text("stop",15+expand,40);
  102.         //println(tr); //for debugging
  103.         //println(bs);
  104.       }    
  105.     }
  106.    
  107.     if ((mx >9+expand)&&(mx<21+expand)&&(my>54) && (my<66)) {//stop clicked
  108.       if(flag==false){ //only send OSC once per click
  109.         tr=-1;
  110.         sendOscData(tr,bs);
  111.         flag=true;
  112.         //switch on play rectangle in red
  113.         fill(255,0,0);
  114.         rect(80+expand,55,10,10);//play
  115.         //switch off stop rectangle
  116.         fill(204);
  117.         noStroke();
  118.         rect(10+expand,55,10,10);//stop
  119.         //clear stop text caption
  120.         rect(1+expand,34,28,16);//stop text clear rect
  121.         //add play text caption
  122.         fill(0);
  123.         text("play",85+expand,40);
  124.         //println(tr); //for debugging
  125.       }
  126.     }
  127.  
  128.     fill(204); //update bs number on the screen
  129.     noStroke(); //clear last entry with a background rectangle
  130.     rect(30+expand,32,35,16);
  131.     stroke(0); //change stroke colour to black
  132.     fill(0); //set fill to black
  133.     text(bs,48+expand,40); //print bs value
  134.   }
  135.   else { //mouse now released
  136.     flag=false; //reset flag to allow OSC sending
  137.     //reset "bs" green rectangle
  138.     fill(255); //set white
  139.     rect(45+expand,90,10,10);//bs-
  140.     rect(45+expand,15,10,10);//bs+
  141.     rect(45+expand,55,10,10);//bs=1
  142.   }
  143.  
  144.  
  145.   delay(50); //loop delay
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement