Newer
Older
PureATN_M / src / main / java / cit / PureATN / WheelEvent_on_Note.java
package cit.PureATN;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.geom.Point2D;

import org.piccolo2d.PCamera;
import org.piccolo2d.PCanvas;
import org.piccolo2d.PLayer;
import org.piccolo2d.event.PBasicInputEventHandler;
import org.piccolo2d.event.PInputEvent;
import org.piccolo2d.util.PBounds;

public class WheelEvent_on_Note extends PBasicInputEventHandler {
    public int direction = -1;
    PCamera camera;
    PLayer layer;
    PCanvas canvas;

    Point2D cursorpoint;
    Robot robot;

    public WheelEvent_on_Note(PCanvas canv) {
        canvas = canv;
        camera = canvas.getCamera();
        layer = canvas.getLayer();
        try {
			robot = new Robot();
		} catch (AWTException e) {
		}
    }
    public void mouseMoved(PInputEvent e){
        cursorpoint = e.getCanvasPosition();
        try {
            e.getPath().getPathTransformTo(layer).inverseTransform(cursorpoint, cursorpoint);
        } catch (RuntimeException ex) {
            cursorpoint = e.getPosition(); // Camera付きのメニューボタンや,背景部分(PCamera)のとき
        }
    }

    public void setDirection(int d) {
        direction = d;
    }

    public void mouseWheelRotated(PInputEvent e) {
        if (!e.isControlDown() && !e.isShiftDown() && !e.isAltDown()) {

            // System.out.println("wheel rotation: " + pc.getAttribute("hoge"));
            PBounds pb = camera.getViewBounds();
            // System.out.println(pb.toString());
            float f = 1.0f + (0.1f * direction * e.getWheelRotation());
            if (e.getWheelRotation() // 下に回すと正になるので
                    * direction < 0) {
                pb = zoomBounds_focusbyCursor(pb, f);
            } else {
                pb = zoomBounds(pb, f);
            }
            if (pb.x == 0 && pb.y == 0) {
                System.out.println("ズームイン位置がまだ準備前");
                return;
            }
            camera.animateViewToCenterBounds(pb, true, 0);
            // if (MyPCanvas.this.wheelListener != null)
            //     MyPCanvas.this.wheelListener.mouseWheelRotated(f);
        }
    }

    /**
     * 引数pbで与えた領域を,引数rateだけ拡大した新しい領域を返す
     * 
     * @param pagemanager 領域
     * @param rate        ズーム率
     * @return 新しい領域
     */
    public static PBounds zoomBounds(PBounds pb, double rate) {
        double x = pb.getX();
        double y = pb.getY();
        double w = pb.getWidth();
        double h = pb.getHeight();
        double nw = w * rate;
        double nh = h * rate;
        double nx = x - (nw - w) / 2;
        double ny = y - (nh - h) / 2;
        return new PBounds(nx, ny, nw, nh);
    }

    public PBounds zoomBounds_focusbyCursor(PBounds pb, double rate) {
        double x = pb.getX();
        double y = pb.getY();
        double w = pb.getWidth();
        double h = pb.getHeight();
        double nw = w * rate;
        double nh = h * rate;
        Point2D camcp = camera.getViewBounds().getCenter2D();
        double camcx = camcp.getX();
        double camcy = camcp.getY();
        // System.out.println("camcp "+camcx+" "+camcy);
        double curx = cursorpoint.getX();
        double cury = cursorpoint.getY();
        // System.out.println("cursor "+curx+" "+cury);
        double nx = x - (nw - w) / 2 + curx - camcx;
        double ny = y - (nh - h) / 2 + cury - camcy;
        PBounds ret = new PBounds(nx, ny, nw, nh);
        cursorpoint = ret.getCenter2D();

        moveCursorPointCenter();
        return ret;
    }
	public void moveCursorPointCenter() {
		Point canvasglobalp = canvas.getLocationOnScreen();
		int canvasw = canvas.getWidth();
		int canvash = canvas.getHeight();
		if (robot != null)
			robot.mouseMove((int) (canvasglobalp.getX() + canvasw / 2),
					(int) (canvasglobalp.getY() + canvash / 2));
	}

}