916b0c1887b1249f85e694746f9d75d9f91aab7a
[oweals/minetest.git] / builtin / mainmenu / tab_singleplayer.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 local function current_game()
19         local last_game_id = core.setting_get("menu_last_game")
20         local game, index = gamemgr.find_by_gameid(last_game_id)
21         
22         return game
23 end
24
25 local function singleplayer_refresh_gamebar()
26
27         local old_bar = ui.find_by_name("game_button_bar")
28
29         if old_bar ~= nil then
30                 old_bar:delete()
31         end
32
33         local function game_buttonbar_button_handler(fields)
34                 for key,value in pairs(fields) do
35                         for j=1,#gamemgr.games,1 do
36                                 if ("game_btnbar_" .. gamemgr.games[j].id == key) then
37                                         mm_texture.update("singleplayer", gamemgr.games[j])
38                                         core.set_topleft_text(gamemgr.games[j].name)
39                                         core.setting_set("menu_last_game",gamemgr.games[j].id)
40                                         menudata.worldlist:set_filtercriteria(gamemgr.games[j].id)
41                                         return true
42                                 end
43                         end
44                 end
45         end
46
47         local btnbar = buttonbar_create("game_button_bar",
48                 game_buttonbar_button_handler,
49                 {x=-0.3,y=5.65}, "horizontal", {x=12.4,y=1.15})
50
51         for i=1,#gamemgr.games,1 do
52                 local btn_name = "game_btnbar_" .. gamemgr.games[i].id
53                 
54                 local image = nil
55                 local text = nil
56                 local tooltip = core.formspec_escape(gamemgr.games[i].name)
57                 
58                 if gamemgr.games[i].menuicon_path ~= nil and
59                         gamemgr.games[i].menuicon_path ~= "" then
60                         image = core.formspec_escape(gamemgr.games[i].menuicon_path)
61                 else
62                 
63                         local part1 = gamemgr.games[i].id:sub(1,5)
64                         local part2 = gamemgr.games[i].id:sub(6,10)
65                         local part3 = gamemgr.games[i].id:sub(11)
66                         
67                         text = part1 .. "\n" .. part2
68                         if part3 ~= nil and
69                                 part3 ~= "" then
70                                 text = text .. "\n" .. part3
71                         end
72                 end
73                 btnbar:add_button(btn_name, text, image, tooltip)
74         end
75 end
76
77 local function get_formspec(tabview, name, tabdata)
78         local retval = ""
79
80         local index = filterlist.get_current_index(menudata.worldlist,
81                                 tonumber(core.setting_get("mainmenu_last_selected_world"))
82                                 )
83
84         retval = retval ..
85                         "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" ..
86                         "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" ..
87                         "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" ..
88                         "button[8.5,4.95;3.25,0.5;play;".. fgettext("Play") .. "]" ..
89                         "label[4,-0.25;".. fgettext("Select World:") .. "]"..
90                         "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
91                         dump(core.setting_getbool("creative_mode")) .. "]"..
92                         "checkbox[0.25,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
93                         dump(core.setting_getbool("enable_damage")) .. "]"..
94                         "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
95                         menu_render_worldlist() ..
96                         ";" .. index .. "]"
97         return retval
98 end
99
100 local function main_button_handler(this, fields, name, tabdata)
101
102         assert(name == "singleplayer")
103
104         local world_doubleclick = false
105
106         if fields["sp_worlds"] ~= nil then
107                 local event = core.explode_textlist_event(fields["sp_worlds"])
108                 local selected = core.get_textlist_index("sp_worlds")
109
110                 menu_worldmt_legacy()
111
112                 if event.type == "DCL" then
113                         world_doubleclick = true
114                 end
115
116                 if event.type == "CHG" and selected ~= nil then
117                         core.setting_set("mainmenu_last_selected_world",
118                                 menudata.worldlist:get_raw_index(selected))
119                         return true
120                 end
121         end
122
123         if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
124                 return true
125         end
126
127         if fields["cb_creative_mode"] then
128                 core.setting_set("creative_mode", fields["cb_creative_mode"])
129                 local selected = core.get_textlist_index("sp_worlds")
130                 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
131
132                 return true
133         end
134
135         if fields["cb_enable_damage"] then
136                 core.setting_set("enable_damage", fields["cb_enable_damage"])
137                 local selected = core.get_textlist_index("sp_worlds")
138                 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
139
140                 return true
141         end
142
143         if fields["play"] ~= nil or
144                 world_doubleclick or
145                 fields["key_enter"] then
146                 local selected = core.get_textlist_index("sp_worlds")
147                 
148                 if selected ~= nil then
149                         gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
150                         gamedata.singleplayer   = true
151                         
152                         core.start()
153                 end
154                 return true
155         end
156
157         if fields["world_create"] ~= nil then
158                 local create_world_dlg = create_create_world_dlg(true)
159                 create_world_dlg:set_parent(this)
160                 this:hide()
161                 create_world_dlg:show()
162                 mm_texture.update("singleplayer",current_game())
163                 return true
164         end
165
166         if fields["world_delete"] ~= nil then
167                 local selected = core.get_textlist_index("sp_worlds")
168                 if selected ~= nil and
169                         selected <= menudata.worldlist:size() then
170                         local world = menudata.worldlist:get_list()[selected]
171                         if world ~= nil and
172                                 world.name ~= nil and
173                                 world.name ~= "" then
174                                 local index = menudata.worldlist:get_raw_index(selected)
175                                 local delete_world_dlg = create_delete_world_dlg(world.name,index)
176                                 delete_world_dlg:set_parent(this)
177                                 this:hide()
178                                 delete_world_dlg:show()
179                                 mm_texture.update("singleplayer",current_game())
180                         end
181                 end
182                 
183                 return true
184         end
185
186         if fields["world_configure"] ~= nil then
187                 local selected = core.get_textlist_index("sp_worlds")
188                 if selected ~= nil then
189                         local configdialog =
190                                 create_configure_world_dlg(
191                                                 menudata.worldlist:get_raw_index(selected))
192                         
193                         if (configdialog ~= nil) then
194                                 configdialog:set_parent(this)
195                                 this:hide()
196                                 configdialog:show()
197                                 mm_texture.update("singleplayer",current_game())
198                         end
199                 end
200                 
201                 return true
202         end
203 end
204
205 local function on_change(type, old_tab, new_tab)
206         local buttonbar = ui.find_by_name("game_button_bar")
207         
208         if ( buttonbar == nil ) then
209                 singleplayer_refresh_gamebar()
210                 buttonbar = ui.find_by_name("game_button_bar")
211         end
212         
213         if (type == "ENTER") then
214                 local game = current_game()
215                 
216                 if game then
217                         menudata.worldlist:set_filtercriteria(game.id)
218                         core.set_topleft_text(game.name)
219                         mm_texture.update("singleplayer",game)
220                 end
221                 buttonbar:show()
222         else
223                 menudata.worldlist:set_filtercriteria(nil)
224                 buttonbar:hide()
225                 core.set_topleft_text("")
226                 mm_texture.update(new_tab,nil)
227         end
228 end
229
230 --------------------------------------------------------------------------------
231 tab_singleplayer = {
232         name = "singleplayer",
233         caption = fgettext("Singleplayer"),
234         cbf_formspec = get_formspec,
235         cbf_button_handler = main_button_handler,
236         on_change = on_change
237         }