%  Cellular automata software written in matlab
%  by Alain Vinet and Pierre Rocque (see MAINAUT1)

%   Copyright 2003
%   Centre for Nonlinear Dynamics in Physiology and Medicine

global nb_state_ex nb_state_arp
global dim_x dim_y
global etat
global threshold
global dim_protoc protocole
global h_main
global run_status stepping id_step id_protoc 
global mat_vois wei_vois
global cmap figcmap
%  run options
run_status='stop';

%stepping option
stepping=0; %flow, 1=step by step


%define state space, lattice, neighbours

nb_state_ex=2;
nb_state_arp=3;
dim_x=10;
dim_y=10;

% define as center + [ vect ]

mat_vois=[0 1;0 -1;1 0;-1 0];
wei_vois=ones(length(mat_vois(:,1)),1);


threshold=zeros(nb_state_ex+nb_state_arp+1,1);
threshold(1)=1;

% items du menu 

%*********************************************************************


%  run options



run_opt=uimenu(h_main,'label','run');
tog_run_start=uimenu(run_opt,'label','start','interruptible','yes',...
		    'callback','run_status=''start'';simau1;');
tog_run_continue=uimenu(run_opt,'label','continue','interruptible','yes',...
		    'callback','run_status=''continue'';simau1;');
tog_run_exit=uimenu(run_opt,'label','exit',...
		    'callback','close all, clear, clear global, return');


stop_opt=uimenu(h_main,'label','stop','call',['run_status=''stop'';']);

%**********************************************************************


%stepping option

%--------------


step_opt=uimenu(h_main,'label','stepping');
un_seul=uimenu(step_opt,'Label','step','callback','stepping=1;');
plusieurs=uimenu(step_opt,'Label','flow','callback','stepping=0;');




%********************************************************************

%define state space, lattice, neighbours
init_opt=uimenu(h_main,'label','define');

%A _ recall option


load_init_opt=uimenu(init_opt,'Label','load','callback','entre');


%B _ define state space





