
scenario = "packet_error";
no_logfile = true;
active_buttons = 3; 
button_codes = 1,2,3;
default_background_color = 127, 127, 127;   
default_text_color = 255, 255, 255;
default_font_size = 18;

begin;


### Inserting test participant name:
picture {
   text {
#      font_size = 24;
      caption = "Please type in your name and press return";
   };
   x = 0; y = 100;
   text {
      font_size = 24;
      caption = " ";
   } response_text;
   left_x = -200;
   y = 0;
} pic;
###

### New calibration screen:
picture {
	background_color = 127, 127, 127;
	box {
		height = 10; width = 10;
		color = 255, 255, 255;
		}box1;
		x=0; y=0;
	} et_calibration;
###
	

### Presenting image 
bitmap   ###Creating an empty bitmap. In PCL we load the images inside this bitmap to present them 
{
	preload = false;
	
} btmBitmap;

trial {    
   trial_duration = forever;
   trial_type = first_response; 
   
   picture {  
      box { height = 1; width = 1; color = 0,0,0; };
      x = 0; y = 0;
   } pic1;
   time = 0;
} trial1;


###

### give pic to rego to center
trial {    
	trial_duration = 1000;
		picture{    
					description = "image01.bmp";
						bitmap{ 
								filename = "files\\recenter.jpg";
						}bmp1;
					x = 0; 
					y = 0;        
		}pic2;
} trial_recenter;
###

### Choosing Score
array {
   text { caption = "0"; } text0;    
   text { caption = "1"; } text1;    
   text { caption = "2"; } text2;
   text { caption = "3"; } text3;
   text { caption = "4"; } text4;
   text { caption = "5"; } text5;
   text { caption = "6"; } text6;
   text { caption = "7"; } text7;
   text { caption = "8"; } text8;
   text { caption = "9"; } text9;
   text { caption = "10"; } text10;
   text { caption = "Very Low";} textPoor;
   text { caption = "Very High";} textExcellent;
} numbers;

picture {   
   # the line
   box { color = 255,255,255; height = 3; width = 500; };
   x = 0; y = -20;  
	# scale marks
   box { color = 255,255,255; height = 10; width = 3; };
   x = -250; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = -200; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = -150; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = -100; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = -50; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 0; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 50; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 100; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 150; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 200; y = -20;  
   box { color = 255,255,255; height = 10; width = 3; };
   x = 250; y = -20; 
   # the cursor
   box { color = 255,0,0; height = 20; width = 7; };
   x = 0; y = -20;
   # the text
   text { caption = "\n\nMove the mouse to control the score slider.\nLeft-click to choose your score"; } cap_text;
   x = 0; y = -150;
   text text0; 
   x = -250; y = 10;     
   text text1; 
   x = -200; y = 10;     
   text text2; 
   x = -150; y = 10;
   text text3; 
   x = -100; y = 10;
   text text4; 
   x = -50; y = 10;
   text text5; 
   x = 0; y = 10;
   text text6; 
   x = 50; y = 10;
   text text7; 
   x = 100; y = 10;
   text text8; 
   x = 150; y = 10;
   text text9; 
   x = 200; y = 10;  
   text text10; 
   x = 250; y = 10;
   text { caption = "Poor"; } poor_text;
   x = -250; y = -50;
   text { caption = "Excellent"; } excellent_text;
   x =  250; y = -50;
} slider_pic;

###


### Instriction screens:
# the instruction trials


trial {
   picture {
				text { caption = " place_holder "; } instruction_text;
      x = 0; y = 30;
   };
   time = 0;
   duration = response;
} instruction_trial;



### Video material

trial {

	trial_duration = forever;
	trial_type = first_response;
	
   video { filename = "FlickAnimation.avi"; 
   speed_type = video_speed_hz;
   speed = 50.0;
   use_audio = false;
   } vid;
       
} video_trial;





#<<<<<<PCL>>>>>>>#
begin_pcl;

#################################### VARIABLES DECLARATIONS ##################################	

array< string > aastrVideoNames[0][0];
int iExpCount = 0;
int vidsPerGroup = 1; #set depending on how many sessions
#bool isFirst;
string file_name;
int iCount = 1;

#mouse stuff
mouse ms = response_manager.get_mouse( 1 );
ms.set_min_max( 1, -250, 250 );
int iCursor_x_delta; 
int resp_count_pre;

