]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_config_world.lua
Reorganizing client and server tabs
[dragonfireclient.git] / builtin / mainmenu / dlg_config_world.lua
1 --Minetest
2 --Copyright (C) 2013 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
20 local function get_formspec(data)
21
22         local mod = data.list:get_list()[data.selected_mod]
23
24         local retval =
25                 "size[11,6.5,true]" ..
26                 "label[0.5,-0.25;" .. fgettext("World:") .. "]" ..
27                 "label[1.75,-0.25;" .. data.worldspec.name .. "]"
28
29         if data.hide_gamemods then
30                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
31         else
32                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
33         end
34
35         if data.hide_modpackcontents then
36                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
37         else
38                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
39         end
40
41         if mod == nil then
42                 mod = {name=""}
43         end
44         
45         retval = retval ..
46                 "label[0,0.45;" .. fgettext("Mod:") .. "]" ..
47                 "label[0.75,0.45;" .. mod.name .. "]" ..
48                 "label[0,1;" .. fgettext("Depends:") .. "]" ..
49                 "textlist[0,1.5;5,4.25;world_config_depends;" ..
50                 modmgr.get_dependencies(mod.path) .. ";0]" ..
51                 "button[9.25,6.35;2,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
52                 "button[7.4,6.35;2,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
53
54         if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
55                 if mod.is_modpack then
56                         local rawlist = data.list:get_raw_list()
57
58                         local all_enabled = true
59                         for j=1,#rawlist,1 do
60                                 if rawlist[j].modpack == mod.name and
61                                         rawlist[j].enabled ~= true then
62                                                 all_enabled = false
63                                                 break
64                                 end
65                         end
66
67                         if all_enabled == false then
68                                 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
69                         else
70                                 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
71                         end
72                 else
73                         if mod.enabled then
74                                 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
75                         else
76                                 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
77                         end
78                 end
79         end
80
81         retval = retval ..
82                 "button[8.5,-0.125;2.5,0.5;btn_all_mods;" .. fgettext("Enable all") .. "]" ..
83                 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
84
85         retval = retval .. modmgr.render_modlist(data.list)
86         retval = retval .. ";" .. data.selected_mod .."]"
87
88         return retval
89 end
90
91 local function enable_mod(this, toset)
92         local mod = this.data.list:get_list()[this.data.selected_mod]
93
94         if mod.typ == "game_mod" then
95                 -- game mods can't be enabled or disabled
96         elseif not mod.is_modpack then
97                 if toset == nil then
98                         mod.enabled = not mod.enabled
99                 else
100                         mod.enabled = toset
101                 end
102         else
103                 local list = this.data.list:get_raw_list()
104                 for i=1,#list,1 do
105                         if list[i].modpack == mod.name then
106                                 if toset == nil then
107                                         toset = not list[i].enabled
108                                 end
109                                 list[i].enabled = toset
110                         end
111                 end
112         end
113 end
114
115
116 local function handle_buttons(this, fields)
117
118         if fields["world_config_modlist"] ~= nil then
119                 local event = core.explode_textlist_event(fields["world_config_modlist"])
120                 this.data.selected_mod = event.index
121                 core.setting_set("world_config_selected_mod", event.index)
122
123                 if event.type == "DCL" then
124                         enable_mod(this)
125                 end
126                 
127                 return true
128         end
129
130         if fields["key_enter"] ~= nil then
131                 enable_mod(this)
132                 return true
133         end
134
135         if fields["cb_mod_enable"] ~= nil then
136                 local toset = core.is_yes(fields["cb_mod_enable"])
137                 enable_mod(this,toset)
138                 return true
139         end
140
141         if fields["btn_mp_enable"] ~= nil or
142                 fields["btn_mp_disable"] then
143                 local toset = (fields["btn_mp_enable"] ~= nil)
144                 enable_mod(this,toset)
145                 return true
146         end
147
148         if fields["cb_hide_gamemods"] ~= nil or
149                 fields["cb_hide_mpcontent"] ~= nil then
150                 local current = this.data.list:get_filtercriteria()
151
152                 if current == nil then
153                         current = {}
154                 end
155                 
156                 if fields["cb_hide_gamemods"] ~= nil then
157                         if core.is_yes(fields["cb_hide_gamemods"]) then
158                                 current.hide_game = true
159                                 this.data.hide_gamemods = true
160                                 core.setting_set("world_config_hide_gamemods", "true")
161                         else
162                                 current.hide_game = false
163                                 this.data.hide_gamemods = false
164                                 core.setting_set("world_config_hide_gamemods", "false")
165                         end
166                 end
167
168                 if fields["cb_hide_mpcontent"] ~= nil then
169                         if core.is_yes(fields["cb_hide_mpcontent"]) then
170                                 current.hide_modpackcontents = true
171                                 this.data.hide_modpackcontents = true
172                                 core.setting_set("world_config_hide_modpackcontents", "true")
173                         else
174                                 current.hide_modpackcontents = false
175                                 this.data.hide_modpackcontents = false
176                                 core.setting_set("world_config_hide_modpackcontents", "false")
177                         end
178                 end
179
180                 this.data.list:set_filtercriteria(current)
181                 return true
182         end
183
184         if fields["btn_config_world_save"] then
185
186                 local filename = this.data.worldspec.path ..
187                                 DIR_DELIM .. "world.mt"
188
189                 local worldfile = Settings(filename)
190                 local mods = worldfile:to_table()
191
192                 local rawlist = this.data.list:get_raw_list()
193
194                 local i,mod
195                 for i,mod in ipairs(rawlist) do
196                         if not mod.is_modpack and
197                                         mod.typ ~= "game_mod" then
198                                 if mod.enabled then
199                                         worldfile:set("load_mod_"..mod.name, "true")
200                                 else
201                                         worldfile:set("load_mod_"..mod.name, "false")
202                                 end
203                                 mods["load_mod_"..mod.name] = nil
204                         end
205                 end
206
207                 -- Remove mods that are not present anymore
208                 for key,value in pairs(mods) do
209                         if key:sub(1,9) == "load_mod_" then
210                                 worldfile:remove(key)
211                         end
212                 end
213
214                 if not worldfile:write() then
215                         core.log("error", "Failed to write world config file")
216                 end
217         
218                 this:delete()
219                 return true
220         end
221
222         if fields["btn_config_world_cancel"] then
223                 this:delete()
224                 return true
225         end
226
227         if fields["btn_all_mods"] then
228                 local list = this.data.list:get_raw_list()
229
230                 for i=1,#list,1 do
231                         if list[i].typ ~= "game_mod" and
232                                 not list[i].is_modpack then
233                                 list[i].enabled = true
234                         end
235                 end
236                 return true
237         end
238
239         return false
240 end
241
242 function create_configure_world_dlg(worldidx)
243
244         local dlg = dialog_create("sp_config_world",
245                                         get_formspec,
246                                         handle_buttons,
247                                         nil)
248
249         dlg.data.hide_gamemods = core.setting_getbool("world_config_hide_gamemods")
250         dlg.data.hide_modpackcontents = core.setting_getbool("world_config_hide_modpackcontents")
251         dlg.data.selected_mod = tonumber(core.setting_get("world_config_selected_mod"))
252         if dlg.data.selected_mod == nil then
253                 dlg.data.selected_mod = 0
254         end
255
256         dlg.data.worldspec = core.get_worlds()[worldidx]
257         if dlg.data.worldspec == nil then dlg:delete() return nil end
258
259         dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
260         
261         if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
262                         dlg.data.worldconfig.id == "" then
263
264                 dlg:delete()
265                 return nil
266         end
267         
268         dlg.data.list = filterlist.create(
269                         modmgr.preparemodlist, --refresh
270                         modmgr.comparemod, --compare
271                         function(element,uid) --uid match
272                                         if element.name == uid then
273                                                 return true
274                                         end
275                                 end,
276                                 function(element,criteria)
277                                         if criteria.hide_game and
278                                                 element.typ == "game_mod" then
279                                                         return false
280                                         end
281
282                                         if criteria.hide_modpackcontents and
283                                                 element.modpack ~= nil then
284                                                         return false
285                                                 end
286                                         return true
287                                 end, --filter
288                                 { worldpath= dlg.data.worldspec.path,
289                                   gameid = dlg.data.worldspec.gameid }
290                         )
291
292
293         if dlg.data.selected_mod > dlg.data.list:size() then
294                 dlg.data.selected_mod = 0
295         end
296
297         dlg.data.list:set_filtercriteria(
298                 {
299                         hide_game=dlg.data.hide_gamemods,
300                         hide_modpackcontents= dlg.data.hide_modpackcontents
301                 })
302         dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
303         dlg.data.list:set_sortmode("alphabetic")
304
305         return dlg
306 end