Trouble passing panel.data through an inline function for mouseCallback
-
I am trying to make my panel data drag and drop more modular by making an inline function for the mouseCallback:
p.setMouseCallback(function(event) { altDragAndDrop(this.data.test); });
inline function altDragAndDrop(dataCopy) { // show "+" cursor when alt/option is pressed local cursor; event.altDown ? cursor = "CopyingCursor" : cursor = "NormalCursor"; this.setMouseCursor(cursor, Colours.white, 1); // alt/option drag copies to clipboard if (event.drag && event.altDown) { Clipboard = dataCopy; } // releasing the mouse on a panel copies the clipboard to the panel, then clears the clipboard if (!event.clicked && event.altDown && dataCopy != Clipboard && Clipboard != undefined) { dataCopy = Clipboard; this.repaint(); Clipboard = undefined; } };
From the mouseCallback, "this" and "event" and my "Clipboard" are all passing into the inline function properly and can be seen in Console printouts:
But my problem is that trying to use the this.data.test passed into the inline function from my panels is only half-working.
It will take that reference and copy the dataCopy (this.data.test) contents (an object) to the Clipboard,
But then it does not allow me to copy Clipboard contents to the dataCopy (this.data.test).Interface:! Line 65, column 12: Cannot assign to this expression!
So a strange thing-
If I just replace the argument name in (dataCopy) with a direct reference this.data.test in that particular spot, the code now works without any errors,
but that defeats the basic convenience of having an inline function. -
@VirtualVirgin Parameters are passed by value, you can't change them within the function.
Minimal example:
reg data = 10; test(data); inline function test(myParam) { myParam = 50; }
It's the same as writing
10 = 50
. -
@d-healey I thought in this case the parameter is the address for the data "this.data.test" being a path, not the actual data.
So I was thinking the path variable is static, but it is just rewriting the contents at that path.
Anyway, I fixed it by using .getValue() and .setValue():p.set("allowCallbacks", "All Callbacks"); p.setMouseCallback(function(event) { altDragAndDrop(); });
inline function altDragAndDrop() { // show "+" cursor when alt/option is pressed local cursor; event.altDown ? cursor = "CopyingCursor" : cursor = "NormalCursor"; this.setMouseCursor(cursor, Colours.white, 1); // alt/option drag copies to clipboard if (event.drag && event.altDown) { Clipboard = this.getValue(); } // releasing the mouse on a panel copies the clipboard to the panel, then clears the clipboard if (!event.clicked && event.altDown && this.getValue() != Clipboard && Clipboard != undefined) { this.setValue(Clipboard); this.repaint(); Clipboard = undefined; } };