protected bool CalculateFadeBorder(TextScrollEnum scrollMode, RectangleF textBox, HorizontalTextAlignEnum horzAlign, out Vector4 fadeBorder)
        {
            fadeBorder = new Vector4(0.0001f, 0.0001f, 0.0001f, 0.0001f);
            bool dofade = false;

            if (scrollMode == TextScrollEnum.Left || scrollMode == TextScrollEnum.Right)
            {
                fadeBorder.X = FADE_SIZE; // Fade on left edge
                fadeBorder.Z = FADE_SIZE; // Fade on right edge
                dofade       = true;
            }
            else if (_lastTextSize.Width > textBox.Width)
            {
                if (horzAlign == HorizontalTextAlignEnum.Right || horzAlign == HorizontalTextAlignEnum.Center)
                {
                    fadeBorder.X = FADE_SIZE; // Fade on left edge
                }
                if (horzAlign == HorizontalTextAlignEnum.Left || horzAlign == HorizontalTextAlignEnum.Center)
                {
                    fadeBorder.Z = FADE_SIZE; // Fade on right edge
                }
                dofade = true;
            }
            if (scrollMode == TextScrollEnum.Up || scrollMode == TextScrollEnum.Down)
            {
                fadeBorder.Y = FADE_SIZE; // Fade on top edge
                fadeBorder.W = FADE_SIZE; // Fade on bottom edge
                dofade       = true;
            }
            else if (_lastTextSize.Height > textBox.Height + _fontSize / 4)
            {
                fadeBorder.W = FADE_SIZE; // Fade on bottom edge
                dofade       = true;
            }
            return(dofade);
        }
Beispiel #2
0
 protected bool CalculateFadeBorder(TextScrollEnum scrollMode, RectangleF textBox, HorizontalTextAlignEnum horzAlign, out Vector4 fadeBorder)
 {
   fadeBorder = new Vector4(0.0001f, 0.0001f, 0.0001f, 0.0001f);
   bool dofade = false;
   if (scrollMode == TextScrollEnum.Left || scrollMode == TextScrollEnum.Right)
   {
     fadeBorder.X = FADE_SIZE; // Fade on left edge
     fadeBorder.Z = FADE_SIZE; // Fade on right edge
     dofade = true;
   }
   else if (_lastTextSize.Width > textBox.Width)
   {
     if (horzAlign == HorizontalTextAlignEnum.Right || horzAlign == HorizontalTextAlignEnum.Center)
       fadeBorder.X = FADE_SIZE; // Fade on left edge
     if (horzAlign == HorizontalTextAlignEnum.Left || horzAlign == HorizontalTextAlignEnum.Center)
       fadeBorder.Z = FADE_SIZE; // Fade on right edge
     dofade = true;
   }
   if (scrollMode == TextScrollEnum.Up || scrollMode == TextScrollEnum.Down)
   {
     fadeBorder.Y = FADE_SIZE; // Fade on top edge
     fadeBorder.W = FADE_SIZE; // Fade on bottom edge
     dofade = true;
   }
   else if (_lastTextSize.Height > textBox.Height + _fontSize / 4)
   {
     fadeBorder.W = FADE_SIZE; // Fade on bottom edge
     dofade = true;
   }
   return dofade;
 }
