[5661] | 1 | -- PickupInventory.lua |
---|
[5559] | 2 | |
---|
[6746] | 3 | local P = createMenuSheet("PickupInventory") |
---|
[6711] | 4 | |
---|
| 5 | P.wrapper = nil |
---|
| 6 | P.detailsWindows = {} |
---|
[7163] | 7 | P.detailPickups = {} |
---|
| 8 | P.pickupsList = {} |
---|
[5587] | 9 | |
---|
[7163] | 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 | |
---|
[6746] | 18 | function P.onLoad() |
---|
[7163] | 19 | P.wrapper = nil |
---|
| 20 | P.detailsWindows = {} |
---|
| 21 | P.detailPickups = {} |
---|
| 22 | P.pickupsList = {} |
---|
[6711] | 23 | end |
---|
[6417] | 24 | |
---|
[6747] | 25 | function P.onShow() |
---|
[6711] | 26 | P.createInventory() |
---|
[7163] | 27 | P.showing = true |
---|
[5559] | 28 | end |
---|
| 29 | |
---|
[6747] | 30 | function P.onHide() |
---|
[7163] | 31 | P.showing = false |
---|
| 32 | P.cleanup(true) |
---|
[5559] | 33 | end |
---|
| 34 | |
---|
[6711] | 35 | function P.update() |
---|
[7163] | 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") |
---|
[7504] | 47 | if orxonox.PickupManager:getInstance():isValidPickup(pickup.pickup) == false then |
---|
[7163] | 48 | useButton:setEnabled(false) |
---|
| 49 | dropButton:setEnabled(false) |
---|
| 50 | P.detailPickups[k] = nil |
---|
| 51 | else |
---|
| 52 | useButton:setEnabled(true) |
---|
[7504] | 53 | if pickup.inUse == true then |
---|
[7163] | 54 | useButton:setText("unuse") |
---|
| 55 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked") |
---|
[7504] | 56 | if pickup.usable == false then |
---|
[7163] | 57 | useButton:setEnabled(false) |
---|
| 58 | end |
---|
| 59 | else |
---|
| 60 | useButton:setText("use") |
---|
| 61 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked") |
---|
[7504] | 62 | if pickup.unusable == false then |
---|
[7163] | 63 | useButton:setEnabled(false) |
---|
| 64 | end |
---|
| 65 | end |
---|
| 66 | |
---|
[7504] | 67 | if pickup.pickedUp == false then |
---|
[7163] | 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 |
---|
[6711] | 81 | |
---|
[5559] | 82 | end |
---|
| 83 | |
---|
[6711] | 84 | function P.createInventory() |
---|
| 85 | local pickupManager = orxonox.PickupManager:getInstance() |
---|
| 86 | |
---|
| 87 | local root = winMgr:getWindow("orxonox/PickupInventory/Inventory") |
---|
[6750] | 88 | P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper") |
---|
[6711] | 89 | P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0))) |
---|
| 90 | root:addChildWindow(P.wrapper) |
---|
| 91 | |
---|
[7163] | 92 | P.pickupsList = {} |
---|
| 93 | |
---|
| 94 | local numPickups = pickupManager:getNumPickups() |
---|
| 95 | local counter = 1 |
---|
[6711] | 96 | local offset = 0 |
---|
[7163] | 97 | while counter <= numPickups do |
---|
| 98 | local pickup = pickupManager:popPickup() |
---|
| 99 | table.insert(P.pickupsList, pickup) |
---|
| 100 | local window = P.createPickupEntry(counter, pickup) |
---|
[6711] | 101 | window:setYPosition(CEGUI.UDim(0,offset)) |
---|
[7163] | 102 | offset = offset + window:getHeight():asAbsolute(1) |
---|
[6711] | 103 | P.wrapper:addChildWindow(window) |
---|
[7163] | 104 | counter = counter + 1 |
---|
[6711] | 105 | end |
---|
[7163] | 106 | |
---|
[6711] | 107 | end |
---|
[6417] | 108 | |
---|
[7163] | 109 | function P.createPickupEntry(index, pickup) |
---|
[9348] | 110 | local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName) |
---|
[6417] | 111 | |
---|
[7163] | 112 | local name = "orxonox/PickupInventory/Box/Pickup" .. index |
---|
[6417] | 113 | |
---|
[7163] | 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))) |
---|
[6711] | 117 | |
---|
[7163] | 118 | local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image") |
---|
[11124] | 119 | image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation()) |
---|
[7163] | 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) |
---|
[6711] | 124 | |
---|
[7163] | 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))) |
---|
[7504] | 135 | if pickup.inUse == false then |
---|
[6711] | 136 | useButton:setText("use") |
---|
[6746] | 137 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseButton_clicked") |
---|
[7504] | 138 | if pickup.usable == false then |
---|
[7163] | 139 | useButton:setEnabled(false) |
---|
| 140 | end |
---|
| 141 | else |
---|
| 142 | useButton:setText("unuse") |
---|
| 143 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseButton_clicked") |
---|
[7504] | 144 | if pickup.unusable == false then |
---|
[7163] | 145 | useButton:setEnabled(false) |
---|
| 146 | end |
---|
[6711] | 147 | end |
---|
[7163] | 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 |
---|
[6711] | 165 | end |
---|
| 166 | |
---|
[7163] | 167 | function P.cleanup(destroyDetails) |
---|
[6711] | 168 | if P.wrapper ~= nil then |
---|
| 169 | winMgr:destroyWindow(P.wrapper) |
---|
| 170 | end |
---|
| 171 | |
---|
| 172 | --Destroy details windows. |
---|
[7163] | 173 | if destroyDetails == false then |
---|
| 174 | return |
---|
| 175 | end |
---|
[6711] | 176 | for k,v in pairs(P.detailsWindows) do |
---|
| 177 | if v ~= nil then |
---|
[9348] | 178 | P.destroyDetailWindow(k) |
---|
[5587] | 179 | end |
---|
| 180 | end |
---|
| 181 | end |
---|
| 182 | |
---|
[7163] | 183 | function P.windowToPickupHelper(e) |
---|
[6711] | 184 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 185 | local name = we.window:getName() |
---|
[6417] | 186 | |
---|
[6711] | 187 | local match = string.gmatch(name, "%d+") |
---|
[7163] | 188 | local pickupIndex = tonumber(match()) |
---|
[6711] | 189 | |
---|
[7163] | 190 | return pickupIndex |
---|
[5587] | 191 | end |
---|
| 192 | |
---|
[7163] | 193 | function P.createDetailsWindow(pickupIndex) |
---|
| 194 | local pickup = P.pickupsList[pickupIndex] |
---|
[9348] | 195 | local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName) |
---|
[7163] | 196 | |
---|
| 197 | local index = P.getNewDetailNumber() |
---|
| 198 | local name = "orxonox/PickupInventory/Details" .. index |
---|
[6711] | 199 | |
---|
[6750] | 200 | local window = winMgr:createWindow("MenuWidgets/FrameWindow", name) |
---|
[6711] | 201 | window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0))) |
---|
[6746] | 202 | orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow") |
---|
[6711] | 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 | |
---|
[6750] | 212 | local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title") |
---|
[7163] | 213 | title:setText(representation:getPickupName()) |
---|
| 214 | title:setHeight(CEGUI.UDim(0, P.textHeight)) |
---|
[6711] | 215 | title:setProperty("FrameEnabled", "set:False") |
---|
| 216 | title:setProperty("BackgroundEnabled", "set:False") |
---|
| 217 | wrapper:addChildWindow(title) |
---|
| 218 | |
---|
[6750] | 219 | local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image") |
---|
[11124] | 220 | image:setProperty("Image", "PickupInventory/" .. representation:getInventoryRepresentation()) |
---|
[6711] | 221 | image:setProperty("BackgroundEnabled", "set:False") |
---|
| 222 | image:setProperty("FrameEnabled", "set:True") |
---|
[7163] | 223 | image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize))) |
---|
| 224 | image:setYPosition(CEGUI.UDim(0, P.textHeight + 5)) |
---|
[6711] | 225 | wrapper:addChildWindow(image) |
---|
| 226 | |
---|
[6750] | 227 | local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description") |
---|
[7163] | 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()) |
---|
[6711] | 232 | description:setProperty("HorzFormatting", "WordWrapLeftAligned") |
---|
| 233 | description:setProperty("VertFormatting", "TopAligned") |
---|
| 234 | box:addChildWindow(description) |
---|
| 235 | wrapper:addChildWindow(box) |
---|
[7163] | 236 | |
---|
[6750] | 237 | local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton") |
---|
[7163] | 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))) |
---|
[7504] | 240 | if pickup.inUse == false then |
---|
[7163] | 241 | useButton:setText("use") |
---|
| 242 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseDetailButton_clicked") |
---|
[7504] | 243 | if pickup.usable == false then |
---|
[7163] | 244 | useButton:setEnabled(false) |
---|
| 245 | end |
---|
| 246 | else |
---|
| 247 | useButton:setText("unuse") |
---|
| 248 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUnuseDetailButton_clicked") |
---|
[7504] | 249 | if pickup.unusable == false then |
---|
[7163] | 250 | useButton:setEnabled(false) |
---|
| 251 | end |
---|
| 252 | end |
---|
[6711] | 253 | wrapper:addChildWindow(useButton) |
---|
| 254 | |
---|
[6750] | 255 | local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton") |
---|
[7163] | 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))) |
---|
[6711] | 258 | dropButton:setText("drop") |
---|
[7163] | 259 | orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked") |
---|
[6711] | 260 | wrapper:addChildWindow(dropButton) |
---|
[7163] | 261 | |
---|
| 262 | P.detailsWindows[index] = window |
---|
| 263 | P.detailPickups[index] = pickup |
---|
[6711] | 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 |
---|
[7163] | 274 | return number+1 |
---|
[6711] | 275 | end |
---|
| 276 | |
---|
| 277 | function P.InventoryUseButton_clicked(e) |
---|
[7163] | 278 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 279 | local pickup = P.pickupsList[pickupIndex] |
---|
| 280 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) |
---|
[6711] | 281 | end |
---|
| 282 | |
---|
[7163] | 283 | function P.InventoryUnuseButton_clicked(e) |
---|
| 284 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 285 | local pickup = P.pickupsList[pickupIndex] |
---|
| 286 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) |
---|
[7163] | 287 | end |
---|
| 288 | |
---|
[6711] | 289 | function P.InventoryDropButton_clicked(e) |
---|
[7163] | 290 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 291 | local pickup = P.pickupsList[pickupIndex] |
---|
| 292 | orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) |
---|
[6711] | 293 | end |
---|
| 294 | |
---|
| 295 | function P.InventoryDetailsButton_clicked(e) |
---|
[7163] | 296 | local pickupIndex = P.windowToPickupHelper(e) |
---|
| 297 | P.createDetailsWindow(pickupIndex) |
---|
[6711] | 298 | end |
---|
| 299 | |
---|
[7163] | 300 | function P.InventoryUseDetailButton_clicked(e) |
---|
| 301 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 302 | local pickup = P.detailPickups[pickupIndex] |
---|
| 303 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) |
---|
[7163] | 304 | end |
---|
| 305 | |
---|
| 306 | function P.InventoryUnuseDetailButton_clicked(e) |
---|
| 307 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 308 | local pickup = P.detailPickups[pickupIndex] |
---|
| 309 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) |
---|
[7163] | 310 | end |
---|
| 311 | |
---|
| 312 | function P.InventoryDropDetailButton_clicked(e) |
---|
| 313 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 314 | local pickup = P.detailPickups[pickupIndex] |
---|
| 315 | orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) |
---|
[7163] | 316 | end |
---|
| 317 | |
---|
[6711] | 318 | function P.closeDetailWindow(e) |
---|
| 319 | --Get some numbers from the window |
---|
| 320 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 321 | local name = we.window:getName() |
---|
| 322 | local match = string.gmatch(name, "%d+") |
---|
| 323 | local detailNr = tonumber(match()) |
---|
| 324 | |
---|
[9348] | 325 | P.destroyDetailWindow(detailNr) |
---|
| 326 | end |
---|
| 327 | |
---|
| 328 | function P.destroyDetailWindow(detailNr) |
---|
[7163] | 329 | local window = P.detailsWindows[detailNr] |
---|
[6711] | 330 | winMgr:destroyWindow(window) |
---|
[7163] | 331 | P.detailsWindows[detailNr] = nil |
---|
| 332 | P.detailPickups[detailNr] = nil |
---|
[6711] | 333 | end |
---|
| 334 | |
---|
| 335 | function P.InventoryBackButton_clicked(e) |
---|
[6750] | 336 | orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory") |
---|
[6711] | 337 | end |
---|
| 338 | |
---|
[5661] | 339 | return P |
---|