Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/data/gui/scripts/PickupInventory.lua @ 8577

Last change on this file since 8577 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
Line 
1-- PickupInventory.lua
2
3local P = createMenuSheet("PickupInventory")
4
5P.wrapper = nil
6P.detailsWindows = {}
7P.detailPickups = {}
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    P.wrapper = nil
20    P.detailsWindows = {}
21    P.detailPickups = {}
22    P.pickupsList = {}
23end
24
25function P.onShow()
26    P.createInventory()
27    P.showing = true
28end
29
30function P.onHide()
31    P.showing = false
32    P.cleanup(true)
33end
34
35function P.update()
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.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.inUse == true then
54                        useButton:setText("unuse")
55                        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
56                        if pickup.usable == 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.unusable == false then
63                            useButton:setEnabled(false)
64                        end
65                    end
66
67                    if pickup.pickedUp == 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
81   
82end
83
84function P.createInventory()
85    local pickupManager = orxonox.PickupManager:getInstance()
86   
87    local root = winMgr:getWindow("orxonox/PickupInventory/Inventory")
88    P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper")
89    P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0)))
90    root:addChildWindow(P.wrapper)
91   
92    P.pickupsList = {}
93
94    local numPickups = pickupManager:getNumPickups()
95    local counter = 1
96    local offset = 0
97    while counter <= numPickups do
98        local pickup = pickupManager:popPickup()
99        table.insert(P.pickupsList, pickup)
100        local window = P.createPickupEntry(counter, pickup)
101        window:setYPosition(CEGUI.UDim(0,offset))
102        offset = offset + window:getHeight():asAbsolute(1)
103        P.wrapper:addChildWindow(window)
104        counter = counter + 1
105    end
106
107end
108
109function P.createPickupEntry(index, pickup)
110    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup.pickup)
111
112    local name = "orxonox/PickupInventory/Box/Pickup" .. index
113
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)))
117
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)
124
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.inUse == false then
136        useButton:setText("use")
137        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
138        if pickup.usable == 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.unusable == false then
145            useButton:setEnabled(false)
146        end
147    end
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
165end
166
167function P.cleanup(destroyDetails)
168    if P.wrapper ~= nil then
169        winMgr:destroyWindow(P.wrapper)
170    end
171   
172    --Destroy details windows.
173    if destroyDetails == false then
174        return
175    end
176    for k,v in pairs(P.detailsWindows) do
177        if v ~= nil then
178            winMgr:destroyWindow(v)
179        end
180    end
181end
182
183function P.windowToPickupHelper(e)
184    local we = CEGUI.toWindowEventArgs(e)
185    local name = we.window:getName()
186
187    local match = string.gmatch(name, "%d+")
188    local pickupIndex = tonumber(match())
189
190    return pickupIndex
191end
192
193function P.createDetailsWindow(pickupIndex)
194    local pickup = P.pickupsList[pickupIndex]
195    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup.pickup)
196
197    local index = P.getNewDetailNumber()
198    local name = "orxonox/PickupInventory/Details" .. index
199   
200    local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
201    window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0)))
202    orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow")
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   
212    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
213    title:setText(representation:getPickupName())
214    title:setHeight(CEGUI.UDim(0, P.textHeight))
215    title:setProperty("FrameEnabled", "set:False")
216    title:setProperty("BackgroundEnabled", "set:False")
217    wrapper:addChildWindow(title)
218   
219    local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image")
220    image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation())
221    image:setProperty("BackgroundEnabled", "set:False")
222    image:setProperty("FrameEnabled", "set:True")
223    image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize)))
224    image:setYPosition(CEGUI.UDim(0, P.textHeight + 5))
225    wrapper:addChildWindow(image)
226   
227    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description")
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())
232    description:setProperty("HorzFormatting", "WordWrapLeftAligned")
233    description:setProperty("VertFormatting", "TopAligned")
234    box:addChildWindow(description)
235    wrapper:addChildWindow(box)
236
237    local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
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.inUse == false then
241        useButton:setText("use")
242        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
243        if pickup.usable == 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.unusable == false then
250            useButton:setEnabled(false)
251        end
252    end
253    wrapper:addChildWindow(useButton)
254   
255    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
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)))
258    dropButton:setText("drop")
259    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked")
260    wrapper:addChildWindow(dropButton)
261
262    P.detailsWindows[index] = window
263    P.detailPickups[index] = pickup
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
274    return number+1
275end
276
277function P.InventoryUseButton_clicked(e)
278    local pickupIndex = P.windowToPickupHelper(e)
279    local pickup = P.pickupsList[pickupIndex]
280    orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
281end
282
283function P.InventoryUnuseButton_clicked(e)
284    local pickupIndex = P.windowToPickupHelper(e)
285    local pickup = P.pickupsList[pickupIndex]
286    orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
287end
288
289function P.InventoryDropButton_clicked(e)
290    local pickupIndex = P.windowToPickupHelper(e)
291    local pickup = P.pickupsList[pickupIndex]
292    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
293end
294
295function P.InventoryDetailsButton_clicked(e)
296    local pickupIndex = P.windowToPickupHelper(e)
297    P.createDetailsWindow(pickupIndex)
298end
299
300function P.InventoryUseDetailButton_clicked(e)
301    local pickupIndex = P.windowToPickupHelper(e)
302    local pickup = P.detailPickups[pickupIndex]
303    orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
304end
305
306function P.InventoryUnuseDetailButton_clicked(e)
307    local pickupIndex = P.windowToPickupHelper(e)
308    local pickup = P.detailPickups[pickupIndex]
309    orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
310end
311
312function P.InventoryDropDetailButton_clicked(e)
313    local pickupIndex = P.windowToPickupHelper(e)
314    local pickup = P.detailPickups[pickupIndex]
315    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
316end
317
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   
325    local window = P.detailsWindows[detailNr]
326    winMgr:destroyWindow(window)
327    P.detailsWindows[detailNr] = nil
328    P.detailPickups[detailNr] = nil
329end
330
331function P.InventoryBackButton_clicked(e)
332    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
333end
334
335return P
Note: See TracBrowser for help on using the repository browser.