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