00001 #include "ois_input_impl.hpp"
00002 #include <utils_exception.hpp>
00003 #include <utils_string.hpp>
00004 #include <OIS.h>
00005
00006 namespace input
00007 {
00008 class ois_key_listener : public OIS::KeyListener
00009 {
00010 ois_handler& mHandler;
00011
00012 public :
00013
00014 ois_key_listener(ois_handler& handler) : mHandler(handler) {}
00015
00016 bool keyPressed(const OIS::KeyEvent& arg) override {
00017 if (arg.text >= 32)
00018 mHandler.lCharsCache_.push_back(arg.text);
00019 return true;
00020 }
00021
00022 bool keyReleased(const OIS::KeyEvent& arg) override {
00023 return true;
00024 }
00025 };
00026
00027 ois_handler::ois_handler(const std::string& sWindowHandle, float fScreenWidth, float fScreenHeight, bool bMouseGrab) :
00028 fScreenWidth_(fScreenWidth), fScreenHeight_(fScreenHeight),
00029 sWindowHandle_(sWindowHandle), bMouseGrab_(bMouseGrab),
00030 fOldMouseX_(-6666.6f), fOldMouseY_(-6666.6f),
00031 pKeyListener_(new ois_key_listener(*this))
00032 {
00033 create_ois_();
00034 }
00035
00036 ois_handler::~ois_handler()
00037 {
00038 delete_ois_();
00039 }
00040
00041 void ois_handler::create_ois_()
00042 {
00043 std::multimap<std::string, std::string> mPL;
00044 mPL.insert(std::make_pair(std::string("WINDOW"), sWindowHandle_));
00045
00046 #ifdef WIN32
00047 mPL.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
00048 if (bMouseGrab_)
00049 mPL.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_EXCLUSIVE")));
00050 else
00051 mPL.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
00052 mPL.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
00053 mPL.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
00054 #else
00055 if (bMouseGrab_)
00056 {
00057 mPL.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("true")));
00058 mPL.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("true")));
00059 }
00060 else
00061 {
00062 mPL.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
00063 mPL.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
00064 }
00065
00066 mPL.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
00067 mPL.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
00068 #endif
00069
00070 pOISInputMgr_ = OIS::InputManager::createInputSystem(mPL);
00071
00072 if (pOISInputMgr_)
00073 {
00074 pKeyboard_ = static_cast<OIS::Keyboard*>(pOISInputMgr_->createInputObject(OIS::OISKeyboard, true));
00075 pKeyboard_->setTextTranslation(OIS::Keyboard::Unicode);
00076 pKeyboard_->setEventCallback(pKeyListener_.get());
00077
00078 pMouse_ = static_cast<OIS::Mouse*>(pOISInputMgr_->createInputObject(OIS::OISMouse, false));
00079
00080
00081
00082
00083 const OIS::MouseState& mMouse = pMouse_->getMouseState();
00084 mMouse.width = fScreenWidth_;
00085 mMouse.height = fScreenHeight_;
00086 }
00087 else
00088 throw utils::exception("ois_input_impl", "Couldn't create OIS input system.");
00089 }
00090
00091 void ois_handler::delete_ois_()
00092 {
00093 pOISInputMgr_->destroyInputObject(pMouse_);
00094 pOISInputMgr_->destroyInputObject(pKeyboard_);
00095 OIS::InputManager::destroyInputSystem(pOISInputMgr_);
00096 }
00097
00098 void ois_handler::toggle_mouse_grab()
00099 {
00100 bMouseGrab_ = !bMouseGrab_;
00101 delete_ois_();
00102 fOldMouseX_ = fOldMouseY_ = -6666.6f;
00103 create_ois_();
00104 }
00105
00106 void ois_handler::update()
00107 {
00108 pKeyboard_->capture();
00109
00110 char lTempBuff[key::K_MAXKEY];
00111 pKeyboard_->copyKeyStates(lTempBuff);
00112
00113 for (int i = 0; i < key::K_MAXKEY; ++i)
00114 mKeyboard.lKeyState[i] = (lTempBuff[i] != 0);
00115
00116 pMouse_->capture();
00117
00118 OIS::MouseState mMouseState = pMouse_->getMouseState();
00119 mMouse.fAbsX = mMouseState.X.abs;
00120 mMouse.fAbsY = mMouseState.Y.abs;
00121 mMouse.fRelX = mMouseState.X.abs/fScreenWidth_;
00122 mMouse.fRelY = mMouseState.Y.abs/fScreenHeight_;
00123
00124 mMouse.bHasDelta = true;
00125 if (fOldMouseX_ != -6666.6f || fOldMouseY_ != -6666.6f)
00126 {
00127 mMouse.fDX = mMouse.fAbsX - fOldMouseX_;
00128 mMouse.fDY = mMouse.fAbsY - fOldMouseY_;
00129 mMouse.fRelDX = mMouse.fRelX - fOldMouseX_/fScreenWidth_;
00130 mMouse.fRelDY = mMouse.fRelY - fOldMouseY_/fScreenHeight_;
00131 }
00132 else
00133 mMouse.fDX = mMouse.fDY = mMouse.fRelDX = mMouse.fRelDY = 0.0f;
00134
00135 fOldMouseX_ = mMouse.fAbsX;
00136 fOldMouseY_ = mMouse.fAbsY;
00137
00138
00139
00140
00141 mMouse.fRelWheel = mMouseState.Z.rel/120.0f;
00142
00143 for (int i = 0; i < INPUT_MOUSE_BUTTON_NUMBER; ++i)
00144 mMouse.lButtonState[i] = mMouseState.buttonDown((OIS::MouseButtonID)i);
00145 }
00146 }