2016-11-30 94 views
0

平移我是新来的DirectX和Direct3D/2D等,只是当前运行是否继续制作一台机器,我们有一个CAD浏览器的实验。2D相机在SharpDX

我使用从这里Direct2dOnWPF控制,使我使用SharpDX显示的Direct2D到WPF窗口。

目前我有控制的工作及其负载文件并显示图。

我现在已经创建了一个摄像头和我已经实现缩放(在一定程度上),但我的问题是与平移。问题是,当平移时,我希望绘图随鼠标移动,但不是。小的运动有点类似,但更大的运动会导致绘图移动到鼠标移动之外。几乎就像我将鼠标移动到一个单一的移动中一样,它移动得越快。

好一些代码,所述Direct2DControl是基于图像控制,所以我有机会获得鼠标事件等。这里是一些的代码与鼠标事件和定时器控制。我尝试了一个计时器来检测鼠标何时停止,因为我发现当鼠标没有时平移不会停止。

// Timer to detect mouse stop 
private Timer tmr; 

public Direct2dControl() 
    { 
     // 
     // .... Init stuff 
     // 

     // Mouse panning 

     // get mouse position 
     MouseOrigin = CurrentMousePosition = new Point(0, 0); 

     tmr = new Timer { Interval = 50 }; 
     tmr.Elapsed += Tmr_Elapsed; 
    } 

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 
    { 
     base.OnMouseLeftButtonDown(e); 

     if (!DragIsOn) 
     { 
      DragIsOn = true; 
     } 
    } 

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) 
    { 
     base.OnMouseLeftButtonUp(e); 

     if (DragIsOn) 
     { 
      DragIsOn = false; 
      DragStarted = false; 
      MouseOrigin = CurrentMousePosition = e.GetPosition(this); 
     } 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 

     if (!DragIsOn) return; 

     MouseMoved = true; 

     if (!DragStarted) 
     { 
      DragStarted = true; 
      MouseOrigin = CurrentMousePosition = e.GetPosition(this); 

      tmr.Start(); 
     } 
     else 
     { 
      CurrentMousePosition = e.GetPosition(this); 

      var x = (float)(MouseOrigin.X - CurrentMousePosition.X); 
      var y = (float) (MouseOrigin.Y - CurrentMousePosition.Y); 

      cam.MoveCamera(cam.ScreenToWorld(new Vector2(x, y))); 

      tmr.Stop(); 
      tmr.Start(); 
     } 
    } 

private void Tmr_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     MouseOrigin = CurrentMousePosition; 

     tmr.Stop(); 
     MouseMoved = false; 
    } 

以及通过移动位置在相机类中的平移。

public void MoveCamera(Vector2 cameraMovement) 
    { 
     Vector2 newPosition = Position + cameraMovement; 

     Position = newPosition; 
    } 

    public Matrix3x2 GetTransform3x2() 
    { 
     return TransformMatrix3x2; 
    } 

    private Matrix3x2 TransformMatrix3x2 
    { 
     get 
     { 
      return 
       Matrix3x2.Translation(new Vector2(-Position.X, -Position.Y)) * 
       Matrix3x2.Rotation(Rotation) * 
       Matrix3x2.Scaling(Zoom) * 
       Matrix3x2.Translation(new Vector2(Bounds.Width * 0.5f, Bounds.Height * 0.5f)); 
     } 
    } 

,并最终在开始时开始渲染我更新渲染目标变换

target.Transform = cam.GetTransform3x2(); 

回答

0

我相信你计算坐标错误。首先,你需要设置OnLeftMouseButtonDown的MouseOrigin变量和不修改它的任何其他方法:

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 
{ 
    base.OnMouseLeftButtonDown(e); 

    if (!DragIsOn) 
    { 
     DragIsOn = true; 
     MouseOrigin = e.GetPosition(this); 

     // I don't know the type of your cam variable so the following is pseudo code 
     MouseOrigin.x -= cam.CurrentPosition.x; 
     MouseOrigin.y -= cam.CurrentPosition.y; 
    } 
} 

而且修改的OnMouseMove这样的:

protected override void OnMouseMove(MouseEventArgs e) 
{ 
    base.OnMouseMove(e); 

    if (!DragIsOn) return; 

    MouseMoved = true; 

    var x = (float)(e.GetPosition(this).x - MouseOrigin.X); 
    var y = (float) (e.GetPosition(this).y - MouseOrigin.Y); 

    cam.MoveCamera(cam.ScreenToWorld(new Vector2(x, y))); 

    tmr.Stop(); 
    tmr.Start(); 

} 

不需要的DragStarted和CurrentMousePosition变量。

让我知道,如果它的工作原理。

+0

感谢您的支持。平移速度现在似乎是正确的,但现在似乎存在与其移动方向有关的问题。如果我点击并从中心拖到右下角,对象将移动到左上角。如果我然后停下来,然后单击并拖动并向上移动鼠标,则对象将继续在其原始路径中移动到左上角,然后向下移动。如果我然后停下来,然后单击并拖动以在屏幕上向左移动,则对象将返回到左上角。 – Gaz83

+0

你的'cam'变量的类型是什么? – user1610015

+0

哦,类型凸轮只是我的相机类。 – Gaz83