2
0

CmInput.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "CmInput.h"
  2. #include "CmTime.h"
  3. #include "CmMath.h"
  4. #include "CmRectI.h"
  5. #include "CmDebug.h"
  6. #include "CmRenderWindowManager.h"
  7. using namespace std::placeholders;
  8. namespace BansheeEngine
  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] = ButtonState::Off;
  26. mOSInputHandler = cm_shared_ptr<OSInputHandler>();
  27. mOSInputHandler->onCharInput.connect(std::bind(&Input::charInput, this, _1));
  28. mOSInputHandler->onCursorMoved.connect(std::bind(&Input::cursorMoved, this, _1));
  29. mOSInputHandler->onCursorPressed.connect(std::bind(&Input::cursorPressed, this, _1));
  30. mOSInputHandler->onCursorReleased.connect(std::bind(&Input::cursorReleased, this, _1));
  31. mOSInputHandler->onDoubleClick.connect(std::bind(&Input::cursorDoubleClick, this, _1));
  32. mOSInputHandler->onInputCommand.connect(std::bind(&Input::inputCommandEntered, this, _1));
  33. RenderWindowManager::instance().onFocusGained.connect(std::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(std::bind(&Input::buttonDown, this, _1, _2));
  49. mRawInputHandler->onButtonUp.connect(std::bind(&Input::buttonUp, this, _1, _2));
  50. mRawInputHandler->onAxisMoved.connect(std::bind(&Input::axisMoved, this, _1, _2));
  51. }
  52. }
  53. }
  54. void Input::_update()
  55. {
  56. // Toggle states only remain active for a single frame before they are transitioned
  57. // into permanent state
  58. for(UINT32 i = 0; i < BC_Count; i++)
  59. {
  60. if(mKeyState[i] == ButtonState::ToggledOff)
  61. mKeyState[i] = ButtonState::Off;
  62. else if(mKeyState[i] == ButtonState::ToggledOn)
  63. mKeyState[i] = ButtonState::On;
  64. }
  65. if(mRawInputHandler == nullptr)
  66. {
  67. LOGERR("Raw input handler not initialized!");
  68. return;
  69. }
  70. else
  71. mRawInputHandler->_update();
  72. if(mOSInputHandler == nullptr)
  73. {
  74. LOGERR("OS input handler not initialized!");
  75. return;
  76. }
  77. else
  78. mOSInputHandler->_update();
  79. updateSmoothInput();
  80. }
  81. void Input::inputWindowChanged(RenderWindow& win)
  82. {
  83. if(mRawInputHandler != nullptr)
  84. mRawInputHandler->_inputWindowChanged(win);
  85. if(mOSInputHandler != nullptr)
  86. mOSInputHandler->_inputWindowChanged(win);
  87. }
  88. void Input::buttonDown(ButtonCode code, UINT64 timestamp)
  89. {
  90. mKeyState[code & 0x0000FFFF] = ButtonState::ToggledOn;
  91. if(!onButtonDown.empty())
  92. {
  93. ButtonEvent btnEvent;
  94. btnEvent.buttonCode = code;
  95. btnEvent.timestamp = timestamp;
  96. onButtonDown(btnEvent);
  97. }
  98. }
  99. void Input::buttonUp(ButtonCode code, UINT64 timestamp)
  100. {
  101. mKeyState[code & 0x0000FFFF] = ButtonState::ToggledOff;
  102. if(!onButtonUp.empty())
  103. {
  104. ButtonEvent btnEvent;
  105. btnEvent.buttonCode = code;
  106. btnEvent.timestamp = timestamp;
  107. onButtonUp(btnEvent);
  108. }
  109. }
  110. void Input::axisMoved(const RawAxisState& state, RawInputAxis axis)
  111. {
  112. if(axis == RawInputAxis::Mouse_XY)
  113. mMouseLastRel = Vector2I(-state.rel.x, -state.rel.y);
  114. mAxes[(int)axis] = state;
  115. }
  116. void Input::cursorMoved(const PointerEvent& event)
  117. {
  118. mMouseAbsPos = event.screenPos;
  119. if(!onPointerMoved.empty())
  120. onPointerMoved(event);
  121. }
  122. void Input::cursorPressed(const PointerEvent& event)
  123. {
  124. mMouseAbsPos = event.screenPos;
  125. if(!onPointerPressed.empty())
  126. onPointerPressed(event);
  127. }
  128. void Input::cursorReleased(const PointerEvent& event)
  129. {
  130. mMouseAbsPos = event.screenPos;
  131. if(!onPointerReleased.empty())
  132. onPointerReleased(event);
  133. }
  134. void Input::cursorDoubleClick(const PointerEvent& event)
  135. {
  136. if(!onPointerDoubleClick.empty())
  137. onPointerDoubleClick(event);
  138. }
  139. void Input::inputCommandEntered(InputCommandType commandType)
  140. {
  141. if(!onInputCommand.empty())
  142. onInputCommand(commandType);
  143. }
  144. void Input::charInput(UINT32 chr)
  145. {
  146. if(!onCharInput.empty())
  147. {
  148. TextInputEvent textInputEvent;
  149. textInputEvent.textChar = chr;
  150. onCharInput(textInputEvent);
  151. }
  152. }
  153. float Input::getHorizontalAxis() const
  154. {
  155. return mSmoothHorizontalAxis;
  156. }
  157. float Input::getVerticalAxis() const
  158. {
  159. return mSmoothVerticalAxis;
  160. }
  161. bool Input::isButtonHeld(ButtonCode button) const
  162. {
  163. return mKeyState[button & 0x0000FFFF] == ButtonState::On || mKeyState[button & 0x0000FFFF] == ButtonState::ToggledOn;
  164. }
  165. bool Input::isButtonUp(ButtonCode button) const
  166. {
  167. return mKeyState[button & 0x0000FFFF] == ButtonState::ToggledOff;
  168. }
  169. bool Input::isButtonDown(ButtonCode button) const
  170. {
  171. return mKeyState[button & 0x0000FFFF] == ButtonState::ToggledOn;
  172. }
  173. void Input::updateSmoothInput()
  174. {
  175. float currentTime = gTime().getTime();
  176. mHorizontalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.x;
  177. mVerticalHistoryBuffer[mCurrentBufferIdx] = (float)mMouseLastRel.y;
  178. mTimesHistoryBuffer[mCurrentBufferIdx] = currentTime;
  179. int i = 0;
  180. int idx = mCurrentBufferIdx;
  181. float currentWeight = 1.0f;
  182. float horizontalTotal = 0.0f;
  183. float verticalTotal = 0.0f;
  184. while(i < HISTORY_BUFFER_SIZE)
  185. {
  186. float timeWeight = 1.0f - (currentTime - mTimesHistoryBuffer[idx]) * 10.0f;
  187. if(timeWeight < 0.0f)
  188. timeWeight = 0.0f;
  189. horizontalTotal += mHorizontalHistoryBuffer[idx] * currentWeight * timeWeight;
  190. verticalTotal += mVerticalHistoryBuffer[idx] * currentWeight * timeWeight;
  191. currentWeight *= WEIGHT_MODIFIER;
  192. idx = (idx + 1) % HISTORY_BUFFER_SIZE;
  193. i++;
  194. }
  195. mCurrentBufferIdx = (mCurrentBufferIdx + 1) % HISTORY_BUFFER_SIZE;
  196. mSmoothHorizontalAxis = Math::clamp(horizontalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  197. mSmoothVerticalAxis = Math::clamp(verticalTotal / HISTORY_BUFFER_SIZE, -1.0f, 1.0f);
  198. mMouseLastRel = Vector2I(0, 0);
  199. }
  200. Input& gInput()
  201. {
  202. return Input::instance();
  203. }
  204. }