No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

containers.lua 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. Containers = {}
  2. local function refreshContainerItems(container)
  3. for slot=0,container:getCapacity()-1 do
  4. local itemWidget = container.itemsPanel:getChildById('item' .. slot)
  5. itemWidget:setItem(container:getItem(slot))
  6. end
  7. end
  8. local function onContainerOpen(container, previousContainer)
  9. local containerWindow
  10. if previousContainer then
  11. containerWindow = previousContainer.window
  12. previousContainer.window = nil
  13. previousContainer.itemsPanel = nil
  14. else
  15. containerWindow = g_ui.createWidget('ContainerWindow', GameInterface.getRightPanel())
  16. end
  17. containerWindow:setId('container' .. container:getId())
  18. local containerPanel = containerWindow:getChildById('contentsPanel')
  19. local containerItemWidget = containerWindow:getChildById('containerItemWidget')
  20. containerWindow.onClose = function()
  21. g_game.close(container)
  22. containerWindow:hide()
  23. end
  24. -- this disables scrollbar auto hiding
  25. local scrollbar = containerWindow:getChildById('miniwindowScrollBar')
  26. scrollbar:mergeStyle({ ['$!on'] = { }})
  27. local upButton = containerWindow:getChildById('upButton')
  28. upButton.onClick = function()
  29. g_game.openParent(container)
  30. end
  31. upButton:setVisible(container:hasParent())
  32. local name = container:getName()
  33. name = name:sub(1,1):upper() .. name:sub(2)
  34. containerWindow:setText(name)
  35. containerItemWidget:setItem(container:getContainerItem())
  36. containerPanel:destroyChildren()
  37. for slot=0,container:getCapacity()-1 do
  38. local itemWidget = g_ui.createWidget('Item', containerPanel)
  39. itemWidget:setId('item' .. slot)
  40. itemWidget:setItem(container:getItem(slot))
  41. itemWidget:setMargin(0)
  42. itemWidget.position = container:getSlotPosition(slot)
  43. end
  44. container.window = containerWindow
  45. container.itemsPanel = containerPanel
  46. end
  47. local function onContainerClose(container)
  48. if container.window then container.window:destroy() end
  49. end
  50. local function onContainerAddItem(container, slot, item)
  51. if not container.window then return end
  52. refreshContainerItems(container)
  53. end
  54. local function onContainerUpdateItem(container, slot, item, oldItem)
  55. if not container.window then return end
  56. local itemWidget = container.itemsPanel:getChildById('item' .. slot)
  57. itemWidget:setItem(item)
  58. end
  59. local function onContainerRemoveItem(container, slot, item)
  60. if not container.window then return end
  61. refreshContainerItems(container)
  62. end
  63. function Containers.init()
  64. g_ui.importStyle('container.otui')
  65. connect(Container, { onOpen = onContainerOpen,
  66. onClose = onContainerClose,
  67. onAddItem = onContainerAddItem,
  68. onUpdateItem = onContainerUpdateItem,
  69. onRemoveItem = onContainerRemoveItem })
  70. connect(Game, { onGameEnd = Containers.clean() })
  71. Containers.reloadContainers()
  72. end
  73. function Containers.terminate()
  74. disconnect(Container, { onOpen = onContainerOpen,
  75. onClose = onContainerClose,
  76. onAddItem = onContainerAddItem,
  77. onUpdateItem = onContainerUpdateItem,
  78. onRemoveItem = onContainerRemoveItem })
  79. disconnect(Game, { onGameEnd = Containers.clean() })
  80. Containers = nil
  81. end
  82. function Containers.reloadContainers()
  83. Containers.clean()
  84. for containerid,container in pairs(g_game.getContainers()) do
  85. onContainerOpen(container)
  86. end
  87. end
  88. function Containers.clean()
  89. for containerid,container in pairs(g_game.getContainers()) do
  90. if container.window then
  91. container.window:destroy()
  92. container.window = nil
  93. container.itemsPanel = nil
  94. end
  95. end
  96. end