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 › toggling controlP5 toggle via keypress, function,
Page Index Toggle Pages: 1
toggling controlP5 toggle via keypress, function, (Read 1353 times)
toggling controlP5 toggle via keypress, function,
May 13th, 2010, 10:27am
 
I'm having trouble with the controlP5 library, with the Toggle class in this instance, but I think I'm also having it with the slider class.

In short:  
  • I can instantiate multiple toggles,
  • and associate them with variables
  • I can also associate a function with the toggle so that the toggle toggling changes states in the sketch but


what I can't figure out is if I have a function that also changes the state programmatically (for example a keypress that activates/deactivates a brush)… 

Is it possible to forward an event to a Toggle?

…And more generally, I feel like I'm hitting weird problems when I try to use other methods from the class, for example:

kp5.addToggle("brush0").linebreak();  // works

but

kp5.controller("brush0").setValue(true);
or
kp5.controller("brush0").toggle()

don't work.  

My assumption is that I have a core misunderstanding of how the dot syntax, objects or constructors work, but can anyone offer any guidance?

Thanks in advance!

(a working sketch with commented out failed approaches is below)
Re: toggling controlP5 toggle via keypress, function,
Reply #1 - May 13th, 2010, 10:27am
 
Quote:
import controlP5.*;

ControlP5 kp5;

int[] memMouse =  new int[4];
int[] nowMouse =  new int[4];
int[] thenMouse =  new int[4];

int little = 5;
int middle = 10;
int big    = 25;

boolean brush0 = false;
boolean brush1 = true;

//Toggle b0;
//Toggle b1;

void setup() {
  size(580, 320);
  background(colorFromPalette());
    stroke(colorFromPalette());
  fill(colorFromPalette());
  frameRate(int(random(2, 20)));
  
  // instantiate controlP5 objects
  kp5 = new ControlP5(this);
  
  //b0 = kp5.controller.addToggle("brush0").linebreak();
  //kp5.controller("brush0").toggle();
  
  kp5.addToggle("brush0").linebreak(); //works
  
  kp5.addToggle("brush1");
  
}

void draw() {
  stroke(colorFromPalette());
  fill(colorFromPalette());
  updateMemMouse();
}

void mouseMoved(){
 brushedSqares(); 
 brushPointist();
}



void keyPressed() {
 if(key=='1') {
    brush0(brush0); // this toggles the brush, but not the state of the Toggle
    
    // other attempts:
    
   // kp5.controller("brush0").toggle(); // gives error:
   //processing.app.debug.RunnerException: The function toggle() does not exist.
   
   //kp5.brush0.toggle();
  // cannot be resolved or is not a fieldd
  
  // brush0.toggle();
  // can not be invoked on primitive type boolean (wasn't a surprise, but I figured I'd try)
  
  
   /*   toggle
        public void toggle()
        switch the state of a toggle.

   */
 } 
}


void brushPointist(){
  if (brush0 == true){
  stroke(colorFromPalette(), int(random(10,60)));
  fill(colorFromPalette(), int(random(20,40)));
  rectMode(CENTER);
  rect(mouseX, mouseY, 10, 10);
  line(mouseX, mouseY, memMouse[0], memMouse[1]);
  smooth();
  }
}

void brushedSqares(){
  if (brush1 == true){
    stroke(colorFromPalette(), random(1, 10));
    fill(colorFromPalette(), int(random(1,24)));
    ellipse(mouseX, mouseY, int(random(16,24)), int(random(16,24)));
    line(mouseX, mouseY, memMouse[0], memMouse[1]);
    smooth();
  }
}

void updateThenMouse() {
  thenMouse[0] = mouseX;
  thenMouse[1] =mouseY;
  thenMouse[2] = pmouseX;
  thenMouse[3] = pmouseY;
}

void updateMemMouse() {
  memMouse[0] = mouseX;
  memMouse[1] =mouseY;
  memMouse[2] = pmouseX;
  memMouse[3] = pmouseY;
}

public void brush0(boolean theValue){
  
  println("### got an event from numberboxC : "+theValue);
  
  if (brush0 == true){
   brush0 = false;
   //processing.app.debug.RunnerException: The function setState(boolean) does not exist.
   //kp5.controller("brush0").setState(false);
  }
  else {
   brush0 = true;
   //kp5.controller("brush0").setState(true);
  }  
}

color colorFromPalette(){
 
   color[] thisColor = new color[5];
 
   int pickThis = int(random(0, 4));
   
  thisColor[0] = color(#A0B6CB);
  thisColor[1] = color(#FDFEEE);
  thisColor[2] = color(#FAEAD0);
  thisColor[3] = color(#FF460D);
  thisColor[4] = color(#00A7E1);  
   
  return thisColor[pickThis];

}

Re: toggling controlP5 toggle via keypress, function,
Reply #2 - May 20th, 2010, 6:14am
 
hi,
kp5.controller("brush0") will return an object of type Controller, not of type Toggle, try the following (casting object  Controller to Toggle)
Code:

((Toggle)kp5.controller("brush0")).toggle();
// same goes for
((Toggle)kp5.controller("brush0")).setState(true);



Code:

kp5.controller("brush0").setValue(true);

does not exist, use:
Code:

kp5.controller("brush0").setValue(1); // 1=true, 0=false

Page Index Toggle Pages: 1