]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/dlg_config_world.lua
Right mouse button behaviour for craft/inventory If right mousebutton clicked once...
[minetest.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
122                 if event.type == "DCL" then
123                         enable_mod(this)
124                 end
125                 
126                 return true
127         end
128
129         if fields["key_enter"] ~= nil then
130                 enable_mod(this)
131                 return true
132         end
133
134         if fields["cb_mod_enable"] ~= nil then
135                 local toset = core.is_yes(fields["cb_mod_enable"])
136                 enable_mod(this,toset)
137                 return true
138         end
139
140         if fields["btn_mp_enable"] ~= nil or
141                 fields["btn_mp_disable"] then
142                 local toset = (fields["btn_mp_enable"] ~= nil)
143                 enable_mod(this,toset)
144                 return true
145         end
146
147         if fields["cb_hide_gamemods"] ~= nil or
148                 fields["cb_hide_mpcontent"] ~= nil then
149                 local current = this.data.list:get_filtercriteria()
150
151                 if current == nil then
152                         current = {}
153                 end
154
155                 if core.is_yes(fields["cb_hide_gamemods"]) then
156                         current.hide_game = true
157                         this.data.hide_gamemods = true
158                 else
159                         current.hide_game = false
160                         this.data.hide_gamemods = false
161                 end
162                 
163                 if core.is_yes(fields["cb_hide_mpcontent"]) then
164                         current.hide_modpackcontents = true
165                         this.data.hide_modpackcontents = true
166                 else
167                         current.hide_modpackcontents = false
168                         this.data.hide_modpackcontents = false
169                 end
170
171                 this.data.list:set_filtercriteria(current)
172                 return true
173         end
174
175         if fields["btn_config_world_save"] then
176
177                 local filename = this.data.worldspec.path ..
178                                 DIR_DELIM .. "world.mt"
179
180                 local worldfile = Settings(filename)
181                 local mods = worldfile:to_table()
182
183                 local rawlist = this.data.list:get_raw_list()
184
185                 local i,mod
186                 for i,mod in ipairs(rawlist) do
187                         if not mod.is_modpack and
188                                         mod.typ ~= "game_mod" then
189                                 if mod.enabled then
190                                         worldfile:set("load_mod_"..mod.name, "true")
191                                 else
192                                         worldfile:set("load_mod_"..mod.name, "false")
193                                 end
194                                 mods["load_mod_"..mod.name] = nil
195                         end
196                 end
197
198                 -- Remove mods that are not present anymore
199                 for key,value in pairs(mods) do
200                         if key:sub(1,9) == "load_mod_" then
201                                 worldfile:remove(key)
202                         end
203                 end
204
205                 if not worldfile:write() then
206                         core.log("error", "Failed to write world config file")
207                 end
208         
209                 this:delete()
210                 return true
211         end
212
213         if fields["btn_config_world_cancel"] then
214                 this:delete()
215                 return true
216         end
217
218         if fields["btn_all_mods"] then
219                 local list = this.data.list:get_raw_list()
220
221                 for i=1,#list,1 do
222                         if list[i].typ ~= "game_mod" and
223                                 not list[i].is_modpack then
224                                 list[i].enabled = true
225                         end
226                 end
227                 return true
228         end
229
230         return false
231 end
232
233 function create_configure_world_dlg(worldidx)
234
235         local dlg = dialog_create("sp_config_world",
236                                         get_formspec,
237                                         handle_buttons,
238                                         nil)
239
240         --TODO read from settings
241         dlg.data.hide_gamemods = false
242         dlg.data.hide_modpackcontents = false
243         dlg.data.selected_mod = 0
244
245         dlg.data.worldspec = core.get_worlds()[worldidx]
246         if dlg.data.worldspec == nil then dlg:delete() return nil end
247
248         dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
249         
250         if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
251                         dlg.data.worldconfig.id == "" then
252
253                 dlg:delete()
254                 return nil
255         end
256         
257         dlg.data.list = filterlist.create(
258                         modmgr.preparemodlist, --refresh
259                         modmgr.comparemod, --compare
260                         function(element,uid) --uid match
261                                         if element.name == uid then
262                                                 return true
263                                         end
264                                 end,
265                                 function(element,criteria)
266                                         if criteria.hide_game and
267                                                 element.typ == "game_mod" then
268                                                         return false
269                                         end
270
271                                         if criteria.hide_modpackcontents and
272                                                 element.modpack ~= nil then
273                                                         return false
274                                                 end
275                                         return true
276                                 end, --filter
277                                 { worldpath= dlg.data.worldspec.path,
278                                   gameid = dlg.data.worldspec.gameid }
279                         )
280                         
281         dlg.data.list:set_filtercriteria(
282                 {
283                         hide_game=dlg.data.hide_gamemods,
284                         hide_modpackcontents= dlg.data.hide_modpackcontents
285                 })
286         dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
287         dlg.data.list:set_sortmode("alphabetic")
288         
289         return dlg
290 end