PageRenderTime 93ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/jahova/OS/Cores/Input/jahova.os.cores.input.js

https://bitbucket.org/GTL/surveyjs-builder
JavaScript | 663 lines | 526 code | 85 blank | 52 comment | 53 complexity | 255847ca6d088d7ef1d758771b6e91c8 MD5 | raw file
  1. com.jahova.os.Instance().Cores.Instance().Input = (function()
  2. {
  3. var pInstance;
  4. function constructor()
  5. {
  6. //PRIVATE ATTRIBUTES
  7. var NAME = "JaHOVA OS Internal API : Input Core";
  8. var VERSION = "0v5";
  9. var PATH = "scripts/jahova/OS/Cores/Audio/jahova.os.cores.audio.js";
  10. var ID = null;
  11. var os = com.jahova.os.Instance();
  12. var utilities = com.jahova.utilities.Instance();
  13. /*
  14. **/
  15. //PRIVATE METHODS
  16. //Private Classes
  17. var _eventID = 0;
  18. var CGamepad = function(rawGamepad){
  19. //ADD ARRAY THAT HOLDS INDICIES OF RAW GAMEPAD ARRAY
  20. // USE THIS ARRAY TO PULL PROPER INDEX, THEREBY ALLOWING THE
  21. // ARRAY TO BE MODIFIED AND CUSTOMIZE BUTTON SCHEMES
  22. var raw = rawGamepad; /*{
  23. buttons: [],
  24. axes: [],
  25. timestamep: rawGamepad.timestamp
  26. };
  27. raw.buttons = rawGamepad.buttons.slice(0,rawGamepad.buttons.length);
  28. raw.axes = rawGamepad.axes.slice(0,rawGamepad.axes.length);
  29. */
  30. this.debugRaw = raw;
  31. this.id = rawGamepad.index;
  32. var self = this;
  33. this.indicies ={
  34. Dpad: {
  35. up: 12,
  36. down: 13,
  37. left: 14,
  38. right: 15
  39. },
  40. LeftShoulder: {
  41. top: 4,
  42. bottom: 6
  43. },
  44. RightShoulder: {
  45. top: 5,
  46. bottom: 7
  47. },
  48. Buttons: {
  49. top: 3,
  50. bottom: 0,
  51. left: 2,
  52. right: 1
  53. },
  54. LeftStick: {
  55. x: 0,
  56. y: 1,
  57. button: 10
  58. },
  59. RightStick: {
  60. x: 2,
  61. y: 3,
  62. button: 11
  63. },
  64. select: 8,
  65. start: 9
  66. }
  67. this.type = ((rawGamepad.id).toUpperCase()).indexOf("PLAYSTATION") < 0 ? "XBOX" : "PLAYSTATION";
  68. this.Dpad ={
  69. Up: function(){
  70. return raw.buttons[self.indicies.Dpad.up]
  71. },
  72. Down: function(){
  73. return raw.buttons[self.indicies.Dpad.down]
  74. },
  75. Left: function(){
  76. return raw.buttons[self.indicies.Dpad.left]
  77. },
  78. Right: function(){
  79. return raw.buttons[self.indicies.Dpad.right]
  80. }
  81. }
  82. this.LeftShoulder ={
  83. Top: function(){
  84. return raw.buttons[self.indicies.LeftShoulder.top]
  85. },
  86. Bottom: function(){
  87. return raw.buttons[self.indicies.LeftShoulder.bottom]
  88. }
  89. }
  90. this.RightShoulder ={
  91. Top: function(){
  92. return raw.buttons[self.indicies.RightShoulder.top]
  93. },
  94. Bottom: function(){
  95. return raw.buttons[self.indicies.RightShoulder.bottom]
  96. }
  97. }
  98. this.Buttons = {
  99. Top: function(){
  100. return raw.buttons[self.indicies.Buttons.top]
  101. },
  102. Bottom: function(){
  103. return raw.buttons[self.indicies.Buttons.bottom]
  104. },
  105. Left: function(){
  106. return raw.buttons[self.indicies.Buttons.left]
  107. },
  108. Right: function(){
  109. return raw.buttons[self.indicies.Buttons.right]
  110. }
  111. }
  112. this.LeftStick = {
  113. X: function(){
  114. return raw.axes[self.indicies.LeftStick.x]
  115. },
  116. Y: function(){
  117. return raw.axes[self.indicies.LeftStick.y]
  118. },
  119. Button: function(){
  120. return raw.buttons[self.indicies.LeftStick.button]
  121. }
  122. }
  123. this.RightStick ={
  124. X: function(){
  125. return raw.axes[self.indicies.RightStick.x]
  126. },
  127. Y: function(){
  128. return raw.axes[self.indicies.RightStick.y]
  129. },
  130. Button: function(){
  131. return raw.buttons[self.indicies.RightStick.button]
  132. }
  133. }
  134. this.Select = function(){ return raw.buttons[self.indicies.select];}
  135. this.Start = function(){ return raw.buttons[self.indicies.start];}
  136. this.Delete = function(){
  137. os.input.Gamepads.current.remove(this.id);
  138. //os.input.Gamepads.previous.remove(this.id);
  139. }
  140. this.Update = function(rawGPad){
  141. if(this.type != "PLAYSTATION"){
  142. //Default setup is Playstation, so save raw value
  143. //raw.buttons = os.input.Gamepads.Raw.current[this.id].buttons.slice(0,os.input.Gamepads.Raw.current[this.id].buttons.length);
  144. //raw.axes = os.input.Gamepads.Raw.current[this.id].axes.slice(0,os.input.Gamepads.Raw.current[this.id].axes.length);
  145. //raw.timestamp = os.input.Gamepads.Raw.current[this.id].timestamp;
  146. //Convert to XBOX mapping and save
  147. raw = os.input.Gamepads.Raw.current[this.id];
  148. }
  149. }
  150. }
  151. // Extend object by using GamepadEvent['gamepadID'] = Array [CCallback]
  152. var CGamepadEvent = function(){
  153. }
  154. var _InputEvents = {
  155. Mouse: {
  156. Down: function(e){
  157. _Mouse.lastX = e.clientX;
  158. _Mouse.lastY = e.clientY;
  159. _Mouse.pressed = true;
  160. var callbackArray = _MouseEvents.get("DOWN");
  161. if(callbackArray){
  162. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  163. if(callbackArray[i].scope){
  164. callbackArray[i].func.apply(callbackArray.scope, [e]);
  165. }else{
  166. callbackArray[i].func(e);
  167. }
  168. }
  169. }
  170. },
  171. Up: function(e){
  172. _Mouse.pressed = false;
  173. var callbackArray = _MouseEvents.get("UP");
  174. if(callbackArray){
  175. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  176. if(callbackArray[i].scope){
  177. callbackArray[i].func.apply(callbackArray.scope, [e]);
  178. }else{
  179. callbackArray[i].func(e);
  180. }
  181. }
  182. }
  183. },
  184. Move: function(e){
  185. if (!_Mouse.pressed) {
  186. return;
  187. }
  188. var callbackArray = _MouseEvents.get("MOVE");
  189. if(callbackArray){
  190. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  191. if(callbackArray[i].scope){
  192. callbackArray[i].func.apply(callbackArray.scope, [e]);
  193. }else{
  194. callbackArray[i].func(e);
  195. }
  196. }
  197. }
  198. _Mouse.lastX = e.clientX;
  199. _Mouse.lastY = e.clientY;
  200. }
  201. },
  202. Touch: {
  203. Start: function(e){
  204. e.preventDefault();
  205. _Touch.lastX = e.touches[0].pageX;
  206. _Touch.lastY = e.touches[0].pageY;
  207. _Touch.startX = e.touches[0].pageX;
  208. _Touch.startY = e.touches[0].pageY;
  209. _Touch.pressed = true;
  210. var callbackArray = _TouchEvents.get("START");
  211. if(callbackArray){
  212. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  213. if(callbackArray[i].scope){
  214. callbackArray[i].func.apply(callbackArray.scope, [e]);
  215. }else{
  216. callbackArray[i].func(e);
  217. }
  218. }
  219. }
  220. },
  221. End: function(e){
  222. e.preventDefault();
  223. _Touch.pressed = false;
  224. _Touch.startX = 0;
  225. _Touch.startY = 0;
  226. var callbackArray = _TouchEvents.get("END");
  227. if(callbackArray){
  228. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  229. if(callbackArray[i].scope){
  230. callbackArray[i].func.apply(callbackArray.scope, [e]);
  231. }else{
  232. callbackArray[i].func(e);
  233. }
  234. }
  235. }
  236. },
  237. Move: function(e){
  238. e.preventDefault();
  239. if (!_Touch.pressed) {
  240. return;
  241. }
  242. var callbackArray = _TouchEvents.get("MOVE");
  243. if(callbackArray){
  244. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  245. if(callbackArray[i].scope){
  246. callbackArray[i].func.apply(callbackArray.scope, [e]);
  247. }else{
  248. callbackArray[i].func(e);
  249. }
  250. }
  251. }
  252. _Touch.lastX = e.touches[0].pageX;
  253. _Touch.lastY = e.touches[0].pageY;
  254. }
  255. },
  256. Keyboard:{
  257. Keydown: function(e){
  258. var callbackArray = e.keyCode > 47 && e.keyCode < 91 ? _KeyDownEvents.get(String.fromCharCode(e.keyCode)) : _KeyDownControlKeyEvents.get(e.keyCode);
  259. if(callbackArray){
  260. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  261. if(callbackArray[i].scope){
  262. callbackArray[i].func.apply(callbackArray.scope, [e]);
  263. }else{
  264. callbackArray[i].func(e);
  265. }
  266. }
  267. }
  268. },
  269. Keyup: function(e){
  270. var callbackArray = e.keyCode > 47 && e.keyCode < 91 ? _KeyUpEvents.get(String.fromCharCode(e.keyCode)) : _KeyUpControlKeyEvents.get(e.keyCode);
  271. if(callbackArray){
  272. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  273. if(callbackArray[i].scope){
  274. callbackArray[i].func.apply(callbackArray.scope, [e]);
  275. }else{
  276. callbackArray[i].func(e);
  277. }
  278. }
  279. }
  280. }
  281. },
  282. Gamepad: {
  283. Connect: function(e){
  284. var gp = new CGamepad(e);
  285. os.input.Gamepads.current.put(gp.id, gp);
  286. //var pgp = new CGamepad(e);
  287. //os.input.Gamepads.previous.put(pgp.id, pgp);
  288. var callbackArray = _GamepadEvents.get("CONNECT");
  289. if(callbackArray){
  290. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  291. if(callbackArray[i].scope){
  292. callbackArray[i].func.apply(callbackArray.scope, [gp]);
  293. }else{
  294. callbackArray[i].func(gp);
  295. }
  296. }
  297. }
  298. },
  299. Disconnect: function(e){
  300. var gp = os.input.Gamepads.current.get(e.index);
  301. var callbackArray = _GamepadEvents.get("DISCONNECT");
  302. if(callbackArray){
  303. for(var i = callbackArray.length - 1; i >= 0 ; i--){
  304. if(callbackArray[i].scope){
  305. callbackArray[i].func.apply(callbackArray.scope, [gp]);
  306. }else{
  307. callbackArray[i].func(gp);
  308. }
  309. }
  310. }
  311. gp.Delete();
  312. }
  313. }
  314. }
  315. var CCallback = function(iID, fFunction, oScope){
  316. this.func = fFunction;
  317. this.scope = oScope == undefined ? false : oScope;
  318. this.id = iID;
  319. }
  320. //Holds state for Touch Events
  321. var _Touch = {
  322. startX: 0,
  323. startY: 0,
  324. lastX: 0,
  325. lastY: 0,
  326. pressed: false
  327. }
  328. //Holds state of mouse for all registered states
  329. var _Mouse = {
  330. lastX: 0,
  331. lastY: 0,
  332. pressed: false
  333. }
  334. // key: mouseEventType, value: Array[CCallbacks]
  335. var _TouchEvents = null;
  336. // key: mouseEventType, value: Array[CCallbacks]
  337. var _MouseEvents = null;
  338. //Holds states/events of all keys
  339. // key: keyCode, value: true/false
  340. var _KeyboardStates = null;
  341. // key: keyCode, value: Array[CCallbacks];
  342. var _KeyDownEvents = null;
  343. var _KeyUpEvents = null;
  344. var _KeyDownControlKeyEvents = null;
  345. var _KeyUpControlKeyEvents = null;
  346. var _GamepadEvents = null;
  347. return{
  348. //PUBLIC ATTRIBUTES
  349. currentTime: 0,
  350. previousTime: 0,
  351. //PUBLIC PRIVILEDGE METHODS
  352. Update: function(dt){
  353. //Update current and check for new Gamepads
  354. os.input.Gamepads.Update(dt);
  355. },
  356. Get: {
  357. State: {
  358. Mouse: function(){
  359. return _Mouse;
  360. },
  361. Touch: function(){
  362. return _Touch;
  363. },
  364. Keyboard: function(keyCode){
  365. return _KeyboardStates.get(keyCode);
  366. },
  367. Gamepad: function(gamepadID){
  368. return _Gamepads.get(gamepadID);
  369. }
  370. }
  371. },
  372. Register: {
  373. Mouse: {
  374. Event: {
  375. Down: function(fFunction, oScope){
  376. var e = new CCallback(_eventID++, fFunction, oScope);
  377. _MouseEvents.get("DOWN").push(e);
  378. return e;
  379. },
  380. Up: function(fFunction, oScope){
  381. var e = new CCallback(_eventID++, fFunction, oScope);
  382. _MouseEvents.get("UP").push(e);
  383. return e;
  384. },
  385. Move: function(fFunction, oScope){
  386. var e = new CCallback(_eventID++, fFunction, oScope);
  387. _MouseEvents.get("MOVE").push(e);
  388. return e;
  389. }
  390. },
  391. State: {
  392. }
  393. },
  394. Touch: {
  395. Event: {
  396. Start: function(fFunction, oScope){
  397. var e = new CCallback(_eventID++, fFunction, oScope);
  398. _TouchEvents.get("START").push(e);
  399. return e;
  400. },
  401. End: function(fFunction, oScope){
  402. var e = new CCallback(_eventID++, fFunction, oScope);
  403. _TouchEvents.get("END").push(e);
  404. return e;
  405. },
  406. Move: function(fFunction, oScope){
  407. var e = new CCallback(_eventID++, fFunction, oScope);
  408. _TouchEvents.get("MOVE").push(e);
  409. return e;
  410. }
  411. },
  412. State: {
  413. }
  414. },
  415. Keyboard: {
  416. Event: {
  417. Keydown: function(Key, fFunction, oScope){
  418. var e = new CCallback(_eventID++, fFunction, oScope);
  419. var eventArray;
  420. if(isNaN(Key)){
  421. eventArray = _KeyDownControlKeyEvents.get(Key);
  422. }
  423. else{
  424. eventArray = _KeyDownControlKeyEvents.get(Key);
  425. }
  426. if(!eventArray){
  427. eventArray = [];
  428. if(isNaN(Key)){ _KeyDownEvents.put(Key, eventArray);}
  429. else{_KeyDownControlKeyEvents.put(Key, eventArray);}
  430. }
  431. eventArray.push(e);
  432. return e;
  433. },
  434. Keyup: function(Key, fFunction, oScope){
  435. var e = new CCallback(_eventID++, fFunction, oScope);
  436. var eventArray;
  437. if(isNaN(Key)){
  438. eventArray = _KeyUpControlKeyEvents.get(Key);
  439. }
  440. else{
  441. eventArray = _KeyUpControlKeyEvents.get(Key);
  442. }
  443. if(!eventArray){
  444. eventArray = [];
  445. if(isNaN(Key)){ _KeyUpEvents.put(Key, eventArray);}
  446. else{_KeyUpControlKeyEvents.put(Key, eventArray);}
  447. }
  448. eventArray.push(e);
  449. return e;
  450. }
  451. },
  452. State: {
  453. }
  454. },
  455. Gamepad: {
  456. Event: {
  457. Connected: function(fFunction, oScope){
  458. var e = new CCallback(_eventID++, fFunction, oScope);
  459. _GamepadEvents.get("CONNECT").push(e);
  460. return e;
  461. },
  462. Disconnected: function(fFunction, oScope){
  463. var e = new CCallback(_eventID++, fFunction, oScope);
  464. _GamepadEvents.get("DISCONNECT").push(e);
  465. return e;
  466. }
  467. },
  468. State: {
  469. }
  470. }
  471. },
  472. Gamepads: {
  473. Raw: {
  474. current: [undefined, undefined,undefined, undefined],
  475. previous: [undefined, undefined,undefined, undefined]
  476. },
  477. current: null,
  478. previous: null,
  479. Poll: function(dt){
  480. for(var i = 3; i >= 0; i--){
  481. os.input.Gamepads.Raw.previous[i] = os.input.Gamepads.Raw.current[i];
  482. }
  483. // Get the array of gamepads Ð the first method (function call)
  484. // is the most modern one, the second is there for compatibility with
  485. // slightly older versions of Chrome, but it shouldnÕt be necessary
  486. // for long.
  487. os.input.Gamepads.Raw.current =
  488. (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) ||
  489. navigator.webkitGamepads;
  490. if (os.input.Gamepads.Raw.current) {
  491. // We donÕt want to use rawGamepads coming straight from the browser,
  492. // since it can have ÒholesÓ (e.g. if you plug two gamepads, and then
  493. // unplug the first one, the remaining one will be at index [1]).
  494. // We only refresh the display when we detect some gamepads are new
  495. // or removed; we do it by comparing raw gamepad table entries to
  496. // Òundefined.Ó
  497. var gamepadsChanged = false;
  498. for (var i = 3; i >= 0; i--) {
  499. if (typeof os.input.Gamepads.Raw.current[i] != typeof os.input.Gamepads.Raw.previous[i]) {
  500. if(typeof os.input.Gamepads.Raw.previous[i] == "undefined"){
  501. _InputEvents.Gamepad.Connect(os.input.Gamepads.Raw.current[i])
  502. }
  503. else{
  504. _InputEvents.Gamepad.Disconnect(os.input.Gamepads.Raw.previous[i])
  505. }
  506. //os.input.Gamepads.Raw.previous[i] = os.input.Gamepads.Raw.current[i];
  507. }
  508. }
  509. }
  510. },
  511. Update: function(dt){
  512. os.input.Gamepads.Poll(dt);
  513. for(var i = os.input.Gamepads.current.size - 1; i >=0; i--){
  514. os.input.Gamepads.current.value().Update();
  515. os.input.Gamepads.current.next();
  516. }
  517. }
  518. },
  519. GetName: function(){
  520. return NAME;
  521. },
  522. GetVersion: function(){
  523. return VERSION;
  524. },
  525. GetPath: function(){
  526. return PATH;
  527. },
  528. GetID: function(){
  529. return ID;
  530. },
  531. Initialize: function(){
  532. document.addEventListener("touchstart", _InputEvents.Touch.Start, false);
  533. document.addEventListener("touchend", _InputEvents.Touch.End, false);
  534. document.addEventListener("touchmove", _InputEvents.Touch.Move, false);
  535. document.addEventListener("mousedown", _InputEvents.Mouse.Down, false);
  536. document.addEventListener("mouseup", _InputEvents.Mouse.Up, false);
  537. document.addEventListener("mousemove", _InputEvents.Mouse.Move, false);
  538. window.addEventListener("keydown", _InputEvents.Keyboard.Keydown, false);
  539. window.addEventListener("keyup", _InputEvents.Keyboard.Keyup, false);
  540. // key: mouseEventType, value: Array[CCallbacks]
  541. _TouchEvents = os.resschmgr.Create.Map();
  542. _TouchEvents.put("START", []);
  543. _TouchEvents.put("END", []);
  544. _TouchEvents.put("MOVE", []);
  545. // key: mouseEventType, value: Array[CCallbacks]
  546. _MouseEvents = os.resschmgr.Create.Map();
  547. _MouseEvents.put("DOWN", []);
  548. _MouseEvents.put("UP", []);
  549. _MouseEvents.put("MOVE", []);
  550. //Holds states/events of all keys
  551. // key: keyCode, value: true/false
  552. _KeyboardStates = os.resschmgr.Create.Map();
  553. // key: keyCode, value: Array[CCallbacks];
  554. _KeyDownEvents = os.resschmgr.Create.Map();
  555. _KeyUpEvents = os.resschmgr.Create.Map();
  556. _KeyDownControlKeyEvents = os.resschmgr.Create.Map();
  557. _KeyUpControlKeyEvents = os.resschmgr.Create.Map();
  558. _GamepadEvents = os.resschmgr.Create.Map();
  559. pInstance.Gamepads.current = os.resschmgr.Create.Map();
  560. pInstance.Gamepads.previous = os.resschmgr.Create.Map();
  561. _GamepadEvents.put("CONNECT", []);
  562. _GamepadEvents.put("DISCONNECT",[]);
  563. }
  564. }
  565. }
  566. return {
  567. //OBJECT ACCESSOR
  568. Instance: function()
  569. {
  570. if(!pInstance)
  571. {
  572. //Instantiate if pInstance does not exist
  573. pInstance = constructor();
  574. }
  575. return pInstance;
  576. }
  577. }
  578. })();