ControlManager.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ControlManager.js
  2. // ControlManager for mouse and keyboard
  3. // FZ, Copyright (c) 2010 Zlongames
  4. (function()
  5. {
  6. FZ.ArrayIndexof = function(arr, obj) {
  7. var index = 0;
  8. for(index = 0; index < arr.length; index++) {
  9. if(obj === arr[index]) {
  10. return index;
  11. }
  12. }
  13. return -1;
  14. };
  15. FZ.ControlManager=
  16. {
  17. m_monitor_list : [],
  18. Mouse:{x:0,y:0,leftKey:false,middleKey:false,rightKey:false},
  19. MouseMove:function(e)
  20. {
  21. if(e.offsetX) {
  22. this.Mouse.x = e.offsetX;
  23. this.Mouse.y = e.offsetY;
  24. }
  25. else if(e.layerX) {
  26. this.Mouse.x = e.layerX - FZ.GameBase.MainCanvas.offsetLeft;
  27. this.Mouse.y = e.layerY - FZ.GameBase.MainCanvas.offsetTop;
  28. }
  29. },
  30. MouseUp:function(e)
  31. {
  32. if (e.button !== 0) return;
  33. this.Mouse.leftKey = false;
  34. for(var index = 0; index < this.m_monitor_list.length; index++) {
  35. var obj = this.m_monitor_list[index];
  36. if((obj) && (obj.CustomMouseUp)) {
  37. obj.CustomMouseUp(this.Mouse);
  38. }
  39. }
  40. // if (this.CustomMouseUp)
  41. // this.CustomMouseUp(e);
  42. },
  43. MouseDown:function(e)
  44. {
  45. if (e.button !== 0) return;
  46. this.Mouse.leftKey = true;
  47. for(var index = 0; index < this.m_monitor_list.length; index++) {
  48. var obj = this.m_monitor_list[index];
  49. if((obj) && (obj.CustomMouseDown)) {
  50. obj.CustomMouseDown(this.Mouse);
  51. }
  52. }
  53. // if (this.CustomMouseDown)
  54. // this.CustomMouseDown(e);
  55. },
  56. addMonitor : function(obj) {
  57. var index = FZ.ArrayIndexof(this.m_monitor_list, obj);
  58. if(-1 === index) {
  59. this.m_monitor_list.push(obj);
  60. }
  61. },
  62. removeMonitor : function(obj) {
  63. var index = FZ.ArrayIndexof(this.m_monitor_list, obj);
  64. if(-1 !== index) {
  65. this.m_monitor_list.splice(index, 1);
  66. }
  67. },
  68. clearMonitor : function() {
  69. while(this.m_monitor_list.length > 0) {
  70. this.m_monitor_list.pop();
  71. }
  72. }
  73. };
  74. })();