We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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))
;
}
}
Answers
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, sayinitializeTexts();
insetup()
(or for other functions indraw()
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 insetup()
, don't repeat the type because that would mean to declare them again. Then they would be valid only inside the functionsetup()
(having local scope) and not globally.So at least
cp5
you should declare beforesetup()
and assign a value to insetup()
as I did now. When you want to use e.g.font
later indraw()
you need to declare it on a global level too (and the others likeString textValue
as well).;-)
Thank youuuuuu !!!
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
;-)