We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I used g4p and the g4p builder tool to create a simple checkbox application for a setup screen. i can get it to run, but how do you exit the setup screen to go to another screen such as play?
Answers
could you post your code here?
you could probably do it with a switch/ case statement and an event
Normally use a variable
int state=0;
in
draw()
useswitch(state) {
to distinguish between the states. Nothing is allowed in draw outside theswitch()
statement.Now with
state=1;
you move on to the play screenThanks. But I really need to be more specific in what I am looking for. I understand using states, however, I was looking for code statements. For example; what is the syntax for making the checkbox1 control invisible/visible, and do I need to declare a function for this to be used? The G4P builder tool doesn't provide much help here.
Look at the g4p reference please it’s all there
Summoning @quark
As @Chrisir points out the first place to look is the online-reference.
If you look at the reference for GCheckbox you will see this
If you scroll down you will see the methods available to this control. Also the diagram at the top shows the inheritance tree for this control. So
GCheckbox
inherits fromGToggleControl
GToggleControl
inherits fromGTextIconBase
GTextIconBase
inherits fromGTextBase
GTextBase
inherits fromGAbstractControl
The public methods from these classes also work for
GCheckbox
so if you click onGAbstractControl
and scroll down you will find a method calledsetVisible(boolean)
which is used for all G4P controls. I suggest you explore the reference pages to get an idea of what is available.Thank you Chrisir and quark. Chrisir, your comment about nothing being in draw() except the switch(0 statements was very critical. And quark, for the explanation of using the reference was very helpful.
I did get my very simple code working, and have posted it here.
I don't fully understand why the part of no other code in draw(0 except the switch statements is critical. I initially tried to make the switchbox1 control invisible with a statement that tested for the state set up by the switchbox1 control clicking. It did not work!
`import g4p_controls.*;
GCheckbox checkbox1;
int state; void setup(){ size(400,400); state=0; fill(0); background(200);
createGUI();
}
void draw(){
switch(state){ case 0:
background(200); checkbox1.setVisible(true); text("Settings state", 50,50);
}
}
println("checkbox1 - GCheckbox >> GEvent." + event + " @ " + millis());
{ state=1; }
{ G4P.messagesEnabled(false); G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME); G4P.setCursor(ARROW); surface.setTitle("Sketch Window"); checkbox1 = new GCheckbox(this, 166, 150, 120, 20); checkbox1.setIconAlign(GAlign.LEFT, GAlign.MIDDLE); checkbox1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE); checkbox1.setText("BEND"); checkbox1.setOpaque(false); checkbox1.addEventHandler(this, "checkbox1_clicked1"); }
I am sorry that my code posting looks so messy. My original code was full of delineated comments, and this sort of messed things up.
Nothing in draw outside the switch is just my rule of thumb. Of course you can have whatever you want.
Having nothing outside switch means that nothing is flowing around not belonging to a state. It just makes a clear program. Same switch in mousePressed and keyPressed by the way because the inputs mean different things in the different states.
But as said not obligatory.
To format code: empty lines before and after the code; select code with mouse; hit ctrl-o OR use the C in the small command bar.
Chrisir
I didn’t mean to be critical or harsh in my comment, sorry for that!
Here is an example of using G4P with states. For clarity I have used G4P with GUI Builder but combined the 2 tabs into one removing some of the comments. Simply copy and paste the who;e code into the main tab.
Again, thank you both for all of the help that you have given me on this topic. The example was a good one of putting the switch method in a procedure, rather than the draw(0) method, which, with your help, worked for me also.
I am not the first one who had problems with this particular issue, and I am sure that i won't be the last one. quark, would you consider putting this example in G4P the next time you revise it? I think that it would be very helpful.
That's not quite correct.
First of all
switch()
is a general command and only a shorter form ofYou can have many
switch()
in your program.2nd: quark is using switch() in changeGUI() but only to change the gui not to show the different screens for setup and play. Instead of this, quark is just saying
text(state, width/2 - 10, height/2 + 10);
. So you still would need a switch() in draw().Example without GUI: just press any key:
Ok..got it Thanks
;-)