api.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. -- global values
  2. hud.registered_items = {}
  3. hud.damage_events = {}
  4. hud.breath_events = {}
  5. -- keep id handling internal
  6. local hud_id = {} -- hud item ids
  7. local sb_bg = {} -- statbar background ids
  8. -- localize often used table
  9. local items = hud.registered_items
  10. local function throw_error(msg)
  11. minetest.log("error", "Better HUD[error]: " .. msg)
  12. end
  13. --
  14. -- API
  15. --
  16. function hud.register(name, def)
  17. if not name or not def then
  18. throw_error("not enough parameters given")
  19. return false
  20. end
  21. --TODO: allow other elements
  22. if def.hud_elem_type ~= "statbar" then
  23. throw_error("The given HUD element is no statbar")
  24. return false
  25. end
  26. if items[name] ~= nil then
  27. throw_error("A statbar with that name already exists")
  28. return false
  29. end
  30. -- actually register
  31. -- add background first since draworder is based on id :\
  32. if def.hud_elem_type == "statbar" and def.background ~= nil then
  33. sb_bg[name] = table.copy(def)
  34. sb_bg[name].text = def.background
  35. if not def.autohide_bg and def.max then
  36. sb_bg[name].number = def.max
  37. end
  38. end
  39. -- add item itself
  40. items[name] = def
  41. -- register events
  42. if def.events then
  43. for _,v in pairs(def.events) do
  44. if v and v.type and v.func then
  45. if v.type == "damage" then
  46. table.insert(hud.damage_events, v)
  47. end
  48. if v.type == "breath" then
  49. table.insert(hud.breath_events, v)
  50. end
  51. end
  52. end
  53. end
  54. -- no error so far, return sucess
  55. return true
  56. end
  57. -- swaps stabar positions
  58. function hud.swap_statbar(player, item1, item2)
  59. if not player or not item1 or not item2 then
  60. throw_error("Not enough parameters given to swap statbars")
  61. return false
  62. end
  63. local def1 = items[item1] or nil
  64. local def2 = items[item2] or nil
  65. if not def1 or not def2 then
  66. throw_error("Can't swap statbars. Given statbars are not correct")
  67. return false
  68. end
  69. local pos_swap = false
  70. local p_name = player:get_player_name()
  71. local elem1 = hud_id[p_name.."_"..item1]
  72. local elem2 = hud_id[p_name.."_"..item2]
  73. player:hud_change(elem2.id, "offset", def1.offset)
  74. player:hud_change(elem1.id, "offset", def2.offset)
  75. if def1.position.x ~= def2.position.x or def1.position.y ~= def2.position.y then
  76. player:hud_change(elem2.id, "position", def1.position)
  77. player:hud_change(elem1.id, "position", def2.position)
  78. pos_swap = true
  79. end
  80. -- do the items have backgrounds? if so, swap them aswell
  81. local bg1 = hud_id[p_name.."_"..item1.."_bg"] or nil
  82. local bg2 = hud_id[p_name.."_"..item2.."_bg"] or nil
  83. if bg1 ~= nil and bg1.id then
  84. player:hud_change(bg1.id, "offset", def2.offset)
  85. if pos_swap == true then
  86. player:hud_change(bg1.id, "position", def2.position)
  87. end
  88. end
  89. if bg2 ~= nil and bg2.id then
  90. player:hud_change(bg2.id, "offset", def1.offset)
  91. if pos_swap == true then
  92. player:hud_change(bg2.id, "position", def1.position)
  93. end
  94. end
  95. return true
  96. end
  97. function hud.change_item(player, name, def)
  98. if not player or not player:is_player() or not name or not def then
  99. throw_error("Not enough parameters given to change HUD item")
  100. return false
  101. end
  102. local i_name = player:get_player_name().."_"..name
  103. local elem = hud_id[i_name]
  104. if not elem then
  105. throw_error("Given HUD element " .. dump(name) .. " does not exist".." hääää")
  106. return false
  107. end
  108. -- Only update if values supported and value actually changed
  109. -- update supported values (currently number and text only)
  110. if def.number and elem.number then
  111. if def.number ~= elem.number then
  112. if elem.max and def.number > elem.max and not def.max then
  113. def.number = elem.max
  114. end
  115. if def.max then
  116. elem.max = def.max
  117. end
  118. player:hud_change(elem.id, "number", def.number)
  119. elem.number = def.number
  120. -- hide background when set
  121. local bg = hud_id[i_name.."_bg"]
  122. if elem.autohide_bg then
  123. if def.number < 1 then
  124. player:hud_change(bg.id, "number", 0)
  125. else
  126. local num = bg.number
  127. if bg.max then
  128. num = bg.max
  129. end
  130. player:hud_change(bg.id, "number", num)
  131. end
  132. else
  133. if bg and bg.max and bg.max < 1 and def.max and def.max > bg.max then
  134. player:hud_change(bg.id, "number", def.max)
  135. bg.max = def.max
  136. bg.number = def.max
  137. end
  138. end
  139. end
  140. end
  141. if def.text and elem.text then
  142. if def.text ~= elem.text then
  143. player:hud_change(elem.id, "text", def.text)
  144. elem.text = def.text
  145. end
  146. end
  147. if def.offset and elem.offset then
  148. if def.item_name and def.offset == "item" then
  149. -- for legacy reasons
  150. if def.item_name then
  151. hud.swap_statbar(player, name, def.item_name)
  152. end
  153. else
  154. player:hud_change(elem.id, "offset", def.offset)
  155. elem.offset = def.offset
  156. end
  157. end
  158. return true
  159. end
  160. function hud.remove_item(player, name)
  161. if not player or not name then
  162. throw_error("Not enough parameters given")
  163. return false
  164. end
  165. local i_name = player:get_player_name().."_"..name
  166. if hud_id[i_name] == nil then
  167. throw_error("Given HUD element " .. dump(name) .. " does not exist")
  168. return false
  169. end
  170. player:hud_remove(hud_id[i_name].id)
  171. hud_id[i_name] = nil
  172. return true
  173. end
  174. --
  175. -- Add registered HUD items to joining players
  176. --
  177. -- Following code is placed here to keep HUD ids internal
  178. local function add_hud_item(player, name, def)
  179. if not player or not name or not def then
  180. throw_error("not enough parameters given")
  181. return false
  182. end
  183. local i_name = player:get_player_name().."_"..name
  184. hud_id[i_name] = def
  185. hud_id[i_name].id = player:hud_add(def)
  186. end
  187. minetest.register_on_joinplayer(function(player)
  188. -- first: hide the default statbars
  189. local hud_flags = player:hud_get_flags()
  190. hud_flags.healthbar = false
  191. hud_flags.breathbar = false
  192. player:hud_set_flags(hud_flags)
  193. -- now add the backgrounds for statbars
  194. for _,item in pairs(sb_bg) do
  195. add_hud_item(player, _.."_bg", item)
  196. end
  197. -- and finally the actual HUD items
  198. for _,item in pairs(items) do
  199. add_hud_item(player, _, item)
  200. end
  201. -- fancy hotbar (only when no crafting mod present)
  202. if minetest.get_modpath("crafting") == nil then
  203. minetest.after(0.5, function()
  204. player:hud_set_hotbar_image("hud_hotbar.png")
  205. player:hud_set_hotbar_selected_image("hud_hotbar_selected.png")
  206. end)
  207. end
  208. end)