tibia-client/modules/game_inventory/inventory.lua

96 lines
2.6 KiB
Lua
Raw Normal View History

2011-11-10 06:29:25 +01:00
Inventory = {}
-- public variables
InventorySlotStyles = {
[InventorySlotHead] = "HeadSlot",
[InventorySlotNeck] = "NeckSlot",
[InventorySlotBack] = "BackSlot",
[InventorySlotBody] = "BodySlot",
[InventorySlotRight] = "RightSlot",
[InventorySlotLeft] = "LeftSlot",
[InventorySlotLeg] = "LegSlot",
[InventorySlotFeet] = "FeetSlot",
[InventorySlotFinger] = "FingerSlot",
[InventorySlotAmmo] = "AmmoSlot"
}
2011-11-10 06:29:25 +01:00
-- private variables
local inventoryWindow
2012-03-28 13:46:15 +02:00
local inventoryPanel
local inventoryButton
2011-11-10 06:29:25 +01:00
-- public functions
function Inventory.init()
connect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange,
onFreeCapacityChange = Inventory.onFreeCapacityChange })
2012-04-27 22:35:39 +02:00
connect(g_game, { onGameStart = Inventory.refresh })
2012-06-26 00:13:30 +02:00
g_keyboard.bindKeyDown('Ctrl+I', Inventory.toggle)
2012-06-26 00:13:30 +02:00
inventoryWindow = g_ui.loadUI('inventory.otui', GameInterface.getRightPanel())
inventoryPanel = inventoryWindow:getChildById('contentsPanel')
inventoryButton = TopMenu.addRightGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', 'inventory.png', Inventory.toggle)
inventoryButton:setOn(true)
Inventory.refresh()
2011-11-10 06:29:25 +01:00
end
function Inventory.terminate()
disconnect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange,
onFreeCapacityChange = Inventory.onFreeCapacityChange })
2012-04-27 22:35:39 +02:00
disconnect(g_game, { onGameStart = Inventory.refresh })
2012-06-26 00:13:30 +02:00
g_keyboard.unbindKeyDown('Ctrl+I')
inventoryWindow:destroy()
inventoryButton:destroy()
inventoryWindow = nil
inventoryButton = nil
inventoryPanel = nil
2012-04-27 06:54:14 +02:00
Inventory = nil
end
function Inventory.refresh()
local player = g_game.getLocalPlayer()
for i=InventorySlotFirst,InventorySlotLast do
2012-04-27 22:35:39 +02:00
if player then
Inventory.onInventoryChange(player, i, player:getInventoryItem(i))
else
Inventory.onInventoryChange(player, i, nil)
end
end
end
function Inventory.toggle()
2012-06-21 21:31:22 +02:00
if inventoryButton:isOn() then
inventoryWindow:close()
inventoryButton:setOn(false)
else
inventoryWindow:open()
inventoryButton:setOn(true)
end
end
function Inventory.onMiniWindowClose()
inventoryButton:setOn(false)
2011-11-10 06:29:25 +01:00
end
-- hooked events
function Inventory.onInventoryChange(player, slot, item, oldItem)
2012-03-28 13:46:15 +02:00
local itemWidget = inventoryPanel:getChildById('slot' .. slot)
if(item) then
itemWidget:setStyle('Item')
itemWidget:setItem(item)
else
itemWidget:setStyle(InventorySlotStyles[slot])
itemWidget:setItem(nil)
end
2011-11-10 06:29:25 +01:00
end
function Inventory.onFreeCapacityChange(player, freeCapacity)
2012-03-28 13:46:15 +02:00
local widget = inventoryPanel:getChildById('capacity')
widget:setText("Cap:\n" .. freeCapacity)
end