function [] = Unimodal()
% -------------------------------------------------------------------------
%   Initialisation of Example Experiment:
%   Integration of Disparity and Texture for Visual Slant
%
%   There are 3 types of trials:
%   - Disparity only
%   - Perspective only
%   - Both single cue conditions
%
%   Base slant is 0 degrees
%
%   The actual code for the Experiment can be found in the file:
%   "SlantExperiment.m"
%
%   Note: Psychtoolbox is needed for presenting the stimuli which is freely
%   downloadable from: https://www.psychtoolbox.org/
%   The website also provides on how to install, use and credit the
%   Psychtoolbox see:
%
%   Note 2: We also assume you have a pair of anaglyph glasses lying
%   around. Please set the stereomode below to the glasses you have
%   according to the following list:
%     6 == Red-Green
%     7 == Green-Red
%     8 == Red-Blue
%     9 == Blue-Red
%
%   Note 3: Stimuli were programmed according to the description of:
%   Hillis, Ernst, Banks & Landy (2002) Science, 298, 1627-1630
%   Hillis, Watt, Landy & Banks (2004) Journal of Vision, 4, 967-992
%   with a few minor adjustments
%
%   by Loes van Dam, 2014
%   Loes.van_Dam@uni-bielefeld.de
%
% -------------------------------------------------------------------------

Screen('Preference', 'SkipSyncTests', 1);   % timing is not important here so will skip any testing of it
screenid = max(Screen('Screens'));          % find screen for display
[screenWidth, screenHeight]=Screen('DisplaySize', screenid);

dialogR=inputdlg({  'Please enter the filename',...
                    'Modality (1: texture 2: disparity 3: both single cues)',...
                    'StereoMode',...
                    'Physical screen width (mm):',...
                    'Physical screen height (mm):'},...
                    'Pre testing dialog',1,{'test','3','8',num2str(screenWidth),num2str(screenHeight)});
filename = [dialogR{1},'.mat'];             % the file name for data storage;
modalityTested = str2num(dialogR{2});       % the modality to use
stereoMode = str2num(dialogR{3});           % the stereomode to use
screenWidth = str2num(dialogR{4});          % the correct physical screenwidth
screenHeight = str2num(dialogR{5});         % the correct physical screenheight

if exist(filename,'file'),  % check if file exist
    fprintf([   'WARNING: The filename already exists!\n',...
                'Will continue trials at point at which it was last left off.\n']);
    continueOld = 1;        % continue trials if necessary or skip to analysis if completed
else
    continueOld = 0;
    
    % ---------------------------------------------------------------------
    %   do some training trials first 
    %
    %   for training we will generate a smaller set
    % ---------------------------------------------------------------------
    train_baseslant = 0;
    train_testslants = -50:25:50; % comparison slants relative to baseslants
    temp = zeros(length(train_testslants),6);
    temp(:,2) = train_baseslant;
    temp(:,4) = train_testslants';
    temp(:,6) = -1;
    Trial_List_Train =[];
    if mod(modalityTested,2)==1
        Trial_List_Train = temp;
    end
    temp(:,1) = 1;
    if modalityTested>1
        Trial_List_Train = [Trial_List_Train;temp];
    end
    clear('temp');
    if isempty(Trial_List_Train)
        disp('Error! The modality provided is incorrect (should be 1, 2 or 3)')
        return;
    end

    SlantExperiment(Trial_List_Train,'delme.mat',1,stereoMode,[screenWidth,screenHeight]);
    
end

% -------------------------------------------------------------------------
%   The conditions
% -------------------------------------------------------------------------

baseslants = [0];       % base slants
                        % Note: with increasing base slant reliability for
                        % texture defined slant increases (Knill, 1998,
                        % Vision Research, 38(11), 1655-1682)
                        
conflicts = [ ];        % conflicts between texture and disparity cues

testslants = -35:5:35;  % comparison slants relative to baseslants

Nr_repititions = 10;    % nr of repititions for each combination


% -------------------------------------------------------------------------
%   Determine the number of trials and populate trial list
%
%   Each row in the following matrix will signify a single trial
%   The numbers within each row corresponds to:
%   [1] cues used: 0 = texture only, 1 = disparity only; 2 = both
%   [2] base slant
%   [3] conflict in base slant
%   [4] comparison slant
%   [5] which first 0 = base slant first; 1 = comparison first
%   [6] response 0 = turned leftward; 1 = turned rightward
% -------------------------------------------------------------------------

Nr_Single_Cue_Trials = length(baseslants)*length(testslants)*Nr_repititions;
Nr_Both_Cues_Trials  = length(baseslants)*length(testslants)*length(conflicts)*Nr_repititions;

Nr_Base_Slants       = length(baseslants);
Nr_Test_Slants       = length(testslants);
Nr_Test_Reps         = length(testslants)*Nr_repititions;
Nr_Base_Reps         = length(baseslants)*Nr_repititions;

Total_Nr_Trials      = ((modalityTested>2)+1)*Nr_Single_Cue_Trials + Nr_Both_Cues_Trials;

skip_Experiment = 0;    % set flag to do experiment unless canceled
if continueOld == 1,    % load datafile and check where to continue

    load(filename, 'Trial_List');
    starttrial = find(Trial_List(:,6) == -1);
    if length(starttrial) == 0,     % already completed move straight to analysis
        skip_Experiment = 1;        % set flag to skip Experiment
    else
        starttrial = starttrial(1);
    end
    
else
    % ---------------------------------------------------------------------    
    %   generate the trial list
    % ---------------------------------------------------------------------    
    Trial_List = zeros(Total_Nr_Trials,6);
    Current_TrialNr = 0;
    NrTrials = Nr_Single_Cue_Trials;
    cues_given =[];
    switch modalityTested
        case 1; cues_given = 0;
        case 2; cues_given = 1;
        case 3; cues_given = [0 1];
    end
    for cues = cues_given,
        if cues < 2,
            conflict_lst = [0];
        else
            conflict_lst = conflicts;
        end
        for conf = 1:length(conflict_lst),
            temp = (baseslants' * ones(1,Nr_Test_Reps))';
            Trial_List(Current_TrialNr+(1:NrTrials),1) = cues;
            Trial_List(Current_TrialNr+(1:NrTrials),2) = temp(:);
            Trial_List(Current_TrialNr+(1:NrTrials),3) = conflict_lst(conf);
            temp = testslants' * ones(1,Nr_Base_Reps);
            Trial_List(Current_TrialNr+(1:NrTrials),4) = temp(:);
            temp = ([0;1] * ones(1,Nr_Test_Reps/2))';
            temp = temp(:)* ones(1,Nr_Base_Slants);
            Trial_List(Current_TrialNr+(1:NrTrials),5) = temp(:);

            Current_TrialNr = Current_TrialNr + NrTrials;
        end    
    end
    Trial_List(:,6) = -1;

    rand('twister',sum(100*clock));                         % set seed for random generator such that different orders are run each time
    Trial_List = Trial_List(randperm(Total_Nr_Trials),:);   % scramble the trial order to intermix conditions

    starttrial = 1;

end


if ~(skip_Experiment == 1),
    SlantExperiment(Trial_List,filename,starttrial,stereoMode,[screenWidth,screenHeight]);
else
    fprintf('Datafile seems to be complete. Please run again using a different name.\n');
end