We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there again, this time I need a little help: I've create a code for a "generative" logo (it's at the start so is not complete), and I want that while pressing one time the BACKSPACE I can go back just for one shape. Now like I have my code, when I press Backspace it delete all.
I leave the code here, thanks !
import controlP5.*;
ControlP5 cp5;
String textValue = "";
String val;
void setup() {
size(700,800);
PFont font = createFont("arial",20);
cp5 = new ControlP5(this);
cp5.addTextfield("INPUT")
.setPosition(width/2-100,600)
.setSize(200,40)
.setFont(font)
.setFocus(true)
.setColor(color(255,255,255))
;
textFont(font);
background(0);
noStroke();
}
void draw() {
if (keyPressed) {
if (key == 'o' || key == 'O') {
fill(205, 152, 59, 100);
ellipse(width/2, height/2, 50, 50);
}
if (key == 'b' || key == 'B') {
fill(20, 84, 42, 100);
rectMode(CENTER);
rect(width/2, height/2, 50, 50);
}
}
if (key == BACKSPACE) { //This reset all, I want to reset just the last one shape
background (0);
}
val = cp5.get(Textfield.class,"INPUT").getText();
println(val.length());
}
Answers
you'd have to store all pixels in an array at the point you wanted to remember it, and then recall them when you press backspace. Is it worth it, when you could just clear the screen with background(0) & redo all the steps except the last?
Well my intent is to create a generative brand like this http://ebologna.it/ , so you think is wrong to put the background(0) ?
Speaking of code, what I have to digit for do what you say?
I get it now - nice looking site. So basically you need an undo. Background(0) clears the screen so that's no good. Never tried this before but I assume if you read each pixel in a nested for loop: for (x=0; x >= width; x ++) {for (y = 0; y>= height; y++); // read value of each pixel into an array; you could save the current screen - but I bet there's a function to do this already & hopefully someone will point us to it.
The pixels() function looks good for this - save the current screen into an array, then loadPixels() to recover it.
So in my code what I have to write? My study is really basic and I never see he pixels function!
Ps: I don't have a int variable, just like you can see in my code, there are only if with the key to be pressed!
Hope someone can answer too! Thanks a lot fuzzySi
Can't test this for you until this evening, but looks like it should be:
loadPixels(); // this should load current image into pixels array Use arrayCopy to copy current pixels[] into a new array eg undoBuffer[] updatePixels();
Your undo function should reverse this ie swap the pixels in the undoBuffer into the pixels array. Only pseudocode so far but that should be a start.
Some useful stuff here, but not the exact code you need https://processing.org/tutorials/pixels/
How about this - functions to backup current display and restore it. I was getting bounce with your keypressed code so I've changed it to keyRelease instead.
Mh but with this code I can just go back only one? Because I want it that I can delete, just one shape, but all the time I push the key BACKSPACE. I think this really work, but do you think can it be like I was saying?
Notice this, I have to create all the alphabet so is right to write if and else if for the key?
(Sorry for bother you a lot!)
I've try too to put, but it work the same as yours :
So you want multiple undos? Ah. Yes, it's possible using the same technique, but to get each array in the right order you'd have to have a series of arrays, and dump the current pixels into the correct array, so a bit more tricky. If you want unlimited undos, you'd need to copy most recent array into previous one etc to keep them in order. You should be able to adapt the code above. Yes can have lots of if/ else statements so OK to put the whole alphabet into that function.
Oh gosh this will be really difficult for me!
well, instead of saving the whole screen I suggest bringing the stuff into an ArrayList and display the content of the ArrayList - and let backspace work on that...
in this version, the data in the textfield is not totally connected to the ArrayList (you can type when the inputbox has no focus and it still works but never mind)