#files
input_file fvidNames = new input_file;
output_file fNewVidLstBkp = new output_file; # saving a backup copy of the list of filenames
output_file fExpRes = new output_file;
output_file fExpComm = new output_file;
int start_time;

# eye traacker stuff

#	int Caltype = 13; #>>>> set the strenght of calibration (5,9,13)
#	eye_tracker tracker = new eye_tracker( "{F62A86B9-6F75-49B7-944F-2B4DECA92F48}" );
#	string cmd_et_bmp = "ET_BMP " + "c:\\judith\\";                                                            
#	string cmd_et_sav = "ET_SAV " + "c:\\judith\\";
	
################################### SUBROUTINES DECLARATIONS #################################	
	
### getting name of participant:
sub
	string get_response
	
	begin
	
		system_keyboard.set_case_mode( 3 ); ##### Accept capital letter input
		system_keyboard.set_max_length( 1 );
	
		loop until false begin  #start the loop
			response_text.set_caption( " " + file_name );
			response_text.redraw();
			pic.present();
			string letter = system_keyboard.get_input(); 
			if (system_keyboard.last_input_type() == keyboard_delimiter) then
				break # end the loop
			end;
      if (letter == "=" && file_name.count( ) != 0) then # Check if you were pressing backspace and there are still letters in the string
		int Str_lngth = file_name.count( );
		Str_lngth = Str_lngth - 1;
		file_name.resize( Str_lngth );
      end; 
      if (letter != "=") then     
      file_name = file_name + letter 
		end;  
		end;
		return file_name
	end; 
###

## loading videos
sub
	loadvideos (string filename)
	begin
	
	fvidNames.open( filename );
	
	iExpCount = int(fvidNames.get_line());
	
		loop 
			int iGroup = 0;
			int iNameCount = 1;
			
			string strGroupName = "";
			string strVidName = "";
			string strFileName = "";
		until fvidNames.end_of_file() # parse text file
		begin
			int iChar = 1;

			strFileName = fvidNames.get_line();
			
			loop # extract image name
			until strFileName[ iChar ] == '.' || strFileName[ iChar ] == '(' || strFileName[ iChar ] == '\n'
			begin
				iChar = iChar + 1;
			end;
				
			strVidName = strFileName.substring( 1, iChar - 1 ); 	
			
			if( strVidName != strGroupName || strGroupName == "" )
			then
				iGroup = iGroup + 1;
				aastrVideoNames.resize( iGroup );
				iNameCount = 1;
				strGroupName = strVidName;
			end;
			aastrVideoNames[iGroup].resize( iNameCount );
			aastrVideoNames[ iGroup ] [iNameCount] = strFileName;
			iNameCount = iNameCount + 1;
		end;
		#imgsPerGroup = iNameCount;
		fvidNames.close();
	###
	end;
	
## DISPLAYS videos	
sub
	display_videos
	begin
		vid.set_filename( aastrVideoNames[iCount][mod( iExpCount, vidsPerGroup)+1]);
		vid.prepare();

		### recentering
		trial_recenter.present();
		###
			   int resp_count = response_manager.response_data_count( );


		##
		fExpRes.print( (clock.time()-start_time) ); fExpRes.print("\t");
		##
		string strTempVidName = aastrVideoNames[iCount][mod( iExpCount, vidsPerGroup)+1];
		strTempVidName.resize (strTempVidName.count() - 4) ;
		strTempVidName = strTempVidName + ".avi";
#		term.print(strTempImgName); term.print("\n");# Debugging, outputting to terminal

####Tracker comment out####
#		tracker.send_command(cmd_et_bmp + "1024bg.bmp");### sending image name to eye tracker
####
		video_trial.present();
			term.print_line("bah");
		
		iCount = iCount + 1;
		
			fExpRes.print( (clock.time()-start_time) ); fExpRes.print("\t");
		##   
		vid.release();
		
  		loop
			ms.set_xy(0,0);
			ms.poll();
			int iCursor_x_initial = ms.x();
			int iCursor_x_current = ms.x();
			iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
		until 
			response_manager.last_response() == 1 && resp_count != (response_manager.response_data_count( ) - 1)
		begin
			ms.poll();
			iCursor_x_current = ms.x();
			iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
			if (iCursor_x_delta > 250) then 
				iCursor_x_delta = 250
			elseif (iCursor_x_delta < -250) then 
				iCursor_x_delta = -250
			end;
			slider_pic.set_part_x( 13, iCursor_x_delta );
			slider_pic.present();
		end;
