Skip to content

Commit

Permalink
Get android controllers working
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Sep 5, 2024
1 parent a143e4a commit 6ef76c1
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 111 deletions.
33 changes: 24 additions & 9 deletions project/src/android/AndroidFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,33 @@ class AndroidStage : public Stage
{
//__android_log_print(ANDROID_LOG_INFO, "NME", "OnJoy %d %d %d", inDeviceId, inCode, inDown);
Event joystick( inDown ? etJoyButtonDown : etJoyButtonUp );
joystick.id = inDeviceId;
joystick.value = inDeviceId;
joystick.code = inCode;
HandleEvent(joystick);
}

void OnJoyMotion(int inDeviceId, int inAxis, float inValue)
void OnJoyMotion(int inDeviceId, int inAxis, bool isGamepad, float inSx, float inSy)
{
Event joystick(etJoyAxisMove);
joystick.id = inDeviceId;
joystick.code = inAxis;
joystick.value = inValue;
HandleEvent(joystick);
if (inAxis<6)
{
Event joystick(etJoyAxisMove);
joystick.value = inDeviceId;
joystick.code = inAxis;
joystick.scaleX = inSx;
joystick.scaleY = inSy;
joystick.y = isGamepad;
HandleEvent(joystick);
}
else // DPad/hat
{
Event joystick(etJoyHatMove);
joystick.value = inDeviceId;
joystick.code = 0;
joystick.scaleX = inSx;
joystick.scaleY = inSy;
joystick.y = true;
HandleEvent(joystick);
}
}

void OnTrackball(double inX, double inY)
Expand Down Expand Up @@ -911,11 +926,11 @@ JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onJoyChange(JNIEnv * env, jobject
return nme::GetResult();
}

JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onJoyMotion(JNIEnv * env, jobject obj, int deviceId, int axis, float value)
JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onJoyMotion(JNIEnv * env, jobject obj, int deviceId, int axis, bool isGame, float inSx, float inSy)
{
AutoHaxe haxe("onJoyMotion");
if (nme::sStage)
nme::sStage->OnJoyMotion(deviceId,axis,value);
nme::sStage->OnJoyMotion(deviceId, axis, isGame, inSx, inSy);
return nme::GetResult();
}

Expand Down
60 changes: 11 additions & 49 deletions src/nme/display/Stage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -800,44 +800,17 @@ class Stage extends DisplayObjectContainer implements nme.app.IPollClient implem
}


private inline function axismap( code:Int )
{
#if openfl_legacy
switch(code)
{
case 3: code = 4;
case 2: code = 3;
case 4: code = 2;
}
#end
return code;
}
private inline function buttonmap( code:Int )
{
#if openfl_legacy
switch(code)
{
case 9: code = GamepadButton.LEFT_SHOULDER;
case 4: code = GamepadButton.BACK;
case 8: code = GamepadButton.RIGHT_STICK;
case 10: code = GamepadButton.RIGHT_SHOULDER;
case 6: code = GamepadButton.START;
case 7: code = GamepadButton.LEFT_STICK;
}
#end
return code;
}

public function onJoystick(inEvent:AppEvent, inType:String):Void
{
var data:Array<Float> = null;
// user = deviceId
var user = inEvent.value;
var isGamePad:Bool = inEvent.y>0;
if(inEvent.flags > 0)
{
///is axis move event
if(inEvent.flags==1)
{
{
if(nmeJoyAxisData[user]==null)
nmeJoyAxisData[user] = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];

Expand All @@ -846,37 +819,26 @@ class Stage extends DisplayObjectContainer implements nme.app.IPollClient implem
data[ inEvent.code+1 ] = inEvent.sy;
}
else if(inEvent.flags==3)
{
{
//isGamePad
if(nmeJoyAxisData[user]==null)
nmeJoyAxisData[user] = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];

data = nmeJoyAxisData[user];
data[ axismap(inEvent.code) ] = inEvent.sx;
data[ axismap(inEvent.code+1) ] = inEvent.sy;
data[ inEvent.code ] = inEvent.sx;
data[ inEvent.code+1 ] = inEvent.sy;
}
else if(inEvent.flags==2)
{
if(nmeJoyAxisData[user]!=null)
for(d in nmeJoyAxisData[user])
d = 0.0;
for(d in 0...nmeJoyAxisData[user].length)
nmeJoyAxisData[user][d] = 0.0;
}
}
#if openfl_legacy
if(isGamePad && StringTools.startsWith(inType,"button"))
{
//map sdl controller to legacy xinput
var evt:JoystickEvent = new JoystickEvent(inType, false, false, inEvent.id, buttonmap(inEvent.code),
inEvent.value, inEvent.sx, inEvent.sy, data, isGamePad);
nmeDispatchEvent(evt);
}
else
#end
{
var evt:JoystickEvent = new JoystickEvent(inType, false, false, inEvent.id, inEvent.code,
inEvent.value, inEvent.sx, inEvent.sy, data, isGamePad);
nmeDispatchEvent(evt);
}

var evt:JoystickEvent = new JoystickEvent(inType, false, false, inEvent.id, inEvent.code,
inEvent.value, inEvent.sx, inEvent.sy, data, isGamePad);
nmeDispatchEvent(evt);

if(GameInput.hasInstances())
{
Expand Down
26 changes: 26 additions & 0 deletions src/nme/ui/GameInput.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class GameInput extends EventDispatcher
static var nmeInstances = [];
static var nmeKeyboardDevices = new Array<KeyboardInputDevice>();
static var added = false;
#if android
static var getDeviceName:Dynamic;
#end


public function new()
Expand All @@ -27,14 +30,21 @@ class GameInput extends EventDispatcher
#if android
if (!added)
{
getDeviceName = JNI.createStaticMethod(
"org/haxe/nme/GameActivity",
"getDeviceName",
"(I)Ljava/lang/String;" );

var setInputManagerCallback = JNI.createStaticMethod(
"org/haxe/nme/GameActivity",
"setInputManagerCallback",
"(Lorg/haxe/nme/HaxeObject;)[I" );
added = true;

var existing:Array<Int> = setInputManagerCallback(this);
for(device in existing)
nme.app.Application.runOnMainThread( () -> nmeGamepadConnect(device) );
consumeGamepadButtons(true);
}
#end

Expand All @@ -49,6 +59,18 @@ class GameInput extends EventDispatcher
//}
}

// Consuming the 'B' button will prevent it from generating a "Back" event
public static function consumeGamepadButtons(consume:Bool)
{
#if android
var setConsumeGamepadButtons = JNI.createStaticMethod(
"org/haxe/nme/GameActivity",
"setConsumeGamepadButtons",
"(Z)V" );
setConsumeGamepadButtons(consume);
#end
}

#if android
// Called from android handler
@keep function onInputDeviceAdded(device:Int)
Expand Down Expand Up @@ -148,8 +170,12 @@ class GameInput extends EventDispatcher

static function getGamepadName(index:Int) : String
{
#if android
return getDeviceName(index);
#else
// TODO
return "name"+index;
#end
}

private static function __getDevice(index:Int):GameInputDevice
Expand Down
6 changes: 3 additions & 3 deletions src/nme/ui/GameInputDevice.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GameInputDevice
nmeControls.push(control);
}

