Virtual and custom gamepads

更新时间:
复制 MD 格式

The SDK provides a built-in virtual gamepad to help you quickly debug games.

Configure and enable the built-in virtual gamepad

1. Gamepad library dependencies

1.1 Add the joystick-uikit package to your project dependencies.

1.2 Add the AAR package dependency to the build.gradle file.

implementation fileTree(dir: 'libs', include the following: ['*.jar'])
implementation files('libs/joystick-uikit.aar')

2. Initialize the gamepad library

Call the `GamePad.defaultInit()` method before the game starts. We recommend that you call this method after the application starts.

Note: The default gamepad is added to the `containerView` that is passed to the SDK. Use the following method to pass the `containerView`:

ACGGamePaasService.getInstance().start(this, containerView);

3. Set the enableCustomInputEvent parameter when you start the game

CGGamePrepareObj prepareObj = new CGGamePrepareObj();
prepareObj.token = "user_token";
prepareObj.userId = "user_id";
prepareObj.mixGameId = "id_of_the_game_to_start";
prepareObj.enableCustomInputEvent = false; // Note: This parameter is optional. The default value is false.

Custom virtual gamepad

1. Add a custom view to act as a handle.

Add a custom gamepad view to the layout file of the game page, as shown in the following example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    android:layout_width="match_parent"   
    android:layout_height="match_parent">   
    <LinearLayout        
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            />
    </LinearLayout>
</RelativeLayout>

2. Pass events to the SDK

Call the `ACGGamePaasService.getInstance().customGamepadEvent(eventObj)` method to pass custom events to the SDK, as shown in the following example:

button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction() || MotionEvent.ACTION_UP == event.getAction()) {
            CGCustomGamepadEventObj gameEvent = new CGCustomGamepadEventObj();
            gameEvent.playerIndex = mStickIdx;
            gameEvent.action = event.getAction();
            gameEvent.event = (int) v.getTag();
            ACGGamePaasService.getInstance().customGamepadEvent(gameEvent);
        }
        return false;
    }
});

3. Object required for the customGamepadEvent method

public class CGCustomGamepadEventObj implements Serializable {
    public int playerIndex; // Gamepad index.
    public int event; // Gamepad button value.
    public int action; // Gamepad event (press, release, or move).
    public int xValue; // The x-value from the joystick or LT/RT.
    public int yValue; // The y-value from the joystick or LT/RT.
}

4. Button values and button events for custom gamepads

public class CGGamePadInputAction {
    public static final int CG_GAMEPAD_BUTTON_DOWN = 0; // Gamepad button pressed.
    public static final int CG_GAMEPAD_BUTTON_UP = 1; // Gamepad button released.
    public static final int CG_GAMEPAD_MOVE = 2; // Joystick moved.
}
public class CGGamePadInputEvent {
    public static final int CG_GAMEPAD_BUTTON_X = 0x88; // Gamepad X button. Gamepad layout index: 1.
    public static final int CG_GAMEPAD_BUTTON_A = 0x89; // Gamepad A button. Gamepad layout index: 2.
    public static final int CG_GAMEPAD_BUTTON_B = 0x8A; // Gamepad B button. Gamepad layout index: 3.
    public static final int CG_GAMEPAD_BUTTON_Y = 0x8B; // Gamepad Y button. Gamepad layout index: 4.
    public static final int CG_GAMEPAD_LEFT_BUMPER = 0x8C; // Gamepad LB. Gamepad layout index: 5.
    public static final int CG_GAMEPAD_RIGHT_BUMPER = 0x8D; // Gamepad RB. Gamepad layout index: 6.
    public static final int CG_GAMEPAD_LEFT_TRIGGER = 0x8E; // Gamepad LT. Gamepad layout index: 7. The corresponding xValue ranges from 0 to 255. The initial state is 0.
    public static final int CG_GAMEPAD_RIGHT_TRIGGER = 0x8F; // Gamepad RT. Gamepad layout index: 8. The corresponding yValue ranges from 0 to 255. The initial state is 0.
    public static final int CG_GAMEPAD_BACK = 0x97; // Gamepad Back button. Gamepad layout index: 9.
    public static final int CG_GAMEPAD_START = 0x98; // Gamepad Start button. Gamepad layout index: 10.
    public static final int CG_GAMEPAD_LEFT_STICK_BUTTON = 0x99; // Left stick button event. Gamepad layout index: 11.
    public static final int CG_GAMEPAD_LEFT_STICK = 0xB8; // Left stick movement event. Gamepad layout index: 11. The corresponding xValue and yValue range from 0 to 255. The initial state is (127, 127).
    public static final int CG_GAMEPAD_RIGHT_STICK_BUTTON = 0x9A; // Right stick button event. Gamepad layout index: 12.
    public static final int CG_GAMEPAD_RIGHT_STICK = 0xB9; // Right stick movement event. Gamepad layout index: 12. The corresponding xValue and yValue range from 0 to 255. The initial state is (127, 127).
    public static final int CG_GAMEPAD_DIRECTPAD_LEFT = 0x9B; // D-pad left button. Gamepad layout index: 13.
    public static final int CG_GAMEPAD_DIRECTPAD_UP = 0x9C; // D-pad up button. Gamepad layout index: 13.
    public static final int CG_GAMEPAD_DIRECTPAD_RIGHT = 0x9D; // D-pad right button. Gamepad layout index: 13.
    public static final int CG_GAMEPAD_DIRECTPAD_DOWN = 0x9E; // D-pad down button. Gamepad layout index: 13.
}