How to get mouseClicked() and others functional in a class?

I searched the intertubes high and low to find an answer, but either the answers where incomplete, obscure or deprecated.. How does the syntax in the class looks like (because this seems to be a bit unusual(?). and how do I get the handler(the state of the mouse) into the class?, it has something to do with register function, but I found no info about this keyword...

thx in advance

I am working the whole day already on this problem and still I cannot get any meaningful results, Its irritating...

Answers

  • edited April 2015

    It is usually done by using Booleans. In your main program create void mouseClicked(), inside that function add something like clicked = true. Now, in your class you can check if (clicked) . You need however to set this Boolean to false manually.

  • That's exactly what I am doing now, but I find the system a bit cumbersome...

  • @Ater: Why are you adding an extra boolean? Why not just use the existing mousePressed variable?

    @Magnetic_Garden: Your question is pretty unclear. Can you post an MCVE showing what you're talking about?

  • edited April 2015

    @KevinWorkman: It is connected with the fact that mousePressed virable is true all the time mouse button is pressed. And this may last couple of frames. Usually, when you look for a click event, that means that you want the action to occur only once after button is pressed. That's where another boolean came from.

    Edit: And yeah, I find this system strange too. But it is only simple method to do it, as there is now mouseClicked virable in Processing. I think the goal may be achieved by creation of own threads, but I don't know yet how it is done. If there is a simplier way, please, advice.

  • I'm not really sure why you're talking about threads- it sounds like the OP is simply asking how to pass data into another class, but we can't really be sure until he posts an MCVE.

  • edited April 2015

    The way I usually do it is to give each class that needs to respond to a mouse click a onClick() function, and then call it from mousePressed():

    class Button{
      //...
      void onClick(){
        if(mouseOver()){ // Can use mouseX and mouseY, on objects' clickable area?
          //...
        }
      }
      //...
    }
    
    //...
    
    void mousePressed(){
      for( button :  buttons ){
        button.onClick();
      }
      //...
    }
    
  • edited April 2015

    Sorry, I didn't realized my question was so vague so here is my MCVE (pseudocode)

    MouseListener mouseListener = new MouseListener(false);
    
    void setup(){
    }
    
    void draw(){
      mouseListener.setMousePressed(mousePressed);
    }
    
    class MouseListener{
    
        boolean mousePressed;
    
        MouseListener( boolean _mousePressed) { mousePressed = _mousePressed;}
    
        public boolean getMousePressed(){
        return mousePressed;
        }
    
        public void setMousePressed(boolean _mousePressed){
        this.mousePressed = _mousePressed;
        println("PRESSED");
        }
        }
    

    problem is how to read the mouse from out of this class, since its outsides the scope...

  • That isn't an MCVE, since it wouldn't compile- is this in its own tab, or is it inside the main sketch tab?

  • edited April 2015

    OKay, I updated the pseudo code to something that actualy functions. the problem is that this class is dependent from draw() for its imput... thats the problem.

  • Uh, don't bother. The variable mousePressed is global, and can be used anywhere, even within your own classes:

    class Shape{
      Shape(){}
      void draw(){
        if(mousePressed){
          fill(255,0,0);
          rect(20,20,40,40);
        } else {
          fill(0,0,255);
          ellipse(40,40,40,40);
        }
      }
    }
    
    Shape shape;
    
    void setup(){
      size(220,220);
      shape = new Shape();
    }
    
    void draw(){
      if(mousePressed){
        background(128,255,0);
      } else {
        background(255,0,255);
      }
      shape.draw();
    }
    
  • Sorry for the late reply, but it does not seem to work this way when you use Intellij, I cannot seem to get it to work in this IDE as it does in Processing itself. Anyway, took Ater's advice and reverted back to the old method by passing a bool.

  • edited May 2015

    When you work inside the Processing IDE what TfGuy44 said is correct as your class is Nested into the PApplet, so you have access to every member of the PApplet class.

    When you work in Intellij or eclipse for example, if your class is not nested (you still can of course) a nice solution is to pass a reference to the PApplet, the context.

    Then you will have access to all the information you might need.

    Here is an example:

        class Drawer {
          private PApplet context;
    
          public Drawer( PApplet P5) { 
            this.context = P5;
          }
    
          public void draw() {
            if (context.mousePressed) {
              context.ellipse(context.mouseX, context.mouseY, 5, 5);
            }
          }
        }
    
        Drawer drawer = new Drawer(this);
    
        void setup() {
        }
    
        void draw() {
          drawer.draw();
        }
    
  • edited May 2015

    Thx! this will be very usefull!! Now I can sanitize my code some more ;)

Sign In or Register to comment.