We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to display/hide text mouseReleased
Page Index Toggle Pages: 1
how to display/hide text mouseReleased (Read 630 times)
how to display/hide text mouseReleased
Jul 14th, 2008, 4:21pm
 
The principle should be so simple, but I just can't seem to figure it out. when mouseReleased show the text, when mouseReleased again, the text should be gone. etc. (on/off). Here's a piece of test code. When mouseReleased it shows the text, but it doesn't remove it. Does anyone know how? Ohw, and not by setting the background(0); again because than also the rest of the app will disappear.

Quote:


int value = 0;

String textVar = ("This is a multiline \nstring with some text");

void setup() {
 size(300, 300);
   noStroke();
   smooth();
}

void draw() {
}

void mouseReleased() {
 if(value == 0) {
   value = 1;
 PFont font = loadFont("Avenir-Book-25.vlw");    
 textFont(font, 16);
 text(textVar, 80, 80);
 }
 else {
   value = 0;
 }
}

Re: how to display/hide text mouseReleased
Reply #1 - Jul 14th, 2008, 9:52pm
 
1. declaring PFont as a global variable and setting it up in setup() would be a good idea.

2. removing the text without clearing the background is simply not possible. at first mouseReleased, the text will be drawn on the screen and remain there until you clear the background.

3. so you would better figure something out to either redraw your scene easily, or print your text on a layer that you would show/hide later.
Re: how to display/hide text mouseReleased
Reply #2 - Jul 15th, 2008, 11:56am
 
As antiplastik said, you have to keep a copy of the background.
Here is a way to do it:

PFont font = loadFont("AmericanTypewriter-24.vlw");    
boolean bDisplay;
PImage pi; // Background image
PImage piBack; // Off-screen graphic buffer
float tPosX = 80.0, tPosY = 80.0;

String textVar = "This is a multiline\nstring with some text\nas filler.";
float tSize = 16, tLead = 18;
float tW, tH, ttH;

void setup() {
 size(300, 300);
 background(128);
 smooth();
 pi = loadImage("../../Globe.png"); // Any graphic will do, this is to show it is hidden only by the real graphics
 image(pi, 0, 0); // Draw background on display

 textFont(font, tSize);
 textLeading(tLead);
 
 // Width of text
 tW = textWidth(textVar);
 // Height of a text line
 tH = textAscent() + textDescent();
 // Total height of the lines of text
 ttH = tLead * 3; // Three lines... Should be computed with number of \n
}

void draw() {
}

void mouseReleased() {
 bDisplay = !bDisplay;
 if (bDisplay) {
   piBack = get(int(tPosX), int(tPosY - tH), int(tW), int(ttH));

//    stroke(255, 0, 0);
//    line(tPosX - 20, tPosY, tPosX + 20, tPosY);
//    line(tPosX, tPosY - 20, tPosX, tPosY + 20);
   noStroke();
   fill(200, 0, 255);
   text(textVar, tPosX, tPosY);
//    fill(0, 0, 255, 20);
//    rect(tPosX, tPosY - tH, tW, ttH);
 } else {
   copy(piBack, 0, 0, int(tW), int(ttH),
       int(tPosX), int(tPosY - tH), int(tW), int(ttH));
 }
}

There is an issue, probably because of rounding errors: when clicking several times, the copy is shifting down one pixel at a time...
Note I made a copy each time the text is displayed, as I supposed the background keeps updating.
Problem: if the background is updated while the text is displayed, the restoration will show an old copy...
Possible solution: do all your drawing off screen, display it on draw() call, adding text or not at this time. Which is roughly equivalent of having two layers.
Page Index Toggle Pages: 1