function OnSceneLoad(screen)        
    options = screen	

    options.game_state = {}    
    local game_state = options.game_state
    game_state.active_object = nil
    
    game_state.polar_coords = {}
	game_state.polar_coords.radius = 0
	game_state.polar_coords.angle = 0
    
    LoadData(screen)
    
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    screen:SetLayer(2)
	
	InitDialogWheels(screen)
	
	game_state.slider_angle = {}
	game_state.slider_angle.min = -0.32131460189801
	game_state.slider_angle.max = 1.1708798408508
	
	GetParameters()
	
	InitButtons(screen)		

	PostLoad(screen)
	
	game_state.center = {}
	game_state.center.x = 525
	game_state.center.y = 466
	
	screen:GetObject("resumeup"):SetVisible(false)
    screen:GetObject("resumeover"):SetVisible(false)
    screen:GetObject("resumedown"):SetVisible(false)
    screen:GetObject("exitup"):SetVisible(false)
    screen:GetObject("exitover"):SetVisible(false)
    screen:GetObject("exitdown"):SetVisible(false)
    
    screen:SetLayer(GUI_LAYER);
end

function PostLoad(screen)
	local anim = CreateAnimation('data/Texture/GUI/Options/front.png',0,0,597,186,1,0)
	anim:SetHotSpot(298,93)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('front',anim,524,472,136,0,1,1)
	screen:GetObject('bg'):Hide()
end

function InitButtons(screen)
    local up = screen:GetObject("buttonbigup"):GetAnimationPtr()
	local over = screen:GetObject("buttonbigover"):GetAnimationPtr()	
	local down = screen:GetObject("buttonbigdown"):GetAnimationPtr()
	local game_state = options.game_state;
	game_state.ok_button = CreateButton(up, over, down, "Resume", screen)
	game_state.ok_button:AttachTo(screen)
	game_state.ok_button:SetXY(523, 414)
	game_state.ok_button:SetZ(140)
end

function GetParameters()
    local game_state = options.game_state
    local volume = GetGameApp():GetSoundVolume()
	options:GetObject("slider_sound"):SetAngle(game_state.slider_angle.min + (game_state.slider_angle.max - game_state.slider_angle.min) * volume)
	local volume = GetGameApp():GetMusicVolume()
	options:GetObject("slider_music"):SetAngle(game_state.slider_angle.min + (game_state.slider_angle.max - game_state.slider_angle.min) * volume)
	local fullscreen = GetGameApp():IsFullScreen()
	if fullscreen then
	    options:GetObject('galochka'):SetOpacity(1)
        options:GetObject('pixel'):SetOpacity(0)
	else
	    options:GetObject('pixel'):SetOpacity(0.01)
        options:GetObject('galochka'):SetOpacity(0)
	end
	local custom_cursor = GetGameApp():IsCustomCursorVisible()
	if custom_cursor then
	    options:GetObject('galochka2'):SetOpacity(1)
        options:GetObject('pixel2'):SetOpacity(0)
	else
	    options:GetObject('pixel2'):SetOpacity(0.01)
        options:GetObject('galochka2'):SetOpacity(0)
	end
end

function SetParameters()
    local game_state = options.game_state
    local object = options:GetObject("slider_sound")
    local scale = game_state.slider_angle.max - game_state.slider_angle.min
    local volume = (object:GetAngle() - game_state.slider_angle.min) / scale
    volume = clamp(volume, 0, 1)
    GetGameApp():SetSoundVolume(volume)
	
	GetProfileManager():GetCurrentProfile().sound_volume = volume
	
    local object = options:GetObject("slider_music")
    local volume = (object:GetAngle() - game_state.slider_angle.min) / scale
    volume = clamp(volume, 0, 1)
    GetGameApp():SetMusicVolume(volume)
	
	GetProfileManager():GetCurrentProfile().music_volume = volume
	
    if options:GetObject('galochka'):GetOpacity() > 0 then
        GetGameApp():SetFullScreen(true)		
    end
    if options:GetObject('pixel'):GetOpacity() > 0 then
        GetGameApp():SetFullScreen(false)		
    end
	if options:GetObject('galochka2'):GetOpacity() > 0 then
	    GetGameApp():SetCustomCursor('CursorArrow')
        GetGameApp():SetCustomCursorVisible(true)		
    end
    if options:GetObject('pixel2'):GetOpacity() > 0 then
        GetGameApp():SetCustomCursorVisible(false)		
    end
	GetProfileManager():GetCurrentProfile().fullscreen = GetGameApp():IsFullScreen();
	GetProfileManager():GetCurrentProfile().custom_cursor_on = GetGameApp():IsCustomCursorVisible();
	
