 function varargout = hodnoceni_dat(varargin)
% HODNOCENI_DAT MATLAB code for hodnoceni_dat.fig
%      HODNOCENI_DAT, by itself, creates a new HODNOCENI_DAT or raises the existing
%      singleton*.
%
%      H = HODNOCENI_DAT returns the handle to a new HODNOCENI_DAT or the handle to
%      the existing singleton*.
%
%      HODNOCENI_DAT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HODNOCENI_DAT.M with the given input arguments.
%
%      HODNOCENI_DAT('Property','Value',...) creates a new HODNOCENI_DAT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before hodnoceni_dat_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to hodnoceni_dat_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help hodnoceni_dat

% Last Modified by GUIDE v2.5 08-Feb-2018 16:40:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @hodnoceni_dat_OpeningFcn, ...
                   'gui_OutputFcn',  @hodnoceni_dat_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before hodnoceni_dat is made visible.
function hodnoceni_dat_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to hodnoceni_dat (see VARARGIN)

% Choose default command line output for hodnoceni_dat
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
set(handles.vykresleni,'Color',[0 0 0]);
% UIWAIT makes hodnoceni_dat wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = hodnoceni_dat_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in zaznam.
function zaznam_Callback(hObject, eventdata, handles)
% hObject    handle to zaznam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data
[d path] = uigetfile({'*.mat'},'File Selector');
f = strcat(path,d);
data = load(f);
assignin('base','data',data);
global x
x = data.x;
global y
y = data.y;
x = x./2.25;
y = y./2.25;
global pupil_leveoko
pupil_leveoko = data.pupil_leveoko;
pupil_leveoko = pupil_leveoko./10;
global pupil_praveoko
pupil_praveoko = data.pupil_praveoko;
pupil_praveoko = pupil_praveoko./10;
global prumer_pupil
prumer_pupil = data.prumer_pupil;
prumer_pupil = prumer_pupil./10;

for i = 2:length(prumer_pupil)
    roz = abs(prumer_pupil(i)-prumer_pupil(i-1));
    if roz > 5
       prumer_pupil(i) = prumer_pupil(i-1);
    end
end
assignin('base','y',y);
assignin('base','x',x);
assignin('base','pupil_leveoko',pupil_leveoko);
assignin('base','pupil_praveoko',pupil_praveoko);


% --- Executes on button press in spustit.
function spustit_Callback(hObject, eventdata, handles)
% hObject    handle to spustit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

addpath('D:\semestrální projekt');

global stop_tl
global nFrames
global mov_pomocna
global pupil_leveoko
global pupil_praveoko

set(handles.slider1,'Max',nFrames);

global hh
hh = handles.tecka;
hh = get(hh,'Value');
assignin('base','hh',hh);

%%% pokud je vybráno hodnocení černobílým překryvem
global cc
cc = handles.prekryv;
cc = get(cc,'Value');
assignin('base','cc',cc)

global prumer_pupil
global x
global y
global mov

% x = round(x./2.25);
% y = round(y./2.25);

global osa_x
osa_x = 30*ones(1,nFrames);
global osa_y
osa_y = 30*ones(1,nFrames);
global osa_x2
osa_x2 = 80*ones(1,nFrames);

%%%% parametry hodnocení %%%%
global pa_pupil
pa_pupil = handles.par_pupil;
pa_pupil = get(pa_pupil,'Value');

global p_rychlost
p_rychlost = handles.par_rychlost;
p_rychlost = get(p_rychlost,'Value');

global p_pohled
p_pohled = handles.par_pohled;
p_pohled = get(p_pohled,'Value');

global doba_p


% pro hodnocení doby pohledu
if p_pohled == 1
    set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

poz2 = [];
i = 1;
while i+1 <(length(x)-1)
    
pom = [0];
pocit = 0;
j=i;
if (x(i+1)-60)<x(i) && x(i)<(x(i+1)+60) && i <= length(x)-1 && (y(i+1)-50)<y(i) && y(i)<(y(i+1)+50)
    while j <= length(x)-1 && (x(j+1)-60)<x(i) && x(i)<(x(j+1)+60) && (y(j+1)-50)<y(i) && y(i)<(y(j+1)+50)
        pocit = pocit +1;
        pom = [pom pocit];
        j = j + 1;
        i=j;
    end
    else
    i=i+1;
end
    poz2 = [poz2 max(pom)];
    