%------------------------------
% new value -> require new defenition of threshold (one is done by defaut




label_state=['excited   ';
             'refractory'];
nom_state=['nb_state_ex ';...
           'nb_state_arp'];
unit_state=['nb';'nb'];


state_opt=uimenu(init_opt,'Label','state space',...
'callback',['mat_state=[nb_state_ex;nb_state_arp];'...
           'h_act=crfigval(mat_state, label_state, unit_state,'...
			 'nom_state, ''state_space'');']);




%C lattice 
% etat is resettedd


label_lat=['horizontal';
            'vertical  '];
nom_lat=['dim_x';...
         'dim_y'];

unit_lat=['nb';'nb'];

lattice_opt=uimenu(init_opt,'Label','lattice',...
'callback',['mat_lat=[dim_x;dim_y];	'...
	    'if(isempty(mat_lat))mat_lat=[10;10];;end;'...
            'h_act=crfigval(mat_lat, label_lat, unit_lat,'...
			 'nom_lat, ''lattice'');']);



%D neighbours 


%---------------

neigh_opt=uimenu(init_opt,'Label','neighbourhood',...
'callback','crvoisin(9);');

%E threshold



threshold_opt=uimenu(init_opt,'Label','threshold ',...
'callback',['clear label_thr nom_thr unit_thr,',...
'label_thr=[''rest''];nom_thr=[''threshold(1)''];unit_thr=[''level''];',...
'for i=1:nb_state_ex,label_thr=str2mat(label_thr,[''ex '' int2str(i)]);end;',...
'for i=1:nb_state_arp,label_thr=str2mat(label_thr,[''rel.r. '' int2str(i)]);end;',...
'for i=2:nb_state_arp+nb_state_ex+1,nom_thr=str2mat(nom_thr,[''threshold('' int2str(i) '')'']);end;',...
'for i=2:nb_state_arp+nb_state_ex+1,unit_thr=str2mat(unit_thr,''level'');end;',...
'h_act=crfigval(threshold, label_thr, unit_thr,'...
			 'nom_thr, ''threshold(0:unexcitable)'');']);



%********************************************************************

%initialize : initial configuration
global etat etat_ass %pour initialization
%--------------------------------------- 
etat_ass=0;
etat=zeros(dim_x,dim_y);
define_opt=uimenu(h_main,'label','state');
def1_opt=uimenu(define_opt,'label','reset','call',...
         ['etat=zeros(dim_x,dim_y);',...
          'll=findobj(h_main,''type'',''patch'');'...
          'for i=1:length(ll), set(ll(i),''Cdata'',1);end']);


def1_opt=uimenu(define_opt,'label','modify',...
'callback',['if(isempty(dim_x))dim_x=10;end;',...
	    'if(isempty(dim_y))dim_y=10;end;',...
	    'if(isempty(nb_state_ex))nb_state_ex=2;end;',...
	    'if(isempty(nb_state_arp))nb_state_arp=3;end;',...
             'map=hot(nb_state_ex+nb_state_arp+1);',...
             'colormap(map);',...
	     'if(isempty(etat))etat=zeros(dim_x,dim_y);end;',...
	     'if(isempty(etat_ass))etat_ass=0;end;',...
	      'ed_res(etat,nb_state_ex+nb_state_arp+1,0,0);']);





%********************************************************************


%protocol : protocol of simulation

global dim_protoc protocole liste_stim liste_disc
%---------------------------------------------

protocol_opt=uimenu(h_main,'label','protocol');

dim_protoc=1;
protocole=zeros(5,1);
protocole(1,1)=1;
label_proc=['nb of stages'];
nom_proc=['dim_protoc'];
unit_proc=['nb'];

nbstep_opt=uimenu(protocol_opt,'Label','nb of stages',...
'callback',['if(isempty(dim_protoc))dim_protoc=1;protocole=zeros(5,1);protocole(1,1)=1;end;'...
	   'h_act=crfigval(dim_protoc, label_proc, unit_proc,'...
			 'nom_proc, ''protocol'');']);

initstep_opt=uimenu(protocol_opt,'Label','set',...
'callback',['init_pro']);





% save : save a simulation

save_opt=uimenu(h_main,'label','save');
act_save=uimenu(save_opt,'label','click','callback','ssim');


%trace 
global liste_tr pile_tr fig_trace id_tr
liste_tr=[];
fig_trace=[];
pile_tr=[];
trace_opt=uimenu(h_main,'label','trace');
act_plot=uimenu(trace_opt,'label','plot',...
   'call',['if(~isempty(liste_tr) | ~isempty(pile_tr) )',...
    'if(isempty(fig_trace))fig_trace=figure;'...
    'else figure(fig_trace);clf,end;'...
    'for i=1:length(pile_tr(1,:)),subplot(length(pile_tr(1,:)),1,i);',...
    'plot(pile_tr(:,i),''o''),hold on,plot(pile_tr(:,i)),hold off,set(gca,''ylim'',[-1 nb_state_ex+nb_state_arp+1]);'...
    'set(gca,''xlim'',[0 length(pile_tr(:,1))]);set(gca,''box'',''off'');'...
    'ylabel(''state''),set(gca,''ytick'',[0:nb_state_ex+nb_state_arp+1]);',...
    'title([''cell x: '' int2str(liste_tr(1,i)) '', y: '' int2str(liste_tr(2,i))]);end;'...
    'xlabel(''step'');end;']);

act_tr=uimenu(trace_opt,'label','choose cells',...
       'call',['if(isempty(dim_x))dim_x=10;end;',...
	       'if(isempty(dim_y))dim_y=10;end;',...
	       'mat_in=ones(dim_x,dim_y);',...
	        'if(~isempty(liste_tr))',...
		 'for j=1:length(liste_tr(1,:))',...
		 'mat_in(liste_tr(1,j),liste_tr(2,j))=0;',...
	         'end; end;',...
	         'ed_res(mat_in,2,3,0)']);


%colormap
%figcmap=[];
%cmap=[];
hcol=uimenu(h_main,'label','colormap','call','drawcmap');
