1function init()
2 connect(Container, { onOpen = onContainerOpen,
3 onClose = onContainerClose,
4 onSizeChange = onContainerChangeSize,
5 onUpdateItem = onContainerUpdateItem })
6 connect(g_game, { onGameEnd = clean })
7
8 reloadContainers()
9end
10
11function terminate()
12 disconnect(Container, { onOpen = onContainerOpen,
13 onClose = onContainerClose,
14 onSizeChange = onContainerChangeSize,
15 onUpdateItem = onContainerUpdateItem })
16 disconnect(g_game, { onGameEnd = clean })
17end
18
19function reloadContainers()
20 clean()
21 for _,container in pairs(g_game.getContainers()) do
22 onContainerOpen(container)
23 end
24end
25
26function clean()
27 for containerid,container in pairs(g_game.getContainers()) do
28 destroy(container)
29 end
30end
31
32function destroy(container)
33 if container.window then
34 container.window:destroy()
35 container.window = nil
36 container.itemsPanel = nil
37 end
38end
39
40function refreshContainerItems(container)
41 for slot=0,container:getCapacity()-1 do
42 local itemWidget = container.itemsPanel:getChildById('item' .. slot)
43 itemWidget:setItem(container:getItem(slot))
44 end
45
46 if container:hasPages() then
47 refreshContainerPages(container)
48 end
49end
50
51function toggleContainerPages(containerWindow, hasPages)
52 if hasPages == containerWindow.pagePanel:isOn() then
53 return
54 end
55 containerWindow.pagePanel:setOn(hasPages)
56 if hasPages then
57 containerWindow.miniwindowScrollBar:setMarginTop(containerWindow.miniwindowScrollBar:getMarginTop() + containerWindow.pagePanel:getHeight())
58 containerWindow.contentsPanel:setMarginTop(containerWindow.contentsPanel:getMarginTop() + containerWindow.pagePanel:getHeight())
59 else
60 containerWindow.miniwindowScrollBar:setMarginTop(containerWindow.miniwindowScrollBar:getMarginTop() - containerWindow.pagePanel:getHeight())
61 containerWindow.contentsPanel:setMarginTop(containerWindow.contentsPanel:getMarginTop() - containerWindow.pagePanel:getHeight())
62 end
63end
64
65function refreshContainerPages(container)
66 local currentPage = 1 + math.floor(container:getFirstIndex() / container:getCapacity())
67 local pages = 1 + math.floor(math.max(0, (container:getSize() - 1)) / container:getCapacity())
68 container.window:recursiveGetChildById('pageLabel'):setText(string.format('Page %i of %i', currentPage, pages))
69
70 local prevPageButton = container.window:recursiveGetChildById('prevPageButton')
71 if currentPage == 1 then
72 prevPageButton:setEnabled(false)
73 else
74 prevPageButton:setEnabled(true)
75 prevPageButton.onClick = function() g_game.seekInContainer(container:getId(), container:getFirstIndex() - container:getCapacity()) end
76 end
77
78 local nextPageButton = container.window:recursiveGetChildById('nextPageButton')
79 if currentPage >= pages then
80 nextPageButton:setEnabled(false)
81 else
82 nextPageButton:setEnabled(true)
83 nextPageButton.onClick = function() g_game.seekInContainer(container:getId(), container:getFirstIndex() + container:getCapacity()) end
84 end
85end
86
87function onContainerOpen(container, previousContainer)
88 local containerWindow
89 if previousContainer then
90 containerWindow = previousContainer.window
91 previousContainer.window = nil
92 previousContainer.itemsPanel = nil
93 else
94 containerWindow = g_ui.createWidget('ContainerWindow', modules.game_interface.getContainerPanel())
95 end
96 containerWindow:setId('container' .. container:getId())
97 local containerPanel = containerWindow:getChildById('contentsPanel')
98 local containerItemWidget = containerWindow:getChildById('containerItemWidget')
99 containerWindow.onClose = function()
100 g_game.close(container)
101 containerWindow:hide()
102 end
103 containerWindow.onDrop = function(container, widget, mousePos)
104 if containerPanel:getChildByPos(mousePos) then
105 return false
106 end
107 local child = containerPanel:getChildByIndex(-1)
108 if child then
109 child:onDrop(widget, mousePos, true)
110 end
111 end
112
113 -- this disables scrollbar auto hiding
114 local scrollbar = containerWindow:getChildById('miniwindowScrollBar')
115 scrollbar:mergeStyle({ ['$!on'] = { }})
116
117 local upButton = containerWindow:getChildById('upButton')
118 upButton.onClick = function()
119 g_game.openParent(container)
120 end
121 upButton:setVisible(container:hasParent())
122
123 local name = container:getName()
124 name = name:sub(1,1):upper() .. name:sub(2)
125 containerWindow:setText(name)
126
127 containerItemWidget:setItem(container:getContainerItem())
128
129 containerPanel:destroyChildren()
130 for slot=0,container:getCapacity()-1 do
131 local itemWidget = g_ui.createWidget('Item', containerPanel)
132 itemWidget:setId('item' .. slot)
133 itemWidget:setItem(container:getItem(slot))
134 itemWidget:setMargin(0)
135 itemWidget.position = container:getSlotPosition(slot)
136
137 if not container:isUnlocked() then
138 itemWidget:setBorderColor('red')
139 end
140 end
141
142 container.window = containerWindow
143 container.itemsPanel = containerPanel
144
145 toggleContainerPages(containerWindow, container:hasPages())
146 refreshContainerPages(container)
147
148 local layout = containerPanel:getLayout()
149 local cellSize = layout:getCellSize()
150 containerWindow:setContentMinimumHeight(cellSize.height)
151 containerWindow:setContentMaximumHeight(cellSize.height*layout:getNumLines())
152
153 if not previousContainer then
154 local filledLines = math.max(math.ceil(container:getItemsCount() / layout:getNumColumns()), 1)
155 containerWindow:setContentHeight(filledLines*cellSize.height)
156 end
157
158 containerWindow:setup()
159end
160
161function onContainerClose(container)
162 destroy(container)
163end
164
165function onContainerChangeSize(container, size)
166 if not container.window then return end
167 refreshContainerItems(container)
168end
169
170function onContainerUpdateItem(container, slot, item, oldItem)
171 if not container.window then return end
172 local itemWidget = container.itemsPanel:getChildById('item' .. slot)
173 itemWidget:setItem(item)
174end
175