Loading...
Logo
Processing Forum
I'd like to move all my GUI code to another class in order free up some space in the main class (the one that extends PApplet.) My hope was that I could create static variables in the GUIManager class and access them from all the additional asset Classes that need data from sliders (including the main class)
It's not working and I'm not sure how to remedy this.

Here's an abstraction of what i'm trying to do:

Copy code
  1. public class StripeGUIManager{


    private ControlP5 control;

    private ControlWindow window;

    private Controller<Toggle> bgColor;

    private Controller<Toggle> rSpeeds;

    private Controller<Knob> blurKnob;

    private Controller<Knob> speedKnob;


    public static float scalerAmt;

    public static float blurAmt;

    public static boolean blackOrWhite;

    public static boolean randomSpeeds;


    public StripeGUIManager(PApplet p) {

          control = new ControlP5(p);


          // Init window

          window = control

                .addControlWindow("Stripe_Control_Window", 100, 100, 200, 200)

                .hideCoordinates().setBackground(p.color(90));

          // Speed Scaler Knob

          speedKnob = control.addKnob("scalerAmt").setRange(0.2f, 3.5f)

                .setValue(1).setPosition(10, 100).setRadius(30)

                .setColorForeground(p.color(255))

                .setColorBackground(p.color(200, 160, 100))

                .setColorActive(p.color(255, 60, 60))

                .setDragDirection(Knob.HORIZONTAL);

          // Init blur knob

          blurKnob = control.addKnob("blurAmt").setRange(1, 100).setValue(5)

                .setPosition(100, 100).setRadius(30)

                .setColorForeground(p.color(255))

                .setColorBackground(p.color(200, 160, 100))

                .setColorActive(p.color(255, 60, 60))

                .setDragDirection(Knob.HORIZONTAL);

          // Init toggle switch

          bgColor = control.addToggle("blackOrWhite").setPosition(10, 40)

                .setSize(50, 20).setValue(true).setMode(ControlP5.SWITCH);


          rSpeeds = control.addToggle("randomSpeeds").setPosition(80, 40)

                .setSize(50, 20).setMode(ControlP5.SWITCH);


          // Set controls to window object

          ((Toggle) bgColor).moveTo(window);

          ((Knob) blurKnob).moveTo(window);

          ((Knob) speedKnob).moveTo(window);

          ((Toggle) rSpeeds).moveTo(window);

    }

    public void ControlEvent(ControlEvent theEvent) {

          if (theEvent.isController()) {

                System.out.println("Something is happening.");

                if (theEvent.getName().matches("blurAmt")) {

                blurAmt = theEvent.getValue();

          }

    }}} //Sorry about the formatting... It's a forum thing..


In the main class, I'd like to use these variables to affect various attributes, like so:

Copy code
  1. //Imports, Namespace, etc.

  2. public class TestClass extends PApplet{
  3.       public void setup(){
  4.             //blah blah
  5.       }
  6.       public void draw(){
  7.             //let's say...
  8.             background(StripeGUIManager.scalerAmt);
  9.       }

So this is clearly not enough, but I can't find any resources or tutorials that don't use ControlP5 in the class that immediately extends PApplet. Does the solution involve adding Listeners?
Thanks!
m

Replies(2)

public void ControlEvent(ControlEvent theEvent) {

I doubt controlP5 expects to find a function with an initial uppercase letter...
Beside, I think it searches functions by reflection in the main PApplet class, so it might not find them in other classes.
Usually, the workaround is to have the functions in the PApplet, forwarding the calls to the classes expecting them.
Ah I solved it, Thanks.
The capital C was a typo in this post. Basically the only thing that needs to be done is that the relevant variables need to be declared in the main class. The external class finds them perfectly, even without including a ControlEvent function.

Thanks PhiLho
mk