tab_content.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
  4. --
  5. --This program is free software; you can redistribute it and/or modify
  6. --it under the terms of the GNU Lesser General Public License as published by
  7. --the Free Software Foundation; either version 2.1 of the License, or
  8. --(at your option) any later version.
  9. --
  10. --This program is distributed in the hope that it will be useful,
  11. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. --GNU Lesser General Public License for more details.
  14. --
  15. --You should have received a copy of the GNU Lesser General Public License along
  16. --with this program; if not, write to the Free Software Foundation, Inc.,
  17. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. local function get_content_icons(packages_with_updates)
  19. local ret = {}
  20. for _, content in ipairs(packages_with_updates) do
  21. ret[content.virtual_path or content.path] = { type = "update" }
  22. end
  23. return ret
  24. end
  25. local packages_raw, packages
  26. local function update_packages()
  27. if not pkgmgr.global_mods then
  28. pkgmgr.refresh_globals()
  29. end
  30. if not pkgmgr.games then
  31. pkgmgr.update_gamelist()
  32. end
  33. packages_raw = {}
  34. table.insert_all(packages_raw, pkgmgr.games)
  35. table.insert_all(packages_raw, pkgmgr.get_texture_packs())
  36. table.insert_all(packages_raw, pkgmgr.global_mods:get_list())
  37. local function get_data()
  38. return packages_raw
  39. end
  40. local function is_equal(element, uid) --uid match
  41. return (element.type == "game" and element.id == uid) or
  42. element.name == uid
  43. end
  44. packages = filterlist.create(get_data, pkgmgr.compare_package,
  45. is_equal, nil, {})
  46. end
  47. local function on_change(type)
  48. if type == "ENTER" then
  49. mm_game_theme.set_engine()
  50. update_packages()
  51. end
  52. end
  53. local function get_formspec(tabview, name, tabdata)
  54. if not packages then
  55. update_packages()
  56. end
  57. if not tabdata.selected_pkg then
  58. tabdata.selected_pkg = 1
  59. end
  60. local use_technical_names = core.settings:get_bool("show_technical_names")
  61. local packages_with_updates = update_detector.get_all()
  62. local update_icons = get_content_icons(packages_with_updates)
  63. local update_count = #packages_with_updates
  64. local contentdb_label
  65. if update_count == 0 then
  66. contentdb_label = fgettext("Browse online content")
  67. else
  68. contentdb_label = fgettext("Browse online content [$1]", update_count)
  69. end
  70. local retval = {
  71. "label[0.4,0.4;", fgettext("Installed Packages:"), "]",
  72. "tablecolumns[color;tree;image,align=inline,width=1.5",
  73. ",tooltip=", fgettext("Update available?"),
  74. ",0=", core.formspec_escape(defaulttexturedir .. "blank.png"),
  75. ",4=", core.formspec_escape(defaulttexturedir .. "cdb_update_cropped.png"),
  76. ";text]",
  77. "table[0.4,0.8;6.3,4.8;pkglist;",
  78. pkgmgr.render_packagelist(packages, use_technical_names, update_icons),
  79. ";", tabdata.selected_pkg, "]",
  80. "button[0.4,5.8;6.3,0.9;btn_contentdb;", contentdb_label, "]"
  81. }
  82. local selected_pkg
  83. if filterlist.size(packages) >= tabdata.selected_pkg then
  84. selected_pkg = packages:get_list()[tabdata.selected_pkg]
  85. end
  86. if selected_pkg then
  87. -- Check for screenshot being available
  88. local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
  89. local screenshotfile, error = io.open(screenshotfilename, "r")
  90. local modscreenshot
  91. if not error then
  92. screenshotfile:close()
  93. modscreenshot = screenshotfilename
  94. else
  95. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  96. end
  97. local info = core.get_content_info(selected_pkg.path)
  98. local desc = fgettext("No package description available")
  99. if info.description and info.description:trim() ~= "" then
  100. desc = core.formspec_escape(info.description)
  101. end
  102. local title_and_name
  103. if selected_pkg.type == "game" then
  104. title_and_name = selected_pkg.name
  105. else
  106. title_and_name = (selected_pkg.title or selected_pkg.name) .. "\n" ..
  107. core.colorize("#BFBFBF", selected_pkg.name)
  108. end
  109. local desc_height = 3.2
  110. if selected_pkg.is_modpack then
  111. desc_height = 2.1
  112. table.insert_all(retval, {
  113. "button[7.1,4.7;8,0.9;btn_mod_mgr_rename_modpack;",
  114. fgettext("Rename"), "]"
  115. })
  116. elseif selected_pkg.type == "mod" then
  117. -- Show dependencies for mods
  118. desc = desc .. "\n\n"
  119. local toadd_hard = table.concat(info.depends or {}, "\n")
  120. local toadd_soft = table.concat(info.optional_depends or {}, "\n")
  121. if toadd_hard == "" and toadd_soft == "" then
  122. desc = desc .. fgettext("No dependencies.")
  123. else
  124. if toadd_hard ~= "" then
  125. desc = desc ..fgettext("Dependencies:") ..
  126. "\n" .. toadd_hard
  127. end
  128. if toadd_soft ~= "" then
  129. if toadd_hard ~= "" then
  130. desc = desc .. "\n\n"
  131. end
  132. desc = desc .. fgettext("Optional dependencies:") ..
  133. "\n" .. toadd_soft
  134. end
  135. end
  136. elseif selected_pkg.type == "txp" then
  137. desc_height = 2.1
  138. if selected_pkg.enabled then
  139. table.insert_all(retval, {
  140. "button[7.1,4.7;8,0.9;btn_mod_mgr_disable_txp;",
  141. fgettext("Disable Texture Pack"), "]"
  142. })
  143. else
  144. table.insert_all(retval, {
  145. "button[7.1,4.7;8,0.9;btn_mod_mgr_use_txp;",
  146. fgettext("Use Texture Pack"), "]"
  147. })
  148. end
  149. end
  150. table.insert_all(retval, {
  151. "image[7.1,0.2;3,2;", core.formspec_escape(modscreenshot), "]",
  152. "label[10.5,1;", core.formspec_escape(title_and_name), "]",
  153. "box[7.1,2.4;8,", tostring(desc_height), ";#000]",
  154. "textarea[7.1,2.4;8,", tostring(desc_height), ";;;", desc, "]",
  155. })
  156. if core.may_modify_path(selected_pkg.path) then
  157. table.insert_all(retval, {
  158. "button[7.1,5.8;4,0.9;btn_mod_mgr_delete_mod;",
  159. fgettext("Uninstall"), "]"
  160. })
  161. end
  162. if update_icons[selected_pkg.virtual_path or selected_pkg.path] then
  163. table.insert_all(retval, {
  164. "button[11.1,5.8;4,0.9;btn_mod_mgr_update;",
  165. fgettext("Update"), "]"
  166. })
  167. end
  168. end
  169. return table.concat(retval)
  170. end
  171. local function handle_doubleclick(pkg)
  172. if pkg.type == "txp" then
  173. if core.settings:get("texture_path") == pkg.path then
  174. core.settings:set("texture_path", "")
  175. else
  176. core.settings:set("texture_path", pkg.path)
  177. end
  178. packages = nil
  179. mm_game_theme.init()
  180. mm_game_theme.set_engine()
  181. end
  182. end
  183. local function handle_buttons(tabview, fields, tabname, tabdata)
  184. if fields.pkglist then
  185. local event = core.explode_table_event(fields.pkglist)
  186. tabdata.selected_pkg = event.row
  187. if event.type == "DCL" then
  188. handle_doubleclick(packages:get_list()[tabdata.selected_pkg])
  189. end
  190. return true
  191. end
  192. if fields.btn_contentdb then
  193. local dlg = create_store_dlg()
  194. dlg:set_parent(tabview)
  195. tabview:hide()
  196. dlg:show()
  197. packages = nil
  198. return true
  199. end
  200. if fields.btn_mod_mgr_rename_modpack then
  201. local mod = packages:get_list()[tabdata.selected_pkg]
  202. local dlg_renamemp = create_rename_modpack_dlg(mod)
  203. dlg_renamemp:set_parent(tabview)
  204. tabview:hide()
  205. dlg_renamemp:show()
  206. packages = nil
  207. return true
  208. end
  209. if fields.btn_mod_mgr_delete_mod then
  210. local mod = packages:get_list()[tabdata.selected_pkg]
  211. local dlg_delmod = create_delete_content_dlg(mod)
  212. dlg_delmod:set_parent(tabview)
  213. tabview:hide()
  214. dlg_delmod:show()
  215. packages = nil
  216. return true
  217. end
  218. if fields.btn_mod_mgr_update then
  219. local pkg = packages:get_list()[tabdata.selected_pkg]
  220. local dlg = create_store_dlg(nil, pkgmgr.get_contentdb_id(pkg))
  221. dlg:set_parent(tabview)
  222. tabview:hide()
  223. dlg:show()
  224. packages = nil
  225. return true
  226. end
  227. if fields.btn_mod_mgr_use_txp or fields.btn_mod_mgr_disable_txp then
  228. local txp_path = ""
  229. if fields.btn_mod_mgr_use_txp then
  230. txp_path = packages:get_list()[tabdata.selected_pkg].path
  231. end
  232. core.settings:set("texture_path", txp_path)
  233. packages = nil
  234. mm_game_theme.init()
  235. mm_game_theme.set_engine()
  236. return true
  237. end
  238. return false
  239. end
  240. return {
  241. name = "content",
  242. caption = function()
  243. local update_count = update_detector.get_count()
  244. if update_count == 0 then
  245. return fgettext("Content")
  246. else
  247. return fgettext("Content [$1]", update_count)
  248. end
  249. end,
  250. cbf_formspec = get_formspec,
  251. cbf_button_handler = handle_buttons,
  252. on_change = on_change
  253. }