FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   text as objects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: text as objects  (Read 3345 times)
thatbrock


text as objects
« on: Apr 13th, 2005, 11:37am »

Hi there. I am sure there is a better way to do this but just don't know how. I'd like to have some text objects on the screen that change if you mouseOver them. Now there's not a proper proce55ing mouseOver method that I've found, and anyway, how would the text object know its own boundaries?
 
So, I've kludged an ugly hack to make this work. Question is: is  there a better way to do put text on the screen and have it respond to the mouse so I can make it do fancy tricks?
 
Also, if anyone spots "stupid newbie tricks" in the code, please set me straight
 
Any ideas welcomed!
 
// a program to test playing around with text objects
// by Brock Craft
// canned warning! - this code ain't elegant - but it works
 
Blat[] blat;
int num=20;     // number of text objects
float textheight=60; // height we want the text at
float w=0;      // guestimate width of a text element
 
void setup(){
  size (400,400);
  background (51);
  textMode(ALIGN_CENTER);
  BFont f = loadFont("hooge_06_66.vlw");
  textFont(f, textheight);
  blat = new Blat[num];
  for(int i=0; i<num; i++) {
    float x=random(400);
    float y=random(400);
    float h=textheight/3.69; // this magic number is the rough measure of pixels that the text scales to,
     // if loaded at 48 points.
    float w=30;    // a guess at the text's width. we'll worry later about counting the number of  
         // characters in the string to set the width        
    String txt=String (i);   // cast the int to a string and give each object a numerical name
    blat[i] = new Blat(x,y,h,w,txt);
  }
}
 
void loop(){
  background (51);
  for(int i=0; i<num; i++) {
    blat[i].draw();
  }
}
 
class Blat {
 
  float x;
  float y;
  float h;
  float w;
  String txt;
 
  Blat (float xloc, float yloc, float myheight, float mywidth, String text){
 
    x=xloc;
    y=yloc;
    h=myheight;
    w=mywidth;
    txt=text;
 
  }
 
  // here's how the text objects draw themselves
   
  void draw(){
    fill(100,100,255,120);  // fill blueish
    for (int i=0;i<blat.length;i++){  // if mouse is over the text area, fill reddish
 if (mouseover()==true){
   fill(255,50,0,120);
 }
    }
    text (txt,x,y);  //draw it
  }
 
  // here, we test whether the mouse if over the object
  // using myheight and mywidth as the test
   
  boolean mouseover (){
    if (mouseY<y && mouseY>y-(2*h)) {
 if (mouseX>x-(w) && mouseX<x+w){
   return true;
 }
    }
    return false;
  }
}
 
 
fry


WWW
Re: text as objects
« Reply #1 on: Apr 13th, 2005, 9:39pm »

font.width("blah") will return the width of a piece of text, and font.ascent() and font.descent() will give you the heights above and below the baseline. using this, you could probably make a simple class that takes care of a text-based button that would be reusable for other tasks.
 
thatbrock


Re: text as objects
« Reply #2 on: Apr 14th, 2005, 1:45pm »

Thanks, Fry!
 
Are the font.(foo) methods documented?  I couldn't find anything. (or is this in the JavaDoc)  
 
Where would I look for other undocumented method calls that Processing understands?
 
Cheers, and thanks for all the fun,
-Brock
 
fry


WWW
Re: text as objects
« Reply #3 on: Apr 14th, 2005, 2:47pm »

i've been working on actual javadoc for this reason, which will probably get posted sometime near beta.
 
for the next release we've also just made it a function called textWidth() that's part of the regular api and will be documented in the regular reference.
 
thatbrock


Re: text as objects
« Reply #4 on: Apr 17th, 2005, 11:55pm »

Fry,
 
One thing to note, the font.width() method returns a value that does not correspond to the text width in pixels, though it does correspond appropriately to changes in the size of the text.
 
Where does the value that is being returned come from? Should I use this value and simply calculate to get an appropriate pixel value or is there a better way?
 
Thanks!
« Last Edit: Apr 17th, 2005, 11:58pm by thatbrock »  
fry


WWW
Re: text as objects
« Reply #5 on: Apr 18th, 2005, 1:56am »

what's it returning.. or what version are you using? it should just be the width in pixels for that font size.. though this will all be moot in just 2-3 days..
 
Pages: 1 

« Previous topic | Next topic »