Search in sources :

Example 1 with Clock

use of org.cesiumjs.cs.core.Clock in project gwt-cs by iSergio.

the class CameraTutorial method buildPanel.

@Override
public void buildPanel() {
    csVPanel = new ViewerPanel();
    csVPanel.addHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent clickEvent) {
            csVPanel.getViewer().canvas().focus();
        }
    }, ClickEvent.getType());
    csVPanel.getViewer().canvas().setAttribute("tabindex", "0");
    csVPanel.getViewer().scene().screenSpaceCameraController().enableRotate = false;
    csVPanel.getViewer().scene().screenSpaceCameraController().enableTranslate = false;
    csVPanel.getViewer().scene().screenSpaceCameraController().enableZoom = false;
    csVPanel.getViewer().scene().screenSpaceCameraController().enableTilt = false;
    csVPanel.getViewer().scene().screenSpaceCameraController().enableLook = false;
    handler = new ScreenSpaceEventHandler(csVPanel.getViewer().canvas());
    handler.setInputAction(new ScreenSpaceEventHandler.Listener<org.cesiumjs.cs.core.events.MouseDownEvent>() {

        @Override
        public void function(org.cesiumjs.cs.core.events.MouseDownEvent event) {
            flags.looking = true;
            mousePosition = startMousePosition = new Cartesian3(event.position.x, event.position.y, 0);
        }
    }, ScreenSpaceEventType.LEFT_DOWN());
    handler.setInputAction(new ScreenSpaceEventHandler.Listener<org.cesiumjs.cs.core.events.MouseMoveEvent>() {

        @Override
        public void function(org.cesiumjs.cs.core.events.MouseMoveEvent event) {
            mousePosition = new Cartesian3(event.endPosition.x, event.endPosition.y, 0);
        }
    }, ScreenSpaceEventType.MOUSE_MOVE());
    handler.setInputAction(new ScreenSpaceEventHandler.Listener<org.cesiumjs.cs.core.events.MouseUpEvent>() {

        @Override
        public void function(org.cesiumjs.cs.core.events.MouseUpEvent event) {
            flags.looking = false;
        }
    }, ScreenSpaceEventType.LEFT_UP());
    RootPanel.get().addDomHandler(new KeyDownHandler() {

        @Override
        public void onKeyDown(KeyDownEvent keyDownEvent) {
            setFlag(keyDownEvent.getNativeKeyCode(), true);
        }
    }, KeyDownEvent.getType());
    RootPanel.get().addDomHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent keyUpEvent) {
            setFlag(keyUpEvent.getNativeKeyCode(), false);
        }
    }, KeyUpEvent.getType());
    csVPanel.getViewer().clock().onTick.addEventListener(new Event.Listener() {

        @Override
        public void function(Object... o) {
            org.cesiumjs.cs.scene.Camera camera = csVPanel.getViewer().camera;
            // First element in array is Clock. Check this.
            if (o[0] instanceof Clock) {
                if (flags.looking) {
                    int width = csVPanel.getViewer().canvas().getClientWidth();
                    int height = csVPanel.getViewer().canvas().getClientHeight();
                    // Coordinate (0.0, 0.0) will be where the mouse was clicked.
                    double x = (mousePosition.x - startMousePosition.x) / width;
                    double y = -(mousePosition.y - startMousePosition.y) / height;
                    double lookFactor = 0.05;
                    camera.lookRight(x * lookFactor);
                    camera.lookUp(y * lookFactor);
                }
                // Change movement speed based on the distance of the camera to the surface of the ellipsoid.
                double cameraHeight = csVPanel.getViewer().scene().globe.ellipsoid.cartesianToCartographic(camera.position).height;
                double moveRate = cameraHeight / 100.0;
                if (flags.moveForward) {
                    camera.moveForward(moveRate);
                }
                if (flags.moveBackward) {
                    camera.moveBackward(moveRate);
                }
                if (flags.moveUp) {
                    camera.moveUp(moveRate);
                }
                if (flags.moveDown) {
                    camera.moveDown(moveRate);
                }
                if (flags.moveLeft) {
                    camera.moveLeft(moveRate);
                }
                if (flags.moveRight) {
                    camera.moveRight(moveRate);
                }
            }
        }
    });
    HTML html = new HTML();
    html.setHTML("<div id=\"toolbar\">\n" + "<div>Click on the Cesium display to start.</div>\n" + "<div>w/s - move forward/backward</div>\n" + "<div>a/d - move left/right</div>\n" + "<div>q/e - move up/down</div>\n" + "<div>left mouse button down plus mouse move changes the look direction</div>\n" + "</div>");
    html.getElement().getStyle().setColor("white");
    AbsolutePanel aPanel = new AbsolutePanel();
    aPanel.add(csVPanel);
    aPanel.add(html, 20, 20);
    contentPanel.add(new HTML("<p>Use the camera to control the view, and create custom event handlers for input.</p>"));
    contentPanel.add(aPanel);
    initWidget(contentPanel);
}
Also used : AbsolutePanel(com.google.gwt.user.client.ui.AbsolutePanel) HTML(com.google.gwt.user.client.ui.HTML) Clock(org.cesiumjs.cs.core.Clock) Cartesian3(org.cesiumjs.cs.core.Cartesian3) ViewerPanel(org.cesiumjs.cs.widgets.ViewerPanel) ScreenSpaceEventHandler(org.cesiumjs.cs.core.ScreenSpaceEventHandler) Event(org.cesiumjs.cs.core.Event)

Aggregations

AbsolutePanel (com.google.gwt.user.client.ui.AbsolutePanel)1 HTML (com.google.gwt.user.client.ui.HTML)1 Cartesian3 (org.cesiumjs.cs.core.Cartesian3)1 Clock (org.cesiumjs.cs.core.Clock)1 Event (org.cesiumjs.cs.core.Event)1 ScreenSpaceEventHandler (org.cesiumjs.cs.core.ScreenSpaceEventHandler)1 ViewerPanel (org.cesiumjs.cs.widgets.ViewerPanel)1