
%% ========= PREDATT EXP 1: REP ========= %%
% Replication of Experiment 1 from: Zhao, Al-Aidroos & Nicholas
% Turk-Browne, (2013), Psychological Science.


% 01/02/2020 - Alexandra Vlassova
 
% Task summary:
% 4 streams of shapes surround fixation, interrupted occasionally by a
% visual search array that contains one T target and three L distractors in
% the four stream locations. Task is to indicate whether the target pointed
% to the left or right. Each stream is generated from a different set of
% nine shapes.

% each shape trial contains one shape from each of the four
% streams: one in each of the top, bottom, left, and right
% positions on the screen. 750 ms followed by an interstimulus
% interval (ISI) of 750 ms.

% 1 session, 2 x trials, mem test at the end
% compare first and second half 


%%=== GET PATHS ===%%
sca; clear all; % clear the workspace and close screens 
codePath = [pwd '\code\']; % get pathname for helper files
cd(codePath)

setupExperiment; % run script to setup screen and generate trials

try
    for iBlock = 1:2
        
        giveInstructions; % show task instructions & practice trials / show break instructions
        
        for n = 1:stim.ntrials
            
            %%% ========= DRAW ISI ========= %%%
            Screen('FrameRect',w,black,stim.box,3); % draw the empty square frames to each location
            Screen('DrawLines',w,stim.fixation,3,black,[centreX, centreY]); % draw fixation cross to the screen center
            vbl = Screen('Flip', w);
            
            %%% ========= MAKE NEXT TRIAL ========= %%%
            if nsearch < 81 && n == data.search(iBlock).trials(nsearch)  % check if search trial
                locT = data.search(iBlock).location(nsearch); % where the target is
                locL = setdiff(1:4,locT); % which locations to put Ls
                shiftL = Shuffle([1 1 2 2]); % which locations do we shift the horizontal up/down
                
                if data.search(iBlock).orientation(nsearch) == 1 % pointing left
                    rotateT = 0; rotateL = Shuffle([0 180 180]); % rotate target and shuffle distractors
                elseif data.search(iBlock).orientation(nsearch) == 2 % pointing right
                    rotateT = 180; rotateL = Shuffle([0 0 180]); % rotate target and shuffle distractors
                end
                
                Ltop = Screen('MakeTexture',w,imread(paths.Ltop)); % make textures for distractors
                Lbottom = Screen('MakeTexture',w,imread(paths.Lbottom)); % make textures for distractors
                Tshape = Screen('MakeTexture',w,imread(paths.T)); % make texture for target

                draw = 0; % draw search trial
                
            else % shape trial
                strPath     = paths.dirs{data.chosen.strStream,data.struct(iBlock,nrep)}; % get chosen shape path
                shapeTexStr = Screen('MakeTexture',w,imread(strPath)); % make tex for structured shape
                for ii = 1:3 % for the 3 random streams
                    randPath{ii}     = paths.dirs{data.chosen.randStreams(ii),data.random(iBlock,ii,nrep)}; % get chosen shape paths
                    shapeTexRand{ii} = Screen('MakeTexture',w,imread(randPath{ii})); % make tex for random shapes
                end 
                nrep = nrep+1;
                draw = 1; % draw shape trial
            end
            
            %%% ========= CHECK TIME ========= %%%
            while 1 %check if ISI time is up
                ISItimer = GetSecs - vbl;
                if ISItimer >= .75
                    break;
                end
            end
            
            %%% ========= DRAW TRIAL ========= %%%
            Screen('FrameRect',w,black,stim.box,3); % draw the empty square frames to each location
            Screen('DrawLines',w,stim.fixation,3,black,[centreX, centreY]); % draw fixation cross to the screen center
            vbl = Screen('Flip', w); % sync to vbl
            startTime = vbl; %  sync to vbl & get starting timestamp
            
            if draw == 1 % DRAW SHAPES
                for frame = 1:stim.frames % show shapes for 750ms
                    Screen('FrameRect',w,black,stim.box,3); % draw the empty square frames to each location
                    Screen('DrawLines',w,stim.fixation,3,black,[centreX, centreY]); % draw fixation cross to the screen center
                    Screen('DrawTextures',w,shapeTexStr,[],stim.positions(data.chosen.strLocation,:)); % draw the random shapes
                    for ii = 1:3
                        Screen('DrawTextures',w,shapeTexRand{ii},[],stim.positions(data.chosen.randLocations(ii),:)); % draw the structured shape
                    end
                    vbl = Screen('Flip', w, vbl + ifi/2);
                end
                
                %% Close textures
                Screen('Close', shapeTexStr);
                for ii = 1:3
                    Screen('Close', shapeTexRand{ii});
                end
            elseif draw == 0 % DRAW SEARCH TRIAL 
                %% ====== MAKE LETTERS ====== %%
               
                KbQueueCreate(device,keysOfInterest); KbQueueStart(device); % start listening for KB responses
                pressed = 0;
                
                for frame = 1:stim.frames % show search array for first 750ms 
                        Screen('DrawTextures',w,Tshape,[],stim.positions(locT(1),:),rotateT); % draw the T pointing right
                        for ii = 1:3
                            if shiftL(ii) == 1, Lshape = Ltop;
                            else Lshape = Lbottom;
                            end
                            Screen('DrawTextures',w,Lshape,[],stim.positions(locL(ii),:),rotateL(ii)); % draw the Ls
                        end
                        Screen('FrameRect',w,black,stim.box,3); % draw the empty square frames to each location
                        Screen('DrawLines',w,stim.fixation,3,black,[centreX, centreY]); % draw fixation cross to the screen center
                        vbl = Screen('Flip', w, vbl + ifi/2);
                        [pressed, firstPress] = KbQueueCheck(id); % Check the queue for key presses.
                    if pressed == 1
                        break; % stop presenting if there's a keypress
                    end  
                end
                 
                while pressed == 0
                    Screen('FrameRect',w,black,stim.box,3); % draw the empty square frames to each location
                    Screen('DrawLines',w,stim.fixation,3,black,[centreX, centreY]); % draw fixation cross to the screen center
                    vbl = Screen('Flip', w, vbl + ifi/2);
                    [pressed, firstPress] = KbQueueCheck(id); % Check the queue for key presses. 
                end
                
                
                %% Record keyboard response
                if firstPress(leftKey)
                    data.responses(iBlock,nsearch) = 1; % respond left
                    data.RT(iBlock,nsearch) = ((firstPress(leftKey) - startTime)); % get RT in seconds (from the search onset)
                elseif firstPress(rightKey)
                    data.responses(iBlock,nsearch) = 2; % respond right
                    data.RT(iBlock,nsearch) = ((firstPress(rightKey) - startTime)); % get RT in seconds (from the search onset)
                elseif firstPress(escapeKey) % close screen
                    sca;
                end
                
                KbQueueRelease(device); % stop listening to keyboard 
                nsearch = nsearch+1; % increase search trial index
                 
                % Close textures
                Screen('Close', Ltop);
                Screen('Close', Lbottom);
                Screen('Close', Tshape);
            end 
        end
        
        % update the save at the end of each block
        save(savePath); % save everything  
    end
    
    %% ========= END TESTS ========= %%
    giveMemory;  % 2AFC test 
     
    save(savePath); % save everything 
    disp(' '); disp('data is saved!');
    
catch
    save(savePath); % save everything 
    Priority(0);
    sca; fclose('all');
    KbQueueRelease(device); ShowCursor; 
    psychrethrow(psychlasterror);
    disp(' '); disp('data is saved!');
end
 






