1 | -- PickupInventory.lua |
---|
2 | |
---|
3 | local P = createMenuSheet("PickupInventory") |
---|
4 | |
---|
5 | P.wrapper = nil |
---|
6 | P.detailsWindows = {} |
---|
7 | P.detailPickups = {} |
---|
8 | P.pickupsList = {} |
---|
9 | |
---|
10 | P.showing = false |
---|
11 | |
---|
12 | -- Design parameters |
---|
13 | P.imageHeight = 50 |
---|
14 | P.detailImageSize = 100 |
---|
15 | P.textHeight = 30 |
---|
16 | P.buttonWidth = 85 |
---|
17 | |
---|
18 | function P.onLoad() |
---|
19 | P.wrapper = nil |
---|
20 | P.detailsWindows = {} |
---|
21 | P.detailPickups = {} |
---|
22 | P.pickupsList = {} |
---|
23 | end |
---|
24 | |
---|
25 | function P.onShow() |
---|
26 | P.createInventory() |
---|
27 | P.showing = true |
---|
28 | end |
---|
29 | |
---|
30 | function P.onHide() |
---|
31 | P.showing = false |
---|
32 | P.cleanup(true) |
---|
33 | end |
---|
34 | |
---|
35 | function 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 | if pickup:isUsable() == 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:isUnusable() == false then |
---|
63 | useButton:setEnabled(false) |
---|
64 | end |
---|
65 | end |
---|
66 | |
---|
67 | if pickup:isPickedUp() == 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 | |
---|
82 | end |
---|
83 | |
---|
84 | function 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 | |
---|
107 | end |
---|
108 | |
---|
109 | function P.createPickupEntry(index, pickup) |
---|
110 | local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(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:isUsed() == false then |
---|
136 | useButton:setText("use") |
---|
137 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked") |
---|
138 | if pickup:isUsable() == 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:isUnusable() == 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 |
---|
165 | end |
---|
166 | |
---|
167 | function 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 |
---|
181 | end |
---|
182 | |
---|
183 | function 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 |
---|
191 | end |
---|
192 | |
---|
193 | function P.createDetailsWindow(pickupIndex) |
---|
194 | local pickup = P.pickupsList[pickupIndex] |
---|
195 | local representation = orxonox.PickupManager:getInstance():getPickupRepresentation(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:isUsed() == false then |
---|
241 | useButton:setText("use") |
---|
242 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked") |
---|
243 | if pickup:isUsable() == 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:isUnusable() == 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 | |
---|
265 | end |
---|
266 | |
---|
267 | function 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 |
---|
275 | end |
---|
276 | |
---|
277 | function P.InventoryUseButton_clicked(e) |
---|
278 | local pickupIndex = P.windowToPickupHelper(e) |
---|
279 | orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], true) |
---|
280 | end |
---|
281 | |
---|
282 | function P.InventoryUnuseButton_clicked(e) |
---|
283 | local pickupIndex = P.windowToPickupHelper(e) |
---|
284 | orxonox.PickupManager:getInstance():usePickup(P.pickupsList[pickupIndex], false) |
---|
285 | end |
---|
286 | |
---|
287 | function P.InventoryDropButton_clicked(e) |
---|
288 | local pickupIndex = P.windowToPickupHelper(e) |
---|
289 | orxonox.PickupManager:getInstance():dropPickup(P.pickupsList[pickupIndex]) |
---|
290 | end |
---|
291 | |
---|
292 | function P.InventoryDetailsButton_clicked(e) |
---|
293 | local pickupIndex = P.windowToPickupHelper(e) |
---|
294 | P.createDetailsWindow(pickupIndex) |
---|
295 | end |
---|
296 | |
---|
297 | function P.InventoryUseDetailButton_clicked(e) |
---|
298 | local pickupIndex = P.windowToPickupHelper(e) |
---|
299 | orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], true) |
---|
300 | end |
---|
301 | |
---|
302 | function P.InventoryUnuseDetailButton_clicked(e) |
---|
303 | local pickupIndex = P.windowToPickupHelper(e) |
---|
304 | orxonox.PickupManager:getInstance():usePickup(P.detailPickups[pickupIndex], false) |
---|
305 | end |
---|
306 | |
---|
307 | function P.InventoryDropDetailButton_clicked(e) |
---|
308 | local pickupIndex = P.windowToPickupHelper(e) |
---|
309 | orxonox.PickupManager:getInstance():dropPickup(P.detailPickups[pickupIndex]) |
---|
310 | end |
---|
311 | |
---|
312 | function P.closeDetailWindow(e) |
---|
313 | --Get some numbers from the window |
---|
314 | local we = CEGUI.toWindowEventArgs(e) |
---|
315 | local name = we.window:getName() |
---|
316 | local match = string.gmatch(name, "%d+") |
---|
317 | local detailNr = tonumber(match()) |
---|
318 | |
---|
319 | local window = P.detailsWindows[detailNr] |
---|
320 | winMgr:destroyWindow(window) |
---|
321 | P.detailsWindows[detailNr] = nil |
---|
322 | P.detailPickups[detailNr] = nil |
---|
323 | end |
---|
324 | |
---|
325 | function P.InventoryBackButton_clicked(e) |
---|
326 | orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory") |
---|
327 | end |
---|
328 | |
---|
329 | return P |
---|