We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there
Building my little demo, I have reached the point where I need to ask the user what he wants to do so here goes my very first steps into building UI interaction into processing
I have a small piece of code that I just wrote and "does the job" but I have the feeling that :
1 I kind of tried to invent the wheel again
2 I'm not going to go very far with my 3 sided wheel
I would very much appreciate your opinions on this
void saveLevel () {
//when saving, first i need to know about synchronizing the 2 levels
//then I need a level number
if (writeOK == false){
if (p5.numInput != 0){
numOK = true;
}
if (p5.gridSynced == true){
syncOK = true;
}
if(numOK == false){
// before that, I need to know if the 2 GRIDs are synchronized
if (syncOK == false){
p5.fill(55, 128);
p5.rect(p5.width/2, p5.height/2, p5.width/2,p5.height/2,17);
p5.fill(255);
p5.text("Grids are not synchronised. Do you want to Sync ? : Y/N", p5.width/2-175, p5.height/2);
p5.askYN = true;
if (p5.yesNo == 1){
level.syncGrids();
//p5.println("sync");
p5.yesNo = 0;
syncOK = true;
p5.askYN = false;
}
else if (p5.yesNo == 2){
//p5.println("save without sync");
p5.yesNo = 0;
syncOK = true;
p5.askYN = false;
}
}
//then ask for the level number
if (syncOK == true){
p5.fill(55, 128);
p5.rect(p5.width/2, p5.height/2, p5.width/2,p5.height/2,17);
p5.fill(255);
p5.text("give the level number:", p5.width/2-55, p5.height/2);
p5.askNum = true;
}
// when I have the level number
} else {
//save to levels.txt
p5.text(p5.numInput, p5.width/2-25, p5.height/2);
writeLevel (p5.numInput);
p5.askNum = false;
}
}
else {
// finally ; display the level number that the user did input
displaySaved(p5.numInput);
}
}
////////////////////////////////////////////////
void writeLevel (int numInput) {}
////////////////////////////////////////////////
// when saving is done, show it is done
void displaySaved(int numInput){
if (doOnce == 0) {
move();
if (isDead() == false){
p5.fill(255, 128,0, msgTrans);
p5.rect(p5.width/2, p5.height/2, p5.width/2,p5.height/2,17);
p5.fill(255);
p5.text("Level SAVED AS: " + p5.numInput, p5.width/2-55, p5.height/2);
}
else{
p5.numInput = 0;
numOK = false;
syncOK = false;
writeOK = false;
msgTrans = 254.0f;
//job's done
p5.goSave = false;
}
}
}
void move () {
alpha += speed;
if (alpha < factor) {
//the formula I choose being: - (x² - factor) + factor²
msgTrans = (-PApplet.sq(alpha)+PApplet.sq(factor)) ;
} else if (alpha > factor) {
alpha = 0.0f;
}
}
boolean isDead() {
if (msgTrans < 0.1) {
doOnce = 1;
return true;
}else {
return false;
}
}
Answers
Maybe you could look into libraries such as ControlP5 or G4P?
Thank you for answering coloured :) From what I've been looking those libraries seems to help at making buttons for their look and interactions... but I am more wondering about the way to structure the code for this simple "question" and further, more sophisticated interface "windows".
For example , in this code, I push the "save level" key and pops up a question: - what number do you want to give to this level ? - but BEFORE that I check if the level is "synchronized" and, if not, want to ask you if you want it to be synchronized - meanwhile I did set-up my question in a way that allows me to end it, at any time before saving, by pushing the escape key - finally, when I get my answers, I want to display that the process has been done successfully
So here it is, a super simple interaction that looks messy to my eyes
what you think ? :)
Well it's a bit hard to comment on your code since you can't run it straight away. Could you post a version that is runnable? It doesn't need to be the whole program.
here you go :)
One comment is that you should definately switch to OOP. Are you familiar with object oriented programming and classes? If not, definately go follow some tutorials.
First of all it's really nicely executed and looks great!
Second, the
int char2int(char myChar)
should just look like this:return (int) myChar-48;
Yes, it's that simple. Look at this link: http://www.asciitable.com/ , it says what char (when converted to an int) corresponds to what number. The numbers start at position 48 so you need to subtract the char with 48 to get the actual number it represents.
Back to OOP. I suggest you make a parent "GuiElement" class and extend it (make a child class) for all the actual elements you need. That way you can still have a large variety of elements but have them work more or less the same. You could make a class for user input, yes/no boxes, feedback etc. so you don't need to do it again and again. They could have an internal state like WAITFORUSERINPUT which only exists within the class so the main program doesn't get clogged with global variables.
wah ! it feels really good to read a nice comment about my job early in the morning :) specially since I would never even think of being good at programming
I'm going to try that (int) char thing again and, about the OOP, I did "un-OOP" my code to share it here because it is spread across various classes already. You can see the various "p5." things in the first bits I pasted in my first post... I did follow a great MOOC on OOP and I kind of "slice" my code into classes yet.( I have still to master the hierarchy principle because I still don't use the parent/ child feature. )
back to my saveLevel () function:
Is it suppose to look like that ?
the 2 first if() feel to me like some patch on bad logic... are they ?
thanks again for the time you take to help (y)
I wouldn't know if it's bad logic... if it works it works, even if there's a better way to do it. As you get more practice when you code, this will improve. Just the other day I wrote something in 50 lines while a year ago I did more or less the same thing in 400 lines... just by using a little recursion trick and ternary operators and all kinds of little tricks I picked up over time.
As for a class scheme maybe something like this?
ok got it I will dig this class scheme thanks again !!