home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / GRAPHICS.DI$ / GINPUT.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  5.6 KB  |  168 lines

  1. function [out1,out2,out3] = ginput(arg1, arg2)
  2. %GINPUT    Graphical input from a mouse or cursor.
  3. %     [X,Y] = GINPUT(N) gets N points from the current axes and returns 
  4. %     the X- and Y-coordinates in length N vectors X and Y.  The cursor
  5. %     can be positioned using a mouse (or by using the Arrow Keys on some 
  6. %     systems).  Data points are entered by pressing a mouse button
  7. %     or any key on the keyboard.  A carriage return terminates the 
  8. %     input before N points are entered.
  9. %     [X,Y] = GINPUT gathers an unlimited number of points until the
  10. %     return key is pressed.
  11. %     [X,Y,BUTTON] = GINPUT(N) returns a third result, BUTTON, that 
  12. %     contains a vector of integers specifying which mouse button was
  13. %     used (1,2,3 from left) or ASCII numbers if a key on the keyboard
  14. %     was used.
  15.  
  16. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  17.  
  18. c = computer;
  19. if ~strcmp(c(1:2),'PC') & ~strcmp(c(1:2),'MA')
  20.     tp = get(0,'TerminalProtocol');
  21. else
  22.     tp = 'micro';
  23. end
  24.  
  25. if ~strcmp(tp,'none') & ~strcmp(tp,'x') & ~strcmp(tp,'micro'),
  26.     if nargout == 1,
  27.         if nargin == 1,
  28.             eval('out1 = trmginput(arg1);');
  29.         else
  30.             eval('out1 = trmginput(arg1,arg2);');
  31.         end
  32.     elseif nargout == 2,
  33.         if nargin == 1,
  34.             eval('[out1,out2] = trmginput(arg1);');
  35.         else
  36.             eval('[out1,out2] = trmginput(arg1,arg2);');
  37.         end
  38.     elseif nargout == 3,
  39.         if nargin == 1,
  40.             eval('[out1,out2,out3] = trmginput(arg1);');
  41.         else
  42.             eval('[out1,out2,out3] = trmginput(arg1,arg2);');
  43.         end
  44.     end
  45. else
  46.  
  47.     fig = gcf;
  48.     ax = gca;
  49.  
  50.     if nargin == 0
  51.         how_many = -1;
  52.         b = [];
  53.     else
  54.         how_many = arg1;
  55.         b = [];
  56.             if isstr(how_many), error('Requires a positive integer.'),end
  57.         if how_many == 0
  58.             ptr_fig = 0;
  59.             while(ptr_fig ~= fig)
  60.                 ptr_fig = get(0,'PointerWindow');
  61.             end
  62.             scrn_pt = get(0,'PointerLocation');
  63.             loc = get(fig,'Position');
  64.             pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)];
  65.             out1 = pt(1); y = pt(2);
  66.         elseif how_many < 0
  67.             error('Argument must be a positive integer.')
  68.         end
  69.     end
  70.  
  71.     pointer = get(gcf,'pointer');
  72.     set(gcf,'pointer','crosshair');
  73.     fig_units = get(fig,'units');
  74.     set(fig,'units','normalized');
  75.     axu = get(ax,'units');
  76.     set(ax,'units','normalized')
  77.     xlin = strcmp('linear',get(ax,'xscale'));
  78.     ylin = strcmp('linear',get(ax,'yscale'));
  79.     if nargin > 1
  80.         norm_rect = [0 0 1 1];
  81.         handle = fig;
  82.     else
  83.         norm_rect = get(ax,'Position');
  84.         handle = ax;
  85.         ydir = get(ax,'ydir');
  86.         ax_lim  = axis;
  87.         if ~xlin, ax_lim(1:2) = log(ax_lim(1:2)); end
  88.         if ~ylin, ax_lim(3:4) = log(ax_lim(3:4)); end
  89.         d_h = ax_lim(4) - ax_lim(3);
  90.         if strcmp(ydir,'reverse'), ax_lim(3) = ax_lim(4); d_h = -d_h; end
  91.         d_w = ax_lim(2) - ax_lim(1);
  92.     end
  93.     char = 0;
  94.  
  95.     while how_many ~= 0
  96.         keydown = waitforbuttonpress;
  97.         ptr_fig = get(0,'CurrentFigure');
  98.         if(ptr_fig == fig)
  99.             if keydown
  100.                 char = get(fig, 'CurrentCharacter');
  101.                 scrn_pt = get(0, 'PointerLocation');
  102.                 set(fig,'units','pixels')
  103.                 loc = get(fig, 'Position');
  104.                 set(fig,'units','normalized')
  105.                 pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)];
  106.                 ax_rect = [fix(norm_rect(1)*loc(3)),fix(norm_rect(2)*loc(4)),...
  107.                     fix(norm_rect(3)*loc(3)),fix(norm_rect(4)*loc(4))];
  108.                 pt(1) = (pt(1) - ax_rect(1))/ax_rect(3);
  109.                 pt(2) = (pt(2) - ax_rect(2))/ax_rect(4);
  110.                 if nargin < 2
  111.                     pt(1) = ax_lim(1) + pt(1) * d_w;
  112.                     pt(2) = ax_lim(3) + pt(2) * d_h;
  113.                     if ~xlin, pt(1) = exp(pt(1)); end
  114.                     if ~ylin, pt(2) = exp(pt(2)); end
  115.                 end
  116.                 button = abs(char);
  117.             else
  118.                 pt = get(handle, 'CurrentPoint');
  119.                 button = get(fig, 'SelectionType');
  120.                 if strcmp(button,'open')
  121.                     button = b(max(size(b)));
  122.                 elseif strcmp(button,'normal')
  123.                     button = 1;
  124.                 elseif strcmp(button,'alt')
  125.                     button = 2;
  126.                 elseif strcmp(button,'extend')
  127.                     button = 3;
  128.                 else
  129.                     error('Invalid mouse selection.')
  130.                 end
  131.             end
  132.  
  133.             how_many = how_many - 1;
  134.  
  135.             if(char == 13 & how_many ~= 0)
  136.                 % if the return key was pressed, char will == 13,
  137.                 % and that's our signal to break out of here whether
  138.                 % or not we have collected all the requested data
  139.                 % points.  
  140.                 % If this was an early breakout, don't include
  141.                 % the <Return> key info in the return arrays.
  142.                 % If this was the last expected event, then do
  143.                 % include the <Return> key info.  This is
  144.                 % exactly how 3.5 does it, and therefore its
  145.                 % how 4.0 is going to do it...
  146.                 break;
  147.             end
  148.  
  149.             out1 = [out1;pt(1,1)];
  150.             y = [y;pt(1,2)];
  151.             b = [b;button];
  152.         end
  153.     end
  154.  
  155.     if nargout > 1
  156.         out2 = y;
  157.         if nargout > 2
  158.             out3 = b;
  159.         end
  160.     else
  161.         out1 = [out1 y];
  162.     end
  163.     set(fig,'pointer',pointer,'units',fig_units);
  164.     set(ax,'units',axu)
  165. end
  166.