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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. Containers = {}
  2. -- private variables
  3. local m_containers = {}
  4. -- public functions
  5. function Containers.clean()
  6. m_containers = {}
  7. end
  8. function Containers.getFreeContainerId()
  9. for i=0,15 do
  10. if not m_containers[i] then
  11. return i
  12. end
  13. end
  14. return 0
  15. end
  16. -- hooked events
  17. function Containers.onContainerOpen(containerId, itemId, name, capacity, hasParent, items)
  18. local container = m_containers[containerId]
  19. if container then
  20. Game.gameRightPanel:removeChild(container)
  21. end
  22. container = displayUI('container.otui', { parent = Game.gameRightPanel })
  23. name = name:sub(1,1):upper() .. name:sub(2)
  24. container:setText(name)
  25. -- set icon, itemid
  26. -- closebutton
  27. -- resize
  28. if hasParent then
  29. -- parent button
  30. end
  31. container.itemCount = #items
  32. container.capacity = capacity
  33. for i=1,capacity do
  34. local itemWidget = UIItem.create()
  35. itemWidget:setStyle('Item')
  36. container:addChild(itemWidget)
  37. itemWidget.position = {x=65535, y=containerId+64, z=i-1}
  38. if i <= #items then
  39. local item = items[i]
  40. item:setPosition(itemWidget.position)
  41. itemWidget:setItem(item)
  42. end
  43. end
  44. m_containers[containerId] = container
  45. end
  46. function Containers.onContainerClose(containerId)
  47. local container = m_containers[containerId]
  48. if container then
  49. Game.gameRightPanel:removeChild(container)
  50. end
  51. m_containers[containerId] = nil
  52. end
  53. function Containers.onContainerAddItem(containerId, item)
  54. local container = m_containers[containerId]
  55. if not container or not item or container.itemCount >= container.capacity then return end
  56. local i = container.itemCount
  57. while i >= 1 do
  58. local itemWidget = container:getChildByIndex(i)
  59. if not itemWidget then return end
  60. local nextItemWidget = container:getChildByIndex(i+1)
  61. if not nextItemWidget then return end
  62. local swapItem = itemWidget:getItem()
  63. if swapItem then
  64. swapItem:setPosition(nextItemWidget.position)
  65. nextItemWidget:setItem(swapItem)
  66. end
  67. i = i - 1
  68. end
  69. local itemWidget = container:getChildByIndex(1)
  70. if not itemWidget then return end
  71. item:setPosition(itemWidget.position)
  72. itemWidget:setItem(item)
  73. container.itemCount = container.itemCount + 1
  74. end
  75. function Containers.onContainerUpdateItem(containerId, slot, item)
  76. local container = m_containers[containerId]
  77. if not container then return end
  78. local itemWidget = container:getChildByIndex(slot + 1)
  79. if not itemWidget then return end
  80. itemWidget:setItem(item)
  81. item:setPosition(itemWidget.position)
  82. end
  83. function Containers.onContainerRemoveItem(containerId, slot)
  84. local container = m_containers[containerId]
  85. if not container then return end
  86. local itemWidget = container:getChildByIndex(slot+1)
  87. if not itemWidget then return end
  88. itemWidget:setItem(nil)
  89. for i=slot,container.itemCount-2 do
  90. local itemWidget = container:getChildByIndex(i+1)
  91. if not itemWidget then return end
  92. local nextItemWidget = container:getChildByIndex(i+2)
  93. if not nextItemWidget then return end
  94. local item = nextItemWidget:getItem()
  95. local pos = item:getPosition()
  96. pos.z = pos.z - 1
  97. item:setPosition(pos)
  98. itemWidget:setItem(item)
  99. nextItemWidget:setItem(nil)
  100. end
  101. container.itemCount = container.itemCount - 1
  102. end
  103. connect(Game, { onGameStart = Containers.clean,
  104. onGameEnd = Containers.clean,
  105. onContainerOpen = Containers.onContainerOpen,
  106. onContainerClose = Containers.onContainerClose,
  107. onContainerAddItem = Containers.onContainerAddItem,
  108. onContainerUpdateItem = Containers.onContainerUpdateItem,
  109. onContainerRemoveItem = Containers.onContainerRemoveItem })