Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/rocket3/data/gui/scripts/PickupInventory.lua @ 7144

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

Made the QuestGUI completely lua based in an attempt to remove a segfault that occured when closing orxonox. Successfully, I might add. ;)
In the process of doing so I expanded the GUITools by adding a function that calculates the height that text in an input window needs, with word-wrap enabled.
Also fixed a small error in the Quest_PirateAttack level.

  • Property svn:eol-style set to native
File size: 12.1 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) == 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
75   
76end
77
78function P.createInventory()
79    local pickupManager = orxonox.PickupManager:getInstance()
80   
81    local root = winMgr:getWindow("orxonox/PickupInventory/Inventory")
82    P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper")
83    P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0)))
84    root:addChildWindow(P.wrapper)
85   
86    P.pickupsList = {}
87
88    local numPickups = pickupManager:getNumPickups()
89    local counter = 1
90    local offset = 0
91    while counter <= numPickups do
92        local pickup = pickupManager:popPickup()
93        table.insert(P.pickupsList, pickup)
94        local window = P.createPickupEntry(counter, pickup)
95        window:setYPosition(CEGUI.UDim(0,offset))
96        offset = offset + window:getHeight():asAbsolute(1)
97        P.wrapper:addChildWindow(window)
98        counter = counter + 1
99    end
100
101end
102
103function P.createPickupEntry(index, pickup)
104    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup)
105
106    local name = "orxonox/PickupInventory/Box/Pickup" .. index
107
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)))
111
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)
118
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)))
121    title:setSize(CEGUI.UVector2(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight)))
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")
127    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+10),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
128    useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
129    if pickup:isUsed() == false then
130        useButton:setText("use")
131        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked")
132    else
133        useButton:setText("unuse")
134        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseButton_clicked")
135    end
136    item:addChildWindow(useButton)
137
138    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
139    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+15+P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
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")
146    detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+20+2*P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
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
153end
154
155function P.cleanup(destroyDetails)
156    if P.wrapper ~= nil then
157        winMgr:destroyWindow(P.wrapper)
158    end
159   
160    --Destroy details windows.
161    if destroyDetails == false then
162        return
163    end
164    for k,v in pairs(P.detailsWindows) do
165        if v ~= nil then
166            winMgr:destroyWindow(v)
167        end
168    end
169end
170
171function P.windowToPickupHelper(e)
172    local we = CEGUI.toWindowEventArgs(e)
173    local name = we.window:getName()
174
175    local match = string.gmatch(name, "%d+")
176    local pickupIndex = tonumber(match())
177
178    return pickupIndex
179end
180
181function P.createDetailsWindow(pickupIndex)
182    local pickup = P.pickupsList[pickupIndex]
183    local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(pickup)
184
185    local index = P.getNewDetailNumber()
186    local name = "orxonox/PickupInventory/Details" .. index
187   
188    local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
189    window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0)))
190    orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow")
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   
200    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
201    title:setText(representation:getPickupName())
202    title:setHeight(CEGUI.UDim(0, P.textHeight))
203    title:setProperty("FrameEnabled", "set:False")
204    title:setProperty("BackgroundEnabled", "set:False")
205    wrapper:addChildWindow(title)
206   
207    local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image")
208    image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation())
209    image:setProperty("BackgroundEnabled", "set:False")
210    image:setProperty("FrameEnabled", "set:True")
211    image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize)))
212    image:setYPosition(CEGUI.UDim(0, P.textHeight + 5))
213    wrapper:addChildWindow(image)
214   
215    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description")
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())
220    description:setProperty("HorzFormatting", "WordWrapLeftAligned")
221    description:setProperty("VertFormatting", "TopAligned")
222    box:addChildWindow(description)
223    wrapper:addChildWindow(box)
224
225    local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
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")
230        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked")
231    else
232        useButton:setText("unuse")
233        orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked")
234    end
235    wrapper:addChildWindow(useButton)
236   
237    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
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)))
240    dropButton:setText("drop")
241    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked")
242    wrapper:addChildWindow(dropButton)
243
244    P.detailsWindows[index] = window
245    P.detailPickups[index] = pickup
246   
247end
248
249function P.getNewDetailNumber()
250    local number = table.getn(P.detailsWindows)
251    for k,v in pairs(P.detailsWindows) do
252        if v == nil then
253            number = k-1
254        end
255    end
256    return number+1
257end
258
259function P.InventoryUseButton_clicked(e)
260    local pickupIndex = P.windowToPickupHelper(e)
261    orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], true)
262end
263
264function P.InventoryUnuseButton_clicked(e)
265    local pickupIndex = P.windowToPickupHelper(e)
266    orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], false)
267end
268
269function P.InventoryDropButton_clicked(e)
270    local pickupIndex = P.windowToPickupHelper(e)
271    orxonox.PickupManager:getInstance():dropPickup(P.pickupsList[pickupIndex])
272end
273
274function P.InventoryDetailsButton_clicked(e)
275    local pickupIndex = P.windowToPickupHelper(e)
276    P.createDetailsWindow(pickupIndex)
277end
278
279function P.InventoryUseDetailButton_clicked(e)
280    local pickupIndex = P.windowToPickupHelper(e)
281    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], true)
282end
283
284function P.InventoryUnuseDetailButton_clicked(e)
285    local pickupIndex = P.windowToPickupHelper(e)
286    orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], false)
287end
288
289function P.InventoryDropDetailButton_clicked(e)
290    local pickupIndex = P.windowToPickupHelper(e)
291    orxonox.PickupManager:getInstance():dropPickup(P.detailPickups[pickupIndex])
292end
293
294function P.closeDetailWindow(e)
295    --Get some numbers from the window
296    local we = CEGUI.toWindowEventArgs(e)
297    local name = we.window:getName()
298    local match = string.gmatch(name, "%d+")
299    local detailNr = tonumber(match())
300   
301    local window = P.detailsWindows[detailNr]
302    winMgr:destroyWindow(window)
303    P.detailsWindows[detailNr] = nil
304    P.detailPickups[detailNr] = nil
305end
306
307function P.InventoryBackButton_clicked(e)
308    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
309end
310
311return P
Note: See TracBrowser for help on using the repository browser.