1. Listview & Inventory Items
This serves exclusively for using the listview to display “inventory items”.
The first question is, what are those items? Here’s some common item types:
-Structure holding the data
-Name, pointing at a data table that holds the data
-Object or Actor.
The Listview needs an Object to handle it as one of its Items, so if your inventory is data only, then you may have to create objects just to pass them to the Listview just to act as mediators.
In the case of using a data table, the EntryWidget could pull the data from the Data Table.
If your items are Objects, they can just be passed to the listview. But still there may be another more practical option.
And that option is to have Mediator objects for each item slot. This should work for constant-sized inventories and would account for empty slots. Think Minecraft or Diablo (though this will not be about having one Item occupy several slots).
So here’s an example of mediator objects acting as pointers to the inventory slot they represent.
2. Drag & Drop solution with mediators
Here in the entry widget I am creating a drag drop operation.
The result is this:

The dragged element is smaller. That is because the TileView has a size setting for its tiles, default to 128x128. Then the behavior for widgets with a border and text is to be as small as possible. To have the dragged element be the same size, unfortunately we have to use a SizeBox wrapping the border.
The SizeBox Height and width should be = Entry height and width - Entry Spacing.
For the On Drop function, the idea is to get the payload index and the index it is dropped on. At first I was setting up this logic in the On Drop function of the EntryWidget. But really, isn’t it better to have the character [or whatever class holds your inventory] always be informed about a drop and let it decide what should happen?
The entry widget does not know about the character, but the mediator does. So you have two options here. You can either tell the mediator to tell the character to inform about the drop, so that the character can swap the items. If you do this, don’t forget to then tell the Listview to regenerate entries, ie to update that the widgets display the correct item. The upside to this is that you can make a function with a return value, so that you can do fancy things with the Entry Widget depending on the outcome of the drop.
OR
Have a dispatcher in the Entrywidget that the Inventory Widget is bound to, which is what is shown here.
The Inventory Widget knows about the character and can tell it that a drop happened, or it can have a dispatcher of its own that the character listens to.
Above is really just a long winded way of getting the payload index and the index of the dropped on mediator.
With a dispatcher, you can still inform the Entry Widget about the outcome of the drop, but then the dispatcher has to provide the entry widget reference and you need to implement some functions to call.
If the Entry Widget is going to have a binding, we have to bind to it when it is generated.
Now for the actual item swap query and logic.
- If the item is dropped on itself, nothing should happen.
- If the item is dropped on an empty slot, set that slot to the item and empty the payload (where it came from)
- if the item is dropped on an occupied slot, swap them by having a helper variable hold item A, as item B is set to occupy A, then set the origin of Item B to equal Item A.
The reason for the return boolean, is that if there’s a change, the listview need to regenerate its entries. Try not regenerating and you’ll see that visually nothing happens.
One note is that while we can drag items around, we can also drag empty slots around. To fix that, you can’t add an additional query in the character function - Inventory Drag Drop, to see if Inventory[PayloadIndex] == None. Because then you’d still be able to drag around an empty slot. Haven’t seen any games do that. So that has to be queried before we create the Drag Drop Operation. The Mediator Object gets a new function returning if its index has an item or not.
Do note that with this type of inventory, you never delete members of the inventory array. You empty the array members that should be empty. If you delete members, you’ll start getting errors since the mediators can’t access index members that does not exist.
– End of drag and drop with mediators–
An alternative option to having mediators for your inventory items is to have the inventory slots be Objects. So instead of an array of Items or structs, you’d have an array of Inventory Slot Objects. These Objects holds the items or structs, and are the ones passed to the listview.
Here’s the concept:
In a game like Diablo, you can have a single item on a slot, or a stack of pots on a slot.
Or in a game like Kenshi, food can be stacked despite them having various amount of consumed. By having a single slot being able to hold several items, you can have item stacks where each item of the stack is an instance.
You just have to query when using drag and drop that the items are of the same type.






