# Experienced Touch task

active_buttons = 2;
default_font = "Arial";
event_code_delimiter = ";";
stimulus_properties = Trial, string, Rating, string, INT, number;
write_codes = true;
response_port_output = false;

begin;


picture { bitmap { filename = "instructions.jpg"; height = 1080; width = 1920; } instruction ;	x = 0; y = 0; } instruction_pic ; 

picture { text {caption = "+";	font_size = 60; font_color= 255,255,255; } fixation ;	x = 0; y = 0;	} default;

sound { wavefile { filename = "3cm.wav" ; description = "10";} ; } slow ;
sound { wavefile { filename = "30cm.wav" ; description = "20";} ; } fast; 
sound { wavefile { filename = "tone.wav"; } ; } tone;

box { height = 80; width = 20; 	color = 255, 0, 0; } cursor;
box { height = 20; width = 1344; color = 255, 255, 255;	} scale;

text { caption = "Hur BEHAGLIG knns stimulering?";  font_size = 40; font_color = 255, 255, 255; } PLEtitle;
text { caption = "Hur INTENSIV knns stimulering?"; font_size = 40; font_color = 255, 255, 255; } INTtitle;
text { caption = "Obehaglig"; font_size = 32; font_color = 255, 255, 255; } NOTcapP;
text { caption = "Behaglig";font_size = 32; font_color = 255, 255, 255; } VERYcapP;
text { caption = "Inte intensiv"; font_size = 32; font_color = 255, 255, 255; } NOTcapI;
text { caption = "Intensiv"; font_size = 32; font_color = 255, 255, 255; } VERYcapI;

picture {
	box cursor; x = 0; y = 0; on_top = true;
	box scale; x = 0; y = 0; on_top = false;
	text INTtitle; x = 0; y = 300;
	text NOTcapI; x = -700; y = -100;
	text VERYcapI; x = 700;  y = -100;
	} INTrating_pic;

picture {
	box cursor; x = 0;  y = 0; on_top = true;
	box scale; x = 0;  y = 0; on_top = false;
	text PLEtitle; x = 0; y = 300;
	text NOTcapP; x = -700; y = -100;
	text VERYcapP;	x = 700;  y = -100;
	} PLErating_pic;
	
trial { picture default; duration = 500; } intro;

#instruction screen; need to press enter to start
trial {											
	trial_duration = forever;
	trial_type = specific_response;
	terminator_button = 2;
	stimulus_event {	
		picture instruction_pic;
		port_code = 0;
		} instruction_event ;
	} instruction_trial;
	
	

trial {
	stimulus_event {
	sound slow;
	time = 0;
		} slow_event;
	} slow_trial;

trial {
	stimulus_event {
	sound fast;
	time = 0;
		} fast_event;
	} fast_trial;

trial { 
	trial_duration = 5000;
	stimulus_event {
	picture default;
	 } rest_event;
	} rest_trial;

#Nothing trial to record Int/Ple ratings
	trial { 
		stimulus_event { 
			nothing {} ; 
			code = "";
		} my_event;
	} my_trial;

#end trial
	picture { text {caption = "Tack!"; font_size = 52; font_color = 255,255,255; } end ;x = 0; y = 0;} end_pic;

	trial {
		trial_duration = 5000;
		stimulus_event {
		picture end_pic;
		} end_event;
	} end_trial;



#--------------------------------------------------------------------------------------------------------------#


begin_pcl;

mouse mse = response_manager.get_mouse( 1 );

array<trial> brushing_array[0];
brushing_array.add ( fast_trial );
brushing_array.add ( slow_trial );
brushing_array.add ( slow_trial );
brushing_array.add ( fast_trial );
brushing_array.add ( fast_trial );
brushing_array.add ( fast_trial );
brushing_array.add ( slow_trial );
brushing_array.add ( slow_trial );

brushing_array.shuffle();

#create array/randomize for block 1
array <int> brush1[brushing_array.count()];			
brush1.fill ( 1, 0, 1, 1 );
brush1.shuffle();

#creat array/randomize for block 2
array <int> brush2[brushing_array.count()];			
brush2.fill ( 1, 0, 1, 1 );
brush2.shuffle();

