Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/data/gui/scripts/PickupInventory.lua @ 11634

Last change on this file since 11634 was 6773, checked in by rgrieder, 15 years ago

Eliminated all unnecessary global Lua variables and replaced them either with a local or a instance variable (P.myVar).

  • Property svn:eol-style set to native
File size: 11.0 KB
RevLine 
[5661]1-- PickupInventory.lua
[5559]2
[6746]3local P = createMenuSheet("PickupInventory")
[6711]4
5P.carrierList = {}
6P.wrapper = nil
7P.detailsWindows = {}
[5587]8
[6746]9function P.onLoad()
[6711]10end
[6417]11
[6747]12function P.onShow()
[6711]13    P.createInventory()
[5559]14end
15
[6747]16function P.onHide()
[6711]17    P.cleanup()
[5559]18end
19
[6711]20function P.update()
21    P.cleanup()
22   
23    P.createInventory()
[5559]24end
25
[6711]26function P.createInventory()
27    local pickupManager = orxonox.PickupManager:getInstance()
28    local carrier = pickupManager:getPawn()
29   
30    local root = winMgr:getWindow("orxonox/PickupInventory/Inventory")
[6750]31    P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper")
[6711]32    P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0)))
33    root:addChildWindow(P.wrapper)
34   
35    P.carrierList = {}
36   
37    --Design parameters:
38    local space = 15
39   
40    P.getCarrierList(carrier)
41    local offset = 0
42    for k,v in pairs(P.carrierList) do
43        local window = P.createCarrierBox(v,k)
44        window:setYPosition(CEGUI.UDim(0,offset))
45        offset = offset + window:getHeight():asAbsolute(1) + space
46        P.wrapper:addChildWindow(window)
47    end
48end
[6417]49
[6711]50function P.getCarrierList(carrier)
[6417]51
[6711]52    -- TODO: Test for nil or 0?
53    if carrier == nil then
54        return
[5587]55    end
[6711]56   
57    table.insert(P.carrierList, carrier)
58   
59    local numCarriers = orxonox.PickupManager:getInstance():getNumCarrierChildren(carrier)
60    if numCarriers == 0 then
61        return
62    end
63   
64    for i=0,numCarriers-1,1 do
65        local child = orxonox.PickupManager:getInstance():getCarrierChild(i, carrier)
66        if child ~= nil then
67            P.getCarrierList(child)
68        end
69    end
70end
[6417]71
[6711]72function P.createCarrierBox(carrier, index)
73
74    local name = "orxonox/PickupInventory/Carrier" .. index
75       
76    --Design parameters:
77    local imageHeight = 50
78    local textHeight = 30
79    local horizontalOffset = 20
80    local buttonWidth = 85
81   
82    local offset = 0
83
[6750]84    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Box")
[6711]85    box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, horizontalOffset), CEGUI.UDim(0, 0)))
86    box:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, -horizontalOffset), CEGUI.UDim(1, 0)))
87   
88    offset = offset+textHeight
[6750]89    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
[6711]90    title:setText(carrier:getCarrierName())
91    title:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(0, offset)))
92    title:setProperty("FrameEnabled", "set:False")
93    box:addChildWindow(title)
94   
95    local numPickups = orxonox.PickupManager:getInstance():getNumPickups(carrier)
96    for i=0,numPickups-1,1 do
97        local pickup = orxonox.PickupManager:getInstance():getPickupRepresentation(i, carrier)
98       
[6750]99        local item = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Box/Pickup" .. i)
[6711]100        item:setSize(CEGUI.UVector2(CEGUI.UDim(1, -horizontalOffset), CEGUI.UDim(0, imageHeight)))
101        item:setPosition(CEGUI.UVector2(CEGUI.UDim(0, horizontalOffset), CEGUI.UDim(0, offset)))
102        box:addChildWindow(item)
103        offset = offset + imageHeight+5
104       
[6750]105        local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Box/Pickup" .. i .. "/Image")
[6711]106        image:setProperty("Image", "set:PickupInventory image:" .. pickup:getInventoryRepresentation())
107        image:setProperty("BackgroundEnabled", "set:False")
108        image:setProperty("FrameEnabled", "set:True")
109        image:setSize(CEGUI.UVector2(CEGUI.UDim(0, imageHeight), CEGUI.UDim(0, imageHeight)))
110        item:addChildWindow(image)
111       
[6750]112        local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Box/Pickup" .. i .. "/Title")
[6711]113        title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, imageHeight+5), CEGUI.UDim(0, (imageHeight-textHeight)/2)))
114        title:setSize(CEGUI.UVector2(CEGUI.UDim(0.4, 0), CEGUI.UDim(0, textHeight)))
115        title:setText(pickup:getPickupName())
116        title:setProperty("FrameEnabled", "set:False")
117        item:addChildWindow(title)
118       
[6750]119        local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/Box/Pickup" .. i .. "/UseButton")
[6711]120        useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, imageHeight+10),CEGUI.UDim(0, (imageHeight-textHeight)/2)))
121        useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0, textHeight)))
122        useButton:setText("use")
[6746]123        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
[6711]124        item:addChildWindow(useButton)
125       
[6750]126        local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/Box/Pickup" .. i .. "/DropButton")
[6711]127        dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, imageHeight+15+buttonWidth),CEGUI.UDim(0, (imageHeight-textHeight)/2)))
128        dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0, textHeight)))
129        dropButton:setText("drop")
[6746]130        orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropButton_clicked")
[6711]131        item:addChildWindow(dropButton)
132       
[6750]133        local detailsButton = winMgr:createWindow("MenuWidgets/Button", name .. "/Box/Pickup" .. i .. "/DetailsButton")
[6711]134        detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.4, imageHeight+20+2*buttonWidth),CEGUI.UDim(0, (imageHeight-textHeight)/2)))
135        detailsButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0, textHeight)))
136        detailsButton:setText("details")
[6746]137        orxonox.GUIManager:subscribeEventHelper(detailsButton, "Clicked", P.name .. ".InventoryDetailsButton_clicked")
[6711]138        item:addChildWindow(detailsButton)
139    end
140   
141    box:setHeight(CEGUI.UDim(0,offset))
142   
143    return box
144end
145
146function P.cleanup()
147    if P.wrapper ~= nil then
148        winMgr:destroyWindow(P.wrapper)
149    end
150   
151    --Destroy details windows.
152    for k,v in pairs(P.detailsWindows) do
153        if v ~= nil then
154            winMgr:destroyWindow(v)
[5587]155        end
156    end
157end
158
[6711]159function P.windowToCarrierHelper(e)
160    local we = CEGUI.toWindowEventArgs(e)
161    local name = we.window:getName()
[6417]162
[6711]163    local match = string.gmatch(name, "%d+")
164    local carrierNr = tonumber(match())
165    local pickupNr = tonumber(match())
166
167    local arguments = {}
168    arguments[1] = carrierNr
169    arguments[2] = pickupNr
170    return arguments
[5587]171end
172
[6711]173function P.createDetailsWindow(pickupIndex, carrierIndex)
174    local carrier = P.carrierList[carrierIndex]
175    local pickup = orxonox.PickupManager:getInstance():getPickupRepresentation(pickupIndex, carrier)
176   
177    local headerOffset = 35
178    --Design parameters
179    local titleHeight = 30
180    local imageSize = 100
181    local buttonWidth = 85
182   
183    local name = "orxonox/PickupInventory/Carrier" .. carrierIndex .. "/Pickup" .. pickupIndex .. "/Details" .. P.getNewDetailNumber()
184   
[6750]185    local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
[6711]186    window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0)))
[6746]187    orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow")
[6711]188   
189    local root = winMgr:getWindow("orxonox/PickupInventory/Background")
190    root:addChildWindow(window)
191   
192    local wrapper = winMgr:createWindow("DefaultWindow", name .. "/Wrapper")
193    wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1, -20),CEGUI.UDim(1, -50)))
194    wrapper:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10),CEGUI.UDim(0, 40)))
195    window:addChildWindow(wrapper)
196   
[6750]197    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
[6711]198    title:setText(pickup:getPickupName())
199    title:setHeight(CEGUI.UDim(0, titleHeight))
200    title:setProperty("FrameEnabled", "set:False")
201    title:setProperty("BackgroundEnabled", "set:False")
202    wrapper:addChildWindow(title)
203   
[6750]204    local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image")
[6711]205    image:setProperty("Image", "set:PickupInventory image:" .. pickup:getInventoryRepresentation())
206    image:setProperty("BackgroundEnabled", "set:False")
207    image:setProperty("FrameEnabled", "set:True")
208    image:setSize(CEGUI.UVector2(CEGUI.UDim(0, imageSize), CEGUI.UDim(0, imageSize)))
209    image:setYPosition(CEGUI.UDim(0, titleHeight + 5))
210    wrapper:addChildWindow(image)
211   
[6750]212    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description")
[6711]213    box:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, -1*(imageSize + 10)),CEGUI.UDim(1, -(titleHeight + 5 + titleHeight + 20))))
214    box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, imageSize + 10),CEGUI.UDim(0, titleHeight + 5)))
215    local description = winMgr:createWindow("TaharezLook/StaticText", name .. "/Description/Text")
216    description:setText(pickup:getPickupDescription())
217    description:setProperty("HorzFormatting", "WordWrapLeftAligned")
218    description:setProperty("VertFormatting", "TopAligned")
219    box:addChildWindow(description)
220    wrapper:addChildWindow(box)
221   
[6750]222    local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
[6711]223    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, imageSize+10),CEGUI.UDim(1, -40)))
224    useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0, titleHeight)))
225    useButton:setText("use")
[6746]226    orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
[6711]227    wrapper:addChildWindow(useButton)
228   
[6750]229    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
[6711]230    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, imageSize+10+buttonWidth+10),CEGUI.UDim(1, -40)))
231    dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0, titleHeight)))
232    dropButton:setText("drop")
[6746]233    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropButton_clicked")
[6711]234    wrapper:addChildWindow(dropButton)
235   
236    table.insert(P.detailsWindows, window)
237   
238end
239
240function P.getNewDetailNumber()
241    local number = table.getn(P.detailsWindows)
242    for k,v in pairs(P.detailsWindows) do
243        if v == nil then
244            number = k-1
245        end
246    end
247    return number
248end
249
250function P.InventoryUseButton_clicked(e)
251    local arguments = P.windowToCarrierHelper(e)
252    orxonox.PickupManager:getInstance():usePickup(arguments[2], P.carrierList[arguments[1]], true)
253end
254
255function P.InventoryDropButton_clicked(e)
256    local arguments = P.windowToCarrierHelper(e)
257    orxonox.PickupManager:getInstance():dropPickup(arguments[2], P.carrierList[arguments[1]])
258end
259
260function P.InventoryDetailsButton_clicked(e)
261    local arguments = P.windowToCarrierHelper(e)
262    P.createDetailsWindow(arguments[2], arguments[1])
263end
264
265function P.closeDetailWindow(e)
266    --Get some numbers from the window
267    local we = CEGUI.toWindowEventArgs(e)
268    local name = we.window:getName()
269    local match = string.gmatch(name, "%d+")
270    local carrierNr = tonumber(match())
271    local pickupNr = tonumber(match())
272    local detailNr = tonumber(match())
273   
274    local window = P.detailsWindows[detailNr+1]
275    winMgr:destroyWindow(window)
276    P.detailsWindows[detailNr+1] = nil
277end
278
279function P.InventoryBackButton_clicked(e)
[6750]280    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
[6711]281end
282
[5661]283return P
Note: See TracBrowser for help on using the repository browser.