function varargout = config_panel(varargin)
% CONFIG_PANEL MATLAB code for config_panel.fig
% Ozkan Sertlek, 2015, University of Leiden


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @config_panel_OpeningFcn, ...
                   'gui_OutputFcn',  @config_panel_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


% --- Executes just before config_panel is made visible.
function config_panel_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for config_panel
whitebg('black')
rgb = imread('agora_logo_notional.png');
whitebg('black')
image(rgb); 
axis off          % Remove axis ticks and numbers
axis image 
set(gcf,'Toolbar','none','Menubar','none');
handles.output = hObject;
def_env=load('environment.ain');
set(handles.duration_edit,'String',num2str(def_env(1)))
set(handles.temp_edit,'String',num2str(def_env(2)))
set(handles.density_Edit,'String',num2str(def_env(3)))
set(handles.sound_edit,'String',num2str(def_env(4)))
set(handles.tao_edit,'String',num2str(def_env(5)))
set(handles.beta_Edit,'String',num2str(def_env(6)))
set(handles.eta_Edit,'String',num2str(def_env(7)))
set(handles.k_edit,'String',num2str(def_env(8)))
set(handles.dt_Edit,'String',num2str(def_env(9)))
handles.def_env=def_env;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes config_panel wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = config_panel_OutputFcn(hObject, eventdata, handles) 
% Get default command line output from handles structure
varargout{1} = handles.output;



function duration_edit_Callback(hObject, eventdata, handles)
TimeMax= str2double(get(hObject, 'String'));
if isnan(TimeMax)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.TimeMax = TimeMax;
handles.def_env(1)=TimeMax;

guidata(hObject,handles)



% --- Executes during object creation, after setting all properties.
function duration_edit_CreateFcn(hObject, eventdata, handles)
% 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 temp_edit_Callback(hObject, eventdata, handles)
T= str2double(get(hObject, 'String'));
if isnan(T)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.T = T;
handles.def_env(2)=T;

guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function temp_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to temp_edit (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 density_Edit_Callback(hObject, eventdata, handles)
rho_Water= str2double(get(hObject, 'String'));
if isnan(rho_Water)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.rho_Water = rho_Water;
handles.def_env(3)=rho_Water;

guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function density_Edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to density_Edit (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 sound_edit_Callback(hObject, eventdata, handles)
c_Water= str2double(get(hObject, 'String'));
if isnan(c_Water)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.c_Water = c_Water;
handles.def_env(4)=c_Water;

guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function sound_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sound_edit (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 tao_edit_Callback(hObject, eventdata, handles)
tao= str2double(get(hObject, 'String'));
if isnan(tao)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.tao = tao;
handles.def_env(5)=tao;

guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function tao_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tao_edit (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 beta_Edit_Callback(hObject, eventdata, handles)
beta= str2double(get(hObject, 'String'));
if isnan(beta)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.beta = beta;
handles.def_env(6)=beta;

guidata(hObject,handles)

function beta_Edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to beta_Edit (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 eta_Edit_Callback(hObject, eventdata, handles)
eta= str2double(get(hObject, 'String'));
if isnan(eta)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.eta = eta;
handles.def_env(7)=eta;
guidata(hObject,handles)

function eta_Edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eta_Edit (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 k_edit_Callback(hObject, eventdata, handles)
k= str2double(get(hObject, 'String'));
if isnan(k)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.k = k;
handles.def_env(8)=k;
guidata(hObject,handles)

function k_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to k_edit (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 dt_Edit_Callback(hObject, eventdata, handles)
dt= str2double(get(hObject, 'String'));
if isnan(dt)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.configparameter.dt = dt;
handles.def_env(9)=dt;


guidata(hObject,handles)

function dt_Edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dt_Edit (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 button press in loaddefault.
function loaddefault_Callback(hObject, eventdata, handles)
%close all;
choice = questdlg('Are you sure to load the deafult parameters?', ...
	'Load the default parameters?', ...
	'Yes,load the default parameters','No','No');
% Handle response
switch choice
    case 'Yes,load the default parameters'
        dos('copy environment_default.ain environment.ain') 
        config_panel
    case 'No'
        disp('Nothing is deleted...')
end




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


% --- Executes on button press in save_config.
function save_config_Callback(hObject, eventdata, handles)
save_env=handles.def_env;
format long
save('environment.ain','save_env','-ascii')
 
% hObject    handle to save_config (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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