input.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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. void pushChar(uint8_t _len, const uint8_t _char[4])
  95. {
  96. for (uint32_t len = m_ring.reserve(4)
  97. ; len < _len
  98. ; len = m_ring.reserve(4)
  99. )
  100. {
  101. popChar();
  102. }
  103. memcpy(&m_char[m_ring.m_current], _char, 4);
  104. m_ring.commit(4);
  105. }
  106. const uint8_t* popChar()
  107. {
  108. if (0 < m_ring.available() )
  109. {
  110. uint8_t* utf8 = &m_char[m_ring.m_read];
  111. m_ring.consume(4);
  112. return utf8;
  113. }
  114. return NULL;
  115. }
  116. void charFlush()
  117. {
  118. m_ring.m_current = 0;
  119. m_ring.m_write = 0;
  120. m_ring.m_read = 0;
  121. }
  122. uint32_t m_key[256];
  123. bool m_once[256];
  124. bx::RingBufferControl m_ring;
  125. uint8_t m_char[256];
  126. };
  127. struct Gamepad
  128. {
  129. Gamepad()
  130. {
  131. reset();
  132. }
  133. void reset()
  134. {
  135. memset(m_axis, 0, sizeof(m_axis) );
  136. }
  137. void setAxis(entry::GamepadAxis::Enum _axis, int32_t _value)
  138. {
  139. m_axis[_axis] = _value;
  140. }
  141. int32_t getAxis(entry::GamepadAxis::Enum _axis)
  142. {
  143. return m_axis[_axis];
  144. }
  145. int32_t m_axis[entry::GamepadAxis::Count];
  146. };
  147. struct Input
  148. {
  149. Input()
  150. {
  151. reset();
  152. }
  153. ~Input()
  154. {
  155. }
  156. void addBindings(const char* _name, const InputBinding* _bindings)
  157. {
  158. m_inputBindingsMap.insert(stl::make_pair(_name, _bindings) );
  159. }
  160. void removeBindings(const char* _name)
  161. {
  162. InputBindingMap::iterator it = m_inputBindingsMap.find(_name);
  163. if (it != m_inputBindingsMap.end() )
  164. {
  165. m_inputBindingsMap.erase(it);
  166. }
  167. }
  168. void process(const InputBinding* _bindings)
  169. {
  170. for (const InputBinding* binding = _bindings; binding->m_key != entry::Key::None; ++binding)
  171. {
  172. uint8_t modifiers;
  173. bool down = Keyboard::decodeKeyState(m_keyboard.m_key[binding->m_key], modifiers);
  174. if (binding->m_flags == 1)
  175. {
  176. if (down)
  177. {
  178. if (modifiers == binding->m_modifiers
  179. && !m_keyboard.m_once[binding->m_key])
  180. {
  181. if (NULL == binding->m_fn)
  182. {
  183. cmdExec( (const char*)binding->m_userData);
  184. }
  185. else
  186. {
  187. binding->m_fn(binding->m_userData);
  188. }
  189. m_keyboard.m_once[binding->m_key] = true;
  190. }
  191. }
  192. else
  193. {
  194. m_keyboard.m_once[binding->m_key] = false;
  195. }
  196. }
  197. else
  198. {
  199. if (down
  200. && modifiers == binding->m_modifiers)
  201. {
  202. if (NULL == binding->m_fn)
  203. {
  204. cmdExec( (const char*)binding->m_userData);
  205. }
  206. else
  207. {
  208. binding->m_fn(binding->m_userData);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. void process()
  215. {
  216. for (InputBindingMap::const_iterator it = m_inputBindingsMap.begin(); it != m_inputBindingsMap.end(); ++it)
  217. {
  218. process(it->second);
  219. }
  220. }
  221. void reset()
  222. {
  223. m_mouse.reset();
  224. m_keyboard.reset();
  225. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gamepad); ++ii)
  226. {
  227. m_gamepad[ii].reset();
  228. }
  229. }
  230. typedef stl::unordered_map<const char*, const InputBinding*> InputBindingMap;
  231. InputBindingMap m_inputBindingsMap;
  232. Mouse m_mouse;
  233. Keyboard m_keyboard;
  234. Gamepad m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS];
  235. };
  236. static Input* s_input;
  237. void inputInit()
  238. {
  239. s_input = BX_NEW(entry::getAllocator(), Input);
  240. }
  241. void inputShutdown()
  242. {
  243. BX_DELETE(entry::getAllocator(), s_input);
  244. }
  245. void inputAddBindings(const char* _name, const InputBinding* _bindings)
  246. {
  247. s_input->addBindings(_name, _bindings);
  248. }
  249. void inputRemoveBindings(const char* _name)
  250. {
  251. s_input->removeBindings(_name);
  252. }
  253. void inputProcess()
  254. {
  255. s_input->process();
  256. }
  257. void inputSetMouseResolution(uint16_t _width, uint16_t _height)
  258. {
  259. s_input->m_mouse.setResolution(_width, _height);
  260. }
  261. void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
  262. {
  263. s_input->m_keyboard.setKeyState(_key, _modifiers, _down);
  264. }
  265. bool inputGetKeyState(entry::Key::Enum _key, uint8_t* _modifiers)
  266. {
  267. return s_input->m_keyboard.getKeyState(_key, _modifiers);
  268. }
  269. void inputChar(uint8_t _len, const uint8_t _char[4])
  270. {
  271. s_input->m_keyboard.pushChar(_len, _char);
  272. }
  273. const uint8_t* inputGetChar()
  274. {
  275. return s_input->m_keyboard.popChar();
  276. }
  277. void inputCharFlush()
  278. {
  279. s_input->m_keyboard.charFlush();
  280. }
  281. void inputSetMousePos(int32_t _mx, int32_t _my, int32_t _mz)
  282. {
  283. s_input->m_mouse.setPos(_mx, _my, _mz);
  284. }
  285. void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state)
  286. {
  287. s_input->m_mouse.setButtonState(_button, _state);
  288. }
  289. void inputGetMouse(float _mouse[3])
  290. {
  291. _mouse[0] = s_input->m_mouse.m_norm[0];
  292. _mouse[1] = s_input->m_mouse.m_norm[1];
  293. _mouse[2] = s_input->m_mouse.m_norm[2];
  294. s_input->m_mouse.m_norm[0] = 0.0f;
  295. s_input->m_mouse.m_norm[1] = 0.0f;
  296. s_input->m_mouse.m_norm[2] = 0.0f;
  297. }
  298. bool inputIsMouseLocked()
  299. {
  300. return s_input->m_mouse.m_lock;
  301. }
  302. void inputSetMouseLock(bool _lock)
  303. {
  304. if (s_input->m_mouse.m_lock != _lock)
  305. {
  306. s_input->m_mouse.m_lock = _lock;
  307. entry::WindowHandle defaultWindow = { 0 };
  308. entry::setMouseLock(defaultWindow, _lock);
  309. if (_lock)
  310. {
  311. s_input->m_mouse.m_norm[0] = 0.0f;
  312. s_input->m_mouse.m_norm[1] = 0.0f;
  313. s_input->m_mouse.m_norm[2] = 0.0f;
  314. }
  315. }
  316. }
  317. void inputSetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis, int32_t _value)
  318. {
  319. s_input->m_gamepad[_handle.idx].setAxis(_axis, _value);
  320. }
  321. int32_t inputGetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis)
  322. {
  323. return s_input->m_gamepad[_handle.idx].getAxis(_axis);
  324. }