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.

healthinfo.lua 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. HealthInfo = {}
  2. -- constants
  3. local Icons = {}
  4. Icons[1] = { tooltip = tr('You are poisoned'), path = '/game_healthinfo/icons/poisoned.png', id = 'condition_poisoned' }
  5. Icons[2] = { tooltip = tr('You are burning'), path = '/game_healthinfo/icons/burning.png', id = 'condition_burning' }
  6. Icons[4] = { tooltip = tr('You are electrified'), path = '/game_healthinfo/icons/electrified.png', id = 'condition_electrified' }
  7. Icons[8] = { tooltip = tr('You are freezing'), path = '/game_healthinfo/icons/drunk.png', id = 'condition_drunk' }
  8. Icons[16] = { tooltip = tr('You are protected by a magic shield'), path = '/game_healthinfo/icons/magic_shield.png', id = 'condition_magic_shield' }
  9. Icons[32] = { tooltip = tr('You are paralysed'), path = '/game_healthinfo/icons/slowed.png', id = 'condition_slowed' }
  10. Icons[64] = { tooltip = tr('You are hasted'), path = '/game_healthinfo/icons/haste.png', id = 'condition_haste' }
  11. Icons[128] = { tooltip = tr('You may not logout during a fight'), path = '/game_healthinfo/icons/logout_block.png', id = 'condition_logout_block' }
  12. Icons[256] = { tooltip = tr('You are drowing'), path = '/game_healthinfo/icons/drowning.png', id = 'condition_drowning' }
  13. Icons[512] = { tooltip = tr('You are freezing'), path = '/game_healthinfo/icons/freezing.png', id = 'condition_freezing' }
  14. Icons[1024] = { tooltip = tr('You are dazzled'), path = '/game_healthinfo/icons/dazzled.png', id = 'condition_dazzled' }
  15. Icons[2048] = { tooltip = tr('You are cursed'), path = '/game_healthinfo/icons/cursed.png', id = 'condition_cursed' }
  16. Icons[4096] = { tooltip = tr('You are strengthened'), path = '/game_healthinfo/icons/strengthened.png', id = 'condition_strengthened' }
  17. Icons[8192] = { tooltip = tr('You may not logout or enter a protection zone'), path = '/game_healthinfo/icons/protection_zone_block.png', id = 'condition_protection_zone_block' }
  18. Icons[16384] = { tooltip = tr('You are within a protection zone'), path = '/game_healthinfo/icons/protection_zone.png', id = 'condition_protection_zone' }
  19. Icons[32768] = { tooltip = tr('You are bleeding'), path = '/game_healthinfo/icons/bleeding.png', id = 'condition_bleeding' }
  20. Icons[65536] = { tooltip = tr('You are hungry'), path = '/game_healthinfo/icons/hungry.png', id = 'condition_hungry' }
  21. -- private variables
  22. local healthInfoWindow
  23. local healthBar
  24. local manaBar
  25. local soulBar
  26. local healthLabel
  27. local manaLabel
  28. local soulLabel
  29. local capLabel
  30. -- public functions
  31. function HealthInfo.init()
  32. connect(LocalPlayer, { onHealthChange = HealthInfo.onHealthChange,
  33. onManaChange = HealthInfo.onManaChange,
  34. onStatesChange = HealthInfo.onStatesChange,
  35. onSoulChange = HealthInfo.onSoulChange,
  36. onFreeCapacityChange = HealthInfo.onFreeCapacityChange })
  37. connect(g_game, { onGameEnd = HealthInfo.offline })
  38. healthInfoWindow = g_ui.loadUI('healthinfo.otui', GameInterface.getRightPanel())
  39. healthInfoButton = TopMenu.addRightGameToggleButton('healthInfoButton', tr('Health Information'), 'healthinfo.png', HealthInfo.toggle)
  40. healthInfoWindow:disableResize()
  41. healthInfoButton:setOn(true)
  42. healthBar = healthInfoWindow:recursiveGetChildById('healthBar')
  43. manaBar = healthInfoWindow:recursiveGetChildById('manaBar')
  44. healthLabel = healthInfoWindow:recursiveGetChildById('healthLabel')
  45. manaLabel = healthInfoWindow:recursiveGetChildById('manaLabel')
  46. soulBar = healthInfoWindow:recursiveGetChildById('soulBar')
  47. soulLabel = healthInfoWindow:recursiveGetChildById('soulLabel')
  48. capLabel = healthInfoWindow:recursiveGetChildById('capLabel')
  49. if g_game.isOnline() then
  50. local localPlayer = g_game.getLocalPlayer()
  51. HealthInfo.onHealthChange(localPlayer, localPlayer:getHealth(), localPlayer:getMaxHealth())
  52. HealthInfo.onManaChange(localPlayer, localPlayer:getMana(), localPlayer:getMaxMana())
  53. HealthInfo.onStatesChange(localPlayer, localPlayer:getStates(), 0)
  54. HealthInfo.onSoulChange(localPlayer, localPlayer:getSoul())
  55. HealthInfo.onFreeCapacityChange(localPlayer, localPlayer:getFreeCapacity())
  56. end
  57. end
  58. function HealthInfo.terminate()
  59. disconnect(LocalPlayer, { onHealthChange = HealthInfo.onHealthChange,
  60. onManaChange = HealthInfo.onManaChange,
  61. onStatesChange = HealthInfo.onStatesChange,
  62. onSoulChange = HealthInfo.onSoulChange,
  63. onFreeCapacityChange = HealthInfo.onFreeCapacityChange })
  64. disconnect(g_game, { onGameEnd = HealthInfo.offline })
  65. healthInfoWindow:destroy()
  66. healthInfoButton:destroy()
  67. healthInfoWindow = nil
  68. healthInfoButton = nil
  69. healthBar = nil
  70. manaBar = nil
  71. soulBar = nil
  72. healthLabel = nil
  73. manaLabel = nil
  74. soulLabel = nil
  75. capLabel = nil
  76. HealthInfo = nil
  77. end
  78. function HealthInfo.toggle()
  79. if healthInfoButton:isOn() then
  80. healthInfoWindow:close()
  81. healthInfoButton:setOn(false)
  82. else
  83. healthInfoWindow:open()
  84. healthInfoButton:setOn(true)
  85. end
  86. end
  87. function HealthInfo.onMiniWindowClose()
  88. healthInfoButton:setOn(false)
  89. end
  90. function HealthInfo.offline()
  91. healthInfoWindow:recursiveGetChildById('conditionPanel'):destroyChildren()
  92. end
  93. -- hooked events
  94. function HealthInfo.onHealthChange(localPlayer, health, maxHealth)
  95. healthLabel:setText(health .. ' / ' .. maxHealth)
  96. healthBar:setPercent(health / maxHealth * 100)
  97. end
  98. function HealthInfo.onManaChange(localPlayer, mana, maxMana)
  99. manaLabel:setText(mana .. ' / ' .. maxMana)
  100. local percent
  101. if maxMana == 0 then
  102. percent = 100
  103. else
  104. percent = (mana * 100)/maxMana
  105. end
  106. manaBar:setPercent(percent)
  107. end
  108. function HealthInfo.onSoulChange(localPlayer, soul)
  109. soulLabel:setText(tr('Soul') .. ': ' .. soul)
  110. end
  111. function HealthInfo.onFreeCapacityChange(player, freeCapacity)
  112. capLabel:setText(tr('Cap') .. ': ' .. freeCapacity)
  113. end
  114. function HealthInfo.onStatesChange(localPlayer, now, old)
  115. if now == old then return end
  116. local bitsChanged = bit32.bxor(now, old)
  117. for i = 1, 32 do
  118. local pow = math.pow(2, i-1)
  119. if pow > bitsChanged then break end
  120. local bitChanged = bit32.band(bitsChanged, pow)
  121. if bitChanged ~= 0 then
  122. HealthInfo.toggleIcon(bitChanged)
  123. end
  124. end
  125. end
  126. function HealthInfo.toggleIcon(bitChanged)
  127. local content = healthInfoWindow:recursiveGetChildById('conditionPanel')
  128. local icon = content:getChildById(Icons[bitChanged].id)
  129. if icon then
  130. icon:destroy()
  131. else
  132. icon = g_ui.createWidget('ConditionWidget', content)
  133. icon:setId(Icons[bitChanged].id)
  134. icon:setImageSource(Icons[bitChanged].path)
  135. icon:setTooltip(Icons[bitChanged].tooltip)
  136. end
  137. end