Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_FS18/data/gui/scripts/PickupInventory.lua @ 12073

Last change on this file since 12073 was 11710, checked in by landauf, 7 years ago

fixed two bugs in PickupInventory.lua:

  • the update function had a messed up logic regarding the use-button in the detail window (condition was partially inverted)
  • updating the event subscription of the use-button in the detail-window doesn't seem to work - probably CEGUI doesn't like updating/changing/overriding an event while begin IN a mouse-click-event already. moving the logic to the Use/Unuse-function instead seems to work.
  • Property svn:eol-style set to native
File size: 12.3 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 == false then
54                        useButton:setText("use")
55                        if pickup.usable == false then
56                            useButton:setEnabled(false)
57                        end
58                    else
59                        useButton:setText("unuse")
60                        if pickup.unusable == false then
61                            useButton:setEnabled(false)
62                        end
63                    end
64
65                    if pickup.pickedUp == false then
66                        useButton:setEnabled(false)
67                        dropButton:setEnabled(false)
68                        P.detailPickups[k] = nil
69                    end
70                end
71            end
72        end
73    end
74
75    -- Update main inventory.
76    P.cleanup(false)
77    P.createInventory()
78    -- TODO: Recover scrolling position
79   
80end
81
82function P.createInventory()
83    local pickupManager = orxonox.PickupManager:getInstance()
84   
85    local root = winMgr:getWindow("orxonox/PickupInventory/Inventory")
86    P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper")
87    P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0)))
88    root:addChildWindow(P.wrapper)
89   
90    P.pickupsList = {}
91
92    local numPickups = pickupManager:getNumPickups()
93    local counter = 1
94    local offset = 0
95    while counter <= numPickups do
96        local pickup = pickupManager:popPickup()
97        table.insert(P.pickupsList, pickup)
98        local window = P.createPickupEntry(counter, pickup)
99        window:setYPosition(CEGUI.UDim(0,offset))
100        offset = offset + window:getHeight():asAbsolute(1)
101        P.wrapper:addChildWindow(window)
102        counter = counter + 1
103    end
104
105end
106
107function P.createPickupEntry(index, pickup)
108    local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName)
109
110    local name = "orxonox/PickupInventory/Box/Pickup" .. index
111
112    local item = winMgr:createWindow("MenuWidgets/StaticText", name)
113    item:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(0, P.imageHeight)))
114    item:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0)))
115
116    local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image")
117    image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation())
118    image:setProperty("BackgroundEnabled", "set:False")
119    image:setProperty("FrameEnabled", "set:True")
120    image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight), CEGUI.UDim(0, P.imageHeight)))
121    item:addChildWindow(image)
122
123    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
124    title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight+5), CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
125    title:setSize(CEGUI.UVector2(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight)))
126    title:setText(representation:getPickupName())
127    title:setProperty("FrameEnabled", "set:False")
128    item:addChildWindow(title)
129
130    local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
131    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+10),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
132    useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
133    if pickup.inUse == false then
134        useButton:setText("use")
135        if pickup.usable == false then
136            useButton:setEnabled(false)
137        end
138    else
139        useButton:setText("unuse")
140        if pickup.unusable == false then
141            useButton:setEnabled(false)
142        end
143    end
144    orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseButton_clicked")
145    item:addChildWindow(useButton)
146
147    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
148    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+15+P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
149    dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
150    dropButton:setText("drop")
151    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropButton_clicked")
152    item:addChildWindow(dropButton)
153
154    local detailsButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DetailsButton")
155    detailsButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0.3, P.imageHeight+20+2*P.buttonWidth),CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2)))
156    detailsButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
157    detailsButton:setText("details")
158    orxonox.GUIManager:subscribeEventHelper(detailsButton, "Clicked", P.name .. ".InventoryDetailsButton_clicked")
159    item:addChildWindow(detailsButton)
160
161    return item
162end
163
164function P.cleanup(destroyDetails)
165    if P.wrapper ~= nil then
166        winMgr:destroyWindow(P.wrapper)
167    end
168   
169    --Destroy details windows.
170    if destroyDetails == false then
171        return
172    end
173    for k,v in pairs(P.detailsWindows) do
174        if v ~= nil then
175            P.destroyDetailWindow(k)
176        end
177    end
178end
179
180function P.windowToPickupHelper(e)
181    local we = CEGUI.toWindowEventArgs(e)
182    local name = we.window:getName()
183
184    local match = string.gmatch(name, "%d+")
185    local pickupIndex = tonumber(match())
186
187    return pickupIndex
188end
189
190function P.createDetailsWindow(pickupIndex)
191    local pickup = P.pickupsList[pickupIndex]
192    local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName)
193
194    local index = P.getNewDetailNumber()
195    local name = "orxonox/PickupInventory/Details" .. index
196   
197    local window = winMgr:createWindow("MenuWidgets/FrameWindow", name)
198    window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0)))
199    orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow")
200   
201    local root = winMgr:getWindow("orxonox/PickupInventory/Background")
202    root:addChildWindow(window)
203   
204    local wrapper = winMgr:createWindow("DefaultWindow", name .. "/Wrapper")
205    wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1, -20),CEGUI.UDim(1, -50)))
206    wrapper:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10),CEGUI.UDim(0, 40)))
207    window:addChildWindow(wrapper)
208   
209    local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title")
210    title:setText(representation:getPickupName())
211    title:setHeight(CEGUI.UDim(0, P.textHeight))
212    title:setProperty("FrameEnabled", "set:False")
213    title:setProperty("BackgroundEnabled", "set:False")
214    wrapper:addChildWindow(title)
215   
216    local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image")
217    image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation())
218    image:setProperty("BackgroundEnabled", "set:False")
219    image:setProperty("FrameEnabled", "set:True")
220    image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize)))
221    image:setYPosition(CEGUI.UDim(0, P.textHeight + 5))
222    wrapper:addChildWindow(image)
223   
224    local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description")
225    box:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, -1*(P.detailImageSize + 10)),CEGUI.UDim(1, -(P.textHeight + 5 + P.textHeight + 20))))
226    box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize + 10),CEGUI.UDim(0, P.textHeight + 5)))
227    local description = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Description/Text")
228    description:setText(representation:getPickupDescription())
229    description:setProperty("HorzFormatting", "WordWrapLeftAligned")
230    description:setProperty("VertFormatting", "TopAligned")
231    box:addChildWindow(description)
232    wrapper:addChildWindow(box)
233
234    local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton")
235    useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10),CEGUI.UDim(1, -40)))
236    useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
237    if pickup.inUse == false then
238        useButton:setText("use")
239        if pickup.usable == false then
240            useButton:setEnabled(false)
241        end
242    else
243        useButton:setText("unuse")
244        if pickup.unusable == false then
245            useButton:setEnabled(false)
246        end
247    end
248    orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseDetailButton_clicked")
249    wrapper:addChildWindow(useButton)
250   
251    local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton")
252    dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10+P.buttonWidth+10),CEGUI.UDim(1, -40)))
253    dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight)))
254    dropButton:setText("drop")
255    orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked")
256    wrapper:addChildWindow(dropButton)
257
258    P.detailsWindows[index] = window
259    P.detailPickups[index] = pickup
260   
261end
262
263function P.getNewDetailNumber()
264    local number = table.getn(P.detailsWindows)
265    for k,v in pairs(P.detailsWindows) do
266        if v == nil then
267            number = k-1
268        end
269    end
270    return number+1
271end
272
273function P.InventoryUseUnuseButton_clicked(e)
274    local pickupIndex = P.windowToPickupHelper(e)
275    local pickup = P.pickupsList[pickupIndex]
276    if pickup.inUse == false then
277        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
278    else
279        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
280    end
281end
282
283function P.InventoryDropButton_clicked(e)
284    local pickupIndex = P.windowToPickupHelper(e)
285    local pickup = P.pickupsList[pickupIndex]
286    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
287end
288
289function P.InventoryDetailsButton_clicked(e)
290    local pickupIndex = P.windowToPickupHelper(e)
291    P.createDetailsWindow(pickupIndex)
292end
293
294function P.InventoryUseUnuseDetailButton_clicked(e)
295    local pickupIndex = P.windowToPickupHelper(e)
296    local pickup = P.detailPickups[pickupIndex]
297    if pickup.inUse == false then
298        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true)
299    else
300        orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false)
301    end
302end
303
304function P.InventoryDropDetailButton_clicked(e)
305    local pickupIndex = P.windowToPickupHelper(e)
306    local pickup = P.detailPickups[pickupIndex]
307    orxonox.PickupManager:getInstance():dropPickup(pickup.pickup)
308end
309
310function P.closeDetailWindow(e)
311    --Get some numbers from the window
312    local we = CEGUI.toWindowEventArgs(e)
313    local name = we.window:getName()
314    local match = string.gmatch(name, "%d+")
315    local detailNr = tonumber(match())
316   
317    P.destroyDetailWindow(detailNr)
318end
319
320function P.destroyDetailWindow(detailNr)
321    local window = P.detailsWindows[detailNr]
322    winMgr:destroyWindow(window)
323    P.detailsWindows[detailNr] = nil
324    P.detailPickups[detailNr] = nil
325end
326
327function P.InventoryBackButton_clicked(e)
328    orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory")
329end
330
331return P
Note: See TracBrowser for help on using the repository browser.