Untitled


SUBMITTED BY: poweromania

DATE: Oct. 15, 2018, 5:15 p.m.

FORMAT: Text only

SIZE: 3.6 kB

HITS: 233

  1. import com.badlogic.gdx.ApplicationAdapter;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
  4. import com.badlogic.gdx.graphics.Color;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  7. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  8. import com.badlogic.gdx.scenes.scene2d.Stage;
  9. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  10. import com.badlogic.gdx.scenes.scene2d.ui.Table;
  11. import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
  12. import com.badlogic.gdx.utils.viewport.ExtendViewport;
  13. public class DragAndDropTest extends ApplicationAdapter {
  14. private Stage stage;
  15. private Label.LabelStyle style;
  16. @Override
  17. public void create() {
  18. stage = new Stage(new ExtendViewport(1280, 720));
  19. style = new Label.LabelStyle(new BitmapFont(), Color.WHITE);
  20. Table left = new Table();
  21. left.top().left();
  22. left.setBounds(0, 0, stage.getWidth() / 2, stage.getHeight());
  23. stage.addActor(left);
  24. left.debug();
  25. Table right = new Table();
  26. right.left().top();
  27. right.setBounds(stage.getWidth() / 2, 0, stage.getWidth() / 2, stage.getHeight());
  28. stage.addActor(right);
  29. right.debug();
  30. DragAndDrop dragFromLeft = new DragAndDrop();
  31. // Set right table as target
  32. dragFromLeft.addTarget(new DragAndDrop.Target(right) {
  33. @Override
  34. public boolean drag(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
  35. System.out.println(this.toString());
  36. return true;
  37. }
  38. @Override
  39. public void reset(DragAndDrop.Source source, DragAndDrop.Payload payload) {
  40. System.out.println("Reset");
  41. super.reset(source, payload);
  42. }
  43. @Override
  44. public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
  45. System.out.println("Valid drop");
  46. }
  47. });
  48. // Add drag source to left table
  49. Label draggableLabel = new Label("Drag me!", style);
  50. // Add to table
  51. left.add(draggableLabel);
  52. // Add as source ot DragAndDrop
  53. dragFromLeft.addSource(new DragAndDrop.Source(draggableLabel) {
  54. @Override
  55. public DragAndDrop.Payload dragStart(InputEvent event, float x, float y, int pointer) {System.out.println("Dragstart...");
  56. DragAndDrop.Payload payload = new DragAndDrop.Payload();
  57. payload.setObject("Some data");
  58. payload.setDragActor(new Label("Some payload...", style));
  59. Label validLabel = new Label("Some payload...", style);
  60. validLabel.setColor(Color.GREEN);
  61. payload.setValidDragActor(validLabel);
  62. return payload;
  63. }
  64. });
  65. Gdx.input.setInputProcessor(stage);
  66. }
  67. @Override
  68. public void render() {
  69. Gdx.gl.glClearColor(.2f, .2f, .25f, 1);
  70. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  71. stage.act();
  72. stage.draw();
  73. }
  74. @Override
  75. public void resize(int width, int height) {
  76. super.resize(width, height);
  77. stage.getViewport().update(width, height);
  78. }
  79. public static void main (String[] args) throws Exception {
  80. new LwjglApplication(new DragAndDropTest());
  81. }
  82. }

comments powered by Disqus