2011-12-31 8 views
0

2次元カメラを使用してピンチズームをアプリケーションに実装しようとしています。XNA 2dカメラのピンチとズーム

case GestureType.Pinch: 
    offset = new Vector2(0, 0);       
    oldPosition1 = gesture.Position - gesture.Delta; 
    oldPosition2 = gesture.Position2 - gesture.Delta2; 
    newDistance = Vector2.Distance(gesture.Position, gesture.Position2); 
    oldDistance = Vector2.Distance(oldPosition1, oldPosition2); 
    scaleFactor = newDistance/oldDistance; 

    if (pinchInProgress == false) 
    { 
     pinchTarget = new Vector2((gesture.Position.X + gesture.Position2.X)/2, (gesture.Position.Y + gesture.Position2.Y)/2); 
     pinchInProgress = true; 
    } 


    // Prevents from zooming out further than full screen 
    if (workSpace.Width * cam.Zoom < SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width && scaleFactor < 1) 
     scaleFactor = 1; 
    if (workSpace.Height * cam.Zoom < SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height && scaleFactor < 1) 
     scaleFactor = 1; 

     cam.Zoom = MathHelper.Clamp(cam.Zoom * scaleFactor, 0.1f, 1.5f); 


     if (cam.Pos.X - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width < -(workSpace.Width * cam.Zoom)) 
     offset.X = -(cam.Pos.X + workSpace.Width * cam.Zoom - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width); 
     if (cam.Pos.Y - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height < -(workSpace.Height * cam.Zoom)) 
     offset.Y = -(cam.Pos.Y + workSpace.Height * cam.Zoom - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height); 
     if (cam.Pos.X + offset.X > 0) 
      offset.X = -(cam.Pos.X); 
     if (cam.Pos.Y + offset.Y > 0) 
      offset.Y = -(cam.Pos.Y); 
     cam.Move(offset); 


     break; 

もカメラは常にワークスペース内にとどまるように、エッジから離れカメラを動かすハンドル:私は、次のコードを使用して正常にズームイン、ズームアウトすることができています。

私は、作業領域のVector2.Zeroではなく、ピンチジェスチャセンターでカメラをズームインするためのメカニズムを実装しようとしていました。だから別の質問から私はピンチセンター(または少なくとも試して)にカメラを得ることができるようだ。

だから、私は次のように利用することができる期待していた:

case GestureType.PinchComplete: 
    pinchInProgress = false; 
    break; 

から別のジェスチャーを区別し、ジェスチャーの初めに定義された1点に向かってカメラの動きをします。

私はそれがすべて意味があることを願っています。

とにかく、実際の問題は、pinchInProgressがfalseに設定されないことです。 GestureType.Pinchブロックで正しく設定されていますが、PinchCompleteがトリガーされないようです。

編集: また、pinchInProgress = falseでブレークポイントを追加しようとしました。これは決してこの点には達しません。

+0

を有効にするのを忘れて判明しました。 – Steven

答えて

1

は、それは私が、私はこの便利なを見つけたPinchCompleteジェスチャーに

TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.Pinch | GestureType.PinchComplete; 

謝罪

+0

ああ、ちょっと、その小さなことが大好きなのですか? –

+0

@ScottW私の頭を机の上で数時間叩いた後、どんな解像度も満足できるものだと言わざるを得ない。 :) – LukeP

関連する問題