Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/data/gui/scripts/PickupInventory.lua @ 7020

Last change on this file since 7020 was 6996, checked in by dafrick, 15 years ago

Loads of changes.
1) PickupInventory should now be working even for extreme cases.
2) Added support for inactive Spawnpoints in Gametype.
3) Made Pickupable rewardble. meaning from now on any Pickupable can be given as a reward for completing Quests.
4) Added some keybinds to KeybindMenu, such as PickupInventory, QuestGUI and Chat.

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