#creat array/randomize for block 3
array <int> brush3[brushing_array.count()];			
brush3.fill ( 1, 0, 1, 1 );
brush3.shuffle();

#creat array/randomize for block 4
array <int> brush4[brushing_array.count()];			
brush4.fill ( 1, 0, 1, 1 );
brush4.shuffle();


#array to determine rating (PLE or INT)
array<int> rating[] = { 1, 2 };
rating.shuffle();


#-----Intensity rating subroutine-----#
sub 
	int INT_rating 
begin
	int int_sel = 0;
	
	mse = response_manager.get_mouse( 1 );
	int max_x = 672;	int min_x = -max_x;
	int max_y = 0;	int min_y = -max_y;
	mse.set_min_max( 1, min_x, max_x );
	mse.set_min_max( 2, min_y, max_y );
	mse.set_restricted( 1, true );
	mse.set_restricted( 2, true );
	mse.poll();
	mse.set_xy ( 0, 0 );

	loop int count = response_manager.total_response_count ( 1 )	
	until response_manager.total_response_count ( 1 ) > count begin
		mse.poll();
		INTrating_pic.set_part_x (1, mse.x());
		INTrating_pic.set_part_y (1, mse.y());
		
		if ( mse.x() < -608 ) && ( mse.x() > -672 ) then int_sel = -10; end;
		if ( mse.x() < -544 ) && ( mse.x() > -608 ) then int_sel = -9;  end;
		if ( mse.x() < -480 ) && ( mse.x() > -544 ) then int_sel = -8;  end;
		if ( mse.x() < -416 ) && ( mse.x() > -480 ) then int_sel = -7;  end;
		if ( mse.x() < -352 ) && ( mse.x() > -416 ) then int_sel = -6;  end;
		if ( mse.x() < -288 ) && ( mse.x() > -352 ) then int_sel = -5;  end;
		if ( mse.x() < -224 ) && ( mse.x() > -288 ) then int_sel = -4;  end;
		if ( mse.x() < -160 ) && ( mse.x() > -224 ) then int_sel = -3;  end;
		if ( mse.x() < -96  ) && ( mse.x() > -160 ) then int_sel = -2;  end;
		if ( mse.x() < -32  ) && ( mse.x() > -96  ) then int_sel = -1;  end;
		if ( mse.x() <  32  ) && ( mse.x() > -32  ) then int_sel = 0;   end;
		if ( mse.x() <  96  ) && ( mse.x() > 32   ) then int_sel = 1;   end;
		if ( mse.x() <  160 ) && ( mse.x() > 96   ) then int_sel = 2;   end;		
		if ( mse.x() <  224 ) && ( mse.x() > 160  ) then int_sel = 3;   end;		
		if ( mse.x() <  288 ) && ( mse.x() > 224  ) then int_sel = 4;   end;
		if ( mse.x() <  352 ) && ( mse.x() > 288  ) then int_sel = 5;   end;
		if ( mse.x() <  416 ) && ( mse.x() > 352  ) then int_sel = 6;   end;
		if ( mse.x() <  480 ) && ( mse.x() > 416  ) then int_sel = 7;   end;
		if ( mse.x() <  544 ) && ( mse.x() > 480  ) then int_sel = 8;   end;
		if ( mse.x() <  608 ) && ( mse.x() > 544  ) then int_sel = 9;   end;
		if ( mse.x() <  672 ) && ( mse.x() > 608  ) then int_sel = 10;  end;
	
	INTrating_pic.present();
	end;
	rest_trial.set_duration ( 500 );
	rest_trial.present();
	return int_sel
	
end;

#-----Pleasantness rating subroutine-----#
sub 
	int PLE_rating 
