babylon.cameraInputsManager.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var BABYLON;
  2. (function (BABYLON) {
  3. BABYLON.CameraInputTypes = {};
  4. var CameraInputsManager = (function () {
  5. function CameraInputsManager(camera) {
  6. this.inputs = {};
  7. this.camera = camera;
  8. this.checkInputs = function () { };
  9. }
  10. CameraInputsManager.prototype.add = function (input) {
  11. var type = input.getTypeName();
  12. if (this.inputs[type]) {
  13. BABYLON.Tools.Warn("camera input of type " + type + " already exists on camera");
  14. return;
  15. }
  16. this.inputs[type] = input;
  17. input.attachCamera(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. };
  24. CameraInputsManager.prototype._addCheckInputs = function (fn) {
  25. var current = this.checkInputs;
  26. return function () {
  27. current();
  28. fn();
  29. };
  30. };
  31. CameraInputsManager.prototype.attachElement = function (element, noPreventDefault) {
  32. for (var cam in this.inputs) {
  33. var input = this.inputs[cam];
  34. if (input.attachElement)
  35. this.inputs[cam].attachElement(element, noPreventDefault);
  36. }
  37. };
  38. CameraInputsManager.prototype.detachElement = function (element) {
  39. for (var cam in this.inputs) {
  40. var input = this.inputs[cam];
  41. if (input.detachElement)
  42. this.inputs[cam].detachElement(element);
  43. }
  44. };
  45. CameraInputsManager.prototype.rebuildInputCheck = function (element) {
  46. this.checkInputs = function () { };
  47. for (var cam in this.inputs) {
  48. var input = this.inputs[cam];
  49. if (input.checkInputs) {
  50. this.checkInputs = this._addCheckInputs(input.checkInputs.bind(input));
  51. }
  52. }
  53. };
  54. CameraInputsManager.prototype.clear = function () {
  55. for (var cam in this.inputs) {
  56. this.inputs[cam].detach();
  57. }
  58. this.inputs = {};
  59. this.checkInputs = function () { };
  60. };
  61. CameraInputsManager.prototype.serialize = function () {
  62. var inputs = {};
  63. for (var cam in this.inputs) {
  64. var input = this.inputs[cam];
  65. var res = BABYLON.SerializationHelper.Serialize(input);
  66. inputs[input.getTypeName()] = res;
  67. }
  68. return inputs;
  69. };
  70. CameraInputsManager.prototype.parse = function (parsedInputs) {
  71. if (parsedInputs) {
  72. this.clear();
  73. for (var n in parsedInputs) {
  74. var construct = BABYLON.CameraInputTypes[n];
  75. if (construct) {
  76. var parsedinput = parsedInputs[n];
  77. var input = BABYLON.SerializationHelper.Parse(function () { return new construct(); }, parsedinput, null);
  78. this.add(input);
  79. }
  80. }
  81. }
  82. };
  83. return CameraInputsManager;
  84. }());
  85. BABYLON.CameraInputsManager = CameraInputsManager;
  86. })(BABYLON || (BABYLON = {}));