181 lines
5.3 KiB
C++
181 lines
5.3 KiB
C++
#include "InputSystemOIS.h"
|
|
|
|
#include <OIS.h>
|
|
using namespace OIS;
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <windows.h>
|
|
#include <vector>
|
|
#include "LString.h"
|
|
|
|
namespace LifeCore
|
|
{
|
|
class input_impl : public KeyListener, MouseListener
|
|
{
|
|
public:
|
|
input_impl(IInputSystem* sys)
|
|
{
|
|
input_system = sys;
|
|
}
|
|
~input_impl()
|
|
{
|
|
if(input)
|
|
InputManager::destroyInputSystem(input);
|
|
}
|
|
void Init(int scrW, int scrH, bool exclusiveMode, int hwnd)
|
|
{
|
|
//setlocale(LC_ALL,"");
|
|
HWND hw = (HWND)hwnd;
|
|
std::ostringstream wnd;
|
|
wnd << (size_t)hw << "\0";
|
|
pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
|
|
|
|
if( exclusiveMode)
|
|
{
|
|
pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
|
|
pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_EXCLUSIVE")));
|
|
}
|
|
else
|
|
{
|
|
pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_BACKGROUND")));
|
|
pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
|
|
}
|
|
pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_BACKGROUND")));
|
|
pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
|
|
|
|
input = InputManager::createInputSystem(pl);
|
|
keyboard = (Keyboard*)input->createInputObject(OISKeyboard,1);
|
|
mouse = (Mouse*)input->createInputObject(OISMouse,1);
|
|
mouse->getMouseState().width = scrW;
|
|
mouse->getMouseState().height = scrH;
|
|
|
|
keyboard->setEventCallback(this);
|
|
mouse->setEventCallback(this);
|
|
keyboard->setTextTranslation(OIS::Keyboard::Unicode);
|
|
}
|
|
|
|
bool KeyPressed(Key key)
|
|
{
|
|
return keyboard->isKeyDown((KeyCode)key);
|
|
}
|
|
bool MouseButtonPressed(LifeCore::MouseKey button)
|
|
{
|
|
MouseButtonID k = (MouseButtonID)(button);
|
|
return mState.buttonDown(k);
|
|
}
|
|
int GetMouseWheel()
|
|
{
|
|
return mState.Z.rel;
|
|
}
|
|
void Capture()
|
|
{
|
|
keyboard->capture();
|
|
mouse->capture();
|
|
|
|
mState = mouse->getMouseState();
|
|
}
|
|
int GetMouseRelativeX()
|
|
{
|
|
return mState.X.rel;
|
|
}
|
|
int GetMouseRelativeY()
|
|
{
|
|
return mState.Y.rel;
|
|
}
|
|
int GetMouseAbsoluteX()
|
|
{
|
|
return mState.X.abs;
|
|
}
|
|
int GetMouseAbsoluteY()
|
|
{
|
|
return mState.Y.abs;
|
|
}
|
|
private:
|
|
IInputSystem* input_system;
|
|
ParamList pl;
|
|
InputManager* input;
|
|
Keyboard* keyboard;
|
|
Mouse* mouse;
|
|
OIS::MouseState mState;
|
|
bool keyPressed( const KeyEvent &arg ){
|
|
//String text = arg.text;
|
|
wchar_t new_key[2];
|
|
new_key[0]= arg.text;
|
|
new_key[1] = L'\0';
|
|
std::wstring str(&new_key[0]);
|
|
std::string res;
|
|
WCSTMBS(str.c_str(), res);
|
|
input_system->OnKeyPress((Key)arg.key,new_key[0], res);
|
|
return true;
|
|
}
|
|
bool keyReleased( const KeyEvent &arg ){
|
|
wchar_t new_key[2];
|
|
new_key[0] = arg.text;
|
|
new_key[1] = L'\0';
|
|
std::wstring str(&new_key[0]);
|
|
std::string res;
|
|
WCSTMBS(str.c_str(), res);
|
|
input_system->OnKeyRelease((Key)arg.key, arg.text, res);
|
|
return true;
|
|
}
|
|
bool mouseMoved( const MouseEvent &arg ) {
|
|
input_system->OnMouseMove(arg.state.X.abs,arg.state.X.rel,arg.state.Y.abs,arg.state.Y.rel);
|
|
return true;
|
|
}
|
|
bool mousePressed( const MouseEvent &arg, MouseButtonID id ){
|
|
input_system->OnMousePress((LifeCore::MouseKey)id);
|
|
return true;
|
|
}
|
|
bool mouseReleased( const MouseEvent &arg, MouseButtonID id ){
|
|
input_system->OnMouseRelease((LifeCore::MouseKey)id);
|
|
return true;
|
|
}
|
|
};
|
|
|
|
InputSystemOIS::InputSystemOIS()
|
|
{
|
|
pImpl = new input_impl(this);
|
|
}
|
|
InputSystemOIS::~InputSystemOIS()
|
|
{
|
|
if(pImpl)
|
|
delete pImpl;
|
|
}
|
|
void InputSystemOIS::Init(int scrW, int scrH, bool exclusiveMode, int hwnd)
|
|
{
|
|
pImpl->Init(scrW, scrH, exclusiveMode, hwnd);
|
|
}
|
|
void InputSystemOIS::Capture()
|
|
{
|
|
pImpl->Capture();
|
|
}
|
|
bool InputSystemOIS::KeyPressed(Key key)
|
|
{
|
|
return pImpl->KeyPressed(key);
|
|
}
|
|
bool InputSystemOIS::MouseButtonPressed(LifeCore::MouseKey key)
|
|
{
|
|
return pImpl->MouseButtonPressed(key);
|
|
}
|
|
int InputSystemOIS::GetMouseWheel()
|
|
{
|
|
return pImpl->GetMouseWheel();
|
|
}
|
|
int InputSystemOIS::GetMouseRelativeX()
|
|
{
|
|
return pImpl->GetMouseRelativeX();
|
|
}
|
|
int InputSystemOIS::GetMouseRelativeY()
|
|
{
|
|
return pImpl->GetMouseRelativeY();
|
|
}
|
|
int InputSystemOIS::GetMouseAbsoluteX()
|
|
{
|
|
return pImpl->GetMouseAbsoluteX();
|
|
}
|
|
int InputSystemOIS::GetMouseAbsoluteY()
|
|
{
|
|
return pImpl->GetMouseAbsoluteY();
|
|
}
|
|
} |