end

doba_p = [];
for i = 1:length(poz2)
    if poz2(i)>0
        pomoc = ones(1,poz2(i));
        pomoc = pomoc.*(poz2(i));
        doba_p = [doba_p pomoc];
    else
        doba_p = [doba_p poz2(i)];
    end
        axes(handles.vykresli_prumer)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.vykresli_prumer,'Visible','Off');
end

%převod vzorku na dobu
 doba_p = doba_p./25;

o = [1:length(doba_p)];
% osa_x1 = linspace(0,length(doba_p)/25,length(doba_p));
axes(handles.graf_parametru)
plot(o,doba_p)
xlim([0 length(doba_p)])
ylim([0 max(doba_p)])
% set(gca,'xtick',[],'ytick',[])
% pom_osax = linspace(0,length(x)/25,10);
% xticks(pom_osax)
set(gca,'ytick',[])
xlabel('Doba pohledu na určité místo [s]')
end

% pro hodnocení pupily
if pa_pupil == 1
set(handles.vykresli_prumer,'Visible','On');
set(handles.vykresli_prumer,'Color',[1 1 1]);
set(handles.prumer,'Visible','On');
set(handles.prumer2,'Visible','On');
set(handles.text2,'Visible','On');
set(handles.text3,'Visible','On');
set(handles.graf_parametru,'Visible','Off'); 

stop_tl = 0;
global rozdil
m = mean(prumer_pupil);
rozdil = prumer_pupil - m;

           axes(handles.p_pupil)
        imagesc(rozdil); 
        colormap(jet(numel(rozdil))); 
        colorbar;
        set(gca,'xtick',[],'ytick',[])
        xlabel('Změna velikosti pupily')
end      

%%%pokud je vybráno hodnocení parametru rychlosti
if p_rychlost == 1
   set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

global v
  
% v = [];
% for u = 3:(length(x)-2)
%     p_akt = (sqrt((x(u-1)-x(u))^2+(y(u-1)-y(u))^2)+sqrt((x(u-2)-x(u-1))^2+(y(u-2)-y(u-1))^2)...
%          + sqrt((x(u+1)-x(u))^2+(y(u+1)-y(u))^2)+ sqrt((x(u+2)-x(u+1))^2+(y(u+2)-y(u+1))^2))/0.2;
%    v = [v p_akt];
% end
v = [];
for u = 2:(length(x))
    p_akt = (sqrt((x(u-1)-x(u))^2+(y(u-1)-y(u))^2))/(1/25);
   v = [v p_akt];
end

    delka = length(x);
% v = v.^2;
%  po_v = ones(1,2);
% v = [po_v v po_v];
assignin('base','v',v);
limit_v = max(v);
    p_vykr = [1:(length(x)-1)];
    axes(handles.graf_parametru)
    plot(p_vykr,v)
    xlim([[0,delka]])
    ylim([-10,limit_v])
  set(gca,'xtick',[],'ytick',[])

    xlabel('Rychlost pohybu očí')
end

stop_tl =0;

if hh ==1 %pokud je vybráno zobrazení tečkou
for i = 1:length(mov)
obr = mov(i).cdata;
axes(handles.vykresleni)
imshow(obr)
hold on
r = ceil((prumer_pupil(i)/3)*10); 
% centerX = ceil(x(i)/2.25);
% centerY = ceil(y(i)/2.25);
centerX = x(i);
centerY = y(i);

rectangle('Position',[centerX - r, centerY - r, r*2, r*2],...
    'Curvature',[1,1],...
    'FaceColor','r','EdgeColor','r');
hold on
if i > 5
plot(x(i-5:i),y(i-5:i),'LineWidth',5,'Color','r')
end
hold off
drawnow
set(handles.slider1,'Value',i);
% stop_tl = get(handles.stop,'Value');
if pa_pupil == 1
        axes(handles.vykresli_prumer)
       set(handles.vykresli_prumer, 'XTick', [], 'ytick',[]);
% set(gca,'xtick',[],'ytick',[])

        plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
        xlim([0 100])
        ylim([0 50])
        hold on
        plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
        hold off
        set(gca,'xtick',[],'ytick',[])
        drawnow

        pomoc = pupil_leveoko(i);
        pomoc = round(pomoc,2);
        set(handles.prumer,'String',pomoc)
        pomoc2 = pupil_praveoko(i);
        pomoc2 = round(pomoc2,2);
        set(handles.prumer2,'String',pomoc2)

