Example usage for com.badlogic.gdx InputAdapter InputAdapter

List of usage examples for com.badlogic.gdx InputAdapter InputAdapter

Introduction

In this page you can find the example usage for com.badlogic.gdx InputAdapter InputAdapter.

Prototype

InputAdapter

Source Link

Usage

From source file:ateamproject.kezuino.com.github.render.screens.GameScreen.java

public GameScreen(Game game) {
    super(game);//from   w ww .j a  v  a2s.  c  o  m

    clearOnRenderColor = Color.BLACK;

    backgroundMusic = Assets.getMusicStream("action.mp3");

    // Gameplay controls handling:
    gameInputAdapter = new InputAdapter() {
        @Override
        public boolean keyDown(int keycode) {
            switch (keycode) {
            case Input.Keys.W:
                if (getSession().getState() == GameState.Paused) {
                    break;
                }
                player.setDirection(Direction.Up);
                break;
            case Input.Keys.S:
                if (getSession().getState() == GameState.Paused) {
                    break;
                }
                player.setDirection(Direction.Down);
                break;
            case Input.Keys.A:
                if (getSession().getState() == GameState.Paused) {
                    break;
                }
                player.setDirection(Direction.Left);
                break;
            case Input.Keys.D:
                if (getSession().getState() == GameState.Paused) {
                    break;
                }
                player.setDirection(Direction.Right);
                break;
            case Input.Keys.SPACE:
                if (getSession().getState() != GameState.Paused) {
                    player.shootProjectile(player.getExactPosition(), player.getDirection());
                    Client.getInstance()
                            .send(new PacketShootProjectile(player.getExactPosition(), player.getDirection()));
                }
                break;
            case Input.Keys.H:
                BalloonMessage.getBalloonMessage(BalloonHelpMe.class).setFollowObject(player)
                        .addBalloonMessage();
                Client.getInstance().send(new PacketBalloonMessage("HelpMe", player.getId(), null));
                break;
            case Input.Keys.F1:
                DebugRenderManager.toggle();
                break;
            case Input.Keys.TAB:
                if (!getSession().isPauseMenuShowing()) {
                    showPlayersView();
                }
                break;
            case Input.Keys.ESCAPE:
                if (!getSession().isPlayerMenuShowing()) {
                    showPauseMenu();
                }
                break;
            default:
                return false;
            }
            return true;
        }
    };
    inputs.addProcessor(gameInputAdapter);

    createGui();
}

From source file:br.ufms.mechsmasher.scenes.Tutorial.java

@Override
public void create() {
    background = new Texture("concrete.jpg");
    text = new Texture("tutorial-texto.png");

    Gdx.input.setInputProcessor(new InputAdapter() {
        @Override//from w  ww  .j  a va2s.  com
        public boolean keyUp(int keycode) {
            if (keycode == Input.Keys.ESCAPE) {
                // Volta para o menu inicial caso o usurio pressione
                // ESC
                getSceneManager().swapScene(new Menu());
                return true;
            }

            return false;
        }
    });
}

From source file:com.anathema_roguelike.main.display.Input.java

License:Open Source License

public Input() {

    keyHandler = new KeyHandler() {

        @Override//w  w w.j a v  a 2 s.  com
        public void handle(char key, boolean alt, boolean ctrl, boolean shift) {
            setResult((inputHandler) -> {
                inputHandler.getKeyHandler().handle(key, alt, ctrl, shift);
            });
        }
    };

    mouse = new SquidMouse(UIConfig.CELL_WIDTH, UIConfig.CELL_HEIGHT, UIConfig.TERM_WIDTH, UIConfig.TERM_HEIGHT,
            0, 0, new InputAdapter() {
                @Override
                public boolean touchUp(int x, int y, int pointer, int button) {
                    setResult((inputHandler) -> {
                        inputHandler.getMouse().touchUp(x, y, pointer, button);

                        MouseCallback callback = mouseCallbacks.get(new Point(x, y));

                        if (callback != null) {
                            callback.onClick();
                        } else {
                        }
                    });
                    return true;
                }

                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                    setResult((inputHandler) -> {
                        inputHandler.getMouse().touchDown(screenX, screenY, pointer, button);
                    });
                    return true;
                }

                @Override
                public boolean touchDragged(int screenX, int screenY, int pointer) {
                    setResult((inputHandler) -> {
                        inputHandler.getMouse().touchDragged(screenX, screenY, pointer);
                    });
                    return true;
                }

                @Override
                public boolean scrolled(int amount) {
                    setResult((inputHandler) -> {
                        inputHandler.getMouse().scrolled(amount);
                    });
                    return true;
                }

                @Override
                public boolean mouseMoved(int x, int y) {
                    setResult((inputHandler) -> {
                        inputHandler.getMouse().mouseMoved(x, y);

                        MouseCallback callback = mouseCallbacks.get(new Point(x, y));

                        if (callback != null) {
                            callback.onMouseover();
                        }
                    });
                    return true;
                }
            });

    input = new SquidInput(keyHandler, mouse);
}

From source file:com.anathema_roguelike.main.display.InputHandler.java

License:Open Source License

public InputHandler(KeyHandler keyHandler) {
    this.keyHandler = keyHandler;
    this.mouse = new InputAdapter();
}

From source file:com.anathema_roguelike.main.ui.uielements.interactiveuielements.InteractiveUIElement.java

License:Open Source License

public T run() {
    Game.getInstance().getUserInterface().addUIElement(this);
    setResult(null);//from  w ww. j a v  a  2s. co m

    KeyHandler keyHandler = new KeyHandler() {

        @Override
        public void handle(char key, boolean alt, boolean ctrl, boolean shift) {

            if (key == SquidInput.ESCAPE) {
                if (isCancellable()) {
                    finish();
                }
            } else {
                processKeyEvent(key, alt, ctrl, shift);
            }
        }
    };

    InputAdapter mouse = new InputAdapter() {
        @Override
        public boolean scrolled(int amount) {
            return processScrollEvent(amount);
        }
    };

    Game.getInstance().getInput().proccessInput(new InputHandler(keyHandler, mouse), () -> {
        return isFinished();
    }, () -> {
        for (InteractiveUIElement<?> element : CollectionUtils.filterByClass(getUIElements(),
                InteractiveUIElement.class)) {
            element.registerMouseCallbacks();
        }
    });

    Game.getInstance().getUserInterface().removeUIElement(this);
    return getResult();
}

From source file:com.badlogic.gdx.tests.lwjgl.Lwjgl3DebugStarter.java

License:Apache License

