function OnSceneLoad(screen)
    LoadData(screen)
    
    screen:GetObject('bttn_background_ok'):GetAnimation():SetColor('0xFFFFFFFF')
    screen:GetObject('bttn_background_continue'):GetAnimation():SetColor('0xFFFFFFFF')
    screen:GetObject('bttn_background_quit'):GetAnimation():SetColor('0xFFFFFFFF')
    screen:GetObject('bttn_background_ok'):SetVisible(false)
    screen:GetObject('bttn_background_continue'):SetVisible(false)
    screen:GetObject('bttn_background_quit'):SetVisible(false)
	
	if play_field --[[and IsExistMiniGame() == false]] then
	    create_button_ex(screen, 'data/Texture/Screen/OptionsScreen/bttn_quit.png', 'Quit', 809, 532, 110)
	    local bttn_cont = create_button_ex(screen, 'data/Texture/Screen/OptionsScreen/bttn_continue.png', 'Continue', 588, 531, 110)
	    bttn_cont:SetHotKey(HGEK_ESCAPE)
	    
	    screen:GetObject('bttn_background_continue'):SetVisible(true)
        screen:GetObject('bttn_background_quit'):SetVisible(true)
    else
	    local bttn_ok = create_button_ex(screen, 'data/Texture/Screen/OptionsScreen/bttn_ok.png', 'Ok', 705, 533, 105)
	    bttn_ok:SetHotKey(HGEK_ESCAPE)
        screen:GetObject('bttn_background_ok'):SetVisible(true)
	end

	if play_field then
	    screen:GetObject('pixel_bg'):GetAnimation():SetColor('0xDD000000')
	else
	    screen:GetObject('pixel_bg'):GetAnimation():SetColor('0x01FFFFFF')
    end
    
    options_screen = screen
    options_screen.active_sound_object = nil
    options_screen.active_music_object = nil
    
    GetOptions()
    PlaySound("opening_window")
    
    local sound_volume = CreateSoundEntity('05_mg_gear_rotate')
    sound_volume:AttachTo(options_screen)
    options_screen.sound_volume = sound_volume
end

function OnSceneFree()
    FreeData()
end

function GetOptions()

    -- Get fullscreen
    local fullscreen = GetGameApp():IsFullScreen()
	if fullscreen then
	    options_screen:GetObject('check_box_fullscreen'):SetOpacity(1)
	else
	    options_screen:GetObject('check_box_fullscreen'):SetOpacity(0.01)
	end
	
	-- Get custom cursor
    local custom_cursor = GetGameApp():IsCustomCursorVisible()
	if custom_cursor then
	    options_screen:GetObject('check_box_custom_cursor'):SetOpacity(1)
	else
	    options_screen:GetObject('check_box_custom_cursor'):SetOpacity(0.01)
	end
	
	-- Get Volume Sound
	local volume_sound = GetGameApp():GetSoundVolume()
	local vol_line_obj = options_screen:GetObject('volume_line_sound')
	local x_vol_line_obj = vol_line_obj:GetScrX()
	local y_vol_line_obj = vol_line_obj:GetScrY()
	local width_vol_line_obj = vol_line_obj:GetAnimation():GetWidth()
	local height_vol_line_obj = vol_line_obj:GetAnimation():GetHeight()
	vol_line_obj :SetClippingRect(x_vol_line_obj, y_vol_line_obj, width_vol_line_obj * volume_sound, height_vol_line_obj )
    if volume_sound == 0 then
        vol_line_obj:SetVisible(false)
    else
        vol_line_obj:SetVisible(true)
    end
    options_screen:GetObject('slider_volume_sound'):SetScrX(x_vol_line_obj + width_vol_line_obj * volume_sound)
    
    -- Get Volume Music
    local volume_music = GetGameApp():GetMusicVolume()
	local vol_line_obj = options_screen:GetObject('volume_line_music')
	local x_vol_line_obj = vol_line_obj:GetScrX()
	local y_vol_line_obj = vol_line_obj:GetScrY()
	local width_vol_line_obj = vol_line_obj:GetAnimation():GetWidth()
	local height_vol_line_obj = vol_line_obj:GetAnimation():GetHeight()
	vol_line_obj :SetClippingRect(x_vol_line_obj, y_vol_line_obj, width_vol_line_obj * volume_music, height_vol_line_obj)
    if volume_music == 0 then
        vol_line_obj:SetVisible(false)
    else
        vol_line_obj:SetVisible(true)
    end
    options_screen:GetObject('slider_volume_music'):SetScrX(x_vol_line_obj + width_vol_line_obj * volume_music)
end

