| 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 = P.window:getChild("Details" .. k .. "/Wrapper/UseButton") | 
|---|
| 46 |                 local dropButton = P.window:getChild("Details" .. k .. "/Wrapper/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 |      | 
|---|
| 80 | end | 
|---|
| 81 |  | 
|---|
| 82 | function P.createInventory() | 
|---|
| 83 |     local pickupManager = orxonox.PickupManager:getInstance() | 
|---|
| 84 |      | 
|---|
| 85 |     local root = P.window:getChild("PickupInventory/Wrapper/Inventory") | 
|---|
| 86 |     P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "Wrapper") | 
|---|
| 87 |     P.wrapper:setSize(CEGUI.USize(CEGUI.UDim(1,0),CEGUI.UDim(1,0))) | 
|---|
| 88 |     root:addChild(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 + P.imageHeight | 
|---|
| 101 |         P.wrapper:addChild(window) | 
|---|
| 102 |         counter = counter + 1 | 
|---|
| 103 |     end | 
|---|
| 104 |  | 
|---|
| 105 | end | 
|---|
| 106 |  | 
|---|
| 107 | function P.createPickupEntry(index, pickup) | 
|---|
| 108 |     local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName) | 
|---|
| 109 |  | 
|---|
| 110 |     local name = "Pickup" .. index | 
|---|
| 111 |  | 
|---|
| 112 |     local item = winMgr:createWindow("MenuWidgets/StaticText", name) | 
|---|
| 113 |     item:setSize(CEGUI.USize(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", "Image") | 
|---|
| 117 |     image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation()) | 
|---|
| 118 |     image:setProperty("BackgroundEnabled", "set:False") | 
|---|
| 119 |     image:setProperty("FrameEnabled", "set:True") | 
|---|
| 120 |     image:setSize(CEGUI.USize(CEGUI.UDim(0, P.imageHeight), CEGUI.UDim(0, P.imageHeight))) | 
|---|
| 121 |     item:addChild(image) | 
|---|
| 122 |  | 
|---|
| 123 |     local title = winMgr:createWindow("MenuWidgets/StaticText", "Title") | 
|---|
| 124 |     title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight+5), CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2))) | 
|---|
| 125 |     title:setSize(CEGUI.USize(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight))) | 
|---|
| 126 |     title:setText(representation:getPickupName()) | 
|---|
| 127 |     title:setProperty("FrameEnabled", "set:False") | 
|---|
| 128 |     item:addChild(title) | 
|---|
| 129 |  | 
|---|
| 130 |     local useButton = winMgr:createWindow("MenuWidgets/Button", "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.USize(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:addChild(useButton) | 
|---|
| 146 |  | 
|---|
| 147 |     local dropButton = winMgr:createWindow("MenuWidgets/Button", "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.USize(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:addChild(dropButton) | 
|---|
| 153 |  | 
|---|
| 154 |     local detailsButton = winMgr:createWindow("MenuWidgets/Button", "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.USize(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:addChild(detailsButton) | 
|---|
| 160 |  | 
|---|
| 161 |     return item | 
|---|
| 162 | end | 
|---|
| 163 |  | 
|---|
| 164 | function 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 | 
|---|
| 178 | end | 
|---|
| 179 |  | 
|---|
| 180 | function P.windowToPickupHelper(e) | 
|---|
| 181 |     local we = CEGUI.toWindowEventArgs(e) | 
|---|
| 182 |     local name = we.window:getNamePath() | 
|---|
| 183 |  | 
|---|
| 184 |     local match = string.gmatch(name, "%d+") | 
|---|
| 185 |     local pickupIndex = tonumber(match()) | 
|---|
| 186 |  | 
|---|
| 187 |     return pickupIndex | 
|---|
| 188 | end | 
|---|
| 189 |  | 
|---|
| 190 | function 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 = "Details" .. index | 
|---|
| 196 |      | 
|---|
| 197 |     local window = winMgr:createWindow("MenuWidgets/FrameWindow", name) | 
|---|
| 198 |     window:setSize(CEGUI.USize(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0))) | 
|---|
| 199 |     orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow") | 
|---|
| 200 |      | 
|---|
| 201 |     P.window:addChild(window) | 
|---|
| 202 |      | 
|---|
| 203 |     local wrapper = winMgr:createWindow("DefaultWindow", "Wrapper") | 
|---|
| 204 |     wrapper:setSize(CEGUI.USize(CEGUI.UDim(1, -20),CEGUI.UDim(1, -50))) | 
|---|
| 205 |     wrapper:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10),CEGUI.UDim(0, 40))) | 
|---|
| 206 |     window:addChild(wrapper) | 
|---|
| 207 |      | 
|---|
| 208 |     local title = winMgr:createWindow("MenuWidgets/StaticText", "Title") | 
|---|
| 209 |     title:setText(representation:getPickupName()) | 
|---|
| 210 |     title:setHeight(CEGUI.UDim(0, P.textHeight)) | 
|---|
| 211 |     title:setProperty("FrameEnabled", "set:False") | 
|---|
| 212 |     title:setProperty("BackgroundEnabled", "set:False") | 
|---|
| 213 |     wrapper:addChild(title) | 
|---|
| 214 |      | 
|---|
| 215 |     local image = winMgr:createWindow("MenuWidgets/StaticImage", "Image") | 
|---|
| 216 |     image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation()) | 
|---|
| 217 |     image:setProperty("BackgroundEnabled", "set:False") | 
|---|
| 218 |     image:setProperty("FrameEnabled", "set:True") | 
|---|
| 219 |     image:setSize(CEGUI.USize(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize))) | 
|---|
| 220 |     image:setYPosition(CEGUI.UDim(0, P.textHeight + 5)) | 
|---|
| 221 |     wrapper:addChild(image) | 
|---|
| 222 |      | 
|---|
| 223 |     local box = winMgr:createWindow("MenuWidgets/ScrollablePane", "Description") | 
|---|
| 224 |     box:setSize(CEGUI.USize(CEGUI.UDim(1.0, -1*(P.detailImageSize + 10)),CEGUI.UDim(1, -(P.textHeight + 5 + P.textHeight + 20)))) | 
|---|
| 225 |     box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize + 10),CEGUI.UDim(0, P.textHeight + 5))) | 
|---|
| 226 |     local description = winMgr:createWindow("MenuWidgets/StaticText", "Text") | 
|---|
| 227 |     description:setText(representation:getPickupDescription()) | 
|---|
| 228 |     description:setProperty("HorzFormatting", "WordWrapLeftAligned") | 
|---|
| 229 |     description:setProperty("VertFormatting", "TopAligned") | 
|---|
| 230 |     box:addChild(description) | 
|---|
| 231 |     wrapper:addChild(box) | 
|---|
| 232 |  | 
|---|
| 233 |     local useButton = winMgr:createWindow("MenuWidgets/Button", "UseButton") | 
|---|
| 234 |     useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10),CEGUI.UDim(1, -40))) | 
|---|
| 235 |     useButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight))) | 
|---|
| 236 |     if pickup.inUse == false then | 
|---|
| 237 |         useButton:setText("use") | 
|---|
| 238 |         if pickup.usable == false then | 
|---|
| 239 |             useButton:setEnabled(false) | 
|---|
| 240 |         end | 
|---|
| 241 |     else | 
|---|
| 242 |         useButton:setText("unuse") | 
|---|
| 243 |         if pickup.unusable == false then | 
|---|
| 244 |             useButton:setEnabled(false) | 
|---|
| 245 |         end | 
|---|
| 246 |     end | 
|---|
| 247 |     orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseDetailButton_clicked") | 
|---|
| 248 |     wrapper:addChild(useButton) | 
|---|
| 249 |      | 
|---|
| 250 |     local dropButton = winMgr:createWindow("MenuWidgets/Button", "DropButton") | 
|---|
| 251 |     dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10+P.buttonWidth+10),CEGUI.UDim(1, -40))) | 
|---|
| 252 |     dropButton:setSize(CEGUI.USize(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight))) | 
|---|
| 253 |     dropButton:setText("drop") | 
|---|
| 254 |     orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked") | 
|---|
| 255 |     wrapper:addChild(dropButton) | 
|---|
| 256 |  | 
|---|
| 257 |     P.detailsWindows[index] = window | 
|---|
| 258 |     P.detailPickups[index] = pickup | 
|---|
| 259 |      | 
|---|
| 260 | end | 
|---|
| 261 |  | 
|---|
| 262 | function P.getNewDetailNumber() | 
|---|
| 263 |     local number = table.getn(P.detailsWindows) | 
|---|
| 264 |     for k,v in pairs(P.detailsWindows) do | 
|---|
| 265 |         if v == nil then | 
|---|
| 266 |             number = k-1 | 
|---|
| 267 |         end | 
|---|
| 268 |     end | 
|---|
| 269 |     return number+1 | 
|---|
| 270 | end | 
|---|
| 271 |  | 
|---|
| 272 | function P.InventoryUseUnuseButton_clicked(e) | 
|---|
| 273 |     local pickupIndex = P.windowToPickupHelper(e) | 
|---|
| 274 |     local pickup = P.pickupsList[pickupIndex] | 
|---|
| 275 |     if pickup.inUse == false then | 
|---|
| 276 |         orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) | 
|---|
| 277 |     else | 
|---|
| 278 |         orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) | 
|---|
| 279 |     end | 
|---|
| 280 | end | 
|---|
| 281 |  | 
|---|
| 282 | function P.InventoryDropButton_clicked(e) | 
|---|
| 283 |     local pickupIndex = P.windowToPickupHelper(e) | 
|---|
| 284 |     local pickup = P.pickupsList[pickupIndex] | 
|---|
| 285 |     orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) | 
|---|
| 286 | end | 
|---|
| 287 |  | 
|---|
| 288 | function P.InventoryDetailsButton_clicked(e) | 
|---|
| 289 |     local pickupIndex = P.windowToPickupHelper(e) | 
|---|
| 290 |     P.createDetailsWindow(pickupIndex) | 
|---|
| 291 | end | 
|---|
| 292 |  | 
|---|
| 293 | function P.InventoryUseUnuseDetailButton_clicked(e) | 
|---|
| 294 |     local pickupIndex = P.windowToPickupHelper(e) | 
|---|
| 295 |     local pickup = P.detailPickups[pickupIndex] | 
|---|
| 296 |     if pickup.inUse == false then | 
|---|
| 297 |         orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) | 
|---|
| 298 |     else | 
|---|
| 299 |         orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) | 
|---|
| 300 |     end | 
|---|
| 301 | end | 
|---|
| 302 |  | 
|---|
| 303 | function P.InventoryDropDetailButton_clicked(e) | 
|---|
| 304 |     local pickupIndex = P.windowToPickupHelper(e) | 
|---|
| 305 |     local pickup = P.detailPickups[pickupIndex] | 
|---|
| 306 |     orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) | 
|---|
| 307 | end | 
|---|
| 308 |  | 
|---|
| 309 | function P.closeDetailWindow(e) | 
|---|
| 310 |     --Get some numbers from the window | 
|---|
| 311 |     local we = CEGUI.toWindowEventArgs(e) | 
|---|
| 312 |     local name = we.window:getNamePath() | 
|---|
| 313 |     local match = string.gmatch(name, "%d+") | 
|---|
| 314 |     local detailNr = tonumber(match()) | 
|---|
| 315 |      | 
|---|
| 316 |     P.destroyDetailWindow(detailNr) | 
|---|
| 317 | end | 
|---|
| 318 |  | 
|---|
| 319 | function P.destroyDetailWindow(detailNr) | 
|---|
| 320 |     local window = P.detailsWindows[detailNr] | 
|---|
| 321 |     winMgr:destroyWindow(window) | 
|---|
| 322 |     P.detailsWindows[detailNr] = nil | 
|---|
| 323 |     P.detailPickups[detailNr] = nil | 
|---|
| 324 | end | 
|---|
| 325 |  | 
|---|
| 326 | function P.InventoryBackButton_clicked(e) | 
|---|
| 327 |     orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory") | 
|---|
| 328 | end | 
|---|
| 329 |  | 
|---|
| 330 | return P | 
|---|