2012-01-13 01:31:39 +01:00
|
|
|
Containers = {}
|
|
|
|
|
|
|
|
-- private variables
|
2012-03-22 22:47:52 +01:00
|
|
|
local containers = {}
|
2012-01-13 01:31:39 +01:00
|
|
|
|
|
|
|
-- public functions
|
|
|
|
function Containers.clean()
|
2012-03-22 22:47:52 +01:00
|
|
|
containers = {}
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Containers.getFreeContainerId()
|
|
|
|
for i=0,15 do
|
2012-03-22 22:47:52 +01:00
|
|
|
if not containers[i] then
|
2012-01-13 01:31:39 +01:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hooked events
|
2012-02-08 22:23:15 +01:00
|
|
|
function Containers.onOpenContainer(containerId, itemId, name, capacity, hasParent, items)
|
2012-03-22 22:47:52 +01:00
|
|
|
local container = containers[containerId]
|
2012-01-13 01:31:39 +01:00
|
|
|
if container then
|
2012-03-22 22:47:52 +01:00
|
|
|
GameInterface.getRightPanel():removeChild(container)
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-03-22 22:47:52 +01:00
|
|
|
container = displayUI('container.otui', GameInterface.getRightPanel())
|
2012-01-14 20:31:20 +01:00
|
|
|
name = name:sub(1,1):upper() .. name:sub(2)
|
2012-01-13 01:31:39 +01:00
|
|
|
container:setText(name)
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-01-13 01:31:39 +01:00
|
|
|
-- set icon, itemid
|
|
|
|
-- closebutton
|
|
|
|
-- resize
|
|
|
|
if hasParent then
|
|
|
|
-- parent button
|
|
|
|
end
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
container.itemCount = #items
|
2012-01-13 01:31:39 +01:00
|
|
|
container.capacity = capacity
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-01-13 01:31:39 +01:00
|
|
|
for i=1,capacity do
|
2012-01-20 03:33:11 +01:00
|
|
|
local itemWidget = UIItem.create()
|
|
|
|
itemWidget:setStyle('Item')
|
|
|
|
container:addChild(itemWidget)
|
|
|
|
itemWidget.position = {x=65535, y=containerId+64, z=i-1}
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
if i <= #items then
|
|
|
|
local item = items[i]
|
2012-01-30 01:00:12 +01:00
|
|
|
item:setPosition(itemWidget.position)
|
2012-01-20 03:33:11 +01:00
|
|
|
itemWidget:setItem(item)
|
|
|
|
end
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-03-22 22:47:52 +01:00
|
|
|
containers[containerId] = container
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-02-09 04:45:19 +01:00
|
|
|
function Containers.onCloseContainer(containerId)
|
2012-03-22 22:47:52 +01:00
|
|
|
local container = containers[containerId]
|
2012-01-13 01:31:39 +01:00
|
|
|
if container then
|
2012-03-22 22:47:52 +01:00
|
|
|
GameInterface.getRightPanel():removeChild(container)
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
2012-03-22 22:47:52 +01:00
|
|
|
containers[containerId] = nil
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Containers.onContainerAddItem(containerId, item)
|
2012-03-22 22:47:52 +01:00
|
|
|
local container = containers[containerId]
|
2012-01-13 01:31:39 +01:00
|
|
|
if not container or not item or container.itemCount >= container.capacity then return end
|
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local i = container.itemCount
|
|
|
|
while i >= 1 do
|
|
|
|
local itemWidget = container:getChildByIndex(i)
|
|
|
|
if not itemWidget then return end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local nextItemWidget = container:getChildByIndex(i+1)
|
|
|
|
if not nextItemWidget then return end
|
|
|
|
|
|
|
|
local swapItem = itemWidget:getItem()
|
|
|
|
if swapItem then
|
2012-01-30 01:00:12 +01:00
|
|
|
swapItem:setPosition(nextItemWidget.position)
|
2012-01-20 03:33:11 +01:00
|
|
|
nextItemWidget:setItem(swapItem)
|
|
|
|
end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
i = i - 1
|
|
|
|
end
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local itemWidget = container:getChildByIndex(1)
|
|
|
|
if not itemWidget then return end
|
2012-01-30 01:00:12 +01:00
|
|
|
item:setPosition(itemWidget.position)
|
2012-01-13 01:31:39 +01:00
|
|
|
itemWidget:setItem(item)
|
2012-01-20 03:33:11 +01:00
|
|
|
|
2012-01-13 01:31:39 +01:00
|
|
|
container.itemCount = container.itemCount + 1
|
2012-01-20 03:33:11 +01:00
|
|
|
end
|
2012-01-13 01:31:39 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
function Containers.onContainerUpdateItem(containerId, slot, item)
|
2012-03-22 22:47:52 +01:00
|
|
|
local container = containers[containerId]
|
2012-01-20 03:33:11 +01:00
|
|
|
if not container then return end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local itemWidget = container:getChildByIndex(slot + 1)
|
|
|
|
if not itemWidget then return end
|
|
|
|
itemWidget:setItem(item)
|
2012-01-30 01:00:12 +01:00
|
|
|
item:setPosition(itemWidget.position)
|
2012-01-20 03:33:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Containers.onContainerRemoveItem(containerId, slot)
|
2012-03-22 22:47:52 +01:00
|
|
|
local container = containers[containerId]
|
2012-01-20 03:33:11 +01:00
|
|
|
if not container then return end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local itemWidget = container:getChildByIndex(slot+1)
|
|
|
|
if not itemWidget then return end
|
|
|
|
itemWidget:setItem(nil)
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
for i=slot,container.itemCount-2 do
|
|
|
|
local itemWidget = container:getChildByIndex(i+1)
|
|
|
|
if not itemWidget then return end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local nextItemWidget = container:getChildByIndex(i+2)
|
|
|
|
if not nextItemWidget then return end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
local item = nextItemWidget:getItem()
|
2012-01-30 01:00:12 +01:00
|
|
|
local pos = item:getPosition()
|
2012-01-20 03:33:11 +01:00
|
|
|
pos.z = pos.z - 1
|
2012-01-30 01:00:12 +01:00
|
|
|
item:setPosition(pos)
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
itemWidget:setItem(item)
|
|
|
|
nextItemWidget:setItem(nil)
|
|
|
|
end
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
container.itemCount = container.itemCount - 1
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-02-08 22:23:15 +01:00
|
|
|
connect(g_game, { onGameStart = Containers.clean,
|
2012-02-08 00:54:33 +01:00
|
|
|
onGameEnd = Containers.clean,
|
2012-02-08 22:23:15 +01:00
|
|
|
onOpenContainer = Containers.onOpenContainer,
|
2012-02-09 04:45:19 +01:00
|
|
|
onCloseContainer = Containers.onCloseContainer,
|
2012-01-20 03:33:11 +01:00
|
|
|
onContainerAddItem = Containers.onContainerAddItem,
|
|
|
|
onContainerUpdateItem = Containers.onContainerUpdateItem,
|
|
|
|
onContainerRemoveItem = Containers.onContainerRemoveItem })
|
2012-02-08 00:54:33 +01:00
|
|
|
|