function OnScreenShown()
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
    
    GetOptions()
    
    if options_screen.active_sound_object then
        local act_obj = options_screen.active_sound_object
        local mouse_x = GetMousePosX()
        if mouse_x > act_obj:GetScrX() + act_obj:GetAnimation():GetWidth() then
            mouse_x = act_obj:GetScrX() + act_obj:GetAnimation():GetWidth()
        end
        if mouse_x < act_obj:GetScrX() then
            mouse_x = act_obj:GetScrX()
        end
        UpdateSound(mouse_x)
        
        if options_screen.sound_volume:IsPlaying() == false then
            options_screen.sound_volume:Play(true)
        end
        options_screen.sound_volume:SetVolume( GetGameApp():GetSoundVolume() )
    end
    
    if options_screen.active_music_object then
        local act_obj = options_screen.active_music_object
        local mouse_x = GetMousePosX()
        if mouse_x > act_obj:GetScrX() + act_obj:GetAnimation():GetWidth() then
            mouse_x = act_obj:GetScrX() + act_obj:GetAnimation():GetWidth()
        end
        if mouse_x < act_obj:GetScrX() then
            mouse_x = act_obj:GetScrX()
        end
        UpdateMusic(mouse_x)
    end
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnObjectMouseDown(object)
    if object:GetName() == 'check_box_fullscreen' then
        local fullscreen = GetGameApp():IsFullScreen()
        GetGameApp():SetFullScreen(not fullscreen)
    end
    if object:GetName() == 'check_box_custom_cursor' then
        local custom_cursor = GetGameApp():IsCustomCursorVisible()
        GetGameApp():SetCustomCursorVisible(not custom_cursor)
    end
    if object:GetName() == 'volume_line_sound' or object:GetName() == 'slider_volume_sound' then
        UpdateSound(GetMousePosX())
        options_screen.active_sound_object = options_screen:GetObject('volume_line_sound')
        options_screen.sound_volume:Play(true)
    end
    if object:GetName() == 'volume_line_music' or object:GetName() == 'slider_volume_music' then
        UpdateMusic(GetMousePosX())
        options_screen.active_music_object = options_screen:GetObject('volume_line_music')
    end
end

function UpdateSound(x)
    local mouse_x = x
    local delta_width = mouse_x - options_screen:GetObject('volume_line_sound'):GetScrX()
    local width = options_screen:GetObject('volume_line_sound'):GetAnimation():GetWidth()
    local volume = delta_width/width
    if volume < 0. then
        volume = 0
    end
    if volume > 1. then
        volume = 1
    end
    GetGameApp():SetSoundVolume(volume)
end

function UpdateMusic(x)
    local mouse_x = x
    local delta_width = mouse_x - options_screen:GetObject('volume_line_sound'):GetScrX()
    local width = options_screen:GetObject('volume_line_sound'):GetAnimation():GetWidth()
    local volume = delta_width/width
    if volume < 0. then
        volume = 0
    end
    if volume > 1. then
        volume = 1
    end
    GetGameApp():SetMusicVolume(volume)
end

function OnObjectMouseUp(object)
    options_screen.active_sound_object = nil
    options_screen.active_music_object = nil
    options_screen.sound_volume:Stop()
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    local current_screen = GetGameManager():GetCurrentScreen()
    
    if title == 'Ok' then
        current_screen:SetAllowInput(false)
        current_screen:ScreenDestroy()
        
        if menu then
            menu:SetAllowInput(true)
            menu:Go()
        end

        if IsExistMiniGame() then
            local screen_mini_game = game_state.screen_mini_game
            screen_mini_game:Go()
            screen_mini_game:SetAllowInput(true)
        end          
    end
    
    if title == 'Quit' then
        current_screen:SetAllowInput(false)
        current_screen:ScreenDestroy()
        
        local screen = load_screen('data/Lua/Screens/DialogExitToMenu.lua')
        screen:SetLayer(current_screen:GetLayer() + 1)
        
        if IsExistMiniGame() then
            screen:SetScrX(0)
        end
    end
    
    if title == 'Continue' then
        if IsExistMiniGame() then
            local screen_mini_game = game_state.screen_mini_game
            screen_mini_game:Go()
            screen_mini_game:SetAllowInput(true)
--        elseif IsExistComics() then
--            local comics = IsExistComics()
        else
            play_field:GetLevelScreen():GetPanel():SetAllowInput(true)
            play_field:GetLevelScreen():GetPhone():SetAllowInput(true)
            play_field:SetEnabled(true)
            play_field:GetLevelScreen():SetAllowInput(true)
           
            play_field:Go()
            play_field:GetLevelScreen():Go()
        end
        
        current_screen:SetAllowInput(false)
        current_screen:ScreenDestroy()
    end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnLostFocus()
end

function OnGainFocus()
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/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xDD000000')
	screen:CreateObject('pixel_bg',anim,512,384,70,0,528,296)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/background.png',0,0,610,469,1,0)
	anim:SetHotSpot(305,234)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,690,372,90,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/check_box.png',0,0,31,31,1,0)
	anim:SetHotSpot(15,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('check_box_fullscreen',anim,830,440,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/check_box.png',0,0,31,31,1,0)
	anim:SetHotSpot(15,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('check_box_custom_cursor',anim,830,377,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/volume_line.png',0,0,200,13,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('volume_line_sound',anim,740,241,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/volume_line.png',0,0,200,13,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('volume_line_music',anim,740,296,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/slider_volume.png',0,0,40,40,1,0)
	anim:SetHotSpot(23,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('slider_volume_sound',anim,743,251,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/slider_volume.png',0,0,40,40,1,0)
	anim:SetHotSpot(23,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('slider_volume_music',anim,743,305,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_background.png',0,0,209,67,1,0)
	anim:SetHotSpot(104,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_background_ok',anim,701,537,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_background.png',0,0,209,67,1,0)
	anim:SetHotSpot(104,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_background_continue',anim,584,536,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_background.png',0,0,209,67,1,0)
	anim:SetHotSpot(104,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_background_quit',anim,805,536,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_continue.png',0,0,160,50,1,0)
	anim:SetHotSpot(80,25)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('bttn_continue',anim,588,531,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_quit.png',0,0,160,50,1,0)
	anim:SetHotSpot(80,25)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('bttn_quit',anim,809,532,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Screen/OptionsScreen/bttn_ok.png',0,0,160,50,1,0)
	anim:SetHotSpot(80,25)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('bttn_ok',anim,705,533,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