Beispiel #3
0
    protected TextScrollEnum UpdateScrollPosition(RectangleF textBox, TextScrollEnum mode, float speed, float scrollDelay)
    {
      if ((SkinContext.FrameRenderingStartTime - _scrollInitialized).TotalSeconds < scrollDelay)
        return TextScrollEnum.None;

      float dif = speed * (float) SkinContext.FrameRenderingStartTime.Subtract(_lastTimeUsed).TotalSeconds;

      if (mode == TextScrollEnum.Auto)
      {
        if (_lastWrap && _lastTextSize.Height > textBox.Height)
          mode = TextScrollEnum.Up;
        else if (_textLines.Length == 1 && _lastTextSize.Width > textBox.Width)
          mode = TextScrollEnum.Left;
        else
          return TextScrollEnum.None;
      }

      switch (mode)
      {
        case TextScrollEnum.Left:
          _scrollPos.X -= dif;
          if (_scrollPos.X + _lastTextSize.Width < textBox.Width / 2.0f)
          {
            _scrollWrapOffset.X = _scrollPos.X;
            _scrollPos.X = textBox.Width + 4;
            _scrollWrapOffset.X -= _scrollPos.X;
          }
          else if (_scrollWrapOffset.X + _scrollPos.X + _lastTextSize.Width < 0.0f)
            _scrollWrapOffset.X = float.NaN;
          break;
        case TextScrollEnum.Right:
          _scrollPos.X += dif;
          if (_scrollPos.X > textBox.Width / 2.0f)
          {
            _scrollWrapOffset.X = _scrollPos.X;
            _scrollPos.X = -_lastTextSize.Width - 4;
            _scrollWrapOffset.X -= _scrollPos.X;
          }
          else if (_scrollWrapOffset.X + _scrollPos.X > textBox.Width)
            _scrollWrapOffset.X = float.NaN;
          break;
        case TextScrollEnum.Down:
          _scrollPos.Y += dif;
          if (_scrollPos.Y > textBox.Height / 2.0f)
          {
            _scrollWrapOffset.Y = _scrollPos.Y;
            _scrollPos.Y = -_lastTextSize.Height - 4;
            _scrollWrapOffset.Y -= _scrollPos.Y;
          }
          else if (_scrollWrapOffset.Y + _scrollPos.Y > textBox.Height)
            _scrollWrapOffset.Y = float.NaN;
          break;
        //case TextScrollEnum.Up:
        default:
          _scrollPos.Y -= dif;
          if (_scrollPos.Y + _lastTextSize.Height < textBox.Height / 2.0f)
          {
            _scrollWrapOffset.Y = _scrollPos.Y;
            _scrollPos.Y = textBox.Height + 4;
            _scrollWrapOffset.Y -= _scrollPos.Y;
          }
          else if (_scrollWrapOffset.Y + _scrollPos.Y + _lastTextSize.Height < 0.0f)
            _scrollWrapOffset.Y = float.NaN;
          break;
      }
      return mode;
    }
