classdef polariMBGeneral < internal.polariMouseBehavior
    
    methods
        function obj = polariMBGeneral
            obj@internal.polariMouseBehavior( ...
                @showToolTipAndPtr_default, ...
                @internal.polariMBGeneral.wbmotion, ...
                @internal.polariMBGeneral.wbdown, ...
                [], ...
                @internal.polariMBMagTicks.wbscroll);
        end
    end
    
    methods (Static)
        function wbmotion(p,ev)
            % General button motion over figure
            %
            % Install mouse handlers and pointer based on mouse event
            % struct EV
            
            % Check on a pending deferred change.  We might add a
            % pending change when graphically editing title text.  We
            % service this request when mouse next moves.
            executeDelayedParamChanges(p);
            
            s = computeHoverLocation(p,ev);
            autoChangeMouseBehavior(p,s);
            
            % Show reminder to user that this is an interactive UI
            %
            % Only show this message once per instance of polarpattern.
            %
            % Only show this when the mouse first enters the UI, so that
            % the banner won't display for "screen captures", especially
            % when publishing examples or other user-facing snapshots.
            % Adding a gating switch so that we can turn it off.
            if ~p.pShownInteractiveBehaviorBanner
                ena = getappdata(0,'polarpatternSuppressInitBanner');
                if isempty(ena) || ena
                    p.pShownInteractiveBehaviorBanner = true;
                    if isDataClean(p)
                        showBannerMessage(p,'The data contains NaN or -Inf. Right click to clean data and interact with the plot');
                    else
                        showBannerMessage(p,'Right click to interact with the plot');
                        hc = p.UIContextMenu_Master;
                        h2 = hc.findobj('Label','Clean Data','-depth',1);
                        h2.Visible = internal.LogicalToOnOff(0);
                    end
                end
            end
        end
        
        function wbdown(p,ev)
            % Handle button-down when mouse is not over anything else
            % categorized by mouse handler.
            %
            % Particular use:
            % - double-click on grid for new cursor
            % - double-click above/below axis for new titles
            
            p.hFigure.CurrentPoint = ev.Point;
            st = p.hFigure.SelectionType;
            if strcmpi(st,'open')
                % Double-click
                
                s = computeHoverLocation(p,ev);
                
                % Figure out where the mouse is -- NOT what fig/axes is
                % associated with p.  That may be different.
                thisFig = ev.Source;
                thisAxes = thisFig.CurrentAxes;
                
                % Check that this polari instance is rendered in the
                % current axis, otherwise we reject the mouse click:
                %
                if p.pAxesIndex == getappdata(thisAxes,'PolariAxesIndex')
                    if s.overGrid
                        % Double-click within grid region
                        %  - add a cursor:
                        m_addCursor(p);
                        return
                    end
                    
                    if ~s.any
                        % Double-click on "nothing"
                        %  - within the HG axes
                        %  - possibly within the polari circle
                        %
                        % If the click is "roughly above" or "roughly below"
                        % centerline of axis, we add NEW title strings.
                        %
                        % Radius must be > 1. If not, we're still within
                        % the unit circle.
                        pt = p.hAxes.CurrentPoint(1,1:2);
                        if norm(pt) > 1
                            % Check NORMALIZED angle (ccw, 0=right):
                            %   55 to  90+55 deg -> top
                            %  -55 to -90-55 deg -> bottom
                            ang = atan2d(pt(2),pt(1));
                            if isempty(p.TitleTop) ...
                                    && ang>=55 && ang<=145
                                p.TitleTop = p.NewTitleString;
                                changeMouseBehavior(p,'titletop');
                            elseif isempty(p.TitleBottom) ...
                                    && ang<=-55 && ang>=-145
                                p.TitleBottom = p.NewTitleString;
                                changeMouseBehavior(p,'titlebottom');
                            end
                        end
                    end
                end
            end
        end
    end
end
