listen to mouseEvents? (registerMethod)

Hi. I'm using p5 with ES6 modules. Basically, I created a global var named p5 to make lib accessible in my classes. Now, I want to be able to listen to mouseEvents. And I'm aware that there's a registerMethod function that should work like this:

p5.registerMethod("mouseEvent", this);

However, it just doesn't work. I'm not receiving any acknowledge in my mouseEvent method.

I've read the documentation here and here, but I'm not quite understanding if this is registering a new something that will be emitted or if this is a listener (observer pattern). Because in p5 the doc says by example:

p5.prototype.registerMethod('remove', p5.prototype.doRemoveStuff);

... and prototype is for creating a new method (registerMethod in this case) and not to use one. Am i wrong? Anyway, tried also this and neither worked:

p5.__proto__.registerMethod("mouseEvent", this);

So, I just want to listen to events from a custom class. What am I missing? Any clue?

My code is:

import * as P5 from 'p5' // used to access library constants

export class ControlZone extends Panel {

    constructor(x, y, width, height) {
        super(x, y, width, height);     

        p5.registerMethod("mouseEvent", this);
    }

    mouseEvent(event) {

    const x = event.getX();
    const y = event.getY();

      console.log("mouse Event");

      switch (event.getAction()) {
        case P5.MouseEvent.PRESS:
          // do something for the mouse being pressed
        this.mousePressed();
          break;
        case P5.MouseEvent.RELEASE:
          // do something for mouse released
        this.mouseReleased();
          break;
        case P5.MouseEvent.CLICK:
          // do something for mouse clicked
          break;
        case P5.MouseEvent.DRAG:
          // do something for mouse dragged
        this.mouseDragged();
          break;
        case P5.MouseEvent.MOVE:
          // do something for mouse moved
          break;
      }

         mousePressed () {
       console.log("mousePressed Event");
     }

    mouseDragged () {
        console.log("mouseDragged Event");
     }

    mouseReleased () {
           console.log("mouseReleased Event");
    }
}

Answers

Sign In or Register to comment.