We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › ControlP5 and loop/noloop
Page Index Toggle Pages: 1
ControlP5 and loop/noloop (Read 967 times)
ControlP5 and loop/noloop
Jan 30th, 2008, 5:30am
 
Hi

I am using P5 in a sketch where I load a tsv file of stocks / mutual funds with price # shares etc; parse the data, and then
a)create a pie graph from values of price * shares, etc - simple stuff.
My actual question is this - when I use tabs / bangs/ buttons (any P5 element), they are inactive and freeze if I use noLoop() before drawing the graphics. On the other hand, if I don't use noLoop, the pie graph doesn't work and the data values are recalculated endlessly. Obviously, I'm missing something...
(also, I wanted to set the color of the buttons to match the appropriate pie graph wedges, but can't get that to work either)
------------------------------------------------
//Code is :
import controlP5.*;
import processing.opengl.*;

ControlP5 controlP5;
Record[] records;
int recordCount;
PFont body;
int startingEntry = 0;
int num;    
String alldata = "";
Textarea myTextarea;
int total;
int networth = 0;
int dailych = 0;
String summary_list;
int diameter = 150;
float lastAng = 0;


void setup() {
 size(600,600,OPENGL);
 background(100, 25);
 frameRate(30);
 controlP5 = new ControlP5(this);
 myTextarea = controlP5.addTextarea("Report", "Data Shown Here",100,100,200,60);
 myTextarea.setColorForeground(0xffff0000);
 controlP5.addSlider("changeWidth",0,200,100,100,80,100,9);
 controlP5.addSlider("changeHeight",0,200,100,100,60,100,9);

 String[] mystocks = loadStrings("mystocks.tsv");//reads mystocks data
 num = mystocks.length;
 records = new Record[mystocks.length]; //sets the size of the array by num rows in the mystocks.tsv
 for (int i = 1; i < mystocks.length; i++) { //skips the first row of headers
   String[] pieces = split(mystocks[i], '\t'); // Load mystocks data into array, based on the tab delimiter
   records[recordCount] = new Record(pieces);
   recordCount++;
 }
 for(int i=0;i<mystocks.length-1;i++) {
   networth += int(records[i].holdings);
   dailych += int((records[i].dollar_daily_change));
   summary_list = "Today's net worth is:  $  "+ nfc(networth) + "\n\n" + "Today's change was:  $  " + nfc(dailych);
   myTextarea.setText(summary_list);
 }

}

void draw() {
 pushMatrix();
 drawpie();
 popMatrix();
}

void changeWidth(int theValue) {
 myTextarea.setWidth(theValue);
}

void changeHeight(int theValue) {
 myTextarea.setHeight(theValue);
}

void controlEvent(ControlEvent theEvent) {
 println(theEvent.controller().id());
}


void button(float theValue) {
 println("a button event. "+theValue);
}

void drawpie(){
    noLoop();
 for (int i = 0; i < num-1; i++) {
   fill(random(255 -5*i), random(255 -5*i), random(255 -5*i));
   arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(records[i].holdings/networth*360));
   lastAng += radians(records[i].holdings/networth*360);
   controlP5.addButton(records[i].symb,30,0, 30+i*15,50,20).setId(i);
//this.setColorValue(color(random(255 -5*i), random(255 -5*i), random(255 -5*i)));
 }
}

//--------------------------------------------------------
The class code is :

class Record {
String symb;
String name;
String asset_class;
float basis;
float num_shares;
float price;
float holdings;
float per_daily_change;
float dollar_daily_change;
float asset_per_holdings;
float per_overall_change;
float dollar_overall_change;
float pe_ratio;
String sector;

public Record(String[] pieces) { //This is used for the order of columns in the tsv file (last 2 are from yahoo)
symb = pieces[0];
name = pieces[1];
asset_class = pieces[2];
basis = float(pieces[3]);
num_shares = float(pieces[4]);
price = float(pieces[5]);
holdings = float(pieces[6]);
per_daily_change = float(pieces[7]);
dollar_daily_change = float(pieces[8]);
asset_per_holdings = float(pieces[9]);
per_overall_change = float(pieces[10]);
dollar_overall_change = float(pieces[11]);
pe_ratio = float(pieces[12]);
}
}


Thanks in advance
Re: ControlP5 and loop/noloop
Reply #1 - Jan 30th, 2008, 9:43am
 
Check out ControlP5's setAutoDraw() methods to disable automatic drawing. Then call ControlP5's draw method to directly control when you want to re-draw the controllers.
Re: ControlP5 and loop/noloop
Reply #2 - Jan 31st, 2008, 5:00am
 
Hi

Thanks for the reply. I'm still not getting it, although I did multiple attempts to use the setAutoDraw() and Draw() methods. I'm very new to Processing - perhaps the overall structure / location of the controls in the sketch is the problem?

I wrote a simple pie graph program with no external data which  illustrates my problem - this can simply be copied and run to see what I'm trying to do

Thanks again

CLS

import controlP5.*;
ControlP5 controlP5;
PFont body;
Textarea myTextarea;
int diameter = 150;
float lastAng = 0;
String[] new_comment = new String[10];
int[] mydata = {30,60,90,40,80,60};

void setup() {
 size(600,600);
 background(100);
 frameRate(30);
 controlP5 = new ControlP5(this);
 myTextarea = controlP5.addTextarea("Report", "Data Shown Here",100,100,200,60);
 myTextarea.setColorForeground(0xffff0000);
 controlP5.addSlider("changeWidth",0,200,100,100,80,100,9);
 controlP5.addSlider("changeHeight",0,200,100,100,60,100,9);
 controlP5.setAutoDraw(false);
 for(int i=0;i<10;i++) {
 new_comment[i] = "Comment number:  " + i;
 }
}

void draw() {
 drawpie();
}

void button(float theValue) {
 println("a button event. "+theValue);
}

void drawpie(){
 noLoop();
 for (int i = 0; i < 6; i++) {
   fill(random(255 -5*i), random(255 -5*i), random(255 -5*i));
   arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(mydata[i]));
   lastAng += radians(mydata[i]);
   controlP5.draw();
   controlP5.addButton("buttton" + i,30,0, 30+i*15,50,20).setId(i);
 }
}
Re: ControlP5 and loop/noloop
Reply #3 - Jan 31st, 2008, 6:32am
 
not sure what you try to do, but what happens in your code is: by using noLoop() you stop calling draw() hence you stop calling drawpie() hence you stop calling controlP5.draw(), therefore you dont see any updates neither in drawing the pie (i assume intended) nor in drawing controlP5 (probably not intended). if you want to update controlP5 continuously, noLoop() will not allow you to do so, you would need to restructure your code in a way so you can abandon the noLoop() call. another option would be to use a controlWindow (see the examples), but you would still face the non-update issue in your main window.

Page Index Toggle Pages: 1