Skip to content

Commit

Permalink
iOS Controllers: implemented isVibrating and cancelVibration, linking…
Browse files Browse the repository at this point in the history
… GameController framework instead of GameKit
  • Loading branch information
MrStahlfelge committed Jan 26, 2021
1 parent 5893a3f commit 602edd1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The recommended way to use gdx-controllers is via dependency management with Gra
*project-root/build.gradle:*

ext {
gdxControllersVersion = '2.0.0'
gdxControllersVersion = '2.0.1' // see badges above for latest versions
}

Add the following dependencies:
Expand Down Expand Up @@ -57,9 +57,9 @@ implementation "com.badlogicgames.gdx-controllers:gdx-controllers-ios:$gdxContro
```
<pattern>com.badlogic.gdx.controllers.IosControllerManager</pattern>
....
<framework>GameKit</framework>
<framework>GameController</framework>
```
If you forget to explicitly link GameKit framework, no game controller will show up.
If you forget to explicitly link the framework, no game controller will show up.

#### html:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.robovm.apple.corehaptic.CHHapticEventType;
import org.robovm.apple.corehaptic.CHHapticParameterCurve;
import org.robovm.apple.corehaptic.CHHapticPattern;
import org.robovm.apple.corehaptic.CHHapticPatternPlayer;
import org.robovm.apple.foundation.Foundation;
import org.robovm.apple.foundation.NSArray;
import org.robovm.apple.foundation.NSErrorException;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class IosController extends AbstractController {
private long lastPausePressedMs = 0;

private CHHapticEngine hapticEngine;
private CHHapticPatternPlayer playingHapticPattern;
private long vibrationEndMs;

public IosController(GCController controller) {
this.controller = controller;
Expand Down Expand Up @@ -379,13 +382,29 @@ public void startVibration(int duration, float strength) {
if (canVibrate()) {
try {
hapticEngine.start(null);
hapticEngine.createPlayer(constructRumbleEvent((float) duration / 1000, strength)).start(0, null);
playingHapticPattern = hapticEngine.createPlayer(constructRumbleEvent((float) duration / 1000, strength));
playingHapticPattern.start(0, null);
vibrationEndMs = TimeUtils.millis() + duration;
} catch (Throwable t) {
Gdx.app.error("Controllers", "Vibration failed", t);
}
}
}

@Override
public boolean isVibrating() {
return canVibrate() && TimeUtils.millis() < vibrationEndMs && playingHapticPattern != null;
}

@Override
public void cancelVibration() {
if (isVibrating()) {
playingHapticPattern.cancelAndReturnError(null);
playingHapticPattern = null;
vibrationEndMs = 0;
}
}

@Override
public ControllerMapping getMapping() {
return MfiMapping.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ public void changed(ChangeEvent event, Actor actor) {
@Override
public void changed(ChangeEvent event, Actor actor) {
if (selectedController != null) {
selectedController.startVibration(1000, 1f);
if (!selectedController.isVibrating()) {
selectedController.startVibration(1000, 1f);
} else {
selectedController.cancelVibration();
}
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/ios/robovm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<framework>OpenGLES</framework>
<framework>QuartzCore</framework>
<framework>CoreGraphics</framework>
<framework>GameKit</framework>
<framework>GameController</framework>
<framework>OpenAL</framework>
<framework>AudioToolbox</framework>
<framework>AVFoundation</framework>
Expand Down

0 comments on commit 602edd1

Please sign in to comment.