begin
	int ple_sel = 0;
	
	mse = response_manager.get_mouse( 1 );
	int max_x = 672;	int min_x = -max_x;
	int max_y = 0;	int min_y = -max_y;
	mse.set_min_max( 1, min_x, max_x );
	mse.set_min_max( 2, min_y, max_y );
	mse.set_restricted( 1, true );
	mse.set_restricted( 2, true );
	mse.poll();
	mse.set_xy ( 0, 0 );

	loop int count = response_manager.total_response_count ( 1 )	
	until response_manager.total_response_count ( 1 ) > count begin
		mse.poll();
		PLErating_pic.set_part_x (1, mse.x());
		PLErating_pic.set_part_y (1, mse.y());

		if ( mse.x() < -608 ) && ( mse.x() > -672 ) then ple_sel = -10; end;
		if ( mse.x() < -544 ) && ( mse.x() > -608 ) then ple_sel = -9;  end;
		if ( mse.x() < -480 ) && ( mse.x() > -544 ) then ple_sel = -8;  end;
		if ( mse.x() < -416 ) && ( mse.x() > -480 ) then ple_sel = -7;  end;
		if ( mse.x() < -352 ) && ( mse.x() > -416 ) then ple_sel = -6;  end;
		if ( mse.x() < -288 ) && ( mse.x() > -352 ) then ple_sel = -5;  end;
		if ( mse.x() < -224 ) && ( mse.x() > -288 ) then ple_sel = -4;  end;
		if ( mse.x() < -160 ) && ( mse.x() > -224 ) then ple_sel = -3;  end;
		if ( mse.x() < -96  ) && ( mse.x() > -160 ) then ple_sel = -2;  end;
		if ( mse.x() < -32  ) && ( mse.x() > -96  ) then ple_sel = -1;  end;
		if ( mse.x() <  32  ) && ( mse.x() > -32  ) then ple_sel = 0;   end;
		if ( mse.x() <  96  ) && ( mse.x() > 32   ) then ple_sel = 1;   end;
		if ( mse.x() <  160 ) && ( mse.x() > 96   ) then ple_sel = 2;   end;		
		if ( mse.x() <  224 ) && ( mse.x() > 160  ) then ple_sel = 3;   end;		
		if ( mse.x() <  288 ) && ( mse.x() > 224  ) then ple_sel = 4;   end;
		if ( mse.x() <  352 ) && ( mse.x() > 288  ) then ple_sel = 5;   end;
		if ( mse.x() <  416 ) && ( mse.x() > 352  ) then ple_sel = 6;   end;
		if ( mse.x() <  480 ) && ( mse.x() > 416  ) then ple_sel = 7;   end;
		if ( mse.x() <  544 ) && ( mse.x() > 480  ) then ple_sel = 8;   end;
		if ( mse.x() <  608 ) && ( mse.x() > 544  ) then ple_sel = 9;   end;
		if ( mse.x() <  672 ) && ( mse.x() > 608  ) then ple_sel = 10;  end;
	
	PLErating_pic.present();
	end;
	rest_trial.set_duration ( 500 );
	rest_trial.present();
	return ple_sel
end;



#----------BRUSHING BLOCK 1----------#
sub
	brushing_1
begin
	#block starts with variable rest trial (4.5 to 5.5 sec)
	rest_trial.set_duration ( random (  4500, 5500 ) );
	rest_trial.present();

