Link controlP5 controls to public variables exposed by an object?


#1

I’m enjoying learning about processing and I love the features of ControlP5. I’ve been using ControlP5 controls that link to global variables in my script. Then I tried to make a class to organize my variables a little more. Uh oh… maybe that was a mistake! I thought there would be some way to link a ControlP5 control to a public variable exposed by my class. An imaginary function like “link this control to this other thing.” But I can’t find anything like that. Is there any way to do that? Or am I stuck with a big list of globals at the top of my file if I want to link them to CP5 controls?

Here is an example:

import controlP5.*;
ControlP5 cp5;

boolean myBoolean;
myObject obj;

void setup() {
   size(800,800); 
   cp5 = new ControlP5(this);
   obj = new myObject();
   
   cp5.addToggle("myBoolean");
   cp5.addToggle(obj.myBooleanInsideObject);  // any way to do something like this? 
                                              // (link control to variable inside object)
}

void draw() {
  background(0);
}

public class myObject {
  public boolean myBooleanInsideObject;
  myObject () {
    myBooleanInsideObject = true; 
  } 
}

Thank you so much in advance for your generous help!


#2

Sojamo.de/libraries/controlP5/reference/controlP5/Controller.html#plugTo-java.lang.Object-java.lang.String-


#3

Hooray! Thank you so much! The right answer in 16 minutes!

Here is the code for the next newb who finds this post.

import controlP5.*;
ControlP5 cp5;

boolean myBoolean;
myObject obj;

void setup() {
   size(800,800); 
   cp5 = new ControlP5(this);
   obj = new myObject();
   
   myBoolean = true;
   
   cp5.addToggle("myBoolean");
   cp5.addToggle("myBooleanInsideObject")
      .setValue(obj.myBooleanInsideObject) // without this the toggle is inverted
      .plugTo(obj);  // link control to variable inside object
}

void draw() {
  background(0);
  text("myBoolean is " + myBoolean,100,100);
  text("obj.myBooleanInsideObject is " + obj.myBooleanInsideObject,100,150);
}

public class myObject {
  public boolean myBooleanInsideObject;
  myObject () {
    myBooleanInsideObject = true; 
  } 
}

#4

Crap, I thought I understood how to do this, but I don’t. It seems like the way plugTo is implemented I can’t have multiple instances of the same kind of object because ControlP5 can’t have multiple controls with the same name? e.g., if I want to use PlugTo to link two ControlP5 controls to:

obj1.theValue
obj2.theValue

How do I avoid naming both ControlP5 controls “theValue”? They are not allowed to have duplicate names.

import controlP5.*;
ControlP5 cp5;

boolean myBoolean;
myObject obj1, obj2;

void setup() {
   size(800,800); 
   cp5 = new ControlP5(this);
   obj1 = new myObject();
   obj2 = new myObject();
   
   myBoolean = true;
   
   cp5.addToggle("myBoolean");
   cp5.addToggle("myBooleanInsideObject")
      .setValue(obj1.myBooleanInsideObject) 
      .plugTo(obj1);  
   cp5.addToggle("myBooleanInsideObject")  // doesn't work
      .setValue(obj2.myBooleanInsideObject) 
      .plugTo(obj2); 
}

void draw() {
  background(0);
  text("myBoolean is " + myBoolean,100,100);
  text("obj1.myBooleanInsideObject is " + obj1.myBooleanInsideObject,100,150);
  text("obj2.myBooleanInsideObject is " + obj2.myBooleanInsideObject,100,200);
}

public class myObject {
  public boolean myBooleanInsideObject;
  myObject () {
    myBooleanInsideObject = true; 
  } 
}

I really appreciate any help.


#5

Sojamo.de/libraries/controlP5/reference/controlP5/Controller.html#plugTo-java.lang.Object-java.lang.String-

/**
 * ControlP5 plugTo() (v1.0)
 * GoToLoop (2018/Aug/09)
 *
 * Discourse.Processing.org/t/
 * link-controlp5-controls-to-public-variables-exposed-by-an-object/2510/5
 */

import controlP5.ControlP5;

static final int OBJECTS = 2;
final MyObject[] objs = new MyObject[OBJECTS];

void setup() {
  size(250, 150);

  final ControlP5 cp5 = new ControlP5(this);

  for (int i = 0; i < OBJECTS; ++i) {
    final MyObject obj = objs[i] = new MyObject();
    cp5.addToggle("Bool: " + i).plugTo(obj, "myBoolean");
  }
}

void draw() {
  clear();
  getSurface().setTitle(objs[0].myBoolean + " - " + objs[1].myBoolean);
}

class MyObject {
  boolean myBoolean;
}

#6

Thanks so much! I understand now. I appreciate the help.

I’m sorry this is turning into something of a saga, but I have one other question about this. I expected plugTo would link the controller to a variable such that if I change the controller the variable changes, and vice versa. But it is only one-way, it changes the variable but does not reflect the variable’s state when it is drawn.

It looks like there used to be a controller.setAutoUpdate() method that would change this behavior and also have the control reflect the current state of the variable, but it is on the deprecated list. Does anyone know if this functionality exists elsewhere? I thought it might be related to setUpdate() / isUpdate but I can’t get that to do this.

Note: I realize I can use setValue() every time I change a variable programatically in order to update the linked controller, I’m just looking for an easy way around having to write that every time. I thought setAutoUpdate() was the answer, but nope.

Any ideas?