Skip to content

Commit

Permalink
Upgrade to Jamepad 1.3.2, fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
MrStahlfelge committed Feb 3, 2020
1 parent 54cb461 commit fb1cb18
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ project(":desktop-jamepad") {
compile project(":core-advcontrollers")
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
compile "com.github.WilliamAHartman:Jamepad:1.2"
}
compile "com.github.WilliamAHartman:Jamepad:1.3.2" }
}

project(":gwt-controllers") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.Logger;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.TimeUtils;
import com.studiohartman.jamepad.ControllerAxis;
import com.studiohartman.jamepad.ControllerButton;
import com.studiohartman.jamepad.ControllerIndex;
Expand Down Expand Up @@ -36,7 +36,7 @@ public class JamepadController implements AdvancedController {
private final IntMap<Float> axisState = new IntMap<>();
private final String uuid;
private boolean connected = true;
private CancelVibrationTask cancelVibrationTask;
private long vibrationEndMs;

public JamepadController(ControllerIndex controllerIndex) {
this.controllerIndex = controllerIndex;
Expand Down Expand Up @@ -199,28 +199,22 @@ public boolean canVibrate() {

@Override
public boolean isVibrating() {
return controllerIndex.isVibrating();
return canVibrate() && TimeUtils.millis() < vibrationEndMs;
}

@Override
public void startVibration(int duration, float strength) {
try {
if (cancelVibrationTask == null)
cancelVibrationTask = new CancelVibrationTask();

controllerIndex.startVibration(strength, strength);
Timer.schedule(cancelVibrationTask, (float) duration / 1000);
controllerIndex.doVibration(strength, strength, duration);
vibrationEndMs = TimeUtils.millis() + duration;
} catch (ControllerUnpluggedException e) {
// do nothing
}
}

@Override
public void cancelVibration() {
if (cancelVibrationTask != null && cancelVibrationTask.isScheduled()) {
cancelVibrationTask.cancel();
cancelVibrationTask.run();
}

}

@Override
Expand Down Expand Up @@ -273,12 +267,4 @@ public boolean isConnected() {
public ControllerMapping getMapping() {
return JamepadMapping.getInstance();
}

private class CancelVibrationTask extends Timer.Task {
@Override
public void run() {
if (controllerIndex.isVibrating())
controllerIndex.stopVibration();
}
}
}

0 comments on commit fb1cb18

Please sign in to comment.