function varargout = pomocivlc(varargin)
% POMOCIVLC MATLAB code for pomocivlc.fig
%      POMOCIVLC, by itself, creates a new POMOCIVLC or raises the existing
%      singleton*.
%
%      H = POMOCIVLC returns the handle to a new POMOCIVLC or the handle to
%      the existing singleton*.
%
%      POMOCIVLC('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in POMOCIVLC.M with the given input arguments.
%
%      POMOCIVLC('Property','Value',...) creates a new POMOCIVLC or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pomocivlc_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pomocivlc_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 pomocivlc

% Last Modified by GUIDE v2.5 04-May-2018 16:35:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pomocivlc_OpeningFcn, ...
                   'gui_OutputFcn',  @pomocivlc_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 pomocivlc is made visible.
function pomocivlc_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 pomocivlc (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
movegui(gcf,'south')
% UIWAIT makes pomocivlc wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = pomocivlc_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 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)

% system(' "C:\Program Files (x86)\VideoLAN\VLC\VLC.exe" 1280x720.mp4') %přehrání videa

addpath('D:\semestrální projekt\vykreslení videa\Videa')
addpath('D:\semestrální projekt');
addpath('D:\Programy\Matlab R2015a\Mathworks Matlab R2015a (64-Bit)-[FirstUploads]\jsonlab-master')
addpath('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\EyeTribe_for_Matlab')

global delka
global datum
datum = datetime('today'); 
datum = string(datum);
global mereni
% mereni = [datum,'_ja.txt'];


%%% pokud je vybrána situace 1
global sit1
sit1 = handles.situace1;
sit1 = get(sit1,'Value');
assignin('base','hh',sit1);

%%% pokud je vybrána situace 2
global sit2
sit2 = handles.situace2;
sit2 = get(sit2,'Value');
assignin('base','gg',sit2);

if sit1 == 0 && sit2 == 0
    errordlg('Vyberte si prosím situaci, kterou chcete přehrát.');
else
tet = eyetribe();
tet.connect(); %připojení eyetribu
pause(8) %pauza pro načtení všech potřebých dat 
end

if sit1==1
fig1 = figure('position', [1 1 1920 1080]);
handles.activex1.URL = 'sit1_1920-strih-na_interpolaci.mp4';
videoo = actxcontrol ('WMPlayer.OCX.7', [1 1 1550 850], fig1);
videoo.currentMedia = videoo.newMedia ('D:\semestrální projekt\vykreslení videa\Videa\sit1_1920-strih-na_interpolaci.mp4');
mereni = [datum,'_situace1_tatka.txt'];

elseif sit2==1
fig1 = figure('position', [1 1 1920 1080]) ;
handles.activex1.URL = 'video2.mp4';
video = actxcontrol ('WMPlayer.OCX.7', [1 1 1550 850], fig1);
video.currentMedia = video.newMedia ('D:\semestrální projekt\vykreslení videa\Videa\sit2_1920-strih-na_interpolaci.mp4');
mereni = [datum,'_situace2_tatka.txt'];
% else 
%     errordlg('Vyberte si prosím situaci, kterou chcete přehrát.');
end
if sit1 == 1 || sit2 == 1
tet.record_start(mereni);  %spuštění nahrávání pohybu očí
f = tet.frame;


global delka_osy_x  
delka_osy_x = tet.trackerProperties.values.screenresw;
global delka_osy_y
delka_osy_y = tet.trackerProperties.values.screenresh;
  
pause(delka) %déka nahrávání pohybu očí
tet.record_stop(); %zastavení nahrávání

tet.disconnect(); %odpojení eyetrackeru
  assignin('base','delka_osy_x',delka_osy_x);
  assignin('base','delka_osy_y',delka_osy_y);
end

% --- 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 mereni
F = readTETfile(mereni);
J = loadjson(F);

global prave_oko
prave_oko =[];
for i=1:length(J)
    if strcmp(J{i}.category,'tracker')
        prave_oko = [prave_oko; [J{i}.values.frame.time J{i}.values.frame.righteye.raw.x J{i}.values.frame.righteye.raw.y]];
    end
end

global leve_oko
leve_oko =[];
for i=1:length(J)
    if strcmp(J{i}.category,'tracker')
       leve_oko = [leve_oko; [J{i}.values.frame.time J{i}.values.frame.lefteye.raw.x J{i}.values.frame.lefteye.raw.y]];
    end
end

global prumer_pupily
prumer_pupily = [];
for i=1:length(J)
    if strcmp(J{i}.category,'tracker')
         prumer_pupily = [prumer_pupily; [J{i}.values.frame.time J{i}.values.frame.righteye.psize J{i}.values.frame.lefteye.psize]];
    end
end

global x_leveoko
global y_leveoko
global x_praveoko
global y_praveoko
global pupil_leveoko
global pupil_praveoko

%velikosti zornic
pupil_leveoko = prumer_pupily(:,3)';
pupil_praveoko = prumer_pupily(:,2)';

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

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


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

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

% %%% zjištění x souřadnic
% x = gazexy(:,2)';
% 
% %%% y
% y = gazexy(:,3)';
x_leveoko = leve_oko(:,2)';
y_leveoko = leve_oko(:,3)';

x_praveoko = prave_oko(:,2)';
y_praveoko = prave_oko(:,3)';

%odstranění nulovýh vzorků (výpadky/mrknutí)
x_leveoko = [1 x_leveoko];
x_praveoko = [1 x_praveoko];
y_leveoko = [1 y_leveoko];
y_praveoko = [1 y_praveoko];

for i =1:length(x_praveoko)
    if x_praveoko(i)==0
        x_praveoko(i) = x_praveoko(i-1);
    end
end


for i =1:length(y_praveoko)
    if y_praveoko(i)==0
        y_praveoko(i) = y_praveoko(i-1);
    end
end


for i =1:length(x_leveoko)
    if x_leveoko(i)==0
        x_leveoko(i) = x_leveoko(i-1);
    end
end

for i =1:length(y_leveoko)
    if y_leveoko(i)==0
        y_leveoko(i) = y_leveoko(i-1);
    end
end
x_leveoko(1) = [];
x_praveoko(1) = [];
y_leveoko(1) = [];
y_praveoko(1) = [];
x = [1];
y = [1];

for i = 2:length(x_leveoko+1)
    pom = abs(x_leveoko(i) - x_praveoko(i));
    pom2 = abs(y_leveoko(i) - y_praveoko(i));
    if pom<30 || pom2<30
        s1 = (x_leveoko(i) + x_praveoko(i))/2;
        x = [x s1];
        s2 = (y_leveoko(i) + y_praveoko(i))/2;
        y = [y s2];
    else
        s1 = x(i-1);
        s2 = y(i-1);
        x = [x s1];
        y = [y s2];
    end
assignin('base','x',x);
assignin('base','y',y);
end

figure(1)

for i = 1:length(x)
    plot(x(i),y(i),'o','MarkerSize',pupil_leveoko(i))

        xlim([0 1536])
        ylim([0 864])
    set(gca,'YDir','reverse');
    drawnow
    pause(0.06)
end


% --- Executes on button press in situace1.
function situace1_Callback(hObject, eventdata, handles)
% hObject    handle to situace1 (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 situace1
global delka
delka = 55;

% --- Executes on button press in situace2.
function situace2_Callback(hObject, eventdata, handles)
% hObject    handle to situace2 (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 situace2
global delka 
delka = 32; 