elseif p_pohled == 1
        axes(handles.graf_parametru)
        set(gca,'xtick',[],'ytick',[])
        plot(o,doba_p)
        ylim([0 max(doba_p)+0.2])
        xlim([0 length(doba_p)]);
%         set(gca,'xtick',[],'ytick',[])
        set(gca,'xtick',[])
        xlabel('Doba pohledu na určité místo [s]')
        hold on
        stem(o(i),doba_p(i),'filled','Color','r')
        drawnow
        hold off
        
elseif p_rychlost == 1
        axes(handles.graf_parametru)
        plot(p_vykr,v)
        xlim([[0,delka]])
        ylim([-10,limit_v])
         set(gca,'xtick',[],'ytick',[])
%         set(gca,'xtick',[])
        xlabel('Rychlost pohybu očí')
        hold on
        stem(p_vykr(i),v(i),'filled','Color','r')
        drawnow
        hold off
end
  if stop_tl == 1
    break;
  else
    continue;
  end
end
end

if cc==1 %prekryv
    for i = 1:nFrames
        obr = mov_pomocna(i).cdata;
        axes(handles.vykresleni)
        imshow(obr)
        drawnow
        set(handles.slider1,'Value',i);
        stop_tl = get(handles.stop,'Value');
     if pa_pupil == 1
            axes(handles.vykresli_prumer)
            plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
            xlim([0 100])
            ylim([0 50])
            hold on
            plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
            hold off
            drawnow

            pomoc = pupil_leveoko(i);
            pomoc = round(pomoc,2);
            set(handles.prumer,'String',pomoc)
            pomoc2 = pupil_praveoko(i);
            pomoc2 = round(pomoc2,2);
            set(handles.prumer2,'String',pomoc2)
      elseif p_pohled == 1
            axes(handles.graf_parametru)
            set(gca,'xtick',[])
            plot(o,doba_p)
            ylim([0 max(doba_p)+0.02])
            xlim([0 length(doba_p)]);
           set(gca,'xtick',[],'ytick',[])
%             set(gca,'xtick',[])
            xlabel('Doba pohledu na určité místo [s]')
            hold on
            stem(o(i),doba_p(i),'filled','Color','r')
            drawnow
            hold off
      elseif p_rychlost == 1
            axes(handles.graf_parametru)
            plot(p_vykr,v)
            xlim([[0,delka]])
            ylim([-10,limit_v])
            set(gca,'xtick',[],'ytick',[])
%             set(gca,'xtick',[])
            xlabel('Rychlost pohybu očí')
            hold on
            stem(p_vykr(i),v(i),'filled','Color','r')
            drawnow
            hold off
        end
    if stop_tl == 1
        break;
    else
        continue;
    end
    end
end


function prumer_Callback(hObject, eventdata, handles)
% hObject    handle to prumer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of prumer as text
%        str2double(get(hObject,'String')) returns contents of prumer as a double


% --- Executes during object creation, after setting all properties.
function prumer_CreateFcn(hObject, eventdata, handles)
% hObject    handle to prumer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function prumer2_Callback(hObject, eventdata, handles)
% hObject    handle to prumer2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of prumer2 as text
%        str2double(get(hObject,'String')) returns contents of prumer2 as a double


% --- Executes during object creation, after setting all properties.
function prumer2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to prumer2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global nFrames
global mov
global mov_pomocna
global x
global y
global stop_tl
% set(handles.slider1,'Max',nFrames);

%%% pokud je vybráno vyhodnocení červenou tečkou
global hh
hh = handles.tecka;
hh = get(hh,'Value');
assignin('base','hh',hh);

%%% pokud je vybráno hodnocení černobílým překryvem
global cc
cc = handles.prekryv;
cc = get(cc,'Value');
assignin('base','cc',cc)

global prehraj

global pa_pupil
global p_rychlost
global p_pohled
global v
% pro hodnocení doby pohledu
if p_pohled == 1
    set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

