mouseaction.java.svn-base

来自「一个JAVA程序员的游戏」· SVN-BASE 代码 · 共 94 行

SVN-BASE
94
字号
/*
 * MouseAction.java
 *
 * Created on 31. Dezember 2005, 14:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.action;

import com.jme.input.Mouse;
import com.jme.input.RelativeMouse;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyLookDownAction;
import com.jme.input.action.KeyLookUpAction;
import com.jme.input.action.MouseInputAction;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.CameraNode;

/**
 * <code>MouseAction</code> defines a mouse action that detects mouse movement
 * and converts it into camera rotations and camera tilts.
 * Based on the MouseLook class from Mark Powell.
 * 
 * @author Mark Powell
 * @author Daniel Gronau
 */
public class MouseAction extends MouseInputAction {

    //actions to handle looking up down left and right.
    private RotateAction lookUp;
    private RotateAction rotateRight;
    private MoveAction forward;
    
    //the event to distribute to the looking actions.
    private InputActionEvent event;
    private CameraNode node;

    private float moveSpeed;
    
    /**
     * Constructor creates a new <code>MouseAction</code>.
     * 
     * @param mouse  the mouse to calculate view changes.
     * @param cameraNode the cameraNode to rotate.
     * @param speed the speed at which to alter the camera.
     * @param inverted determines if the y-axis should be inverted
     */
    public MouseAction(Mouse mouse, CameraNode node, float speed, boolean inverted) {
        this.mouse = (RelativeMouse) mouse;
        this.speed = speed;
        this.node = node;

        lookUp = new RotateAction(node, speed, inverted 
                                               ? RotateAction.Direction.DOWN 
                                               : RotateAction.Direction.UP);
        rotateRight = new RotateAction(node, speed, RotateAction.Direction.RIGHT);
        
        event = new InputActionEvent();
    }

    /**
     * 
     * <code>setSpeed</code> sets the speed of the mouse look.
     * 
     * @param speed the speed of the mouse look.
     */
    public void setSpeed(float rotSpeed) {
        super.setSpeed(rotSpeed);
        lookUp.setSpeed(rotSpeed);
        rotateRight.setSpeed(rotSpeed);
    }

    /**
     * <code>performAction</code> checks for any movement of the mouse, and
     * calls the appropriate method to alter the camera's orientation when
     * applicable.
     * 
     * @see com.jme.input.action.MouseInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
        float time = evt.getTime() * speed;

        event.setTime(time * mouse.getLocalTranslation().x);
        rotateRight.performAction(event);
        
        Vector3f direction = node.getCamera().getDirection().normalize();
        event.setTime(time * mouse.getLocalTranslation().y);
        lookUp.performAction(event);
        
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?