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

begin;


### Screen for 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;
} participant_name;
###

### Eye-tracker calibration screen:
picture {
	background_color = 127, 127, 127;
	box {
		height = 10; width = 10;
		color = 0, 0, 0; #check if we want the square another color
		}box1;
		x=0; y=0;
	} et_calibration;
###

### give pic to rego to center
trial {    
	trial_duration = 2000;
		picture{
		background_color = 127,127,127;
	}infra_video;
} trial_recenter;
###

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

###
picture {
   text {
	### font_size = 24;
      caption = "Please left-click your mouse to go to the scoring screen";
   };
   x = 0; y = 100;

} after_video_screen;

trial {

	trial_duration = stimuli_length;
	trial_type = fixed;
	
   video { filename = "blkblr_v1_1280x720_0.avi"; 
   speed_type = video_speed_hz;
   speed = 50;
   use_audio = false;
   } vid;
       
} video_trial;



### 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 = "Imperceptible";} textPoor;
		text { caption = "Very Strong";} textExcellent;
	} numbers;
	

trial
{
   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\nPlease rate the strength of the defects in the video"; font_size = 24;} exp_text;
		x = 0; y = 300;  
		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 = "Imperceptible"; } poor_text;
		x = -250; y = -50;
		text { caption = "Very Strong"; } excellent_text;
		x =  250; y = -50;
	} slider_pic;

} slider_trial;

box {height = 10 ; width = 10;}border_box;

array {
	LOOP $i 20; 
	text {caption = " "; background_color = 127, 127, 127 ;};
	ENDLOOP;
}menu_text;

text{caption = "mii"; font_size = 7; background_color = 255,255,255; }cursor;
text{caption = " "; font_size = 36; background_color = 127,127,127;}titletext;

trial{
	picture{
	background_color = 127,127,127;
	}pic;
}menu_screen;



###########
### PCL ###
###########

begin_pcl;

### global variables
array< string > VideoNames[0][0];
int iExpCount = 0;
int sessions = 1; ### set depending on how many sessions
string file_name;
string input_file_name;
int iCount = 1;
int max_x = display_device.width() / 2;
int min_x = -max_x;
int max_y = display_device.height() / 2;
int min_y = -max_y;
int bottom_max_y = 100; ### for restricting mouse to bottom half
int bottom_min_y = min_y + 40; ### for restricting mouse to bottom half
int start_time;
string instructions;


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

### files
input_file filevideoNames = new input_file;
output_file fExpRes = new output_file;
output_file newFileList = new output_file;

### Tracker Comment Out ###

#	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:\\packet_loss\\";                                                            
#	string cmd_et_sav = "ET_SAV " + "c:\\packet_loss\\";
#####


											###################
#################################### Subroutines ########################################
											###################

# 1. Utility subroutines
########################

##### Tracker Comment Out ###

####	EYE TRACKER CALIBRATION
#	sub
#		calibration 
#	begin
#		if (Caltype == 5)
#		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;
	###

### mouse activity
sub bool resp (int button, int current_count)
begin
	if response_manager.total_response_count(button) > current_count then
		return true
	end;
	return false;
end;

### 2. experiment-specific routines
#################################

### Records 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();
			participant_name.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; 
###

### shows videos
sub
	int showVideo(string filename)
	begin
		vid.set_filename(filename);
		
		vid.prepare();

		### recentering
		trial_recenter.present();

		fExpRes.print( (clock.time()-start_time) ); fExpRes.print("\t");
		
##### Tracker comment out ###

		#tracker.send_command(cmd_et_bmp + "ET_background.jpg");### sending image name to eye tracker

		video_trial.present();
		fExpRes.print( (clock.time()-start_time) ); fExpRes.print("\t");
		return 1;
	end;
	
### allows entering scores
sub 
	int score_video
		begin
		int resp_count = response_manager.response_data_count( );
	loop
		mse.set_xy(0,0);
		mse.poll();
		int iCursor_x_initial = mse.x();
		int iCursor_x_current = mse.x();
		iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
	until 
		resp(1,resp_count)
	begin
		mse.poll();
		iCursor_x_current = mse.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_trial.present();
	end;
		
		int score = (iCursor_x_delta+250)/5;
		return score;
	
	end;
###	

