Home Game Development Libgdx zoom and pan the map on the similar time

Libgdx zoom and pan the map on the similar time

0
Libgdx zoom and pan the map on the similar time

[ad_1]

I used to be capable of do the zoom logic and pan logic, however like in google maps, I need to have the ability to zoom to the map and on the similar time pan the map close to to it.

Example

beginning place:

enter image description here

zoom to kennedy airport:

enter image description here

As you’ll be able to see the map shouldn’t be zoomed to the middle ( New York label), as a result of the mouse pointer was on the kennedy airport and a pan is added.

How do the identical with libgdx ?

code for map zoom:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.ScreenUtils;


public class GameScreen extends BaseScreen {

    personal Image map;

    public GameScreen(Launcher launcher) {
        tremendous(launcher);
    }

    @Override
    public void present() {
        batch = new SpriteBatch();
        // create ui
        stage = new Stage();
        pores and skin = new Skin(Gdx.recordsdata.inner("defaultSkin/uiskin.json"));
        closing Dialog dialog = new Dialog("Welcome", pores and skin, "dialog") {
            public void consequence(Object obj) {
                System.out.println("Closed dialog!");
            }
        };
        dialog.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
        dialog.textual content("Hello World ! Try to maneuver with mouse and zoom with mouse wheel");
        dialog.button("Close", true); // sends "true" because the consequence
        dialog.pack();
        dialog.setVisible(true);
        // stage.addActor(dialog);
        // init digicam
        digicam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        digicam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        // init map picture
        map = new Image(new Texture("map/World-map4.png"));
        map.setSize(1080, 720);
        // init enter processor
        InputMultiplexer multi = new InputMultiplexer();
        multi.addProcessor(stage);
        multi.addProcessor(new InputProcessor() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                return false;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                System.out.println("clicked: " + screenX + "," + screenY);
                return false;
            }

            @Override
            public boolean mouseMoved(int screenX, int screenY) {
                return false;
            }

            @Override
            public boolean keyUp(int keycode) {
                return false;
            }

            @Override
            public boolean keyTyped(char character) {
                return false;
            }

            @Override
            public boolean keyDown(int keycode) {
                System.out.println("Key down: " + keycode);
                int step = 20;
                int dx = 0;
                int dy = 0;
                if (keycode == Keys.LEFT) {
                    dx = -step;
                }
                if (keycode == Keys.RIGHT) {
                    dx = step;
                }
                if (keycode == Keys.UP) {
                    dy = step;
                }
                if (keycode == Keys.DOWN) {
                    dy = -step;
                }
                digicam.translate(dx, dy);
                return true;
            }

            @Override
            public boolean scrolled(float amountX, float amountY) {
                // arbitrary max zoomIn and max zoomOut values right here
                if (digicam.zoom - amountY >= -0.8f && digicam.zoom - amountY <= 2) {
                    digicam.zoom -= (float) amountY / 10f;
                    System.out.println("zoom  :" + digicam.zoom);
                }
                return true;
            }
        });

        Gdx.enter.setInputProcessor(multi);
    }

    @Override
    public void render(float delta) {
        ScreenUtils.clear(0, 0.36f, 0.65f, 1);
        digicam.replace();
        batch.setProjectionMatrix(digicam.mixed);
        batch.start();
        map.draw(batch, 1);
        batch.finish();
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    @Override
    public void dispose() {

    }

}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here