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 › Can I bind events to an object
Page Index Toggle Pages: 1
Can I bind events to an object? (Read 822 times)
Can I bind events to an object?
Mar 15th, 2009, 12:55pm
 
Hi, there!

I have a quick question. I wrote an object that displays text within a box. I would like to use this as a button.

If there a simple way to bind the mousedown event to the image that the object created?

The tutorial examples on buttons seems to indicate that I am supposed to search through the objects in the screen to see if the event hit something. Doable, but a bit messy for my taste Smiley
Re: Can I bind events to an object?
Reply #1 - Mar 15th, 2009, 8:28pm
 
Processing is a medium level library: it is higher level than Java2D, and simpler, but not as high level as Swing or JavaFX.
So it offers ways to capture a mouse click event, but doesn't know the concept of nodes: a rectangle, once drawn, is just a bunch of pixels.
You have to provide yourself the abstraction to manipulate rectangles, or other shapes.
Likely, the mouse event must be mapped to the objects you handle, and so you have to check if it happens inside one of these objects yourself.
Which isn't so hard, finding if a point is inside a rectangle is quite simple.
Re: Can I bind events to an object?
Reply #2 - Mar 15th, 2009, 9:29pm
 
Hello. Perhaps this little piece of code will help you on your way. It is my very basic implementation of buttons, that I threw together when I needed a way to interactively toggle the effects for my sketches.
Quote:
abstract class AbstractButton {
  protected boolean mouseOver = false, mouseOut = false, mouseDown = false;
  //AbstractButton() {  }

  abstract public boolean mouseOver();
  
  public boolean mouseDown() {
    if ( (mousePressed) && (mouseButton == LEFT) ) return true;
    else return false;
  }
  
  public void update() {
    if (mouseOut == true) mouseOut = false;
    if (mouseOver()) {
      if (mouseOver == false) onMouseOver();
      mouseOver = true;
    }
    else {
      if (mouseOver == true) mouseOut = true;
      mouseOver = false;
    }
    if (mouseOut) onMouseOut();
    if (mouseDown() && !mouseDown) {
      if ( mouseOver() ) onClick();
      mouseDown = true;
    }
    else if (mouseDown && !mouseDown()){
      mouseDown = false;
    }
    onUpdate();
  }
  
  public void onClick() {  }
  public void onMouseOver() {  }
  public void onMouseOut() {  }
  public void onUpdate() { }
  
  abstract public void render();
};

class SquareButton extends AbstractButton {
  protected PVector pos;
  protected float myWidth,myHeight;

  protected final int fillColorActive = 0xFF00AAFF;
  protected final int fillColorPassive = 0xFF33AAAA;
  protected int fillColor = fillColorPassive;

  SquareButton(float _x,float _y,float _w,float _h) {
    myWidth = _w; myHeight = _h;
    pos = new PVector(_x,_y);
  }
  
  public boolean mouseOver() {
    if ( (mouseX > (pos.x-myWidth/2.0)) && (mouseX < (pos.x+myWidth/2.0)) && (mouseY > (pos.y-myHeight/2.0)) && (mouseY < (pos.y+myHeight/2.0)) ) return true;
    else return false;
  }
  public void onClick() {
    println("onClick fired");
  }
  public void onMouseOver() {
    println("mouseOver fired");
    fillColor = fillColorActive;
  }
  public void onMouseOut() {
    println("mouseOut fired");
    fillColor = fillColorPassive;
  }
  public void render() {
    pushStyle();
      smooth();
      fill(fillColor);
      rectMode(CENTER);
      rect(pos.x,pos.y,myWidth,myHeight);
    popStyle();
  }
};



Then you use it like this:
Quote:
SquareButton b;

void setup() {
  size(640,480,P2D);
  b = new SquareButton(width-80,height-60,120,80);
}

void draw() {
  b.update();
  b.render();
}


Re: Can I bind events to an object?
Reply #3 - Mar 15th, 2009, 9:32pm
 
The forum won't let me extend the post so here goes the rest of it:

Yes, you must call update every frame (so that it checks itself if it is pressed or if the mouse is over it, etc.).

If you want different behaviour on click or on mouse over, you just subclass it or subclass AbstractButton instead. If you subclass AbstractButton, you must provide a function (mouseOver()), that would then determine if the mouse is over the button. Obviously, it will be different for different shapes.

I am not a java expert so forgive me if my code is not well-structured, is bad programming practice or something like that (it still compiles and does its job though). I am just trying to help.
Page Index Toggle Pages: 1