- 新增 Platform 模块:PlatformTypes.h, Window.h, WindowsWindow - 新增 Input 模块:InputTypes, InputEvent, InputAxis, InputModule, InputManager - 新增 WindowsInputModule 处理 Win32 消息转换 - 将 RHI 集成测试从 render_model 迁移到 sphere - 更新 CMakeLists.txt 添加 Platform 和 Input 模块
241 lines
7.5 KiB
C++
241 lines
7.5 KiB
C++
#include "Input/Platform/WindowsInputModule.h"
|
|
#include "Input/InputManager.h"
|
|
#include <Windows.h>
|
|
|
|
namespace XCEngine {
|
|
namespace Input {
|
|
namespace Platform {
|
|
|
|
WindowsInputModule::WindowsInputModule()
|
|
: m_hwnd(nullptr)
|
|
, m_isInitialized(false) {
|
|
}
|
|
|
|
WindowsInputModule::~WindowsInputModule() {
|
|
Shutdown();
|
|
}
|
|
|
|
void WindowsInputModule::Initialize(void* windowHandle) {
|
|
if (m_isInitialized) return;
|
|
|
|
m_hwnd = reinterpret_cast<HWND>(windowHandle);
|
|
m_isInitialized = true;
|
|
m_lastMousePosition = Math::Vector2::Zero();
|
|
}
|
|
|
|
void WindowsInputModule::Shutdown() {
|
|
if (!m_isInitialized) return;
|
|
|
|
m_hwnd = nullptr;
|
|
m_isInitialized = false;
|
|
}
|
|
|
|
void WindowsInputModule::PumpEvents() {
|
|
}
|
|
|
|
void WindowsInputModule::HandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
|
|
if (!m_isInitialized) return;
|
|
|
|
switch (message) {
|
|
case WM_KEYDOWN:
|
|
ProcessKeyDown(wParam, lParam);
|
|
break;
|
|
|
|
case WM_KEYUP:
|
|
ProcessKeyUp(wParam);
|
|
break;
|
|
|
|
case WM_CHAR:
|
|
ProcessCharInput(wParam);
|
|
break;
|
|
|
|
case WM_MOUSEMOVE:
|
|
ProcessMouseMove(wParam, lParam);
|
|
break;
|
|
|
|
case WM_LBUTTONDOWN:
|
|
ProcessMouseButton(wParam, lParam, true, MouseButton::Left);
|
|
break;
|
|
|
|
case WM_LBUTTONUP:
|
|
ProcessMouseButton(wParam, lParam, false, MouseButton::Left);
|
|
break;
|
|
|
|
case WM_RBUTTONDOWN:
|
|
ProcessMouseButton(wParam, lParam, true, MouseButton::Right);
|
|
break;
|
|
|
|
case WM_RBUTTONUP:
|
|
ProcessMouseButton(wParam, lParam, false, MouseButton::Right);
|
|
break;
|
|
|
|
case WM_MBUTTONDOWN:
|
|
ProcessMouseButton(wParam, lParam, true, MouseButton::Middle);
|
|
break;
|
|
|
|
case WM_MBUTTONUP:
|
|
ProcessMouseButton(wParam, lParam, false, MouseButton::Middle);
|
|
break;
|
|
|
|
case WM_MOUSEWHEEL:
|
|
ProcessMouseWheel(wParam, lParam);
|
|
break;
|
|
|
|
case WM_XBUTTONDOWN:
|
|
ProcessMouseButton(wParam, lParam, true,
|
|
(HIWORD(wParam) == 1) ? MouseButton::Button4 : MouseButton::Button5);
|
|
break;
|
|
|
|
case WM_XBUTTONUP:
|
|
ProcessMouseButton(wParam, lParam, false,
|
|
(HIWORD(wParam) == 1) ? MouseButton::Button4 : MouseButton::Button5);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void WindowsInputModule::ProcessKeyDown(WPARAM wParam, LPARAM lParam) {
|
|
bool repeat = (lParam & (1 << 30)) != 0;
|
|
KeyCode keyCode = VKCodeToKeyCode(static_cast<int>(wParam));
|
|
|
|
if (keyCode != KeyCode::None) {
|
|
InputManager::Get().ProcessKeyDown(keyCode, repeat);
|
|
}
|
|
}
|
|
|
|
void WindowsInputModule::ProcessKeyUp(WPARAM wParam) {
|
|
KeyCode keyCode = VKCodeToKeyCode(static_cast<int>(wParam));
|
|
|
|
if (keyCode != KeyCode::None) {
|
|
InputManager::Get().ProcessKeyUp(keyCode);
|
|
}
|
|
}
|
|
|
|
void WindowsInputModule::ProcessMouseMove(WPARAM wParam, LPARAM lParam) {
|
|
int x = LOWORD(lParam);
|
|
int y = HIWORD(lParam);
|
|
|
|
int deltaX = x - static_cast<int>(m_lastMousePosition.x);
|
|
int deltaY = y - static_cast<int>(m_lastMousePosition.y);
|
|
|
|
m_lastMousePosition.x = static_cast<float>(x);
|
|
m_lastMousePosition.y = static_cast<float>(y);
|
|
|
|
InputManager::Get().ProcessMouseMove(x, y, deltaX, deltaY);
|
|
}
|
|
|
|
void WindowsInputModule::ProcessMouseButton(WPARAM wParam, LPARAM lParam, bool pressed, MouseButton button) {
|
|
int x = LOWORD(lParam);
|
|
int y = HIWORD(lParam);
|
|
|
|
InputManager::Get().ProcessMouseButton(button, pressed, x, y);
|
|
}
|
|
|
|
void WindowsInputModule::ProcessMouseWheel(WPARAM wParam, LPARAM lParam) {
|
|
int x = LOWORD(lParam);
|
|
int y = HIWORD(lParam);
|
|
short delta = static_cast<short>(HIWORD(wParam));
|
|
|
|
InputManager::Get().ProcessMouseWheel(static_cast<float>(delta) / 120.0f, x, y);
|
|
}
|
|
|
|
void WindowsInputModule::ProcessCharInput(WPARAM wParam) {
|
|
char c = static_cast<char>(wParam);
|
|
|
|
if (c >= 32 && c < 127) {
|
|
InputManager::Get().ProcessTextInput(c);
|
|
}
|
|
}
|
|
|
|
KeyCode WindowsInputModule::VKCodeToKeyCode(int vkCode) {
|
|
switch (vkCode) {
|
|
case 'A': return KeyCode::A;
|
|
case 'B': return KeyCode::B;
|
|
case 'C': return KeyCode::C;
|
|
case 'D': return KeyCode::D;
|
|
case 'E': return KeyCode::E;
|
|
case 'F': return KeyCode::F;
|
|
case 'G': return KeyCode::G;
|
|
case 'H': return KeyCode::H;
|
|
case 'I': return KeyCode::I;
|
|
case 'J': return KeyCode::J;
|
|
case 'K': return KeyCode::K;
|
|
case 'L': return KeyCode::L;
|
|
case 'M': return KeyCode::M;
|
|
case 'N': return KeyCode::N;
|
|
case 'O': return KeyCode::O;
|
|
case 'P': return KeyCode::P;
|
|
case 'Q': return KeyCode::Q;
|
|
case 'R': return KeyCode::R;
|
|
case 'S': return KeyCode::S;
|
|
case 'T': return KeyCode::T;
|
|
case 'U': return KeyCode::U;
|
|
case 'V': return KeyCode::V;
|
|
case 'W': return KeyCode::W;
|
|
case 'X': return KeyCode::X;
|
|
case 'Y': return KeyCode::Y;
|
|
case 'Z': return KeyCode::Z;
|
|
|
|
case '0': return KeyCode::Zero;
|
|
case '1': return KeyCode::One;
|
|
case '2': return KeyCode::Two;
|
|
case '3': return KeyCode::Three;
|
|
case '4': return KeyCode::Four;
|
|
case '5': return KeyCode::Five;
|
|
case '6': return KeyCode::Six;
|
|
case '7': return KeyCode::Seven;
|
|
case '8': return KeyCode::Eight;
|
|
case '9': return KeyCode::Nine;
|
|
|
|
case VK_SPACE: return KeyCode::Space;
|
|
case VK_TAB: return KeyCode::Tab;
|
|
case VK_RETURN: return KeyCode::Enter;
|
|
case VK_ESCAPE: return KeyCode::Escape;
|
|
case VK_SHIFT: return KeyCode::LeftShift;
|
|
case VK_CONTROL: return KeyCode::LeftCtrl;
|
|
case VK_MENU: return KeyCode::LeftAlt;
|
|
|
|
case VK_UP: return KeyCode::Up;
|
|
case VK_DOWN: return KeyCode::Down;
|
|
case VK_LEFT: return KeyCode::Left;
|
|
case VK_RIGHT: return KeyCode::Right;
|
|
|
|
case VK_HOME: return KeyCode::Home;
|
|
case VK_END: return KeyCode::End;
|
|
case VK_PRIOR: return KeyCode::PageUp;
|
|
case VK_NEXT: return KeyCode::PageDown;
|
|
case VK_DELETE: return KeyCode::Delete;
|
|
case VK_BACK: return KeyCode::Backspace;
|
|
|
|
case VK_F1: return KeyCode::F1;
|
|
case VK_F2: return KeyCode::F2;
|
|
case VK_F3: return KeyCode::F3;
|
|
case VK_F4: return KeyCode::F4;
|
|
case VK_F5: return KeyCode::F5;
|
|
case VK_F6: return KeyCode::F6;
|
|
case VK_F7: return KeyCode::F7;
|
|
case VK_F8: return KeyCode::F8;
|
|
case VK_F9: return KeyCode::F9;
|
|
case VK_F10: return KeyCode::F10;
|
|
case VK_F11: return KeyCode::F11;
|
|
case VK_F12: return KeyCode::F12;
|
|
|
|
case VK_OEM_MINUS: return KeyCode::Minus;
|
|
case VK_OEM_PLUS: return KeyCode::Equals;
|
|
case VK_OEM_4: return KeyCode::BracketLeft;
|
|
case VK_OEM_6: return KeyCode::BracketRight;
|
|
case 0xBA: return KeyCode::Semicolon;
|
|
case 0xDE: return KeyCode::Quote;
|
|
case VK_OEM_COMMA: return KeyCode::Comma;
|
|
case VK_OEM_PERIOD: return KeyCode::Period;
|
|
case VK_OEM_2: return KeyCode::Slash;
|
|
case VK_OEM_5: return KeyCode::Backslash;
|
|
case VK_OEM_3: return KeyCode::Backtick;
|
|
|
|
default: return KeyCode::None;
|
|
}
|
|
}
|
|
|
|
} // namespace Platform
|
|
} // namespace Input
|
|
} // namespace XCEngine
|