import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
public class DragAndDropTest extends ApplicationAdapter {
private Stage stage;
private Label.LabelStyle style;
@Override
public void create() {
stage = new Stage(new ExtendViewport(1280, 720));
style = new Label.LabelStyle(new BitmapFont(), Color.WHITE);
Table left = new Table();
left.top().left();
left.setBounds(0, 0, stage.getWidth() / 2, stage.getHeight());
stage.addActor(left);
left.debug();
Table right = new Table();
right.left().top();
right.setBounds(stage.getWidth() / 2, 0, stage.getWidth() / 2, stage.getHeight());
stage.addActor(right);
right.debug();
DragAndDrop dragFromLeft = new DragAndDrop();
// Set right table as target
dragFromLeft.addTarget(new DragAndDrop.Target(right) {
@Override
public boolean drag(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
System.out.println(this.toString());
return true;
}
@Override
public void reset(DragAndDrop.Source source, DragAndDrop.Payload payload) {
System.out.println("Reset");
super.reset(source, payload);
}
@Override
public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
System.out.println("Valid drop");
}
});
// Add drag source to left table
Label draggableLabel = new Label("Drag me!", style);
// Add to table
left.add(draggableLabel);
// Add as source ot DragAndDrop
dragFromLeft.addSource(new DragAndDrop.Source(draggableLabel) {
@Override
public DragAndDrop.Payload dragStart(InputEvent event, float x, float y, int pointer) {System.out.println("Dragstart...");
DragAndDrop.Payload payload = new DragAndDrop.Payload();
payload.setObject("Some data");
payload.setDragActor(new Label("Some payload...", style));
Label validLabel = new Label("Some payload...", style);
validLabel.setColor(Color.GREEN);
payload.setValidDragActor(validLabel);
return payload;
}
});
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
Gdx.gl.glClearColor(.2f, .2f, .25f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
stage.getViewport().update(width, height);
}
public static void main (String[] args) throws Exception {
new LwjglApplication(new DragAndDropTest());
}
}