Structured programming with Tabs

edited June 2015 in Library Questions

Hi!

I'm creating a sketch with some controls from the ControlP5 Library... I'm trying to separate this new content in another TAB but I don't know how extract the setup code.

void setup() {

  ControlP5 cp5;
  String textValue = "";
  Textarea myTextarea;

  PFont font = createFont("arial",20);  
  cp5 = new ControlP5(this);
  cp5.getTooltip().setDelay(500);

  cp5.addTextfield("Entrada")
   .setPosition( width-(width-10), height-60 )
   .setSize(width-20,40)
   .setFont(font)
   .setFocus(true)
   .setColor(color(225,225,225))
   ;  

  size(800,600);    
}

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

How can I extract the code from de ControlP5 and put it in another tab? Like this...

/* SketchTAB */

void setup() {      
       initializeTexts;  
       size(800,600);       
}   

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


/* Text TAB */

initializeTexts {

ControlP5 cp5;
      String textValue = "";
      Textarea myTextarea;

      PFont font = createFont("arial",20);  
      cp5 = new ControlP5(this);
      cp5.getTooltip().setDelay(500);

      cp5.addTextfield("Entrada")
       .setPosition( width-(width-10), height-60 )
       .setSize(width-20,40)
       .setFont(font)
       .setFocus(true)
       .setColor(color(225,225,225))
       ;  


    }

}
Tagged:

Answers

  • edited June 2015 Answer ✓

    Hello,

    the tabs in processing are only for the human. They make it very easy to find passages of your code.

    The text is put together for processing itself, internally, so processing joins all tabs to one text.

    Therefore tabs are very easy to use, just split your code text up.

    Functions

    So your problem is more how do I write a function like initializeTexts().

    Functions can occur also in the main tab. You don't need to use tabs to use functions. Functions are very good to modularize your code. Later you'll come to classes to do this (see objects / OOP in the tutorial).

    So this function void initializeTexts() { } says :

    • I am a function called initializeTexts. When you want to use it, say initializeTexts(); in setup() (or for other functions in draw() or anywhere).

    • I get no parameters to work with: The round brackets ( ) are empty. You can write functions that receive parameters.

    • What I do is between the curly brackets { }.

    • And I return nothing: void. Other function can return int or float or a class or array etc.

    Variables

    Also please note that variables that are valid everywhere (have global scope) must be declared before setup(). When you assign the declared variable (with global scope) then in setup(), don't repeat the type because that would mean to declare them again. Then they would be valid only inside the function setup() (having local scope) and not globally.

    So at least cp5 you should declare before setup() and assign a value to in setup() as I did now. When you want to use e.g. font later in draw() you need to declare it on a global level too (and the others like String textValue as well).

    ;-)

    import controlP5.*;
    
    ControlP5 cp5;   // global scope 
    
    void setup() {
      size(800, 600);    
      initializeTexts();
    }
    
    void draw() {
      background(0);  
      fill(255);
    }
    
    // next tab (but you can have it in the same tab too) -------------------
    
    void initializeTexts() {
    
      String textValue = "";
      Textarea myTextarea;
    
      PFont font = createFont("arial", 20);  
    
      cp5 = new ControlP5(this);
      cp5.getTooltip().setDelay(500);
    
      cp5.addTextfield("Entrada")
        .setPosition( width-(width-10), height-60 )
          .setSize(width-20, 40)
            .setFont(font)
              .setFocus(true)
                .setColor(color(225, 225, 225)) ;
    }
    //
    
  • Thank youuuuuu !!!

  • edited June 2015

    I have some standard name for my tabs that I use often :

    • Main Program

    • InputMouse (all mouse functions)

    • InputKeyBoard (all keyboard functions)

    • Classes (see Objects / OOP)

    • States (which is another concept you don't need yet)

    • Tools (which collects more basic functions)

    other tabs when needed of course, sometimes one tab per class

    ;-)

Sign In or Register to comment.