PDA

View Full Version : Change order of items in a window list by Drag and Drop possible?



MadBeardMan
March 16th, 2020, 00:33
Greetings,

I've just added basic Spacecraft data into the Traveller 2E ruleset, you can simply add a line item for a ship detail, ie want to add in the Weapons, click the + and then enter it.

But I would like to be able to change the window order, ie move that newly added weapon line upto the bottom of the existing weapon lines.

Drag and drop would be ace, for some reason my brain tells me there's something out there already, but I just can't think of it.

Cheers,
MBM

darrenan
March 16th, 2020, 00:37
window lists sort on whatever you tell them to. If the underlying items had an item that specified the order, then changing it would change the display order. Think of changing initiative in the CT for instance.

so you could have 'groups', such as the weapon list you were mentioning, have a common 100s or 1000s digit, and use the lower order digits to specify sorting within the group.

MadBeardMan
March 16th, 2020, 00:39
window lists sort on whatever you tell them to. If the underlying items had an item that specified the order, then changing it would change the display order. Think of changing initiative in the CT for instance.

I did think of adding an 'order' value on entry (and then hiding it when not editing), but wondered if there was another way, but what you mention makes me think what I think can work, will work.

So in a very long wordy statement, cheers!

MBM

Trenloe
March 16th, 2020, 07:19
You could base some ordering code on what Darren mentions (a hidden field) and expand on that to have code attached to a drag/drop to calculate the new order by values when an entry is drag/dropped within the list.

celestian
March 16th, 2020, 22:01
I've been wanting to do something with re-ordering for a while but this thread for me thinking last night and I did some proof of concept work.

https://i.imgur.com/6G96T5O.gif

This is the target onDrop() code.



function onDrop(x, y, draginfo)
if draginfo.isType("reorder") then
local sClass, sRecord = draginfo.getShortcutData();
if sClass == "reorder_power" and sRecord ~= "" then
local win = getWindowAt(x,y);
if win then
local nodeActionTarget = win.getDatabaseNode();
local nodeActionSource = DB.findNode(sRecord);
local nOrderSource = DB.getValue(nodeActionSource,"order",0);
local nOrderTarget = DB.getValue(nodeActionTarget,"order",0);
DB.setValue(nodeActionSource,"order","number",nOrderTarget);
DB.setValue(nodeActionTarget,"order","number",nOrderSource);

return true;
end
end
end
end



This is the onDrag start code



function action(draginfo)
local node = window.getDatabaseNode();
draginfo.setDescription(DB.getValue(node,"name",""));
draginfo.setShortcutData("reorder_power",node.getPath())
draginfo.setIcon("ct_select_right");
draginfo.setType("reorder");
return true;
end
function onDragStart(button, x, y, draginfo)
return action(draginfo);
end


Just need to make sure you're working from the same windowlist. For this It was a bit more complicated because it's windowlist within windowlists but once I figure out the right locations it was pretty easy. Just make sure the windowlist sorted by "order".

celestian
March 17th, 2020, 03:03
I've tweaked the onDrop() with some better logic. This will reorder a bit smarter without bloating number values. This presumes that your code sets "order" on a new entry to the last order+1 when added.



function setNewOrder(node,nTarget,nSource)
for _,v in pairs(DB.getChildren(getDatabaseNode(), "")) do
local nCurrent = DB.getValue(v, "order", 0);
if v.getPath() == node.getPath() then
DB.setValue(v, "order", "number", nTarget)
elseif nTarget > nSource and nCurrent > nSource and nCurrent <= nTarget then
DB.setValue(v, "order", "number", nCurrent-1)
elseif nTarget < nSource and nCurrent >= nTarget and nCurrent < nSource then
DB.setValue(v, "order", "number", nCurrent+1)
end
end
end

function onDrop(x, y, draginfo)
if draginfo.isType("reorder") then
local sClass, sRecord = draginfo.getShortcutData();
if sClass == "reorder_power" and sRecord ~= "" then
local win = getWindowAt(x,y);
if win then
local nodeActionTarget = win.getDatabaseNode();
local nodeActionSource = DB.findNode(sRecord);
if nodeActionTarget ~= nodeActionSource then
local nOrderSource = DB.getValue(nodeActionSource,"order",0);
local nOrderTarget = DB.getValue(nodeActionTarget,"order",0);
setNewOrder(nodeActionSource, nOrderTarget, nOrderSource);
end
return true;
end
end
end
end

MadBeardMan
March 18th, 2020, 10:07
I've tweaked the onDrop() with some better logic. This will reorder a bit smarter without bloating number values. This presumes that your code sets "order" on a new entry to the last order+1 when added.



function setNewOrder(node,nTarget,nSource)
for _,v in pairs(DB.getChildren(getDatabaseNode(), "")) do
local nCurrent = DB.getValue(v, "order", 0);
if v.getPath() == node.getPath() then
DB.setValue(v, "order", "number", nTarget)
elseif nTarget > nSource and nCurrent > nSource and nCurrent <= nTarget then
DB.setValue(v, "order", "number", nCurrent-1)
elseif nTarget < nSource and nCurrent >= nTarget and nCurrent < nSource then
DB.setValue(v, "order", "number", nCurrent+1)
end
end
end

function onDrop(x, y, draginfo)
if draginfo.isType("reorder") then
local sClass, sRecord = draginfo.getShortcutData();
if sClass == "reorder_power" and sRecord ~= "" then
local win = getWindowAt(x,y);
if win then
local nodeActionTarget = win.getDatabaseNode();
local nodeActionSource = DB.findNode(sRecord);
if nodeActionTarget ~= nodeActionSource then
local nOrderSource = DB.getValue(nodeActionSource,"order",0);
local nOrderTarget = DB.getValue(nodeActionTarget,"order",0);
setNewOrder(nodeActionSource, nOrderTarget, nOrderSource);
end
return true;
end
end
end
end



Morning Celestian,

That's super awesome of you to have first written it, and second shared it.

I'll make use of this and see what results. I do like your animated example, I should invest some time in attaching pictures to my posts so folks can see it clearer.

Cheers,
MBM

MadBeardMan
March 22nd, 2020, 19:15
Hi @Celestian,

Just a word to say 'Thanks!'. I have this working inside the Traveller 2E ruleset, still pretty clunky but the drag and drop work excellently.

Cheers,
MBM

celestian
March 22nd, 2020, 20:13
Hi @Celestian,

Just a word to say 'Thanks!'. I have this working inside the Traveller 2E ruleset, still pretty clunky but the drag and drop work excellently.

Cheers,
MBM

Glad to hear it ;) I've been making use of it myself. I did notice some very sluggish behavior during a online game on the CT "reorder" but everything else seems to be working good so far.