#		term.print( (iCursor_x_delta+250)/5 ); term.print("\n");# Debugging, outputting to terminal
		##
		fExpRes.print( (iCursor_x_delta+250)/5 ); fExpRes.print("\t");
		fExpRes.print(aastrVideoNames[iCount-1][mod( iExpCount, vidsPerGroup)+1]); fExpRes.print("\n"); #iCount has already been incremented above, hense the (-1)
		##
		resp_count = response_manager.response_data_count( );
		###
			term.print( iCount ); term.print("\n");# Debugging, outputting to terminal
				system_keyboard.set_case_mode( 3 ); ##### Accept capital letter input
		system_keyboard.set_max_length( 1 );
		system_keyboard.set_log_keypresses(true);
		### Getting score for image
	
		
		
	end;
	###


##	EYE TRACKER CALIBRATION
#	sub
#		calibration 
#	begin
#		then tracker.calibrate( et_calibrate_default, 5.0, 0.0, 0.0 );
#		elseif (Caltype == 9)
#		then tracker.calibrate( et_calibrate_default, 9.0, 0.0, 0.0 );
#		elseif (Caltype == 13)
#		then tracker.calibrate( et_calibrate_default, 13.0, 0.0, 0.0 );
#		end;
#	end;
	###


##############################################################################################
####################################### MAIN ROUTINE #########################################	
##############################################################################################

