Index: code/branches/presentation3/data/gui/scripts/PickupInventory.lua
===================================================================
--- code/branches/presentation3/data/gui/scripts/PickupInventory.lua	(revision 6965)
+++ code/branches/presentation3/data/gui/scripts/PickupInventory.lua	(revision 6996)
@@ -3,7 +3,7 @@
 local P = createMenuSheet("PickupInventory")
 
-P.carrierList = {}
 P.wrapper = nil
 P.detailsWindows = {}
+P.detailPickups = {}
 P.pickupsList = {}
 
@@ -17,5 +17,8 @@
 
 function P.onLoad()
-    carrierList = {}
+    P.wrapper = nil
+    P.detailsWindows = {}
+    P.detailPickups = {}
+    P.pickupsList = {}
 end
 
@@ -27,5 +30,5 @@
 function P.onHide()
     P.showing = false
-    P.cleanup()
+    P.cleanup(true)
 end
 
@@ -34,8 +37,41 @@
         return
     end
-    
-    P.cleanup()
-    
+
+    -- Update opened detail windows.
+    for k,v in pairs(P.detailsWindows) do
+        if v ~= nil then
+            local pickup = P.detailPickups[k]
+            if pickup ~= nil and pickup ~= 0 then
+                local useButton = winMgr:getWindow("orxonox/PickupInventory/Details" .. k .. "/UseButton")
+                local dropButton = winMgr:getWindow("orxonox/PickupInventory/Details" .. k .. "/DropButton")
+                if orxonox.PickupManager:getInstance():isValidPickup(pickup) == false then
+                    useButton:setEnabled(false)
+                    dropButton:setEnabled(false)
+                    P.detailPickups[k] = nil
+                else
+                    useButton:setEnabled(true)
+                    if pickup:isUsed() == true then
+                        useButton:setText("unuse")
+                        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
+                    else
+                        useButton:setText("use")
+                        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked")
+                    end
+
+                    if pickup:isPickedUp() == false then
+                        useButton:setEnabled(false)
+                        dropButton:setEnabled(false)
+                        P.detailPickups[k] = nil
+                    end
+                end
+            end
+        end
+    end
+
+    -- Update main inventory.
+    P.cleanup(false)
     P.createInventory()
+    -- TODO: Recover scrolling position
+    
 end
 
@@ -83,5 +119,5 @@
     local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
     title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight+5), CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
-    title:setSize(CEGUI.UVector2(CEGUI.UDim(0.4, 0), CEGUI.UDim(0, P.textHeight)))
+    title:setSize(CEGUI.UVector2(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight)))
     title:setText(representation:getPickupName())
     title:setProperty("FrameEnabled", "set:False")
@@ -89,5 +125,5 @@
 
     local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
-    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, P.imageHeight+10),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
+    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+10),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
     useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
     if pickup:isUsed() == false then
@@ -101,5 +137,5 @@
 
     local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
-    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, P.imageHeight+15+P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
+    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+15+P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
     dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
     dropButton:setText("drop")
@@ -108,5 +144,5 @@
 
     local detailsButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DetailsButton")
-    detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, P.imageHeight+20+2*P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
+    detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+20+2*P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
     detailsButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
     detailsButton:setText("details")
@@ -117,5 +153,5 @@
 end
 
-function P.cleanup()
+function P.cleanup(destroyDetails)
     if P.wrapper ~= nil then
         winMgr:destroyWindow(P.wrapper)
@@ -123,4 +159,7 @@
     
     --Destroy details windows.
+    if destroyDetails == false then
+        return
+    end
     for k,v in pairs(P.detailsWindows) do
         if v ~= nil then
@@ -143,10 +182,7 @@
     local pickup = P.pickupsList[pickupIndex]
     local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup)
-    
-    local headerOffset = 35
-    --Design parameters
-    local imageSize = 100
-    
-    local name = "orxonox/PickupInventory/Pickup" .. pickupIndex .. "/Details" .. P.getNewDetailNumber()
+
+    local index = P.getNewDetailNumber()
+    local name = "orxonox/PickupInventory/Details" .. index
     
     local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
@@ -192,8 +228,8 @@
     if pickup:isUsed() == false then
         useButton:setText("use")
-        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
+        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
     else
         useButton:setText("unuse")
-        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseButton_clicked")
+        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked")
     end
     wrapper:addChildWindow(useButton)
@@ -203,11 +239,13 @@
     dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
     dropButton:setText("drop")
-    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropButton_clicked")
+    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked")
     wrapper:addChildWindow(dropButton)
-    
-    table.insert(P.detailsWindows, window)
-    
-end
-
+
+    P.detailsWindows[index] = window
+    P.detailPickups[index] = pickup
+    
+end
+
+-- TODO: Smarter
 function P.getNewDetailNumber()
     local number = table.getn(P.detailsWindows)
@@ -217,5 +255,5 @@
         end
     end
-    return number
+    return number+1
 end
 
@@ -238,4 +276,19 @@
     local pickupIndex = P.windowToPickupHelper(e)
     P.createDetailsWindow(pickupIndex)
+end
+
+function P.InventoryUseDetailButton_clicked(e)
+    local pickupIndex = P.windowToPickupHelper(e)
+    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], true)
+end
+
+function P.InventoryUnuseDetailButton_clicked(e)
+    local pickupIndex = P.windowToPickupHelper(e)
+    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], false)
+end
+
+function P.InventoryDropDetailButton_clicked(e)
+    local pickupIndex = P.windowToPickupHelper(e)
+    orxonox.PickupManager:getInstance():dropPickup(P.detailPickups[pickupIndex])
 end
 
@@ -245,10 +298,10 @@
     local name = we.window:getName()
     local match = string.gmatch(name, "%d+")
-    local pickupNr = tonumber(match())
     local detailNr = tonumber(match())
     
-    local window = P.detailsWindows[detailNr+1]
+    local window = P.detailsWindows[detailNr]
     winMgr:destroyWindow(window)
-    P.detailsWindows[detailNr+1] = nil
+    P.detailsWindows[detailNr] = nil
+    P.detailPickups[detailNr] = nil
 end
 