poz2 = [];
i = 1;
while i+1 <length(x)
pom = [0];
pocit = 0;
j=i;
if i <= length(x)-1 && (x(i+1)-50)<x(i) && x(i)<(x(i+1)+50) && (y(i+1)-40)<y(i) && y(i)<(y(i+1)+40)
    while j <= length(x)-1 && (x(j+1)-50)<x(i) && x(i)<(x(j+1)+50) && (y(j+1)-40)<y(i) && y(i)<(y(j+1)+40)
        pocit = pocit +1;
        pom = [pom pocit];
        j = j + 1;
        i=j;
    end
    else
    i=i+1;
end
    poz2 = [poz2 max(pom)];
    
end

doba_p = [];
for i = 1:length(poz2)
    if poz2(i)>0
        pomoc = ones(1,poz2(i));
        pomoc = pomoc.*(poz2(i));
        doba_p = [doba_p pomoc];
    else
        doba_p = [doba_p poz2(i)];
    end
end
doba_p = doba_p./25;
o = [1:length(doba_p)];
% plot(o,novy)
xlim([0 length(doba_p)])
axes(handles.graf_parametru)
plot(o,doba_p)
xlim([0 length(doba_p)])
set(gca,'xtick',[])
xlabel('Doba pohledu na určité místo [s]')
 %odranit kružnice z prumeru pupily
        axes(handles.vykresli_prumer)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.vykresli_prumer,'Visible','Off');
  % odsranit colorbar z pruměru pupily
        axes(handles.p_pupil)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.p_pupil,'Visible','Off');
end


% pro hodnocení pupily
if pa_pupil == 1
set(handles.vykresli_prumer,'Visible','On');
set(handles.vykresli_prumer,'Color',[1 1 1]);
set(handles.prumer,'Visible','On');
set(handles.prumer2,'Visible','On');
set(handles.text2,'Visible','On');
set(handles.text3,'Visible','On');
set(handles.graf_parametru,'Visible','Off'); 

stop_tl = 0;
global rozdil
        axes(handles.p_pupil)
        imagesc(rozdil); 
        colormap(jet(numel(rozdil))); 
        colorbar;
        set(gca,'xtick',[],'ytick',[])
        xlabel('Změna velikosti pupily')
end     

if cc==1
    prehraj = mov_pomocna;
elseif hh==1
    prehraj = mov;
end

%%%pokud je vybráno hodnocení parametru rychlosti
if p_rychlost == 1
   set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

% v = [];
% 
% for u = 3:(length(x)-2)
%     p_akt = (sqrt((x(u-1)-x(u))^2+(y(u-1)-y(u))^2)+sqrt((x(u-2)-x(u-1))^2+(y(u-2)-y(u-1))^2)...
%          + sqrt((x(u+1)-x(u))^2+(y(u+1)-y(u))^2)+ sqrt((x(u+2)-x(u+1))^2+(y(u+2)-y(u+1))^2))/0.2;
%    v = [v p_akt];
% end
v = [];
for u = 2:(length(x))
    p_akt = sqrt(((x(u-1)-x(u))^2+(y(u-1)-y(u))^2))/(1/25);
   v = [v p_akt];
end

    delka = length(x);
%  v = v.^2;
%  po_v = ones(1,2);
% v = [po_v v po_v];
limit_v = max(v);
    p_vykr = [1:(length(x)-1)];
    axes(handles.graf_parametru)
    plot(p_vykr,v)
    xlim([[0,delka]])
    ylim([-10,limit_v])
    set(gca,'xtick',[],'ytick',[])
    xlabel('Rychlost pohybu očí')
     %odranit kružnice z prumeru pupily
        axes(handles.vykresli_prumer)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.vykresli_prumer,'Visible','Off');
        % odsranit colorbar z pruměru pupily
        axes(handles.p_pupil)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.p_pupil,'Visible','Off');
end

assignin('base','prehraj',prehraj);
global prumer_pupil
global pupil_leveoko
global pupil_praveoko
global osa_x
global osa_y
global osa_x2



global posun
    posun = get(handles.slider1,'Value');
    posun = round(posun);
    assignin('base','posun',posun);
    
if hh == 1
    for i = posun:length(mov)
obr = mov(i).cdata;
axes(handles.vykresleni)
imshow(obr)
hold on
r = ceil((prumer_pupil(i)/3)*10); 
% centerX = ceil(x(i)/2.25);
% centerY = ceil(y(i)/2.25);
centerX = x(i);
centerY = y(i);
rectangle('Position',[centerX - r, centerY - r, r*2, r*2],...
    'Curvature',[1,1],...
    'FaceColor','r','EdgeColor','r');
