simpleinteractionhelper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var Sandbox;
  2. (function (Sandbox) {
  3. var PointerEventTypes = BABYLON.PointerEventTypes;
  4. var AbstractMesh = BABYLON.AbstractMesh;
  5. /**
  6. * The purpose of this class is to allow the camera manipulation, single node selection and manipulation.
  7. * You can use it as an example to create your more complexe/different interaction helper
  8. */
  9. var SimpleInteractionHelper = (function () {
  10. function SimpleInteractionHelper(scene) {
  11. var _this = this;
  12. this._actionStack = new Array();
  13. this._scene = scene;
  14. this._pointerObserver = this._scene.onPointerObservable.add(function (p, s) { return _this.pointerCallback(p, s); }, -1, true);
  15. }
  16. Object.defineProperty(SimpleInteractionHelper.prototype, "currentAction", {
  17. get: function () {
  18. if (this._actionStack.length === 0) {
  19. return 1 /* Selector */;
  20. }
  21. return this._actionStack[this._actionStack.length - 1];
  22. },
  23. enumerable: true,
  24. configurable: true
  25. });
  26. Object.defineProperty(SimpleInteractionHelper.prototype, "manipulator", {
  27. get: function () {
  28. if (!this._manipulator) {
  29. this._manipulator = new Sandbox.ManipulatorInteractionHelper(this._scene);
  30. }
  31. return this._manipulator;
  32. },
  33. enumerable: true,
  34. configurable: true
  35. });
  36. SimpleInteractionHelper.prototype.pointerCallback = function (p, s) {
  37. this.detectActionChanged(p, s);
  38. switch (this.currentAction) {
  39. case 1 /* Selector */:
  40. this.doSelectorInteraction(p, s);
  41. break;
  42. case 2 /* Camerator */:
  43. if (p.type & (PointerEventTypes.POINTERUP | PointerEventTypes.POINTERWHEEL)) {
  44. this._actionStack.pop();
  45. }
  46. break;
  47. }
  48. };
  49. SimpleInteractionHelper.prototype.doSelectorInteraction = function (p, s) {
  50. s.skipNextObservers = true;
  51. // We want left button up.
  52. if (p.type !== PointerEventTypes.POINTERUP || p.event.button !== 0) {
  53. return;
  54. }
  55. var selectedMesh;
  56. if (p.pickInfo.hit) {
  57. selectedMesh = p.pickInfo.pickedMesh;
  58. }
  59. // We selected the same mesh? nothing to do
  60. if (this._pickedNode === selectedMesh) {
  61. selectedMesh.showBoundingBox = !selectedMesh.showBoundingBox;
  62. if (selectedMesh.showBoundingBox === false) {
  63. this.manipulator.detachManipulatedNode(this._pickedNode);
  64. this._pickedNode = null;
  65. }
  66. return;
  67. }
  68. // Detach the manipulator to the current selected mesh
  69. if (this._pickedNode) {
  70. if (this._pickedNode instanceof AbstractMesh) {
  71. var mesh = this._pickedNode;
  72. mesh.showBoundingBox = false;
  73. }
  74. this.manipulator.detachManipulatedNode(this._pickedNode);
  75. this._pickedNode = null;
  76. }
  77. // Nothing selected, our job's done
  78. if (!selectedMesh) {
  79. return;
  80. }
  81. this._pickedNode = selectedMesh;
  82. selectedMesh.showBoundingBox = true;
  83. this.manipulator.attachManipulatedNode(this._pickedNode);
  84. };
  85. SimpleInteractionHelper.prototype.detectActionChanged = function (p, s) {
  86. // Detect switch from selection to camerator
  87. if (this.currentAction === 1 /* Selector */) {
  88. if (p.type === PointerEventTypes.POINTERDOWN) {
  89. if (!p.pickInfo.hit) {
  90. this._actionStack.push(2 /* Camerator */);
  91. return;
  92. }
  93. }
  94. if (p.type === PointerEventTypes.POINTERWHEEL) {
  95. this._actionStack.push(2 /* Camerator */);
  96. return;
  97. }
  98. }
  99. };
  100. SimpleInteractionHelper.CameratorSwitchThreshold = 4.0;
  101. return SimpleInteractionHelper;
  102. }());
  103. Sandbox.SimpleInteractionHelper = SimpleInteractionHelper;
  104. })(Sandbox || (Sandbox = {}));
  105. //# sourceMappingURL=simpleinteractionhelper.js.map