Skip to content

Commit

Permalink
refactor:add new enum/struct for touch event info
Browse files Browse the repository at this point in the history
  • Loading branch information
oahc09 committed Sep 21, 2023
1 parent a079fd1 commit 31cb4a2
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 174 deletions.
6 changes: 6 additions & 0 deletions @types/jsb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ declare namespace jsb {
id: number;
axisInfoList: AxisInfo[],
buttonInfoList: ButtonInfo[],
touchInfoList: TouchInfo[],
}

export interface AxisInfo {
Expand All @@ -92,6 +93,11 @@ declare namespace jsb {
isPressed: boolean,
}

export interface TouchInfo {
code: number,
value: number,
}

export let onControllerInput: (infoList: ControllerInfo[]) => void | undefined;
export let onHandleInput: (infoList: ControllerInfo[]) => void | undefined;
export let onControllerChange: (controllerIds: number[]) => void | undefined;
Expand Down
12 changes: 12 additions & 0 deletions native/cocos/bindings/event/EventDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,20 @@ void EventDispatcher::dispatchControllerEvent(const ControllerEvent &controllerE
jsAxisInfoList->setArrayElement(axisIndex, se::Value(jsAxisInfo));
axisIndex++;
}

se::HandleObject jsTouchInfoList{se::Object::createArrayObject(static_cast<uint32_t>(controller->touchInfos.size()))};

uint32_t touchIndex = 0;
for (const auto &touchInfo : controller->touchInfos) {
se::HandleObject jsTouchInfo{se::Object::createPlainObject()};
jsTouchInfo->setProperty("code", se::Value(static_cast<uint32_t>(touchInfo.key)));
jsTouchInfo->setProperty("value", se::Value(touchInfo.value));
jsTouchInfoList->setArrayElement(touchIndex, se::Value(jsTouchInfo));
touchIndex++;
}
jsController->setProperty("axisInfoList", se::Value(jsAxisInfoList));
jsController->setProperty("buttonInfoList", se::Value(jsButtonInfoList));
jsController->setProperty("touchInfoList", se::Value(jsTouchInfoList));

jsControllerEventArray->setArrayElement(controllerIndex, se::Value(jsController));
controllerIndex++;
Expand Down
13 changes: 12 additions & 1 deletion native/cocos/engine/EngineEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ enum class StickAxisCode {
R2,
LEFT_GRIP,
RIGHT_GRIP,
};

enum class StickTouchCode {
UNDEFINE = 0,
A,
B,
X,
Y,
LEFT_TRIGGER,
RIGHT_TRIGGER,
LEFT_THUMBSTICK,
Expand All @@ -143,10 +149,15 @@ struct ControllerInfo {
bool isPress{false};
ButtonInfo(StickKeyCode key, bool isPress) : key(key), isPress(isPress) {}
};

struct TouchInfo {
StickTouchCode key{StickTouchCode::UNDEFINE};
float value{0.F};
TouchInfo(StickTouchCode key, float value) : key(key), value(value) {}
};
int napdId{0};
std::vector<AxisInfo> axisInfos;
std::vector<ButtonInfo> buttonInfos;
std::vector<TouchInfo> touchInfos;
};

struct ControllerEvent {
Expand Down
40 changes: 32 additions & 8 deletions native/cocos/platform/interfaces/modules/XRCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ enum class XREventType {
STICK,
GRAB,
POSE,
TOUCH,
UNKNOWN
};

Expand Down Expand Up @@ -289,14 +290,6 @@ struct XRGrab : public XRControllerInfo {
GRIP_LEFT,
TRIGGER_RIGHT,
GRIP_RIGHT,
TOUCH_A,
TOUCH_B,
TOUCH_X,
TOUCH_Y,
TOUCH_TRIGGER_LEFT,
TOUCH_TRIGGER_RIGHT,
TOUCH_THUMBSTICK_LEFT,
TOUCH_THUMBSTICK_RIGHT,
UNKNOWN
};

Expand All @@ -318,6 +311,37 @@ struct XRGrab : public XRControllerInfo {
}
};

struct XRTouch : public XRControllerInfo {
enum class Type {
TOUCH_A,
TOUCH_B,
TOUCH_X,
TOUCH_Y,
TOUCH_TRIGGER_LEFT,
TOUCH_TRIGGER_RIGHT,
TOUCH_THUMBSTICK_LEFT,
TOUCH_THUMBSTICK_RIGHT,
UNKNOWN
};

bool isActive = false;
float value = 0.F;
Type type = Type::UNKNOWN;

XRTouch(Type type, bool isActive)
: type(type),
isActive(isActive) {}

XRTouch(Type type, float value)
: type(type),
isActive(true),
value(value) {}

XREventType getXREventType() const override {
return XREventType::TOUCH;
}
};

struct XRPose : public XRControllerInfo {
enum class Type {
VIEW_LEFT,
Expand Down
35 changes: 26 additions & 9 deletions native/cocos/platform/java/modules/XRInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ const static ccstd::unordered_map<xr::XRGrab::Type, StickAxisCode> GRAB_TYPE_TO_
{xr::XRGrab::Type::TRIGGER_RIGHT, StickAxisCode::R2},
{xr::XRGrab::Type::GRIP_LEFT, StickAxisCode::LEFT_GRIP},
{xr::XRGrab::Type::GRIP_RIGHT, StickAxisCode::RIGHT_GRIP},
{xr::XRGrab::Type::TOUCH_A, StickAxisCode::A},
{xr::XRGrab::Type::TOUCH_B, StickAxisCode::B},
{xr::XRGrab::Type::TOUCH_X, StickAxisCode::X},
{xr::XRGrab::Type::TOUCH_Y, StickAxisCode::Y},
{xr::XRGrab::Type::TOUCH_TRIGGER_LEFT, StickAxisCode::LEFT_TRIGGER},
{xr::XRGrab::Type::TOUCH_TRIGGER_RIGHT, StickAxisCode::RIGHT_TRIGGER},
{xr::XRGrab::Type::TOUCH_THUMBSTICK_LEFT, StickAxisCode::LEFT_THUMBSTICK},
{xr::XRGrab::Type::TOUCH_THUMBSTICK_RIGHT, StickAxisCode::RIGHT_THUMBSTICK},
};

const static ccstd::unordered_map<xr::XRTouch::Type, StickTouchCode> TOUCH_TYPE_TO_AXIS_CODE = {
{xr::XRTouch::Type::TOUCH_A, StickTouchCode::A},
{xr::XRTouch::Type::TOUCH_B, StickTouchCode::B},
{xr::XRTouch::Type::TOUCH_X, StickTouchCode::X},
{xr::XRTouch::Type::TOUCH_Y, StickTouchCode::Y},
{xr::XRTouch::Type::TOUCH_TRIGGER_LEFT, StickTouchCode::LEFT_TRIGGER},
{xr::XRTouch::Type::TOUCH_TRIGGER_RIGHT, StickTouchCode::RIGHT_TRIGGER},
{xr::XRTouch::Type::TOUCH_THUMBSTICK_LEFT, StickTouchCode::LEFT_THUMBSTICK},
{xr::XRTouch::Type::TOUCH_THUMBSTICK_RIGHT, StickTouchCode::RIGHT_THUMBSTICK},
};

void XRInterface::dispatchGamepadEventInternal(const xr::XRControllerEvent &xrControllerEvent) {
Expand Down Expand Up @@ -292,6 +295,14 @@ void XRInterface::dispatchHandleEventInternal(const xr::XRControllerEvent &xrCon
controllerInfo->axisInfos.emplace_back(ControllerInfo::AxisInfo(stickAxisCode, xrGrab->value));
}
} break;
case xr::XREventType::TOUCH: {
auto *xrTouch = static_cast<xr::XRTouch *>(xrControllerEvent.xrControllerInfos.at(i).get());
if(TOUCH_TYPE_TO_AXIS_CODE.count(xrTouch->type) > 0) {
StickTouchCode stickTouchCode = TOUCH_TYPE_TO_AXIS_CODE.at(xrTouch->type);
controllerInfo->touchInfos.emplace_back(ControllerInfo::TouchInfo(stickTouchCode, xrTouch->value));
}
break;
}
case xr::XREventType::POSE: {
auto *xrPose = static_cast<xr::XRPose *>(xrControllerEvent.xrControllerInfos.at(i).get());
switch (xrPose->type) {
Expand Down Expand Up @@ -331,7 +342,7 @@ void XRInterface::dispatchHandleEventInternal(const xr::XRControllerEvent &xrCon
EventDispatcher::doDispatchJsEvent("onHandlePoseInput", args);
}

if (!controllerInfo->buttonInfos.empty() || !controllerInfo->axisInfos.empty()) {
if (!controllerInfo->buttonInfos.empty() || !controllerInfo->axisInfos.empty() || !controllerInfo->touchInfos.empty()) {
controllerInfo->napdId = 0; // xr only one handle connection
_controllerEvent.controllerInfos.emplace_back(controllerInfo);
_controllerEvent.type = ControllerEvent::Type::HANDLE;
Expand All @@ -348,6 +359,12 @@ void XRInterface::dispatchHandleEventInternal(const xr::XRControllerEvent &xrCon
_xrRemotePreviewManager->sendControllerKeyInfo(axisInfo);
}
}

if (!controllerInfo->touchInfos.empty()) {
for (const auto &touchInfo : controllerInfo->touchInfos) {
_xrRemotePreviewManager->sendControllerKeyInfo(touchInfo);
}
}
}
#endif
events::Controller::broadcast(_controllerEvent);
Expand Down
27 changes: 27 additions & 0 deletions native/cocos/xr/XRRemotePreviewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,33 @@ void XRRemotePreviewManager::sendControllerKeyInfo(const ControllerInfo::AxisInf
#endif
}

void XRRemotePreviewManager::sendControllerKeyInfo(const ControllerInfo::TouchInfo &info) {
#if CC_USE_WEBSOCKET_SERVER
if (_webSocketServer && _isConnectionChanged) {
_isConnectionChanged = false;
_wssConnections = _webSocketServer->getConnections();
}

if (_webSocketServer && !_wssConnections.empty()) {
for (auto &wssConn : _wssConnections) {
if (wssConn->getReadyState() == WebSocketServerConnection::ReadyState::OPEN) {
XRControllerKeyInfo ctrlKeyInfo;
ctrlKeyInfo.type = static_cast<int16_t>(XRDataPackageType::DPT_MSG_CONTROLLER_KEY);
ctrlKeyInfo.stickTouchValue = info.value;
ctrlKeyInfo.stickTouchCode = static_cast<int16_t>(info.key);
ctrlKeyInfo.keyEventType = static_cast<int16_t>(XRKeyEventType::KET_TOUCH);
size_t dataLen = 8 + sizeof(ctrlKeyInfo);
char data[dataLen];
packControllerKeyData(ctrlKeyInfo, data);
wssConn->sendBinaryAsync(data, dataLen, nullptr);
} else {
CC_LOG_ERROR("[XRRemotePreviewManager] sendControllerTouchInfo failed !!!");
}
}
}
#endif
}

void XRRemotePreviewManager::resume() {
CC_LOG_INFO("[XRRemotePreviewManager] resume");
sendMessage("resume");
Expand Down
6 changes: 5 additions & 1 deletion native/cocos/xr/XRRemotePreviewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ enum class XRDataPackageType {
enum class XRKeyEventType {
KET_CLICK,
KET_STICK,
KET_GRAB
KET_GRAB,
KET_TOUCH,
};

#pragma pack(1)
Expand Down Expand Up @@ -88,6 +89,8 @@ struct XRControllerKeyInfo {
float stickAxisValue{0};
int16_t stickKeyCode{0};
bool isButtonPressed{false};
int16_t stickTouchCode{0};
float stickTouchValue{0};
};
#pragma pack()

Expand All @@ -100,6 +103,7 @@ class XRRemotePreviewManager : public RefCounted {
void sendPoseInfo(const XRPoseInfo &info);
void sendControllerKeyInfo(const ControllerInfo::ButtonInfo &info);
void sendControllerKeyInfo(const ControllerInfo::AxisInfo &info);
void sendControllerKeyInfo(const ControllerInfo::TouchInfo &info);
void tick();
void resume();
void pause();
Expand Down
24 changes: 19 additions & 5 deletions pal/input/input-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class InputSourceAxis3D extends InputSource<Vec3> {
* @en The class for input source of Quaternion, which is used to control the input signal of a mono input source
* @zh 四元数的 InputSource 类,该类用于控制单一输入源的输入信号
*/
export class InputSourceQuat extends InputSource<Quat> {
export class InputSourceQuat extends InputSource<Quat> {
/**
* @en Get the signal value of the input source, which returns a Quat object.
* @zh 获取输入源的信号值,该方法返回一个 Quat 对象
Expand Down Expand Up @@ -288,12 +288,12 @@ export class InputSourceStick extends CompositeInputSourceAxis2D {
* @en The class for input source of orientation, whose input signal value a Quat object
* @zh 方向输入源类, 输入信号源的取值是一个 Quat 对象
*/
export class InputSourceOrientation extends InputSourceQuat {
export class InputSourceOrientation extends InputSourceQuat {
/**
* @en Get the signal value of the input source, which returns a Quat object.
* @zh 获取输入源的信号值,该方法返回一个 Quat 对象
*/
getValue (): Quat {
getValue (): Quat {
return super.getValue();
}
}
Expand All @@ -302,12 +302,26 @@ export class InputSourceStick extends CompositeInputSourceAxis2D {
* @en The class for input source of position, whose input signal value a Vec3 object
* @zh 坐标输入源类, 输入信号源的取值是一个 Vec3 对象
*/
export class InputSourcePosition extends InputSourceAxis3D {
export class InputSourcePosition extends InputSourceAxis3D {
/**
* @en Get the signal value of the input source, which returns a Vec3 object.
* @zh 获取输入源的信号值,该方法返回一个 Vec3 对象
*/
getValue (): Vec3 {
getValue (): Vec3 {
return super.getValue();
}
}

/**
* @en The class for input source of mono button touch, whose input signal value is ranged from 0 or 1
* @zh 单一按键触摸输入源类, 输入信号源的取值范围是 0 或 1
*/
export class InputSourceTouch extends InputSourceAxis1D {
/**
* @en Get the signal value of the input source, ranged from 0 or 1
* @zh 获取输入源的信号值,取值范围从 0 或 1
*/
getValue (): number {
return super.getValue();
}
}
51 changes: 26 additions & 25 deletions pal/input/minigame/handle-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { HandleCallback } from 'pal/input';
import { InputEventType } from '../../../cocos/input/types/event-enum';
import { EventTarget } from '../../../cocos/core/event';
import { InputSourceButton, InputSourceStick, InputSourcePosition, InputSourceOrientation } from '../input-source';
import { InputSourceButton, InputSourceStick, InputSourcePosition, InputSourceOrientation, InputSourceTouch } from '../input-source';
import { Vec3, Quat } from '../../../cocos/core/math';

export class HandleInputDevice {
Expand Down Expand Up @@ -53,14 +53,14 @@ export class HandleInputDevice {
public get aimLeftOrientation (): InputSourceOrientation { return this._aimLeftOrientation; }
public get aimRightPosition (): InputSourcePosition { return this._aimRightPosition; }
public get aimRightOrientation (): InputSourceOrientation { return this._aimRightOrientation; }
public get touchButtonA (): InputSourceButton { return this._touchButtonA; }
public get touchButtonB (): InputSourceButton { return this._touchButtonB; }
public get touchButtonX (): InputSourceButton { return this._touchButtonX; }
public get touchButtonY (): InputSourceButton { return this._touchButtonY; }
public get touchButtonTriggerLeft (): InputSourceButton { return this._touchButtonTriggerLeft; }
public get touchButtonTriggerRight (): InputSourceButton { return this._touchButtonTriggerRight; }
public get touchButtonThumbStickLeft (): InputSourceButton { return this._touchButtonThumbStickLeft; }
public get touchButtonThumbStickRight (): InputSourceButton { return this._touchButtonThumbStickRight; }
public get touchButtonA (): InputSourceTouch { return this._touchButtonA; }
public get touchButtonB (): InputSourceTouch { return this._touchButtonB; }
public get touchButtonX (): InputSourceTouch { return this._touchButtonX; }
public get touchButtonY (): InputSourceTouch { return this._touchButtonY; }
public get touchButtonTriggerLeft (): InputSourceTouch { return this._touchButtonTriggerLeft; }
public get touchButtonTriggerRight (): InputSourceTouch { return this._touchButtonTriggerRight; }
public get touchButtonThumbStickLeft (): InputSourceTouch { return this._touchButtonThumbStickLeft; }
public get touchButtonThumbStickRight (): InputSourceTouch { return this._touchButtonThumbStickRight; }

private _eventTarget: EventTarget = new EventTarget();

Expand Down Expand Up @@ -88,14 +88,14 @@ export class HandleInputDevice {
private _aimLeftOrientation!: InputSourceOrientation;
private _aimRightPosition!: InputSourcePosition;
private _aimRightOrientation!: InputSourceOrientation;
private _touchButtonA!: InputSourceButton;
private _touchButtonB!: InputSourceButton;
private _touchButtonX!: InputSourceButton;
private _touchButtonY!: InputSourceButton;
private _touchButtonTriggerLeft!: InputSourceButton;
private _touchButtonTriggerRight!: InputSourceButton;
private _touchButtonThumbStickLeft!: InputSourceButton;
private _touchButtonThumbStickRight!: InputSourceButton;
private _touchButtonA!: InputSourceTouch;
private _touchButtonB!: InputSourceTouch;
private _touchButtonX!: InputSourceTouch;
private _touchButtonY!: InputSourceTouch;
private _touchButtonTriggerLeft!: InputSourceTouch;
private _touchButtonTriggerRight!: InputSourceTouch;
private _touchButtonThumbStickLeft!: InputSourceTouch;
private _touchButtonThumbStickRight!: InputSourceTouch;

constructor () {
this._initInputSource();
Expand Down Expand Up @@ -179,21 +179,22 @@ export class HandleInputDevice {
this._aimRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
this._aimRightOrientation = new InputSourceOrientation();
this._aimRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
this._touchButtonA = new InputSourceButton();

this._touchButtonA = new InputSourceTouch();
this._touchButtonA.getValue = (): number => 0;
this._touchButtonB = new InputSourceButton();
this._touchButtonB = new InputSourceTouch();
this._touchButtonB.getValue = (): number => 0;
this._touchButtonX = new InputSourceButton();
this._touchButtonX = new InputSourceTouch();
this._touchButtonX.getValue = (): number => 0;
this._touchButtonY = new InputSourceButton();
this._touchButtonY = new InputSourceTouch();
this._touchButtonY.getValue = (): number => 0;
this._touchButtonTriggerLeft = new InputSourceButton();
this._touchButtonTriggerLeft = new InputSourceTouch();
this._touchButtonTriggerLeft.getValue = (): number => 0;
this._touchButtonTriggerRight = new InputSourceButton();
this._touchButtonTriggerRight = new InputSourceTouch();
this._touchButtonTriggerRight.getValue = (): number => 0;
this._touchButtonThumbStickLeft = new InputSourceButton();
this._touchButtonThumbStickLeft = new InputSourceTouch();
this._touchButtonThumbStickLeft.getValue = (): number => 0;
this._touchButtonThumbStickRight = new InputSourceButton();
this._touchButtonThumbStickRight = new InputSourceTouch();
this._touchButtonThumbStickRight.getValue = (): number => 0;
}
}
Loading

0 comments on commit 31cb4a2

Please sign in to comment.