hold on
plot(x(i-5:i),y(i-5:i),'LineWidth',5,'Color','r')
hold off
drawnow
set(handles.slider1,'Value',i);
if pa_pupil ==1 
        axes(handles.vykresli_prumer)
        plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
        xlim([0 100])
         ylim([0 50])
        hold on
        plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
        hold off
        drawnow

        pomoc = pupil_leveoko(i);
        pomoc = round(pomoc,2);
        set(handles.prumer,'String',pomoc)
        pomoc2 = pupil_praveoko(i);
        pomoc2 = round(pomoc2,2);
        set(handles.prumer2,'String',pomoc2)
    elseif p_pohled == 1
            axes(handles.graf_parametru)
            set(gca,'xtick',[],'ytick',[])
            plot(o,doba_p)
            ylim([0 max(doba_p)+0.2])
            xlim([0 length(doba_p)]);
            set(gca,'xtick',[])
            xlabel('Doba pohledu na určité místo [s]')
            hold on
            stem(o(i),doba_p(i),'filled','Color','r')
            drawnow
            hold off
   elseif p_rychlost == 1
            axes(handles.graf_parametru)
            plot(p_vykr,v)
            xlim([[0,delka]])
            ylim([-10,limit_v])
            set(gca,'xtick',[],'ytick',[])
            xlabel('Rychlost pohybu očí')
            hold on
            stem(p_vykr(i),v(i),'filled','Color','r')
            drawnow
            hold off
end
    stop_tl = get(handles.stop,'Value');
  if stop_tl == 1
    break;
  else
    continue;
  end
    end
end

 
if cc ==1 
     for i = posun:nFrames
        obr = mov_pomocna(i).cdata;
        axes(handles.vykresleni)
        imshow(obr)
        drawnow
        set(handles.slider1,'Value',i);
        if pa_pupil == 1
          axes(handles.vykresli_prumer)
        plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
        xlim([0 100])
         ylim([0 50])
        hold on
        plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
        hold off
        drawnow

        pomoc = pupil_leveoko(i);
        pomoc = round(pomoc,2);
        set(handles.prumer,'String',pomoc)
        pomoc2 = pupil_praveoko(i);
        pomoc2 = round(pomoc2,2);
        set(handles.prumer2,'String',pomoc2)
    elseif p_pohled == 1
            axes(handles.graf_parametru)
            set(gca,'xtick',[],'ytick',[])
            plot(o,doba_p)
            ylim([0 max(doba_p)+0.02])
            xlim([0 length(doba_p)]);
            set(gca,'xtick',[])
            xlabel('Doba pohledu na určité místo [s]')
            hold on
            stem(o(i),doba_p(i),'filled','Color','r')
            drawnow
            hold off
   elseif p_rychlost == 1
            axes(handles.graf_parametru)
            plot(p_vykr,v)
            xlim([[0,delka]])
            ylim([-10,limit_v])
            set(gca,'xtick',[],'ytick',[])
            xlabel('Rychlost pohybu očí')
            hold on
            stem(p_vykr(i),v(i),'filled','Color','r')
            drawnow
            hold off
        end
       stop_tl = get(handles.stop,'Value');
    if stop_tl == 1
        break;
    else
        continue;
    end
    end
end


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of stop
global mov
global v
global mov_pomocna
global nFrames
global hh
global cc
global prumer_pupil
global x
global y
global pupil_leveoko
global pupil_praveoko
global osa_x
global osa_y
global osa_x2

global pa_pupil
pa_pupil = handles.par_pupil;
pa_pupil = get(pa_pupil,'Value');

global p_rychlost
p_rychlost = handles.par_rychlost;
p_rychlost = get(p_rychlost,'Value');

global p_pohled
p_pohled = handles.par_pohled;
p_pohled = get(p_pohled,'Value');

% pro hodnocení doby pohledu
if p_pohled == 1
    set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

poz2 = [];
i = 1;
while i+1 <length(x)
pom = [0];
pocit = 0;
j=i;
if i <= length(x)-1 &&(x(i+1)-50)<x(i) && x(i)<(x(i+1)+50) && (y(i+1)-40)<y(i) && y(i)<(y(i+1)+40)
    while j <= length(x)-1 && (x(j+1)-50)<x(i) && x(i)<(x(j+1)+50) && (y(j+1)-40)<y(i) && y(i)<(y(j+1)+40)
        pocit = pocit +1;
        pom = [pom pocit];
        j = j + 1;
        i=j;
    end
    else
    i=i+1;
end
    poz2 = [poz2 max(pom)];
    
end

doba_p = [];
for i = 1:length(poz2)
    if poz2(i)>0
        pomoc = ones(1,poz2(i));
        pomoc = pomoc.*(poz2(i));
        doba_p = [doba_p pomoc];
    else
        doba_p = [doba_p poz2(i)];
    end
end

% o = [1:length(doba_p)];
 doba_p = doba_p./25;
 o = [1:length(doba_p)];
% plot(o,novy)
xlim([0 length(doba_p)])
axes(handles.graf_parametru)
plot(o,doba_p)
xlim([0 length(doba_p)])
set(gca,'xtick',[])
xlabel('Doba pohledu na určité místo')
 %odranit kružnice z prumeru pupily
        axes(handles.vykresli_prumer)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.vykresli_prumer,'Visible','Off');
        % odsranit colorbar z pruměru pupily
        axes(handles.p_pupil)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.p_pupil,'Visible','Off');
end

% pro hodnocení parametru velikosti pupily
if pa_pupil == 1
set(handles.vykresli_prumer,'Visible','On');
set(handles.vykresli_prumer,'Color',[1 1 1]);
set(handles.prumer,'Visible','On');
set(handles.prumer2,'Visible','On');
set(handles.text2,'Visible','On');
set(handles.text3,'Visible','On');
set(handles.graf_parametru,'Visible','Off'); 

% stop_tl = 0;
global rozdil
m = mean(prumer_pupil);
rozdil = prumer_pupil - m;
           axes(handles.p_pupil)
        imagesc(rozdil); 
        colormap(jet(numel(rozdil))); 
        colorbar;
        set(gca,'xtick',[],'ytick',[])
        xlabel('Změna velikosti pupily')
end     


%%%pokud je vybráno hodnocení parametru rychlosti
if p_rychlost == 1
   set(handles.vykresli_prumer,'Visible','Off');
set(handles.prumer,'Visible','Off');
set(handles.prumer2,'Visible','Off');
set(handles.text2,'Visible','Off');
set(handles.text3,'Visible','Off'); 
set(handles.graf_parametru,'Visible','On'); 
set(handles.p_pupil,'Visible','Off'); 

%     v = [];
% 
% for u = 3:(length(x)-2)
%     p_akt = (sqrt((x(u-1)-x(u))^2+(y(u-1)-y(u))^2)+sqrt((x(u-2)-x(u-1))^2+(y(u-2)-y(u-1))^2)...
%          + sqrt((x(u+1)-x(u))^2+(y(u+1)-y(u))^2)+ sqrt((x(u+2)-x(u+1))^2+(y(u+2)-y(u+1))^2))/0.2;
%    v = [v p_akt];
% end
v = [];
for u = 2:(length(x))
    p_akt = (sqrt((x(u-1)-x(u))^2+(y(u-1)-y(u))^2))/(1/25);
   v = [v p_akt];
end

    delka = length(x);
%  v = v.^2;
%  po_v = ones(1,2);
% v = [po_v v po_v];
limit_v = max(v);
    p_vykr = [1:(delka-1)];
    axes(handles.graf_parametru)
    plot(p_vykr,v)
    xlim([[0,delka]])
    ylim([-10,limit_v])
    set(gca,'xtick',[],'ytick',[])
    xlabel('Rychlost pohybu očí')
    %odranit kružnice z prumeru pupily
        axes(handles.vykresli_prumer)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.vykresli_prumer,'Visible','Off');
        % odsranit colorbar z pruměru pupily
        axes(handles.p_pupil)
        pom_x = [1];
        pom_y = [1];
        pom_pr = [0.1];
        plot(pom_x,pom_y,'o','MarkerSize',pom_pr)
        set(handles.p_pupil,'Visible','Off');
end
global prehraj
if cc==1 %prekryv
    prehraj = mov_pomocna;
elseif hh==1 %tecka
    prehraj = mov;
end

global stop_tl
stop_tl = get(hObject,'Value')
assignin('base','stop_tl',stop_tl);


assignin('base','prehraj',prehraj);

global posun
    posun = get(handles.slider1,'Value');
    posun = round(posun)+1;
    assignin('base','posun',posun);

