Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7480 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 12.7 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")
47                if orxonox.PickupManager:getInstance():isValidPickup(pickup) == false then
48                    useButton:setEnabled(false)
49                    dropButton:setEnabled(false)
50                    P.detailPickups[k] = nil
51                else
52                    useButton:setEnabled(true)
53                    if pickup:isUsed() == true then
54                        useButton:setText("unuse")
55                        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
56                        if pickup:isUsable() == false then
57                            useButton:setEnabled(false)
58                        end
59                    else
60                        useButton:setText("use")
61                        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked")
62                        if pickup:isUnusable() == false then
63                            useButton:setEnabled(false)
64                        end
65                    end
66
67                    if pickup:isPickedUp() == false then
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)
110    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(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)))
135    if pickup:isUsed() == false then
[6711]136        useButton:setText("use")
[6746]137        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
[7163]138        if pickup:isUsable() == false then
139            useButton:setEnabled(false)
140        end
141    else
142        useButton:setText("unuse")
143        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseButton_clicked")
144        if pickup:isUnusable() == false then
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]
195    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup)
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)))
240    if pickup:isUsed() == false then
241        useButton:setText("use")
242        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
243        if pickup:isUsable() == false then
244            useButton:setEnabled(false)
245        end
246    else
247        useButton:setText("unuse")
248        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked")
249        if pickup:isUnusable() == false then
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)
279    orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], true)
[6711]280end
281
[7163]282function P.InventoryUnuseButton_clicked(e)
283    local pickupIndex = P.windowToPickupHelper(e)
284    orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], false)
285end
286
[6711]287function P.InventoryDropButton_clicked(e)
[7163]288    local pickupIndex = P.windowToPickupHelper(e)
289    orxonox.PickupManager:getInstance():dropPickup(P.pickupsList[pickupIndex])
[6711]290end
291
292function P.InventoryDetailsButton_clicked(e)
[7163]293    local pickupIndex = P.windowToPickupHelper(e)
294    P.createDetailsWindow(pickupIndex)
[6711]295end
296
[7163]297function P.InventoryUseDetailButton_clicked(e)
298    local pickupIndex = P.windowToPickupHelper(e)
299    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], true)
300end
301
302function P.InventoryUnuseDetailButton_clicked(e)
303    local pickupIndex = P.windowToPickupHelper(e)
304    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], false)
305end
306
307function P.InventoryDropDetailButton_clicked(e)
308    local pickupIndex = P.windowToPickupHelper(e)
309    orxonox.PickupManager:getInstance():dropPickup(P.detailPickups[pickupIndex])
310end
311
[6711]312function P.closeDetailWindow(e)
313    --Get some numbers from the window
314    local we = CEGUI.toWindowEventArgs(e)
315    local name = we.window:getName()
316    local match = string.gmatch(name, "%d+")
317    local detailNr = tonumber(match())
318   
[7163]319    local window = P.detailsWindows[detailNr]
[6711]320    winMgr:destroyWindow(window)
[7163]321    P.detailsWindows[detailNr] = nil
322    P.detailPickups[detailNr] = nil
[6711]323end
324
325function P.InventoryBackButton_clicked(e)
[6750]326    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
[6711]327end
328
[5661]329return P
Note: See TracBrowser for help on using the repository browser.