%A very simple function plotter to demo GUI building:
%--pushbuttons
%--static/editable text fields
%--drawing axes
%--This version uses an explicit event-loop to check for
%   user actions

clf
clear all

%define the plot button
plotbutton=uicontrol('style','pushbutton',...
   'string','Connect', ...
   'fontsize',12, ...
   'position',[100,400,60,20], ...
   'callback', 'drawit=1;');

%define the erase button
erasebutton=uicontrol('style','pushbutton',...
   'string','erase', ...
   'fontsize',12, ...
   'position',[200,400,50,20], ...
   'callback','clrit=1;');

%define the function editable text field
colorfield=uicontrol('style','edit',...
   'string','green', ...
   'fontsize',12, ...
   'position',[220,1,100,20]...
   );

%define the x range editable text field
numptsedit=uicontrol('style','edit',...
   'string','10', ...
   'fontsize',12, ...
   'position',[100,1,100,20] ...
      );

%define some static text
uicontrol('style','text',...
   'string','Num Pts=',...
   'fontsize',12, ...
   'position',[40,1,60,20]);

%define the erase button
quitbutton=uicontrol('style','pushbutton',...
   'string','Quit', ...
   'fontsize',12, ...
   'position',[500,400,50,20], ...
   'callback','stopit=1;close;');

%define the capture button
capturebutton=uicontrol('style','pushbutton',...
   'string','Capture', ...
   'fontsize',12, ...
   'position',[300,400,50,20], ...
   'callback','capit=1;');

%define the save button
capturebutton=uicontrol('style','pushbutton',...
   'string','Save...', ...
   'fontsize',12, ...
   'position',[400,400,50,20], ...
   'callback','saveit=1;');

%define the setcolor button
colorbutton=uicontrol('style','pushbutton',...
   'string','Set Bkgnd color...', ...
   'fontsize',12, ...
   'position',[350,1,130,20], ...
   'callback','setbkgnd=1;');

%make some axes
axeshandle = axes(...
   'position',[.2 .2 .7 .7],...
   'xlim',[-1 1],...
   'ylim',[-1 1]);

%Main event loop
stopit=0; %wait for a quit button push
drawit=0; %wait for a draw 
clrit =0; %wait for a clear
capit =0; %wait for a capture command
setbkgnd=0;
saveit=0;

%null out x and y
x=[];y=[];
bkgndcolor = [1,1,1];

while (stopit==0)
   drawnow; %need this in the loop for controls to work
   
   if drawit==1
      drawit=0;
      if (isempty(x))
         errordlg('You MUST capture before connecting');
      else
         plot(x,y,get(colorfield,'string'));
         axis([-1 1 -1 1]);
         set(axeshandle,'color',bkgndcolor);
         drawnow;
      end
   end
   
   if clrit==1
      clrit=0;
      cla;
   end
   
   if capit==1
      capit=0;
      numpts = str2num(get(numptsedit,'string'));
      if ( isempty(numpts))
         errordlg('NumPts MUST be a number');
      elseif (numpts<1)
         errordlg('NumPts MUST be >=1');
      else
         clear x y
         for i=1:numpts
            [x(i),y(i)]=ginput(1);
            plot(x(i),y(i),'kx');
            hold on;
            set(axeshandle,'color',bkgndcolor);
            axis([-1 1 -1 1]);
         end
      end  
   end
   
   if setbkgnd==1
      setbkgnd=0;
      bkgndcolor = uisetcolor;
      if bkgndcolor==0
      else
         set(axeshandle,'color',bkgndcolor);
      end  
   end  
   
   if saveit==1
      saveit=0;
      if (isempty(x))
         errordlg('You MUST capture before saving');
      else
         [filename,pathname]=uiputfile;
         save (strcat(pathname,filename), 'x', 'y', '-ascii');
      end
   end
end
