[ControlP5] How to set ToggleSwitch vertical?? (+ check SwitchMode?)

edited September 2014 in Library Questions

hello,
1) how can i change the orientation from a ToggleSwitch?
now it is horizontal. But i want it vertical.

2) how can i check if the toggle has SWITCH-Mode or DEFAULT-Mode? (see line 20)

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(400, 400);
  smooth();
  cp5 = new ControlP5(this);

  // create a toggle
  cp5.addToggle("toggleValue")
    .setPosition(40, 100)
    .setSize(20, 50)
      .setMode(ControlP5.SWITCH);
}


void draw() {
  background(0);
  println(cp5.getController("toggleValue").getMode());   //doesn't work
}

Answers

  • edited September 2014

    nobody an idea?

    i have this:
    Toggle1
    and i want this:
    Toggle2
    (retouched with Potoshop :P)

  • is there any method or path to do this?

  • edited September 2014

    Hi, you can override the ControllerView for your Toggle (also see the ControlP5customView example that comes with controlP5), I have attached an example below and added comments accordingly:

    import controlP5.*;
    
    ControlP5 cp5;
    
    // this example uses ControlP5 version 2.0.4
    
    void setup() {
      size(400, 400);
      smooth();
      cp5 = new ControlP5(this);
    
      // create a toggle
      cp5.addToggle("toggleValue")
         .setPosition(40, 100)
         .setSize(20, 50)
         // use the setView method to override the default ControllerView for a Toggle
         // with controlP5 version 2.2.2 and later use 
         // display(PGraphics theGraphics , Toggle c )
         .setView(new ControllerView<Toggle>() {
           public void display(PApplet theGraphics , Toggle c ) {
             CColor col = c.getColor();
             // draw the toggle's background 
             theGraphics.fill(col.getBackground());
             theGraphics.rect(0,0,c.getWidth(),c.getHeight());
             // draw the toggle's foreground with highlighted area pointing 
             // up or down depending on the state of the toggle 
             theGraphics.fill(col.getForeground());
             int h =c.getHeight()/2;
             // getState() returns true or false
             theGraphics.rect(0,c.getState() ? h:0,c.getWidth(),h);
             // draw the caption-label
             c.getCaptionLabel().draw(theGraphics, 0,0,c);
           }
         })
         ;
    }
    
    
    void draw() {
      background(0);
      // with ControlP5 2.2.2 and earlier versions there is no such method for a Toggle,
      // but will be added with future versions.
      // println(cp5.getController("toggleValue").getMode());   //doesn't work
    }
    
Sign In or Register to comment.