Drag and drop in a UI

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
prototypez
Prole
Posts: 14
Joined: Wed Jul 21, 2010 8:17 pm

Drag and drop in a UI

Post by prototypez »

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:

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
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
I love dashes... -- omnomnomnom oishii desu!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Drag and drop in a UI

Post by kikito »

I'm not understanding your code fully, but you are creating a function again and again on each draw call, and that doesn't look very efficient.

I'd recommend creating the function call once (i.e. on object creation) and using the created function on draw. Something similar to this (probably this will not work out-of-the box, you will have to adapt it to what you have)

Code: Select all


function component:create() -- or whatever you call it
  self.draggingitem = function()
    -- special function for dragging the component  
  end
end
...
function component:draw()
   if (self.dragging) then
       uimanager.draggingitem = self.draggingitem -- or something similar
   end
end

Last edited by kikito on Thu Dec 23, 2010 12:40 pm, edited 1 time in total.
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drag and drop in a UI

Post by Robin »

You are asking about upvalues, right?

In Lua, a function can access the local variables in the environment it was created:

Code: Select all

local want = 'ice cream'
function i_want()
    print('I want ' .. want)
end
So in your case, self would refer to the self in the outer function. But kikito is right, his approach is better because it doesn't bind the function every frakking frame (or at least every draw time, I don't know if you are using framebuffers for your GUI).
Help us help you: attach a .love.
User avatar
prototypez
Prole
Posts: 14
Joined: Wed Jul 21, 2010 8:17 pm

Re: Drag and drop in a UI

Post by prototypez »

Upvalues... yes... they seem a bit complicated for a simple language like Lua.
Robin wrote:...his approach is better because it doesn't bind the function every frakking frame (or at least every draw time, I don't know if you are using framebuffers for your GUI).
Yes, thanks for the soft hammer on the head to get jolt that dumb idea from my brain. :ultrahappy:

Actually, I blame these terrible ideas on the corrupting influence of Miner Dig Deep, which I really need to stop playing so I can code more.
I love dashes... -- omnomnomnom oishii desu!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests