Creative Commons License R.Muralikrishnan, MPI for Empirical Aesthetics. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Licence.
##############################################################################
##                                                                          ##
##  Read-Aloud Task: Stress and Pause Determination during Natural Reading  ##
##             Experiment Script for use with Presentation                  ##
##           Experimenters: Christine Knoop and Marissa Gemma               ##
##                       Author: R. Muralikrishnan                          ##
##                                                                          ##
##############################################################################

##############################################################################
# Scenario Description Language (SDL) Header Part
##############################################################################

active_buttons = 9;      # There are 9 active buttons defined in 'Settings -> Response'
button_codes = 1,2,3,4,5,6,7,8,9;    # These are the codes of those buttons

response_matching = simple_matching;  # Enables features not in legacy_matching

# Only those button presses that one expects are logged; not all
response_logging = log_active;

# Needed for EEG Trigger files
#write_codes = true;
#pulse_width = 5;
#default_output_port = 1;  # This is most probably going to be the same for everyone.
# This is the LPT Parallel port that you will define in the Port settings tab of
# Presentation.  This will be used as the output port for sending codes to EEG.
# Defining this here saves getting a port number in the PCL part from the output_manager
# and calling send_code etc.  Stimulus events themselves will send these for us, provided we
# define the correct port_codes in each Stimulus event.

# Default Settings for Font, FG and BG Colours etc.
default_background_color = "232, 240, 247"; # RGB codes in decimal; #000000 => Black
default_font = "Verdana";
default_font_size = 34;
default_text_color = "000, 102, 153"; # "000, 102, 153"; # 006699 A pleasant bluish colour
default_text_align = align_center;

default_deltat = 0;
default_picture_duration = next_picture; #/* This implies that all pictures ...
#/...are shown until the next picture is shown, unless otherwise specified */

#default_all_responses = false;

##############################################################################
# Scenario Description Language (SDL) Part
##############################################################################

begin;

# ============================
# SDL Variable Declarations: -
# ============================

#
# We don't use any variables.  All the timer parameters appear as numbers.

# ==============================
# Picture Stimuli Definitions: - Everything that is shown, including text!
# ==============================

# Screen definition for the trial that starts an Experimental Session.
# Used as the first screen that the participant sees.  Operator Controlled.

line_graphic {
   coordinates = -600, -72, -600, 71;
   coordinates = 600, -72, 600, 71;
   coordinates = -600, -70, 600, -70;
   coordinates = -600, 70, 600, 70;
   line_width = 4;
   line_color = 255, 255, 255;
} Lin_Border;

picture
{
    text { caption = "Welcome!  We will begin shortly."; };
    x = 0;
    y = 0;

    line_graphic Lin_Border;
    x = 0;
    y = 0;
} P_Start_Exp;

# Screen definition for the Pause trial
# Operator Controlled!
picture
{   text { caption = "Let us continue after a short break!"; };
    x = 0;
    y = 0;

    line_graphic Lin_Border;
    x = 0;
    y = 0;
} P_Pause;

# Screen definition for the Continue trial
# Participant Controlled!
picture
{
    text { caption = "Press a button to continue."; };
    x = 0;
    y = 0;

    line_graphic Lin_Border;
    x = 0;
    y = 0;
} P_Continue;

# Screen definition for showing the focus star
picture
{
   # bitmap { filename = "Focus_Star_new1.bmp"; };     # Use Focus_Star.bmp
   text { caption = " "; };
   x = 0;
   y = 0;
} P_Focus_Star;

# Screen definition for the End_Thanks trial
picture
{
    text {caption = "We're done. Thank you!";};
    x = 0;
    y = 0;

    line_graphic Lin_Border;
    x = 0;
    y = 0;
} P_End_Thanks;


# Screen definition for showing a message
picture
{

	bitmap { filename = "Open-Book-N.png"; scale_factor = 0.93;};
	x = 0;
	y = 0;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Header rule, left
	x = -480;
	y = 490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Header rule, right
	x = 480;
	y = 490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Footer rule, left
	x = -480;
	y = -490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Footer rule, right
	x = 480;
	y = -490;

   text { caption = " "; font = "Bookman Old Style"; font_size = 27; font_color = "63,63,63";
          background_color = "255,255,255"; transparent_color = "255,255,255";
          max_text_width = 655; width = 700; height = 1000; formatted_text = true;}Txt_Message;
   x = 480;
   y = 0;


} P_Message;

####  Specific for presenting Visual Stimuli   --------------------------------
# Screen definition for the trial that presents Visual Stimuli (PDF page images).
picture
{

	bitmap { filename = "Open-Book-N.png"; scale_factor = 0.93;};
	x = 0;
	y = 0;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Header rule, left
	x = -480;
	y = 490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Header rule, right
	x = 480;
	y = 490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Footer rule, left
	x = -480;
	y = -490;

	box { width = 655; height = 3; color = 210, 210, 210; }; # Footer rule, right
	x = 480;
	y = -490;

	bitmap { filename = "Blank-Page.png"; transparent_color = "255,255,255";} Bmp_Visual_Stimulus_L;
	x = -480;
	y = 0;

	bitmap { filename = "Blank-Page.png"; transparent_color = "255,255,255";} Bmp_Visual_Stimulus_R;
	x = 480;
	y = 0;

   # The white background on the text page image won't be drawn, since we've defined white colour to be transparent in the BMP definitions above.

   text { caption = " "; font = "Verdana"; font_size = 11; font_color = "0,0,0"; formatted_text = true;
          background_color = "255,255,255"; transparent_color = "255,255,255";
          text_align = align_center;} Txt_Footer_L;
   x = -480;
   y = -515;


   text { caption = " "; font = "Verdana"; font_size = 11; font_color = "0,0,0"; formatted_text = true;
          background_color = "255,255,255"; transparent_color = "255,255,255";
          text_align = align_center;} Txt_Footer_R;
   x = 480;
   y = -515;

} P_Visual_Stimulus;

####

#=====================
# Trial Definitions: -
#=====================

/****
/#  Note that the name 'Trial' could be misleading.  Unless otherwise specified,
/#  what we mean by a 'Trial' here is actually a sub-trial or sub-task that is
/#  part - and thereby defines the structure - of an actual experimental trial.
****/

/****
/# Template for defining an SDL Trial.
/# trial
/# {
/#      Trial-related parameters such as trial_duration, trial_type etc.
/#     Trial-related parameters
/#
/#     Stimulus event 1 such as picture, sound, video, nothing or force-FB;
/#     Stimulus event parameters such as time, event code etc.
/#     If the parameters aren't specified, the default values are used!!!
/#
/#     Stimulus event 2;
/#     its parameters...and so on
/# }
****/

# Screen definition of the Trial to show a Blank Screen between Experimental Trials
trial
{
    trial_duration = 300;
    all_responses = false;

    stimulus_event
    {
        #picture {};     # This is the default picture, which is nothing!
        picture P_Message;        # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "CLS";   # Comment this out to avoid seeing this in the logfile.
    };
} T_Blank_Screen;

# Definition of the Trial to start an Experimental Session.
# Operator Controlled!
trial
{
    trial_duration = forever;       # Keep running the trial...
    trial_type = specific_response; # ...until the following specific response.
    terminator_button = 9;          # Operator PC - ENTER key.

    stimulus_event
    {
        picture P_Message;        # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "The Experiment starts now!";
    };
} T_Start_Exp;

# Definition of the Trial to continue further.
# Participant Controlled!
trial
{
    trial_duration = forever;        # Keep running the trial...
    trial_type = specific_response;  # ...until the following specific response.
    terminator_button = 8;         # Participant Joystick - L or R button.

    stimulus_event
    {
        picture P_Message;        # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "Trials follow!";
    };
} T_Continue;

# Definition of the Trial to Launch an Experimental Trial
trial
{
    trial_duration = 400;       # Run the trial for 300 ms...
    all_responses = false;      # ..without recognising any key presses.

    stimulus_event
    {
        picture P_Message;      # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "";              # Code set below in the PCL program.
        # port_code = ;         # Port code set by the PCL program.
    } E_Launch_New_Trial;
} T_Launch_New_Trial;


# Definition of the Trial to keep showing the Focus Star for fixation.
trial
{
    trial_duration = 1000;      # Show star until the next clear-screen...
    all_responses = false;      # ..without recognising any key presses.
    stimulus_event
    {
        picture P_Focus_Star;   # Show the focus star for fixation.
        code = "*";             # Comment this out later to avoid seeing this in the log file.
    };
} T_Keep_Showing_Star;

####  Specific for presenting Visual Stimuli   --------------------------------
# Definition of the Trial to present Visual Stimuli word-by-word
trial
{
    trial_duration = forever;
    trial_type = first_response;
    terminator_button = 7,8;  # The exact response can be either of these;

    stimulus_event
    {
        picture P_Visual_Stimulus;
        code = "";  # PCL Program fills this!!!
        # port_code = ; # PCL Program fills this!!!
        target_button = 8;        # PCL Program sets this.
    } E_Visual_Stimulus;
} T_Visual_Stimulus;

####

####  Specific for presenting Visual Stimuli   --------------------------------
# Definition of the Trial to present Visual Stimuli word-by-word
trial
{
    trial_duration = forever;
    trial_type = specific_response;
    terminator_button = 1,2,3,4,5,6;

    stimulus_event
    {
        picture P_Message;
    } E_Message;
} T_Message;

# Definition of the Trial to show a Pause
# Operator Controlled
trial
{
    trial_duration = forever;
    trial_type = specific_response;
    terminator_button = 8,9;

    stimulus_event
    {
        picture P_Message;        # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "Pause";
    };
} T_Pause;

# Definition of the Trial to end the session
# Operator Controlled
trial
{
    trial_duration = forever;
    trial_type = specific_response; # ...until the following specific response.
    terminator_button = 9; # ENTER

    stimulus_event
    {
        picture P_Message;        # Show P_Message...the text will be filled dynamically in the PCL part below.
        code = "End of Session!";
    };

} T_End_Thanks;


##############################################################################
# Presentation Control Language (PCL) Program Part
##############################################################################

begin_pcl;

preset string V_Version; # Prompt the version at the beginning!!!

int N_of_Blocks = 4;           # = Number of stories.
int N_of_Trials_per_Block = 2; # = Number of times each story will be presented.

input_file F_Session_List = new input_file;
F_Session_List.open("Session_List_" + V_Version + ".txt");
# This is the file containing the list of stimuli for one session.
# Each line in this file must correspond to one experimental trial.

####

# Columns of strings in the order found in the input Session List file.
string V_Story;
int V_N_of_Single_Pages;

bool V_Last_Single_Page_Blank = false;
string V_Visual_Stimulus_L;
string V_Visual_Stimulus_R;

# output_port O_Port_to_EEG = output_port_manager.get_port(1);
stimulus_data D_Stimulus_Data = stimulus_manager.last_stimulus_data();
int D_Last_Response = response_manager.last_response();

#========================
# Main Experiment Loop: -
#========================
loop     /*** Begin Main Loop 1 for Blocks ***/
    int V_Current_Block = 1

until
    V_Current_Block > N_of_Blocks

begin
    if V_Current_Block == 1 then

	     Txt_Message.set_caption("Welcome!\n\nWe'll begin presenting\nthe stories in a moment.\n\nPlease wait.");
	     Txt_Message.redraw();

        T_Start_Exp.present();
    else
		  Txt_Message.set_caption("A short break now.\n\nWhen you are ready,\npress the right arrow key to proceed.");
	     Txt_Message.redraw();

        T_Pause.present();
    end;

	 Txt_Message.set_caption("Story  " + string(V_Current_Block) + "  of  " +  string(N_of_Blocks) + "\n\nThe story will be presented twice.\n\nFirst time : read it silently\nSecond time : read it aloud\n\nPress the right arrow key to proceed.");
	 Txt_Message.redraw();

    T_Continue.present();

	 Txt_Message.set_caption(" ");
	 Txt_Message.redraw();
	 T_Blank_Screen.present();

	 V_Story = F_Session_List.get_string();     # Get the story code...Eg:- E01
	 V_N_of_Single_Pages = F_Session_List.get_int();    # Get the no. of single pages ( = no. of page images)

    #==========
    # Loop 2: -
    #==========
    loop    /*** Begin Loop 2 for Trials ***/
        int V_Current_Trial = 1

    until
        V_Current_Trial > N_of_Trials_per_Block

    begin

    if V_Current_Trial == 1 then

	     Txt_Message.set_caption("Story  " + string(V_Current_Block) + "  of  " +  string(N_of_Blocks) + "\n\nRead silently\n\nPress the right arrow key to read on.");
	     Txt_Message.redraw();

        T_Continue.present();
    else
		  Txt_Message.set_caption("Story  " + string(V_Current_Block) + "  of  " +  string(N_of_Blocks) + "\n\nRead aloud\n\nPress the right arrow key to read on.");
	     Txt_Message.redraw();

        T_Continue.present();
    end;

	 Txt_Message.set_caption(" ");
	 Txt_Message.redraw();
	 T_Blank_Screen.present();

			if (V_N_of_Single_Pages % 2 > 0) then # The story has odd number of single pages; so the last single page must be blank.
				V_Last_Single_Page_Blank = true;
				#V_N_of_Facing_Pages = (V_N_of_Single_Pages+1)/2;
			else
				V_Last_Single_Page_Blank = false;
				#V_N_of_Facing_Pages = V_N_of_Single_Pages/2;
			end;



        # Do filename and event-code assignments valid during this exp-trial;
        # Load wavefile and bitmap stimuli that are needed for this trial;
        /*# We do this before launching the experimental trial so as to keep
          # as minimal intereference as possible in trial timings.  You see,
          # these things consume processor time, however small they are!
        */

        E_Launch_New_Trial.set_event_code("B" + string(V_Current_Block) + " T" + string(V_Current_Trial));

        # Execute the Experimental Trial!!!

        T_Launch_New_Trial.present();                      # Show clear screen for 400 ms


        # Present the pages of Stimulus Text in a staggered loop, such that the participant can browse through the
        # pages back and forth.
        #============
        # Loop 2.1: -
        #============
			loop int V_Current_Single_Page = 1
			until V_Current_Single_Page > V_N_of_Single_Pages # Staggered loop!!! That is, the loop counter is
			begin                                    # sometimes incremented by 2, sometimes decremented by 2 and sometimes neither.
				                                      # This allows going backwards (unless first page) or forwards (unless last page)
				                                      # as many times as the participant wants.



				V_Visual_Stimulus_L =  V_Story + "-" + string(V_Current_Single_Page) +".png";

				if (V_Last_Single_Page_Blank && V_Current_Single_Page == V_N_of_Single_Pages) then
					V_Visual_Stimulus_R = "Blank-Page.png";
            else
					V_Visual_Stimulus_R =  V_Story + "-" + string(V_Current_Single_Page+1) +".png";
            end;



				/*****************  Visual Stimulus Presentation Begins  ***********************/

				# Put the current word on the picture and redraw the text part in it.
				Bmp_Visual_Stimulus_L.set_filename(V_Visual_Stimulus_L);
				Bmp_Visual_Stimulus_L.load();

				Bmp_Visual_Stimulus_R.set_filename(V_Visual_Stimulus_R);
				Bmp_Visual_Stimulus_R.load();

            Txt_Footer_L.set_caption("<b>" + string(V_Current_Single_Page) + "</b>");
            Txt_Footer_L.redraw();

            if (V_Visual_Stimulus_R != "Blank-Page.png") then
					Txt_Footer_R.set_caption("<b>" + string(V_Current_Single_Page+1) + "</b>");
					Txt_Footer_R.redraw();
				else
					Txt_Footer_R.set_caption(" ");
					Txt_Footer_R.redraw();
				end;

				# Present the trial that will show the changed text on screen for x ms.
				T_Visual_Stimulus.present();


				/*****************  Visual Stimulus Presentation Ends  *************************/

			   # Get the last button pressed, and either go backwards or forwards accordingly.
			   D_Last_Response = response_manager.last_response();

			   if (D_Last_Response == 7) then
					# Go backwards one set of facing pages
					if (V_Current_Single_Page == 1) then
						# We're on the first page; so going backwards not possible
						# So just don't change the Visual_Stimuli and run the loop again with the same page number.
					else
						V_Current_Single_Page = V_Current_Single_Page - 2;
					end;

			   elseif (D_Last_Response == 8) then

				   #if (V_Current_Single_Page == V_N_of_Single_Pages || V_Current_Single_Page+1 == V_N_of_Single_Pages) then
					   # We're on the last page; so going forwards is not possible
					   # So just don't change the Visual_Stimuli and run the loop again with the same page number.
					#else
						# Go forwards to the next set of facing pages.
						V_Current_Single_Page = V_Current_Single_Page + 2;
					#end;
				end;

		  #=============
		  # End Loop 2.2
		  #=============
		  end; /*** End Loop 2.2 for Facing Pages ***/

        T_Blank_Screen.present();

        # Go to the Next Trial
        V_Current_Trial = V_Current_Trial + 1;

    #===========
    # End Loop 2
    #===========
    end; /*** End Loop 2 for Trials ***/


	Txt_Message.set_caption("1. Question\n\n<font face='Verdana'><font size='20'>Did you like the text?\n\nnot at all                                  very much\n\n1 ___ 2 ___ 3 ___ 4 ___ 5 ___ 6</font></font>");
	Txt_Message.redraw();
	T_Message.present();

	Txt_Message.set_caption("2. Question\n\n<font face='Verdana'><font size='20'>Did you enjoy reading the text aloud?\n\nnot at all                                  very much\n\n1 ___ 2 ___ 3 ___ 4 ___ 5 ___ 6</font></font>");
	Txt_Message.redraw();
	T_Message.present();


# Go to the Next Block
V_Current_Block = V_Current_Block + 1;

#================
# End Main Loop 1
#================
end;    /*** End Main Loop 1 for Blocks ***/

# Close all the open files
F_Session_List.close();

Txt_Message.set_caption("We're done.  Thank you!");
Txt_Message.redraw();

# Finish off the session!!!
T_End_Thanks.present();