summaryrefslogtreecommitdiff
path: root/builtin/mainmenu/tab_simple_main.lua
blob: 46f4b6190f19c26afb30d49318659674ba246e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
--Minetest
--Copyright (C) 2013 sapier
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

--------------------------------------------------------------------------------
local function get_formspec(tabview, name, tabdata)
	local retval = ""

	local render_details = dump(core.setting_getbool("public_serverlist"))

	retval = retval ..
		"label[0,3.0;".. fgettext("Address/Port") .. "]"..
		"label[8,0.5;".. fgettext("Name/Password") .. "]" ..
		"field[0.25,3.25;5.5,0.5;te_address;;" ..core.setting_get("address") .."]" ..
		"field[5.75,3.25;2.25,0.5;te_port;;" ..core.setting_get("remote_port") .."]" ..
		"checkbox[8,-0.25;cb_public_serverlist;".. fgettext("Public Serverlist") .. ";" ..
		render_details .. "]"

	retval = retval ..
		"button[8,2.5;4,1.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
		"field[8.75,1.5;3.5,0.5;te_name;;" ..core.setting_get("name") .."]" ..
		"pwdfield[8.75,2.3;3.5,0.5;te_pwd;]"

	--favourites
	retval = retval ..
		"textlist[-0.05,0.0;7.55,2.75;favourites;"

	if #menudata.favorites > 0 then
		retval = retval .. render_favorite(menudata.favorites[1],render_details)

		for i=2,#menudata.favorites,1 do
			retval = retval .. "," .. render_favorite(menudata.favorites[i],render_details)
		end
	end

	if tabdata.fav_selected ~= nil then
		retval = retval .. ";" .. tabdata.fav_selected .. "]"
	else
		retval = retval .. ";0]"
	end

	-- separator
	retval = retval ..
		"box[-0.3,3.75;12.4,0.1;#FFFFFF]"

	-- checkboxes
	retval = retval ..
		"checkbox[1.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
		dump(core.setting_getbool("creative_mode")) .. "]"..
		"checkbox[5.0,3.9;cb_damage;".. fgettext("Enable Damage") .. ";" ..
		dump(core.setting_getbool("enable_damage")) .. "]" ..
		"checkbox[8,3.9;cb_fly_mode;".. fgettext("Fly mode") .. ";" ..
		dump(core.setting_getbool("free_move")) .. "]"
	-- buttons
	retval = retval ..
		"button[3.0,4.5;6,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]"

	return retval
end

--------------------------------------------------------------------------------

local function main_button_handler(tabview, fields, name, tabdata)
	if fields["btn_start_singleplayer"] then
		gamedata.selected_world	= gamedata.worldindex
		gamedata.singleplayer	= true
		core.start()
	end

	if fields["favourites"] ~= nil then
		local event = core.explode_textlist_event(fields["favourites"])

		if event.type == "CHG" then
			if event.index <= #maintab_favorites then
				local address = maintab_favorites[event.index].address
				local port = maintab_favorites[event.index].port

				if address ~= nil and
					port ~= nil then
					core.setting_set("address",address)
					core.setting_set("remote_port",port)
				end

				tabdata.fav_selected = event.index
			end
		end
		return
	end

	if fields["cb_public_serverlist"] ~= nil then
		core.setting_set("public_serverlist", fields["cb_public_serverlist"])

		if core.setting_getbool("public_serverlist") then
			asyncOnlineFavourites()
		else
			maintab_favorites = core.get_favorites("local")
		end
		return
	end

	if fields["cb_creative"] then
		core.setting_set("creative_mode", fields["cb_creative"])
	end

	if fields["cb_damage"] then
		core.setting_set("enable_damage", fields["cb_damage"])
	end

	if fields["cb_fly_mode"] then
		core.setting_set("free_move", fields["cb_fly_mode"])
	end

	if fields["btn_mp_connect"] ~= nil or
		fields["key_enter"] ~= nil then

		gamedata.playername		= fields["te_name"]
		gamedata.password		= fields["te_pwd"]
		gamedata.address		= fields["te_address"]
		gamedata.port			= fields["te_port"]

		local fav_idx = core.get_textlist_index("favourites")

		if fav_idx ~= nil and fav_idx <= #menudata.favorites and
			menudata.favorites[fav_idx].address == fields["te_address"] and
			menudata.favorites[fav_idx].port    == fields["te_port"] then

			gamedata.servername			= menudata.favorites[fav_idx].name
			gamedata.serverdescription	= menudata.favorites[fav_idx].description
		else
			gamedata.servername			= ""
			gamedata.serverdescription	= ""
		end

		gamedata.selected_world = 0

		core.setting_set("address",fields["te_address"])
		core.setting_set("remote_port",fields["te_port"])

		core.start()
		return
	end
end

--------------------------------------------------------------------------------
local function on_activate(type,old_tab,new_tab)
	if type == "LEAVE" then
		return
	end
	if core.setting_getbool("public_serverlist") then
		asyncOnlineFavourites()
	else
		menudata.favorites = core.get_favorites("local")
	end
end

--------------------------------------------------------------------------------
tab_simple_main = {
	name = "main",
	caption = fgettext("Main"),
	cbf_formspec = get_formspec,
	cbf_button_handler = main_button_handler,
	on_change = on_activate
	}