input.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright 2010-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <memory.h>
  6. #include "entry_p.h"
  7. #include "input.h"
  8. #include "cmd.h"
  9. #include <bx/allocator.h>
  10. #include <bx/ringbuffer.h>
  11. #include <tinystl/allocator.h>
  12. #include <tinystl/unordered_map.h>
  13. namespace stl = tinystl;
  14. struct Mouse
  15. {
  16. Mouse()
  17. : m_width(1280)
  18. , m_height(720)
  19. , m_wheelDelta(120)
  20. , m_lock(false)
  21. {
  22. }
  23. void reset()
  24. {
  25. if (m_lock)
  26. {
  27. m_norm[0] = 0.0f;
  28. m_norm[1] = 0.0f;
  29. m_norm[2] = 0.0f;
  30. }
  31. memset(m_buttons, 0, sizeof(m_buttons) );
  32. }
  33. void setResolution(uint16_t _width, uint16_t _height)
  34. {
  35. m_width = _width;
  36. m_height = _height;
  37. }
  38. void setPos(int32_t _mx, int32_t _my, int32_t _mz)
  39. {
  40. m_absolute[0] = _mx;
  41. m_absolute[1] = _my;
  42. m_absolute[2] = _mz;
  43. m_norm[0] = float(_mx)/float(m_width);
  44. m_norm[1] = float(_my)/float(m_height);
  45. m_norm[2] = float(_mz)/float(m_wheelDelta);
  46. }
  47. void setButtonState(entry::MouseButton::Enum _button, uint8_t _state)
  48. {
  49. m_buttons[_button] = _state;
  50. }
  51. int32_t m_absolute[3];
  52. float m_norm[3];
  53. int32_t m_wheel;
  54. uint8_t m_buttons[entry::MouseButton::Count];
  55. uint16_t m_width;
  56. uint16_t m_height;
  57. uint16_t m_wheelDelta;
  58. bool m_lock;
  59. };
  60. struct Keyboard
  61. {
  62. Keyboard()
  63. : m_ring(BX_COUNTOF(m_char) )
  64. {
  65. }
  66. void reset()
  67. {
  68. memset(m_key, 0, sizeof(m_key) );
  69. memset(m_once, 0xff, sizeof(m_once) );
  70. }
  71. static uint32_t encodeKeyState(uint8_t _modifiers, bool _down)
  72. {
  73. uint32_t state = 0;
  74. state |= uint32_t(_modifiers)<<16;
  75. state |= uint32_t(_down)<<8;
  76. return state;
  77. }
  78. static bool decodeKeyState(uint32_t _state, uint8_t& _modifiers)
  79. {
  80. _modifiers = (_state>>16)&0xff;
  81. return 0 != ( (_state>> 8)&0xff);
  82. }
  83. void setKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
  84. {
  85. m_key[_key] = encodeKeyState(_modifiers, _down);
  86. m_once[_key] = false;
  87. }
  88. bool getKeyState(entry::Key::Enum _key, uint8_t* _modifiers)
  89. {
  90. uint8_t modifiers;
  91. _modifiers = NULL == _modifiers ? &modifiers : _modifiers;
  92. return decodeKeyState(m_key[_key], *_modifiers);
  93. }
  94. uint8_t getModifiersState()
  95. {
  96. uint8_t modifiers = 0;
  97. for (uint32_t ii = 0; ii < entry::Key::Count; ++ii)
  98. {
  99. modifiers |= (m_key[ii]>>16)&0xff;
  100. }
  101. return modifiers;
  102. }
  103. void pushChar(uint8_t _len, const uint8_t _char[4])
  104. {
  105. for (uint32_t len = m_ring.reserve(4)
  106. ; len < _len
  107. ; len = m_ring.reserve(4)
  108. )
  109. {
  110. popChar();
  111. }
  112. memcpy(&m_char[m_ring.m_current], _char, 4);
  113. m_ring.commit(4);
  114. }
  115. const uint8_t* popChar()
  116. {
  117. if (0 < m_ring.available() )
  118. {
  119. uint8_t* utf8 = &m_char[m_ring.m_read];
  120. m_ring.consume(4);
  121. return utf8;
  122. }
  123. return NULL;
  124. }
  125. void charFlush()
  126. {
  127. m_ring.m_current = 0;
  128. m_ring.m_write = 0;
  129. m_ring.m_read = 0;
  130. }
  131. uint32_t m_key[256];
  132. bool m_once[256];
  133. bx::RingBufferControl m_ring;
  134. uint8_t m_char[256];
  135. };
  136. struct Gamepad
  137. {
  138. Gamepad()
  139. {
  140. reset();
  141. }
  142. void reset()
  143. {
  144. memset(m_axis, 0, sizeof(m_axis) );
  145. }
  146. void setAxis(entry::GamepadAxis::Enum _axis, int32_t _value)
  147. {
  148. m_axis[_axis] = _value;
  149. }
  150. int32_t getAxis(entry::GamepadAxis::Enum _axis)
  151. {
  152. return m_axis[_axis];
  153. }
  154. int32_t m_axis[entry::GamepadAxis::Count];
  155. };
  156. struct Input
  157. {
  158. Input()
  159. {
  160. reset();
  161. }
  162. ~Input()
  163. {
  164. }
  165. void addBindings(const char* _name, const InputBinding* _bindings)
  166. {
  167. m_inputBindingsMap.insert(stl::make_pair(_name, _bindings) );
  168. }
  169. void removeBindings(const char* _name)
  170. {
  171. InputBindingMap::iterator it = m_inputBindingsMap.find(_name);
  172. if (it != m_inputBindingsMap.end() )
  173. {
  174. m_inputBindingsMap.erase(it);
  175. }
  176. }
  177. void process(const InputBinding* _bindings)
  178. {
  179. for (const InputBinding* binding = _bindings; binding->m_key != entry::Key::None; ++binding)
  180. {
  181. uint8_t modifiers;
  182. bool down = Keyboard::decodeKeyState(m_keyboard.m_key[binding->m_key], modifiers);
  183. if (binding->m_flags == 1)
  184. {
  185. if (down)
  186. {
  187. if (modifiers == binding->m_modifiers
  188. && !m_keyboard.m_once[binding->m_key])
  189. {
  190. if (NULL == binding->m_fn)
  191. {
  192. cmdExec( (const char*)binding->m_userData);
  193. }
  194. else
  195. {
  196. binding->m_fn(binding->m_userData);
  197. }
  198. m_keyboard.m_once[binding->m_key] = true;
  199. }
  200. }
  201. else
  202. {
  203. m_keyboard.m_once[binding->m_key] = false;
  204. }
  205. }
  206. else
  207. {
  208. if (down
  209. && modifiers == binding->m_modifiers)
  210. {
  211. if (NULL == binding->m_fn)
  212. {
  213. cmdExec( (const char*)binding->m_userData);
  214. }
  215. else
  216. {
  217. binding->m_fn(binding->m_userData);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. void process()
  224. {
  225. for (InputBindingMap::const_iterator it = m_inputBindingsMap.begin(); it != m_inputBindingsMap.end(); ++it)
  226. {
  227. process(it->second);
  228. }
  229. }
  230. void reset()
  231. {
  232. m_mouse.reset();
  233. m_keyboard.reset();
  234. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gamepad); ++ii)
  235. {
  236. m_gamepad[ii].reset();
  237. }
  238. }
  239. typedef stl::unordered_map<const char*, const InputBinding*> InputBindingMap;
  240. InputBindingMap m_inputBindingsMap;
  241. Mouse m_mouse;
  242. Keyboard m_keyboard;
  243. Gamepad m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS];
  244. };
  245. static Input* s_input;
  246. void inputInit()
  247. {
  248. s_input = BX_NEW(entry::getAllocator(), Input);
  249. }
  250. void inputShutdown()
  251. {
  252. BX_DELETE(entry::getAllocator(), s_input);
  253. }
  254. void inputAddBindings(const char* _name, const InputBinding* _bindings)
  255. {
  256. s_input->addBindings(_name, _bindings);
  257. }
  258. void inputRemoveBindings(const char* _name)
  259. {
  260. s_input->removeBindings(_name);
  261. }
  262. void inputProcess()
  263. {
  264. s_input->process();
  265. }
  266. void inputSetMouseResolution(uint16_t _width, uint16_t _height)
  267. {
  268. s_input->m_mouse.setResolution(_width, _height);
  269. }
  270. void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
  271. {
  272. s_input->m_keyboard.setKeyState(_key, _modifiers, _down);
  273. }
  274. bool inputGetKeyState(entry::Key::Enum _key, uint8_t* _modifiers)
  275. {
  276. return s_input->m_keyboard.getKeyState(_key, _modifiers);
  277. }
  278. uint8_t inputGetModifiersState()
  279. {
  280. return s_input->m_keyboard.getModifiersState();
  281. }
  282. void inputChar(uint8_t _len, const uint8_t _char[4])
  283. {
  284. s_input->m_keyboard.pushChar(_len, _char);
  285. }
  286. const uint8_t* inputGetChar()
  287. {
  288. return s_input->m_keyboard.popChar();
  289. }
  290. void inputCharFlush()
  291. {
  292. s_input->m_keyboard.charFlush();
  293. }
  294. void inputSetMousePos(int32_t _mx, int32_t _my, int32_t _mz)
  295. {
  296. s_input->m_mouse.setPos(_mx, _my, _mz);
  297. }
  298. void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state)
  299. {
  300. s_input->m_mouse.setButtonState(_button, _state);
  301. }
  302. void inputGetMouse(float _mouse[3])
  303. {
  304. _mouse[0] = s_input->m_mouse.m_norm[0];
  305. _mouse[1] = s_input->m_mouse.m_norm[1];
  306. _mouse[2] = s_input->m_mouse.m_norm[2];
  307. s_input->m_mouse.m_norm[0] = 0.0f;
  308. s_input->m_mouse.m_norm[1] = 0.0f;
  309. s_input->m_mouse.m_norm[2] = 0.0f;
  310. }
  311. bool inputIsMouseLocked()
  312. {
  313. return s_input->m_mouse.m_lock;
  314. }
  315. void inputSetMouseLock(bool _lock)
  316. {
  317. if (s_input->m_mouse.m_lock != _lock)
  318. {
  319. s_input->m_mouse.m_lock = _lock;
  320. entry::WindowHandle defaultWindow = { 0 };
  321. entry::setMouseLock(defaultWindow, _lock);
  322. if (_lock)
  323. {
  324. s_input->m_mouse.m_norm[0] = 0.0f;
  325. s_input->m_mouse.m_norm[1] = 0.0f;
  326. s_input->m_mouse.m_norm[2] = 0.0f;
  327. }
  328. }
  329. }
  330. void inputSetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis, int32_t _value)
  331. {
  332. s_input->m_gamepad[_handle.idx].setAxis(_axis, _value);
  333. }
  334. int32_t inputGetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis)
  335. {
  336. return s_input->m_gamepad[_handle.idx].getAxis(_axis);
  337. }