api.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -- global values
  2. hud.registered_items = {}
  3. hud.damage_events = {}
  4. hud.breath_events = {}
  5. -- Localize for performance.
  6. local math_floor = math.floor
  7. -- keep id handling internal
  8. -- Actually no, allow external access for debugging purposes.
  9. -- HUD bugs are a pain >:(
  10. hud.all_hud_ids = {}
  11. hud.all_sb_bgs = {}
  12. local hud_id = hud.all_hud_ids -- hud item ids
  13. local sb_bg = hud.all_sb_bgs -- statbar background ids
  14. -- localize often used table
  15. local items = hud.registered_items
  16. local function throw_error(msg)
  17. minetest.log("error", "Better HUD[error]: " .. msg)
  18. end
  19. --------------------------------------------------------------------------------
  20. -- API
  21. --------------------------------------------------------------------------------
  22. function hud.register(name, def)
  23. if not name or not def then
  24. throw_error("Not enough parameters given")
  25. return false
  26. end
  27. --TODO: allow other elements
  28. if def.hud_elem_type ~= "statbar" then
  29. throw_error("The given HUD element is not a statbar")
  30. return false
  31. end
  32. if items[name] ~= nil then
  33. throw_error("A statbar with that name already exists")
  34. return false
  35. end
  36. -- Actually register
  37. -- Add background first since draworder is based on id.
  38. if def.hud_elem_type == "statbar" and def.background ~= nil then
  39. sb_bg[name] = table.copy(def)
  40. sb_bg[name].text = def.background
  41. sb_bg[name].number = 20
  42. end
  43. -- add item itself
  44. items[name] = def
  45. -- register events
  46. if def.events then
  47. for _, v in pairs(def.events) do
  48. if v and v.type and v.func then
  49. if v.type == "damage" then
  50. table.insert(hud.damage_events, v)
  51. end
  52. if v.type == "breath" then
  53. table.insert(hud.breath_events, v)
  54. end
  55. end
  56. end
  57. end
  58. -- no error so far, return sucess
  59. return true
  60. end
  61. function hud.change_item(player, name, def)
  62. if not player or not player:is_player() or not name or not def then
  63. throw_error("Not enough parameters given to change HUD item")
  64. return false
  65. end
  66. local i_name = player:get_player_name().."_"..name
  67. local elem = hud_id[i_name]
  68. if not elem then
  69. --throw_error("Given HUD element " .. dump(name) .. " does not exist")
  70. return false
  71. end
  72. -- Update supported values (currently number and text only)
  73. if def.number and elem.number then
  74. elem.number = math_floor((def.number / def.max) * 20)
  75. player:hud_change(elem.id, "number", elem.number)
  76. end
  77. if def.text and elem.text then
  78. player:hud_change(elem.id, "text", def.text)
  79. elem.text = def.text
  80. end
  81. if def.offset and elem.offset then
  82. player:hud_change(elem.id, "offset", def.offset)
  83. elem.offset = def.offset
  84. end
  85. return true
  86. end
  87. function hud.remove_item(player, name)
  88. if not player or not name then
  89. throw_error("Not enough parameters given")
  90. return false
  91. end
  92. local i_name = player:get_player_name() .. "_" .. name
  93. if hud_id[i_name] == nil then
  94. --throw_error("Given HUD element " .. dump(name) .. " does not exist")
  95. return false
  96. end
  97. player:hud_remove(hud_id[i_name].id)
  98. hud_id[i_name] = nil
  99. return true
  100. end
  101. --------------------------------------------------------------------------------
  102. -- Add registered HUD items to joining players
  103. --------------------------------------------------------------------------------
  104. -- Following code is placed here to keep HUD ids internal.
  105. local function add_hud_item(player, name, def)
  106. if not player or not name or not def then
  107. throw_error("not enough parameters given")
  108. return false
  109. end
  110. -- Every player must have their own copy of the HUD element definition table,
  111. -- otherwise modifications to the table will leak to other players, and to
  112. -- the default global state. How this got missed in the original mod, I'll
  113. -- never know.
  114. local i_name = player:get_player_name() .. "_" .. name
  115. hud_id[i_name] = table.copy(def) -- Copy that table!
  116. hud_id[i_name].id = player:hud_add(def)
  117. end
  118. minetest.register_on_joinplayer(function(player)
  119. -- First: hide the default statbars.
  120. local hud_flags = player:hud_get_flags()
  121. hud_flags.healthbar = false
  122. hud_flags.breathbar = false
  123. player:hud_set_flags(hud_flags)
  124. -- Now add the backgrounds for statbars.
  125. for _, item in pairs(sb_bg) do
  126. add_hud_item(player, _ .. "_bg", item)
  127. end
  128. -- And finally the actual HUD items.
  129. for _, item in pairs(items) do
  130. add_hud_item(player, _, item)
  131. end
  132. end)