Hi! I’m wondering if anyone has suggestions about using an oldschool joystick with Game Control Plus. The difference is that the stick returns only -1.0, 0.0 or 1.0. In this case it would be convenient to have events, but events are only available for buttons.
I can implement it myself by comparing the last value and the current value to notice if there’s been a change, which would mean = an event.
But before doing that I was wondering if someone has done this already. Thanks!
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;
ControlIO control;
ControlDevice stick;
int playerX = 5;
int playerY = 5;
int prevX = 0;
int prevY = 0;
int checkDelay = 250;
long timeToCheck;
int playerColor = 0;
void setup() {
size(400, 400);
control = ControlIO.getInstance(this);
stick = control.getMatchedDevice("LIGD_Joystick");
if (stick == null) {
println("No suitable device configured");
exit();
}
stick.getButton("topBL").plug(this, "onJoystickTopBL", ControlIO.ON_PRESS);
}
void onJoystickTopBL() {
playerColor = color(random(255), random(255), random(255));
}
void changeX(int dx) {
playerX += dx;
}
void changeY(int dy) {
playerY -= dy;
}
void checkInput() {
int x = (int)stick.getSlider("x").getValue();
int y = (int)stick.getSlider("y").getValue();
long now = System.currentTimeMillis();
boolean changed = false;
if (x != 0 && (prevX != x || now > timeToCheck)) {
changeX(x);
changed = true;
}
if (y != 0 && (prevY != y || now > timeToCheck)) {
changeY(y);
changed = true;
}
if (changed) {
timeToCheck = now + checkDelay;
}
prevX = x;
prevY = y;
}
void draw() {
checkInput();
background(100);
fill(playerColor);
ellipse(playerX * 30, playerY * 30, 30, 30);
}
What it solves is to work around the fact that Game Control Plus assumes the joystick provides float values, but my joystick has switches for the directions. With joysticks that provide meaningful float values you want to query their tilt 60 times per second, but with switches you only want discrete events (more like pressing keys). In the program above I call changeX() and changeY() for such discrete events, which I’ll use to control a screen menu.
What makes the program above a tiny bit more complicated is that I also wanted to emulate auto-repeat, like when you leave a key pressed on your keyboard and the same key is triggered multiple times. If auto-repeat is not needed then it looks like this:
Not sure if this will work as you expect but might be worth trying
void checkInput() {
int x = (int)stick.getSlider("x").getValue();
int y = (int)stick.getSlider("y").getValue();
if (x != 0) {
if (prevX != x) {
changeX(x);
prevX = x;
} else {
// auto repeat on X
}
}
if (y != 0) {
if (prevY != y) {
changeY(y);
prevY = y;
} else {
// auto repeat on Y
}
}
}