Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Cleaned up in PickupInventory, to be able to improve it at a later stage.

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