Exemple #1
0
        // ------------------------------------------------------- @HoverHeight
        // -- Animate the Height of target element to desired value on hover --
        // --------------------------------------------------------------------
        public static void HoverHeight(this VisualElement target, float initialHeight = 0f, float desiredHeight = 100f, int duration = 1000, Action hoverCallback = null,
                                       Action leaveCallback = null,
                                       bool afterAnimation  = false)
        {
            initialHeight = initialHeight == 0f ? target.resolvedStyle.height : initialHeight;
            var enterAnim = new ValueAnimation <StyleValues>();
            var leaveAnim = new ValueAnimation <StyleValues>();

            void MouseEnter()
            {
                enterAnim = afterAnimation
                    ? target.AnimateHeight(initialHeight, desiredHeight, duration, hoverCallback)
                    : target.AnimateHeight(initialHeight, desiredHeight, duration);
            } // @formatter:off

            void MouseLeave()
            {
                leaveAnim = target.AnimateHeight(desiredHeight, initialHeight, duration);
            }                                                                                               // @formatter:on

            target.RegisterCallback <MouseOverEvent>(evt =>
            {
                if (enterAnim.isRunning)
                {
                    enterAnim.Stop();
                }
                if (leaveAnim.isRunning)
                {
                    leaveAnim.Stop();
                }
                MouseEnter();
                if (!afterAnimation)
                {
                    hoverCallback?.Invoke();
                }
                evt.StopPropagation();
            });

            target.RegisterCallback <MouseLeaveEvent>(evt =>
            {
                if (enterAnim.isRunning)
                {
                    enterAnim.Stop();
                }
                if (leaveAnim.isRunning)
                {
                    leaveAnim.Stop();
                }
                MouseLeave();
                evt.StopPropagation();
                target.schedule.Execute(leaveCallback).StartingIn(duration);
            });
        }