tab_mods.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. retval = retval ..
  32. -- "label[0.8,4.2;" .. fgettext("Add mod:") .. "]" ..
  33. -- TODO Disabled due to upcoming release 0.4.8 and irrlicht messing up localization
  34. -- "button[0.75,4.85;1.8,0.5;btn_mod_mgr_install_local;".. fgettext("Local install") .. "]" ..
  35. -- TODO Disabled due to service being offline, and not likely to come online again, in this form
  36. -- "button[0,4.85;5.25,0.5;btn_modstore;".. fgettext("Online mod repository") .. "]"
  37. ""
  38. local selected_mod = nil
  39. if filterlist.size(modmgr.global_mods) >= tabdata.selected_mod then
  40. selected_mod = modmgr.global_mods:get_list()[tabdata.selected_mod]
  41. end
  42. if selected_mod ~= nil then
  43. local modscreenshot = nil
  44. --check for screenshot beeing available
  45. local screenshotfilename = selected_mod.path .. DIR_DELIM .. "screenshot.png"
  46. local error = nil
  47. local screenshotfile,error = io.open(screenshotfilename,"r")
  48. if error == nil then
  49. screenshotfile:close()
  50. modscreenshot = screenshotfilename
  51. end
  52. if modscreenshot == nil then
  53. modscreenshot = defaulttexturedir .. "no_screenshot.png"
  54. end
  55. retval = retval
  56. .. "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]"
  57. .. "label[8.25,0.6;" .. selected_mod.name .. "]"
  58. local descriptionlines = nil
  59. error = nil
  60. local descriptionfilename = selected_mod.path .. "description.txt"
  61. local descriptionfile,error = io.open(descriptionfilename,"r")
  62. if error == nil then
  63. local descriptiontext = descriptionfile:read("*all")
  64. descriptionlines = core.wrap_text(descriptiontext, 42)
  65. descriptionfile:close()
  66. else
  67. descriptionlines = {}
  68. descriptionlines[#descriptionlines + 1] = fgettext("No mod description available")
  69. end
  70. retval = retval ..
  71. "label[5.5,1.7;".. fgettext("Mod information:") .. "]" ..
  72. "textlist[5.5,2.2;6.2,2.4;description;"
  73. for i=1,#descriptionlines,1 do
  74. retval = retval .. core.formspec_escape(descriptionlines[i]) .. ","
  75. end
  76. if selected_mod.is_modpack then
  77. retval = retval .. ";0]" ..
  78. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;" ..
  79. fgettext("Rename") .. "]"
  80. retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
  81. .. fgettext("Uninstall selected modpack") .. "]"
  82. else
  83. --show dependencies
  84. local toadd_hard, toadd_soft = modmgr.get_dependencies(selected_mod.path)
  85. if toadd_hard == "" and toadd_soft == "" then
  86. retval = retval .. "," .. fgettext("No dependencies.")
  87. else
  88. if toadd_hard ~= "" then
  89. retval = retval .. "," .. fgettext("Dependencies:") .. ","
  90. retval = retval .. toadd_hard
  91. end
  92. if toadd_soft ~= "" then
  93. if toadd_hard ~= "" then
  94. retval = retval .. ","
  95. end
  96. retval = retval .. "," .. fgettext("Optional dependencies:") .. ","
  97. retval = retval .. toadd_soft
  98. end
  99. end
  100. retval = retval .. ";0]"
  101. retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
  102. .. fgettext("Uninstall selected mod") .. "]"
  103. end
  104. end
  105. return retval
  106. end
  107. --------------------------------------------------------------------------------
  108. local function handle_buttons(tabview, fields, tabname, tabdata)
  109. if fields["modlist"] ~= nil then
  110. local event = core.explode_table_event(fields["modlist"])
  111. tabdata.selected_mod = event.row
  112. return true
  113. end
  114. if fields["btn_mod_mgr_install_local"] ~= nil then
  115. core.show_file_open_dialog("mod_mgt_open_dlg",fgettext("Select Mod File:"))
  116. return true
  117. end
  118. if fields["btn_modstore"] ~= nil then
  119. local modstore_ui = ui.find_by_name("modstore")
  120. if modstore_ui ~= nil then
  121. tabview:hide()
  122. modstore.update_modlist()
  123. modstore_ui:show()
  124. else
  125. print("modstore ui element not found")
  126. end
  127. return true
  128. end
  129. if fields["btn_mod_mgr_rename_modpack"] ~= nil then
  130. local dlg_renamemp = create_rename_modpack_dlg(tabdata.selected_mod)
  131. dlg_renamemp:set_parent(tabview)
  132. tabview:hide()
  133. dlg_renamemp:show()
  134. return true
  135. end
  136. if fields["btn_mod_mgr_delete_mod"] ~= nil then
  137. local dlg_delmod = create_delete_mod_dlg(tabdata.selected_mod)
  138. dlg_delmod:set_parent(tabview)
  139. tabview:hide()
  140. dlg_delmod:show()
  141. return true
  142. end
  143. if fields["mod_mgt_open_dlg_accepted"] ~= nil and
  144. fields["mod_mgt_open_dlg_accepted"] ~= "" then
  145. modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
  146. return true
  147. end
  148. return false
  149. end
  150. --------------------------------------------------------------------------------
  151. return {
  152. name = "mods",
  153. caption = fgettext("Mods"),
  154. cbf_formspec = get_formspec,
  155. cbf_button_handler = handle_buttons,
  156. on_change = gamemgr.update_gamelist
  157. }