tab_content.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. --Luanti
  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. pkgmgr.load_all()
  28. packages_raw = {}
  29. table.insert_all(packages_raw, pkgmgr.games)
  30. table.insert_all(packages_raw, pkgmgr.texture_packs)
  31. table.insert_all(packages_raw, pkgmgr.global_mods:get_list())
  32. local function get_data()
  33. return packages_raw
  34. end
  35. local function is_equal(element, uid) --uid match
  36. return (element.type == "game" and element.id == uid) or
  37. element.name == uid
  38. end
  39. packages = filterlist.create(get_data, pkgmgr.compare_package,
  40. is_equal, nil, {})
  41. end
  42. local function on_change(type)
  43. if type == "ENTER" then
  44. mm_game_theme.set_engine()
  45. update_packages()
  46. end
  47. end
  48. local function get_formspec(tabview, name, tabdata)
  49. if not packages then
  50. update_packages()
  51. end
  52. if not tabdata.selected_pkg then
  53. tabdata.selected_pkg = 1
  54. end
  55. local use_technical_names = core.settings:get_bool("show_technical_names")
  56. local packages_with_updates = update_detector.get_all()
  57. local update_icons = get_content_icons(packages_with_updates)
  58. local update_count = #packages_with_updates
  59. local contentdb_label
  60. if update_count == 0 then
  61. contentdb_label = fgettext("Browse online content")
  62. else
  63. contentdb_label = fgettext("Browse online content [$1]", update_count)
  64. end
  65. local retval = {
  66. "label[0.4,0.4;", fgettext("Installed Packages:"), "]",
  67. "tablecolumns[color;tree;image,align=inline,width=1.5",
  68. ",tooltip=", fgettext("Update available?"),
  69. ",0=", core.formspec_escape(defaulttexturedir .. "blank.png"),
  70. ",4=", core.formspec_escape(defaulttexturedir .. "cdb_update_cropped.png"),
  71. ";text]",
  72. "table[0.4,0.8;6.3,4.8;pkglist;",
  73. pkgmgr.render_packagelist(packages, use_technical_names, update_icons),
  74. ";", tabdata.selected_pkg, "]",
  75. "button[0.4,5.8;6.3,0.9;btn_contentdb;", contentdb_label, "]"
  76. }
  77. local selected_pkg
  78. if filterlist.size(packages) >= tabdata.selected_pkg then
  79. selected_pkg = packages:get_list()[tabdata.selected_pkg]
  80. end
  81. if selected_pkg then
  82. -- Check for screenshot being available
  83. local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
  84. local screenshotfile, error = io.open(screenshotfilename, "r")
  85. local modscreenshot
  86. if not error then
  87. screenshotfile:close()
  88. modscreenshot = screenshotfilename
  89. else
  90. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  91. end
  92. local desc = fgettext("No package description available")
  93. if selected_pkg.description and selected_pkg.description:trim() ~= "" then
  94. desc = core.formspec_escape(selected_pkg.description)
  95. end
  96. local info = core.get_content_info(selected_pkg.path)
  97. local title_and_name
  98. if selected_pkg.type == "game" then
  99. title_and_name = selected_pkg.title or selected_pkg.name
  100. else
  101. title_and_name = (selected_pkg.title or selected_pkg.name) .. "\n" ..
  102. core.colorize("#BFBFBF", selected_pkg.name)
  103. end
  104. local desc_height = 3.2
  105. if selected_pkg.is_modpack then
  106. desc_height = 2.1
  107. table.insert_all(retval, {
  108. "button[7.1,4.7;8,0.9;btn_mod_mgr_rename_modpack;",
  109. fgettext("Rename"), "]"
  110. })
  111. elseif selected_pkg.type == "mod" then
  112. -- Show dependencies for mods
  113. desc = desc .. "\n\n"
  114. local toadd_hard = table.concat(info.depends or {}, "\n")
  115. local toadd_soft = table.concat(info.optional_depends or {}, "\n")
  116. if toadd_hard == "" and toadd_soft == "" then
  117. desc = desc .. fgettext("No dependencies.")
  118. else
  119. if toadd_hard ~= "" then
  120. desc = desc ..fgettext("Dependencies:") ..
  121. "\n" .. toadd_hard
  122. end
  123. if toadd_soft ~= "" then
  124. if toadd_hard ~= "" then
  125. desc = desc .. "\n\n"
  126. end
  127. desc = desc .. fgettext("Optional dependencies:") ..
  128. "\n" .. toadd_soft
  129. end
  130. end
  131. elseif selected_pkg.type == "txp" then
  132. desc_height = 2.1
  133. if selected_pkg.enabled then
  134. table.insert_all(retval, {
  135. "button[7.1,4.7;8,0.9;btn_mod_mgr_disable_txp;",
  136. fgettext("Disable Texture Pack"), "]"
  137. })
  138. else
  139. table.insert_all(retval, {
  140. "button[7.1,4.7;8,0.9;btn_mod_mgr_use_txp;",
  141. fgettext("Use Texture Pack"), "]"
  142. })
  143. end
  144. end
  145. table.insert_all(retval, {
  146. "image[7.1,0.2;3,2;", core.formspec_escape(modscreenshot), "]",
  147. "label[10.5,1;", core.formspec_escape(title_and_name), "]",
  148. "box[7.1,2.4;8,", tostring(desc_height), ";#000]",
  149. "textarea[7.1,2.4;8,", tostring(desc_height), ";;;", desc, "]",
  150. })
  151. if core.may_modify_path(selected_pkg.path) then
  152. table.insert_all(retval, {
  153. "button[7.1,5.8;4,0.9;btn_mod_mgr_delete_mod;",
  154. fgettext("Uninstall"), "]"
  155. })
  156. end
  157. if update_icons[selected_pkg.virtual_path or selected_pkg.path] then
  158. table.insert_all(retval, {
  159. "button[11.1,5.8;4,0.9;btn_mod_mgr_update;",
  160. fgettext("Update"), "]"
  161. })
  162. end
  163. end
  164. return table.concat(retval)
  165. end
  166. local function handle_doubleclick(pkg)
  167. if pkg.type == "txp" then
  168. if core.settings:get("texture_path") == pkg.path then
  169. core.settings:set("texture_path", "")
  170. else
  171. core.settings:set("texture_path", pkg.path)
  172. end
  173. packages = nil
  174. pkgmgr.reload_texture_packs()
  175. mm_game_theme.init()
  176. mm_game_theme.set_engine()
  177. end
  178. end
  179. local function handle_buttons(tabview, fields, tabname, tabdata)
  180. if fields.pkglist then
  181. local event = core.explode_table_event(fields.pkglist)
  182. tabdata.selected_pkg = event.row
  183. if event.type == "DCL" then
  184. handle_doubleclick(packages:get_list()[tabdata.selected_pkg])
  185. end
  186. return true
  187. end
  188. if fields.btn_contentdb then
  189. local dlg = create_contentdb_dlg()
  190. dlg:set_parent(tabview)
  191. tabview:hide()
  192. dlg:show()
  193. packages = nil
  194. return true
  195. end
  196. if fields.btn_mod_mgr_rename_modpack then
  197. local mod = packages:get_list()[tabdata.selected_pkg]
  198. local dlg_renamemp = create_rename_modpack_dlg(mod)
  199. dlg_renamemp:set_parent(tabview)
  200. tabview:hide()
  201. dlg_renamemp:show()
  202. packages = nil
  203. return true
  204. end
  205. if fields.btn_mod_mgr_delete_mod then
  206. local mod = packages:get_list()[tabdata.selected_pkg]
  207. local dlg_delmod = create_delete_content_dlg(mod)
  208. dlg_delmod:set_parent(tabview)
  209. tabview:hide()
  210. dlg_delmod:show()
  211. packages = nil
  212. return true
  213. end
  214. if fields.btn_mod_mgr_update then
  215. local pkg = packages:get_list()[tabdata.selected_pkg]
  216. local dlg = create_contentdb_dlg(nil, pkgmgr.get_contentdb_id(pkg))
  217. dlg:set_parent(tabview)
  218. tabview:hide()
  219. dlg:show()
  220. packages = nil
  221. return true
  222. end
  223. if fields.btn_mod_mgr_use_txp or fields.btn_mod_mgr_disable_txp then
  224. local txp_path = ""
  225. if fields.btn_mod_mgr_use_txp then
  226. txp_path = packages:get_list()[tabdata.selected_pkg].path
  227. end
  228. core.settings:set("texture_path", txp_path)
  229. packages = nil
  230. pkgmgr.reload_texture_packs()
  231. mm_game_theme.init()
  232. mm_game_theme.set_engine()
  233. return true
  234. end
  235. return false
  236. end
  237. return {
  238. name = "content",
  239. caption = function()
  240. local update_count = core.settings:get_bool("contentdb_enable_updates_indicator") and update_detector.get_count() or 0
  241. if update_count == 0 then
  242. return fgettext("Content")
  243. else
  244. return fgettext("Content [$1]", update_count)
  245. end
  246. end,
  247. cbf_formspec = get_formspec,
  248. cbf_button_handler = handle_buttons,
  249. on_change = on_change
  250. }