function buttonUp(src, evnt)
% buttonUp
%   button up callback

%   Project: IMPACT
%   Title of Code: buttonUp
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

handles = guidata(src);
hAx = gca;
pt = get(hAx, 'CurrentPoint');
ptX = pt(1,1);
ptY = pt(1,2);

hMag = handles.hMag;
hPhase = handles.hPhase;


%% freeze or best fit
% xlim
xLim = get(hMag(1), 'XLim');
% mag ylim
yLimMag = get(hMag(1), 'YLim');
% phase ylim
yLimPhase = get(hPhase(1), 'YLim');


str = handles.Tb.selected;
switch str     
    case 'arrow'
    case {'pole', 'zero', 'comPole', 'comZero', 'eraser'}
    case 'zoomIn'
        ZoomIn = handles.Tb.ZoomIn;
        if strcmp(ZoomIn.status, 'off')
            return;
        end
        hLine = ZoomIn.hLine;
        set(hLine, 'XData', [], 'YData', []);
        
        upPoint = [ptX, ptY];
        ZoomIn.upPoint = upPoint;
        downPoint = handles.Tb.ZoomIn.downPoint;
             
        % set x, y limit
        if isequal(upPoint, downPoint) % no rectangular
            zoomInOne(gca, upPoint(1));
        else
            zoomInTwo(gca, downPoint, upPoint, handles.Data.w, [hMag(1), hPhase(1)]);
        end
        ZoomIn.status = 'off';
        handles.Tb.ZoomIn = ZoomIn;
        guidata(handles.hFig, handles);
    case 'zoomOut'
        ZoomOut = handles.Tb.ZoomOut;
        upPoint = [ptX, ptY];
        ZoomOut.upPoint = upPoint;

        % set x, y limit
        zoomOutOne(gca, upPoint(1), handles.Data.w);
    case 'hand'
        setptr(handles.hFig, 'hand');
        handles.Tb.Hand.status = 'off';
        Hand = handles.Tb.Hand;
        Hand.zpk = '';
        handles.Tb.Hand = Hand;
        guidata(handles.hFig, handles);
        
        set(handles.hMag(1), 'YLimMode', 'auto');
        changeZPK(Hand, [ptX, ptY], handles);
end

%% freeze or best fit
if strcmpi(get(handles.hUimenu.Axes.freeze, 'Checked'), 'on')
    % freeze
    set(hMag(1), 'xLim', xLim, 'yLim', yLimMag);
    set(hPhase(1), 'xLim', xLim, 'yLim', yLimPhase);
end


function zoomInOne(hAxes, ptX)
% zoom in x lim for one click
% x lim is of log scale
% pt: point x value
% warning('off', 'MATLAB:log:logOfZero');
xLim = log10(get(hAxes, 'XLim'));
% warning('on', 'MATLAB:log:logOfZero');
ptX = log10(ptX);

rangeVal = xLim(2) - xLim(1);

if (ptX - rangeVal/4) < xLim(1)
    xLim(2) = xLim(1) + rangeVal/2;
elseif (ptX + rangeVal/4) > xLim(2)
    xLim(1) = xLim(2) - rangeVal/2;
else
    xLim(1) = ptX - rangeVal/4;
    xLim(2) = ptX + rangeVal/4;
end
set(hAxes, 'XLim', 10.^xLim);
set(hAxes, 'YLimMode', 'auto');


function zoomOutOne(hAxes, ptX, w)
% zoom out x lim for one click

% ptX: point x value
% x: original frequency range
% warning('off', 'MATLAB:log:logOfZero');
xLim = log10(get(hAxes, 'XLim'));
% warning('on', 'MATLAB:log:logOfZero');
ptX = log10(ptX);
w = log10(w);

rangeVal = xLim(2) - xLim(1);

xLim(1) = ptX - rangeVal;
xLim(2) = ptX + rangeVal;
if xLim(1) < w(1)
    xLim(1) = w(1);
    xLim(2) = xLim(1) + min(2*rangeVal, w(end) - w(1));
end
if xLim(2) > w(end)
    xLim(2) = w(end);
    xLim(1) = xLim(2) - min(2*rangeVal, w(end) - w(1));
end
set(hAxes, 'XLim', 10.^xLim);
set(hAxes, 'YLimMode', 'auto');


function zoomInTwo(hAxes, downPoint, upPoint, w, bothAxes)
% zoom in x, y lim for two points

minX = min([downPoint(1), upPoint(1)]);
maxX = max([downPoint(1), upPoint(1)]);

xLim(1) = max([minX, w(1)]);
xLim(2) = min([maxX, w(end)]);
set(hAxes, 'XLim', xLim);

% set y lim
yLim(1) = min([downPoint(2), upPoint(2)]);
yLim(2) = max([downPoint(2), upPoint(2)]);
set(hAxes, 'YLim', yLim);
if hAxes == bothAxes(1)
    set(bothAxes(2), 'YLimMode', 'auto')
else
    set(bothAxes(1), 'YLimMode', 'auto')
end






