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