ofEvents problem with buttons in same position, different scene

Hello everyone ,

Using the base of simpleEventsExample example , I created two “scenes” , each with a button in the same position that draws the next or previous scene.

The problem is that when you press the first button that goes to the scene where another button in the same position, it does not respond .

It is because although they are in different classes are drawn independently, they are in the same position .

I hope to be able to express myself well , you know what I mean? any ideas ?.

Thank you very much .

Hi Seldon,

I imagine you have create your own class for button. Every time you add a Listener, you have to remove it when your button isn’t use.

Here is a example of a simple button class.

The steps :

  • Create a class button

  • Create an instance of this class

  • Enable the instance when you need (load scene)

  • Disable when you need (change scene)

    #include “simpleBtn.h”

    void simpleBtn::setup() {

      state = 0;
      onOver = false;
      onPressed = false;
      onOverEffect = true;
    

    }

    void simpleBtn::setObj(eventsObject &e) {
    evObject = &e;
    }

    void simpleBtn::enable() {
    ofAddListener(evObject->newMouseEvent,this, &simpleBtn::mouseMove);
    ofAddListener(evObject->newMouseClic,this, &simpleBtn::mouseClic);
    ofAddListener(evObject->newMouseReleased,this, &simpleBtn::mouseReleased);
    }

    void simpleBtn::disable() {
    ofRemoveListener(evObject->newMouseEvent,this, &simpleBtn::mouseMove);
    ofRemoveListener(evObject->newMouseClic,this, &simpleBtn::mouseClic);
    ofRemoveListener(evObject->newMouseReleased,this, &simpleBtn::mouseReleased);

      state = 0;
      onOver = false;
      onPressed = false;
    

    }

    void simpleBtn::_update() {
    }

    void simpleBtn::_draw() {
    }

    void simpleBtn::mouseMove(ofVec2f & p) {

      int x = p.x;
      int y = p.y;
    
      posY = p.y;
    
      if(hitTest(x, y)) {
      	if(!onOver) {
      		onOver = true;
      	}
    
      } else if(onOver) {
      	onOver = false;
      }
    

    }
    void simpleBtn::mouseClic(ofVec2f & p) {

      int x = p.x;
      int y = p.y;
    
      if(hitTest(x, y)) {
    
              onPressed = true;
              state = 1;
          }
    
      }
    

    }

    void simpleBtn::mouseReleased(ofVec2f & p) {
    onPressed = false;
    }

    bool simpleBtn::hitTest(int tx, int ty) const {
    return ((tx > x) && (tx < x + width) && (ty > y) && (ty < y + height));
    }

Hope it’s usefull :slight_smile:

1 Like

Hello taktik!

thanks for your answer; I think the problem is that I’m not disable the events of the button when switch scenes ; the example that I am leading is more complex than your solution , so I’ll try yours.

Thank you very much again, very useful !

you could bypass the listeners by setting a boolean that would only return something if the button is enabled. e.g.

if(isEnabled){
  return mouse click or whatever
}

That way you wouldn’t have to remove or add the listeners, though the above method is a bit safer