menu_helpers.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. if UiContainer == nil then UiContainer = {} end
  2. UiContainer.pad_value = function(value, length)
  3. local str = tostring(value)
  4. while #(str) < length do
  5. str = " " .. str
  6. end
  7. return str
  8. end
  9. UiContainer.zero_pad_value = function(value, length)
  10. local str = tostring(value)
  11. while #(str) < length do
  12. str = "0" .. str
  13. end
  14. return str
  15. end
  16. --- Populates a character data.
  17. --
  18. -- @param widget Name of the widget with the character data.
  19. -- @param character The character to get the data from. If nill, the character details will be
  20. -- hidden.
  21. UiContainer.populate_character_data = function(widget, character)
  22. if character == nil then
  23. ui_manager:get_widget(widget):set_visible(false)
  24. return 0
  25. end
  26. -- Maximum HP and MP include the bonuses of equipment and materia. Before the stats are first
  27. -- calculated they are not set, so fall back to the base values.
  28. local hp_max = character.stats.hp.max or character.stats.hp.base
  29. local mp_max = character.stats.mp.max or character.stats.mp.base
  30. local char_id = character.char_id
  31. ui_manager:get_widget(widget):set_visible(true)
  32. -- Critical?
  33. if character.stats.hp.current == 0 then
  34. ui_manager:get_widget(widget .. ".Data.HpCurrent"):set_colour(1.0, 0, 0)
  35. elseif character.stats.hp.current <= hp_max / 4 then
  36. ui_manager:get_widget(widget .. ".Data.HpCurrent"):set_colour(1.0, 1.0, 0)
  37. else
  38. ui_manager:get_widget(widget .. ".Data.HpCurrent"):set_colour(0.9, 0.9, 0.9)
  39. end
  40. -- Text data
  41. ui_manager:get_widget(widget .. ".Data.Name"):set_text(character.name)
  42. ui_manager:get_widget(widget .. ".Data.LvNumber"):set_text(UiContainer.pad_value(character.level, 3))
  43. ui_manager:get_widget(widget .. ".Data.HpCurrent"):set_text(UiContainer.pad_value(character.stats.hp.current, 5))
  44. ui_manager:get_widget(widget .. ".Data.HpMax"):set_text(UiContainer.pad_value(hp_max, 5))
  45. ui_manager:get_widget(widget .. ".Data.MpCurrent"):set_text(UiContainer.pad_value(character.stats.mp.current, 5))
  46. ui_manager:get_widget(widget .. ".Data.MpMax"):set_text(UiContainer.pad_value(mp_max, 5))
  47. -- Calculate the HP and MP bar.
  48. local max_hp_width= ui_manager:get_widget(widget .. ".Data.HpLine"):get_width()
  49. local hp_width = math.floor(character.stats.hp.current * max_hp_width / hp_max)
  50. ui_manager:get_widget(widget .. ".Data.HpLineCurrent"):set_width(hp_width)
  51. local max_mp_width= ui_manager:get_widget(widget .. ".Data.MpLine"):get_width()
  52. local mp_width = character.stats.mp.current * max_mp_width / mp_max
  53. ui_manager:get_widget(widget .. ".Data.MpLineCurrent"):set_width(mp_width)
  54. -- Status (Fury/Sadness)
  55. local status_text = ""
  56. for _, st in ipairs(character.status) do
  57. if st == UiContainer.Status.SADNESS then
  58. status_text = "Sadness"
  59. break
  60. elseif st == UiContainer.Status.FURY then
  61. status_text = "Fury"
  62. break
  63. end
  64. end
  65. local status_widget = ui_manager:get_widget(widget .. ".Data.Status")
  66. status_widget:set_colour(0.8, 0, 1)
  67. status_widget:set_text(status_text)
  68. return 0
  69. end