ui_helper.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. local M = {}
  2. -- Font resource ------------------------------------------------
  3. -- Get the font resource of the default font.
  4. -- @param node_id The ID of the node to get the font resource of.
  5. -- @return The font resource of the default font.
  6. function M.get_font_resource(node_id)
  7. -- Get the font resource of the default font.
  8. return gui.get_font_resource(gui.get_font(node_id))
  9. end
  10. -- Buttons visuals ----------------------------------------------
  11. -- Visual states used by language selection buttons.
  12. M.button_state = {
  13. released = 1,
  14. hovered = 2,
  15. pressed = 3,
  16. }
  17. -- Predefined button colors for the given button state.
  18. local button_color = {
  19. [M.button_state.released] = vmath.vector4(1, 1, 1, 0.7),
  20. [M.button_state.hovered] = vmath.vector4(1, 1, 1, 0.9),
  21. [M.button_state.pressed] = vmath.vector4(1, 1, 1, 1.0),
  22. }
  23. -- UI Initialization --------------------------------------------
  24. -- Initialize UI with the given languages.
  25. -- @param self The self table from the GUI script.
  26. function M.initialize_ui(self)
  27. -- Resolve GUI text nodes for information display once during init.
  28. self.text_node = gui.get_node("text")
  29. self.language_name_node = gui.get_node("language_name")
  30. -- Create a table of all buttons with their language name, node, and state.
  31. self.buttons = {}
  32. for language_name, _ in pairs(self.languages) do
  33. -- Set initial button state to released.
  34. local state = M.button_state.released
  35. -- Get the button node.
  36. local node = gui.get_node("btn_" .. language_name)
  37. -- Add the button to the buttons table.
  38. self.buttons[language_name] = {
  39. language_name = language_name,
  40. node = node,
  41. state = state
  42. }
  43. -- Update initial button color according to the state.
  44. gui.set_color(node, button_color[state])
  45. end
  46. msg.post(".", "acquire_input_focus")
  47. end
  48. -- Layout definitions ------------------------------------------
  49. -- Supported text flow directions used by language layout.
  50. M.layout = {
  51. ltr = "LTR",
  52. rtl = "RTL",
  53. }
  54. -- Predefined text node positions for the given layout.
  55. local text_position = {
  56. [M.layout.ltr] = vmath.vector3(-380, 0, 0),
  57. [M.layout.rtl] = vmath.vector3(380, 0, 0),
  58. }
  59. -- Predefined text node pivots for the given layout.
  60. local text_pivot = {
  61. [M.layout.ltr] = gui.PIVOT_W,
  62. [M.layout.rtl] = gui.PIVOT_E,
  63. }
  64. -- Update UI content --------------------------------------------
  65. -- Update the UI content with the requested language.
  66. -- @param self The self table from the GUI script.
  67. function M.update_ui_content_callback(self)
  68. -- Update the language text.
  69. gui.set_text(self.text_node, self.requested_text.text or "")
  70. -- Update the language name and layout information.
  71. local layout = self.languages[self.requested_lang].layout
  72. local header_text = string.format("%s (%s)", self.requested_text.title, layout)
  73. gui.set_text(self.language_name_node, header_text)
  74. -- Update the text layout.
  75. gui.set_pivot(self.text_node, text_pivot[layout])
  76. gui.set_position(self.text_node, text_position[layout])
  77. -- Update the input focus.
  78. msg.post(".", "acquire_input_focus")
  79. end
  80. -- Clear text nodes while a language change is in progress.
  81. -- @param self The self table from the GUI script.
  82. function M.clear_text_nodes(self)
  83. gui.set_text(self.text_node, "")
  84. gui.set_text(self.language_name_node, "")
  85. end
  86. -- Input handling -----------------------------------------------
  87. -- Handle pointer move event.
  88. -- @param self The self table from the GUI script.
  89. -- @param x The x position of the pointer.
  90. -- @param y The y position of the pointer.
  91. function M.on_pointer_moved(self, x, y)
  92. -- Update the state of all buttons according to the pointer position.
  93. for _, button in pairs(self.buttons) do
  94. -- Check if the pointer is over the button.
  95. if gui.pick_node(button.node, x, y) then
  96. -- If so, set the button state to hovered.
  97. button.state = M.button_state.hovered
  98. else
  99. -- Otherwise, set the button state to released.
  100. button.state = M.button_state.released
  101. end
  102. -- Update the button color according to the new state.
  103. gui.set_color(button.node, button_color[button.state])
  104. end
  105. end
  106. -- Get the button pressed and return the language name.
  107. -- @param self The self table from the GUI script.
  108. -- @param x The x position of the pointer.
  109. -- @param y The y position of the pointer.
  110. function M.get_selected_language_on_pressed(self, x, y)
  111. local selected_language = nil
  112. -- Find the button pressed.
  113. for _, button in pairs(self.buttons) do
  114. -- Check if the pointer is over the button.
  115. if gui.pick_node(button.node, x, y) then
  116. -- If so, set the button state to pressed.
  117. button.state = M.button_state.pressed
  118. selected_language = button.language_name
  119. end
  120. -- Update the button color according to the new state.
  121. gui.set_color(button.node, button_color[button.state])
  122. end
  123. -- Return the language name of the button pressed, nil if not found
  124. return selected_language
  125. end
  126. -- Handle touch released event.
  127. -- @param self The self table from the GUI script.
  128. -- @param x The x position of the pointer.
  129. -- @param y The y position of the pointer.
  130. function M.on_touch_released(self, x, y)
  131. -- Find the button released.
  132. for _, button in pairs(self.buttons) do
  133. -- Check if the pointer is over the button.
  134. if gui.pick_node(button.node, x, y) then
  135. -- If so, set the button state to released.
  136. button.state = M.button_state.released
  137. end
  138. -- Update the button color according to the new state.
  139. gui.set_color(button.node, button_color[button.state])
  140. end
  141. end
  142. return M