How to properly handle with textBox's in draw() function ?

edited May 2017 in Library Questions

Hi all!

In my project i've managed to create a sketch with two states representing a main screen, and once you click a button it changes to second state (at this point, everything is fine). The problem comes when (in the second state) I try to write a username in textBox that i made using Control5P. The text appear and disappear instantaneously and i know this happens because the function that make the textBox is inside the Draw(). But i couldn't make it work properly. How should i handle this?

Thanks in advance

Part of the code i've got so far.

void draw() {
  switch (state){
    case main_screen:
      show_main_screen();
      break;
    case login_screen:
      show_login();
      break;
  }


}

void  show_main_screen(){
//some code
}

void show_login(){

//some code

String[] Login = {"Username","Password"};
    PFont font = createFont("arial",20);

    textBox= new ControlP5(this);

    int y=20;
    int x = 20;
    int spacing=90;
    for(String s : Login){
      textBox.addTextfield(s)
         .setValue(username)
         .setPosition(x,y)
         .setSize(160,50)
         .setFont(font)
         .setFocus(true)
         .setColor(color(255,0,0))
         .setAutoClear(false);
         ;

        y+=spacing;
    }

   textFont(font);
Tagged:

Answers

  • Any object that you create in draw, it is created 60 times per second. You need to create your object in setup. Check this: https://processing.org/reference/draw_.html

    Check also the examples in the ControlP5's reference as they are always a good way to start: http://www.sojamo.de/libraries/controlP5/

    Kf

  • edited May 2017

    Thanks for the help! I thought about put on setup(), but if i do that, those textBox's will appear in the first state(mainscreen) too, and i only want them to appear on second...

  • You can switch it off and on

    Look at the reference of the library

  • Can you give one example on how can i achieve that? I was looking for something in the library, but i couldn't find anything that helps me!

  • Answer ✓

    You create it the handles of your objects with a global scope. You instantiate them in setup. For controlP5 in your case, you can do textBox.hide() and textBox.show() to toggle its visibility. By the way, textbox is not a good way for a controlP5. Thing of a controlP5 to be a container, and then you associate your controlP5 widgets to this container. Having a proper name for the functionality of your objects adds clarity to your code.

    Related post: https://forum.processing.org/two/discussion/comment/97513/#Comment_97513

    Kf

Sign In or Register to comment.