PolyCoreInput.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyCoreInput.h"
  20. #include "PolyInputEvent.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyCore.h"
  23. namespace Polycode {
  24. JoystickInfo::JoystickInfo() {
  25. for(int i=0; i < 32; i++) {
  26. joystickAxisState[i] = 0.0;
  27. }
  28. for(int i=0; i < 64; i++) {
  29. joystickButtonState[i] = false;
  30. }
  31. }
  32. CoreInput::CoreInput() : EventDispatcher() {
  33. clearInput();
  34. simulateTouchWithMouse = false;
  35. simulateTouchAsPen = false;
  36. simulateMouseWithTouch = false;
  37. ignoreOffScreenTouch = false;
  38. keyRepeat = true;
  39. }
  40. void CoreInput::clearInput() {
  41. mouseButtons[0] = false;
  42. mouseButtons[1] = false;
  43. mouseButtons[2] = false;
  44. for(int i=0; i < 512; i++) {
  45. keyboardState[i] = 0;
  46. }
  47. }
  48. CoreInput::~CoreInput() {
  49. }
  50. unsigned int CoreInput::getNumJoysticks() {
  51. return joysticks.size();
  52. }
  53. JoystickInfo *CoreInput::getJoystickInfoByIndex(unsigned int index) {
  54. if(index > joysticks.size()-1 || joysticks.size() == 0) {
  55. return NULL;
  56. }
  57. return &joysticks[index];
  58. }
  59. bool CoreInput::getJoystickButtonState(int joystickIndex, int button) {
  60. JoystickInfo *info = getJoystickInfoByIndex(joystickIndex);
  61. if(info) {
  62. return info->joystickButtonState[button];
  63. } else {
  64. return false;
  65. }
  66. }
  67. Number CoreInput::getJoystickAxisValue(int joystickIndex, int axis) {
  68. JoystickInfo *info = getJoystickInfoByIndex(joystickIndex);
  69. if(info) {
  70. return info->joystickAxisState[axis];
  71. } else {
  72. return 0.0;
  73. }
  74. }
  75. JoystickInfo *CoreInput::getJoystickInfoByID(unsigned int deviceID) {
  76. for(int i=0;i<joysticks.size();i++) {
  77. if(joysticks[i].deviceID == deviceID) {
  78. joysticks[i].deviceIndex = i;
  79. return &joysticks[i];
  80. }
  81. }
  82. return NULL;
  83. }
  84. void CoreInput::addJoystick(unsigned int deviceID) {
  85. JoystickInfo joystick;
  86. joystick.deviceID = deviceID;
  87. joysticks.push_back(joystick);
  88. InputEvent *evt = new InputEvent();
  89. evt->joystickDeviceID = deviceID;
  90. evt->joystickIndex = joysticks.size()-1;
  91. dispatchEvent(evt, InputEvent::EVENT_JOYDEVICE_ATTACHED);
  92. }
  93. void CoreInput::removeJoystick(unsigned int deviceID) {
  94. for(int i=0;i<joysticks.size();i++) {
  95. if(joysticks[i].deviceID == deviceID) {
  96. joysticks.erase(joysticks.begin()+i);
  97. InputEvent *evt = new InputEvent();
  98. evt->joystickDeviceID = deviceID;
  99. evt->joystickIndex = i;
  100. dispatchEvent(evt, InputEvent::EVENT_JOYDEVICE_DETACHED);
  101. return;
  102. }
  103. }
  104. }
  105. void CoreInput::joystickAxisMoved(unsigned int axisID, float value, unsigned int deviceID) {
  106. JoystickInfo *info = getJoystickInfoByID(deviceID);
  107. if(info) {
  108. info->joystickAxisState[axisID] = value;
  109. InputEvent *evt = new InputEvent();
  110. evt->joystickDeviceID = deviceID;
  111. evt->joystickAxis = axisID;
  112. evt->joystickAxisValue = value;
  113. evt->joystickIndex = info->deviceIndex;
  114. dispatchEvent(evt, InputEvent::EVENT_JOYAXIS_MOVED);
  115. }
  116. }
  117. void CoreInput::joystickButtonDown(unsigned int buttonID, unsigned int deviceID) {
  118. JoystickInfo *info = getJoystickInfoByID(deviceID);
  119. if(info) {
  120. info->joystickButtonState[buttonID] = true;
  121. InputEvent *evt = new InputEvent();
  122. evt->joystickDeviceID = deviceID;
  123. evt->joystickButton = buttonID;
  124. evt->joystickIndex = info->deviceIndex;
  125. dispatchEvent(evt, InputEvent::EVENT_JOYBUTTON_DOWN);
  126. }
  127. }
  128. void CoreInput::joystickButtonUp(unsigned int buttonID, unsigned int deviceID) {
  129. JoystickInfo *info = getJoystickInfoByID(deviceID);
  130. if(info) {
  131. info->joystickButtonState[buttonID] = false;
  132. InputEvent *evt = new InputEvent();
  133. evt->joystickDeviceID = deviceID;
  134. evt->joystickButton = buttonID;
  135. evt->joystickIndex = info->deviceIndex;
  136. dispatchEvent(evt, InputEvent::EVENT_JOYBUTTON_UP);
  137. }
  138. }
  139. bool CoreInput::getMouseButtonState(int mouseButton) {
  140. return mouseButtons[mouseButton];
  141. }
  142. void CoreInput::setMouseButtonState(int mouseButton, bool state, int ticks) {
  143. InputEvent *evt = new InputEvent(mousePosition, ticks);
  144. evt->mouseButton = mouseButton;
  145. if(state)
  146. dispatchEvent(evt, InputEvent::EVENT_MOUSEDOWN);
  147. else
  148. dispatchEvent(evt, InputEvent::EVENT_MOUSEUP);
  149. mouseButtons[mouseButton] = state;
  150. if(simulateTouchWithMouse) {
  151. TouchInfo touch;
  152. touch.position = mousePosition;
  153. touch.id = 0;
  154. if (simulateTouchAsPen){
  155. touch.type = TouchInfo::TYPE_PEN;
  156. }
  157. std::vector<TouchInfo> touches;
  158. touches.push_back(touch);
  159. if(state) {
  160. touchesBegan(touch, touches, ticks);
  161. } else {
  162. touchesEnded(touch, touches, ticks);
  163. }
  164. }
  165. }
  166. void CoreInput::mouseWheelDown(int ticks) {
  167. InputEvent *evt = new InputEvent(mousePosition, ticks);
  168. dispatchEvent(evt, InputEvent::EVENT_MOUSEWHEEL_DOWN);
  169. }
  170. void CoreInput::mouseWheelUp(int ticks) {
  171. InputEvent *evt = new InputEvent(mousePosition, ticks);
  172. dispatchEvent(evt, InputEvent::EVENT_MOUSEWHEEL_UP);
  173. }
  174. void CoreInput::setMousePosition(int x, int y, int ticks) {
  175. mousePosition.x = x;
  176. mousePosition.y = y;
  177. InputEvent *evt = new InputEvent(mousePosition, ticks);
  178. dispatchEvent(evt, InputEvent::EVENT_MOUSEMOVE);
  179. if(simulateTouchWithMouse) {
  180. TouchInfo touch;
  181. touch.position = mousePosition;
  182. touch.id = 0;
  183. if (simulateTouchAsPen){
  184. touch.type = TouchInfo::TYPE_PEN;
  185. }
  186. std::vector<TouchInfo> touches;
  187. touches.push_back(touch);
  188. if(mouseButtons[MOUSE_BUTTON1]) {
  189. touchesMoved(touch, touches, ticks);
  190. }
  191. }
  192. }
  193. Vector2 CoreInput::getMouseDelta() {
  194. return deltaMousePosition;
  195. }
  196. void CoreInput::setDeltaPosition(int x, int y) {
  197. deltaMousePosition.x = (Number)x;
  198. deltaMousePosition.y = (Number)y;
  199. }
  200. Vector2 CoreInput::getMousePosition() {
  201. return mousePosition;
  202. }
  203. bool CoreInput::getKeyState(PolyKEY keyCode) {
  204. if(keyCode < 512)
  205. return keyboardState[keyCode];
  206. else
  207. return false;
  208. }
  209. void CoreInput::setKeyState(PolyKEY keyCode, wchar_t code, bool newState, int ticks) {
  210. if(newState && !keyRepeat) {
  211. if(keyboardState[keyCode]) {
  212. return;
  213. }
  214. }
  215. InputEvent *evt = new InputEvent(keyCode, code, ticks);
  216. if(keyCode < 512)
  217. keyboardState[keyCode] = newState;
  218. if(newState) {
  219. dispatchEvent(evt, InputEvent::EVENT_KEYDOWN);
  220. } else {
  221. dispatchEvent(evt, InputEvent::EVENT_KEYUP);
  222. }
  223. }
  224. void CoreInput::touchesBegan(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
  225. if(ignoreOffScreenTouch) {
  226. Core *core = CoreServices::getInstance()->getCore();
  227. if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
  228. return;
  229. }
  230. }
  231. InputEvent *evt = new InputEvent();
  232. evt->touch = touch;
  233. evt->touches = touches;
  234. evt->timestamp = ticks;
  235. dispatchEvent(evt, InputEvent::EVENT_TOUCHES_BEGAN);
  236. if(simulateMouseWithTouch) {
  237. mousePosition = touch.position;
  238. setMouseButtonState(MOUSE_BUTTON1, true, ticks);
  239. }
  240. }
  241. void CoreInput::touchesMoved(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
  242. if(ignoreOffScreenTouch) {
  243. Core *core = CoreServices::getInstance()->getCore();
  244. if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
  245. return;
  246. }
  247. }
  248. InputEvent *evt = new InputEvent();
  249. evt->touch = touch;
  250. evt->touches = touches;
  251. evt->timestamp = ticks;
  252. dispatchEvent(evt, InputEvent::EVENT_TOUCHES_MOVED);
  253. if(simulateMouseWithTouch) {
  254. setMousePosition(touch.position.x, touch.position.y, ticks);
  255. }
  256. }
  257. void CoreInput::touchesEnded(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
  258. if(ignoreOffScreenTouch) {
  259. Core *core = CoreServices::getInstance()->getCore();
  260. if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
  261. return;
  262. }
  263. }
  264. InputEvent *evt = new InputEvent();
  265. evt->touch = touch;
  266. evt->touches = touches;
  267. evt->timestamp = ticks;
  268. dispatchEvent(evt, InputEvent::EVENT_TOUCHES_ENDED);
  269. if(simulateMouseWithTouch) {
  270. mousePosition = touch.position;
  271. setMouseButtonState(MOUSE_BUTTON1, false, ticks);
  272. }
  273. }
  274. }