tab_mods.lua 5.1 KB

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