#counters to track how many of each trial type (needed to determine which rating to display)
int fast_count = 0;
int slow_count = 0;	

	loop 	int i = 1	until 	i > brush1.count ()	
	begin
	#fixation (2-4 sec of rest before each brush trial) 
	rest_trial.set_duration ( random ( 2000, 4000 ) ) ;
	rest_trial.present () ;
	
	#main trial
	int this_trial = brush1[i];
			
		#sets port codes and counts the number of fast trials (need fast_count variable to determine which rating will be displayed)
		if 	brushing_array[this_trial] == fast_trial
		then	fast_event.set_port_code ( 20 );
			fast_event.set_code_width ( 9000 );
			fast_count = fast_count + 1;
		end;
		
		#sets port codes and counts the nubmer of slow trials
		if 	brushing_array[this_trial] == slow_trial
		then	slow_event.set_port_code ( 10 );
			slow_event.set_code_width ( 9000 );
			slow_count = slow_count + 1;
		end;		
		
	#presents brushing trial (audio clip)
	brushing_array[this_trial].present();
	
	#brief presentation of the fixation cross/rest prior to ratings (0.5 sec)
	rest_trial.set_duration ( 500 );
	rest_trial.present();
		
	#determine which rating to present and record rating
	if brushing_array[this_trial] == fast_trial	then	
		#if it is one of the first two fast trials
		if fast_count < 3	then
			#AND if the array "rating" is 2 at index 1 (this array only has 2 variables, 1 and 2, which are randomized in each block)
			#then the pleasantness rating will be shown
			if rating[1] == 2	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 
					#otherwise, if "rating" is 1 at index 1, then the intensity rating will be shown
						else	int INT_selection = INT_rating();
							my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
						end;
		#otherwise, if it is the 3rd or 4th fast trial (i.e. fast_count is NOT less than 3)
		#AND if the array "rating" is 1 at index 1, then the pleasantness rating will be shown								
		else	if rating[1] == 1	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 
					#otherwise, if "rating" is 2 at index 1, the intensity rating will be shown		
						else	int INT_selection = INT_rating();
							my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
						end;
	end;
	end;
	if brushing_array[this_trial] == slow_trial	then
	if slow_count < 3	then
	if rating[1] == 1		then	int PLE_selection = PLE_rating();
						my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
					else	int INT_selection = INT_rating();
						my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
					end;
	else	if rating[1] == 2	then	int PLE_selection = PLE_rating();
						my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
					else	int INT_selection = INT_rating();
						my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
				end;
		end;
			
	end;
	
	#brief presentation of the fixation cross/rest prior to ratings (0.5 sec)
	rest_trial.present();
		
	#"nothing trial" used to record ratings and trial type
	my_trial.present();

	i = i + 1;
	end;
end;


#----------BRUSHING BLOCK 2----------#
sub
	brushing_2
begin
	rating.shuffle();	
	
	rest_trial.set_duration ( random (  4500, 5500 ) );
	rest_event.set_port_code ( 0 );
	rest_trial.present();

int fast_count = 0;
int slow_count = 0;	

	loop 	int i = 1	until 	i > brush2.count ()	
	begin
	rest_trial.set_duration ( random ( 2000, 4000 ) ) ;
	rest_trial.present () ;

	int this_trial = brush2[i];

		if 	brushing_array[this_trial] == fast_trial
		then	fast_event.set_port_code ( 20 );
			fast_event.set_code_width ( 9000 );
			fast_count = fast_count + 1;
		end;
			
		if 	brushing_array[this_trial] == slow_trial
		then	slow_event.set_port_code ( 10 );
			slow_event.set_code_width ( 9000 );
			slow_count = slow_count + 1;
		end;		
		
	brushing_array[this_trial].present();
	
	rest_trial.set_duration ( 500 );
	rest_trial.present();
	
	if brushing_array[this_trial] == fast_trial	then	
		if fast_count < 3	then
				if rating[1] == 2	then	int PLE_selection = PLE_rating();
								 my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;					
		else		if rating[1] == 1	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 	
							else	int INT_selection = INT_rating();
							my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
				end;
		end;
	
		if brushing_array[this_trial] == slow_trial	then
			if slow_count < 3	then
			if rating[1] == 1		then	int PLE_selection = PLE_rating();
								my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
			else	if rating[1] == 2	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
			end;
		end;
		
		rest_trial.present();

		my_trial.present();

	i = i + 1;
	end;

end;

#----------BRUSHING BLOCK 3----------#
sub
	brushing_3
begin
	rating.shuffle();	
	
	rest_trial.set_duration ( random (  4500, 5500 ) );
	rest_trial.present();

int fast_count = 0;
int slow_count = 0;	

	loop 	int i = 1	until 	i > brush3.count ()	
	begin
	rest_trial.set_duration ( random ( 2000, 4000 ) ) ;
	rest_trial.present () ;

	int this_trial = brush3[i];

		if 	brushing_array[this_trial] == fast_trial
		then	fast_event.set_port_code ( 20 );
			fast_event.set_code_width ( 9000 );
			fast_count = fast_count + 1;
		end;
		
		if 	brushing_array[this_trial] == slow_trial
		then	slow_event.set_port_code ( 10 );
			slow_event.set_code_width ( 9000 );
			slow_count = slow_count + 1;
		end;		
		
		brushing_array[this_trial].present();
		
		rest_trial.set_duration ( 500 );
		rest_trial.present();
		
		if brushing_array[this_trial] == fast_trial	then	
			if fast_count < 3	then
				if rating[1] == 1	then	int PLE_selection = PLE_rating();
								 my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 
							else	int INT_selection = INT_rating();
								 my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;					
			else	if rating[1] == 2	then	int PLE_selection = PLE_rating();
								my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 	
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
			end;
		end;
	
		if brushing_array[this_trial] == slow_trial	then
			if slow_count < 3	then
				if rating[1] == 2	then	int PLE_selection = PLE_rating();
								my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
				else	if rating[1] == 1	then	int PLE_selection = PLE_rating();
									my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
								else	int INT_selection = INT_rating();
									my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
			end;
		end;
		
		rest_trial.present();

		my_trial.present();

	i = i + 1;
	end;