end

function OnUpdate(dt)
    GetParameters()
    local game_state = options.game_state
    if game_state.active_object then
	    local old_angle = game_state.polar_coords.angle
	
	    local v = Vector(GetMouseX() - game_state.center.x, GetMouseY() - game_state.center.y)
	
	    game_state.polar_coords.radius = v:Length()
											           
	    game_state.polar_coords.angle = v:Angle(nil)
	    --game_state.active_object.start_dangle = v:Angle(nil)
	    
	    --local obj_angle = game_state.active_object:GetAngle()
	    --local new_angle = obj_angle + game_state.polar_coords.angle - old_angle
		local desired_angle = game_state.active_object.start_angle + v:Angle(nil) - game_state.active_object.start_dangle
		desired_angle = clamp(desired_angle, game_state.slider_angle.min, game_state.slider_angle.max)
        game_state.active_object:SetAngle(desired_angle) 
        SetParameters()      
    end
end

function UpdateSound()
end

function UpdateMusic()
end

function OnExit()
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)   
    local obj_name = object:GetName()    
    local game_state = options.game_state
    if obj_name == 'slider_music' or obj_name == 'slider_sound' then
        game_state.active_object = options:GetObject(obj_name)        
		local v = Vector(GetMouseX() - game_state.center.x, GetMouseY() - game_state.center.y)
    	game_state.active_object.start_angle = game_state.active_object:GetAngle()		
		game_state.active_object.start_dangle = v:Angle(nil)
    end
    if obj_name == 'galochka' then
        options:GetObject('pixel'):SetOpacity(0.01)
        object:SetOpacity(0)
        SetParameters()
    end
    if obj_name == 'pixel' then
        options:GetObject('galochka'):SetOpacity(1)
        object:SetOpacity(0)
        SetParameters()
    end    
	if obj_name == 'galochka2' then
        options:GetObject('pixel2'):SetOpacity(0.01)
        object:SetOpacity(0)
        SetParameters()
    end
    if obj_name == 'pixel2' then
        options:GetObject('galochka2'):SetOpacity(1)
        object:SetOpacity(0)
        SetParameters()
    end    
end

function OnObjectRightMouseDown(object)
end

function OnScreenShown()
end

function OnObjectMouseUp(object)    
	options.game_state.active_object = nil	
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    ShowToolFailEffect()
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)    
    DefaultOnButtonPress(title)
	if title == "Resume" then		
		SetParameters();
		GetProfileManager():SaveProfiles()
		GetProfileManager():SaveProfiles()
		options:ScreenDestroy()		
	end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)    
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/GUI/Options/galochka.png',0,0,32,32,1,0)
	anim:SetHotSpot(16,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('galochka',anim,485,310,100,-50,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/strelka.png',0,0,62,153,1,0)
	anim:SetHotSpot(26,116)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('strelka',anim,521,466,134,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/01/fish.png',0,0,48,31,1,0)
	anim:SetHotSpot(24,235)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('slider_sound',anim,527,466,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/01/fish.png',0,0,48,31,1,0)
	anim:SetHotSpot(24,205)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('slider_music',anim,526,467,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel',anim,480,313,137,0,5,5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xDDFFFFFF')
	screen:CreateObject('bg',anim,512,384,88,0,128,96)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,0,168,72,1,0)
	anim:SetHotSpot(84,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigup',anim,699,48,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,72,168,72,1,0)
	anim:SetHotSpot(84,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigover',anim,701,95,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,144,168,72,1,0)
	anim:SetHotSpot(84,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigdown',anim,699,145,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/exit.png',0,0,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('exitup',anim,156,363,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/exit.png',0,66,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('exitover',anim,161,418,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/exit.png',0,132,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('exitdown',anim,156,469,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/resume.png',0,0,162,65,1,0)
	anim:SetHotSpot(81,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resumeup',anim,898,310,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/resume.png',0,65,162,65,1,0)
	anim:SetHotSpot(81,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resumeover',anim,907,371,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/resume.png',0,130,162,65,1,0)
	anim:SetHotSpot(81,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resumedown',anim,904,437,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/01/options.png',0,0,576,364,1,0)
	anim:SetHotSpot(288,182)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('options',anim,525,363,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel2',anim,677,406,137,0,5,5)
	local anim = CreateAnimation('data/Texture/GUI/Options/galochka.png',0,0,32,32,1,0)
	anim:SetHotSpot(16,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('galochka2',anim,680,409,100,40,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
