SDK提供了内置的虚拟手柄供业务方使用,方便业务方快速调试游戏。

内置虚拟手柄配置及启用方法

1.手柄库依赖

1.1 将joystick-uikit包添加到项目依赖中。

1.2 在build.gradle文件中添加该aar包依赖

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

2.初始化手柄库

在启动游戏前(最好是启动完应用时)调用如下方法:GamePad.defaultInit()

注:默认手柄会添加到传入SDK的containerView上,containerView是通过如下方法传入到SDK的

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

3.启动游戏时设置参数enableCustomInputEvent

CGGamePrepareObj prepareObj = new CGGamePrepareObj();
prepareObj.token = "用户token";
prepareObj.userId = "用户userid";
prepareObj.mixGameId = "要启动游戏的id";
prepareObj.enableCustomInputEvent = false; //注:可以不设置,该参数默认为false

自定义虚拟手柄

1.添加自定义手柄View

在游戏页的布局文件中添加自定义手柄的view,示例如下:
<?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.事件传入SDK

调用ACGGamePaasService.getInstance().customGamepadEvent(eventObj),将自定义事件传入SDK,示例如下:
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.调用customGamepadEvent方法所需的对象
public class CGCustomGamepadEventObj implements Serializable {
    public int playerIndex;//手柄index
    public int event;//手柄按键值
    public int action;//手柄事件(按下、抬起、移动)   
    public int xValue;//摇杆或LT/RT传入的x值    
    public int yValue;//摇杆或LT/RT传入的y值 
}

4.自定义手柄事件的按键值及按键事件

public class CGGamePadInputAction {
    public static final int CG_GAMEPAD_BUTTON_DOWN = 0; //手柄按钮按下 
    public static final int CG_GAMEPAD_BUTTON_UP = 1; //手柄按钮抬起    
    public static final int CG_GAMEPAD_MOVE = 2; //摇杆移动
}
public class CGGamePadInputEvent {
    public static final int CG_GAMEPAD_BUTTON_X = 0x88; //手柄X按钮,手柄图索引(1)
    public static final int CG_GAMEPAD_BUTTON_A = 0x89; //手柄A按钮,手柄图索引(2)
    public static final int CG_GAMEPAD_BUTTON_B = 0x8A; //手柄B按钮,手柄图索引(3)
    public static final int CG_GAMEPAD_BUTTON_Y = 0x8B; //手柄Y按钮,手柄图索引(4)
    public static final int CG_GAMEPAD_LEFT_BUMPER = 0x8C; //手柄LB,手柄图索引(5)  
    public static final int CG_GAMEPAD_RIGHT_BUMPER = 0x8D; //手柄RB,手柄图索引(6)   
    public static final int CG_GAMEPAD_LEFT_TRIGGER = 0x8E; //手柄LT,手柄图索引(7);对应xValue从0到255,初始状态为0
    public static final int CG_GAMEPAD_RIGHT_TRIGGER = 0x8F; //手柄RT,手柄图索引(8);对应yValue从0到255,初始状态为0
    public static final int CG_GAMEPAD_BACK = 0x97; //手柄back,手柄图索引(9)   
    public static final int CG_GAMEPAD_START = 0x98; //手柄start,手柄图索引(10)  
    public static final int CG_GAMEPAD_LEFT_STICK_BUTTON = 0x99; //手柄左侧摇杆按键事件,手柄图索引(11)     
    public static final int CG_GAMEPAD_LEFT_STICK = 0xB8; //手柄左侧摇杆摇杆事件,手柄图索引(11);对应xValue从0到255,yValue从0到255,初始状态(127,127)    
    public static final int CG_GAMEPAD_RIGHT_STICK_BUTTON = 0x9A; //手柄右侧摇杆按键事件,手柄图索引(12)    
    public static final int CG_GAMEPAD_RIGHT_STICK = 0xB9; //手柄右侧摇杆摇杆事件,手柄图索引(12);对应xValue从0到255,yValue从0到255,初始状态(127,127)   
    public static final int CG_GAMEPAD_DIRECTPAD_LEFT = 0x9B; //手柄方向键左按钮,手柄图索引(13)    
    public static final int CG_GAMEPAD_DIRECTPAD_UP = 0x9C; //手柄方向键上按钮,手柄图索引(13)    
    public static final int CG_GAMEPAD_DIRECTPAD_RIGHT = 0x9D; //手柄方向键右按钮,手柄图索引(13)    
    public static final int CG_GAMEPAD_DIRECTPAD_DOWN = 0x9E; //手柄方向键下按钮,手柄图索引(13)
}