示例#1
0
        public void ClampStatic2()
        {
            Vector2F clamped = new Vector2F(-10, 1);

            clamped = Vector2F.Clamp(clamped, -1, 0);
            Assert.AreEqual(-1, clamped.X);
            Assert.AreEqual(0, clamped.Y);
        }
示例#2
0
        public void ClampStatic1()
        {
            Vector2F clamped = new Vector2F(-10f, 1f);

            clamped = Vector2F.Clamp(clamped, -100f, 100f);
            Assert.AreEqual(-10f, clamped.X);
            Assert.AreEqual(1f, clamped.Y);
        }
示例#3
0
        public void Clamp2()
        {
            Vector2F clamped = new Vector2F(-10, 1);

            clamped.Clamp(-1, 0);
            Assert.AreEqual(-1, clamped.X);
            Assert.AreEqual(0, clamped.Y);
        }
示例#4
0
        public void Clamp1()
        {
            Vector2F clamped = new Vector2F(-10f, 1f);

            clamped.Clamp(-100f, 100f);
            Assert.AreEqual(-10f, clamped.X);
            Assert.AreEqual(1f, clamped.Y);
        }
示例#5
0
    /// <summary>
    /// Checks whether the given box collider intersects with this circle, calculating
    /// the collision data in that case.
    /// </summary>
    /// <param name="other">box collider to check</param>
    /// <param name="intersection">the collision data, <code>null</code> if no collision has been detected.</param>
    /// <returns>true if the colliders intersect, false otherwise.</returns>
    public override bool Intersects(DBoxCollider other, out Manifold intersection)
    {
        intersection = null;

        Vector2F halfExtents = other.GetExtents() / 2;
        Vector2F boxCenter   = other.Min + halfExtents;

        Vector2F difference = center - boxCenter;
        Vector2F clamped    = Vector2F.Clamp(difference, -halfExtents, halfExtents);
        Vector2F closest    = boxCenter + clamped;

        difference = closest - center;

        if (difference.SqrtMagnitude > radius * radius)
        {
            return(false);
        }

        //check if one of them is a trigger
        if (this.IsTrigger || other.IsTrigger)
        {
            intersection = new Manifold(this.Body, other.Body);
            return(true);
        }

        Fix32    dist = difference.Magnitude;
        Fix32    penetration;
        Vector2F normal;

        if (dist > Fix32.Zero)
        {
            penetration = radius - dist;
            normal      = difference / dist;
        }
        else
        {
            penetration = radius;
            normal      = new Vector2F(1, 0);
        }
        intersection = new Manifold(this.Body, other.Body, normal, penetration);
        return(true);
    }
示例#6
0
 public void Clamp2()
 {
     Vector2F clamped = new Vector2F(-10, 1);
       clamped.Clamp(-1, 0);
       Assert.AreEqual(-1, clamped.X);
       Assert.AreEqual(0, clamped.Y);
 }
示例#7
0
 public void Clamp1()
 {
     Vector2F clamped = new Vector2F(-10f, 1f);
       clamped.Clamp(-100f, 100f);
       Assert.AreEqual(-10f, clamped.X);
       Assert.AreEqual(1f, clamped.Y);
 }
