PDA

View Full Version : Creating, Cloning, or Writing a Class for dragdata



Minty23185Fresh
February 27th, 2019, 15:06
Has anyone had any success with creating a dragdata object from scratch?

I need one inside a double-click event handler. Some controls in the character sheet have both drag and click functionality, that pretty much end with the same result. It appears as though much of my work would be done if I could populate a dragdata object inside the click handler and send it on its way.

But I have not found any way to create a blank or empty dragdata object. LilCthulhu asked this same question (https://www.fantasygrounds.com/forums/showthread.php?12175-Create-drag-data) back in 2010, and Moon Wizard sent him to throwDice(). I started going down that rabbit hole and it's scary down there.

I also tried draginfo.createBaseData("dragdata"). This seems to be a "cloner". You need an original to clone from, and there is no persistence. When the original dies, the clone dies (becomes empty or non functional).

So, two questions:
(1) After eight years (LilCthulhu's question) can we still not instantiate a fresh dragdata object?
(2) If I write my own class (lua table) with embedded functions and variables that mocks the dragdata object, your opinions on how successful it might be when I send it into the maze of onDragStart/onDrag but originating in onDoubleClick?

Thanks, in advance for your help, opinions, chides :) and gaffaws :).

dulux-oz
February 27th, 2019, 15:20
This is from my DOR Deadlands Classic Ruleset and deals with a "Fate Chip" construct that gets Dragged and Dropped to and from a number of locations.



oDragData.createBaseData("fatechip");
oDragData.setType("fatechip");
oDragData.setIcon("iPokerChip" .. sChip);
oDragData.setDescription(sChip);
oDragData.setNumberData(1);

Minty23185Fresh
February 27th, 2019, 17:12
Thank you dulux-oz.
Interesting. I suspect this rectifies the persistence problem I had. But isn’t oDragData in the code above a dragdata object itself? Meaning I have to have one to create one? Otherwise, if I understand correctly, it would not have the embedded createBaseData() function.

Moon Wizard
February 27th, 2019, 17:29
You can not create a dragdata object from scratch. The createBaseData API creates a child dragdata object, but requires a dragdata object to create.

Regards,
JPG

Minty23185Fresh
February 27th, 2019, 22:19
You can not create a dragdata object from scratch. The createBaseData API creates a child dragdata object, but requires a dragdata object to create.

Waaaah! :mad: That's kinda of what I thought. Thanks Moon Wizard.

MyGivinOpinion
March 7th, 2019, 13:28
Waaaah! :mad: That's kinda of what I thought. Thanks Moon Wizard.

Is it possible to trick the system by code to think that an instance of dragdata was created, so that information from it may be processed?

Minty23185Fresh
March 7th, 2019, 15:14
Is it possible to trick the system by code to think that an instance of dragdata was created, so that information from it may be processed?
It is much more complicated than that.
The dragdata “table” is very much like a class in other languages. It has get and set functions embedded. So code down the line does a
draginfo.setDescription(blah);
rather than a simple
draginfo.desc = blah;
I wil have to add all those property getters and setters to my pseudo-dragdata “object”.

I tried what you suggest, create a dragdata table put in my data, and sent it on its way. It didn’t make it very far before an error was thrown complaining of such-and-such “get” function was nil.

Varsuuk
March 7th, 2019, 19:52
Riiiight... the “duck” principle.

Since the “typing” in Lua is quite coarse, and tables are their “classes”, you could in theory actually recreate a DragInfo class/instance if you implemented it completely, maybe? I’m too ignorant. However as an opaque object it is u likely you’d get enough of it done and done right, in effect Lua would just give you enough rope to shoot yourself in the foot with :)

If I needed it, Id quickly copy and

Minty23185Fresh
March 7th, 2019, 20:28
... shoot yourself in the foot ...
Agreed. I alluded to this in my (unanswered) second question, in the original post.


If I needed it, Id quickly copy and
I think I know where you were headed with this. If I’ve guessed correctly, I also mentioned copying (or cloning) an original and lack of persistence.

Thanks for the input.

I like a good challenge and plan on giving this a go here in the near future. Working on DWSI at the moment though.

MyGivinOpinion
March 9th, 2019, 17:54
Agreed. I alluded to this in my (unanswered) second question, in the original post.


I think I know where you were headed with this. If I’ve guessed correctly, I also mentioned copying (or cloning) an original and lack of persistence.

Thanks for the input.

I like a good challenge and plan on giving this a go here in the near future. Working on DWSI at the moment though.

Please let me know how it goes, I'm currently trying to do something similar with drag data. i'm trying to access it while still performing an onhover

Minty23185Fresh
March 21st, 2019, 06:11
I have a functioning “on demand” pseudo-dragdata object. It uses the genericcontrol as a wrapper. All functionality published in the XML and Scripting Reference document is supported except createBaseData() and one other function. A type(pseudo) will result in “windowcontrol” instead of “dragdata”.
I have a little bit more testing to do and some documentation to think about. I hope to publish in a day or two.

Minty23185Fresh
March 21st, 2019, 19:09
Posted https://www.fantasygrounds.com/forums/showthread.php?48594-On-Demand-(pseudo)-dragdata-Object

celestian
March 21st, 2019, 20:08
I'm curious the use case from your code (the link to the other post). I didn't want to clutter it up there.

It looks like you're coding the drag/drop off a double click? I've recently been tinkering with a custom drag/drop for the "quicknote" system I use for NPCs in AD&D Core. I wanted to be able to drag/drop the quicknotes between the "Ability Notes" and "Weapon" notes. I found a lot of times special weapon/attacks needed an entry and instead of reformatting everything for both, drag/drop was easier.


It ended up like this:

<buttoncontrol>
<script>
...


function onDrop(x, y, draginfo)
if draginfo.isType("shortcut") then
local node = window.getDatabaseNode();
local sClass, sRecord = draginfo.getShortcutData();
if (sClass == "quicknote") then
local nodeEncounter = DB.findNode(sRecord);
if (nodeEncounter) then
local sName = DB.getValue(nodeEncounter,"name","");
local sText = DB.getValue(nodeEncounter,"text","");
local nLocked = DB.getValue(nodeEncounter,"locked",0);
DB.setValue(node,"itemnote.name","string",sName);
DB.setValue(node,"itemnote.text","formattedtext",sText);
DB.setValue(node,"itemnote.locked","number",nLocked);
end
end
return true;
end
end

function onDragStart(button, x, y, draginfo)
local node = window.getDatabaseNode();
local sName = DB.getValue(node,"name","");
local sPathToItem = node.getPath() .. ".itemnote";
draginfo.setType("shortcut");
draginfo.setShortcutData( "quicknote", sPathToItem );
draginfo.setDescription(sName);
draginfo.setIcon("button_link_down");
return true;
end

...
<script>
</buttoncontrol>

This might be completely out of your use case but...