for (i in 0...15)
for (i in 0...GamepadButton.COUNT)
{
control = new GameInputControl(this, "BUTTON_" + i, 0, 1);
nmeButton.set(i, control);
Expand All @@ -50,15 +50,15 @@ class GameInputDevice

public function toString() return 'GameInputDevice($id:$name)';

public function getButtonAt(i:Int) return i>=0 && i<15 ? nmeControls[i+6] : null;
public function getButtonAt(i:Int) return i>=0 && i<GamepadButton.COUNT ? nmeControls[i+6] : null;

public function getAxisAt(i:Int) return i>=0 && i<6 ? nmeControls[i] : null;

public function getCachedSamples(data:ByteArray, append:Bool = false):Int return 0;

public function isButtonDown(buttonId:Int)
{
if (buttonId<0 || buttonId>=15)
if (buttonId<0 || buttonId>=GamepadButton.COUNT)
return false;
return nmeControls[buttonId+6].value>0;
}
Expand Down
21 changes: 11 additions & 10 deletions src/nme/ui/GamepadButton.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ class GamepadButton
public static inline var B:Int = 1;
public static inline var X:Int = 2;
public static inline var Y:Int = 3;
#if openfl_legacy
public static inline var LEFT_SHOULDER:Int = 4;
public static inline var RIGHT_SHOULDER:Int = 5;
public static inline var BACK:Int = 6;
public static inline var START:Int = 7;
public static inline var LEFT_STICK:Int = 8;
public static inline var RIGHT_STICK:Int = 9;
public static inline var GUIDE:Int = 10;
#else

public static inline var BACK:Int = 4;
public static inline var GUIDE:Int = 5;
public static inline var START:Int = 6;
public static inline var LEFT_STICK:Int = 7;
public static inline var RIGHT_STICK:Int = 8;
public static inline var LEFT_SHOULDER:Int = 9;
public static inline var RIGHT_SHOULDER:Int = 10;
#end

public static inline var DPAD_UP:Int = 11;
public static inline var DPAD_DOWN:Int = 12;
public static inline var DPAD_LEFT:Int = 13;
public static inline var DPAD_RIGHT:Int = 14;

public static inline var LEFT_SHOULDER2:Int = 15;
public static inline var RIGHT_SHOULDER2:Int = 16;
public static inline var SELECT:Int = 17;

public static inline var COUNT:Int = 18;

public static function toString(id:Int) : String
{
switch(id)
Expand All @@ -49,6 +47,9 @@ class GamepadButton
case GamepadButton.DPAD_DOWN: return "GamepadButton.DPAD_DOWN";
case GamepadButton.DPAD_LEFT: return "GamepadButton.DPAD_LEFT";
case GamepadButton.DPAD_RIGHT: return "GamepadButton.DPAD_RIGHT";
case GamepadButton.LEFT_SHOULDER2: return "GamepadButton.LEFT_SHOULDER2";
case GamepadButton.RIGHT_SHOULDER2: return "GamepadButton.RIGHT_SHOULDER2";
case GamepadButton.SELECT: return "GamepadButton.SELECT";
default: return "BUTTON UNKNOWN[id:"+id+"]";
}
}
Expand Down
1 change: 1 addition & 0 deletions templates/android/extension-api/src/org/haxe/nme/NME.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class NME {
public static native int onContextLost();
public static native int onTrackball(float x,float y);
public static native int onJoyChange(int inDeviceID, int inCode, boolean inIsDown);
public static native int onJoyMotion(int inDeviceID, int inAxis, boolean isGamepad, float inX, float inY);
public static native int onKeyChange(int inKeyCode, int inCharCode, boolean inIsDown, boolean isChar);
public static native int onText(String inNewText, int inReplacePos, int inReplaceLength);
public static native int onTextSelect( int inReplacePos, int inReplaceLength);
Expand Down
Loading

0 comments on commit 6ef76c1

Please sign in to comment.