ControlP5 addTextField

edited January 2015 in Library Questions

Hi,

My code below almost works. I have a text entry field and want the option to either enter the number manually or use keys 'm' & 'n' to scroll up or down through the numbers. The scrolling works and updates the text field, but when I try entering a number the text field only accepts the first number and not a String of numbers.

Any ideas?

import controlP5.*;
ControlP5 cp5;
int x = 0;

void setup() {
  size(400,200);

  PFont font = createFont("arial",10);

  cp5 = new ControlP5(this);

  cp5.addTextfield("Path")
     .setPosition(100,100)
     .setSize(100,15)
     .setFont(font)
     .setColor(255)
     .setAutoClear(false)
     .setText(Integer.toString(x))
     .setValue(0)
     ;

  textFont(font);
}

void draw() {
  background(0);
  fill(255);
}  

void keyPressed() {
  if (key=='q' && x != 0) x--;
  if (key=='a' && x != 199) x++;
  cp5.get(Textfield.class,"Path").setText(Integer.toString(x));   
}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isAssignableFrom(Textfield.class)) x = Integer.parseInt(cp5.get(Textfield.class,"Path").getText());
}

Answers

  • Answer ✓
  • edited January 2015

    Thanks GoToLoop,

    With your advice I got a little further. The looping was a silly error and you're right about the verbosity - this comes from looking at Java forums for solutions.

    The code I have now works and it's good enough for my purposes, but for the setInputFilter() I just couldn't workout what I should write in order to implement it as I'm not that used to the Java documentation. When the focus is on the text field and I use 'q' & 'a' to navigate the text appears in the box, but since the 'displayPath' doesn't pick up the characters it doesn't cause an error. It's just a bit ugly.

    import controlP5.*;
    ControlP5 cp5;
    int displayPath = 0;
    
    void setup() {
      size(400,200);
    
      PFont font = createFont("arial",10);
    
      cp5 = new ControlP5(this);
    
      cp5.addTextfield("Path")
         .setPosition(100,100)
         .setSize(100,15)
         .setFont(font)
         .setColor(255)
         .setAutoClear(false)
         .setText(str(displayPath))
         .setValue(0)
         ;
    
      textFont(font);
    }
    
    void draw() {
      background(0);
      fill(255);
      println(displayPath);
    }  
    
    void keyPressed() {
      if (key=='q'||key=='a'){
        if (key=='q' && displayPath != 0) displayPath--;
        if (key=='a' && displayPath != 199) displayPath++;
        cp5.get(Textfield.class,"Path").setText(str(displayPath));
      }   
    }
    
    void controlEvent(ControlEvent theEvent) {
      if(theEvent.isAssignableFrom(Textfield.class)) displayPath = int(cp5.get(Textfield.class,"Path").getText());
    }
    
  • edited January 2015 Answer ✓

    ... but for the setInputFilter() I just couldn't work out what I should write in order to implement it...

    Sorry about that 1! Seems like Textfield.InputFilter.INTEGER is for internal use. b-(
    Nevertheless, found out that ControlP5.INTEGER works instead! *-:)
    Now things got easier as the Textfield only accepts number characters now! \m/

    // forum.processing.org/two/discussion/9055/controlp5-addtextfield
    
    import controlP5.ControlP5;
    import controlP5.Textfield;
    Textfield txt;
    
    static final int MIN = 0, MAX = 200, FPS = 30, SMOOTH = 4;
    
    void setup() {
      size(200, 100, JAVA2D);
      noLoop();
      frameRate(FPS);
      smooth(SMOOTH);
    
      txt = new ControlP5(this)
               .addTextfield("Path")
               .setPosition(width>>2, height>>1)
               .setSize(width>>3, 20)
               .setColor(-1)
               .setText(MIN + "")
               .setFocus(true)
               .setAutoClear(false)
               .setFont(createFont("DejaVu Sans", 12, true))
               .setInputFilter(ControlP5.INTEGER);
    }
    
    void draw() {
      background(0);
    }
    
    void keyTyped() {
      int k = key|040, val = int(txt.getText());
      val = constrain(val + int(k == 'a') - int(k == 'q'), MIN, MAX);
    
      print(txt.setText("" + val).getText() + " ");
      redraw();
    }
    
  • hi Setup, hi gotoloop, Can you help solve my problem ??

    I want to send the value of the text field to set the pace a servo, but I do not know how to send these values, and I marked with a question mark on the sketch because I do not know. please help me, help you all would be greatly appreciated. The following is a sketch of processing.

        import controlP5.*;
        import processing.serial.*;
    
        ControlP5 cp5a;
    
        Serial myPort;
    
        void setup()
        {
          size(300, 115);
    
         println(Serial.list());
    
         myPort = new Serial(this, Serial.list()[0], 9600);
    
          cp5a = new ControlP5(this);
    
         cp5a.addTextfield("input_value")
             .setPosition(30,30)
             .setSize(128,35)
             .setAutoClear(false);
    
         cp5a.addBang("clear")
             .setPosition(168,30)
             .setSize(80,35)
             .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER);
    
        }
    
        void draw()
        {
          background(128);
    
        }
    
        public void clear() {
            cp5a.get(Textfield.class,"input_value").clear();
    
        }
    
        void controlEvent(ControlEvent theEvent) {
            if(theEvent.isAssignableFrom(Textfield.class))
            if(theEvent.isController())
            { myPort.write ??????
    

    This is arduino sketch to receive the value to be sent from the processing

        #include <Stepper.h>
    
        int in1Pin = 12;
    
        int in2Pin = 11;
    
        int in3Pin = 10;
    
        int in4Pin = 9;
    
        Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);
    
        void setup() {
    
        pinMode(in1Pin, OUTPUT);
    
        pinMode(in2Pin, OUTPUT);
    
        pinMode(in3Pin, OUTPUT);
    
        pinMode(in4Pin, OUTPUT);
    
        while (!Serial);
    
        Serial.begin(9600);
    
        motor.setSpeed(20); }
    
        void loop() {
    
        if (Serial.available()) {
    
        int steps = Serial.parseInt();
    
        motor.step(steps);
    
          }
    
        }
    

    please help me to finish this project, thanks.

Sign In or Register to comment.