tab_content.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 retval =
  47. "label[0.05,-0.25;".. fgettext("Installed Packages:") .. "]" ..
  48. "tablecolumns[color;tree;text]" ..
  49. "table[0,0.25;5.1,4.3;pkglist;" ..
  50. pkgmgr.render_packagelist(packages) ..
  51. ";" .. tabdata.selected_pkg .. "]" ..
  52. "button[0,4.85;5.25,0.5;btn_contentdb;".. fgettext("Browse online content") .. "]"
  53. local selected_pkg
  54. if filterlist.size(packages) >= tabdata.selected_pkg then
  55. selected_pkg = packages:get_list()[tabdata.selected_pkg]
  56. end
  57. if selected_pkg ~= nil then
  58. --check for screenshot beeing available
  59. local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
  60. local screenshotfile, error = io.open(screenshotfilename, "r")
  61. local modscreenshot
  62. if error == nil then
  63. screenshotfile:close()
  64. modscreenshot = screenshotfilename
  65. end
  66. if modscreenshot == nil then
  67. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  68. end
  69. local info = core.get_content_info(selected_pkg.path)
  70. local desc = fgettext("No package description available")
  71. if info.description and info.description:trim() ~= "" then
  72. desc = info.description
  73. end
  74. retval = retval ..
  75. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
  76. "label[8.25,0.6;" .. core.formspec_escape(selected_pkg.name) .. "]" ..
  77. "box[5.5,2.2;6.15,2.35;#000]"
  78. if selected_pkg.type == "mod" then
  79. if selected_pkg.is_modpack then
  80. retval = retval ..
  81. "button[8.65,4.65;3.25,1;btn_mod_mgr_rename_modpack;" ..
  82. fgettext("Rename") .. "]"
  83. else
  84. --show dependencies
  85. desc = desc .. "\n\n"
  86. local toadd_hard = table.concat(info.depends or {}, "\n")
  87. local toadd_soft = table.concat(info.optional_depends or {}, "\n")
  88. if toadd_hard == "" and toadd_soft == "" then
  89. desc = desc .. fgettext("No dependencies.")
  90. else
  91. if toadd_hard ~= "" then
  92. desc = desc ..fgettext("Dependencies:") ..
  93. "\n" .. toadd_hard
  94. end
  95. if toadd_soft ~= "" then
  96. if toadd_hard ~= "" then
  97. desc = desc .. "\n\n"
  98. end
  99. desc = desc .. fgettext("Optional dependencies:") ..
  100. "\n" .. toadd_soft
  101. end
  102. end
  103. end
  104. else
  105. if selected_pkg.type == "txp" then
  106. if selected_pkg.enabled then
  107. retval = retval ..
  108. "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_txp;" ..
  109. fgettext("Disable Texture Pack") .. "]"
  110. else
  111. retval = retval ..
  112. "button[8.65,4.65;3.25,1;btn_mod_mgr_use_txp;" ..
  113. fgettext("Use Texture Pack") .. "]"
  114. end
  115. end
  116. end
  117. retval = retval .. "textarea[5.85,2.2;6.35,2.9;;" ..
  118. fgettext("Information:") .. ";" .. desc .. "]"
  119. if core.may_modify_path(selected_pkg.path) then
  120. retval = retval ..
  121. "button[5.5,4.65;3.25,1;btn_mod_mgr_delete_mod;" ..
  122. fgettext("Uninstall Package") .. "]"
  123. end
  124. end
  125. return retval
  126. end
  127. --------------------------------------------------------------------------------
  128. local function handle_buttons(tabview, fields, tabname, tabdata)
  129. if fields["pkglist"] ~= nil then
  130. local event = core.explode_table_event(fields["pkglist"])
  131. tabdata.selected_pkg = event.row
  132. return true
  133. end
  134. if fields["btn_contentdb"] ~= nil then
  135. local dlg = create_store_dlg()
  136. dlg:set_parent(tabview)
  137. tabview:hide()
  138. dlg:show()
  139. packages = nil
  140. return true
  141. end
  142. if fields["btn_mod_mgr_rename_modpack"] ~= nil then
  143. local mod = packages:get_list()[tabdata.selected_pkg]
  144. local dlg_renamemp = create_rename_modpack_dlg(mod)
  145. dlg_renamemp:set_parent(tabview)
  146. tabview:hide()
  147. dlg_renamemp:show()
  148. packages = nil
  149. return true
  150. end
  151. if fields["btn_mod_mgr_delete_mod"] ~= nil then
  152. local mod = packages:get_list()[tabdata.selected_pkg]
  153. local dlg_delmod = create_delete_content_dlg(mod)
  154. dlg_delmod:set_parent(tabview)
  155. tabview:hide()
  156. dlg_delmod:show()
  157. packages = nil
  158. return true
  159. end
  160. if fields.btn_mod_mgr_use_txp then
  161. local txp = packages:get_list()[tabdata.selected_pkg]
  162. core.settings:set("texture_path", txp.path)
  163. packages = nil
  164. return true
  165. end
  166. if fields.btn_mod_mgr_disable_txp then
  167. core.settings:set("texture_path", "")
  168. packages = nil
  169. return true
  170. end
  171. return false
  172. end
  173. --------------------------------------------------------------------------------
  174. return {
  175. name = "content",
  176. caption = fgettext("Content"),
  177. cbf_formspec = get_formspec,
  178. cbf_button_handler = handle_buttons,
  179. on_change = pkgmgr.update_gamelist
  180. }