Transfer data onObservable

Hi,

I want to transfer data from a scaleGizmo.dragBehavior.onDragStartObservable to a scaleGizmo.dragBehavior.onDragEndObservable or others like this. Same as HTML when you drag something you can set DataTransfer.setData().

Does it possible and how can i do ?

Tanks by advance.

Hello! From what I understand, the DataTransfer API is more related to files, which has nothing to do with the drag behaviors in Babylon. Can you elaborate a bit more on what you want to do? :slight_smile:

By data, you mean JS objects?

I would do it with a var variable defined at a higher scope than the observable. In other words, a global var.

To illustrate, on classic JS :

document.getElementById("divToDrag").addEventListener("dragstart", function(event) => {
   event.dataTransfer.setData("text/plain", event.target.id);  // "Save" in the event the dragged div id
}, false);

document.getElementById("divToDrop").addEventListener("drop", function(event) => {
  const draggedDivID = event.dataTransfer.getData("text"); // Get the data "saved" in the event
}, false);

And my question is, there is a way to do that in BABYLON for the scaleGizmo drag events ?
For example :

let scaleGizmo = new BABYLON.AxisScaleGizmo(new BABYLON.Vector3(0, 1, 0), BABYLON.Color3.FromHexString("#00ff00"), utilLayer);
scaleGizmo.dragBehavior.onDragStartObservable.add(event => {
  event.tansferMetaData = myData; // "Save" data in the event
});
scaleGizmo.dragBehavior.onDragEndObservable.add(event => {
  const myDataTransfered = event.transferMetaData; // Get data "saved" in the event
});

No, the event us instanciated per call.
I would simply do:

var dataTransfer;
let scaleGizmo = new BABYLON.AxisScaleGizmo(new BABYLON.Vector3(0, 1, 0), BABYLON.Color3.FromHexString("#00ff00"), utilLayer);
scaleGizmo.dragBehavior.onDragStartObservable.add(event => {
  dataTransfer = myData; // "Save" data in the event
});
scaleGizmo.dragBehavior.onDragEndObservable.add(event => {
  const myDataTransfered = dataTransfer;
});

yes, I had already thought about this solution but I don’t find it clean, I work in class and for me adding a this.dataTransfer is not the best, but never mind, thanks for the help :slight_smile:

PS: do you plan to add this feature one day?

No, but we are open for a PR if you want to add the feature.

2 Likes