Improved Tooltip System

Have fun with another preview of 1.0.4 !

Todays topic: Tooltips

Here’s a snippet of the changelog:

“Desktop.TooltipControl is now of type Tooltip.
Inherit the abstract Tooltip class to create your own Tooltip behaviour.
Tooltip inherits Frame. Override the SetContext method to receive the current tooltip context.
Several Desktop properties wered moved into this new class.
The TooltipControl defaults to a SimpleTooltip, which is an autosized label.”
 

I think this sums it up pretty well. This change allows you to create Tooltip controls of arbitrary complexity. You can nest controls, animate them, position/align the whole salad where you want, or set AutoLayout to true and have the baseclass handle it for you.

This is important to understand:
In order to use your Tooltip control, you assign it to Desktop.TooltipControl.
This control is dynamically added/removed to Desktop.Elements, based on its Visible property.
This ensures it always stays on top of all other controls.

Note how i use the properties Delay, Opacity and Visible in the SimpleTooltip sourcecode below:

    // Simple text tooltip that fades in and out
    public class SimpleTooltip : Tooltip
    {
        private int FadeDirection = 1; // used to fade in&out
        private float DelayTimer; // timer to keep track of delay

        // label to display text
        public Label Label { get; private set; }

        // time (ms) it takes to fade in&out
        public float FadeDuration { get; set; }

        // time (ms) it takes before the tooltip fades in.
        // Delay only applies when the tooltip is completely faded out.
        public float Delay { get; set; }

        public SimpleTooltip()
        {
            Delay = 500f; // 0.5 seconds delay
            FadeDuration = 500f; // 0.5 seconds fade
            Opacity = 0; // start transparent
            AutoSize = AutoSize.HorizontalVertical;

            // lets just use a Label to display the tooltip as simple text
            Label = new Label();
            Label.BBCodeEnabled = true;
            Label.AutoSize = AutoSize.HorizontalVertical;
            Label.Style = "tooltip";
            Controls.Add(Label);
        }

        // Gets called when the tooltip context is updated.
        // This occurs when the Hot control changes.
        public override void SetContext(Control context)
        {
            // if there's no tooltip context
            if (context == null || string.IsNullOrEmpty(context.Tooltip))
            {
                // fade out
                FadeDirection = -1;
            }
            else
            {
                // grab the tooltip text
                Label.Text = context.Tooltip;

                // make the control visible
                Visible = true;

                // fade in
                FadeDirection = 1;
            }
        }

        protected override void OnUpdate()
        {
            // increment timer if delay isnt reached
            if(DelayTimer < Delay) DelayTimer += GuiHost.TimeElapsed;                          // if delay is reached             if (DelayTimer >= Delay)
            {
                // fade Opacity in/out over Duration depending on FadeDirection
                Opacity += (GuiHost.TimeElapsed / FadeDuration) * FadeDirection;

                // clamp between 0 and 1
                Opacity = Opacity < 0 ? 0 : (Opacity > 1 ? 1 : Opacity);
            }

            // make the control invisible when completely faded out
            if (FadeDirection < 0 && Opacity == 0)
            {
                Visible = false;
                DelayTimer = 0;
            }
        }
    }
Posted in SQUID

Leave a Reply

Your email address will not be published.