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