end;


#----------BRUSHING BLOCK 4----------#
sub
	brushing_4
begin
	rating.shuffle();	
	
	rest_trial.set_duration ( random (  4500, 5500 ) );
	rest_trial.present();

int fast_count = 0;
int slow_count = 0;	

	loop 	int i = 1	until 	i > brush3.count ()	
	begin
	rest_trial.set_duration ( random ( 2000, 4000 ) ) ;
	rest_trial.present () ;

	int this_trial = brush4[i];

		if 	brushing_array[this_trial] == fast_trial
		then	fast_event.set_port_code ( 20 );
			fast_event.set_code_width ( 9000 );
			fast_count = fast_count + 1;
		end;
		
		if 	brushing_array[this_trial] == slow_trial
		then	slow_event.set_port_code ( 10 );
			slow_event.set_code_width ( 9000 );
			slow_count = slow_count + 1;
		end;		
		
	brushing_array[this_trial].present();
		
	rest_trial.set_duration ( 500 );
	rest_trial.present();
		
	if brushing_array[this_trial] == fast_trial	then	
		if fast_count < 3	then
		if rating[1] == 1	then	int PLE_selection = PLE_rating();
						 my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 
					else	int INT_selection = INT_rating();
						 my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
					end;					
		else	if rating[1] == 2	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Fast" + "; " + "Pleasant " + ";" + string (PLE_selection) ); 	
						else	int INT_selection = INT_rating();
							my_event.set_event_code ( "Fast" + "; " + "Intense" + ";" + string (INT_selection) ); 
						end;
		end;
	end;
	
	if brushing_array[this_trial] == slow_trial	then
		if slow_count < 3	then
			if rating[1] == 2	then	int PLE_selection = PLE_rating();
							my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
						else	int INT_selection = INT_rating();
							my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
												end;
			else	if rating[1] == 1	then	int PLE_selection = PLE_rating();
								my_event.set_event_code ( "Slow" + "; " + "Pleasant " + ";" + string (PLE_selection) );
							else	int INT_selection = INT_rating();
								my_event.set_event_code ( "Slow" + "; " + "Intense" + ";" + string (INT_selection) ); 
							end;
			end;
		end;
		
		rest_trial.present();

	my_trial.present();

	i = i + 1;
	end;

end;



#---------------------------Run Trials---------------------------#

#instruction screen
instruction_event.set_port_code ( 0 );
instruction_trial.present();	

#block 1
brushing_1();

#indicates end of block 1
tone.present();

#post-block rest
rest_trial.set_duration ( random ( 6000, 8000 ) );
rest_event.set_port_code (0);
rest_trial.present();

#block 2
brushing_2();

#indicates end of block 2
tone.present();
tone.present();

#post-block rest
rest_trial.set_duration ( random ( 6000, 8000 ) );
rest_event.set_port_code (0);
rest_trial.present();

#block 3
brushing_3();

#indicates end of block 3
tone.present();
tone.present();
tone.present();

#post-block rest
rest_trial.set_duration ( random ( 6000, 8000 ) );
rest_event.set_port_code (0);
rest_trial.present();

#block 4
brushing_4();

#post-block rest
rest_trial.set_duration ( random ( 1500, 2500 ) );
rest_event.set_port_code (0);
rest_trial.present();


#Thank you screen
end_event.set_port_code ( 1 );
end_event.set_code_width ( 100 );
end_trial.present();

