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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. InventorySlotStyles = {
  2. [InventorySlotHead] = "HeadSlot",
  3. [InventorySlotNeck] = "NeckSlot",
  4. [InventorySlotBack] = "BackSlot",
  5. [InventorySlotBody] = "BodySlot",
  6. [InventorySlotRight] = "RightSlot",
  7. [InventorySlotLeft] = "LeftSlot",
  8. [InventorySlotLeg] = "LegSlot",
  9. [InventorySlotFeet] = "FeetSlot",
  10. [InventorySlotFinger] = "FingerSlot",
  11. [InventorySlotAmmo] = "AmmoSlot"
  12. }
  13. inventoryWindow = nil
  14. inventoryPanel = nil
  15. inventoryButton = nil
  16. purseButton = nil
  17. function init()
  18. connect(LocalPlayer, {
  19. onInventoryChange = onInventoryChange,
  20. onBlessingsChange = onBlessingsChange
  21. })
  22. connect(g_game, { onGameStart = refresh })
  23. g_keyboard.bindKeyDown('Ctrl+I', toggle)
  24. inventoryButton = modules.client_topmenu.addRightGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', '/images/topbuttons/inventory', toggle)
  25. inventoryButton:setOn(true)
  26. inventoryWindow = g_ui.loadUI('inventory', modules.game_interface.getRightPanel())
  27. inventoryWindow:disableResize()
  28. inventoryPanel = inventoryWindow:getChildById('contentsPanel')
  29. purseButton = inventoryPanel:getChildById('purseButton')
  30. local function purseFunction()
  31. local purse = g_game.getLocalPlayer():getInventoryItem(InventorySlotPurse)
  32. if purse then
  33. g_game.use(purse)
  34. end
  35. end
  36. purseButton.onClick = purseFunction
  37. refresh()
  38. inventoryWindow:setup()
  39. end
  40. function terminate()
  41. disconnect(LocalPlayer, {
  42. onInventoryChange = onInventoryChange,
  43. onBlessingsChange = onBlessingsChange
  44. })
  45. disconnect(g_game, { onGameStart = refresh })
  46. g_keyboard.unbindKeyDown('Ctrl+I')
  47. inventoryWindow:destroy()
  48. inventoryButton:destroy()
  49. end
  50. function toggleAdventurerStyle(hasBlessing)
  51. for slot = InventorySlotFirst, InventorySlotLast do
  52. local itemWidget = inventoryPanel:getChildById('slot' .. slot)
  53. if itemWidget then
  54. itemWidget:setOn(hasBlessing)
  55. end
  56. end
  57. end
  58. function refresh()
  59. local player = g_game.getLocalPlayer()
  60. for i = InventorySlotFirst, InventorySlotPurse do
  61. if g_game.isOnline() then
  62. onInventoryChange(player, i, player:getInventoryItem(i))
  63. else
  64. onInventoryChange(player, i, nil)
  65. end
  66. toggleAdventurerStyle(player and Bit.hasBit(player:getBlessings(), Blessings.Adventurer) or false)
  67. end
  68. purseButton:setVisible(g_game.getFeature(GamePurseSlot))
  69. end
  70. function toggle()
  71. if inventoryButton:isOn() then
  72. inventoryWindow:close()
  73. inventoryButton:setOn(false)
  74. else
  75. inventoryWindow:open()
  76. inventoryButton:setOn(true)
  77. end
  78. end
  79. function onMiniWindowClose()
  80. inventoryButton:setOn(false)
  81. end
  82. -- hooked events
  83. function onInventoryChange(player, slot, item, oldItem)
  84. if slot > InventorySlotPurse then return end
  85. if slot == InventorySlotPurse then
  86. if g_game.getFeature(GamePurseSlot) then
  87. purseButton:setEnabled(item and true or false)
  88. end
  89. return
  90. end
  91. local itemWidget = inventoryPanel:getChildById('slot' .. slot)
  92. if item then
  93. itemWidget:setStyle('InventoryItem')
  94. itemWidget:setItem(item)
  95. else
  96. itemWidget:setStyle(InventorySlotStyles[slot])
  97. itemWidget:setItem(nil)
  98. end
  99. end
  100. function onBlessingsChange(player, blessings, oldBlessings)
  101. local hasAdventurerBlessing = Bit.hasBit(blessings, Blessings.Adventurer)
  102. if hasAdventurerBlessing ~= Bit.hasBit(oldBlessings, Blessings.Adventurer) then
  103. toggleAdventurerStyle(hasAdventurerBlessing)
  104. end
  105. end