ControlP5

edited October 2013 in Library Questions

Hey everybody,

I'm a recently new user to Processing and I've now been thrown into what I believe is a big project.

I'm supposed to make a interactive quiz where the user answers certain questions that are then either correct or false.

To make these textboxes, I've used the ControlP5 GUI, where I'm kind of stuck with the following code:

import controlP5.*;

ControlP5 cp5;

String textValue = "";

void setup() {
  size(700,700);

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

  cp5 = new ControlP5(this);

  cp5.addTextfield("Your answer")
     .setPosition(200,100)
     .setSize(200,40)
     .setFont(font)
     .setFocus(true)
     ;


  cp5.addBang("Check answer")
     .setPosition(420,100)
     .setSize(80,40)
     .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
     ;    


  textFont(font);
}

void draw() {
  background(0);
  fill(255);
  //text(cp5.get(Textfield.class,"Your answer").getText(), 360,130);
  text(textValue, 360,180);
}

//public void clear() {
  //cp5.get(Textfield.class,"textValue").clear();
//}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isAssignableFrom(Textfield.class)) {
    println("controlEvent: accessing a string from controller '"
            +theEvent.getName()+"': "
            +theEvent.getStringValue()
            );
  }
}


public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
}

What I need an answer on is how I make the button register when the correct answer is typed in. So, the user needs to type the answer into the textbox and thereafter confirm if this is right by clicking on the button.

Tagged:

Answers

  • That's a reason that .addButton() exists.

    addBang() is not a button (at least I don't think it is).

    So instead, you can use addButton() like so:

    answer = cp5.addButton("   Check answer")
        .setPosition(420, 100)
          .setSize(80, 40)
              ;    
    
      if(answer.booleanValue() == true) {
        //Code after the check answer button was pressed
      }
    

    Hope that helps.

    -- MenteCode.

  • edited October 2013

    Oh. And before I forget, you must declare the button at the beginning, along with ControlP5 cp5:

    Button Answer
    

    Otherwise, the button won't work.

Sign In or Register to comment.