file_name = get_response();
 
 
instruction_text.set_caption( "Welcome!\n\n Thank you for taking part in this experiment\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();

##eyetracking calibration
	instruction_text.set_caption( "Please put your head on the chin-rest\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();
	instruction_text.set_caption( "You're now going to see a small white square on the screen\n\n Please keep your eyes fixed on it\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();
   ###
   

####Tracker comment out####   
	###Eye tracker code

#	tracker.start_tracking(); 
		

	### more eye tracker code
#	string cal = "cal.bmp";
	#string idf_file_name = file_name; 	#naming the IDF file using the name of the test participant
	#idf_file_name.append ("_Trial_"+ string(iExpCount));
	#idf_file_name.append (".idf");
#	string idf = file_name + "_Trial_" + string(iExpCount) + ".idf";    

#	calibration();                           
#	tracker.send_command( "ET_CLR" );
#	tracker.set_recording( true );
	###
#	start_time = clock.time();




####################################
############# TRAINING #############
####################################


## loading training images
string inputFileName = "training\\training_list.txt";
loadvideos(inputFileName);

## instructions for training
if mod( iExpCount, vidsPerGroup) == 0 #skip some instructions if participant already did the experiment once

# if response_manager.last_response() != 2 
	then
		instruction_text.set_caption( " During this experiment you will be asked to evaluate" + 
												" the visual quality of a set of images \n\n\n\n\n Left-click to continue " );

		instruction_text.redraw(); instruction_trial.present();

#instruction_text.set_caption( "You will be scoring images using the mouse. \n The score range goes from 0 (Poor quality) to 10 (Excellent quality) \n with 5 being the middle of the range.\n\n Next you will see an example of the scoring screen. Try to insert a score of about 8. \n (Please note that extreme precision is not required) \n\n\n\n Left-click to continue " ); instruction_text.redraw(); instruction_trial.present();

instruction_text.set_caption( "You will presented with a set of impaired videos \n\n\n"+
										"For each video, we will ask you to indicate a visual quality score on a numerical scale.\n " +
										"The visual quality scale ranges from from 0 (poorest quality) to 10 (highest quality), \n" +
										"with 5 being the middle of the range.\n\n " +
										"The scoring scale will appear in a separate scoring screen after you have visualized the video \n"+
										"Next you will see an example of the scoring screen. Try to insert a score of about 8. \n " +
										"(Please note that extreme precision is not required) \n\n\n\n Left-click to continue " ); 

#instruction_text.redraw(); instruction_trial.present();

ms.set_xy(0,0);
ms.poll();
resp_count_pre = response_manager.response_data_count( );

#instruction_text.set_caption( "In a second moment, we will also ask you to comment on your scoring strategy. \n" +
#										"You will be asked to fill in a text box by indicating the set of critera that you used for scoring.\n\n " +
#										"You are not requested to thoroughly analyze your scoring strategy, \n" +
#										"but rather to shortly mention which elements you took into account when giving your score. \n\n" +
#										"The textbox will appear in a separate screen after you have scored the image \n" + 
#										"when you are ready to fill it in, please left click the mouse to make the text box appear. \n\n\n" +
#										"\n Left-click to continue " ); 

#instruction_text.redraw(); instruction_trial.present();

#### SLIDER DEMO

loop 
	int iCursor_x_initial = ms.x();
	int iCursor_x_current = ms.x();
	iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
#		term.print("iCursor_x_initial ="); term.print( iCursor_x_initial ); term.print("\n");# Debugging, outputting to terminal
#		term.print("iCursor_x_current ="); term.print( iCursor_x_current ); term.print("\n");# Debugging, outputting to terminal
#		term.print("iCursor_x_delta ="); term.print( iCursor_x_delta ); term.print("\n");# Debugging, outputting to terminal
	int resp_count = response_manager.response_data_count( );
	until 
	response_manager.last_response() == 1 && resp_count_pre != (response_manager.response_data_count( ))
	begin

		ms.poll();
		iCursor_x_current = ms.x();
		iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
#			term.print("iCursor_x_delta ="); term.print( iCursor_x_delta ); term.print("\n");# Debugging, outputting to terminal

		if (iCursor_x_delta > 250) then 
			iCursor_x_delta = 250
		elseif (iCursor_x_delta < -250) then 
			iCursor_x_delta = -250
		end;
		slider_pic.set_part_x( 13, iCursor_x_delta );
		slider_pic.present();
		resp_count = response_manager.response_data_count( );
	end;
#		term.print( (iCursor_x_delta+250)/5 ); term.print("\n");# Debugging, outputting to terminal
end;

#### END OF SLIDER DEMO


### Creating the experiment results output file

string file_name_txt = file_name;
file_name_txt.append ( "_Training_Trial_"+ string(iExpCount));
file_name_txt.append (".txt"); #this adds the text in brackets to the string (here "file_name_txt")
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tScore\tImage\n");
###

### Creating the comments output file

	file_name_txt = file_name;
	file_name_txt.append ( "_Training_comments_"+ string(iExpCount));
	file_name_txt.append (".txt"); #this adds the text in brackets to the string (here "file_name_txt")
	fExpComm.open( "output\\" + file_name_txt );
	

instruction_text.set_caption( "A short training session will begin now.\n"+ 
										"You will see a few videos with different contents,\n" + 
										"in a fashion similar to the videos you will be scoring during the experiment \n\n" + 
										"You will be allowed to practice with the slider for scoring\n" +
										"\n\n\n Left-click to continue " ); 

instruction_text.redraw(); instruction_trial.present();

### display training images
	loop # Displaying the bitmap images
		string strInput;
	   int resp_count = response_manager.response_data_count( );
		until iCount == aastrVideoNames.count()+1
		begin

		display_videos();
		end;

#term.print( masterCounter ); term.print("\n");# Debugging, outputting to terminal
#	term.print( iCount ); term.print("\n");# Debugging, outputting to terminal
#	term.print( iExpCount ); term.print("\n");# Debugging, outputting to terminal
#	term.print( imgsPerGroup ); term.print("\n");# Debugging, outputting to terminal

fExpRes.close();
fExpComm.close();

## END OF THE TRAINING ##
#########################

instruction_text.set_caption( "Now the experiment starts\n\nLook at the image and decide its score then left-click to go to the scoring screen\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();


#############################
###### REAL EXPERIMENT ######
#############################

loop 
	int masterCounter = 1;
#	term.print_line("imgspergroup: ");
#	term.print_line(imgsPerGroup);
	
until	(masterCounter > vidsPerGroup)
begin

if(masterCounter > 1)
then
	instruction_text.set_caption( "A new session is starting now. You can take a short break and restart whenever you feel ready\n\n\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();
	instruction_text.set_caption( "Look at the image and decide its score then left-click to go to the scoring screen\n\n\n\n\n\n\n\nLeft-click to continue" ); instruction_text.redraw(); instruction_trial.present();


end;
	term.print( masterCounter ); term.print("\n");# Debugging, outputting to terminal
	term.print( iExpCount ); term.print("\n");# Debugging, outputting to terminal
	term.print( vidsPerGroup ); term.print("\n");# Debugging, outputting to terminal
	#term.print( masterCounter ); term.print("\n");# Debugging, outputting to terminal
	inputFileName = "files\\file_list.txt";
	loadvideos(inputFileName);

	### Creating the experiment results output file
	file_name_txt = file_name;
	file_name_txt.append ( "_Trial_"+ string(iExpCount));
	file_name_txt.append (".txt"); #this adds the text in brackets to the string (here "file_name_txt")
	fExpRes.open( "output\\" + file_name_txt );
	fExpRes.print( "T-Start\tT-End\tScore\tImage\n");
	###

	### Creating the comments output file

	file_name_txt = file_name;
	file_name_txt.append ( "_comments_"+ string(iExpCount));
	file_name_txt.append (".txt"); #this adds the text in brackets to the string (here "file_name_txt")
	fExpComm.open( "output\\" + file_name_txt );
	
	
	### display training images
	display_videos();
	fExpRes.close();
	fExpComm.close();

	### Saving the new shuffled list of file names and another copy for future reference

	### SAVES THE BACKUP VERSION OF THE LIST
		if iExpCount >= 10
		then
			fNewVidLstBkp.open( "output\\ImageList_Trial_"+ string(iExpCount) + ".txt");
		else
			fNewVidLstBkp.open( "output\\ImageList_Trial_0"+ string(iExpCount) + ".txt");
		end;
		fNewVidLstBkp.print( iExpCount ); fNewVidLstBkp.print( "\n" );  
		loop # output what is in aastrImageNames
			int iGroup = 1;
			int iName = 1;
		until (aastrVideoNames.count( ) < iGroup )
		begin
			loop # output what is in aastrImageNames
			until (aastrVideoNames[iGroup].count( ) < iName ) #Chack if the last word is reached
			begin
				fNewVidLstBkp.print( aastrVideoNames [ iGroup ] [ iName ]  ); fNewVidLstBkp.print( "\n" );  
				iName = iName + 1;
			end;
			iGroup = iGroup + 1;
			iName = 1;
		end;
		fNewVidLstBkp.close();


	###Randomizing order of images in experiment
	aastrVideoNames.shuffle(); # shuffle order of groups

   if mod( iExpCount, vidsPerGroup) == (vidsPerGroup-1)
	then
		loop # shuffle names of files in group
			int iGroup = 1;
		until (aastrVideoNames.count( ) == iGroup )
		begin
			aastrVideoNames[iGroup].shuffle();
			iGroup = iGroup+1;
		end;
	end;
	###

	
	### saving the new list of file names
		output_file fNewVidLst = new output_file; 
		fNewVidLst.open( "files\\file_list.txt",true);
		fNewVidLst.print( iExpCount+1 ); fNewVidLst.print( "\n" );  
		loop # output what is in aastrImageNames
			int iGroup = 1;
			int iName = 1;
		until (aastrVideoNames.count( ) < iGroup )
		begin
			loop # output what is in aastrImageNames
			until (aastrVideoNames[iGroup].count( ) < iName ) #Chack if the last word is reached
			begin
				fNewVidLst.print( aastrVideoNames [ iGroup ] [ iName ]  ); fNewVidLst.print( "\n" );  
				iName = iName + 1;
			end;
			iGroup = iGroup + 1;
			iName = 1;
		end;
		fNewVidLst.close();
		masterCounter = masterCounter + 1;
end;


	

	term.print( iExpCount ); term.print("\n");# Debugging, outputting to terminal

###Tracker comment out####
#	tracker.set_recording( false ); 
#	tracker.send_command( cmd_et_sav + "roi_exp_01\\" + idf );
#	tracker.stop_tracking();

   ###Instructions
#	instruction_text.set_caption( " Is this the first time you are doing this experiment \n\n\n\n Yes >> Left-click \n No >> Right-click" ); instruction_text.redraw(); instruction_trial.present();

instruction_text.set_caption( " \n\n\n\nThis is the end of the experimnt\n\nThank you for participating!" ); instruction_text.redraw(); instruction_trial.present();

	
## END OF THE EXPERIMENT ##
###########################
	


/*		### recentering
		trial_recenter.present();	
		btmBitmap.set_filename( "training\\" + "caps_blur_(20).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
		### recentering
		trial_recenter.present();
		btmBitmap.set_filename( "training\\" + "Churchandcapitol_blur_(10).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "Sailing2_jpeg_(30).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();	
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "dancers_noise_(10).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "flowersonih_jpeg_(10).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "lighthouse2_noise_(30).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "monarch_noise_(20).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "Sailing1_blur_(30).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
#		### recentering
#		trial_recenter.present();
#		btmBitmap.set_filename( "training\\" + "Sailing4_jpeg_(20).bmp");	btmBitmap.load();	pic1.set_part( 1, btmBitmap );	trial1.present(); btmBitmap.unload();
	
*/

	

	
	


	###
