Android Question Gamepad Support [SOLVED]

wonder

Expert
Licensed User
Longtime User
Last edited:

wonder

Expert
Licensed User
Longtime User
Upvote 0

wonder

Expert
Licensed User
Longtime User
(tl;dr: What's the proper and elegant way to get the X,Y values from the event object?)

Hi Erel!
I've followed your steps the best I could, despite having zero experience with Java / libs.


You can try to create a small library that handles the onGenericMotionEvent event:
http://www.b4x.com/android/forum/th...ss-wrapper-library-gdk-jar.40694/#post-243590
I managed to compile your Java code into a library in order to catch the Generic Motion Event, see the attached file.




As soon as I got the lib imported into B4A, in order to test my controller, I've created the following program:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #Extends: anywheresoftware.b4a.objects.ActivityEx
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Main_Cycle As Timer
 
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private Output As Label
    Dim Global_Key_Code As Int
    Dim GME As String
    Dim X As Float
    Dim Y As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    Output.Left   = 0%x
    Output.Top    = 0%y
    Output.Width  = 100%x
    Output.Height = 100%y
    Main_Cycle.Initialize("Main_Cycle", 1000/60)
    Main_Cycle.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Main_Cycle_Tick
    Output.text = GME & CRLF & CRLF & _
                  "Last KeyCode: " & Global_Key_Code & CRLF & CRLF & _
                  "X: " & Round2(x, 2) & "  Y: " & Round2(y, 2)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 
    Global_Key_Code = KeyCode
    Return True
End Sub

Sub Activity_OnGenericMotionEvent(event As Object)
    GME = event
 
    For i = 0 To (GME.Length - 1)
        If GME.SubString2(i, i + 5) = "x[0]=" Then
            Dim tempX As String
            For j = i + 5 To (GME.Length - 1)
                If GME.CharAt(j) = "," Then
                    Exit
                Else
                    tempX = tempX & GME.CharAt(j)
                End If
            Next
            If tempX <> "" Then Exit
        End If
    Next
    x = tempX

    For i = 0 To (GME.Length - 1)
        If GME.SubString2(i, i + 5) = "y[0]=" Then
            Dim tempY As String
            For j = i + 5 To (GME.Length - 1)
                If GME.CharAt(j) = "," Then
                    Exit
                Else
                    tempY = tempY & GME.CharAt(j)
                End If
            Next
            If tempY <> "" Then Exit
        End If 
    Next
    y = tempY
 
End Sub

Now, as you can see by my rudimentary code, I'm converting the event object into a string and catching the X and Y values of the left analog stick.
My question is, what's the proper and elegant way to get the X,Y values from the event object?

Many thanks in advance!
 

Attachments

  • Generic Motion Event.zip
    5.6 KB · Views: 210
  • Gamepad.zip
    7.5 KB · Views: 203
Upvote 0

wonder

Expert
Licensed User
Longtime User
I've trying the JavaObject method with GetField("x") but at the end, I had no idea what I was doing. I got an error saying there is no such field.


So, if I understood correctly the code would be something like:
B4X:
x = GME.RunMethod("getAxisValue(0)", null)
output.text = x

Am I on the right path?
 
Last edited:
Upvote 0

wonder

Expert
Licensed User
Longtime User
I got it to work, Erel! Thank you so much!!!!!!!!

Solution:
B4X:
x  = GamePad.RuNMethod("getAxisValue", Array(00))
y  = GamePad.RuNMethod("getAxisValue", Array(01))
rx = GamePad.RuNMethod("getAxisValue", Array(12))
ry = GamePad.RuNMethod("getAxisValue", Array(13))
z  = GamePad.RuNMethod("getAxisValue", Array(11))
rz = GamePad.RuNMethod("getAxisValue", Array(14))

These indexes should probably be specific for my game controller.
In order to support all controllers, I'm guessing I should create a search algorithm then:
B4X:
Dim X_Axis_Index = -1 as Int
Dim i = 0 as Int

Do While X_Axis_Index = -1
     If GamePad.RuNMethod("axisToString", Array(i)) = "AXIS_X" Then X_Axis_Index = i
     i = i + 1
Loop

(etc...)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
So, did you get this working, I'm curious!
So the PS3 controller connects via bluetooth to your device?
Can you post the whole code pertaining to the controller?

Thanks,
Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…