trinMouse.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. function TrinMouse() {
  2. this.x = 0;
  3. this.y = 0;
  4. this.current = 0;
  5. this.last = 0;
  6. this.out = false;
  7. this.rawX = 0;
  8. this.rawY = 0;
  9. }
  10. TrinMouse.prototype = {
  11. update: function() {
  12. this.x = Math.min(Math.floor(this.rawX), _TrinGame.width);
  13. this.y = Math.min(Math.floor(this.rawY), _TrinGame.height);
  14. if (this.current === -1) {
  15. this.current = 0;
  16. } else if (this.current === 2) {
  17. this.current = 1;
  18. }
  19. this.last = this.current;
  20. },
  21. isDown: function() {
  22. return this.current > 0;
  23. },
  24. isPressed: function() {
  25. return this.current === 2;
  26. },
  27. isReleased: function() {
  28. return this.current === -1;
  29. },
  30. onMouseDown: function() {
  31. var mouse = arguments.callee.mouse;
  32. mouse.current = (mouse.current > 0) ? 1 : 2;
  33. },
  34. onMouseUp: function() {
  35. var mouse = arguments.callee.mouse;
  36. mouse.current = (mouse.current > 0) ? -1 : 0;
  37. },
  38. onTouchStart: function(event) {
  39. var mouse = arguments.callee.mouse;
  40. if (_TrinGame.isAndroid) {
  41. event.preventDefault();
  42. mouse.rawX = mouse.x = (event.changedTouches[0].pageX + event.layerX) / _TrinGame.scaleFactor;
  43. mouse.rawY = mouse.y = (event.changedTouches[0].pageY + event.layerY) / _TrinGame.scaleFactor;
  44. } else {
  45. mouse.rawX = mouse.x = event.layerX / _TrinGame.scaleFactor;
  46. mouse.rawY = mouse.y = event.layerY / _TrinGame.scaleFactor;
  47. }
  48. mouse.current = (mouse.current > 0) ? 1 : 2;
  49. return false;
  50. },
  51. onTouchEnd: function() {
  52. var mouse = arguments.callee.mouse;
  53. mouse.current = (mouse.current > 0) ? -1 : 0;
  54. mouse.rawX = -999;
  55. mouse.rawY = -999;
  56. return false;
  57. },
  58. reset: function() {
  59. this.current = 0;
  60. }
  61. };