 menu_t = 1
name_rot = 0
local sel=b(5)
local name_x = 500
local te1_y = 300
local te2_y = 300
local te3_y = 300
local te4_y = 300
local te5_y = 300
local te6_y = 300
local can = false
local string=""
local state=""
local saves

while pge.running() do
	pge.controls.update()
	
	if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<5 and can then
		menu_t = menu_t + 1
		sel=b(5)
	end
	if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 and can then
		menu_t = menu_t - 1
		sel=b(5)
	end
	sel[menu_t]=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))
	
	if pge.controls.pressed(PGE_CTRL_CROSS) and can then
		if menu_t==1 then
			local temp = pge.dir.open("saves")
			saves = pge.dir.read(temp)
			pge.dir.close(temp)
			if saves then
				local temp
				while pge.running() do
					name_rot = name_rot + 0.045
					pge.controls.update()
					
					if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<#saves then
						menu_t = menu_t + 1
					end
					if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 then
						menu_t = menu_t - 1
					end
					if pge.controls.pressed(PGE_CTRL_CIRCLE) then
						break
					end
					if pge.controls.pressed(PGE_CTRL_CROSS) then
						name=saves[menu_t].name
						dofile("saves/"..saves[menu_t].name)
					end
					if pge.controls.pressed(PGE_CTRL_SQUARE) then
						pge.gfx.startdrawing()
						pge.texture.activate(pix["bg"])	
						pge.texture.draweasy(pix["bg"],0,0)
						pge.font.activate(font_1)
						pge.font.print(font_1,50,30,orange,"Are you sure you want to delete "..string.sub(saves[menu_t].name,1,-5).."?")
						pge.font.print(font_1,50,60,orange,"Press cross to delete")
						pge.font.print(font_1,50,80,orange,"Press cricle to cancel")
						pge.gfx.enddrawing()
						pge.gfx.swapbuffers()
						while pge.running() do
							pge.controls.update()
							if pge.controls.pressed(PGE_CTRL_CROSS) then
								pge.file.remove("saves/"..saves[menu_t].name)
								local temp = pge.dir.open("saves")
								saves = pge.dir.read(temp)
								pge.dir.close(temp)
								break
							end
							if pge.controls.pressed(PGE_CTRL_CIRCLE) then
								break
							end
						end
						if saves==nil then
							break
						end
						if menu_t>#saves then
							menu_t=#saves
						end
					end
					
					pge.gfx.startdrawing()
					
					pge.texture.activate(pix["bg"])	
					pge.texture.draweasy(pix["bg"],0,0)
					
					pge.font.activate(font_1)
					pge.font.print(font_1,50,30,orange,"Select world")
					pge.font.print(font_1,50,240,orange,"Press square to delete the selected world")
					for t=1,#saves do
						if menu_t~=t then
							temp=black
						else
							temp=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))
						end
						local temp2=math.floor(t/7)
						pge.font.print(font_1,50+temp2*140,30+t*30-temp2*210,temp,string.sub(saves[t].name,1,-5))
					end
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				menu_t = 1
			else
				pge.gfx.startdrawing()
				pge.texture.activate(pix["bg"])	
				pge.texture.draweasy(pix["bg"],0,0)
				pge.font.activate(font_1)
				pge.font.print(font_1,50,30,orange,"No saves found")
				pge.font.print(font_1,50,50,orange,"Press circle to return")
				pge.gfx.enddrawing()
				pge.gfx.swapbuffers()
				while not pge.controls.pressed(PGE_CTRL_CIRCLE) do
					pge.controls.update()
				end
			end
		elseif menu_t==2 then
			pge.utils.oskinit("New world name?","World"..math.random(1,15))
			while pge.running() do
				pge.gfx.startdrawing()
				pge.texture.activate(pix["bg"])	
				pge.texture.draweasy(pix["bg"],0,0)
				pge.gfx.enddrawing()
				state,string = pge.utils.oskupdate()
				pge.gfx.swapbuffers()
				if state then
					if string~="" then
						local temp
						if pge.file.exists("saves/"..string..".lua") then
							pge.gfx.startdrawing()
							pge.texture.activate(pix["bg"])	
							pge.texture.draweasy(pix["bg"],0,0)
							pge.font.activate(font_1)
							pge.font.print(font_1,50,30,orange,"World with that name already exists")
							pge.font.print(font_1,50,60,orange,"Press cross to overwrite")
							pge.font.print(font_1,50,80,orange,"Press cricle to cancel")
							pge.gfx.enddrawing()
							pge.gfx.swapbuffers()
							while pge.running() do
								pge.controls.update()
								if pge.controls.pressed(PGE_CTRL_CROSS) then
									break
								end
								if pge.controls.pressed(PGE_CTRL_CIRCLE) then
									temp=false
									break
								end
							end
						end
						if temp==nil then
							name=string..".lua"
							game_data={min=-63,max=64}
							height={[-64]=math.random(20,68)}
							gen(-63,64)
							time=0
							x=24
							y=select(1,string.find(game_data[5],"%a"))*48-192
							funq={}
							funq_05={}
							funq_10={}
							flags={}
							blocks={"d","s","u","l","G","D","i","5","3"}
							dofile("game.lua")
						end
						break
					else
						break
					end
				end
			end
		elseif menu_t==3 then
			pge.gfx.startdrawing()
			pge.texture.activate(pix["bg"])	
			pge.texture.draweasy(pix["bg"],0,0)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			pge.net.init()
			pge.utils.netinit()
			while pge.running() do
				pge.gfx.startdrawing()
				pge.texture.activate(pix["bg"])	
				pge.texture.draweasy(pix["bg"],0,0)
				pge.gfx.enddrawing()
				state=pge.utils.netupdate()
				pge.gfx.swapbuffers()
				if state~=PGE_UTILS_DIALOG_RUNNING then
					if state==0 then
						dofile("net.lua")
					elseif state==1 then
						pge.net.shutdown()
						break
					end
				end
			end
		elseif menu_t==4 then
			sel=b(2)
			menu_t=1
			local old=theme
			local theme_t
			if not themes then
				local dir=pge.dir.open("pix")
				local open=pge.dir.read(dir)
				pge.dir.close(dir)
				themes={}
				for i,v in pairs(open) do
					if v.dir then
						table.insert(themes,string.lower(v.name))
					end
				end
			end
			for i,v in pairs(themes) do
				if v==theme then
					theme_t=i
					break
				end
			end
			local old_cons=cons_file
			while pge.running() do
				name_rot = name_rot + 0.045
				
				pge.controls.update()
				if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<2 then
					menu_t = menu_t + 1
					sel=b(2)
				end
				if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 then
					menu_t = menu_t - 1
					sel=b(2)
				end
				sel[menu_t]=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))
				
				if menu_t==1 then
					if pge.controls.pressed(PGE_CTRL_LEFT) then
						if theme_t==1 then
							theme_t=#themes
						else
							theme_t=theme_t-1
						end
						string="You must restart to apply the theme"
						theme=themes[theme_t]
					end
					if pge.controls.pressed(PGE_CTRL_RIGHT) then
						if theme_t==#themes then
							theme_t=1
						else
							theme_t=theme_t+1
						end
						string="You must restart to apply the new theme"
						theme=themes[theme_t]
					end
				end
				
				if pge.controls.pressed(PGE_CTRL_CIRCLE) then
					break
				end
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					if menu_t==1 then
						if theme_t==#themes then
							theme_t=1
						else
							theme_t=theme_t+1
						end
						theme=themes[theme_t]
					elseif menu_t==2 then
						sel=b(15)
						menu_t=1
						local string="Select the function you wish to change"
						local cons={}
						cons[1]={
							"Move left: ",
							"Move right: ",
							"Jump: ",
							"Look up: ",
							"Look down: ",
							"Remove Block: ",
							"Place Block: ",
							"Move left in inventory: ",
							"Move right in inventory: ",
							"Open inventory / Use: ",
							"Pause: ",
							"Move camera left: ",
							"Move camera right: ",
							"Move camera up: ",
							"Move camera down: "
						}
						local conv={
							["0"]="Left",
							["1"]="Right",
							["2"]="Up",
							["3"]="Down",
							["4"]="Cross",
							["5"]="Circle",
							["6"]="Triangle",
							["7"]="Square",
							["8"]="L trigger",
							["9"]="R trigger",
							a="Select",
							b="Start",
							c="Analog left",
							d="Analog right",
							e="Analog down",
							f="Analog up"
						}
						cons[2]={}
						for t=1,15 do
							cons[2][t]=conv[char(cons_file,t)]
						end
						
						local key=false
						local can=true
						while pge.running() do
							name_rot = name_rot + 0.045
							
							pge.controls.update()
							
							sel[menu_t]=pge.gfx.createcolor(255,255,0,math.abs(math.sin(name_rot)*255))
							if can then
								if pge.controls.pressed(PGE_CTRL_DOWN) and menu_t<15 then
									menu_t = menu_t + 1
									sel=b(15)
								end
								if pge.controls.pressed(PGE_CTRL_UP) and menu_t>1 then
									menu_t = menu_t - 1
									sel=b(15)
								end
								
								if pge.controls.pressed(PGE_CTRL_CROSS) then
									string="Now press the key you prefer for that function"
									can=false
								end
								
								if pge.controls.pressed(PGE_CTRL_CIRCLE) then
									break
								end
							else
								if pge.controls.pressed(PGE_CTRL_LEFT) then key="0"
								elseif pge.controls.pressed(PGE_CTRL_RIGHT) then key="1"
								elseif pge.controls.pressed(PGE_CTRL_UP) then key="2"
								elseif pge.controls.pressed(PGE_CTRL_DOWN) then key="3"
								elseif pge.controls.pressed(PGE_CTRL_CIRCLE) then key="5"
								elseif pge.controls.pressed(PGE_CTRL_CROSS) then key="4"
								elseif pge.controls.pressed(PGE_CTRL_SQUARE) then key="7"
								elseif pge.controls.pressed(PGE_CTRL_TRIANGLE) then key="6"
								elseif pge.controls.pressed(PGE_CTRL_START) then key="b"
								elseif pge.controls.pressed(PGE_CTRL_SELECT) then key="a"
								elseif pge.controls.pressed(PGE_CTRL_LTRIGGER) then key="8"
								elseif pge.controls.pressed(PGE_CTRL_RTRIGGER) then key="9"
								end
								
								if pge.controls.analogx()<-45 then
									key="c"
								elseif pge.controls.analogx()>45 then
									key="d"
								elseif pge.controls.analogy()>45 then
									key="e"
								elseif pge.controls.analogy()<-45 then
									key="f"
								end
								
								if key then
									cons_file=string.sub(cons_file,1,menu_t-1)..key..string.sub(cons_file,menu_t+1)
									cons[2][menu_t]=conv[key]
									string="Select the function you wish to change"
									can=true
									key=false
								end
							end
							
							pge.gfx.startdrawing()
							pge.texture.activate(pix["bg"])	
							pge.texture.draweasy(pix["bg"],0,0)
							pge.font.activate(font_2)
							for t=1,15 do
								pge.font.print(font_2,120,14+t*14,sel[t],cons[1][t]..cons[2][t])
							end
							pge.font.activate(font_1)
							pge.font.print(font_1,35,248,orange,string)
							pge.gfx.enddrawing()
							pge.gfx.swapbuffers()
						end
						sel=b(2)
						menu_t=2
					end
				end
				pge.gfx.startdrawing()
				pge.texture.activate(pix["bg"])	
				pge.texture.draweasy(pix["bg"],0,0)
				pge.font.activate(font_1)
				pge.font.printcenter(font_1,100,sel[1],"Theme: "..theme)
				pge.font.printcenter(font_1,130,sel[2],"Change Controls")
				pge.gfx.enddrawing()
				pge.gfx.swapbuffers()
			end
			if old~=theme then
				local dir=pge.file.open("settings.cfg",PGE_FILE_RDONLY)
				local temp=pge.file.read(dir,15)
				pge.file.close(dir)
				local temp2=pge.file.open("settings.cfg",PGE_FILE_WRONLY+PGE_FILE_TRUNC)
				pge.file.write(temp2,temp..theme)
				pge.file.close(temp2)
				pix={}
				collectgarbage("collect")
				local temp = pge.texture.load("pix/load.png")
				pge.gfx.startdrawing()
				pge.texture.activate(temp)
				pge.texture.draweasy(temp,0,0)
				pge.gfx.enddrawing()
				pge.gfx.swapbuffers()
				dofile("resources.lua")
			end
			if cons_file~=old_cons then
				local temp=pge.file.open("settings.cfg",PGE_FILE_WRONLY)
				pge.file.write(temp,cons_file..theme)
				pge.file.close(temp)
				dofile("controls.lua")
			end
			sel=b(5)
			menu_t=4
		elseif menu_t==5 then
			pge.exit()
		end
	end
	
	if pge.controls.pressed(PGE_CTRL_LTRIGGER) and can then
		local x={}
		for t=1,6 do
			table.insert(x,-480)
		end
		local clouds={}
		for t=1,10 do
			table.insert(clouds,{math.random(480),math.random(10,210),math.random(38,82),math.random(20,62),pge.math.randfloat(0.7,1.4)})
		end
		while pge.running() do
			pge.controls.update()
			
			if pge.controls.pressed(PGE_CTRL_CIRCLE) then
				break
			end
			
			for t=1,6 do
				if x[t]<40 then
					x[t]=x[t]+12.5
					break
				end
			end
			
			pge.gfx.startdrawing()
			
			pge.texture.activate(pix["bg"])	
			pge.texture.draweasy(pix["bg"],0,0)
			
			pge.texture.activate(pix["cloud"])
			for t=1,#clouds do
				if clouds[t][1]<480 then
					if clouds[t][1]+clouds[t][3]>0 then
						pge.texture.draw(pix["cloud"], pge.math.round(clouds[t][1]),pge.math.round(clouds[t][2]),clouds[t][3],clouds[t][4])
					end
					clouds[t][1]=clouds[t][1]+clouds[t][5]
				else
					clouds[t]={math.random(-260,-90),math.random(10,210),math.random(38,82),math.random(20,62),pge.math.randfloat(0.7,1.4)}
				end
			end
			
			pge.font.activate(font_2)
			pge.font.print(font_2,x[1],40,orange,"Mojang for creating the original game")
			pge.font.print(font_2,x[2],60,orange,"InsertWittyName for the PGE Lua Engine")
			pge.font.print(font_2,x[3],80,orange,"Me (Perli55) for the coding")
			pge.font.print(font_2,x[4],100,orange,"Dr Higgle Tiggle for a Minecraft texture pack, ")
			pge.font.print(font_2,x[5],120,orange,"which I used to make the default theme")
			
			pge.font.print(font_2,x[6],200,orange,"Probably more names wil be added in the future")
			
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
		end
	end
	
	if not can then
		if name_x>105 then
			name_x = name_x - 5
		elseif te1_y>110 then
			te1_y = te1_y - 7.5
		elseif te2_y>140 then
			te2_y = te2_y - 7.5
		elseif te3_y>170 then
			te3_y = te3_y - 7.5
		elseif te4_y>200 then
			te4_y = te4_y - 7.5
		elseif te5_y>230 then
			te5_y = te5_y - 7.5
		elseif te6_y>210 then
			te6_y = te6_y - 7.5
		else 
			can = true
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			name_x=105
			te1_y=105
			te2_y=135
			te3_y=167.5
			te4_y=195
			te5_y=225
			te6_y=210
			can=true
		end
	end
	
	name_rot=name_rot+0.045
	
	pge.gfx.startdrawing()
	
	pge.texture.activate(pix["bg"])	
	pge.texture.draweasy(pix["bg"],0,0)
	pge.texture.activate(pix["credits"])
	pge.texture.draweasy(pix["credits"],329,te6_y)
	pge.texture.activate(pix["name"])
	pge.texture.draweasy(pix["name"],name_x, 15, math.sin( name_rot) /5)
	
	pge.font.activate(font_1)
	pge.font.printcenter(font_1,te1_y,sel[1],"Load World")
	pge.font.printcenter(font_1,te2_y,sel[2],"New World")
	pge.font.printcenter(font_1,te3_y,sel[3],"Online Section")
	pge.font.printcenter(font_1,te4_y,sel[4],"Options")
	pge.font.printcenter(font_1,te5_y,sel[5],"Exit")
	
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
end
