NInput组件
在开发过程中,获取物理键盘/鼠标输入是比不可少的,而随着开发者们对输入系统要求的日益增加,经常会有项目在做多平台适配和跨平台移植时对不同的操作逻辑感到烦恼。为此,引擎的输入系统提供了从pc至头显,从键鼠到手柄,手势。对于不同平台,不同设备的输入支持,为开发者们提供了巨大的便捷。
| 接口名称 | 返回值 | 接口作用 |
|---|---|---|
| GetKey | bool | 按钮状态 |
| GetKeyDown | bool | 按钮是否被按下 |
| GetKeyUp | bool | 按钮是否被抬起 |
| GetKeyName | std::string | 按钮名称 |
| GetMousePosition | Vector2 | 鼠标位置 |
| GetMouseScreenPosition | Vector2 | 鼠标屏幕位置 |
| GetMousePositionDelta | Vector2 | 鼠标位置偏移值 |
| GetMouseScrollDelta | float | 滚轮偏移值 |
| GetMouseButton | bool | 鼠标按键状态 |
| GetMouseButtonDown | bool | 鼠标按钮按下 |
| GetMouseButtonUp | bool | 鼠标按钮抬起 |
| GetMouseButtonDoubleClick | bool | 鼠标按钮双击 |
| GetTouchDown | bool | 触碰按下 |
| GetTouchMove | bool | 触碰移动 |
| GetTouchUp | bool | 触碰抬起 |
| GetTouchPointerIds | std::set<int32>& | 触碰点位置 |
| SendKeyDownEvent | bool | 注入按键按下事件 |
| SendKeyUpEvent | bool | 注入按键抬起事件 |
| IsHmdConnected | bool | 头显链接状态 |
| IsEyeTrackConnected | bool | 眼动链接状态 |
| GetGazeMode | EGazeMode | 注视点模式 |
| IsControllerConnected | bool | 手柄链接状态 |
| GetControllerKey | bool | 手柄按键状态 |
| GetControllerKeyDown | bool | 手柄按键按下 |
| GetControllerKeyUp | bool | 手柄按键抬起 |
| GetControllerTouch | bool | 手柄触控状态 |
| GetControllerTouchPosition | Vector2 | 手柄触控点位置 |
| GetHandTrackData | NHandJoint* | 手势数据 |
| GetHandActive | bool | 手势激活状态 |
| GetJoyStickKey | bool | 摇杆按键状态 |
| GetJoyStickKeyUp | bool | 摇杆按键抬起 |
| GetJoyStickKeyDown | bool | 摇杆按键按下 |
| GetJoyStickPos | float | 摇杆位置 |
| GetJoyStickPosMin | float | 摇杆位置最小值 |
| GetJoyStickPosMax | float | 摇杆位置最大值 |
| CreateCustomTrackingDevice | NCustomInputTrackedDevicePtr | 创建自定义设备 |
| SetCustomDeviceRay | void | 设置自定义设备射线 |
| SetCustomDeviceScreenPosition | void | 设置自定义设备鼠标位置 |
| SetEnableDeviceTracking | void | 开启设备追踪 |
| IsTrackingInputTrackedDevice | bool | 设备追踪状态 |
| GetEventCamera | NCameraPtr | 事件相机 |
| GetControllerRay | bool | 控制射线获取 |
| SetDeviceRayFromScreenPosition | void | 设置设备射线状态 |
| OnHitActorsCallback | bool | 注册击中Actor组回调 |
| OnHitActorCallback | bool | 注册击中Actor回调 |
| UnRegisterHitActorCallback | void | 取消Actor组回调 |
| UnRegisterHitActorsCallback | void | 取消Actor回调 |
| InjectKeyEvent | bool | 注入按键事件 |
| InjectMouseEvent | bool | 注入鼠标事件 |
| InjectMouseButtonEvent | bool | 注入鼠标按键事件 |
| InjectMouseWheelEvent | bool | 注入鼠标滚轮事件 |
| InjectTouchEvent | bool | 注入按键状态 |
| InjectTouchGestureEvent | bool | 注入触碰缩放事件 |
| InjectControllerKeyEvent | bool | 注入手柄按键 |
| InjectControllerTouchEvent | bool | 注入手柄触碰 |
| InjectJoyStickKey | bool | 注入摇杆事件 |
| InjectText | bool | 注入文本 |
示例代码:
- 获取键盘按键移动物体
cpp
if (NInput::GetKey(KeyboardKeys::A))
{
m_CameraPosition = m_CameraRotation.AxisX() * -m_MoveSpeed;
}
else if (NInput::GetKey(KeyboardKeys::D))
{
m_CameraPosition = m_CameraRotation.AxisX() * m_MoveSpeed;
}
else if (NInput::GetKey(KeyboardKeys::S))
{
m_CameraPosition = m_CameraRotation.AxisZ() * -m_MoveSpeed;
}
else if (NInput::GetKey(KeyboardKeys::W))
{
m_CameraPosition = m_CameraRotation.AxisZ() * m_MoveSpeed;
}
else if (NInput::GetKey(KeyboardKeys::Q))
{
m_CameraPosition = m_CameraRotation.AxisY() * -m_MoveSpeed;
}
else if (NInput::GetKey(KeyboardKeys::E))
{
m_CameraPosition = m_CameraRotation.AxisY() * m_MoveSpeed;
}
m_Camera->SetPosition(m_Camera->GetPosition() + m_CameraPosition);- 获取鼠标位置移动物体
cpp
m_CameraRotate = m_Camera->GetRotation().ToEulerZXY();
if (NInput::GetMouseButtonDown(MouseButton::Left))
{
m_PreMousePosition = NInput::GetMousePosition();
}
if (NInput::GetMouseButton(MouseButton::Left))
{
m_CurMousePosition = NInput::GetMousePosition();
if (m_CameraRotate.z >= 90 && m_CameraRotate.z <= 270)
{
m_Flag = -1;
}
else
{
m_Flag = 1;
}
m_CameraRotate.x += (m_CurMousePosition.y - m_PreMousePosition.y) * m_RotateSpeed * m_Flag;
m_CameraRotate.y += (m_CurMousePosition.x - m_PreMousePosition.x) * m_RotateSpeed;
m_Camera->SetRotation(Quaternion::FromEulerZXY(m_CameraRotate));
m_PreMousePosition = m_CurMousePosition;
}
