Search in sources :

Example 1 with PhysicsModel

use of nars.rover.PhysicsModel in project narchy by automenta.

the class AWTPanelHelper method addHelpAndPanelListeners.

/**
 * Adds common help text and listeners for awt-based testbeds.
 */
public static void addHelpAndPanelListeners(Component panel, final TestbedState model, final PhysicsController controller, final int screenDragButton) {
    final Vec2 oldDragMouse = new Vec2();
    final Vec2 mouse = new Vec2();
    List<String> help = Lists.newArrayList();
    help.add("Click and drag the left mouse button to move objects.");
    help.add("Click and drag the right mouse button to move the view.");
    help.add("Shift-Click to aim a bullet, or press space.");
    help.add("Scroll to zoom in/out on the mouse position");
    help.add("Press '[' or ']' to change tests, and 'r' to restart.");
    model.setImplSpecificHelp(help);
    panel.addMouseWheelListener(new MouseWheelListener() {

        public void mouseWheelMoved(MouseWheelEvent e) {
            int notches = e.getWheelRotation();
            PhysicsModel currTest = model.getCurrTest();
            if (currTest == null) {
                return;
            }
            ZoomType zoom = notches < 0 ? ZoomType.ZOOM_IN : ZoomType.ZOOM_OUT;
            currTest.getCamera().zoomToPoint(mouse, zoom);
        }
    });
    panel.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent arg0) {
            if (arg0.getButton() == screenDragButton) {
                screenDragButtonDown = false;
            } else if (model.getCodedKeys()[KeyEvent.VK_SHIFT] && !mouseJointButtonDown) {
                controller.queueMouseUp(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
            } else {
                if (arg0.getButton() == PhysicsModel.MOUSE_JOINT_BUTTON) {
                    mouseJointButtonDown = false;
                }
                Vec2 vv = worldPos(model, arg0);
                controller.queueMouseUp(vv, arg0.getButton());
            }
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            if (arg0.getButton() == screenDragButton) {
                screenDragButtonDown = true;
                oldDragMouse.set(arg0.getX(), arg0.getY());
                return;
            } else if (model.getCodedKeys()[KeyEvent.VK_SHIFT]) {
                controller.queueMouseDown(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
            } else {
                if (arg0.getButton() == PhysicsModel.MOUSE_JOINT_BUTTON) {
                    mouseJointButtonDown = true;
                }
                Vec2 vv = worldPos(model, arg0);
                controller.queueMouseDown(vv, arg0.getButton());
            }
        }
    });
    panel.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent arg0) {
            mouse.set(arg0.getX(), arg0.getY());
            controller.queueMouseMove(new Vec2(mouse));
        }

        @Override
        public void mouseDragged(MouseEvent arg0) {
            mouse.set(arg0.getX(), arg0.getY());
            if (screenDragButtonDown) {
                PhysicsModel currTest = model.getCurrTest();
                if (currTest == null) {
                    return;
                }
                Vec2 diff = oldDragMouse.sub(mouse);
                currTest.getCamera().moveWorld(diff);
                oldDragMouse.set(mouse);
            } else if (mouseJointButtonDown) {
                Vec2 vv = worldPos(model, arg0);
                controller.queueMouseDrag(vv, PhysicsModel.MOUSE_JOINT_BUTTON);
            } else if (model.getCodedKeys()[KeyEvent.VK_SHIFT]) {
                controller.queueMouseDrag(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
            } else {
                controller.queueMouseDrag(new Vec2(mouse), arg0.getButton());
            }
        }
    });
    panel.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent arg0) {
            controller.queueKeyReleased(arg0.getKeyChar(), arg0.getKeyCode());
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            char c = arg0.getKeyChar();
            controller.queueKeyPressed(c, arg0.getKeyCode());
            switch(c) {
                case '[':
                    controller.lastTest();
                    break;
                case ']':
                    controller.nextTest();
                    break;
                case 'r':
                    controller.reset();
                    break;
                case ' ':
                    controller.queueLaunchBomb();
                    break;
                case 'p':
                    controller.queuePause();
                    break;
            }
        }
    });
}
Also used : ZoomType(nars.rover.physics.PhysicsCamera.ZoomType) PhysicsModel(nars.rover.PhysicsModel) Vec2(spacegraph.space2d.phys.common.Vec2)

Aggregations

PhysicsModel (nars.rover.PhysicsModel)1 ZoomType (nars.rover.physics.PhysicsCamera.ZoomType)1 Vec2 (spacegraph.space2d.phys.common.Vec2)1