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