public static void main(String[] argv) {
    GdxTest test = new GdxTest() {
        float r = 0;
        SpriteBatch batch;/*from  w  w  w . j av  a2  s  .  c  o  m*/
        BitmapFont font;

        @Override
        public void create() {
            batch = new SpriteBatch();
            font = new BitmapFont();
            Gdx.input.setInputProcessor(new InputAdapter() {

                @Override
                public boolean keyDown(int keycode) {
                    System.out.println("Key down: " + Keys.toString(keycode));
                    return false;
                }

                @Override
                public boolean keyUp(int keycode) {
                    System.out.println("Key up: " + Keys.toString(keycode));
                    return false;
                }

                @Override
                public boolean keyTyped(char character) {
                    System.out.println("Key typed: '" + character + "', " + (int) character);
                    return false;
                }
            });
        }

        @Override
        public void render() {
            Gdx.gl.glClearColor(r, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            batch.begin();
            font.draw(batch, Gdx.input.getDeltaX() + ", " + Gdx.input.getDeltaY(), 0, 20);
            batch.end();
            if (Gdx.input.justTouched()) {
                System.out.println("Just touched");
            }
            if (Gdx.input.isKeyJustPressed(Keys.ANY_KEY)) {
                System.out.println("Pressed any key");
            }
        }
    };
    Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
    config.width = 960;
    config.height = 600;
    config.vSyncEnabled = false;
    config.useHDPI = true;
    new Lwjgl3Application(test, config);
}

From source file:com.badlogic.gdx.tests.lwjgl3.Lwjgl3DebugStarter.java

License:Apache License

public static void main(String[] argv) throws NoSuchFieldException, SecurityException, ClassNotFoundException {
    GdxTest test = new GdxTest() {
        float r = 0;
        SpriteBatch batch;//from   w  w  w.j ava2  s  . c o m
        BitmapFont font;
        FPSLogger fps = new FPSLogger();
        Texture texture;

        @Override
        public void create() {
            BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);
            texture = new Texture("data/badlogic.jpg");
            batch = new SpriteBatch();
            font = new BitmapFont();
            Gdx.input.setInputProcessor(new InputAdapter() {

                @Override
                public boolean keyDown(int keycode) {
                    System.out.println("Key down: " + Keys.toString(keycode));
                    return false;
                }

                @Override
                public boolean keyUp(int keycode) {
                    System.out.println("Key up: " + Keys.toString(keycode));
                    return false;
                }

                @Override
                public boolean keyTyped(char character) {
                    System.out.println("Key typed: '" + character + "', " + (int) character);

                    if (character == 'f') {
                        Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());
                        //                     DisplayMode[] modes = Gdx.graphics.getDisplayModes();
                        //                     for(DisplayMode mode: modes) {
                        //                        if(mode.width == 1920 && mode.height == 1080) {
                        //                           Gdx.graphics.setFullscreenMode(mode);
                        //                           break;
                        //                        }
                        //                     }
                    }
                    if (character == 'w') {
                        Gdx.graphics.setWindowedMode(MathUtils.random(400, 800), MathUtils.random(400, 800));
                    }
                    if (character == 'e') {
                        throw new GdxRuntimeException("derp");
                    }
                    if (character == 'c') {
                        Gdx.input.setCursorCatched(!Gdx.input.isCursorCatched());
                    }
                    Lwjgl3Window window = ((Lwjgl3Graphics) Gdx.graphics).getWindow();
                    if (character == 'v') {
                        window.setVisible(false);
                    }
                    if (character == 's') {
                        window.setVisible(true);
                    }
                    if (character == 'q') {
                        window.closeWindow();
                    }
                    if (character == 'i') {
                        window.iconifyWindow();
                    }
                    if (character == 'm') {
                        window.maximizeWindow();
                    }
                    if (character == 'r') {
                        window.restoreWindow();
                    }
                    if (character == 'u') {
                        Gdx.net.openURI("https://google.com");
                    }
                    return false;
                }
            });
        }

        long start = System.nanoTime();

        @Override
        public void render() {
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            HdpiUtils.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            batch.begin();
            font.draw(batch,
                    Gdx.graphics.getWidth() + "x" + Gdx.graphics.getHeight() + ", "
                            + Gdx.graphics.getBackBufferWidth() + "x" + Gdx.graphics.getBackBufferHeight()
                            + ", " + Gdx.input.getX() + ", " + Gdx.input.getY() + ", " + Gdx.input.getDeltaX()
                            + ", " + Gdx.input.getDeltaY(),
                    0, 20);
            batch.draw(texture, Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
            batch.end();
            fps.log();
        }

        @Override
        public void resize(int width, int height) {
            Gdx.app.log("Test", "Resized " + width + "x" + height);
        }

        @Override
        public void resume() {
            Gdx.app.log("Test", "resuming");
        }

        @Override
        public void pause() {
            Gdx.app.log("Test", "pausing");
        }

        @Override
        public void dispose() {
            Gdx.app.log("Test", "disposing");
        }
    };
    Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
    config.setWindowedMode(640, 480);
    config.setWindowListener(new Lwjgl3WindowListener() {
        @Override
        public void created(Lwjgl3Window window) {
            Gdx.app.log("Window", "created");
        }

        @Override
        public void iconified(boolean isIconified) {
            Gdx.app.log("Window", "iconified: " + (isIconified ? "true" : "false"));
        }

        @Override
        public void maximized(boolean isMaximized) {
            Gdx.app.log("Window", "maximized: " + (isMaximized ? "true" : "false"));
        }

        @Override
        public void focusLost() {
            Gdx.app.log("Window", "focus lost");
        }

        @Override
        public void focusGained() {
            Gdx.app.log("Window", "focus gained");
        }

        @Override
        public boolean closeRequested() {
            Gdx.app.log("Window", "closing");
            return false;
        }

        @Override
        public void filesDropped(String[] files) {
            for (String file : files) {
                Gdx.app.log("Window", "File dropped: " + file);
            }
        }

        @Override
        public void refreshRequested() {
            Gdx.app.log("Window", "refreshRequested");
        }
    });
    for (DisplayMode mode : Lwjgl3ApplicationConfiguration.getDisplayModes()) {
        System.out.println(mode.width + "x" + mode.height);
    }

    System.setProperty("java.awt.headless", "true");
    new Lwjgl3Application(test, config);
}

