Mercurial > LightClone
view LightClone/Source/InputManager.cpp @ 39:d0ce73ced12e
Added base GuiDialog for event handling; Hookup level complete dialog
author | koryspansel <koryspansel@bendbroadband.com> |
---|---|
date | Thu, 22 Sep 2011 09:01:42 -0700 |
parents | 0b729faa4e73 |
children | d27c06bd8ce1 |
line wrap: on
line source
/* * InputManager */ #include "InputManager.h" #include "EventSystem.h" /* * fMouseSensitivity */ static const float fMouseSensitivity = 1.5f; /* * InputManager */ InputManager::InputManager() { pDirectInput = NULL; pKeyboard = NULL; pMouse = NULL; fMouseX = 0.0f; fMouseY = 0.0f; memset(kCurrentKeyboardState, 0, sizeof(kCurrentKeyboardState)); memset(kPreviousKeyboardState, 0, sizeof(kPreviousKeyboardState)); memset(&kCurrentMouseState, 0, sizeof(kCurrentMouseState)); memset(&kPreviousMouseState, 0, sizeof(kPreviousMouseState)); } /* * Initialze */ ErrorCode InputManager::Initialize(HWND kWindow) { HINSTANCE hInstance = GetModuleHandle(NULL); HRESULT hResult = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDirectInput, NULL); if(FAILED(hResult)) { return Error_Fail; } hResult = pDirectInput->CreateDevice(GUID_SysKeyboard, &pKeyboard, NULL); if(FAILED(hResult)) { Terminate(); return Error_Fail; } hResult = pKeyboard->SetDataFormat(&c_dfDIKeyboard); if(FAILED(hResult)) { Terminate(); return Error_Fail; } hResult = pKeyboard->SetCooperativeLevel(kWindow, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if(FAILED(hResult)) { Terminate(); return Error_Fail; } hResult = pDirectInput->CreateDevice(GUID_SysMouse, &pMouse, NULL); if(FAILED(hResult)) { Terminate(); return Error_Fail; } hResult = pMouse->SetDataFormat(&c_dfDIMouse); if(FAILED(hResult)) { Terminate(); return Error_Fail; } hResult = pMouse->SetCooperativeLevel(kWindow, DISCL_EXCLUSIVE | DISCL_FOREGROUND); if(FAILED(hResult)) { Terminate(); return Error_Fail; } pMouse->Acquire(); return Error_Success; } /* * Terminate */ void InputManager::Terminate() { if(pMouse) { pMouse->Release(); pMouse = NULL; } if(pKeyboard) { pKeyboard->Release(); pKeyboard = NULL; } if(pDirectInput) { pDirectInput->Release(); pDirectInput = NULL; } } /* * Update */ void InputManager::Update(EventSystem* pEventSystem, float fElapsed) { if(pKeyboard) { memcpy(kPreviousKeyboardState, kCurrentKeyboardState, sizeof(kCurrentKeyboardState)); HRESULT hResult = pKeyboard->GetDeviceState(sizeof(kCurrentKeyboardState), kCurrentKeyboardState); if(FAILED(hResult)) { hResult = pKeyboard->Acquire(); if(SUCCEEDED(hResult)) { hResult = pKeyboard->GetDeviceState(sizeof(kCurrentKeyboardState), kCurrentKeyboardState); } } for(uint32 i = 0; i < sizeof(kCurrentKeyboardState) / sizeof(kCurrentKeyboardState[0]); ++i) { // check for key up events if(kPreviousKeyboardState[i] & 0x80) { if(!(kCurrentKeyboardState[i] & 0x80)) { InputEvent kEvent; kEvent.nType = InputEventType_KeyUp; kEvent.nKey = i; kEvent.nDuration = 0; pEventSystem->Post(kEvent); } } else { if(kCurrentKeyboardState[i] & 0x80) { InputEvent kEvent; kEvent.nType = InputEventType_KeyDown; kEvent.nKey = i; kEvent.nDuration = 0; pEventSystem->Post(kEvent); } } } } if(pMouse) { memcpy(&kPreviousMouseState, &kCurrentMouseState, sizeof(kCurrentMouseState)); HRESULT hResult = pMouse->GetDeviceState(sizeof(kCurrentMouseState), &kCurrentMouseState); if(FAILED(hResult)) { hResult = pMouse->Acquire(); if(SUCCEEDED(hResult)) { hResult = pMouse->GetDeviceState(sizeof(kCurrentMouseState), &kCurrentMouseState); } } fMouseX = Clamp(fMouseX + fMouseSensitivity * kCurrentMouseState.lX, kMouseBounds.X, kMouseBounds.X + kMouseBounds.Width - 1.0f); fMouseY = Clamp(fMouseY + fMouseSensitivity * kCurrentMouseState.lY, kMouseBounds.Y, kMouseBounds.Y + kMouseBounds.Height - 1.0f); for(uint32 i = 0; i < sizeof(kCurrentMouseState.rgbButtons) / sizeof(kCurrentMouseState.rgbButtons[0]); ++i) { if(kPreviousMouseState.rgbButtons[i] & 0x80) { if(!(kCurrentMouseState.rgbButtons[i] & 0x80)) { InputEvent kEvent; kEvent.nType = InputEventType_MouseUp; kEvent.nButton = i; kEvent.nDuration = 0; pEventSystem->Post(kEvent); } } else { if(kCurrentMouseState.rgbButtons[i] & 0x80) { InputEvent kEvent; kEvent.nType = InputEventType_MouseDown; kEvent.nButton = i; kEvent.nDuration = 0; pEventSystem->Post(kEvent); } } } if(kCurrentMouseState.lX != 0 || kCurrentMouseState.lY != 0) { InputEvent kEvent; kEvent.nType = InputEventType_MouseMove; kEvent.fX = fMouseX; kEvent.fY = fMouseY; pEventSystem->Post(kEvent); } } } /* * SetBounds */ void InputManager::SetBounds(float fMinimumX, float fMinimumY, float fMaximumX, float fMaximumY) { kMouseBounds.X = fMinimumX; kMouseBounds.Y = fMinimumY; kMouseBounds.Width = fMaximumX - fMinimumX; kMouseBounds.Height = fMaximumY - fMinimumY; } /* * SetMouse */ void InputManager::SetMouse(float fX, float fY) { fMouseX = Clamp(fX, kMouseBounds.X, kMouseBounds.X + kMouseBounds.Width - 1.0f); fMouseY = Clamp(fY, kMouseBounds.Y, kMouseBounds.Y + kMouseBounds.Height - 1.0f); } /* * IsKeyDown */ bool InputManager::IsKeyDown(uint32 nKey) const { return (kCurrentKeyboardState[nKey] & 0x80) != 0; } /* * WasKeyDown */ bool InputManager::WasKeyDown(uint32 nKey) const { return (kPreviousKeyboardState[nKey] & 0x80) != 0; } /* * IsButtonDown */ bool InputManager::IsButtonDown(uint32 nButton) const { return (kCurrentMouseState.rgbButtons[nButton] & 0x80) != 0; } /* * WasButtonDown */ bool InputManager::WasButtonDown(uint32 nButton) const { return (kPreviousMouseState.rgbButtons[nButton] & 0x80) != 0; } /* * IsButtonPressed */ bool InputManager::IsButtonPressed(uint32 nButton) const { return ((kCurrentMouseState.rgbButtons[nButton] & 0x80) != 0) && ((kPreviousMouseState.rgbButtons[nButton] & 0x80) == 0); } /* * IsButtonReleased */ bool InputManager::IsButtonReleased(uint32 nButton) const { return ((kCurrentMouseState.rgbButtons[nButton] & 0x80) == 0) && ((kPreviousMouseState.rgbButtons[nButton] & 0x80) != 0); } /* * GetMouseX */ float InputManager::GetMouseX() const { return fMouseX; } /* * GetMouseY */ float InputManager::GetMouseY() const { return fMouseY; } /* * GetMouseDeltaX */ float InputManager::GetMouseDeltaX() const { return (float)kCurrentMouseState.lX; } /* * GetMouseDeltaY */ float InputManager::GetMouseDeltaY() const { return (float)kCurrentMouseState.lY; }