Beispiel #4
0
    /// <summary>
    /// Standard draw method for this text.
    /// </summary>
    /// <param name="textBox">The box where the text should be drawn.</param>
    /// <param name="horzAlignment">The horizontal alignment.</param>
    /// <param name="vertAlignment">The vertical alignment.</param>
    /// <param name="color">The color.</param>
    /// <param name="wrap">If <c>true</c> then text will be word-wrapped to fit the <paramref name="textBox"/>.</param>
    /// <param name="fade">If <c>true</c> then text will be faded at the edge of the <paramref name="textBox"/>.</param>
    /// <param name="zOrder">A value indicating the depth (and thus position in the visual heirachy) that this element should be rendered at.</param>
    /// <param name="scrollMode">Text scrolling behaviour.</param>
    /// <param name="scrollSpeed">Text scrolling speed in units (pixels at original skin size) per second.</param>
    /// <param name="scrollDelay">Text scrolling delay in seconds.</param>
    /// <param name="finalTransform">The final combined layout/render-transform.</param>
    public void Render(RectangleF textBox, HorizontalTextAlignEnum horzAlignment, VerticalTextAlignEnum vertAlignment, Color4 color,
        bool wrap, bool fade, float zOrder, TextScrollEnum scrollMode, float scrollSpeed, float scrollDelay, Matrix finalTransform)
    {
      if (!IsAllocated || wrap != _lastWrap || _textChanged || (wrap && textBox.Width != _lastTextBoxWidth))
      {
        Allocate(textBox.Width, wrap);
        if (!IsAllocated)
          return;
      }

      // Update scrolling
      TextScrollEnum actualScrollMode = scrollMode;
      if (scrollMode != TextScrollEnum.None && _lastTimeUsed != DateTime.MinValue)
        actualScrollMode = UpdateScrollPosition(textBox, scrollMode, scrollSpeed, scrollDelay);

      // Prepare horizontal alignment info for shader. X is position offset, Y is multiplyer for line width.
      Vector4 alignParam;
      switch (horzAlignment)
      {
        case HorizontalTextAlignEnum.Center:
          alignParam = new Vector4(textBox.Width / 2.0f, -0.5f, zOrder, 1.0f);
          break;
        case HorizontalTextAlignEnum.Right:
          alignParam = new Vector4(textBox.Width, -1.0f, zOrder, 1.0f);
          break;
        //case TextAlignEnum.Left:
        default:
          alignParam = new Vector4(0.0f, 0.0f, zOrder, 1.0f);
          break;
      }
      // Do vertical alignment by adjusting yPosition
      float yPosition = 0.0f;
      switch (vertAlignment)
      {
        case VerticalTextAlignEnum.Bottom:
          yPosition = Math.Max(textBox.Height - _lastTextSize.Height, 0.0f);
          break;
        case VerticalTextAlignEnum.Center:
          yPosition += Math.Max((textBox.Height - _lastTextSize.Height) / 2.0f, 0.0f);
          break;
        //case TextAlignEnum.Top:
        // Do nothing
      }

      // Do we need to add fading edges?
      Vector4 fadeBorder;
      if (fade && CalculateFadeBorder(actualScrollMode, textBox, horzAlignment, out fadeBorder))
      {
        _effect = ContentManager.Instance.GetEffect(EFFECT_FONT_FADE);
        _effect.Parameters[PARAM_FADE_BORDER] = fadeBorder;
      }
      else
        _effect = ContentManager.Instance.GetEffect(EFFECT_FONT);

      // Render
      _effect.Parameters[PARAM_COLOR] = color;
      _effect.Parameters[PARAM_ALIGNMENT] = alignParam;
      _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X, _scrollPos.Y + yPosition, 0.0f, 0.0f);
      _effect.Parameters[PARAM_TEXT_RECT] = new Vector4(textBox.Left, textBox.Top, textBox.Width, textBox.Height);
      DoRender(finalTransform);

      // Because text wraps around before it is complete scrolled off the screen we may need to render a second copy 
      // to create the desired wrapping effect
      if (scrollMode != TextScrollEnum.None)
      {
        if (!float.IsNaN(_scrollWrapOffset.X))
        {
          _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X + _scrollWrapOffset.X, _scrollPos.Y, 0.0f, 0.0f);
          DoRender(finalTransform);
        }
        else if (!float.IsNaN(_scrollWrapOffset.Y))
        {
          _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X, _scrollPos.Y + _scrollWrapOffset.Y, 0.0f, 0.0f);
          DoRender(finalTransform);
        }
      }
      _lastTimeUsed = SkinContext.FrameRenderingStartTime;
    }
        protected TextScrollEnum UpdateScrollPosition(RectangleF textBox, TextScrollEnum mode, float speed, float scrollDelay)
        {
            if ((SkinContext.FrameRenderingStartTime - _scrollInitialized).TotalSeconds < scrollDelay)
            {
                return(TextScrollEnum.None);
            }

            float dif = speed * (float)SkinContext.FrameRenderingStartTime.Subtract(_lastTimeUsed).TotalSeconds;

            if (mode == TextScrollEnum.Auto)
            {
                if (_lastWrap && _lastTextSize.Height > textBox.Height)
                {
                    mode = TextScrollEnum.Up;
                }
                else if (_textLines.Length == 1 && _lastTextSize.Width > textBox.Width)
                {
                    mode = TextScrollEnum.Left;
                }
                else
                {
                    return(TextScrollEnum.None);
                }
            }

            switch (mode)
            {
            case TextScrollEnum.Left:
                _scrollPos.X -= dif;
                if (_scrollPos.X + _lastTextSize.Width < textBox.Width / 2.0f)
                {
                    _scrollWrapOffset.X  = _scrollPos.X;
                    _scrollPos.X         = textBox.Width + 4;
                    _scrollWrapOffset.X -= _scrollPos.X;
                }
                else if (_scrollWrapOffset.X + _scrollPos.X + _lastTextSize.Width < 0.0f)
                {
                    _scrollWrapOffset.X = float.NaN;
                }
                break;

            case TextScrollEnum.Right:
                _scrollPos.X += dif;
                if (_scrollPos.X > textBox.Width / 2.0f)
                {
                    _scrollWrapOffset.X  = _scrollPos.X;
                    _scrollPos.X         = -_lastTextSize.Width - 4;
                    _scrollWrapOffset.X -= _scrollPos.X;
                }
                else if (_scrollWrapOffset.X + _scrollPos.X > textBox.Width)
                {
                    _scrollWrapOffset.X = float.NaN;
                }
                break;

            case TextScrollEnum.Down:
                _scrollPos.Y += dif;
                if (_scrollPos.Y > textBox.Height / 2.0f)
                {
                    _scrollWrapOffset.Y  = _scrollPos.Y;
                    _scrollPos.Y         = -_lastTextSize.Height - 4;
                    _scrollWrapOffset.Y -= _scrollPos.Y;
                }
                else if (_scrollWrapOffset.Y + _scrollPos.Y > textBox.Height)
                {
                    _scrollWrapOffset.Y = float.NaN;
                }
                break;

            //case TextScrollEnum.Up:
            default:
                _scrollPos.Y -= dif;
                if (_scrollPos.Y + _lastTextSize.Height < textBox.Height / 2.0f)
                {
                    _scrollWrapOffset.Y  = _scrollPos.Y;
                    _scrollPos.Y         = textBox.Height + 4;
                    _scrollWrapOffset.Y -= _scrollPos.Y;
                }
                else if (_scrollWrapOffset.Y + _scrollPos.Y + _lastTextSize.Height < 0.0f)
                {
                    _scrollWrapOffset.Y = float.NaN;
                }
                break;
            }
            return(mode);
        }
        /// <summary>
        /// Standard draw method for this text.
        /// </summary>
        /// <param name="textBox">The box where the text should be drawn.</param>
        /// <param name="horzAlignment">The horizontal alignment.</param>
        /// <param name="vertAlignment">The vertical alignment.</param>
        /// <param name="color">The color.</param>
        /// <param name="wrap">If <c>true</c> then text will be word-wrapped to fit the <paramref name="textBox"/>.</param>
        /// <param name="fade">If <c>true</c> then text will be faded at the edge of the <paramref name="textBox"/>.</param>
        /// <param name="zOrder">A value indicating the depth (and thus position in the visual heirachy) that this element should be rendered at.</param>
        /// <param name="scrollMode">Text scrolling behaviour.</param>
        /// <param name="scrollSpeed">Text scrolling speed in units (pixels at original skin size) per second.</param>
        /// <param name="scrollDelay">Text scrolling delay in seconds.</param>
        /// <param name="finalTransform">The final combined layout/render-transform.</param>
        public void Render(RectangleF textBox, HorizontalTextAlignEnum horzAlignment, VerticalTextAlignEnum vertAlignment, Color4 color,
                           bool wrap, bool fade, float zOrder, TextScrollEnum scrollMode, float scrollSpeed, float scrollDelay, Matrix finalTransform)
        {
            if (!IsAllocated || wrap != _lastWrap || _textChanged || (wrap && textBox.Width != _lastTextBoxWidth))
            {
                Allocate(textBox.Width, wrap);
                if (!IsAllocated)
                {
                    return;
                }
            }

            // Update scrolling
            TextScrollEnum actualScrollMode = scrollMode;

            if (scrollMode != TextScrollEnum.None && _lastTimeUsed != DateTime.MinValue)
            {
                actualScrollMode = UpdateScrollPosition(textBox, scrollMode, scrollSpeed, scrollDelay);
            }

            // Prepare horizontal alignment info for shader. X is position offset, Y is multiplyer for line width.
            Vector4 alignParam;

            switch (horzAlignment)
            {
            case HorizontalTextAlignEnum.Center:
                alignParam = new Vector4(textBox.Width / 2.0f, -0.5f, zOrder, 1.0f);
                break;

            case HorizontalTextAlignEnum.Right:
                alignParam = new Vector4(textBox.Width, -1.0f, zOrder, 1.0f);
                break;

            //case TextAlignEnum.Left:
            default:
                alignParam = new Vector4(0.0f, 0.0f, zOrder, 1.0f);
                break;
            }
            // Do vertical alignment by adjusting yPosition
            float yPosition = 0.0f;

            switch (vertAlignment)
            {
            case VerticalTextAlignEnum.Bottom:
                yPosition = Math.Max(textBox.Height - _lastTextSize.Height, 0.0f);
                break;

            case VerticalTextAlignEnum.Center:
                yPosition += Math.Max((textBox.Height - _lastTextSize.Height) / 2.0f, 0.0f);
                break;
                //case TextAlignEnum.Top:
                // Do nothing
            }

            // Do we need to add fading edges?
            Vector4 fadeBorder;

            if (fade && CalculateFadeBorder(actualScrollMode, textBox, horzAlignment, out fadeBorder))
            {
                _effect = ContentManager.Instance.GetEffect(EFFECT_FONT_FADE);
                _effect.Parameters[PARAM_FADE_BORDER] = fadeBorder;
            }
            else
            {
                _effect = ContentManager.Instance.GetEffect(EFFECT_FONT);
            }

            // Render
            _effect.Parameters[PARAM_COLOR]           = color;
            _effect.Parameters[PARAM_ALIGNMENT]       = alignParam;
            _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X, _scrollPos.Y + yPosition, 0.0f, 0.0f);
            _effect.Parameters[PARAM_TEXT_RECT]       = new Vector4(textBox.Left, textBox.Top, textBox.Width, textBox.Height);
            DoRender(finalTransform);

            // Because text wraps around before it is complete scrolled off the screen we may need to render a second copy
            // to create the desired wrapping effect
            if (scrollMode != TextScrollEnum.None)
            {
                if (!float.IsNaN(_scrollWrapOffset.X))
                {
                    _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X + _scrollWrapOffset.X, _scrollPos.Y, 0.0f, 0.0f);
                    DoRender(finalTransform);
                }
                else if (!float.IsNaN(_scrollWrapOffset.Y))
                {
                    _effect.Parameters[PARAM_SCROLL_POSITION] = new Vector4(_scrollPos.X, _scrollPos.Y + _scrollWrapOffset.Y, 0.0f, 0.0f);
                    DoRender(finalTransform);
                }
            }
            _lastTimeUsed = SkinContext.FrameRenderingStartTime;
        }