Change the number of tick marks on ControlP5 Slider

edited July 2016 in Library Questions

I'm wanting to change the number of tick marks on a Slider in the CP5 library after the Slider has been created.

Anyone have an idea on how to do this? Example below would change the number when the space key is pressed.

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(300, 125);
  noStroke();
  cp5 = new ControlP5(this);

  cp5.addSlider("slider")
    .setPosition(50, 40)
    .setSize(200, 20)
    .setRange(0, 200)
    .setNumberOfTickMarks(10)
    ;
}

void draw() {
  background(0);
}

void keyPressed() {
  if (key==' ') {
    if (cp5.getController("slider") !=null) {
      // change the number of ticks here
    }
  }
}

Answers

  • Answer ✓
    slider chi = (slider)cp5.getController("slider");
    if (chi !=null) {
      chi.setNumberOfTickMarks(3);
    }
    

    I hope this helps,

    Kf

  • That does it. Thank you!

    import controlP5.*;
    
    ControlP5 cp5;
    
    void setup() {
      size(300, 125);
      noStroke();
      cp5 = new ControlP5(this);
    
      cp5.addSlider("slider")
        .setPosition(50, 40)
        .setSize(200, 20)
        .setRange(0, 200)
        .setNumberOfTickMarks(10)
        ;
    }
    
    void draw() {
      background(0);
    }
    
    void keyPressed() {
      if (key==' ') {
        if (cp5.getController("slider") !=null) {
          // change the number of ticks here
          Slider chi = (Slider)cp5.getController("slider");
          chi.setNumberOfTickMarks(3);
        }
      }
    }
    
Sign In or Register to comment.