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