2012-07-24 07:30:08 +02:00
|
|
|
function init()
|
2013-01-18 23:39:11 +01:00
|
|
|
g_ui.importStyle('container')
|
2012-07-24 07:30:08 +02:00
|
|
|
|
|
|
|
connect(Container, { onOpen = onContainerOpen,
|
|
|
|
onClose = onContainerClose,
|
2014-07-15 23:19:08 +02:00
|
|
|
onSizeChange = onContainerChangeSize,
|
|
|
|
onUpdateItem = onContainerUpdateItem })
|
2012-07-24 07:30:08 +02:00
|
|
|
connect(Game, { onGameEnd = clean() })
|
|
|
|
|
|
|
|
reloadContainers()
|
|
|
|
end
|
|
|
|
|
|
|
|
function terminate()
|
|
|
|
disconnect(Container, { onOpen = onContainerOpen,
|
|
|
|
onClose = onContainerClose,
|
2014-07-15 23:19:08 +02:00
|
|
|
onSizeChange = onContainerChangeSize,
|
|
|
|
onUpdateItem = onContainerUpdateItem })
|
2012-07-24 07:30:08 +02:00
|
|
|
disconnect(Game, { onGameEnd = clean() })
|
|
|
|
end
|
|
|
|
|
|
|
|
function reloadContainers()
|
|
|
|
clean()
|
2012-08-22 16:21:02 +02:00
|
|
|
for _,container in pairs(g_game.getContainers()) do
|
2012-07-24 07:30:08 +02:00
|
|
|
onContainerOpen(container)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function clean()
|
|
|
|
for containerid,container in pairs(g_game.getContainers()) do
|
2013-02-23 07:38:52 +01:00
|
|
|
destroy(container)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function destroy(container)
|
|
|
|
if container.window then
|
|
|
|
container.window:destroy()
|
|
|
|
container.window = nil
|
|
|
|
container.itemsPanel = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
end
|
2012-04-03 16:15:11 +02:00
|
|
|
|
2012-07-26 08:10:28 +02:00
|
|
|
function refreshContainerItems(container)
|
2012-04-03 01:09:47 +02:00
|
|
|
for slot=0,container:getCapacity()-1 do
|
|
|
|
local itemWidget = container.itemsPanel:getChildById('item' .. slot)
|
|
|
|
itemWidget:setItem(container:getItem(slot))
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
2014-07-15 23:19:08 +02:00
|
|
|
|
|
|
|
if container:hasPages() then
|
|
|
|
refreshContainerPages(container)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function toggleContainerPages(containerWindow, pages)
|
|
|
|
containerWindow:getChildById('miniwindowScrollBar'):setMarginTop(pages and 42 or 22)
|
|
|
|
containerWindow:getChildById('contentsPanel'):setMarginTop(pages and 42 or 22)
|
|
|
|
containerWindow:getChildById('pagePanel'):setVisible(pages)
|
|
|
|
end
|
|
|
|
|
|
|
|
function refreshContainerPages(container)
|
|
|
|
local currentPage = 1 + math.floor(container:getFirstIndex() / container:getCapacity())
|
|
|
|
local pages = 1 + math.floor(math.max(0, (container:getSize() - 1)) / container:getCapacity())
|
|
|
|
container.window:recursiveGetChildById('pageLabel'):setText(string.format('Page %i of %i', currentPage, pages))
|
|
|
|
|
|
|
|
local prevPageButton = container.window:recursiveGetChildById('prevPageButton')
|
|
|
|
if currentPage == 1 then
|
|
|
|
prevPageButton:setEnabled(false)
|
|
|
|
else
|
|
|
|
prevPageButton:setEnabled(true)
|
|
|
|
prevPageButton.onClick = function() g_game.seekInContainer(container:getId(), container:getFirstIndex() - container:getCapacity()) end
|
|
|
|
end
|
|
|
|
|
|
|
|
local nextPageButton = container.window:recursiveGetChildById('nextPageButton')
|
|
|
|
if currentPage >= pages then
|
|
|
|
nextPageButton:setEnabled(false)
|
|
|
|
else
|
|
|
|
nextPageButton:setEnabled(true)
|
|
|
|
nextPageButton.onClick = function() g_game.seekInContainer(container:getId(), container:getFirstIndex() + container:getCapacity()) end
|
|
|
|
end
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-07-26 08:10:28 +02:00
|
|
|
function onContainerOpen(container, previousContainer)
|
2012-04-03 01:09:47 +02:00
|
|
|
local containerWindow
|
|
|
|
if previousContainer then
|
|
|
|
containerWindow = previousContainer.window
|
|
|
|
previousContainer.window = nil
|
|
|
|
previousContainer.itemsPanel = nil
|
|
|
|
else
|
2012-07-24 07:30:08 +02:00
|
|
|
containerWindow = g_ui.createWidget('ContainerWindow', modules.game_interface.getRightPanel())
|
2012-04-03 01:09:47 +02:00
|
|
|
end
|
|
|
|
containerWindow:setId('container' .. container:getId())
|
|
|
|
local containerPanel = containerWindow:getChildById('contentsPanel')
|
|
|
|
local containerItemWidget = containerWindow:getChildById('containerItemWidget')
|
|
|
|
containerWindow.onClose = function()
|
|
|
|
g_game.close(container)
|
|
|
|
containerWindow:hide()
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-04-30 18:40:12 +02:00
|
|
|
-- this disables scrollbar auto hiding
|
|
|
|
local scrollbar = containerWindow:getChildById('miniwindowScrollBar')
|
2012-07-13 01:40:55 +02:00
|
|
|
scrollbar:mergeStyle({ ['$!on'] = { }})
|
2012-04-30 18:40:12 +02:00
|
|
|
|
2012-04-03 01:09:47 +02:00
|
|
|
local upButton = containerWindow:getChildById('upButton')
|
|
|
|
upButton.onClick = function()
|
|
|
|
g_game.openParent(container)
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
2012-04-03 01:09:47 +02:00
|
|
|
upButton:setVisible(container:hasParent())
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-04-03 01:09:47 +02:00
|
|
|
local name = container:getName()
|
|
|
|
name = name:sub(1,1):upper() .. name:sub(2)
|
|
|
|
containerWindow:setText(name)
|
2012-01-14 20:31:20 +01:00
|
|
|
|
2012-05-12 06:52:16 +02:00
|
|
|
containerItemWidget:setItem(container:getContainerItem())
|
2012-02-08 00:54:33 +01:00
|
|
|
|
2012-04-03 01:09:47 +02:00
|
|
|
containerPanel:destroyChildren()
|
|
|
|
for slot=0,container:getCapacity()-1 do
|
2012-06-26 00:13:30 +02:00
|
|
|
local itemWidget = g_ui.createWidget('Item', containerPanel)
|
2012-04-03 01:09:47 +02:00
|
|
|
itemWidget:setId('item' .. slot)
|
|
|
|
itemWidget:setItem(container:getItem(slot))
|
2012-07-13 01:40:55 +02:00
|
|
|
itemWidget:setMargin(0)
|
2012-04-03 01:09:47 +02:00
|
|
|
itemWidget.position = container:getSlotPosition(slot)
|
2014-07-15 23:19:08 +02:00
|
|
|
|
|
|
|
if not container:isUnlocked() then
|
|
|
|
itemWidget:setBorderColor('red')
|
|
|
|
end
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-04-03 01:09:47 +02:00
|
|
|
container.window = containerWindow
|
|
|
|
container.itemsPanel = containerPanel
|
2012-08-18 07:04:01 +02:00
|
|
|
|
2014-07-15 23:19:08 +02:00
|
|
|
toggleContainerPages(containerWindow, container:hasPages())
|
|
|
|
refreshContainerPages(container)
|
|
|
|
|
2012-08-18 07:04:01 +02:00
|
|
|
local layout = containerPanel:getLayout()
|
|
|
|
local cellSize = layout:getCellSize()
|
2012-08-22 16:21:02 +02:00
|
|
|
containerWindow:setContentMinimumHeight(cellSize.height)
|
2012-08-18 07:04:01 +02:00
|
|
|
containerWindow:setContentMaximumHeight(cellSize.height*layout:getNumLines())
|
2012-08-21 01:56:16 +02:00
|
|
|
|
|
|
|
if not previousContainer then
|
|
|
|
local filledLines = math.max(math.ceil(container:getItemsCount() / layout:getNumColumns()), 1)
|
|
|
|
containerWindow:setContentHeight(filledLines*cellSize.height)
|
|
|
|
end
|
2012-08-21 23:40:47 +02:00
|
|
|
|
|
|
|
containerWindow:setup()
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:41:59 +02:00
|
|
|
function onContainerClose(container)
|
2013-02-23 07:38:52 +01:00
|
|
|
destroy(container)
|
2012-01-13 01:31:39 +01:00
|
|
|
end
|
|
|
|
|
2014-07-15 23:19:08 +02:00
|
|
|
function onContainerChangeSize(container, size)
|
2012-04-03 16:15:11 +02:00
|
|
|
if not container.window then return end
|
2012-04-03 01:09:47 +02:00
|
|
|
refreshContainerItems(container)
|
2012-01-20 03:33:11 +01:00
|
|
|
end
|
2012-01-13 01:31:39 +01:00
|
|
|
|
2012-07-24 07:41:59 +02:00
|
|
|
function onContainerUpdateItem(container, slot, item, oldItem)
|
2012-04-03 16:15:11 +02:00
|
|
|
if not container.window then return end
|
2012-04-03 01:09:47 +02:00
|
|
|
local itemWidget = container.itemsPanel:getChildById('item' .. slot)
|
2012-01-20 03:33:11 +01:00
|
|
|
itemWidget:setItem(item)
|
|
|
|
end
|