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