### Sets New Instruction Screen
sub
	instruction_screen(string instr)
	begin
		instruction_text.set_caption(instr); 
		instruction_text.redraw(); 
		instruction_trial.present();
	end;
###

### loads the video lists for the current task
sub
	loadvideos (string filename, int nSess)
	begin
	filevideoNames.open( filename );
	
	VideoNames.resize(nSess);
	
		loop 
			int iNameCount = 1;
			string strFileName = "";
		until filevideoNames.end_of_file() ### parse text file
		begin
			loop
				int j = 1;
			until j > nSess
			begin
				strFileName = filevideoNames.get_line();
				if strFileName != ""
				then
					VideoNames[j].resize( iNameCount );
					VideoNames[j] [iNameCount] = strFileName;
				end;
			j = j+1;
			end;
			iNameCount = iNameCount + 1;
		end;
		### imgsPerGroup = iNameCount;
		filevideoNames.close();	
		
		loop
		int j = 1;
		until j > nSess
		begin
			VideoNames[j].shuffle();
			j = j+1;
		end;

end;
	


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


file_name = get_response();

instructions = "Welcome! Thank you for participating in this experiment.\n\n" +
 " The experiment is divided in 5 sessions: \n \n" +
 " (1) equipment calibration, (2) free viewing, (3) training, \n" + 
 " (4) practice trials,  and (5) main experimental session. \n\n\n" +
 " At the beginning of each session, I will briefly  \n" + 
 " explain what you are required to do at each stage.\n" +
 " \n\n\n\nLeft-click to continue";
instruction_screen(instructions);


### eyetracking calibration
instructions = "The distance from the monitor to your eyes \n" +
"is very important during the presentation.\n\n" +
"Please put your head on the CHIN-REST in front of you. \n\n" +
"Try not to lean backward. \n" +
" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION\n\n\n" +
"Please, wait for a few seconds while I adjust the system.\n\n" +
"When the system is ready, I will ask you to continue.\n\n" +
" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION\n\n\n" +
 "You're now going to see a series of small black SQUARES on the screen.\n\n"+
 "At each screen, one black square will appear at a different position.\n\n" +
 "Please, keep your eyes fixed on each of these squares.\n"+
 "\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

### free looking of originals 

 ### Tracker comment out ###   
	
#	tracker.start_tracking(); 
	
	#string cal = "cal.bmp";
	#string idf = file_name + "S1.idf";    

	#calibration();                           
	#tracker.send_command( "ET_CLR" );
	#tracker.set_recording( true );

	start_time = clock.time();

#####
	
instructions = "(1) CALIBRATION SESSION\n\n\n" +
"Thank you!\n\n The calibration is now complete.\n\n"+
"\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(2) FREE VIEWING SESSION\n\n\n" +
				"You are now going to watch a series of 7 videos. \n\n"+
				"\nPlease, whatch them as if you were at home watching TV.\n\n"+
				"\n\n\n Whenever you are ready to start, please left-click to continue";
instruction_screen(instructions); 

### VIDEO FREE-LOOKING
######################

### timing output file 
string file_name_txt = file_name;
file_name_txt.append ( "_FL_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");

input_file_name = "originals\\originals_list.txt";
loadvideos(input_file_name, 1);

loop
int j = 1;
string vfn;
int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	j = j+1;
end;

fExpRes.close();


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

#Part 1: show videos with impairments, no scoring
instructions = "(3) TRAINING SESSION\n\n\n" +
					"This study is concerned with defects or impairments in video images and\n"+
					"their effect on human viewers.  We are not concerned with the content of\n"+
					"the videos.  We are interested in whether or not you see any defects or\n"+
					"impairments in the videos that we will show, and if so, how strong these defects are.\n"+
					"\n\n\n Left-click to continue";
instruction_screen(instructions);

instructions = "(3) TRAINING SESSION\n\n\n" +
					"Here is how you will determine the strength value. I am about\n" +
					"to show you a set of sample clips. The sample clips include \n" +
					"two sets of videos. The first set has 4 high quality videos.\n"+
					"\n\n\n Left-click to play the set of high quality videos.";
instruction_screen(instructions);

### Playing high quality videos
file_name_txt = file_name;
file_name_txt.append ( "_training_ORG_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");

