% standard opening commands
clc;
clear all;
close all;
rng('shuffle');
% Hide the cursor and "disable" keyboard
% Press ctl-c to bring it back
ListenChar(2);
HideCursor();

% define variables, the keys we want people to respond with (this is from the previous exercise)
% lets define out target keys, this also gets the ASCII or numeric index for they key
k1 = 'a';
k2 = 'l';
keyOne = KbName(k1);
keyTwo = KbName(k2);

% open a drawing window
Screen('Preference', 'SkipSyncTests', 1); 
[window, window_size] = Screen('OpenWindow', 0, [0 0 0], [],32,2);

% figure out where the middle of the screen is
x_mid = window_size(3)/2;
y_mid = window_size(4)/2;

% define a the rectanges that will be draw. recall these just consist of
% four numbers, the top left x and y values and the bottom right x and y
% valuesa
stimulus_size = 50;
distance_from_middle = 200;
left_top_x = x_mid - distance_from_middle - stimulus_size;
left_top_y = y_mid - stimulus_size;
left_bottom_x = x_mid - distance_from_middle + stimulus_size;
left_bottom_y = y_mid + stimulus_size;
left_square = [left_top_x left_top_y left_bottom_x left_bottom_y];
right_top_x = x_mid + distance_from_middle - stimulus_size;
right_top_y = y_mid - stimulus_size;
right_bottom_x = x_mid + distance_from_middle + stimulus_size;
right_bottom_y = y_mid + stimulus_size;
right_square = [right_top_x right_top_y right_bottom_x right_bottom_y];
% do not panic, work through what these values are - they just refer to two squares

% set the number of trials
number_of_trials = 20;

% draw an instruction
Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['Get ready!'],'center', 'center', [255 255 255],[],[],[],2);
% never forget to flip!
Screen('Flip',window);

% wait for three seconds
WaitSecs(3);

for trial_counter = 1:number_of_trials

    % figure out where we are drawing the square
    stimulus_side = rand(1);
    if stimulus_side <= 0.5
        stimulus_side = 1; % use 1 to code for a stimulus on the left
        drawn_square = left_square;
    else
        stimulus_side = 2; % use 2 to code for a stimulus on the right
        drawn_square = right_square;
    end

    % I am going to use Formatted Text to draw the fixation + sign
    Screen(window,'TextFont','Arial');
    Screen(window,'TextSize',40);
    DrawFormattedText(window, ['+'],'center', 'center', [255 255 255],[],[],[],2);
    % never forget to flip!
    Screen('Flip',window);

    % wait for some amount of time, between 400 and 700 ms
    random_delay = 0.4 + rand(1)*0.3;
    WaitSecs(random_delay);

    % start a reaction time clock
    tic;

    % draw the target square
    Screen('FillRect', window , [255 255 255], drawn_square);
    % redraw the fixation cross, the flip command erases all previously drawn
    % items
    DrawFormattedText(window, ['+'],'center', 'center', [255 255 255],[],[],[],2);
    % never forget to flip!
    Screen('Flip',window);

    % wait for the response
    while 1

        % read the current status of the keyboard
        [keyIsDown, secs, keyCode] = KbCheck;
        % this returns some key information
        % keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
        % secs is the time of the key press
        % keyCode is a vector for each key, a 1 will appear in the vector for the key that has been pressed

        % this is a logic test, if the value of keyCode corresponding to 'a' is pressed
        if keyCode(keyOne) == 1
            reaction_time = toc;
            break;
        end
        if keyCode(keyTwo) == 1
            reaction_time = toc;
            break;
        end

    end
    
    % store some data for analysis
    student_data(trial_counter,1) = trial_counter;
    student_data(trial_counter,2) = stimulus_side;
    student_data(trial_counter,3) = reaction_time;
    
end

% close the drawing window
Screen('CloseAll');
% Hide the cursor and "disable" keyboard
% Press ctl-c to bring it back
ListenChar(0);
ShowCursor();

clc;
% note how cool this is? we can begin doing analysis if we want WHILE we
% are running the study if we want
disp(['The mean reaction time was: ' num2str(mean(student_data(:,3)))]);

plot(student_data(:,3));
title('Reaction Time Over Trials');