tab_content.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 packages_raw
  19. local packages
  20. --------------------------------------------------------------------------------
  21. local function get_formspec(tabview, name, tabdata)
  22. if pkgmgr.global_mods == nil then
  23. pkgmgr.refresh_globals()
  24. end
  25. if pkgmgr.games == nil then
  26. pkgmgr.update_gamelist()
  27. end
  28. if packages == nil then
  29. packages_raw = {}
  30. table.insert_all(packages_raw, pkgmgr.games)
  31. table.insert_all(packages_raw, pkgmgr.get_texture_packs())
  32. table.insert_all(packages_raw, pkgmgr.global_mods:get_list())
  33. local function get_data()
  34. return packages_raw
  35. end
  36. local function is_equal(element, uid) --uid match
  37. return (element.type == "game" and element.id == uid) or
  38. element.name == uid
  39. end
  40. packages = filterlist.create(get_data, pkgmgr.compare_package,
  41. is_equal, nil, {})
  42. end
  43. if tabdata.selected_pkg == nil then
  44. tabdata.selected_pkg = 1
  45. end
  46. local use_technical_names = core.settings:get_bool("show_technical_names")
  47. local retval =
  48. "label[0.05,-0.25;".. fgettext("Installed Packages:") .. "]" ..
  49. "tablecolumns[color;tree;text]" ..
  50. "table[0,0.25;5.1,4.3;pkglist;" ..
  51. pkgmgr.render_packagelist(packages, use_technical_names) ..
  52. ";" .. tabdata.selected_pkg .. "]" ..
  53. "button[0,4.85;5.25,0.5;btn_contentdb;".. fgettext("Browse online content") .. "]"
  54. local selected_pkg
  55. if filterlist.size(packages) >= tabdata.selected_pkg then
  56. selected_pkg = packages:get_list()[tabdata.selected_pkg]
  57. end
  58. if selected_pkg ~= nil then
  59. --check for screenshot beeing available
  60. local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
  61. local screenshotfile, error = io.open(screenshotfilename, "r")
  62. local modscreenshot
  63. if error == nil then
  64. screenshotfile:close()
  65. modscreenshot = screenshotfilename
  66. end
  67. if modscreenshot == nil then
  68. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  69. end
  70. local info = core.get_content_info(selected_pkg.path)
  71. local desc = fgettext("No package description available")
  72. if info.description and info.description:trim() ~= "" then
  73. desc = info.description
  74. end
  75. local title_and_name
  76. if selected_pkg.type == "game" then
  77. title_and_name = selected_pkg.name
  78. else
  79. title_and_name = (selected_pkg.title or selected_pkg.name) .. "\n" ..
  80. core.colorize("#BFBFBF", selected_pkg.name)
  81. end
  82. retval = retval ..
  83. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
  84. "label[8.25,0.6;" .. core.formspec_escape(title_and_name) .. "]" ..
  85. "box[5.5,2.2;6.15,2.35;#000]"
  86. if selected_pkg.type == "mod" then
  87. if selected_pkg.is_modpack then
  88. retval = retval ..
  89. "button[8.65,4.65;3.25,1;btn_mod_mgr_rename_modpack;" ..
  90. fgettext("Rename") .. "]"
  91. else
  92. --show dependencies
  93. desc = desc .. "\n\n"
  94. local toadd_hard = table.concat(info.depends or {}, "\n")
  95. local toadd_soft = table.concat(info.optional_depends or {}, "\n")
  96. if toadd_hard == "" and toadd_soft == "" then
  97. desc = desc .. fgettext("No dependencies.")
  98. else
  99. if toadd_hard ~= "" then
  100. desc = desc ..fgettext("Dependencies:") ..
  101. "\n" .. toadd_hard
  102. end
  103. if toadd_soft ~= "" then
  104. if toadd_hard ~= "" then
  105. desc = desc .. "\n\n"
  106. end
  107. desc = desc .. fgettext("Optional dependencies:") ..
  108. "\n" .. toadd_soft
  109. end
  110. end
  111. end
  112. else
  113. if selected_pkg.type == "txp" then
  114. if selected_pkg.enabled then
  115. retval = retval ..
  116. "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_txp;" ..
  117. fgettext("Disable Texture Pack") .. "]"
  118. else
  119. retval = retval ..
  120. "button[8.65,4.65;3.25,1;btn_mod_mgr_use_txp;" ..
  121. fgettext("Use Texture Pack") .. "]"
  122. end
  123. end
  124. end
  125. retval = retval .. "textarea[5.85,2.2;6.35,2.9;;" ..
  126. fgettext("Information:") .. ";" .. desc .. "]"
  127. if core.may_modify_path(selected_pkg.path) then
  128. retval = retval ..
  129. "button[5.5,4.65;3.25,1;btn_mod_mgr_delete_mod;" ..
  130. fgettext("Uninstall Package") .. "]"
  131. end
  132. end
  133. return retval
  134. end
  135. --------------------------------------------------------------------------------
  136. local function handle_doubleclick(pkg)
  137. if pkg.type == "txp" then
  138. if core.settings:get("texture_path") == pkg.path then
  139. core.settings:set("texture_path", "")
  140. else
  141. core.settings:set("texture_path", pkg.path)
  142. end
  143. packages = nil
  144. mm_game_theme.init()
  145. mm_game_theme.reset()
  146. end
  147. end
  148. --------------------------------------------------------------------------------
  149. local function handle_buttons(tabview, fields, tabname, tabdata)
  150. if fields["pkglist"] ~= nil then
  151. local event = core.explode_table_event(fields["pkglist"])
  152. tabdata.selected_pkg = event.row
  153. if event.type == "DCL" then
  154. handle_doubleclick(packages:get_list()[tabdata.selected_pkg])
  155. end
  156. return true
  157. end
  158. if fields["btn_contentdb"] ~= nil then
  159. local dlg = create_store_dlg()
  160. dlg:set_parent(tabview)
  161. tabview:hide()
  162. dlg:show()
  163. packages = nil
  164. return true
  165. end
  166. if fields["btn_mod_mgr_rename_modpack"] ~= nil then
  167. local mod = packages:get_list()[tabdata.selected_pkg]
  168. local dlg_renamemp = create_rename_modpack_dlg(mod)
  169. dlg_renamemp:set_parent(tabview)
  170. tabview:hide()
  171. dlg_renamemp:show()
  172. packages = nil
  173. return true
  174. end
  175. if fields["btn_mod_mgr_delete_mod"] ~= nil then
  176. local mod = packages:get_list()[tabdata.selected_pkg]
  177. local dlg_delmod = create_delete_content_dlg(mod)
  178. dlg_delmod:set_parent(tabview)
  179. tabview:hide()
  180. dlg_delmod:show()
  181. packages = nil
  182. return true
  183. end
  184. if fields.btn_mod_mgr_use_txp or fields.btn_mod_mgr_disable_txp then
  185. local txp_path = ""
  186. if fields.btn_mod_mgr_use_txp then
  187. txp_path = packages:get_list()[tabdata.selected_pkg].path
  188. end
  189. core.settings:set("texture_path", txp_path)
  190. packages = nil
  191. mm_game_theme.init()
  192. mm_game_theme.reset()
  193. return true
  194. end
  195. return false
  196. end
  197. --------------------------------------------------------------------------------
  198. return {
  199. name = "content",
  200. caption = fgettext("Content"),
  201. cbf_formspec = get_formspec,
  202. cbf_button_handler = handle_buttons,
  203. on_change = pkgmgr.update_gamelist
  204. }