function OnSceneLoad(screen)
    game_state = {}
    LoadData(screen)
    
    menu:SetAllowInput(false)
    menu:Stop()
    players_screen = screen

    -- create bttn
    local bttn_ok = create_button_ex(screen,        'data/Texture/GUI/Button/bttn_ok.png',      'Ok',       687, 527, 105)
    game_state.bttn_delete = create_button_ex(screen,    'data/Texture/GUI/Button/bttn_delete.png',  'Delete',   867, 527, 105)
    local bttn_new = create_button_ex(screen,       'data/Texture/GUI/Button/bttn_new.png',     'New',      506, 527, 105)
    game_state.bttn_new = bttn_new
    
    game_state.text_user = {}
    
    for i = 0, 5 do
        local name_box = 'nik_box_'..i
        screen:GetObject(name_box):GetAnimation():SetColor('0x01FFFFFF')
    end

    screen:GetObject('nik_box'):SetOpacity(0)
    screen:IgnoreOnTestPoint('nik_box')
    
    UpdateUserPlayersScreen(false)
    PlaySound("opening_window")	
end

function UpdateUserPlayersScreen(move)
    for i, v in pairs(game_state.text_user) do
        v:Destroy()
    end -- for
    game_state.text_user = {}
    
    local vecUsers = GetGameManager():GetPlayers()
    local iSizeUser = vecUsers:size()
	if iSizeUser == 0 then
		game_state.bttn_delete:Stop()
	else
		game_state.bttn_delete:Go()
	end
    for i = 0, iSizeUser - 1 do
        local player = vecUsers:get(i)
        local nameUser = player:GetName()
        local textUser = CreateTextEntity('WindowText28', nameUser)
        local name_select_object = 'nik_box_' .. i
        local obj_select_object = players_screen:GetObject(name_select_object)
	    textUser:AttachTo( players_screen )
	    textUser:SetScale(1)
	    textUser:SetZ(150)
	    textUser:SetScrXY(obj_select_object:GetX(), obj_select_object:GetY() - 3)
	    textUser:SetOpacity(1)
	    textUser:SetTextAlignment(HGETEXT_TOP + HGETEXT_CENTER)
        table.insert(game_state.text_user, textUser)
        
        local TIME_MOVE_BOX = 0.5
        if player:GetActive() then
            if not move then
                TIME_MOVE_BOX = 0.001
            end
        
            local nik_box = players_screen:GetObject('nik_box')
            local af = CreateAnimationEffector()
            af:AttachTo( nik_box )
            af:SetKey(_TRACK_O, 0, nik_box:GetOpacity())
            af:SetKey(_TRACK_O, TIME_MOVE_BOX, 1)
            af:SetKey(_TRACK_X, 0, nik_box:GetX())
            af:SetKey(_TRACK_X, TIME_MOVE_BOX, obj_select_object:GetX())
            af:SetKey(_TRACK_Y, 0, nik_box:GetY())
            af:SetKey(_TRACK_Y, TIME_MOVE_BOX, obj_select_object:GetY())
            af:Play()
        end -- if player:GetActive() then
    end -- for i = 0, iSizeUser - 1 do
    
    if iSizeUser > 0 then
        players_screen:GetObject('nik_box'):SetVisible(true)
    else
        players_screen:GetObject('nik_box'):SetVisible(false)
    end
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function OnUpdate(dt)
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)
    obj_name = object:GetName()
    local player = nil
    
    if obj_name == 'nik_box_0' then
        player = GetGameManager():GetPlayerIndex(0)
    end
    if obj_name == 'nik_box_1' then
        player = GetGameManager():GetPlayerIndex(1)
    end
    if obj_name == 'nik_box_2' then
        player = GetGameManager():GetPlayerIndex(2)
    end
    if obj_name == 'nik_box_3' then
        player = GetGameManager():GetPlayerIndex(3)
    end
    if obj_name == 'nik_box_4' then
        player = GetGameManager():GetPlayerIndex(4)
    end
    if obj_name == 'nik_box_5' then
        player = GetGameManager():GetPlayerIndex(5)
    end
    if obj_name == 'nik_box_6' then
        player = GetGameManager():GetPlayerIndex(6)
    end
    if obj_name == 'nik_box_7' then
        player = GetGameManager():GetPlayerIndex(7)
    end

    if player then
        GetGameManager():SetCurrPlayer( player:GetName() )
        UpdateUserPlayersScreen(true)
    end    
end

function OnObjectMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    
    if title == 'Ok' then
        players_screen:SetAllowInput(false)
        players_screen:ScreenDestroy()
        players_screen = nil
        
        menu:Go()
        menu:SetAllowInput(true)
    end
    if title == 'Delete' and GetGameManager():GetProfilesCount() > 0 then
        players_screen:SetAllowInput(false)
        players_screen:Stop()
        
        local screen = load_screen('data/Lua/Screens/DialogDeletePlayer.lua')
        screen:SetLayer(players_screen:GetLayer() + 1)
    end
    if title == 'New' then
        players_screen:SetAllowInput(false)
--      players_screen:Stop()
                
        if GetGameManager():GetProfilesCount() < 6 then
            local time_hide = 0.2
            local af = CreateAnimationEffector()
            af:AttachTo(players_screen)
            af:SetKey(_TRACK_O, 0,          1)
            af:SetKey(_TRACK_O, time_hide,  0)
            af:Play()
            TimerDo(players_screen:GetTimer(), time_hide, function()
                                                            players_screen:Stop()
                                                            game_state.bttn_new:SetPressed(false)
                                                          end )

            local screen = load_screen('data/Lua/Screens/DialogPlayerName.lua')
            screen:SetLayer(players_screen:GetLayer() + 1)
            menu.change_player_button:SetOpacity(0)
        else
            local screen = load_screen('data/Lua/Screens/DialogErrorMaxPlayer.lua')
            screen:SetLayer(players_screen:GetLayer() + 1)
        end            
    end
    
    UpdateCurrentUser()
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('0x0FFFFFFF')
	screen:CreateObject('pixel_bottom_layer',anim,512,384,70,0,128,96)
	local anim = CreateAnimation('data/Texture/Screen/PlayersScreen/players.png',0,0,601,464,1,0)
	anim:SetHotSpot(300,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg_players',anim,687,375,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_0',anim,684,241,110,0,44,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_1',anim,684,280,110,0,44,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_2',anim,684,320,110,0,44,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_3',anim,684,360,110,0,44,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_4',anim,684,400,110,0,44,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('0xFFFFFFFF')
	screen:CreateObject('nik_box_5',anim,684,440,110,0,44,5)
	local anim = CreateAnimation('data/Texture/Screen/PlayersScreen/nik_box.png',0,0,360,70,1,0)
	anim:SetHotSpot(180,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nik_box',anim,681,589,350,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