input_file_name = "training\\training_ORG_list.txt";
loadvideos(input_file_name, 1);

loop
	int j = 1;
string vfn;
int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	j = j+1;
end;
fExpRes.close();
			
instructions = "(3) TRAINING SESSION\n\n\n" +
					"The second set has degraded videos. They will give you an\n"+
					"idea of the range of quality that you will be seeing.\n\n\n"+
					"You are to assign an STRENGTH value of 10 to defect types \n" +
					"as strong as the strongest defects seen among all the sample clips\n"+
					"\n\n\n Left-click to play the set of impaired videos.";
instruction_screen(instructions);

file_name_txt = file_name;
file_name_txt.append ( "_training_FL_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");

input_file_name = "training\\training_LQ_list.txt";
loadvideos(input_file_name, 1);

loop
	int j = 1;
string vfn;
int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];

	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	j = j+1;
end;
fExpRes.close();

instructions = "(3) TRAINING SESSION\n\n\n" +
					"Did you see any defects? \n\n"+
					"Remember that the strongest defect that you have seen\n"+ 
					"is to have a value of 10.\n\n"+
					"If the strength value of a defect in the experiment\n"+
					"is half of the worst sample clip, give it a 5; if it \n"+
					"is 1/10th as bad, give it a 1; \n" +
					"If you didn't see the defect at all, call it zero.\n\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

### Part 2:scoring training

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"Before we start the experiment, you will have 5 practice trials\n"+
					"to be sure that you understand the task. You will be asked to \n"+
					"perform the exact same tasks you will perform in the main experiment.\n"+
					"You will respond in these trials just like you will in the main experiment.\n"+
					"We will not use the data from the practice trials, so don't be concerned \n"+
					"if you make a mistake here.\n\n"+
					"If you have any questions or concerns, feel free to ask me.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"You will be presented with one video clip on each trial.\n"+
					"Each clip will last 10 seconds and will be played twice.\n"+ 
					"After the clip is played, questions will appear on the monitor.\n"+
					"The same questions will be asked after every trial.\n"+ 
					"Do not spend a lot of time thinking about your responses.\n"+
					"We want to know your initial impressions.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"You will be asked to estimate the strength of defects or\n"+
					"impairments in the video. The defects can be found in any \n"+
					"region of video and at any time during the clip.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"After you viewed the video, You will be asked to indicate the strength of \n"+
					"the defect you saw using a scale with values ranging from 0 and 10.\n\n"+
					"You are to assign a strength value of 10 to strongest defect.\n"+
					"If the strength of a defect in the experiment is half of the \n"+
					"worst sample clip, give it a 5; if it is 1/10th as bad, give it a 1.\n"+
					"If you did not perceive any defects, call it zero.\n\n"+ 
					"You should enter the scores using the mouse to LEFT-click on the desired value.\n\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);					

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"After you finished entering your choice of number,\n"+
					"Left-click to play the next video.\n\n"+
					"Do you have any questions?\n\n"+
					"\n\n\n Left-click to start PRACTICE TRIALS.";
instruction_screen(instructions);					
					
### not used anymore
### int main_menu_choice = menu(main_menu_options, "Did you perceive any impairments or defects in the video?");
### score_video();

file_name_txt = file_name;
file_name_txt.append ( "_training_scores.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\tStrength Score\n");

input_file_name = "training\\training_list.txt";
loadvideos(input_file_name, 1);

loop
	int j = 1;
string vfn;
int nVid = VideoNames[1].count();
int t_score;
until j > nVid
begin
	### current video
	vfn = VideoNames[1][j];
	
	### show video twice
	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	showVideo(vfn);
	
	### get score for video
	t_score = score_video();
	fExpRes.print( vfn ); 
	fExpRes.print("\t");
	fExpRes.print(t_score);
	fExpRes.print("\n");
	
	j = j+1;
end;

fExpRes.close();


#####################
## MAIN EXPERIMENT ##
#####################

