Search in sources :

Example 1 with DragAndDrop

use of com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop in project libgdx by libgdx.

the class DragAndDropTest method create.

public void create() {
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    final Skin skin = new Skin();
    skin.add("default", new LabelStyle(new BitmapFont(), Color.WHITE));
    skin.add("badlogic", new Texture("data/badlogic.jpg"));
    Image sourceImage = new Image(skin, "badlogic");
    sourceImage.setBounds(50, 125, 100, 100);
    stage.addActor(sourceImage);
    Image validTargetImage = new Image(skin, "badlogic");
    validTargetImage.setBounds(200, 50, 100, 100);
    stage.addActor(validTargetImage);
    Image invalidTargetImage = new Image(skin, "badlogic");
    invalidTargetImage.setBounds(200, 200, 100, 100);
    stage.addActor(invalidTargetImage);
    DragAndDrop dragAndDrop = new DragAndDrop();
    dragAndDrop.addSource(new Source(sourceImage) {

        public Payload dragStart(InputEvent event, float x, float y, int pointer) {
            Payload payload = new Payload();
            payload.setObject("Some payload!");
            payload.setDragActor(new Label("Some payload!", skin));
            Label validLabel = new Label("Some payload!", skin);
            validLabel.setColor(0, 1, 0, 1);
            payload.setValidDragActor(validLabel);
            Label invalidLabel = new Label("Some payload!", skin);
            invalidLabel.setColor(1, 0, 0, 1);
            payload.setInvalidDragActor(invalidLabel);
            return payload;
        }
    });
    dragAndDrop.addTarget(new Target(validTargetImage) {

        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);
        }
    });
    dragAndDrop.addTarget(new Target(invalidTargetImage) {

        public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
            getActor().setColor(Color.RED);
            return false;
        }

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

        public void drop(Source source, Payload payload, float x, float y, int pointer) {
        }
    });
}
Also used : Label(com.badlogic.gdx.scenes.scene2d.ui.Label) LabelStyle(com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle) Image(com.badlogic.gdx.scenes.scene2d.ui.Image) Texture(com.badlogic.gdx.graphics.Texture) Source(com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source) Target(com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target) DragAndDrop(com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) Payload(com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

Texture (com.badlogic.gdx.graphics.Texture)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 Stage (com.badlogic.gdx.scenes.scene2d.Stage)1 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 LabelStyle (com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 DragAndDrop (com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop)1 Payload (com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload)1 Source (com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source)1 Target (com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target)1