CmInput.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "CmInput.h"
  2. #include "CmTime.h"
  3. #include "CmMath.h"
  4. #include "CmRect.h"
  5. #include "CmDebug.h"
  6. #include "CmRenderWindowManager.h"
  7. #include <boost/bind.hpp>
  8. namespace CamelotFramework
  9. {
  10. const int Input::HISTORY_BUFFER_SIZE = 10; // Size of buffer used for input smoothing
  11. const float Input::WEIGHT_MODIFIER = 0.5f;
  12. Input::Input()
  13. :mSmoothHorizontalAxis(0.0f), mSmoothVerticalAxis(0.0f), mCurrentBufferIdx(0), mMouseLastRel(0, 0), mRawInputHandler(nullptr)
  14. {
  15. mHorizontalHistoryBuffer = cm_newN<float>(HISTORY_BUFFER_SIZE);
  16. mVerticalHistoryBuffer = cm_newN<float>(HISTORY_BUFFER_SIZE);
  17. mTimesHistoryBuffer = cm_newN<float>(HISTORY_BUFFER_SIZE);
  18. for(int i = 0; i < HISTORY_BUFFER_SIZE; i++)
  19. {
  20. mHorizontalHistoryBuffer[i] = 0.0f;
  21. mVerticalHistoryBuffer[i] = 0.0f;
  22. mTimesHistoryBuffer[i] = 0.0f;
  23. }
  24. for(int i = 0; i < BC_Count; i++)
  25. mKeyState[i] = false;
  26. mOSInputHandler = cm_shared_ptr<OSInputHandler>();
  27. mOSInputHandler->onCharInput.connect(boost::bind(&Input::charInput, this, _1));
  28. mOSInputHandler->onMouseMoved.connect(boost::bind(&Input::mouseMoved, this, _1));
  29. RenderWindowManager::instance().onFocusGained.connect(boost::bind(&Input::inputWindowChanged, this, _1));
  30. }
  31. Input::~Input()
  32. {
  33. cm_deleteN(mHorizontalHistoryBuffer, HISTORY_BUFFER_SIZE);
  34. cm_deleteN(mVerticalHistoryBuffer, HISTORY_BUFFER_SIZE);
  35. cm_deleteN(mTimesHistoryBuffer, HISTORY_BUFFER_SIZE);
  36. }
  37. void Input::registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler)
  38. {
  39. if(mRawInputHandler != inputHandler)
  40. {
  41. mRawInputHandler = inputHandler;
  42. if(mRawInputHandler != nullptr)
  43. {
  44. mRawInputHandler->onButtonDown.connect(boost::bind(&Input::buttonDown, this, _1));
  45. mRawInputHandler->onButtonUp.connect(boost::bind(&Input::buttonUp, this, _1));
  46. mRawInputHandler->onAxisMoved.connect(boost::bind(&Input::axisMoved, this, _1, _2));
  47. }
  48. }
  49. }
  50. void Input::update()
  51. {
  52. if(mOSInputHandler == nullptr)
  53. {
  54. LOGERR("OS input handler not initialized!");
  55. return;
  56. }
  57. else
  58. mOSInputHandler->update();
  59. if(mRawInputHandler == nullptr)
  60. {
  61. LOGERR("Raw input handler not initialized!");
  62. return;
  63. }
  64. else
  65. mRawInputHandler->update();
  66. updateSmoothInput();
  67. }
  68. void Input::inputWindowChanged(RenderWindow& win)
  69. {
  70. if(mRawInputHandler != nullptr)
  71. mRawInputHandler->inputWindowChanged(win);
  72. if(mOSInputHandler != nullptr)
  73. mOSInputHandler->inputWindowChanged(win);
  74. }
  75. void Input::buttonDown(ButtonCode code)
  76. {
  77. mKeyState[code & 0x0000FFFF] = true;
  78. if(!onKeyDown.empty())
  79. {
  80. ButtonEvent btnEvent;
  81. btnEvent.buttonCode = code;
  82. onKeyDown(btnEvent);
  83. }
  84. }
  85. void Input::buttonUp(ButtonCode code)
  86. {
  87. mKeyState[code & 0x0000FFFF] = false;
  88. if(!onKeyUp.empty())
  89. {
  90. ButtonEvent btnEvent;
  91. btnEvent.buttonCode = code;
  92. onKeyUp(btnEvent);
  93. }
  94. }
  95. void Input::axisMoved(const RawAxisState& state, RawInputAxis axis)
  96. {
  97. if(axis == RawInputAxis::Mouse_XY)
  98. mMouseLastRel = Int2(-state.rel.x, -state.rel.y);
  99. mAxes[(int)axis] = state;
  100. }
  101. void Input::mouseMoved(const Int2& screenPos)
  102. {
  103. mMouseAbsPos = screenPos;
  104. if(!onMouseMoved.empty())
  105. {
  106. MouseEvent mouseEvent;
  107. mouseEvent.screenPos = screenPos;
  108. onMouseMoved(mouseEvent);
  109. }
  110. }
  111. void Input::charInput(UINT32 chr)
  112. {
  113. if(!onCharInput.empty())
  114. {
  115. TextInputEvent textInputEvent;
  116. textInputEvent.textChar = chr;
  117. onCharInput(textInputEvent);
  118. }
  119. }
  120. float Input::getHorizontalAxis() const
  121. {
  122. return mSmoothHorizontalAxis;
  123. }
  124. float Input::getVerticalAxis() const
  125. {
  126. return mSmoothVerticalAxis;
  127. }
  128. bool Input::isButtonDown(ButtonCode button) const
  129. {
  130. return mKeyState[button & 0x0000FFFF];
  131. }
  132. void Input::updateSmoothInput()
  133. {
  134. float currentTime = gTime().getTime();
  135. mHorizontalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.x;
  136. mVerticalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.y;
  137. mTimesHistoryBuffer[mCurrentBufferIdx] = currentTime;
  138. int i = 0;
  139. int idx = mCurrentBufferIdx;
  140. float currentWeight = 1.0f;
  141. float horizontalTotal = 0.0f;
  142. float verticalTotal = 0.0f;
  143. while(i < HISTORY_BUFFER_SIZE)
  144. {
  145. float timeWeight = 1.0f - (currentTime - mTimesHistoryBuffer[idx]) * 10.0f;
  146. if(timeWeight < 0.0f)
  147. timeWeight = 0.0f;
  148. horizontalTotal += mHorizontalHistoryBuffer[idx] * currentWeight * timeWeight;
  149. verticalTotal += mVerticalHistoryBuffer[idx] * currentWeight * timeWeight;
  150. currentWeight *= WEIGHT_MODIFIER;
  151. idx = (idx + 1) % HISTORY_BUFFER_SIZE;
  152. i++;
  153. }
  154. mCurrentBufferIdx = (mCurrentBufferIdx + 1) % HISTORY_BUFFER_SIZE;
  155. mSmoothHorizontalAxis = Math::Clamp(horizontalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  156. mSmoothVerticalAxis = Math::Clamp(verticalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  157. mMouseLastRel = Int2(0, 0);
  158. }
  159. Input& gInput()
  160. {
  161. return Input::instance();
  162. }
  163. }