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.

inventory.lua 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Inventory = {}
  2. -- public variables
  3. InventorySlotStyles = {
  4. [InventorySlotHead] = "HeadSlot",
  5. [InventorySlotNeck] = "NeckSlot",
  6. [InventorySlotBack] = "BackSlot",
  7. [InventorySlotBody] = "BodySlot",
  8. [InventorySlotRight] = "RightSlot",
  9. [InventorySlotLeft] = "LeftSlot",
  10. [InventorySlotLeg] = "LegSlot",
  11. [InventorySlotFeet] = "FeetSlot",
  12. [InventorySlotFinger] = "FingerSlot",
  13. [InventorySlotAmmo] = "AmmoSlot"
  14. }
  15. -- private variables
  16. local inventoryWindow
  17. local inventoryPanel
  18. local inventoryButton
  19. -- public functions
  20. function Inventory.init()
  21. connect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange })
  22. connect(g_game, { onGameStart = Inventory.refresh })
  23. g_keyboard.bindKeyDown('Ctrl+I', Inventory.toggle)
  24. inventoryWindow = g_ui.loadUI('inventory.otui', GameInterface.getRightPanel())
  25. inventoryWindow:disableResize()
  26. inventoryPanel = inventoryWindow:getChildById('contentsPanel')
  27. inventoryButton = TopMenu.addRightGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', 'inventory.png', Inventory.toggle)
  28. inventoryButton:setOn(true)
  29. Inventory.refresh()
  30. end
  31. function Inventory.terminate()
  32. disconnect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange })
  33. disconnect(g_game, { onGameStart = Inventory.refresh })
  34. g_keyboard.unbindKeyDown('Ctrl+I')
  35. inventoryWindow:destroy()
  36. inventoryButton:destroy()
  37. inventoryWindow = nil
  38. inventoryButton = nil
  39. inventoryPanel = nil
  40. Inventory = nil
  41. end
  42. function Inventory.refresh()
  43. local player = g_game.getLocalPlayer()
  44. for i=InventorySlotFirst,InventorySlotLast do
  45. if player then
  46. Inventory.onInventoryChange(player, i, player:getInventoryItem(i))
  47. else
  48. Inventory.onInventoryChange(player, i, nil)
  49. end
  50. end
  51. end
  52. function Inventory.toggle()
  53. if inventoryButton:isOn() then
  54. inventoryWindow:close()
  55. inventoryButton:setOn(false)
  56. else
  57. inventoryWindow:open()
  58. inventoryButton:setOn(true)
  59. end
  60. end
  61. function Inventory.onMiniWindowClose()
  62. inventoryButton:setOn(false)
  63. end
  64. -- hooked events
  65. function Inventory.onInventoryChange(player, slot, item, oldItem)
  66. local itemWidget = inventoryPanel:getChildById('slot' .. slot)
  67. if(item) then
  68. itemWidget:setStyle('Item')
  69. itemWidget:setItem(item)
  70. else
  71. itemWidget:setStyle(InventorySlotStyles[slot])
  72. itemWidget:setItem(nil)
  73. end
  74. end