LibGDX in 10 Minute Slices: Drag & Drop

LibGDX Tutorial: Drag & Drop

Code: DragandDropDemo

package com.draganddropdemo.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.utils.ScreenUtils;

public class DragandDropDemo extends ApplicationAdapter {

	private Stage stage;
	private DragAndDrop dragAndDrop;
	private CustomWindow basket;
	private CustomWindow merchant;
	private Image apple;

	@Override
	public void create() {

		stage = new Stage();

		// Create our windows
		basket = createWindow(200, 200, 50, 100);
		merchant = createWindow(200, 200, 300, 100);

		// Create an apple, add to basket
		apple = new Image(new Texture("apple.png"));
		basket.add(apple);
		apple.setUserObject(basket);

		// Add the windows to the stage
		stage.addActor(basket);
		stage.addActor(merchant);

		Gdx.input.setInputProcessor(stage);

		// Create a new DragAndDrop object
		dragAndDrop = new DragAndDrop();

		dragAndDrop.addSource(new DragAndDrop.Source(apple) {
			@Override
			public DragAndDrop.Payload dragStart(InputEvent inputEvent, float v, float v1, int i) {
				DragAndDrop.Payload payload = new DragAndDrop.Payload();
				payload.setDragActor(getActor());
				stage.addActor(getActor());
				dragAndDrop.setDragActorPosition(getActor().getWidth() / 2, -getActor().getHeight() / 2);

				return payload;
			}

			@Override
			public void dragStop(InputEvent event, float x, float y, int pointer, DragAndDrop.Payload payload, DragAndDrop.Target target) {
				if (target == null){
					((CustomWindow)apple.getUserObject()).add(apple);
				}
			}
		});

		dragAndDrop.addTarget(new DragAndDrop.Target(merchant) {
			@Override
			public boolean drag(DragAndDrop.Source source, DragAndDrop.Payload payload, float v, float v1, int i) {
				return true;
			}

			@Override
			public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float v, float v1, int i) {
				merchant.add((Image) payload.getDragActor());
				apple.setUserObject(merchant);
			}
		});

		dragAndDrop.addTarget(new DragAndDrop.Target(basket) {
			@Override
			public boolean drag(DragAndDrop.Source source, DragAndDrop.Payload payload, float v, float v1, int i) {
				return true;
			}

			@Override
			public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float v, float v1, int i) {
				basket.add((Image) payload.getDragActor());
				apple.setUserObject(basket);
			}
		});

	}

	private CustomWindow createWindow(float width, float height, float positionX, float positionY) {

		CustomWindow customWindow = new CustomWindow();
		customWindow.setSize(width, height);
		customWindow.setVisible(true);
		customWindow.setMovable(true);
		customWindow.setPosition(positionX, positionY);

		return customWindow;

	}

	@Override
	public void render() {
		ScreenUtils.clear(0, 0, 0, 1);

		//camera.update();
		stage.draw();
		stage.act();

	}

	@Override
	public void dispose() {

		stage.dispose();
	}

}

Code: CustomWindow

package com.draganddropdemo.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class CustomWindow extends Window {

    private static final WindowStyle windowStyle;
    private static final ImageButton.ImageButtonStyle closeButtonStyle;

    static {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("windows.pack"));

        windowStyle = new WindowStyle(new BitmapFont(), Color.BLACK, new TextureRegionDrawable(textureAtlas.findRegion("window-1-background")));
        closeButtonStyle = new ImageButton.ImageButtonStyle();
        closeButtonStyle.imageUp = new TextureRegionDrawable(textureAtlas.findRegion("window-1-close-button"));
    }

    /**
     * Default constructor.
     */
    public CustomWindow() {
        super("", windowStyle);

        final Button closeButton = new ImageButton(closeButtonStyle);
        closeButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                setVisible(false);
            }
        });
        getTitleTable().add(closeButton).size(38, 38).padRight(10).padTop(0);

        setClip(false);
        setTransform(true);


    }
}
0
YOUR CART
  • No products in the cart.