[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) |
---|
[11710] | 53 | if pickup.inUse == false then |
---|
| 54 | useButton:setText("use") |
---|
[7504] | 55 | if pickup.usable == false then |
---|
[7163] | 56 | useButton:setEnabled(false) |
---|
| 57 | end |
---|
| 58 | else |
---|
[11710] | 59 | useButton:setText("unuse") |
---|
[7504] | 60 | if pickup.unusable == false then |
---|
[7163] | 61 | useButton:setEnabled(false) |
---|
| 62 | end |
---|
| 63 | end |
---|
| 64 | |
---|
[7504] | 65 | if pickup.pickedUp == false then |
---|
[7163] | 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 |
---|
[6711] | 79 | |
---|
[5559] | 80 | end |
---|
| 81 | |
---|
[6711] | 82 | function P.createInventory() |
---|
| 83 | local pickupManager = orxonox.PickupManager:getInstance() |
---|
| 84 | |
---|
| 85 | local root = winMgr:getWindow("orxonox/PickupInventory/Inventory") |
---|
[6750] | 86 | P.wrapper = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/PickupInventory/Inventory/Wrapper") |
---|
[6711] | 87 | P.wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1,0),CEGUI.UDim(1,0))) |
---|
| 88 | root:addChildWindow(P.wrapper) |
---|
| 89 | |
---|
[7163] | 90 | P.pickupsList = {} |
---|
| 91 | |
---|
| 92 | local numPickups = pickupManager:getNumPickups() |
---|
| 93 | local counter = 1 |
---|
[6711] | 94 | local offset = 0 |
---|
[7163] | 95 | while counter <= numPickups do |
---|
| 96 | local pickup = pickupManager:popPickup() |
---|
| 97 | table.insert(P.pickupsList, pickup) |
---|
| 98 | local window = P.createPickupEntry(counter, pickup) |
---|
[6711] | 99 | window:setYPosition(CEGUI.UDim(0,offset)) |
---|
[7163] | 100 | offset = offset + window:getHeight():asAbsolute(1) |
---|
[6711] | 101 | P.wrapper:addChildWindow(window) |
---|
[7163] | 102 | counter = counter + 1 |
---|
[6711] | 103 | end |
---|
[7163] | 104 | |
---|
[6711] | 105 | end |
---|
[6417] | 106 | |
---|
[7163] | 107 | function P.createPickupEntry(index, pickup) |
---|
[9348] | 108 | local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName) |
---|
[6417] | 109 | |
---|
[7163] | 110 | local name = "orxonox/PickupInventory/Box/Pickup" .. index |
---|
[6417] | 111 | |
---|
[7163] | 112 | local item = winMgr:createWindow("MenuWidgets/StaticText", name) |
---|
| 113 | item:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(0, P.imageHeight))) |
---|
| 114 | item:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0))) |
---|
[6711] | 115 | |
---|
[7163] | 116 | local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image") |
---|
| 117 | image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation()) |
---|
| 118 | image:setProperty("BackgroundEnabled", "set:False") |
---|
| 119 | image:setProperty("FrameEnabled", "set:True") |
---|
| 120 | image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight), CEGUI.UDim(0, P.imageHeight))) |
---|
| 121 | item:addChildWindow(image) |
---|
[6711] | 122 | |
---|
[7163] | 123 | local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title") |
---|
| 124 | title:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.imageHeight+5), CEGUI.UDim(0, (P.imageHeight-P.textHeight)/2))) |
---|
| 125 | title:setSize(CEGUI.UVector2(CEGUI.UDim(0.3, 0), CEGUI.UDim(0, P.textHeight))) |
---|
| 126 | title:setText(representation:getPickupName()) |
---|
| 127 | title:setProperty("FrameEnabled", "set:False") |
---|
| 128 | item:addChildWindow(title) |
---|
| 129 | |
---|
| 130 | local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/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.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight))) |
---|
[7504] | 133 | if pickup.inUse == false then |
---|
[6711] | 134 | useButton:setText("use") |
---|
[7504] | 135 | if pickup.usable == false then |
---|
[7163] | 136 | useButton:setEnabled(false) |
---|
| 137 | end |
---|
| 138 | else |
---|
| 139 | useButton:setText("unuse") |
---|
[7504] | 140 | if pickup.unusable == false then |
---|
[7163] | 141 | useButton:setEnabled(false) |
---|
| 142 | end |
---|
[6711] | 143 | end |
---|
[11710] | 144 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseButton_clicked") |
---|
[7163] | 145 | item:addChildWindow(useButton) |
---|
| 146 | |
---|
| 147 | local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/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.UVector2(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:addChildWindow(dropButton) |
---|
| 153 | |
---|
| 154 | local detailsButton = winMgr:createWindow("MenuWidgets/Button", name .. "/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.UVector2(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:addChildWindow(detailsButton) |
---|
| 160 | |
---|
| 161 | return item |
---|
[6711] | 162 | end |
---|
| 163 | |
---|
[7163] | 164 | function P.cleanup(destroyDetails) |
---|
[6711] | 165 | if P.wrapper ~= nil then |
---|
| 166 | winMgr:destroyWindow(P.wrapper) |
---|
| 167 | end |
---|
| 168 | |
---|
| 169 | --Destroy details windows. |
---|
[7163] | 170 | if destroyDetails == false then |
---|
| 171 | return |
---|
| 172 | end |
---|
[6711] | 173 | for k,v in pairs(P.detailsWindows) do |
---|
| 174 | if v ~= nil then |
---|
[9348] | 175 | P.destroyDetailWindow(k) |
---|
[5587] | 176 | end |
---|
| 177 | end |
---|
| 178 | end |
---|
| 179 | |
---|
[7163] | 180 | function P.windowToPickupHelper(e) |
---|
[6711] | 181 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 182 | local name = we.window:getName() |
---|
[6417] | 183 | |
---|
[6711] | 184 | local match = string.gmatch(name, "%d+") |
---|
[7163] | 185 | local pickupIndex = tonumber(match()) |
---|
[6711] | 186 | |
---|
[7163] | 187 | return pickupIndex |
---|
[5587] | 188 | end |
---|
| 189 | |
---|
[7163] | 190 | function P.createDetailsWindow(pickupIndex) |
---|
| 191 | local pickup = P.pickupsList[pickupIndex] |
---|
[9348] | 192 | local representation = orxonox.PickupManager:getInstance():getRepresentation(pickup.representationName) |
---|
[7163] | 193 | |
---|
| 194 | local index = P.getNewDetailNumber() |
---|
| 195 | local name = "orxonox/PickupInventory/Details" .. index |
---|
[6711] | 196 | |
---|
[6750] | 197 | local window = winMgr:createWindow("MenuWidgets/FrameWindow", name) |
---|
[6711] | 198 | window:setSize(CEGUI.UVector2(CEGUI.UDim(0.5,0),CEGUI.UDim(0.4,0))) |
---|
[6746] | 199 | orxonox.GUIManager:subscribeEventHelper(window, "CloseClicked", P.name .. ".closeDetailWindow") |
---|
[6711] | 200 | |
---|
| 201 | local root = winMgr:getWindow("orxonox/PickupInventory/Background") |
---|
| 202 | root:addChildWindow(window) |
---|
| 203 | |
---|
| 204 | local wrapper = winMgr:createWindow("DefaultWindow", name .. "/Wrapper") |
---|
| 205 | wrapper:setSize(CEGUI.UVector2(CEGUI.UDim(1, -20),CEGUI.UDim(1, -50))) |
---|
| 206 | wrapper:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10),CEGUI.UDim(0, 40))) |
---|
| 207 | window:addChildWindow(wrapper) |
---|
| 208 | |
---|
[6750] | 209 | local title = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Title") |
---|
[7163] | 210 | title:setText(representation:getPickupName()) |
---|
| 211 | title:setHeight(CEGUI.UDim(0, P.textHeight)) |
---|
[6711] | 212 | title:setProperty("FrameEnabled", "set:False") |
---|
| 213 | title:setProperty("BackgroundEnabled", "set:False") |
---|
| 214 | wrapper:addChildWindow(title) |
---|
| 215 | |
---|
[6750] | 216 | local image = winMgr:createWindow("MenuWidgets/StaticImage", name .. "/Image") |
---|
[7163] | 217 | image:setProperty("Image", "set:PickupInventory image:" .. representation:getInventoryRepresentation()) |
---|
[6711] | 218 | image:setProperty("BackgroundEnabled", "set:False") |
---|
| 219 | image:setProperty("FrameEnabled", "set:True") |
---|
[7163] | 220 | image:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize), CEGUI.UDim(0, P.detailImageSize))) |
---|
| 221 | image:setYPosition(CEGUI.UDim(0, P.textHeight + 5)) |
---|
[6711] | 222 | wrapper:addChildWindow(image) |
---|
| 223 | |
---|
[6750] | 224 | local box = winMgr:createWindow("MenuWidgets/ScrollablePane", name .. "/Description") |
---|
[7163] | 225 | box:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, -1*(P.detailImageSize + 10)),CEGUI.UDim(1, -(P.textHeight + 5 + P.textHeight + 20)))) |
---|
| 226 | box:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize + 10),CEGUI.UDim(0, P.textHeight + 5))) |
---|
| 227 | local description = winMgr:createWindow("MenuWidgets/StaticText", name .. "/Description/Text") |
---|
| 228 | description:setText(representation:getPickupDescription()) |
---|
[6711] | 229 | description:setProperty("HorzFormatting", "WordWrapLeftAligned") |
---|
| 230 | description:setProperty("VertFormatting", "TopAligned") |
---|
| 231 | box:addChildWindow(description) |
---|
| 232 | wrapper:addChildWindow(box) |
---|
[7163] | 233 | |
---|
[6750] | 234 | local useButton = winMgr:createWindow("MenuWidgets/Button", name .. "/UseButton") |
---|
[7163] | 235 | useButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10),CEGUI.UDim(1, -40))) |
---|
| 236 | useButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight))) |
---|
[7504] | 237 | if pickup.inUse == false then |
---|
[7163] | 238 | useButton:setText("use") |
---|
[7504] | 239 | if pickup.usable == false then |
---|
[7163] | 240 | useButton:setEnabled(false) |
---|
| 241 | end |
---|
| 242 | else |
---|
| 243 | useButton:setText("unuse") |
---|
[7504] | 244 | if pickup.unusable == false then |
---|
[7163] | 245 | useButton:setEnabled(false) |
---|
| 246 | end |
---|
| 247 | end |
---|
[11710] | 248 | orxonox.GUIManager:subscribeEventHelper(useButton, "Clicked", P.name .. ".InventoryUseUnuseDetailButton_clicked") |
---|
[6711] | 249 | wrapper:addChildWindow(useButton) |
---|
| 250 | |
---|
[6750] | 251 | local dropButton = winMgr:createWindow("MenuWidgets/Button", name .. "/DropButton") |
---|
[7163] | 252 | dropButton:setPosition(CEGUI.UVector2(CEGUI.UDim(0, P.detailImageSize+10+P.buttonWidth+10),CEGUI.UDim(1, -40))) |
---|
| 253 | dropButton:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.buttonWidth), CEGUI.UDim(0, P.textHeight))) |
---|
[6711] | 254 | dropButton:setText("drop") |
---|
[7163] | 255 | orxonox.GUIManager:subscribeEventHelper(dropButton, "Clicked", P.name .. ".InventoryDropDetailButton_clicked") |
---|
[6711] | 256 | wrapper:addChildWindow(dropButton) |
---|
[7163] | 257 | |
---|
| 258 | P.detailsWindows[index] = window |
---|
| 259 | P.detailPickups[index] = pickup |
---|
[6711] | 260 | |
---|
| 261 | end |
---|
| 262 | |
---|
| 263 | function P.getNewDetailNumber() |
---|
| 264 | local number = table.getn(P.detailsWindows) |
---|
| 265 | for k,v in pairs(P.detailsWindows) do |
---|
| 266 | if v == nil then |
---|
| 267 | number = k-1 |
---|
| 268 | end |
---|
| 269 | end |
---|
[7163] | 270 | return number+1 |
---|
[6711] | 271 | end |
---|
| 272 | |
---|
[11710] | 273 | function P.InventoryUseUnuseButton_clicked(e) |
---|
[7163] | 274 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 275 | local pickup = P.pickupsList[pickupIndex] |
---|
[11710] | 276 | if pickup.inUse == false then |
---|
| 277 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) |
---|
| 278 | else |
---|
| 279 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) |
---|
| 280 | end |
---|
[6711] | 281 | end |
---|
| 282 | |
---|
| 283 | function P.InventoryDropButton_clicked(e) |
---|
[7163] | 284 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 285 | local pickup = P.pickupsList[pickupIndex] |
---|
| 286 | orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) |
---|
[6711] | 287 | end |
---|
| 288 | |
---|
| 289 | function P.InventoryDetailsButton_clicked(e) |
---|
[7163] | 290 | local pickupIndex = P.windowToPickupHelper(e) |
---|
| 291 | P.createDetailsWindow(pickupIndex) |
---|
[6711] | 292 | end |
---|
| 293 | |
---|
[11710] | 294 | function P.InventoryUseUnuseDetailButton_clicked(e) |
---|
[7163] | 295 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 296 | local pickup = P.detailPickups[pickupIndex] |
---|
[11710] | 297 | if pickup.inUse == false then |
---|
| 298 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, true) |
---|
| 299 | else |
---|
| 300 | orxonox.PickupManager:getInstance():usePickup(pickup.pickup, false) |
---|
| 301 | end |
---|
[7163] | 302 | end |
---|
| 303 | |
---|
| 304 | function P.InventoryDropDetailButton_clicked(e) |
---|
| 305 | local pickupIndex = P.windowToPickupHelper(e) |
---|
[7504] | 306 | local pickup = P.detailPickups[pickupIndex] |
---|
| 307 | orxonox.PickupManager:getInstance():dropPickup(pickup.pickup) |
---|
[7163] | 308 | end |
---|
| 309 | |
---|
[6711] | 310 | function P.closeDetailWindow(e) |
---|
| 311 | --Get some numbers from the window |
---|
| 312 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 313 | local name = we.window:getName() |
---|
| 314 | local match = string.gmatch(name, "%d+") |
---|
| 315 | local detailNr = tonumber(match()) |
---|
| 316 | |
---|
[9348] | 317 | P.destroyDetailWindow(detailNr) |
---|
| 318 | end |
---|
| 319 | |
---|
| 320 | function P.destroyDetailWindow(detailNr) |
---|
[7163] | 321 | local window = P.detailsWindows[detailNr] |
---|
[6711] | 322 | winMgr:destroyWindow(window) |
---|
[7163] | 323 | P.detailsWindows[detailNr] = nil |
---|
| 324 | P.detailPickups[detailNr] = nil |
---|
[6711] | 325 | end |
---|
| 326 | |
---|
| 327 | function P.InventoryBackButton_clicked(e) |
---|
[6750] | 328 | orxonox.CommandExecutor:execute("OrxonoxOverlay toggleVisibility PickupInventory") |
---|
[6711] | 329 | end |
---|
| 330 | |
---|
[5661] | 331 | return P |
---|