if stop_tl == 1
set(handles.stop,'String','Pokračovat')
else
    set(handles.stop,'String','Pauza')
 if hh==1
    for i = posun:nFrames
        obr = mov(i).cdata;
        axes(handles.vykresleni)
        imshow(obr)
        hold on
        r = ceil((prumer_pupil(i)/3)*10);
%         centerX = ceil(x(i)/2.25);
%         centerY = ceil(y(i)/2.25);
        centerX = x(i);
        centerY = y(i);
        rectangle('Position',[centerX - r, centerY - r, r*2, r*2],...
        'Curvature',[1,1],...
        'FaceColor','r','EdgeColor','r');
        hold on
        plot(x(i-5:i),y(i-5:i),'LineWidth',5,'Color','r')
        hold off
        drawnow
        set(handles.slider1,'Value',i);
        pozice = i;
     if pa_pupil == 1
        axes(handles.vykresli_prumer)
        plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
        xlim([0 100])
         ylim([0 50])
        hold on
        plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
        hold off
        drawnow

        pomoc = pupil_leveoko(i);
        pomoc = round(pomoc,2);
        set(handles.prumer,'String',pomoc)
        pomoc2 = pupil_praveoko(i);
        pomoc2 = round(pomoc2,2);
        set(handles.prumer2,'String',pomoc2)
     elseif p_pohled == 1
            axes(handles.graf_parametru)
            set(gca,'xtick',[],'ytick',[])
            plot(o,doba_p)
            ylim([0 max(doba_p)+0.02])
            xlim([0 length(doba_p)]);
            set(gca,'xtick',[])
            xlabel('Doba pohledu na určité místo [s]')
            hold on
            stem(o(i),doba_p(i),'filled','Color','r')
            drawnow
            hold off
    elseif p_rychlost == 1
            axes(handles.graf_parametru)
            plot(p_vykr,v)
            xlim([[0,delka]])
            ylim([-10,limit_v])
            set(gca,'xtick',[],'ytick',[])
            xlabel('Rychlost pohybu očí')
            hold on
            stem(p_vykr(i),v(i),'filled','Color','r')
            drawnow
            hold off
    end
         if stop_tl == 1
            break;
         else
            continue;
         end
    end
 end
 
 if cc==1
    for i = posun:nFrames
        obr = mov_pomocna(i).cdata;
        axes(handles.vykresleni)
        imshow(obr)
        drawnow
        set(handles.slider1,'Value',i);
        stop_tl = get(handles.stop,'Value');
        
        if pa_pupil ==1 %vybráno hodnocení pupily
        axes(handles.vykresli_prumer)
        plot(osa_x(i),osa_y(i),'o','MarkerSize',10*pupil_leveoko(i))
        xlim([0 100])
         ylim([0 50])
        hold on
        plot(osa_x2(i),osa_y(i),'o','MarkerSize',10*pupil_praveoko(i))
        hold off
        drawnow

        pomoc = pupil_leveoko(i);
        pomoc = round(pomoc,2);
        set(handles.prumer,'String',pomoc)
        pomoc2 = pupil_praveoko(i);
        pomoc2 = round(pomoc2,2);
        set(handles.prumer2,'String',pomoc2)
     elseif p_pohled == 1
            axes(handles.graf_parametru)
            set(gca,'xtick',[],'ytick',[])
            plot(o,doba_p)
            ylim([0 max(doba_p)+0.02])
            xlim([0 length(doba_p)]);
            set(gca,'xtick',[])
            xlabel('Doba pohledu na určité místo [s]')
            hold on
            stem(o(i),doba_p(i),'filled','Color','r')
            drawnow
            hold off
     elseif p_rychlost == 1
            axes(handles.graf_parametru)
            plot(p_vykr,v)
            xlim([[0,delka]])
            ylim([-10,limit_v])
            set(gca,'xtick',[],'ytick',[])
            xlabel('Rychlost pohybu očí')
            hold on
            stem(p_vykr(i),v(i),'filled','Color','r')
            drawnow
            hold off
     end
    if stop_tl == 1
        break;
    else
        continue;
    end
    end
 end
 
end

