📄 walkviewerbehavior.java
字号:
// where positive X mouse movement is to the right, and
// positive Y mouse movement is **down** the screen.
//
if ( buttonPressed != BUTTON1 )
return;
int deltaX = previousX - initialX;
int deltaY = previousY - initialY;
double yRotationAngle = -deltaX * YRotationAnimationFactor;
double zTranslationDistance = deltaY * ZTranslationAnimationFactor;
//
// Build transforms
//
transform1.rotY( yRotationAngle );
translate.set( 0.0, 0.0, zTranslationDistance );
// Get and save the current transform matrix
subjectTransformGroup.getTransform( currentTransform );
currentTransform.get( matrix );
// Translate to the origin, rotate, then translate back
currentTransform.setTranslation( origin );
currentTransform.mul( transform1, currentTransform );
// Translate back from the origin by the original translation
// distance, plus the new walk translation... but force walk
// to travel on a plane by ignoring the Y component of a
// transformed translation vector.
currentTransform.transform( translate );
translate.x += matrix.m03; // add in existing X translation
translate.y = matrix.m13; // use Y translation
translate.z += matrix.m23; // add in existing Z translation
currentTransform.setTranslation( translate );
// Update the transform group
subjectTransformGroup.setTransform( currentTransform );
}
/**
* Responds to a button1 event (press, release, or drag). On a
* press, the method adds a wakeup criterion to the behavior's
* set, callling for the behavior to be awoken on each frame.
* On a button prelease, this criterion is removed from the set.
*
* @param mouseEvent the MouseEvent to respond to
*/
public void onButton1( MouseEvent mev )
{
if ( subjectTransformGroup == null )
return;
int x = mev.getX( );
int y = mev.getY( );
if ( mev.getID( ) == MouseEvent.MOUSE_PRESSED )
{
// Mouse button pressed: record position and change
// the wakeup criterion to include elapsed time wakeups
// so we can animate.
previousX = x;
previousY = y;
initialX = x;
initialY = y;
// Swap criterion... parent class will not reschedule us
mouseCriterion = mouseAndAnimationCriterion;
// Change to a "move" cursor
if ( parentComponent != null )
{
savedCursor = parentComponent.getCursor( );
parentComponent.setCursor(
Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
}
return;
}
if ( mev.getID( ) == MouseEvent.MOUSE_RELEASED )
{
// Mouse button released: restore original wakeup
// criterion which only includes mouse activity, not
// elapsed time
mouseCriterion = savedMouseCriterion;
// Switch the cursor back
if ( parentComponent != null )
parentComponent.setCursor( savedCursor );
return;
}
previousX = x;
previousY = y;
}
/**
* Responds to a button2 event (press, release, or drag). On a
* press, the method records the initial cursor location.
* On a drag, the difference between the current and previous
* cursor location provides a delta that controls the amount by
* which to rotate in X and Y.
*
* @param mouseEvent the MouseEvent to respond to
*/
public void onButton2( MouseEvent mev )
{
if ( subjectTransformGroup == null )
return;
int x = mev.getX( );
int y = mev.getY( );
if ( mev.getID( ) == MouseEvent.MOUSE_PRESSED )
{
// Mouse button pressed: record position
previousX = x;
previousY = y;
initialX = x;
initialY = y;
// Change to a "rotate" cursor
if ( parentComponent != null )
{
savedCursor = parentComponent.getCursor( );
parentComponent.setCursor(
Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR ) );
}
return;
}
if ( mev.getID( ) == MouseEvent.MOUSE_RELEASED )
{
// Mouse button released: do nothing
// Switch the cursor back
if ( parentComponent != null )
parentComponent.setCursor( savedCursor );
return;
}
//
// Mouse moved while button down: create a rotation
//
// Compute the delta in X and Y from the previous
// position. Use the delta to compute rotation
// angles with the mapping:
//
// positive X mouse delta --> negative Y-axis rotation
// positive Y mouse delta --> negative X-axis rotation
//
// where positive X mouse movement is to the right, and
// positive Y mouse movement is **down** the screen.
//
int deltaX = x - previousX;
int deltaY = 0;
if ( Math.abs(y-initialY) > DELTAY_DEADZONE )
{
// Cursor has moved far enough vertically to consider
// it intentional, so get it's delta.
deltaY = y - previousY;
}
if ( deltaX > UNUSUAL_XDELTA || deltaX < -UNUSUAL_XDELTA ||
deltaY > UNUSUAL_YDELTA || deltaY < -UNUSUAL_YDELTA )
{
// Deltas are too huge to be believable. Probably a glitch.
// Don't record the new XY location, or do anything.
return;
}
double xRotationAngle = -deltaY * XRotationFactor;
double yRotationAngle = -deltaX * YRotationFactor;
//
// Build transforms
//
transform1.rotX( xRotationAngle );
transform2.rotY( yRotationAngle );
// Get and save the current transform matrix
subjectTransformGroup.getTransform( currentTransform );
currentTransform.get( matrix );
translate.set( matrix.m03, matrix.m13, matrix.m23 );
// Translate to the origin, rotate, then translate back
currentTransform.setTranslation( origin );
currentTransform.mul( transform2, currentTransform );
currentTransform.mul( transform1 );
currentTransform.setTranslation( translate );
// Update the transform group
subjectTransformGroup.setTransform( currentTransform );
previousX = x;
previousY = y;
}
/**
* Responds to a button3 event (press, release, or drag).
* On a drag, the difference between the current and previous
* cursor location provides a delta that controls the amount by
* which to translate in X and Y.
*
* @param mouseEvent the MouseEvent to respond to
*/
public void onButton3( MouseEvent mev )
{
if ( subjectTransformGroup == null )
return;
int x = mev.getX( );
int y = mev.getY( );
if ( mev.getID( ) == MouseEvent.MOUSE_PRESSED )
{
// Mouse button pressed: record position
previousX = x;
previousY = y;
// Change to a "move" cursor
if ( parentComponent != null )
{
savedCursor = parentComponent.getCursor( );
parentComponent.setCursor(
Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR ) );
}
return;
}
if ( mev.getID( ) == MouseEvent.MOUSE_RELEASED )
{
// Mouse button released: do nothing
// Switch the cursor back
if ( parentComponent != null )
parentComponent.setCursor( savedCursor );
return;
}
//
// Mouse moved while button down: create a translation
//
// Compute the delta in X and Y from the previous
// position. Use the delta to compute translation
// distances with the mapping:
//
// positive X mouse delta --> positive X-axis translation
// positive Y mouse delta --> negative Y-axis translation
//
// where positive X mouse movement is to the right, and
// positive Y mouse movement is **down** the screen.
//
int deltaX = x - previousX;
int deltaY = y - previousY;
if ( deltaX > UNUSUAL_XDELTA || deltaX < -UNUSUAL_XDELTA ||
deltaY > UNUSUAL_YDELTA || deltaY < -UNUSUAL_YDELTA )
{
// Deltas are too huge to be believable. Probably a glitch.
// Don't record the new XY location, or do anything.
return;
}
double xTranslationDistance = deltaX * XTranslationFactor;
double yTranslationDistance = -deltaY * YTranslationFactor;
//
// Build transforms
//
translate.set( xTranslationDistance, yTranslationDistance, 0.0 );
transform1.set( translate );
// Get and save the current transform
subjectTransformGroup.getTransform( currentTransform );
// Translate as needed
currentTransform.mul( transform1 );
// Update the transform group
subjectTransformGroup.setTransform( currentTransform );
previousX = x;
previousY = y;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -