Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/PickupInventory.lua @ 12412

Last change on this file since 12412 was 11806, checked in by landauf, 7 years ago

migrated all ingame menus to cegui 0.8

  • Property svn:eol-style set to native
File size: 11.9 KB
RevLine 
[5661]1-- PickupInventory.lua
[5559]2
[6746]3local P = createMenuSheet("PickupInventory")
[6711]4
5P.wrapper = nil
6P.detailsWindows = {}
[7163]7P.detailPickups = {}
8P.pickupsList = {}
[5587]9
[7163]10P.showing = false
11
12-- Design parameters
13P.imageHeight = 50
14P.detailImageSize = 100
15P.textHeight = 30
16P.buttonWidth = 85
17
[6746]18function P.onLoad()
[7163]19    P.wrapper = nil
20    P.detailsWindows = {}
21    P.detailPickups = {}
22    P.pickupsList = {}
[6711]23end
[6417]24
[6747]25function P.onShow()
[6711]26    P.createInventory()
[7163]27    P.showing = true
[5559]28end
29
[6747]30function P.onHide()
[7163]31    P.showing = false
32    P.cleanup(true)
[5559]33end
34
[6711]35function P.update()
[7163]36    if P.showing == false then
37        return
38    end
39
40    -- Update opened detail windows.
41    for k,v in pairs(P.detailsWindows) do
42        if v ~= nil then
43            local pickup = P.detailPickups[k]
44            if pickup ~= nil and pickup ~= 0 then
[11806]45                local useButton = P.window:getChild("Details" .. k .. "/Wrapper/UseButton")
46                local dropButton = P.window:getChild("Details" .. k .. "/Wrapper/DropButton")
[7504]47                if orxonox.PickupManager:getInstance():isValidPickup(pickup.pickup) == false then
[7163]48                    useButton:setEnabled(false)
49                    dropButton:setEnabled(false)
50                    P.detailPickups[k] = nil
51                else
52                    useButton:setEnabled(true)
[11710]53                    if pickup.inUse == false then
54                        useButton:setText("use")
[7504]55                        if pickup.usable == false then
[7163]56                            useButton:setEnabled(false)
57                        end
58                    else
[11710]59                        useButton:setText("unuse")
[7504]60                        if pickup.unusable == false then
[7163]61                            useButton:setEnabled(false)
62                        end
63                    end
64
[7504]65                    if pickup.pickedUp == false then
[7163]66                        useButton:setEnabled(false)
67                        dropButton:setEnabled(false)
68                        P.detailPickups[k] = nil
69                    end
70                end
71            end
72        end
73    end
74
75    -- Update main inventory.
76    P.cleanup(false)
77    P.createInventory()
78    -- TODO: Recover scrolling position
[6711]79   
[5559]80end
81
[6711]82function P.createInventory()
83    local pickupManager = orxonox.PickupManager:getInstance()
84   
[11806]85    local root = P.window:getChild("PickupInventory/Wrapper/Inventory")
86    P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "Wrapper")
87    P.wrapper:setSize(CEGUI.USize(CEGUI.UDim(1,0),CEGUI.UDim(1,0)))
88    root:addChild(P.wrapper)
[6711]89   
[7163]90    P.pickupsList = {}
91
92    local numPickups = pickupManager:getNumPickups()
93    local counter = 1
[6711]94    local offset = 0
[7163]95    while counter <= numPickups do
96        local pickup = pickupManager:popPickup()
97        table.insert(P.pickupsList, pickup)
98        local window = P.createPickupEntry(counter, pickup)
[6711]99        window:setYPosition(CEGUI.UDim(0,offset))
[11806]100        offset = offset + P.imageHeight
101        P.wrapper:addChild(window)
[7163]102        counter = counter + 1
[6711]103    end
[7163]104
[6711]105end
[6417]106
[7163]107function P.createPickupEntry(index, pickup)
[9348]108    local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName)
[6417]109
[11806]110    local name = "Pickup" .. index
[6417]111
[7163]112    local item = winMgr:createWindow("MenuWidgets/StaticText", name)
[11806]113    item:setSize(CEGUI.USize(CEGUI.UDim(1, 0), CEGUI.UDim(0, P.imageHeight)))
[7163]114    item:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0)))
[6711]115
[11806]116    local image = winMgr:createWindow("MenuWidgets/StaticImage", "Image")
[11795]117    image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation())
[7163]118    image:setProperty("BackgroundEnabled", "set:False")
119    image:setProperty("FrameEnabled", "set:True")
[11806]120    image:setSize(CEGUI.USize(CEGUI.UDim(0, P.imageHeight), CEGUI.UDim(0, P.imageHeight)))
121    item:addChild(image)
[6711]122
[11806]123    local title = winMgr:createWindow("MenuWidgets/StaticText", "Title")
[7163]124    title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight+5), CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
[11806]125    title:setSize(CEGUI.USize(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight)))
[7163]126    title:setText(representation:getPickupName())
127    title:setProperty("FrameEnabled", "set:False")
[11806]128    item:addChild(title)
[7163]129
[11806]130    local useButton = winMgr:createWindow("MenuWidgets/Button", "UseButton")
[7163]131    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+10),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
[11806]132    useButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
[7504]133    if pickup.inUse == false then
[6711]134        useButton:setText("use")
[7504]135        if pickup.usable == false then
[7163]136            useButton:setEnabled(false)
137        end
138    else
139        useButton:setText("unuse")
[7504]140        if pickup.unusable == false then
[7163]141            useButton:setEnabled(false)
142        end
[6711]143    end
[11710]144    orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseButton_clicked")
[11806]145    item:addChild(useButton)
[7163]146
[11806]147    local dropButton = winMgr:createWindow("MenuWidgets/Button", "DropButton")
[7163]148    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+15+P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
[11806]149    dropButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
[7163]150    dropButton:setText("drop")
151    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropButton_clicked")
[11806]152    item:addChild(dropButton)
[7163]153
[11806]154    local detailsButton = winMgr:createWindow("MenuWidgets/Button", "DetailsButton")
[7163]155    detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+20+2*P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
[11806]156    detailsButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
[7163]157    detailsButton:setText("details")
158    orxonox.GUIManager:subscribeEventHelper(detailsButton, "Clicked", P.name .. ".InventoryDetailsButton_clicked")
[11806]159    item:addChild(detailsButton)
[7163]160
161    return item
[6711]162end
163
[7163]164function P.cleanup(destroyDetails)
[6711]165    if P.wrapper ~= nil then
166        winMgr:destroyWindow(P.wrapper)
167    end
168   
169    --Destroy details windows.
[7163]170    if destroyDetails == false then
171        return
172    end
[6711]173    for k,v in pairs(P.detailsWindows) do
174        if v ~= nil then
[9348]175            P.destroyDetailWindow(k)
[5587]176        end
177    end
178end
179
[7163]180function P.windowToPickupHelper(e)
[6711]181    local we = CEGUI.toWindowEventArgs(e)
[11806]182    local name = we.window:getNamePath()
[6417]183
[6711]184    local match = string.gmatch(name, "%d+")
[7163]185    local pickupIndex = tonumber(match())
[6711]186
[7163]187    return pickupIndex
[5587]188end
189
[7163]190function P.createDetailsWindow(pickupIndex)
191    local pickup = P.pickupsList[pickupIndex]
[9348]192    local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName)
[7163]193
194    local index = P.getNewDetailNumber()
[11806]195    local name = "Details" .. index
[6711]196   
[6750]197    local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
[11806]198    window:setSize(CEGUI.USize(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0)))
[6746]199    orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow")
[6711]200   
[11806]201    P.window:addChild(window)
[6711]202   
[11806]203    local wrapper = winMgr:createWindow("DefaultWindow", "Wrapper")
204    wrapper:setSize(CEGUI.USize(CEGUI.UDim(1, -20),CEGUI.UDim(1, -50)))
[6711]205    wrapper:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10),CEGUI.UDim(0, 40)))
[11806]206    window:addChild(wrapper)
[6711]207   
[11806]208    local title = winMgr:createWindow("MenuWidgets/StaticText", "Title")
[7163]209    title:setText(representation:getPickupName())
210    title:setHeight(CEGUI.UDim(0, P.textHeight))
[6711]211    title:setProperty("FrameEnabled", "set:False")
212    title:setProperty("BackgroundEnabled", "set:False")
[11806]213    wrapper:addChild(title)
[6711]214   
[11806]215    local image = winMgr:createWindow("MenuWidgets/StaticImage", "Image")
[11795]216    image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation())
[6711]217    image:setProperty("BackgroundEnabled", "set:False")
218    image:setProperty("FrameEnabled", "set:True")
[11806]219    image:setSize(CEGUI.USize(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize)))
[7163]220    image:setYPosition(CEGUI.UDim(0, P.textHeight + 5))
[11806]221    wrapper:addChild(image)
[6711]222   
[11806]223    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", "Description")
224    box:setSize(CEGUI.USize(CEGUI.UDim(1.0, -1*(P.detailImageSize + 10)),CEGUI.UDim(1, -(P.textHeight + 5 + P.textHeight + 20))))
[7163]225    box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize + 10),CEGUI.UDim(0, P.textHeight + 5)))
[11806]226    local description = winMgr:createWindow("MenuWidgets/StaticText", "Text")
[7163]227    description:setText(representation:getPickupDescription())
[6711]228    description:setProperty("HorzFormatting", "WordWrapLeftAligned")
229    description:setProperty("VertFormatting", "TopAligned")
[11806]230    box:addChild(description)
231    wrapper:addChild(box)
[7163]232
[11806]233    local useButton = winMgr:createWindow("MenuWidgets/Button", "UseButton")
[7163]234    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10),CEGUI.UDim(1, -40)))
[11806]235    useButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
[7504]236    if pickup.inUse == false then
[7163]237        useButton:setText("use")
[7504]238        if pickup.usable == false then
[7163]239            useButton:setEnabled(false)
240        end
241    else
242        useButton:setText("unuse")
[7504]243        if pickup.unusable == false then
[7163]244            useButton:setEnabled(false)
245        end
246    end
[11710]247    orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseDetailButton_clicked")
[11806]248    wrapper:addChild(useButton)
[6711]249   
[11806]250    local dropButton = winMgr:createWindow("MenuWidgets/Button", "DropButton")
[7163]251    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10+P.buttonWidth+10),CEGUI.UDim(1, -40)))
[11806]252    dropButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
[6711]253    dropButton:setText("drop")
[7163]254    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked")
[11806]255    wrapper:addChild(dropButton)
[7163]256
257    P.detailsWindows[index] = window
258    P.detailPickups[index] = pickup
[6711]259   
260end
261
262function P.getNewDetailNumber()
263    local number = table.getn(P.detailsWindows)
264    for k,v in pairs(P.detailsWindows) do
265        if v == nil then
266            number = k-1
267        end
268    end
[7163]269    return number+1
[6711]270end
271
[11710]272function P.InventoryUseUnuseButton_clicked(e)
[7163]273    local pickupIndex = P.windowToPickupHelper(e)
[7504]274    local pickup = P.pickupsList[pickupIndex]
[11710]275    if pickup.inUse == false then
276        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
277    else
278        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
279    end
[6711]280end
281
282function P.InventoryDropButton_clicked(e)
[7163]283    local pickupIndex = P.windowToPickupHelper(e)
[7504]284    local pickup = P.pickupsList[pickupIndex]
285    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
[6711]286end
287
288function P.InventoryDetailsButton_clicked(e)
[7163]289    local pickupIndex = P.windowToPickupHelper(e)
290    P.createDetailsWindow(pickupIndex)
[6711]291end
292
[11710]293function P.InventoryUseUnuseDetailButton_clicked(e)
[7163]294    local pickupIndex = P.windowToPickupHelper(e)
[7504]295    local pickup = P.detailPickups[pickupIndex]
[11710]296    if pickup.inUse == false then
297        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
298    else
299        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
300    end
[7163]301end
302
303function P.InventoryDropDetailButton_clicked(e)
304    local pickupIndex = P.windowToPickupHelper(e)
[7504]305    local pickup = P.detailPickups[pickupIndex]
306    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
[7163]307end
308
[6711]309function P.closeDetailWindow(e)
310    --Get some numbers from the window
311    local we = CEGUI.toWindowEventArgs(e)
[11806]312    local name = we.window:getNamePath()
[6711]313    local match = string.gmatch(name, "%d+")
314    local detailNr = tonumber(match())
315   
[9348]316    P.destroyDetailWindow(detailNr)
317end
318
319function P.destroyDetailWindow(detailNr)
[7163]320    local window = P.detailsWindows[detailNr]
[6711]321    winMgr:destroyWindow(window)
[7163]322    P.detailsWindows[detailNr] = nil
323    P.detailPickups[detailNr] = nil
[6711]324end
325
326function P.InventoryBackButton_clicked(e)
[6750]327    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
[6711]328end
329
[5661]330return P
Note: See TracBrowser for help on using the repository browser.