babylon.cameraInputsManager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var BABYLON;
  2. (function (BABYLON) {
  3. BABYLON.CameraInputTypes = {};
  4. var CameraInputsManager = (function () {
  5. function CameraInputsManager(camera) {
  6. this.attached = {};
  7. this.camera = camera;
  8. this.checkInputs = function () { };
  9. }
  10. CameraInputsManager.prototype.add = function (input) {
  11. var type = input.getSimpleName();
  12. if (this.attached[type]) {
  13. BABYLON.Tools.Warn("camera input of type " + type + " already exists on camera");
  14. return;
  15. }
  16. this.attached[type] = input;
  17. input.camera = this.camera;
  18. //for checkInputs, we are dynamically creating a function
  19. //the goal is to avoid the performance penalty of looping for inputs in the render loop
  20. if (input.checkInputs) {
  21. this.checkInputs = this._addCheckInputs(input.checkInputs.bind(input));
  22. }
  23. if (this.attachedElement) {
  24. input.attachControl(this.attachedElement);
  25. }
  26. };
  27. CameraInputsManager.prototype.remove = function (inputToRemove) {
  28. for (var cam in this.attached) {
  29. var input = this.attached[cam];
  30. if (input === inputToRemove) {
  31. input.detachControl(this.attachedElement);
  32. delete this.attached[cam];
  33. this.rebuildInputCheck();
  34. }
  35. }
  36. };
  37. CameraInputsManager.prototype.removeByType = function (inputType) {
  38. for (var cam in this.attached) {
  39. var input = this.attached[cam];
  40. if (input.getTypeName() === inputType) {
  41. input.detachControl(this.attachedElement);
  42. delete this.attached[cam];
  43. this.rebuildInputCheck();
  44. }
  45. }
  46. };
  47. CameraInputsManager.prototype._addCheckInputs = function (fn) {
  48. var current = this.checkInputs;
  49. return function () {
  50. current();
  51. fn();
  52. };
  53. };
  54. CameraInputsManager.prototype.attachInput = function (input) {
  55. input.attachControl(this.attachedElement, this.noPreventDefault);
  56. };
  57. CameraInputsManager.prototype.attachElement = function (element, noPreventDefault) {
  58. if (this.attachedElement) {
  59. return;
  60. }
  61. noPreventDefault = BABYLON.Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
  62. this.attachedElement = element;
  63. this.noPreventDefault = noPreventDefault;
  64. for (var cam in this.attached) {
  65. var input = this.attached[cam];
  66. this.attached[cam].attachControl(element, noPreventDefault);
  67. }
  68. };
  69. CameraInputsManager.prototype.detachElement = function (element) {
  70. if (this.attachedElement !== element) {
  71. return;
  72. }
  73. for (var cam in this.attached) {
  74. var input = this.attached[cam];
  75. this.attached[cam].detachControl(element);
  76. }
  77. this.attachedElement = null;
  78. };
  79. CameraInputsManager.prototype.rebuildInputCheck = function () {
  80. this.checkInputs = function () { };
  81. for (var cam in this.attached) {
  82. var input = this.attached[cam];
  83. if (input.checkInputs) {
  84. this.checkInputs = this._addCheckInputs(input.checkInputs.bind(input));
  85. }
  86. }
  87. };
  88. CameraInputsManager.prototype.clear = function () {
  89. if (this.attachedElement) {
  90. this.detachElement(this.attachedElement);
  91. }
  92. this.attached = {};
  93. this.attachedElement = null;
  94. this.checkInputs = function () { };
  95. };
  96. CameraInputsManager.prototype.serialize = function (serializedCamera) {
  97. var inputs = {};
  98. for (var cam in this.attached) {
  99. var input = this.attached[cam];
  100. var res = BABYLON.SerializationHelper.Serialize(input);
  101. inputs[input.getTypeName()] = res;
  102. }
  103. serializedCamera.inputsmgr = inputs;
  104. };
  105. CameraInputsManager.prototype.parse = function (parsedCamera) {
  106. var parsedInputs = parsedCamera.inputsmgr;
  107. if (parsedInputs) {
  108. this.clear();
  109. for (var n in parsedInputs) {
  110. var construct = BABYLON.CameraInputTypes[n];
  111. if (construct) {
  112. var parsedinput = parsedInputs[n];
  113. var input = BABYLON.SerializationHelper.Parse(function () { return new construct(); }, parsedinput, null);
  114. this.add(input);
  115. }
  116. }
  117. }
  118. else {
  119. //2016-03-08 this part is for managing backward compatibility
  120. for (var n in this.attached) {
  121. var construct = BABYLON.CameraInputTypes[this.attached[n].getTypeName()];
  122. if (construct) {
  123. var input = BABYLON.SerializationHelper.Parse(function () { return new construct(); }, parsedCamera, null);
  124. this.remove(this.attached[n]);
  125. this.add(input);
  126. }
  127. }
  128. }
  129. };
  130. return CameraInputsManager;
  131. })();
  132. BABYLON.CameraInputsManager = CameraInputsManager;
  133. })(BABYLON || (BABYLON = {}));