main.gui_script 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- Helper module for UI operations
  2. -- Separated in order not to clutter the example.
  3. local ui = require "example.ui_helper"
  4. -- Helper module for localization operations
  5. local loc = require "example.localization_helper"
  6. function init(self)
  7. -- Per-language content path, layout (LTR/RTL),
  8. -- and an optional proxy with font resource to load,
  9. -- and a True Type Font (TTF) file pre-hashed path.
  10. self.languages = {
  11. en = {
  12. json = "/assets/texts/text_en.json",
  13. layout = ui.layout.ltr,
  14. proxy = false,
  15. },
  16. ar = {
  17. json = "/assets/texts/text_ar.json",
  18. layout = ui.layout.rtl,
  19. proxy = "#ar_proxy",
  20. ttf_hash = hash("/assets/fonts/NotoSansArabic-Medium.ttf"),
  21. },
  22. pt = {
  23. json = "/assets/texts/text_pt.json",
  24. layout = ui.layout.ltr,
  25. proxy = false,
  26. },
  27. ja = {
  28. json = "/assets/texts/text_ja.json",
  29. layout = ui.layout.ltr,
  30. proxy = "#ja_proxy",
  31. ttf_hash = hash("/assets/fonts/NotoSansJP-Regular.ttf"),
  32. },
  33. }
  34. -- We delegate UI handling to a separate helper module
  35. -- in order not to clutter the example.
  36. ui.initialize_ui(self)
  37. -- Store the font resource of the default font
  38. -- that is initally used for the text gui node.
  39. self.default_font_resource = ui.get_font_resource(self.text_node)
  40. -- Set the GUI initial current language.
  41. self.current_lang = "en"
  42. -- Set the initial requested language to the same one.
  43. self.requested_lang = "en"
  44. -- Get the text for the requested language from the JSON file.
  45. self.requested_text = loc.get_content_from_json(self)
  46. -- Clear texts and update after fonts are prewarmed.
  47. ui.clear_text_nodes(self)
  48. loc.finish_language_change(self, ui.update_ui_content_callback)
  49. end
  50. -- Pre-hashed message IDs.
  51. local msg_proxy_loaded = hash("proxy_loaded")
  52. local msg_proxy_unloaded = hash("proxy_unloaded")
  53. function on_message(self, message_id, message, sender)
  54. -- React to proxy lifecycle messages and continue pending language switch.
  55. if message_id == msg_proxy_unloaded then
  56. -- Remove runtime font once its owning proxy is unloaded.
  57. font.remove_font(self.default_font_resource, self.languages[self.current_lang].ttf_hash)
  58. -- If old font resource was unloaded, load the new one (or finish with default).
  59. if self.languages[self.requested_lang].proxy then
  60. msg.post(self.languages[self.requested_lang].proxy, "async_load")
  61. else
  62. loc.finish_language_change(self, ui.update_ui_content_callback)
  63. end
  64. elseif message_id == msg_proxy_loaded then
  65. loc.finish_language_change(self, ui.update_ui_content_callback)
  66. end
  67. end
  68. -- Pre-hashed action ID.
  69. local action_touch = hash("touch")
  70. function on_input(self, action_id, action)
  71. -- Pointer move arrives with nil action_id in Defold.
  72. if action_id == nil and action.x and action.y then
  73. ui.on_pointer_moved(self, action.x, action.y)
  74. end
  75. -- If the action is not a touch:
  76. if action_id ~= action_touch then
  77. -- Skip the rest of the input handling.
  78. return
  79. end
  80. -- If the action is a touch and pressed:
  81. if action.pressed then
  82. -- Get the selected language on pressed.
  83. local selected_language = ui.get_selected_language_on_pressed(self, action.x, action.y)
  84. -- Set the requested language and text.
  85. self.requested_lang = selected_language or self.requested_lang
  86. -- If the requested language is different from the current language:
  87. if self.requested_lang ~= self.current_lang then
  88. -- Clear current texts while the new font is prepared.
  89. ui.clear_text_nodes(self)
  90. -- Get the text for the requested language from the JSON file.
  91. self.requested_text = loc.get_content_from_json(self)
  92. end
  93. -- Process the language change.
  94. loc.process_language_change(self, ui.update_ui_content_callback)
  95. end
  96. -- If the action is a touch and released:
  97. if action.released then
  98. -- Handle the touch released event.
  99. ui.on_touch_released(self, action.x, action.y)
  100. end
  101. end