/*
 *    Copyright 2008 Christian Stussak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.mfo.jsurf.util;

import java.awt.Point;
import javax.vecmath.*;

public class RotateSphericalDragger
{
    Point lastLocation;
    Matrix4d rotation;
    double xSpeed;
    double ySpeed;
    
    public RotateSphericalDragger()
    {
        this( 1, 1 );
    }
    
    public RotateSphericalDragger( double xSpeed, double ySpeed )
    {
        lastLocation = new Point();
        rotation = new Matrix4d();
        rotation.setIdentity();
        this.xSpeed = xSpeed;
        this.ySpeed = ySpeed;
    }
    
    public void startDrag( Point p )
    {
        lastLocation = new Point( p );
    }
    
    public void dragTo( Point p )
    {
        double xAngle = ( lastLocation.x - p.x ) * xSpeed;
        double yAngle = ( lastLocation.y - p.y ) * ySpeed;
        
        Matrix4d rotX = new Matrix4d();
        rotX.setIdentity();
        rotX.rotX( ( Math.PI / 180.0 ) * yAngle );

        Matrix4d rotY = new Matrix4d();
        rotY.setIdentity();
        rotY.rotY( ( Math.PI / 180.0 ) * xAngle );
        
        rotation.mul( rotX );
        rotation.mul( rotY );
        
        lastLocation = new Point( p );
    }
    
    public Matrix4d getRotation()
    {
        return new Matrix4d( rotation );
    }
    
    public void setRotation( Matrix4d m )
    {
        rotation = new Matrix4d( m );
    }
    
    public double getXSpeed()
    {
        return xSpeed;
    }
    
    public void setXSpeed( double xSpeed )
    {
        this.xSpeed = xSpeed;
    }
    
    public double getYSpeed()
    {
        return ySpeed;
    }
    
    public void setYSpeed( double ySpeed )
    {
        this.ySpeed = ySpeed;
    }
}

@@ -793,24 +793,44 @@
 ySpeed;%0A    
+boolean isDragging;%0A
 %0A    public 
@@ -880,37 +880,33 @@
 ( 1, 1 );%0A    %7D%0A
-    
 %0A
+
     public Rotat
@@ -953,32 +953,60 @@
  ySpeed )%0A    %7B%0A
+        isDragging = false;%0A
         lastLoca
@@ -1150,37 +1150,33 @@
 = ySpeed;%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -1194,32 +1194,59 @@
 Point p )%0A    %7B%0A
+        isDragging = true;%0A
         lastLoca
@@ -1266,37 +1266,33 @@
 int( p );%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -1307,32 +1307,79 @@
 Point p )%0A    %7B%0A
+        if( !isDragging )%0A            return;%0A%0A
         double x
@@ -1480,25 +1480,17 @@
 ySpeed;%0A
-        
 %0A
+
         
@@ -1716,33 +1716,25 @@
 * xAngle );%0A
-        
 %0A
+
         rota
@@ -1785,17 +1785,9 @@
  );%0A
-        
 %0A
+
     
@@ -1819,36 +1819,100 @@
 int( p );%0A    %7D%0A
+%0A
     
+public void stopDrag()%0A    %7B%0A        isDragging = false;%0A    %7D%0A
 %0A    public Matr
@@ -1975,37 +1975,33 @@
 tation );%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -2068,37 +2068,33 @@
 x4d( m );%0A    %7D%0A
-    
 %0A
+
     public doubl
@@ -2134,37 +2134,33 @@
 n xSpeed;%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -2220,37 +2220,33 @@
 = xSpeed;%0A    %7D%0A
-    
 %0A
+
     public doubl
@@ -2294,20 +2294,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

Add possibility to disable RotateSphericalDragger to help dealing with drag events that occur before mouse pressed events