Drag and Drop for VGUI

From Garry's Mod
Jump to: navigation, search

Drag 'N' Drop

This is a drag n drop tutorial since it barely documented.

It's very easy, all you need to have is have two panels; receiver and droppable panel.

local receiver = vgui.Create('DPanel')
receiver:SetSize(ScrW()/2,ScrH()/2)

local droppable = vgui.Create('DPanel')
droppable:SetSize(100,100)

Now we need to assign receiver and droppable functions for the panels

receiver:Receiver('name',function(receiver, tableOfDroppedPanels, isDropped, menuIndex, mouseX, mouseY) end,{})
droppable:Droppable('name')

Receiver metamethod takes three arguments, first one being the name of the DnD so it can receiver all droppable panels that have the same name as the receiver, callback function which gets executed as soon as a droppable panel is hovered over the receiver (if you want to detect it as soon as it was dropped, use isDropped argument) and last one is a table of strings that will act as a menu if drag'n'drop was performed with a right click.

Use the callback function for your stuff, setting position of the dropped panel or whatever you wanted to do.

Check Panel:Receiver for more information about the receiver metamethod.

The droppable method only takes one argument and it's the name of the receiver you want to drop it on.

More information at Panel:Droppable

Now you're able to drop droppable onto receiver.

Personal tools
Navigation