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