CmInput.cpp 5.9 KB

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