示例#8
0
    protected override void OnHandleInput(InputContext context)
    {
      var inputService = InputService;


      if (_isTouchDevice)
      {
        // Scrolling on phone/tablet has priority over the actions of the visual children.
        // Therefore base.OnHandleInput() is called after this code section.

        Vector2F mousePosition = inputService.MousePosition;
        float t = (float)context.DeltaTime.TotalSeconds;
        if (_isDragging)
        {
          if (inputService.IsMouseOrTouchHandled || inputService.IsUp(MouseButtons.Left))
          {
            // Dragging ends.
            _isDragging = false;

            // Check flick gesture.
            foreach (var gesture in inputService.Gestures)
            {
              if (gesture.GestureType == GestureType.Flick)
              {
                // Flick detected.
                // --> Set a scroll velocity proportional to the flick delta.
                _scrollVelocity = (Vector2F)(-gesture.Delta * FlickScrollVelocityFactor / t);
                _scrollVelocity = Vector2F.Clamp(_scrollVelocity, -MaxScrollVelocity, MaxScrollVelocity);

                inputService.IsMouseOrTouchHandled = true;
                break;
              }
            }
          }
          else
          {
            // Dragging continues.
            bool canScrollHorizontally = (ExtentWidth > ViewportWidth);
            bool canScrollVertically = (ExtentHeight > ViewportHeight);
            if (!_scrollToleranceExceeded)
            {
              // Check if drag tolerance has been exceeded.
              if (canScrollHorizontally && Math.Abs(mousePosition.X - _scrollStartPosition.X) > _scrollThreshold
                  || canScrollVertically && Math.Abs(mousePosition.Y - _scrollStartPosition.Y) > _scrollThreshold)
              {
                // Start dragging. (Use current mouse position to avoid a "jump".)
                _scrollStartPosition = mousePosition;
                _scrollToleranceExceeded = true;
              }
            }

            if (_scrollToleranceExceeded)
            {
              // Drag content.
              if (canScrollHorizontally && inputService.MousePositionDelta.X != 0
                  || canScrollVertically && inputService.MousePositionDelta.Y != 0)
              {
                inputService.IsMouseOrTouchHandled = true;

                Vector2F minOffset = new Vector2F(0, 0);
                Vector2F maxOffset = new Vector2F(Math.Max(ExtentWidth - ViewportWidth, 0),
                  Math.Max(ExtentHeight - ViewportHeight, 0));
                Vector2F minVirtualOffset = minOffset - new Vector2F(SpringLength);
                Vector2F maxVirtualOffset = maxOffset + new Vector2F(SpringLength);
                Vector2F newOffset = _scrollStartOffset + _scrollStartPosition - mousePosition;

                if (canScrollHorizontally)
                {
                  HorizontalOffset = MathHelper.Clamp(newOffset.X, minOffset.X, maxOffset.X);
                  _virtualOffset.X = MathHelper.Clamp(newOffset.X, minVirtualOffset.X, maxVirtualOffset.X);
                }

                if (canScrollVertically)
                {
                  VerticalOffset = MathHelper.Clamp(newOffset.Y, minOffset.Y, maxOffset.Y);
                  _virtualOffset.Y = MathHelper.Clamp(newOffset.Y, minVirtualOffset.Y, maxVirtualOffset.Y);
                }

                _scrollVelocity = -inputService.MousePositionDelta / t;
                _scrollVelocity = Vector2F.Clamp(_scrollVelocity, -MaxScrollVelocity, MaxScrollVelocity);
              }
            }
          }
        }
        else
        {
          if (!inputService.IsMouseOrTouchHandled
              && inputService.IsPressed(MouseButtons.Left, false)
              && IsMouseOver)
          {
            // Dragging starts.
            _isDragging = true;

            // Remember the mouse position.
            _scrollStartPosition = mousePosition;
            _scrollStartOffset = _virtualOffset;
            _scrollToleranceExceeded = false;
          }
        }

        if (!inputService.IsMouseOrTouchHandled && inputService.IsDown(MouseButtons.Left))
          _scrollVelocity = Vector2F.Zero;
      }


      base.OnHandleInput(context);

      if (!IsLoaded)
        return;


      // Mouse wheel scrolls vertically when the mouse cursor is over the scroll viewer.
      if (!inputService.IsMouseOrTouchHandled && IsMouseOver)
      {
        if (inputService.MouseWheelDelta != 0
            && VerticalScrollBarVisibility != ScrollBarVisibility.Disabled
            && _verticalScrollBar != null)
        {
          inputService.IsMouseOrTouchHandled = true;

          var screen = Screen;
          float offset = inputService.MouseWheelDelta / screen.MouseWheelScrollDelta * screen.MouseWheelScrollLines;
          offset *= _verticalScrollBar.SmallChange;
          offset = VerticalOffset - offset;
          if (offset < 0)
            offset = 0;
          if (offset > ExtentHeight - ViewportHeight)
            offset = ExtentHeight - ViewportHeight;

          VerticalOffset = offset;
        }
      }


      // Scroll with game pad right stick.
      if (!inputService.IsGamePadHandled(context.AllowedPlayer))
      {
        var gamePadState = inputService.GetGamePadState(context.AllowedPlayer);
        Vector2 rightStick = gamePadState.ThumbSticks.Right;
        float x = rightStick.X;
        float y = rightStick.Y;

        if (!Numeric.IsZero(x + y) && IsInActiveWindow())
        {
          if (_horizontalScrollBar != null)
          {
            float offset = HorizontalOffset + 0.5f * x * _horizontalScrollBar.SmallChange;
            offset = MathHelper.Clamp(offset, 0, ExtentWidth - ViewportWidth);
            HorizontalOffset = offset;
          }

          if (_verticalScrollBar != null)
          {
            float offset = VerticalOffset - 0.5f * y * _verticalScrollBar.SmallChange;
            offset = MathHelper.Clamp(offset, 0, ExtentHeight - ViewportHeight);
            VerticalOffset = offset;
          }

          inputService.SetGamePadHandled(context.AllowedPlayer, true);
        }
      }
    }