#include "missions.lua"

function optionsSlider(setting, def, mi, ma)
	UiColor(1,1,0.5)
	UiPush()
		UiTranslate(0, -8)
		local val = GetInt(setting)
		val = (val-mi) / (ma-mi)
		local w = 100
		UiRect(w, 3)
		UiAlign("center middle")
		val = UiSlider("common/dot.png", "x", val*w, 0, w) / w
		val = math.floor(val*(ma-mi)+mi)
		SetInt(setting, val)
	UiPop()
	return val
end

mapCurInput = ""

function optionsInputDesc(op, key, x1,mapinput)
	UiPush()
		if mapinput then
			UiAlign("left")
			UiTranslate(x1,-17)
			if UiIsMouseInRect(230, 20) and InputPressed("lmb") then
				mapCurInput = key;
			end
			if mapCurInput == key then
				local str = InputLastPressedKey()
				if str ~= "" and str ~= "tab" and str~= "esc" and tonumber(str) == nil then
					mapCurInput = ""
					SetString(key,str)
				end
				UiColor(1,1,1,0.2)
			else
				UiColor(1,1,1,0.1)
			end
			UiRect(230, 20)
		end
	UiPop()
	UiPush()
		UiText(op)
		UiTranslate(x1,0)
		UiAlign("left")
		UiColor(0.7,0.7,0.7)
		if mapinput then
			UiText(string.upper(GetString(key)))
		else
			UiText(key)
		end
	UiPop()
	UiTranslate(0, UiFontHeight())
end

function toolTip(desc)
	local showDesc = false
	UiPush()
		UiAlign("top left")
		UiTranslate(-265, -18)
		if UiIsMouseInRect(550, 21) and UiReceivesInput() then
			showDesc = true
		end
	UiPop()
	if showDesc then
		UiPush()
			UiTranslate(340, -50)
			UiFont("regular.ttf", 20)
			UiAlign("left")
			UiWordWrap(300)
			UiColor(0, 0, 0, 0.5)
			local w,h = UiGetTextSize(desc)
			UiImageBox("common/box-solid-6.png", w+40, h+40, 6, 6)
			UiColor(.8, .8, .8)
			UiTranslate(20, 37)
			UiText(desc)
		UiPop()
	end
end

function group(name, line)
	UiPush()
		UiAlign("top center")
		UiTranslate(0, -24)
		if line then
			UiColor(1,1,1,0.1)
			UiRect(300, 2)
		end
		UiTranslate(0, -28)
		UiFont("bold.ttf", 24)
		UiColor(0.9, 0.9, 0.9)
		UiText(name)
	UiPop()
end

