browser_app.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. laptop.browser_api = {} -- Be useable from other mods
  2. -- Helper function - navigate to page
  3. function laptop.browser_api.navigate(app, mtos, pagename)
  4. local page_prep = minetest.strip_colors(pagename)
  5. if page_prep == "" then
  6. return
  7. end
  8. local page_prep = page_prep:lower()
  9. local page = laptop.apps[page_prep]
  10. if page and page.browser_page then
  11. mtos:set_app(page_prep)
  12. else
  13. mtos:set_app("error.404")
  14. end
  15. end
  16. --- Navigation bar - Shown on each page - needs to be included to each page
  17. function laptop.browser_api.header_formspec_func(app, mtos)
  18. local currentpage = ""
  19. if app.browser_page then
  20. currentpage = app.name
  21. end
  22. local formspec = "field[.2,.5;13,1;input_field;;"..currentpage.."]field_close_on_enter[input_field;false]"..
  23. mtos.theme:get_image_button('12.9,.3;.8,.8', 'toolbar', 'go_button', 'laptop_go_web.png', '', 'Go') ..
  24. mtos.theme:get_image_button('13.6,.3;.8,.8', 'toolbar', 'home_button', 'laptop_home_web.png', '', 'Home') ..
  25. mtos.theme:get_image_button('14.3,.3;.8,.8', 'toolbar', 'settings_button', 'laptop_settings_web.png', '', 'Settings')
  26. return formspec
  27. end
  28. -- Page header / Navigation bar buttons processing. Needs to be included to each page
  29. function laptop.browser_api.header_receive_fields_func(app, mtos, sender, fields)
  30. if (fields.key_enter and fields.key_enter_field == "input_field") or fields.go_button then
  31. laptop.browser_api.navigate(app, mtos, fields.input_field)
  32. elseif fields.home_button then
  33. mtos:set_app("browser")
  34. elseif fields.settings_button then
  35. mtos:set_app("browser:settings")
  36. elseif fields.page_link then
  37. laptop.browser_api.navigate(app, mtos, fields.page_link)
  38. end
  39. end
  40. --------------------------------------------
  41. -- Browser home page
  42. --------------------------------------------
  43. laptop.register_app("browser", {
  44. app_name = "MineBrowse",
  45. app_icon = "laptop_browser.png",
  46. app_info = "Web Browser",
  47. os_min_version = "5.51",
  48. formspec_func = function(app, mtos)
  49. local formspec = laptop.browser_api.header_formspec_func(app, mtos) ..
  50. --"image[.1,1.3;18,1.6;laptop_header_web.png]"..
  51. "image[0,1.3;18,1.6;laptop_welcome_web.png]"..
  52. mtos.theme:get_label('.1,2.7', 'MineBrowse is a working web browser powered by formspecs. It is community driven,', 'contrast') ..
  53. mtos.theme:get_label('.1,3', 'which means websites are created by the community. If you like to add your own site', 'contrast') ..
  54. mtos.theme:get_label('.1,3.3', 'visit submit.official for further details.', 'contrast') ..
  55. "background[0,1.2;15,9;laptop_background.png]"..
  56. mtos.theme:get_button('11.3,9.3;3,.8', 'url_bright', 'page_link', 'submit.official') ..
  57. "image[11,2.8;4,8.1;laptop_ad1_web.png]"..
  58. "image[.1,3.8;12,1.2;laptop_wa_web.png]"
  59. -- Prepare / Generate Pages list
  60. local c_row_count = 12
  61. local pageslist_sorted = {}
  62. for name, def in pairs(laptop.apps) do
  63. if def.browser_page and mtos:is_app_compatible(name) then
  64. table.insert(pageslist_sorted, {name = name, def = def})
  65. end
  66. end
  67. table.sort(pageslist_sorted, function(a,b) return a.name < b.name end)
  68. for i, e in ipairs(pageslist_sorted) do
  69. local x = math.floor((i-1) / c_row_count)*5 + 0.3
  70. local y = ((i-1) % c_row_count)*0.4 + 4.7
  71. formspec = formspec .. mtos.theme:get_button(x..','..y..';3,.5', 'url_dark', "page_link", e.name)..
  72. mtos.theme:get_label((x+3)..','..y, e.def.app_info, 'contrast')
  73. end
  74. return formspec
  75. end,
  76. receive_fields_func = function(app, mtos, sender, fields)
  77. laptop.browser_api.header_receive_fields_func(app, mtos, sender, fields)
  78. end
  79. })
  80. --------------------------------------------
  81. -- Error 404 - Page not found
  82. --------------------------------------------
  83. laptop.register_view("error.404", {
  84. app_info = "Page not found",
  85. formspec_func = function(app, mtos)
  86. local formspec = laptop.browser_api.header_formspec_func(app, mtos) ..
  87. mtos.theme:get_label('.3,1.1','Error 404 - Page not found')
  88. return formspec
  89. end,
  90. receive_fields_func = function(app, mtos, sender, fields)
  91. laptop.browser_api.header_receive_fields_func(app, mtos, sender, fields)
  92. end
  93. })
  94. --------------------------------------------
  95. -- Settings
  96. --------------------------------------------
  97. laptop.register_view("browser:settings", {
  98. app_info = "Browser settings",
  99. formspec_func = function(app, mtos)
  100. local formspec = mtos.theme:get_label('.3,1.1','Settings')
  101. return formspec
  102. end,
  103. receive_fields_func = function(app, mtos, sender, fields)
  104. laptop.browser_api.header_receive_fields_func(app, mtos, sender, fields)
  105. end
  106. })
  107. --------------------------------------------
  108. -- Submit informations
  109. --------------------------------------------
  110. laptop.register_view("submit.official", {
  111. app_info = "Contribute to Minetest Laptop Mod",
  112. browser_page = true,
  113. formspec_func = function(app, mtos)
  114. local formspec = laptop.browser_api.header_formspec_func(app, mtos) ..
  115. mtos.theme:get_label('.1,2.8', 'You can help create many more websites that people can explore!', 'contrast') ..
  116. mtos.theme:get_label('.1,3.1', 'Each website can have multiple pages.', 'contrast') ..
  117. mtos.theme:get_label('.1,3.4', 'Please submit your website/webpages to:', 'contrast') ..
  118. mtos.theme:get_label('.1,3.7', 'https://github.com/Gerold55/MineBrowse-Sites', 'contrast') ..
  119. "background[0,1.2;15,9;laptop_background.png]"
  120. return formspec
  121. end,
  122. receive_fields_func = function(app, mtos, sender, fields)
  123. laptop.browser_api.header_receive_fields_func(app, mtos, sender, fields)
  124. end
  125. })