tomm
tomm

Reputation: 21

libgdx drag and drop

Im trying to add drag and drop functionality to several images in Libgdx. I have looked at this example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DragAndDropTest.java but Its still not working. The images do not drag and drop. Would anyone be able to give me some pointers in why its not working? Thanks

private void createButton() {
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    skin = new Skin();
    skin.add("up", new Texture(Gdx.files.internal("assets/data/up.png")));
    skin.add("def", new Texture(Gdx.files.internal("assets/data/Goal.png")));

    final Image up = new Image(skin, "up");
    up.setBounds(1090, 630, 40, 40);
    stage.addActor(up);
    Image def = new Image(skin, "def");
    def.setBounds(1090, 585, 40, 40);
    stage.addActor(def);
    DragAndDrop dragAndDrop = new DragAndDrop();
    dragAndDrop.addSource(new Source(up) {
            public Payload dragStart (InputEvent event, float x, float y, int pointer) {
                    Payload payload = new Payload();
                    payload.setObject(payload);
                    payload.setDragActor(up);
                   payload.setDragActor(new Label("up", skin));
                    Label validLabel = new Label("up", skin);
                    validLabel.setColor(0, 1, 0, 1);
                    payload.setValidDragActor(validLabel);

                    return payload;
            }
    });

    dragAndDrop.addTarget(new Target(def) {
        public boolean drag (Source source, Payload payload, float x, float y, int pointer) {
                getActor().setColor(Color.GREEN);
                return true;
        }

        public void reset (Source source, Payload payload) {
                getActor().setColor(Color.WHITE);
        }

        public void drop (Source source, Payload payload, float x, float y, int pointer) {
                System.out.println("Accepted: " + payload.getObject() + " " + x + ", " + y);
        }
});
    render();
}

  public void render () {
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
        Table.drawDebug(stage);
}

Upvotes: 2

Views: 3534

Answers (2)

arv
arv

Reputation: 250

Another way to do drag in a better way...

public class CaveInterection implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture bgTexture;
    private Sprite sprite;
    private Stage stage;
    private Texture mirrTexture;
    private MyActor mirrorActor;
    Sprite img1,img2;
    @Override
    public void create() {      
        camera = new OrthographicCamera(1024, 550);
        camera.position.set(1024 / 2, 550 / 2, 0);
        batch = new SpriteBatch();
        stage = new Stage(1024, 550, false);
        //bgTexture = new Texture(Gdx.files.internal("data/cave.jpg"));
        //bgTexture = new Texture(Gdx.files.internal("data/bg.jpg"));

        mirrTexture = new Texture(Gdx.files.internal("data/mirror.png"));
        mirrTexture
        .setFilter(TextureFilter.Linear, TextureFilter.Linear);
        mirrorActor = new MyActor(new TextureRegion(mirrTexture));
        mirrorActor.setPosition(700, 400);
        mirrorActor.setOrigin(mirrorActor.getWidth()/2, mirrorActor.getHeight()/2);
        stage.addActor(mirrorActor);


        // finally stage as the input process 
        Gdx.input.setInputProcessor(stage);

    }

    @Override
    public void dispose() {
        batch.dispose();
        //bgTexture.dispose();
    }

    @Override
    public void render() {      
        // clear the screen, update the camera and make the sprite batch
        // use its matrices.
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        //batch.draw(bgTexture, 0,0);
    //  Gdx.app.log("arvind","X :"+mirrorActor.getX()+ " Y :"+mirrorActor.getY());
        //batch.draw(bgTexture, 0, 0);
        batch.end();
        // tell the stage to act and draw itself
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    public class MyActor extends Actor {
        TextureRegion region;
        float lastX;
        float lastY;

        public MyActor (TextureRegion region) {
            this.region = region;
            setWidth(region.getRegionWidth());
            setHeight(region.getRegionHeight());

            addListener(new InputListener() {
                public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                    Gdx.app.log("arv", "pointer1+"+pointer);
                    // we only care for the first finger to make things easier
                    if (pointer != 0) return false;

                    // record the coordinates the finger went down on. they
                    // are given relative to the actor's   corner (0, 0)
                    Gdx.app.log("arvind", "touchDown");
                    Gdx.app.log("arv", "pointer2+"+pointer+""+x+"::::"+y);
                    lastX = x;
                    lastY = y;
                    return true;
                }

                public void touchDragged (InputEvent event, float x, float y, int pointer) {
                    // we only care for the first finger to make things easier
                    if (pointer != 0) return;
                    Gdx.app.log("arv", "touchDragged");
                    // adjust the actor's position by (current mouse position - last mouse position)
                    // in the actor's coordinate system.
                translate(x - lastX, y - lastY);
                //   rotate(2);
                    // save the current mouse position as the basis for the next drag event.
                    // we adjust by the same delta so next time drag is called, lastX/lastY
                    // are in the actor's local coordinate system automatically.
                    lastX = x - (x - lastX);
                    lastY = y - (y - lastY);
                }
            });
        }

        @Override
        public void draw (SpriteBatch batch, float parentAlpha) {
            //batch.draw(region, getX(), getY());
            batch.draw(region, getX(), getY(), mirrorActor.getOriginX(), mirrorActor.getOriginY(), mirrorActor.getWidth(), mirrorActor.getHeight(), 1, 1,getRotation(), true);
        }
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }       
}

Upvotes: -1

KenWin0x539
KenWin0x539

Reputation: 339

I implemented your code on a project of mine.

I removed your render and used the one below. Also, you shouldn't need the assets/ prefix to your image import.

skin.add("up", new Texture(Gdx.files.internal("images/coin.png")));
skin.add("def", new Texture(Gdx.files.internal("images/coin.png")));

 @Override
public void render(float delta) {
    super.render(delta);
    stage.draw();
    stage.act(Gdx.graphics.getDeltaTime());

}

Upvotes: 3

Related Questions