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