Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7669 was 7504, checked in by dafrick, 14 years ago

Pickups module is now (from what I can tell after some basic testing) fully functional over the network.
However it's still a little messy, needs some cleanup and documentation.
I introduced a new class, the PickupListener, which allows reacting to pickups becoming used, unused, picked up or dropped.

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