CmInput.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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->onCursorMoved.connect(boost::bind(&Input::cursorMoved, this, _1));
  29. mOSInputHandler->onCursorPressed.connect(boost::bind(&Input::cursorPressed, this, _1));
  30. mOSInputHandler->onCursorReleased.connect(boost::bind(&Input::cursorReleased, this, _1));
  31. RenderWindowManager::instance().onFocusGained.connect(boost::bind(&Input::inputWindowChanged, this, _1));
  32. }
  33. Input::~Input()
  34. {
  35. cm_deleteN(mHorizontalHistoryBuffer, HISTORY_BUFFER_SIZE);
  36. cm_deleteN(mVerticalHistoryBuffer, HISTORY_BUFFER_SIZE);
  37. cm_deleteN(mTimesHistoryBuffer, HISTORY_BUFFER_SIZE);
  38. }
  39. void Input::registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler)
  40. {
  41. if(mRawInputHandler != inputHandler)
  42. {
  43. mRawInputHandler = inputHandler;
  44. if(mRawInputHandler != nullptr)
  45. {
  46. mRawInputHandler->onButtonDown.connect(boost::bind(&Input::buttonDown, this, _1));
  47. mRawInputHandler->onButtonUp.connect(boost::bind(&Input::buttonUp, this, _1));
  48. mRawInputHandler->onAxisMoved.connect(boost::bind(&Input::axisMoved, this, _1, _2));
  49. }
  50. }
  51. }
  52. void Input::update()
  53. {
  54. if(mRawInputHandler == nullptr)
  55. {
  56. LOGERR("Raw input handler not initialized!");
  57. return;
  58. }
  59. else
  60. mRawInputHandler->update();
  61. if(mOSInputHandler == nullptr)
  62. {
  63. LOGERR("OS input handler not initialized!");
  64. return;
  65. }
  66. else
  67. mOSInputHandler->update();
  68. updateSmoothInput();
  69. }
  70. void Input::inputWindowChanged(RenderWindow& win)
  71. {
  72. if(mRawInputHandler != nullptr)
  73. mRawInputHandler->inputWindowChanged(win);
  74. if(mOSInputHandler != nullptr)
  75. mOSInputHandler->inputWindowChanged(win);
  76. }
  77. void Input::buttonDown(ButtonCode code)
  78. {
  79. mKeyState[code & 0x0000FFFF] = true;
  80. if(!onButtonDown.empty())
  81. {
  82. ButtonEvent btnEvent;
  83. btnEvent.buttonCode = code;
  84. onButtonDown(btnEvent);
  85. }
  86. }
  87. void Input::buttonUp(ButtonCode code)
  88. {
  89. mKeyState[code & 0x0000FFFF] = false;
  90. if(!onButtonUp.empty())
  91. {
  92. ButtonEvent btnEvent;
  93. btnEvent.buttonCode = code;
  94. onButtonUp(btnEvent);
  95. }
  96. }
  97. void Input::axisMoved(const RawAxisState& state, RawInputAxis axis)
  98. {
  99. if(axis == RawInputAxis::Mouse_XY)
  100. mMouseLastRel = Int2(-state.rel.x, -state.rel.y);
  101. mAxes[(int)axis] = state;
  102. }
  103. void Input::cursorMoved(const PositionalInputEvent& event)
  104. {
  105. mMouseAbsPos = event.screenPos;
  106. if(!onCursorMoved.empty())
  107. onCursorMoved(event);
  108. }
  109. void Input::cursorPressed(const PositionalInputEvent& event)
  110. {
  111. mMouseAbsPos = event.screenPos;
  112. if(!onCursorPressed.empty())
  113. onCursorPressed(event);
  114. }
  115. void Input::cursorReleased(const PositionalInputEvent& event)
  116. {
  117. mMouseAbsPos = event.screenPos;
  118. if(!onCursorReleased.empty())
  119. onCursorReleased(event);
  120. }
  121. void Input::charInput(UINT32 chr)
  122. {
  123. if(!onCharInput.empty())
  124. {
  125. TextInputEvent textInputEvent;
  126. textInputEvent.textChar = chr;
  127. onCharInput(textInputEvent);
  128. }
  129. }
  130. float Input::getHorizontalAxis() const
  131. {
  132. return mSmoothHorizontalAxis;
  133. }
  134. float Input::getVerticalAxis() const
  135. {
  136. return mSmoothVerticalAxis;
  137. }
  138. bool Input::isButtonDown(ButtonCode button) const
  139. {
  140. return mKeyState[button & 0x0000FFFF];
  141. }
  142. void Input::updateSmoothInput()
  143. {
  144. float currentTime = gTime().getTime();
  145. mHorizontalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.x;
  146. mVerticalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.y;
  147. mTimesHistoryBuffer[mCurrentBufferIdx] = currentTime;
  148. int i = 0;
  149. int idx = mCurrentBufferIdx;
  150. float currentWeight = 1.0f;
  151. float horizontalTotal = 0.0f;
  152. float verticalTotal = 0.0f;
  153. while(i < HISTORY_BUFFER_SIZE)
  154. {
  155. float timeWeight = 1.0f - (currentTime - mTimesHistoryBuffer[idx]) * 10.0f;
  156. if(timeWeight < 0.0f)
  157. timeWeight = 0.0f;
  158. horizontalTotal += mHorizontalHistoryBuffer[idx] * currentWeight * timeWeight;
  159. verticalTotal += mVerticalHistoryBuffer[idx] * currentWeight * timeWeight;
  160. currentWeight *= WEIGHT_MODIFIER;
  161. idx = (idx + 1) % HISTORY_BUFFER_SIZE;
  162. i++;
  163. }
  164. mCurrentBufferIdx = (mCurrentBufferIdx + 1) % HISTORY_BUFFER_SIZE;
  165. mSmoothHorizontalAxis = Math::Clamp(horizontalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  166. mSmoothVerticalAxis = Math::Clamp(verticalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  167. mMouseLastRel = Int2(0, 0);
  168. }
  169. Input& gInput()
  170. {
  171. return Input::instance();
  172. }
  173. }