function [] = BimodalSimple_1Interval()
% -------------------------------------------------------------------------
%   Initialisation of Example Experiment:
%   Integration of Disparity and Texture for Visual Slant
%
%   There are 3 types of trials:
%   - Disparity only
%   - Perspective only
%   - Both cues combined
%
%   Base slant is 0 degrees
%
%   The actual code for the Experiment can be found in the file:
%   "SlantExperiment2Interval.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',...
                    'StereoMode',...
                    'Physical screen width (mm):',...
                    'Physical screen height (mm):'},...
                    'Pre testing dialog',1,{'test','8',num2str(screenWidth),num2str(screenHeight)});
filename = [dialogR{1},'.mat'];             % the file name for data storage;
stereoMode = str2num(dialogR{2});           % the stereomode to use
screenWidth = str2num(dialogR{3});
screenHeight = str2num(dialogR{4});




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_baseslants = [0];       
    train_conflicts = [0];   % conflicts between texture and disparity cues
    train_testslants = -50:25:50; % comparison slants relative to baseslants
    temp = zeros(length(train_testslants),5);
    temp(:,4) = train_testslants';
    temp(:,5) = -1;
    Trial_List_Train = temp;
    temp(:,1) = 1;
    Trial_List_Train = [Trial_List_Train;temp];
    temp(:,1) = 2;
    Trial_List_Train = [Trial_List_Train;temp];    
    clear('temp');

    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 = [-15,15];   % 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] 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      = 2*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,5);
    Current_TrialNr = 0;
    NrTrials = Nr_Single_Cue_Trials;
    for cues = 0:2,
        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(:);

            Current_TrialNr = Current_TrialNr + NrTrials;
        end    
    end
    Trial_List(:,5) = -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