This site requires JavaScript, please enable it in your browser!
Greenfoot back
CubeGamer6
CubeGamer6 wrote ...

2017/12/17

Workaround for double mouse input

CubeGamer6 CubeGamer6

2017/12/17

#
I've got a code that sets click, press and release booleans for every mouse button. However, while testing i noticed when pressing buttons simultaniously, the MouseInfo.getButton() only returned one of the two pressed buttons. This meant that the other button's pressing boolean was stuck at true. Any idea on how to create a workaround for this? Here's the code i'm using:
import greenfoot.*;
import java.awt.Point;

public class Base extends Actor
{
    public static boolean update=false;
    public static float UPDATE_FREQ=30;
    private static long lastUpdate=0;
    
    public static Point mousePos;
    public static boolean[] click;
    public static boolean[] pressing;
    public static boolean[] clicked;
    
    public Base()
    {
        mousePos=new Point();
        click=new boolean[5];
        pressing=new boolean[5];
        clicked=new boolean[5];
    }
    
    public void act()
    {
        update();
        if(update) //this resets the boolean arrays
        {
            click=new boolean[5];
            clicked=new boolean[5];
        }
        basicMouseInfo();
    }
    
    public void update() //this lets subclasses work at a slower rate
    {
        long currentTime=System.currentTimeMillis();
        long nextUpdate=(long)(lastUpdate+1/UPDATE_FREQ*1000);
        if(nextUpdate<=currentTime)
        {
            lastUpdate=nextUpdate;
            update=true;
            return;
        }
        update=false;
    }
    
    public void setUpdateFreq(int freq) { if(freq>0 && freq<=120) UPDATE_FREQ=freq; }
    
    public void basicMouseInfo() //this monitors the mouse position and status of mouse buttons
    {
        MouseInfo mouse=Greenfoot.getMouseInfo();
        if(mouse!=null)
        {
            mousePos=new Point(mouse.getX(), mouse.getY());
            int button=mouse.getButton();
            if(button!=0)
            {
                if(pressing[button-1]) //if the button was already pressed (this is when the button is released)
                {
                    pressing[button-1]=false;
                    clicked[button-1]=true;
                    return;
                }
                if(!click[button-1]) //if the button wasn't clicked before (this is when the button is first pressed)
                {
                    click[button-1]=true;
                    pressing[button-1]=true;
                }
            }
        }
    }
}
danpost danpost

2017/12/18

#
If the state of more than one button is changed on any one act cycle, you will only get mouse info on one of them. You can increase the speed of the scenario to make that less likely to happen. Also, dragging the mouse will cause a return of a button; so it is not a clear press and release with a return of a button value. You not only need to check which button was detected, but you also need to ask which mouse action was performed (pressing, dragging or releasing) -- you cannot just assume that the first is pressing and the next will be releasing.
CubeGamer6 CubeGamer6

2017/12/18

#
danpost wrote...
You not only need to check which button was detected, but you also need to ask which mouse action was performed (pressing, dragging or releasing) -- you cannot just assume that the first is pressing and the next will be releasing.
I see. So that means i have to use the Greenfoot.mousePressed(), Greenfoot.mouseClicked(), etc to determine what happened. Thanks for the reply!
You need to login to post a reply.