Is there a way to grab the scroll wheel (on the mouse, both, when I scroll down and when I scroll up) in Slick2d? I looked in input but didn’t see it.
Thanks for that, but that’s not way I’m doing. What I found, with the help of your, is MouseListener, MouseWheelEvent and MouseWheelListener which implements mouseWheelMoved(MouseWheelEvent e), but I’m not sure how to get this change value, just like mouseWheelEvent(int change) has.
However, the problem with MouseListener is that I don’t need all of his implementation methods expect only mouseWheelMoved, so is there any other way of doing this so that I can just call mouseWheelMoved method as annonymous class, inside my actionPerformed method, or somethingi?
public void performAction(MouseListener ml, GameContainer gc, int mx, int my) throws SlickException {
Input input = gc.getInput();
ml = new MouseListener() {
@Override
public void mouseWheelMoved(int change) {
if (change < 0) {
xpos -= 10;
}
if (change > 0) {
xpos += 10;
}
}
@Override
public void mouseClicked(int button, int x, int y, int clickCount) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mousePressed(int button, int x, int y) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(int button, int x, int y) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setInput(Input input) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isAcceptingInput() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void inputEnded() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void inputStarted() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
float minusBoxMinX = this.x;
float minusBoxMinY = this.y;
float minusBoxMaxX = this.x + this.height;
float minusBoxMaxY = this.y + this.height;
if (contains(mx, my, minusBoxMinX, minusBoxMaxX, minusBoxMinY, minusBoxMaxY)) {
if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
if (this.currentPercentage > 0) {
this.currentPercentage--;
this.currentValue = (this.currentPercentage / 100.0f) * this.maxValue;
}
}
}
float plusBoxMinX = this.x + this.width - this.height;
float plusBoxMinY = this.y;
float plusBoxMaxX = this.x + this.width;
float plusBoxMaxY = this.y + this.height;
if (contains(mx, my, plusBoxMinX, plusBoxMaxX, plusBoxMinY, plusBoxMaxY)) {
if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
if (this.currentPercentage < 100) {
this.currentPercentage++;
this.currentValue = (this.currentPercentage / 100.0f) * this.maxValue;
}
}
}
}
In short, MouseListener is an interface so you have to implement all of the methods. If you’re worried about making your code more compact, create an abstract base class that implements all of the required methods as empty functions with the exception of isAcceptingInput() which requires a boolean return value. I just return true for this method though throwing an exception in lieu of an empty body or default return value should work as well.
public class ProxyMouseListener implements MouseListener {
@Override
public void mouseWheelMoved(int change) {}
@Override
public void mouseClicked(int button, int x, int y, int clickCount) {}
@Override
public void mousePressed(int button, int x, int y) {}
@Override
public void mouseReleased(int button, int x, int y) {}
@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {}
@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {}
@Override
public void setInput(Input input) {}
@Override
public boolean isAcceptingInput() {
return true;
}
@Override
public void inputEnded() {}
@Override
public void inputStarted() {}
}
When you want to capture a mouse event (such as mouseWheelMoved(int)), add it like so:
ml = new ProxyMouseListener() {
@Override
public void mouseWheelMoved(int change) {
if (change < 0) {
xpos -= 10;
}
if (change > 0) {
xpos += 10;
}
}
};
If all you ever care about is the mouse wheel event then you could make the base class abstract and leave the mouseWheelMoved function unimplemented. I use pretty much the same code as above when dealing with Slick’s input events. The only real difference is that my class handles all of the input events though so it’s not tied to mouse based input only.