tab_mods.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. --------------------------------------------------------------------------------
  18. local function get_formspec(tabview, name, tabdata)
  19. if modmgr.global_mods == nil then
  20. modmgr.refresh_globals()
  21. end
  22. if tabdata.selected_mod == nil then
  23. tabdata.selected_mod = 1
  24. end
  25. local retval =
  26. "label[0.05,-0.25;".. fgettext("Installed Mods:") .. "]" ..
  27. "tablecolumns[color;tree;text]" ..
  28. "table[0,0.25;5.1,5;modlist;" ..
  29. modmgr.render_modlist(modmgr.global_mods) ..
  30. ";" .. tabdata.selected_mod .. "]"
  31. local selected_mod = nil
  32. if filterlist.size(modmgr.global_mods) >= tabdata.selected_mod then
  33. selected_mod = modmgr.global_mods:get_list()[tabdata.selected_mod]
  34. end
  35. if selected_mod ~= nil then
  36. local modscreenshot = nil
  37. --check for screenshot beeing available
  38. local screenshotfilename = selected_mod.path .. DIR_DELIM .. "screenshot.png"
  39. local error = nil
  40. local screenshotfile,error = io.open(screenshotfilename,"r")
  41. if error == nil then
  42. screenshotfile:close()
  43. modscreenshot = screenshotfilename
  44. end
  45. if modscreenshot == nil then
  46. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  47. end
  48. retval = retval
  49. .. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]"
  50. .. "label[8.25,0.6;" .. selected_mod.name .. "]"
  51. local descriptionlines = nil
  52. error = nil
  53. local descriptionfilename = selected_mod.path .. "description.txt"
  54. local descriptionfile,error = io.open(descriptionfilename,"r")
  55. if error == nil then
  56. local descriptiontext = descriptionfile:read("*all")
  57. descriptionlines = core.wrap_text(descriptiontext, 42, true)
  58. descriptionfile:close()
  59. else
  60. descriptionlines = {}
  61. descriptionlines[#descriptionlines + 1] = fgettext("No mod description available")
  62. end
  63. retval = retval ..
  64. "label[5.5,1.7;".. fgettext("Mod Information:") .. "]" ..
  65. "textlist[5.5,2.2;6.2,2.4;description;"
  66. for i=1,#descriptionlines,1 do
  67. retval = retval .. core.formspec_escape(descriptionlines[i]) .. ","
  68. end
  69. if selected_mod.is_modpack then
  70. retval = retval .. ";0]" ..
  71. "button[9.9,4.65;2,1;btn_mod_mgr_rename_modpack;" ..
  72. fgettext("Rename") .. "]"
  73. retval = retval .. "button[5.5,4.65;4.5,1;btn_mod_mgr_delete_mod;"
  74. .. fgettext("Uninstall Selected Modpack") .. "]"
  75. else
  76. --show dependencies
  77. local toadd_hard, toadd_soft = modmgr.get_dependencies(selected_mod.path)
  78. if toadd_hard == "" and toadd_soft == "" then
  79. retval = retval .. "," .. fgettext("No dependencies.")
  80. else
  81. if toadd_hard ~= "" then
  82. retval = retval .. "," .. fgettext("Dependencies:") .. ","
  83. retval = retval .. toadd_hard
  84. end
  85. if toadd_soft ~= "" then
  86. if toadd_hard ~= "" then
  87. retval = retval .. ","
  88. end
  89. retval = retval .. "," .. fgettext("Optional dependencies:") .. ","
  90. retval = retval .. toadd_soft
  91. end
  92. end
  93. retval = retval .. ";0]"
  94. retval = retval .. "button[5.5,4.65;4.5,1;btn_mod_mgr_delete_mod;"
  95. .. fgettext("Uninstall Selected Mod") .. "]"
  96. end
  97. end
  98. return retval
  99. end
  100. --------------------------------------------------------------------------------
  101. local function handle_buttons(tabview, fields, tabname, tabdata)
  102. if fields["modlist"] ~= nil then
  103. local event = core.explode_table_event(fields["modlist"])
  104. tabdata.selected_mod = event.row
  105. return true
  106. end
  107. if fields["btn_mod_mgr_install_local"] ~= nil then
  108. core.show_file_open_dialog("mod_mgt_open_dlg",fgettext("Select Mod File:"))
  109. return true
  110. end
  111. if fields["btn_mod_mgr_rename_modpack"] ~= nil then
  112. local dlg_renamemp = create_rename_modpack_dlg(tabdata.selected_mod)
  113. dlg_renamemp:set_parent(tabview)
  114. tabview:hide()
  115. dlg_renamemp:show()
  116. return true
  117. end
  118. if fields["btn_mod_mgr_delete_mod"] ~= nil then
  119. local dlg_delmod = create_delete_mod_dlg(tabdata.selected_mod)
  120. dlg_delmod:set_parent(tabview)
  121. tabview:hide()
  122. dlg_delmod:show()
  123. return true
  124. end
  125. if fields["mod_mgt_open_dlg_accepted"] ~= nil and
  126. fields["mod_mgt_open_dlg_accepted"] ~= "" then
  127. modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
  128. return true
  129. end
  130. return false
  131. end
  132. --------------------------------------------------------------------------------
  133. return {
  134. name = "mods",
  135. caption = fgettext("Mods"),
  136. cbf_formspec = get_formspec,
  137. cbf_button_handler = handle_buttons,
  138. on_change = gamemgr.update_gamelist
  139. }