CmInput.cpp 5.6 KB

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