% --- Executes on button press in tecka.
function tecka_Callback(hObject, eventdata, handles)
% hObject    handle to tecka (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of tecka
set(handles.spustit,'Visible','On');


% --- Executes on button press in prekryv.
function prekryv_Callback(hObject, eventdata, handles)
% hObject    handle to prekryv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of prekryv
set(handles.spustit,'Visible','On');
global mov
global x
global y
global mov_pomocna 
mov_pomocna = mov;
% x = x./2.25;
% y = y./2.25;

global obr
global obr2
global obr_puv
% global obr_sedy
% 
for i = 1:6 %pro 1. 15 snímků pouze černobílý obraz
    obr = mov(i).cdata;
        obr_puv = rgb2gray(obr); 
        obr(:,:,1) = obr_puv;
        obr(:,:,2) = obr_puv;
        obr(:,:,3) = obr_puv;
    mov_pomocna(i).cdata = obr;
end

for i = 7:(length(x)-6)
    obr = mov(i).cdata;
o_x = round(x((i-6):(i+6))); %vybrání doby půl s před a po (15 vzorků +/-)
max_x = max(o_x);
if max_x > 854; %pokud by hodnota byla vyšší (proband se díval až za obrazovku)
    max_x = 854;
end
min_x = min(o_x);
if min_x <= 0
    min_x =1;
end
oblast_x = [min_x:max_x]; 

o_y = round(y((i-6):(i+6)));
max_y = max(o_y);
min_y = min(o_y);
if max_y >480
    max_y = 480;
end
if min_y <= 0
    min_y =1;
end
oblast_y = [min_y:max_y];

obr2 = obr(oblast_y,oblast_x,:);
obr2 = imadjust(obr2,[.1 .2 .1; .7 .7 0.8],[]); %barevná zvyrazněná část

obr_puv = rgb2gray(obr); %původní v šedém tonu
obr_puv = obr_puv - 20; %změna jasu šedého obrazu
obr(:,:,1) = obr_puv;
obr(:,:,2) = obr_puv;
obr(:,:,3) = obr_puv;
obr(oblast_y,oblast_x,:)=obr2; %nahrazení zírané oblasti barevným polem
    
mov_pomocna(i).cdata = obr;
end

% --- Executes on button press in video1.
function video1_Callback(hObject, eventdata, handles)
% hObject    handle to video1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of video1
addpath('D:\semestrální projekt\uložení a vykreslení eyetribe');
addpath('D:\semestrální projekt\vykreslení videa\Videa');
a = load('sit1_854_strih-na_interpolaci(tecky_ve_predu_i_vzadu).mat');
% a = load('situace1_854strih.mat');
global mov
mov = a.mov;
global nFrames
nFrames = a.nFrames;
assignin('base','mov',mov);
assignin('base','nFrames',nFrames);

global pupil_leveoko
global pupil_praveoko

%%% odstranění mrknutí %%%
pupil_leveoko = [1 pupil_leveoko];
pupil_praveoko = [1 pupil_praveoko];

for i =2:length(pupil_praveoko)
    if pupil_praveoko(i)==1
       pupil_praveoko(i) = pupil_praveoko(i-1);
    end
end


for i =2:length(pupil_leveoko)
    if pupil_leveoko(i)==1
        pupil_leveoko(i) = pupil_leveoko(i-1);
    end
end

pupil_leveoko(1) = [];
pupil_praveoko(1) = [];

% --- Executes on button press in video2.
function video2_Callback(hObject, eventdata, handles)
% hObject    handle to video2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of video2
addpath('D:\semestrální projekt\uložení a vykreslení eyetribe');
addpath('D:\semestrální projekt\vykreslení videa\Videa');
% load('video2_srazka.mat');
a = load('sit2_854_strih-na_interpolaci(tecky_ve_predu_i_v_vzadu).mat');
global nFrames
nFrames = a.nFrames;
global mov
mov = a.mov;
assignin('base','mov',mov);
assignin('base','nFrames',nFrames);

global pupil_leveoko
global pupil_praveoko

%%% odstranění mrknutí %%%
pupil_leveoko = [1 pupil_leveoko];
pupil_praveoko = [1 pupil_praveoko];

for i =2:length(pupil_praveoko)
    if pupil_praveoko(i)==1
       pupil_praveoko(i) = pupil_praveoko(i-1);
    end
end


for i =2:length(pupil_leveoko)
    if pupil_leveoko(i)==1
        pupil_leveoko(i) = pupil_leveoko(i-1);
    end
end

pupil_leveoko(1) = [];
pupil_praveoko(1) = [];