function drawOptions(scale, allowDisplayChanges)
	if scale == 0.0 then
		gOptionsShown = false
		return true 
	end

	if not gOptionsShown then
		UiSound("common/options-on.ogg")
		gOptionsShown = true
	end

	UiModalBegin()
	
	if not optionsTab then
		optionsTab = "gfx"
	end
	
	local displayMode = GetInt("options.display.mode")
	local displayResolution = GetInt("options.display.resolution")

	if not optionsCurrentDisplayMode then
		optionsCurrentDisplayMode = displayMode
		optionsCurrentDisplayResolution = displayResolution
	end
	
	local applyResolution = allowDisplayChanges and optionsTab == "display" and (displayMode ~= optionsCurrentDisplayMode or displayResolution ~= optionsCurrentDisplayResolution)
	local open = true
	UiPush()
		UiFont("regular.ttf", 26)

		UiColorFilter(1,1,1,scale)
		
		UiTranslate(UiCenter(), UiMiddle())
		UiAlign("center middle")
		
		local showLargeUI = GetBool("game.largeui")
		if showLargeUI then
			UiScale(1.2)
		end
		UiScale(1, scale)
		UiWindow(640, 800)
		UiAlign("top left")

		if InputPressed("esc") or (not UiIsMouseInRect(640, 800) and InputPressed("lmb")) then
			UiSound("common/options-off.ogg")
			Command("hydra.eventSettings")
			if mapCurInput == "" then
				open = false
			end
			mapCurInput = ""
		end

		UiColor(.0, .0, .0, 0.55)
		UiImageBox("common/box-solid-shadow-50.png", 640, 800, -50, -50)

		UiColor(0.96, 0.96, 0.96)
		UiPush()
			UiFont("regular.ttf", 26)
			local w = 0.3
			UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 0.96, 0.96, 0.96, 0.8)
			UiAlign("center middle")
			UiScale(1)
			UiTranslate(80, 40)
			local oldTab = optionsTab
			UiPush()
				if optionsTab == "display" then 
					UiColor(1,1,0.7)
					UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 1, 1, 0.7, 0.8)
				end
				if UiTextButton("àîôãñ", 110, 40) then optionsTab = "display" end
			UiPop()
			UiTranslate(120, 0)
			UiPush()
				if optionsTab == "gfx" then 
					UiColor(1,1,0.7)
					UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 1, 1, 0.7, 0.8)
				end
				if UiTextButton("Ãôãúìîã", 110, 40) then optionsTab = "gfx" end
			UiPop()
			UiTranslate(120, 0)
			UiPush()
				if optionsTab == "audio" then
					UiColor(1,1,0.7)
					UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 1, 1, 0.7, 0.8)
				end
				if UiTextButton("Àùèìò", 110, 40) then optionsTab = "audio" end
			UiPop()
			UiTranslate(120, 0)
			UiPush()
				if optionsTab == "game" then 
					UiColor(1,1,0.7)
					UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 1, 1, 0.7, 0.8)
				end
				if UiTextButton("Éçôã", 110, 40) then optionsTab = "game" end
			UiPop()
			UiTranslate(120, 0)
			UiPush()
				if optionsTab == "input" then
					UiColor(1,1,0.7)
					UiButtonImageBox("common/box-outline-fill-6.png", 6, 6, 1, 1, 0.7, 0.8)
				end
				if UiTextButton("Ôóôãåïéñ.", 110, 40) then optionsTab = "input" end
			UiPop()
			if optionsTab ~= oldTab then
				UiSound("common/click.ogg")
			end
		UiPop()

		UiPush()
			UiFont("regular.ttf", 22)
			UiTranslate(0, 150)
			local x0 = 320
			local x1 = 20
			
			UiTranslate(x0, 0)
			UiAlign("right")

			local lh = 22

			if optionsTab == "display" then
				if allowDisplayChanges then
					UiText("Ñéêìð")
					UiAlign("left")
					UiTranslate(x1,0)
					toolTip("Éçôã äùèéö òöòäôãêéñã å óòïñò½îôãññòð ôéêìðé. Teardown å ñãõöò¿ÿéé åôéð¿ ïùýþé åõéçò ôãäòöãéö õ ýãõöòöòí òäñòåïéñì¿ 60 Ãü, öãî ýöò òñ äùèéö ãåöòðãöìýéõîì åºäôãñ, éõïì óòèèéôêìåãéöõ¿ åãþìð ðòñìöòôòð. àöò ôéîòðéñèùéðã¿ ñãõöôòíîã èï¿ äòï¼þìñõöåã õìõöéð.")
					if displayMode == 0 then UiColor(1,1,0.7) else UiColor(1,1,1) end
					if UiTextButton("Ðòïñò½îôãññºí") then
						SetInt("options.display.mode", 0)
					end
					UiTranslate(0, lh)
					toolTip("Éçôã äùèéö òöòäôãêéñã å òîòññòð ôéêìðé.")
					if displayMode == 1 then UiColor(1,1,0.7) else UiColor(1,1,1) end
					if UiTextButton("Â òîñé") then
						SetInt("options.display.mode", 1)
					end
					UiTranslate(0, lh)
					toolTip("Éçôã äùèéö òöòäôãêéñã å òîòññòð ôéêìðé, å óòïñºí ½îôãñ ì äéë óòïéí, ìõóòï¼ëù¿ öò êé ôãëôéþéñìé ì ýãõöòöù òäñòåïéñì¿, ýöò ì Âãþ ôãäòýìí õöòï. Îã ñéîòöòôºû õìõöéðãû ½öò ðòêéö óôìåéõöì î óòåºþéñì¾ óôòìëåòèìöéï¼ñòõöì.")
					if displayMode == 2 then UiColor(1,1,0.7) else UiColor(1,1,1) end
					if UiTextButton("Ðòïñò½îôãññºí å òîñé") then
						SetInt("options.display.mode", 2)
						SetInt("options.display.resolution", 0)
					end
					UiTranslate(0, lh)

					UiTranslate(0, lh)
					UiTranslate(-x1, 0)
					UiAlign("right")
					UiColor(1,1,1)

					UiText("Ñãëôéþéñìé")		
					UiAlign("left")
					UiTranslate(x1,0)
					if displayMode == 2 then
						local w,h = GetDisplayResolution(2, 0)
						UiColor(.8, .8, .8)
						UiText(w.."x"..h)
					else
						local c = GetDisplayResolutionCount(displayMode)
						for i=0,c-1 do
							if displayResolution==i then
								UiColor(1,1,0.7)
							else
								UiColor(1,1,1)
							end
							local w,h = GetDisplayResolution(displayMode, i)
							if UiTextButton(w.."x"..h) then
								SetInt("options.display.resolution", i)
							end
							UiTranslate(0, lh)
						end	
					end
				else
					UiAlign("center")
					UiText("Îãõöôòíîì ½îôãñã èòõöùóñº\nöòï¼îò ìë çïãåñòçò ðéñ¾")
				end
			end

			if optionsTab == "gfx" then
				UiPush()
					toolTip("Íãõþöãäìôòåãñìé ôãëôéþéñì¿ ôéñèéôìñçã å óôòüéñöãû (öéîõöòåºí ìñöéôúéíõ óò-óôéêñéðù äùèéö òöòäôãêãö¼õ¿ å óòïñòð ôãëôéþéñìì). Òñìêéñìé ½öòçò óãôãðéöôã ëñãýìöéï¼ñò óòåºõìö óôòìëåòèìöéï¼ñòõö¼ ñã äòï¼þìñõöåé õìõöéð.")
					UiText("Íãõþöãä çôãúìîì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local res = GetInt("options.gfx.renderscale")
					if res == 100 then
						if UiTextButton("100%") then		
							SetInt("options.gfx.renderscale", 75)
						end
					elseif res == 75 then
						if UiTextButton("75%") then		
							SetInt("options.gfx.renderscale", 50)
						end
					else
						if UiTextButton("50%") then		
							SetInt("options.gfx.renderscale", 100)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				
				UiPush()
					toolTip("àöòö óãôãðéöô åïì¿éö ñã îãýéõöåò åìëùãïìëãüìì öéñéí, òöôãêéñìí ì þùðòóòèãåïéñì¿, ã öãîêé ñã óôòìëåòèìöéï¼ñòõö¼ äòï¼þìñõöåã õìõöéð.")
					UiText("Ëãýéõöåò çôãúìîì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local quality = GetInt("options.gfx.quality")
					if quality == 3 then
						if UiTextButton("Âºõòîòé") then		
							SetInt("options.gfx.quality", 1)
						end
					elseif quality == 2 then
						if UiTextButton("Òôéèñéé") then		
							SetInt("options.gfx.quality", 3)
						end
					else
						if UiTextButton("Îìëîòé") then		
							SetInt("options.gfx.quality", 2)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				UiTranslate(0, 20)
				
				UiPush()
					UiText("Ããððã-îòôôéîüì¿")
					UiTranslate(x1, 0)
					UiAlign("left")
					local val = optionsSlider("options.gfx.gamma", 100, 50, 150)
					UiTranslate(120, 0)
					UiText(val/100)
				UiPop()
				UiTranslate(0, lh)

				UiPush()
					UiText("Ðòïé ëôéñì¿")
					UiTranslate(x1, 0)
					UiAlign("left")
					local val = optionsSlider("options.gfx.fov", 90, 60, 120)
					UiTranslate(120, 0)
					UiText(val)
				UiPop()
				UiTranslate(0, lh)

				UiPush()
					UiText("Ãïùäìñã ôéëîòõöì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.gfx.dof")
					if val == 1 then
						if UiTextButton("ÂËÌ") then		
							SetInt("options.gfx.dof", 0)
						end
					else
						if UiTextButton("ÂÞËÌ") then		
							SetInt("options.gfx.dof", 1)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				
				UiPush()
					UiText("Ììñëòåºé ìõîãêéñì¿")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.gfx.barrel")
					if val == 1 then
						if UiTextButton("ÂËÌ") then		
							SetInt("options.gfx.barrel", 0)
						end
					else
						if UiTextButton("ÂÞËÌ") then		
							SetInt("options.gfx.barrel", 1)
						end
					end
				UiPop()	
				UiTranslate(0, lh)
				
				UiPush()
					UiText("Ñãëðºöìé å èåìêéñìì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.gfx.motionblur")
					if val == 1 then
						if UiTextButton("ÂËÌ") then		
							SetInt("options.gfx.motionblur", 0)
						end
					else
						if UiTextButton("ÂÞËÌ") then		
							SetInt("options.gfx.motionblur", 1)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				UiTranslate(0, 20)

				UiPush()
					toolTip('Teardown óôéèñãëñãýéñ èï¿ ìçôº õ åîï¾ýéññòí åéôöìîãï¼ñòí õìñûôòñìëãüìéí. Íº ñãõöò¿öéï¼ñò ôéîòðéñèùéð ìõóòï¼ëòåãö¼ "îãêèºí îãèô" ì ýãõöòöù òäñòåïéñì¿ ðòñìöòôã 60 Ãü èï¿ ðãîõìðãï¼ñò óïãåñòçò åòõóôì¿öì¿. Ïöîï¾ýéñìé åéôöìîãï¼ñòí õìñûôòñìëãüìì ìñòçèã ðòêéö óôìåéõöì î óôéôºåìõöòðù èåìêéñì¾ îãðéôº.')
					UiText("Âéôöìîãï¼ñã¿ õìñûôòñìëãüì¿")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.gfx.vsync")
					if val == -1 then
						if UiTextButton("Àèãóöìåñºí") then		
							SetInt("options.gfx.vsync", 1)
						end
					elseif val == 1 then
						if UiTextButton("Ëãêèºí îãèô") then		
							SetInt("options.gfx.vsync", 2)
						end
					elseif val == 2 then
						if UiTextButton("Ëãêèºí åöòôòí îãèô") then		
							SetInt("options.gfx.vsync", 0)
						end
					else
						if UiTextButton("ÂÞËÌ") then		
							SetInt("options.gfx.vsync", -1)
						end
					end
				UiPop()
				UiTranslate(0, lh)
			end
			
			if optionsTab == "audio" then
				UiPush()
					UiText("Íùëºîã")
					UiTranslate(x1, 0)
					UiAlign("left")
					optionsSlider("options.audio.musicvolume", 100, 0, 100)
				UiPop()
				UiTranslate(0, lh)
				UiPush()
					UiText("Èåùîì")
					UiTranslate(x1, 0)
					UiAlign("left")
					optionsSlider("options.audio.soundvolume", 100, 0, 100)
				UiPop()
				UiTranslate(0, lh)
				if not GetBool("game.deploy") then
					UiPush()
						UiText("Èåùîì òîôùêéñì¿")
						UiTranslate(x1, 0)
						UiAlign("left")
						optionsSlider("options.audio.ambiencevolume", 100, 0, 100)
					UiPop()
					UiTranslate(0, lh)
				end
				UiPush()
					UiText("Íéñ¾ ðùëºîì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.audio.menumusic")
					if val == 1 then
						if UiTextButton("Íùëºîã") then		
							SetInt("options.audio.menumusic", 0)
						end
					else
						if UiTextButton("Âºîï¾ýéñò") then		
							SetInt("options.audio.menumusic", 1)
						end
					end
				UiPop()
				UiTranslate(0, lh)
			end
			
			if optionsTab == "game" then
				local showLargeUI = GetBool("game.largeui")
				UiTranslate(0, -20)
				UiPush()
					UiAlign("left")
					UiTranslate(-200, -20)
					if showLargeUI then
						UiFont("regular.ttf", 21)
					else
						UiFont("regular.ttf", 20)
					end
	
					UiWordWrap(430)
					UiText("Íº õèéïãïì åõé åòëðòêñòé, ýöòäº õäãïãñõìôòåãö¼ õïòêñòõö¼ ìçôº Teardown èò óòèûòè¿ÿéçò, óò ñãþéðù ðñéñì¾, ùôòåñ¿ õïòêñòõöì. Åõïì åãð îãêéöõ¿, ýöò ìçôã õïìþîòð õïòêñã¿, õïìþîòð ïéçîã¿ ìïì åº óôòõöò ûòöìöé óòïùýìö¼ äòïéé õóòîòíñºí òóºö, åº ðòêéöé åñéõöì îòôôéîöìåº ëèéõ¼.")
				UiPop()
				UiTranslate(0, 150)
				group("Ëãðóãñì¿", true)
				UiPush()
					toolTip("àöã òóüì¿ ôéçùïìôùéö îòïìýéõöåò åôéðéñì èò óôìäºöì¿ åéôöòïéöã å ðìõõì¿û îãðóãñìì õ öãíðéôòð. Ôåéïìýéñìé åôéðéñì òäïéçýìö ìçôù.")
					UiText("Òìçñãï öôéåòçì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.campaign.time")
					if val == 15 then
						if UiTextButton("+15 õéîùñè") then		
							SetInt("options.game.campaign.time", 30)
						end
					elseif val == 30 then
						if UiTextButton("+30 õéîùñè") then		
							SetInt("options.game.campaign.time", 60)
						end
					elseif val == 60 then
						if UiTextButton("+60 õéîùñè") then		
							SetInt("options.game.campaign.time", -10)
						end
					elseif val == -10 then
						if UiTextButton("-10 õéîùñè (Òïòêñò)") then		
							SetInt("options.game.campaign.time", -20)
						end
					elseif val == -20 then
						if UiTextButton("-20 õéîùñè (Òïòêñò)") then		
							SetInt("options.game.campaign.time", 0)
						end
					else
						if UiTextButton("Âºîï¾ýéñò") then		
							SetInt("options.game.campaign.time", 15)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				UiPush()
					toolTip("Â ñãýãïé îãêèòí ðìõõìì îãðóãñìì ñãõöôòíöé óãöôòñº èï¿ åõéû ìñõöôùðéñöòå. Áòï¼þéé îòïìýéõöåò óãöôòñòå òäïéçýìö ìçôù. àöò ñé åïì¿éö ñã óãöôòñº å òöðºýîãû ìïì óãöôòñº å îòñüéñöôãöòôé..")
					UiText("Îãõöôòíöé óãöôòñº")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.campaign.ammo")
					if val == 50 then
						if UiTextButton("+50%") then		
							SetInt("options.game.campaign.ammo", 100)
						end
					elseif val == 100 then
						if UiTextButton("+100%") then		
							SetInt("options.game.campaign.ammo", -1)
						end
					elseif val == -1 then
						if UiTextButton("Îéö óãöôòñòå (Òïòêñò)") then		
							SetInt("options.game.campaign.ammo", 0)
						end
					else
						if UiTextButton("Âºîï¾ýéñò") then		
							SetInt("options.game.campaign.ammo", 50)
						end
					end
				UiPop()				
				UiTranslate(0, lh)
				UiPush()
					toolTip("Îãõöôòíöé ðãîõìðãï¼ñòé ëèòôòå¼é. Áòï¼þéé îòïìýéõöåò ëèòôòå¼¿ õñìêãéö åéôò¿öñòõö¼ õðéôöì òö åëôºåòå, óùï¼, òçñ¿, åòèº ì ö.è .")
					UiText("Îãõöôòìö¼ ëèòôòå¼é")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.campaign.health")
					if val == 50 then
						if UiTextButton("+50%") then		
							SetInt("options.game.campaign.health", 100)
						end
					elseif val == 100 then
						if UiTextButton("+100%") then		
							SetInt("options.game.campaign.health", -50)
						end
					elseif val == -50 then
						if UiTextButton("-50% (Òïòêñò)") then		
							SetInt("options.game.campaign.health", 0)
						end
					else
						if UiTextButton("Âºîï¾ýéñò") then		
							SetInt("options.game.campaign.health", 50)
						end
					end
				UiPop()				
				UiTranslate(0, lh)
				UiPush()
					toolTip("àöã òóüì¿ óòëåòï¿éö óôòóùõöìö¼ ðìõõì¾ îãðóãñìì, éõïì òñã îãêéöõ¿ åãð õïìþîòð öôùèñòí. Âîï¾ýéñìé ½öòí òóüìì èòäãåìö îñòóîì óôòóùõîã ñã öéôðìñãï ì ½îôãñ ñéùèãý.")
					UiText("Ðôòóùõî ðìõõìì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.missionskipping")
					if val == 1 then
						if UiTextButton("Âîï¾ýìö¼") then		
							SetInt("options.game.missionskipping", 0)
						end
					else
						if UiTextButton("Âºîï¾ýìö¼") then		
							SetInt("options.game.missionskipping", 1)
						end
					end
				UiPop()				
				UiTranslate(0, lh)
				UiPush()
					toolTip("Ñãëôéþìö¼ ðéñ¾ õóãåñã å ï¾äòé åôéð¿, ã ñé öòï¼îò ñã ùôòåñ¿û óéõòýñìüº.")
					UiText("Ñãëôéþìö¼ õóãåñ")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.spawn")
					if val == 1 then
						if UiTextButton("Âîï¾ýìö¼") then		
							SetInt("options.game.spawn", 0)
						end
					else
						if UiTextButton("Âºîï¾ýìö¼") then		
							SetInt("options.game.spawn", 1)
						end
					end
				UiPop()				
				
				UiTranslate(0, 100)
				
				group("Ðéõòýñìüã", true)
				UiPush()
					toolTip("Ñãëäïòîìôùíöé åõé ùôòåñì å ôéêìðé óéõòýñìüº, èãêé éõïì òñì éÿé ñé èòõöìçñùöº å îãðóãñìì. Åõïì åº õòäìôãéöéõ¼ óôòíöì îãðóãñì¾, ðº ôéîòðéñèùéð òöîï¾ýìö¼ ½öù úùñîüì¾, ýöòäº ñé ìõóòôöìö¼ åóéýãöïéñì¿ òö ìçôº.")
					UiText("Ñãëäïòîìôòåãö¼ åõé ùôòåñì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.sandbox.unlocklevels")
					if val == 1 then
						if UiTextButton("Âîï¾ýìö¼") then		
							SetInt("options.game.sandbox.unlocklevels", 0)
						end
					else
						if UiTextButton("Âºîï¾ýìö¼") then		
							SetInt("options.game.sandbox.unlocklevels", 1)
						end
					end
				UiPop()
				UiTranslate(0, lh)
				UiPush()
					toolTip("Ñãëäïòîìôùíöé åõé ìñõöôùðéñöº å ôéêìðé óéõòýñìüº, èãêé éõïì òñì éÿé ñé óòïùýéñº å îãðóãñìì. Åõïì åº õòäìôãéöéõ¼ óôòíöì îãðóãñì¾, ðº ôéîòðéñèùéð òöîï¾ýìö¼ ½öù úùñîüì¾, ýöòäº ñé ìõóòôöìö¼ åóéýãöïéñì¿ òö ìçôº.")
					UiText("Ñãëäïòîìôòåãö¼ åõé ìñõöôùðéñöº")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.game.sandbox.unlocktools")
					if val == 1 then
						if UiTextButton("Âîï¾ýìö¼") then		
							SetInt("options.game.sandbox.unlocktools", 0)
						end
					else
						if UiTextButton("Âºîï¾ýìö¼") then		
							SetInt("options.game.sandbox.unlocktools", 1)
						end
					end
				UiPop()
				
				UiTranslate(0, 100)

				local totalScore = 0
				local missionCount = 0
				local missions = ListKeys("savegame.mission")
				for i=1,#missions do
					local s = GetInt("savegame.mission."..missions[i]..".score")
					if s > 0 then
						totalScore = totalScore + s
						missionCount = missionCount + 1
					end
				end
				local tools = ListKeys("savegame.tool")
				local toolCount = #tools
		
				group("Ðôòçôéõõ", true)
				UiPush()
					UiText("Ðôòíèéññºé ðìõõìì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(0.7,0.7,0.7)
					UiText(missionCount)
				UiPop()
				UiTranslate(0, lh)
				UiPush()
					UiText("Ñãëäïòîìôòåãñº ìñõöôùðéñöº")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(0.7,0.7,0.7)
					UiText(toolCount)
				UiPop()
				UiTranslate(0, lh)
				UiPush()
					UiText("Âõéçò òýîòå")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(0.7,0.7,0.7)
					UiText(totalScore)
				UiPop()
				UiTranslate(0, lh+20)
				UiPush()
					UiPush()
						UiTranslate(0, 6)
						toolTip("Éõóòï¼ëùíöé ½öò, ýöòäº ñãåõéçèã õöéôéö¼ åéõ¼ óôòçôéõõ å õòûôãñéññºû ìçôãû ì ñãýãö¼ åõé õ ñùï¿.")
					UiPop()
					UiAlign("center middle")
					UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1, 0.9)
					if UiTextButton("Òäôòõ óôòçôéõõã...", 200, 34) then
						confirmWipe = 0
						SetValue("confirmWipe", 1, "easeout", 0.25)
					end
				UiPop()
				UiTranslate(0, lh+30)
				if showLargeUI then
					UiFont("regular.ttf", 20)
				else
					UiFont("regular.ttf", 18)
				end 
				UiColor(0.8, 0.8, 0.8, 0.5)
				UiAlign("center")
				UiText("Âãþ úãíï õòûôãñéñì¿ ìçôº ñãûòèìöõ¿ ëèéõ¼:", true)
				UiText(GetString("game.savegamepath"))
			end
			
			if optionsTab == "input" then
				UiTranslate(0, -30)
			
				UiPush()
					UiText("Úùåõöåìöéï¼ñòõö¼ ðºþì")
					UiTranslate(x1, 0)
					UiAlign("left")
					optionsSlider("options.input.sensitivity", 100, 25, 200)
				UiPop()
				UiTranslate(0, lh)

				UiPush()
					UiText("Òçïãêìåãñìé ðºþì")
					UiTranslate(x1, 0)
					UiAlign("left")
					optionsSlider("options.input.smoothing", 0, 0, 100)
				UiPop()
				UiTranslate(0, lh)

				UiPush()
					UiText("Éñåéôõì¿ ðºþì")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.input.invert")
					if val == 1 then
						if UiTextButton("ÂËÌ") then		
							SetInt("options.input.invert", 0)
						end
					else
						if UiTextButton("ÂÞËÌ") then		
							SetInt("options.input.invert", 1)
						end
					end
				UiPop()	
				UiTranslate(0, lh)

				UiPush()
					toolTip("àúúéîö îãýãñì¿ çòïòåº ì éé ñãîïòñã. Ðòóôòäùíöé õñìëìö¼ éçò, éõïì åº ìõóºöºåãéöé öòþñòöù ìïì çòïòåòîôùêéñìé åò åôéð¿ ìçôº.")
					UiText("Ðòîãýìåãñìé çòïòåº")
					UiTranslate(x1, 0)
					UiAlign("left")
					UiColor(1,1,0.7)
					local val = GetInt("options.input.toolsway")
					if val == 1 then
						if UiTextButton("Âîï¾ýìö¼") then		
							SetInt("options.input.toolsway", 0)
						end
					else
						if UiTextButton("Âºîï¾ýìö¼") then		
							SetInt("options.input.toolsway", 1)
						end
					end
				UiPop()	
				UiTranslate(0, lh)

				UiPush()
					toolTip("Éëðéñìöé ½úúéîö óòîãýìåãñì¿ çòïòåº ì ñãîïòñã. Ðòóôòäùíöé ùðéñ¼þìö¼ ½öòö óãôãðéöô, éõïì åò åôéð¿ ìçôº åº ìõóºöºåãéöé öòþñòöù ìïì çòïòåòîôùêéñìé.")
					UiText("Ëãýãñìé çòïòåòí")
					UiTranslate(x1, 0)
					UiAlign("left")
					optionsSlider("options.input.headbob", 0, 0, 100)
				UiPop()

				UiTranslate(0, 30)
				UiTranslate(0, lh)
				UiTranslate(0, lh)

				UiPush()
					UiColor(1, 1, 1, 0.05)
					UiAlign("center top")
					UiTranslate(0, -37)
					UiImageBox("common/box-solid-6.png", 580, 545, 6, 6)
				UiPop()

				UiPush()
					if GetString("game.input.curdevice") == "gamepad" then
						UiColorFilter(1,1,1,0.5)
						UiDisableInput()
					end

					UiTranslate(0, 10)
					
					optionsInputDesc("Äåìçãö¼õ¿ åóéôéè", "options.input.keymap.forward", x1, true)
					optionsInputDesc("Äåìçãö¼õ¿ ñãëãè", "options.input.keymap.backward", x1, true)
					optionsInputDesc("Äåìçãö¼õ¿ åïéåò", "options.input.keymap.left", x1, true)
					optionsInputDesc("Äåìçãö¼õ¿ åóôãåò", "options.input.keymap.right", x1, true)
					optionsInputDesc("Ðôºêòî", "options.input.keymap.jump", x1, true)
					optionsInputDesc("Ðôìõéõö¼", "options.input.keymap.crouch", x1, true)
					optionsInputDesc("Âëãìðòèéíõöåìé", "options.input.keymap.interact", x1, true)
					optionsInputDesc("Õòñãô¼", "options.input.keymap.flashlight", x1, true)

					UiTranslate(0, 4)
					UiPush()
						UiTranslate(x1,8)
						UiAlign("left")
						UiButtonImageBox("common/box-outline-6.png", 6, 6, 0.96, 0.96, 0.96, 0.9)
						if UiTextButton("Òäôòõ î õöãñèãôöù", 230, 30) then
							Command("options.input.keymap.resettodefault")
						end
					UiPop()
					UiTranslate(0, 55)

					optionsInputDesc("Ëãôöã", "Tab", x1, false)
					optionsInputDesc("Ðãùëã", "Esc", x1, false)
					UiTranslate(0, 20)
					optionsInputDesc("Òðéñìö¼ ìñõöôùðéñö", "Ëòïéõò ðºþì ìïì 1-6", x1, false)
					optionsInputDesc("Éõóòï¼ëòåãö¼ ìñõöôùðéñö", "ÌËÍ", x1, false)
					UiTranslate(0, 20)
					optionsInputDesc("Âë¿ö¼", "Èãêãö¼ ÐËÍ", x1, false)
					optionsInputDesc("Äìõöãñüì¿ ëãûåãöã", "Èãêãö¼ ÐËÍ + Ëòïéõò ðºþì", x1, false)
					optionsInputDesc("Áôòõìö¼", "Èãêãö¼ ÐËÍ + ÌËÍ", x1, false)
					local _, height = UiGetRelativePos()
				UiPop()
				UiTranslate(0, height)
				
				UiTranslate(0, UiFontHeight()+10)
				UiPush()
					UiText("Ãéíðóãè")
					UiTranslate(x1,2)
					UiAlign("left")
					local hasController = GetBool("game.steam.hascontroller")
					if hasController then
						UiColor(1,1,1)
						UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1, 0.9)
						if UiTextButton("Ëòñúìçùôãüì¿ Steam...", 230, 30) then
							Command("game.steam.showbindingpanel")
						end
					else
						UiDisableInput()
						UiColor(0.8,0.8,0.8)
						UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1, 0.5)
						UiTextButton("Ãéíðóãè ñé òäñãôùêéñ", 230, 30)
						UiEnableInput()
					end
				UiPop()
				UiTranslate(0, UiFontHeight())
			end

		UiPop()

		UiPush()
			UiTranslate(UiCenter(), UiHeight()-50)
			UiAlign("center middle")
			if applyResolution then
				UiTranslate(0,-40)
				UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1, 0.9)
				if UiTextButton("Ðôìðéñìö¼ ½öì ñãõöôòíîì", 300, 40) then
					Command("game.applydisplay")
				end
			end
		UiPop()
	UiPop()

	if confirmWipe and confirmWipe > 0 then
		UiBlur(confirmWipe)
		UiPush()
			UiColor(1, 1, 1, 0.1*confirmWipe)
			UiRect(UiWidth(), UiHeight())
		UiPop()
		UiTranslate(UiCenter(), UiMiddle())
		UiModalBegin()
		UiPush()
			UiTranslate(0, 400*(1-confirmWipe))
			UiScale(confirmWipe)
			UiColorFilter(1, 1, 1, confirmWipe)
			UiColor(.0, .0, .0, 0.55)
			UiAlign("center middle")
			UiImageBox("common/box-solid-shadow-50.png", 500, 200, -50, -50)
			UiWindow(500, 200)
			UiColor(0.9, 0.9, 0.9)
			UiPush()
				UiTranslate(UiCenter(), 30)
				UiFont("bold.ttf", 32)
				UiText("Âº ùåéôéñº?")
			UiPop()
			UiPush()
				UiTranslate(50, 70)
				UiAlign("left")
				UiWordWrap(400)
				UiColor(1,1,1)
				UiFont("regular.ttf", 20)
				UiText("Åõïì åº õäôòõìöé óôòçôéõõ, åõé èãññºé åãþìû õòûôãñéñìí äùèùö ñãåõéçèã óòöéô¿ñº, ì åãð óôìèéöõ¿ ñãýìñãö¼ åõé õ ñùï¿.")
			UiPop()

			UiFont("regular.ttf", 24)
			UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1, 0.5)
			UiPush()
				UiTranslate(130, UiHeight()-40)
				UiColor(1, 1, 1)
				if UiTextButton("Ïöðéñã", 200, 40) or InputPressed("esc") then
					SetValue("confirmWipe", 0, "easein", 0.25) 
				end
			UiPop()
			UiPush()
				UiTranslate(UiWidth()-130, UiHeight()-40)
				UiColor(1,0.5, 0.5)
				if UiTextButton("Òäôòõ óôòçôéõõã", 200, 40) then
					local keys = ListKeys("savegame")
					for i=1, #keys do
						ClearKey("savegame."..keys[i])
					end
					if not allowDisplayChanges then
						--If we're not already on main menu, go there
						Menu()
					end
					SetValue("confirmWipe", 0, "easein", 0.25) 
				end
			UiPop()
		UiPop()
		UiModalEnd()
	end

	
	UiModalEnd()
	
	return open
end

function clamp(value, mi, ma)
	if value < mi then value = mi end
	if value > ma then value = ma end
	return value
end