From source file:com.badlogic.gdx.tests.ViewportTest1.java

License:Apache License

public void create() {
    stage = new Stage();
    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

    label = new Label("", skin);

    Table root = new Table(skin);
    root.setFillParent(true);//from  w w  w  . j a va  2  s .  c o  m
    root.setBackground(skin.getDrawable("default-pane"));
    root.debug().defaults().space(6);
    root.add(new TextButton("Button 1", skin));
    root.add(new TextButton("Button 2", skin)).row();
    root.add("Press spacebar to change the viewport:").colspan(2).row();
    root.add(label).colspan(2);
    stage.addActor(root);

    viewports = getViewports(stage.getCamera());
    names = getViewportNames();

    stage.setViewport(viewports.first());
    label.setText(names.first());

    Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {
        public boolean keyDown(int keycode) {
            if (keycode == Input.Keys.SPACE) {
                int index = (viewports.indexOf(stage.getViewport(), true) + 1) % viewports.size;
                label.setText(names.get(index));
                Viewport viewport = viewports.get(index);
                stage.setViewport(viewport);
                resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            }
            return false;
        }
    }, stage));
}

From source file:com.badlogic.gdx.tests.ViewportTest2.java

License:Apache License

public void create() {
    font = new BitmapFont();
    font.setColor(0, 0, 0, 1);//from  ww  w.  ja  v  a 2s.  c  o  m

    Pixmap pixmap = new Pixmap(16, 16, Format.RGBA8888);
    pixmap.setColor(1, 1, 1, 1);
    pixmap.fill();
    texture = new Texture(pixmap);

    batch = new SpriteBatch();

    camera = new OrthographicCamera();
    camera.position.set(100, 100, 0);
    camera.update();

    viewports = ViewportTest1.getViewports(camera);
    viewport = viewports.first();

    names = ViewportTest1.getViewportNames();
    name = names.first();

    Gdx.input.setInputProcessor(new InputAdapter() {
        public boolean keyDown(int keycode) {
            if (keycode == Input.Keys.SPACE) {
                int index = (viewports.indexOf(viewport, true) + 1) % viewports.size;
                name = names.get(index);
                viewport = viewports.get(index);
                resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            }
            return false;
        }
    });
}

From source file:com.badlogic.gdx.tests.ViewportTest3.java

License:Apache License

public void create() {
    modelBatch = new ModelBatch();
    modelBuilder = new ModelBuilder();

    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.3f, 0.3f, 0.3f, 1.f));
    shadowLight = new DirectionalLight();
    shadowLight.set(0.8f, 0.8f, 0.8f, -0.5f, -1f, 0.7f);
    environment.add(shadowLight);//  ww w.ja  v  a 2 s. com

    modelBatch = new ModelBatch();

    camera = new PerspectiveCamera();
    camera.fieldOfView = 67;
    camera.near = 0.1f;
    camera.far = 300f;
    camera.position.set(0, 0, 100);
    camera.lookAt(0, 0, 0);

    viewports = ViewportTest1.getViewports(camera);
    viewport = viewports.first();

    names = ViewportTest1.getViewportNames();
    name = names.first();

    ModelBuilder modelBuilder = new ModelBuilder();
    Model boxModel = modelBuilder.createBox(50f, 50f, 50f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position | Usage.Normal);
    boxInstance = new ModelInstance(boxModel);
    boxInstance.transform.rotate(1, 0, 0, 30);
    boxInstance.transform.rotate(0, 1, 0, 30);

    Gdx.input.setInputProcessor(new InputAdapter() {
        public boolean keyDown(int keycode) {
            if (keycode == Input.Keys.SPACE) {
                int index = (viewports.indexOf(viewport, true) + 1) % viewports.size;
                name = names.get(index);
                viewport = viewports.get(index);
                resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            }
            return false;
        }
    });
}