tab_content.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. retval = retval ..
  70. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
  71. "label[8.25,0.6;" .. core.formspec_escape(selected_pkg.name) .. "]" ..
  72. "label[5.5,1.7;".. fgettext("Information:") .. "]" ..
  73. "textlist[5.5,2.2;6.2,2.4;description;"
  74. local info = core.get_content_info(selected_pkg.path)
  75. local desc = info.description or fgettext("No package description available")
  76. local descriptionlines = core.wrap_text(desc, 42, true)
  77. for i = 1, #descriptionlines do
  78. retval = retval .. core.formspec_escape(descriptionlines[i]) .. ","
  79. end
  80. if selected_pkg.type == "mod" then
  81. if selected_pkg.is_modpack then
  82. retval = retval .. ";0]" ..
  83. "button[8.9,4.65;3,1;btn_mod_mgr_rename_modpack;" ..
  84. fgettext("Rename") .. "]"
  85. else
  86. --show dependencies
  87. local toadd_hard = table.concat(info.depends or {}, ",")
  88. local toadd_soft = table.concat(info.optional_depends or {}, ",")
  89. if toadd_hard == "" and toadd_soft == "" then
  90. retval = retval .. "," .. fgettext("No dependencies.")
  91. else
  92. if toadd_hard ~= "" then
  93. retval = retval .. "," .. fgettext("Dependencies:") .. ","
  94. retval = retval .. toadd_hard
  95. end
  96. if toadd_soft ~= "" then
  97. if toadd_hard ~= "" then
  98. retval = retval .. ","
  99. end
  100. retval = retval .. "," .. fgettext("Optional dependencies:") .. ","
  101. retval = retval .. toadd_soft
  102. end
  103. end
  104. retval = retval .. ";0]"
  105. end
  106. else
  107. retval = retval .. ";0]"
  108. if selected_pkg.type == "txp" then
  109. if selected_pkg.enabled then
  110. retval = retval ..
  111. "button[8.9,4.65;3,1;btn_mod_mgr_disable_txp;" ..
  112. fgettext("Disable Texture Pack") .. "]"
  113. else
  114. retval = retval ..
  115. "button[8.9,4.65;3,1;btn_mod_mgr_use_txp;" ..
  116. fgettext("Use Texture Pack") .. "]"
  117. end
  118. end
  119. end
  120. retval = retval .. "button[5.5,4.65;3,1;btn_mod_mgr_delete_mod;"
  121. .. fgettext("Uninstall Package") .. "]"
  122. end
  123. return retval
  124. end
  125. --------------------------------------------------------------------------------
  126. local function handle_buttons(tabview, fields, tabname, tabdata)
  127. if fields["pkglist"] ~= nil then
  128. local event = core.explode_table_event(fields["pkglist"])
  129. tabdata.selected_pkg = event.row
  130. return true
  131. end
  132. if fields["btn_mod_mgr_install_local"] ~= nil then
  133. core.show_file_open_dialog("mod_mgt_open_dlg", fgettext("Select Package File:"))
  134. return true
  135. end
  136. if fields["btn_contentdb"] ~= nil then
  137. local dlg = create_store_dlg()
  138. dlg:set_parent(tabview)
  139. tabview:hide()
  140. dlg:show()
  141. packages = nil
  142. return true
  143. end
  144. if fields["btn_mod_mgr_rename_modpack"] ~= nil then
  145. local dlg_renamemp = create_rename_modpack_dlg(tabdata.selected_pkg)
  146. dlg_renamemp:set_parent(tabview)
  147. tabview:hide()
  148. dlg_renamemp:show()
  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. if fields["mod_mgt_open_dlg_accepted"] and
  172. fields["mod_mgt_open_dlg_accepted"] ~= "" then
  173. pkgmgr.install_mod(fields["mod_mgt_open_dlg_accepted"],nil)
  174. return true
  175. end
  176. return false
  177. end
  178. --------------------------------------------------------------------------------
  179. return {
  180. name = "content",
  181. caption = fgettext("Content"),
  182. cbf_formspec = get_formspec,
  183. cbf_button_handler = handle_buttons,
  184. on_change = pkgmgr.update_gamelist
  185. }