function OnSceneLoad(screen)
    LoadData(screen)

    dialog_corrupted_file = screen
    dialog_corrupted_file:SetAllowInput(true)
    
    local bttn_ok = create_button_ex(screen, 'data/Texture/GUI/Button/bttn_ok.png', 'ok', 697, 477, 110)
    bttn_ok:SetHotKey(HGEK_ENTER)
    bttn_ok:Stop()
    game_state.bttn_ok = bttn_ok
	
	game_state.players_name = CreateTextEntity("WindowText28", "|")
	game_state.players_name:AttachTo( screen:GetObject('YesNoScreen') )
	game_state.players_name:SetScale(1.0)
	game_state.players_name:SetZ(490)
	game_state.players_name:SetXY(0, 30)
	
	PlaySound("opening_window")
	SetCaptions(screen)
end

function SetCaptions(screen)
	text_title = CreateTextEntity("WindowText28", "")
    text_title:AttachTo( screen:GetObject('YesNoScreen') )
	text_title:SetBorderSize(420, 80)
	text_title:SetScale(1)
	text_title:SetZ(190)
	text_title:SetXY(-210, -90)
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function OnUpdate(dt)
    local ch = GetChar()
	if not ch or ch == 0 then
	    return
	end
	local text = GetNameCurPlayer()
	local size = text:len()
	
	if ch == HGEK_BACKSPACE then
	   if size > 0 then
	       text = text:sub(1, text:len() - 1)
		   game_state.players_name:SetText(text)
	   end
	else
   	   if ch ~= 0 and size < 10 and ch ~= HGEK_ENTER and ch ~= HGEK_ESCAPE then
            text = text..string.char(ch)
	   end
    end
    game_state.players_name:SetText(text.."|")
    
    if GetNameCurPlayer():len() > 0 then
        game_state.bttn_ok:Go()
    else
        game_state.bttn_ok:Stop()
    end
end

function GetNameCurPlayer()
    local text = game_state.players_name:GetText()
	text = text:sub(1, text:len() - 1)
	
	return text
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnButtonMouseEnter(title)
end

function OnObjectMouseDown(object)
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(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
        local name_player = GetNameCurPlayer()
        
        if GetGameManager():GetPlayer(name_player) then
            local screen = load_screen('data/Lua/Screens/DialogErrorProfileExists.lua')
            screen:SetLayer(40)
        else
            if name_player:len() > 0 then
                local result_add = GetGameManager():AddPlayer(name_player)
                if result_add then
                    GetGameManager():SetCurrPlayer(name_player)
                end
            end -- if name_player:len() > 0 then
            DestroyDialogCorruptedFile()
        end            
    end -- if title == 'yes' then
    
    if players_screen then
        players_screen:Go()
        players_screen:SetAllowInput(true)
        UpdateUserPlayersScreen(true)
        menu.change_player_button:SetOpacity(1)
    else
        menu:Go()
        menu:SetAllowInput(true)
        UpdateCurrentUser()
    end
end

function DestroyDialogCorruptedFile()
    if dialog_corrupted_file then
        dialog_corrupted_file:SetAllowInput(false)
        dialog_corrupted_file:ScreenDestroy()
        dialog_corrupted_file = nil
    end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
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('0x01FFFFFF')
	screen:CreateObject('pixel',anim,512,385,70,0,128,96)
	local anim = CreateAnimation('data/Texture/Screen/YesNoScreen/YesNoScreen.png',0,0,606,333,1,0)
	anim:SetHotSpot(303,166)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('YesNoScreen',anim,686,375,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Button/bttn_back.png',0,0,185,72,1,0)
	anim:SetHotSpot(92,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_back_yes',anim,692,482,105,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
