Drag and drop in a UI
Posted: Wed Dec 22, 2010 11:23 pm
I am in the midst of writing a set of reusable user interface components(yes, I know about LoveUI, it's a kind of challenge and I want to use my own licensing, natch) and have been implementing drag and drop. The problem I'm running across is getting the dragged object in the proper z order, and I don't want to create a "new" instance of the object to place in the UI stack. So, my solution so far has been to create a dragged item specific framebuffer and the draw it last, with a flag indicating the component is in a dragging state causing the target component to draw itself with a few modifications to the 'drag' layer, a seperate framebuffer.
My question is, is this wise from a performance standpoint? I haven't seen a big drop in framerate yet, but that is without any game logic at all, but it *does* work.
The other solution I came up with but have not tried is to create a dynamic draw function in the dragging component that renders the component at its drag location and pass that function out to the UI manager to draw last, but as I am unused to Lua's handling of reference passing, I don't know if the variables would retain their value. Here's an example of what I mean:
Then the UI manager would call that function (in a completely seperate context) after everything else is drawn. Do the variables maintain their context where they are defined, or would they get the context of the calling block?
Help is, as always, greatly appreciated
My question is, is this wise from a performance standpoint? I haven't seen a big drop in framerate yet, but that is without any game logic at all, but it *does* work.
The other solution I came up with but have not tried is to create a dynamic draw function in the dragging component that renders the component at its drag location and pass that function out to the UI manager to draw last, but as I am unused to Lua's handling of reference passing, I don't know if the variables would retain their value. Here's an example of what I mean:
Code: Select all
function component:draw()
--normal draw stuff
if (self.dragging) then
uimanager.draggingitem = function()
-- vars in question, what will their value be? A mystery...
local dragitem = self
local dragX, dragY = self.dragX, dragY
love.graphics.draw(dragitem.image, dragX, dragY)
end
end
end
Help is, as always, greatly appreciated