instructions = "(5) MAIN EXPERIMENT SESSION \n\n\n" +
					"The experiment is divided in 2 parts. Each part should\n"+
					"last approximately 12 minutes. A break will be given after\n"+
					"the first part of the experiment is finished.\n\n"+
					"If you need to take a small break at any time, enter\n"+ 
					"your answers for the most recent video, but wait to Left-click\n"+
					"until you are ready to go on.  You should stop if you are confused \n"+
					"about what to do. If you realize you have entered data incorrectly,\n"+
					"tell me and I will go back and fix it later.\n\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(5) MAIN EXPERIMENT SESSION  -- PART I \n\n\n" +
					"Do you have any questions?\n\n"+
					"\n\n\n Left-click to start PART I.";
instruction_screen(instructions);

### instructions = "Now the experiment starts\n\nHalfway your task you will be allowed to take a small break and rest your eyes\n\n\n\nLeft-click to continue";
### instruction_screen(instructions);

file_name_txt = file_name;
file_name_txt.append ( "_S1.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\t\Strength Score\n");

input_file_name = "files\\video_listvvv.txt";
loadvideos(input_file_name, 2);

loop
	int j = 1;
string vfn;
int nVid = VideoNames[1].count();
int t_score;
until j > nVid
begin

	### current video
	vfn = VideoNames[1][j];
	

	### show video twice
	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	showVideo(vfn);
	
	### get score for video
	t_score = score_video();
	fExpRes.print( vfn ); 
	fExpRes.print("\t");
	fExpRes.print(t_score);
	fExpRes.print("\n");
	
	j = j+1;
	
end;

fExpRes.close();

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

instructions = "\n\n\nThis is the end of the first part of the experiment.\n"+
					"\n\n\nPlease Left-click whenever you are ready to resume the experiment. \n";
					instruction_screen(instructions);

file_name_txt = file_name;
file_name_txt.append ( "_S2.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\tStrength Score\n");

################
# Part 2
#################

instructions = "(1) CALIBRATION SESSION -- PART II\n\n\n" +
					"You are now going to perform the calibration session again.\n"+
					"Please, wait for a few seconds while the experimenter adjusts the system.\n\n" +
					"When the system is ready, the experimenter will ask you to continue.\n\n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION  -- PART II \n\n\n" +
 "You're now going to see a series of small black SQUARES on the screen.\n\n"+
 "At each screen, one black square will appear at a different position.\n\n" +
 "Please, keep your eyes fixated on each of these squares.\n"+
 "\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

### instructions = "(1) CALIBRATION FOR PART II \n\n\n" +
###					"You are now going to perform the calibration session again.
### \n\n Please keep your eyes fixed on the small black square\n\n\n\nLeft-click to continue";
### instruction_screen(instructions); 


#### Tracker comment out####   
	
	#tracker.start_tracking(); 

	#cal = "cal.bmp";
	#idf = file_name + "S2.idf";    

	#calibration();                           
	#tracker.send_command( "ET_CLR" );
	#tracker.set_recording( true );

 start_time = clock.time();

 ###

instructions = "Thank you!\n\n The calibration is now complete.\n\n"+
					"Left-click to start the Part II of the experiment."+
					"\n\n\n\n Left-click to continue";
instruction_screen(instructions); 

loop
	int j = 1;
string vfn;
int nVid = VideoNames[2].count();
int t_score;
until j > nVid
begin
	
	### current video
	vfn = VideoNames[2][j];
	

	### show video twice
	showVideo(vfn);
	fExpRes.print( vfn ); fExpRes.print("\n");
	showVideo(vfn);
	
	### get score for video
	t_score = score_video();
	fExpRes.print( vfn ); 
	fExpRes.print("\t");
	fExpRes.print(t_score);
	fExpRes.print("\n");
	
	j = j+1;

end;

newFileList.open("files\\video_listvvv.txt");

loop
int i = 1;
int ll = VideoNames[1].count();
string vfn;
until i >ll
begin
	vfn = VideoNames[1][i];
	term.print_line(vfn);
	newFileList.print(vfn);
	newFileList.print("\n");
	i = i+1;
	end;
	
loop
int i = 1;
int ll = VideoNames[2].count();
string vfn;
until i > ll
begin
	vfn = VideoNames[2][i];
	newFileList.print(vfn);
	newFileList.print("\n");
	i = i+1;
end;


fExpRes.close();
newFileList.close();

instructions = "This is the end of the experiment.\n\n\n\n\nThank you for participating!";
instruction_screen(instructions);

 ### Tracker comment out####

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

 ###