Example usage for com.badlogic.gdx.scenes.scene2d.utils DragAndDrop addTarget

List of usage examples for com.badlogic.gdx.scenes.scene2d.utils DragAndDrop addTarget

Introduction

In this page you can find the example usage for com.badlogic.gdx.scenes.scene2d.utils DragAndDrop addTarget.

Prototype

public void addTarget(Target target) 

Source Link

Usage

From source file:com.mangogames.imagealphabets.ui.layer.AlphabetsLayer.java

License:Open Source License

/**
 * Perform DragAndDrop functionality  /* w  ww  .  j  a  v  a 2s  .  c o m*/
 * @param actor
 */
public void handleTouchEvents(final Actor actor) {
    final AlphabetSprite alphabetSprite = (AlphabetSprite) actor;
    DragAndDrop dragAndDrop = new DragAndDrop();

    char ch = alphabetSprite.getAlphabetName().charAt(0);
    String s = ch + "2";

    if (alphabetSprite.getAlphabetName().equals(s)) {
        dragAndDrop.addSource(new Source(alphabetSprite) {

            @Override
            public Payload dragStart(InputEvent event, float x, float y, int pointer) {
                alphabetSprite.setDropSuccessful(false);
                setAlphabetSprite(0, alphabetSprite.getColpos(), true);
                Payload payload = new Payload();
                payload.setDragActor(alphabetSprite);
                return payload;
            }

            @Override
            public void dragStop(InputEvent event, float x, float y, int pointer, Target target) {
                super.dragStop(event, x, y, pointer, target);
                if (alphabetSprite.isDropSuccessful() == false) {
                    setAlphabetSprite(0, alphabetSprite.getColpos(), false);
                }

            }
        });

        dragAndDrop.addTarget(new Target(boardScene.getBackgroundLayer().image) {
            public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
                return true;
            }

            @Override
            public void drop(Source source, Payload payload, float x, float y, int pointer) {
                setAlphabetSprite(0, ((AlphabetSprite) payload.getDragActor()).getColpos(), false);
                if (alphabetSprite.isDropSuccessful() == false)
                    alphabetSprite.setDropSuccessful(true);
            }
        });

        int length = boardScene.getImagesLayer().imageTable.getCells().size();
        for (int i = 0; i < length; i++) {
            final int col = i;
            final ImageSprite imageSprite = boardScene.getImageSprite(0, col);
            dragAndDrop.addTarget(new Target(imageSprite) {
                public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
                    getActor().setColor(Color.GREEN);
                    return true;
                }

                @Override
                public void drop(Source source, Payload payload, float x, float y, int pointer) {
                    if (alphabetSprite.isDropSuccessful() == false)
                        alphabetSprite.setDropSuccessful(true);
                    if (imageSprite.getImageName().charAt(0) == alphabetSprite.getAlphabetName().charAt(0)) {
                        boardScene.setImageSprite(0, col);
                        String alphasound = imageSprite.getImageName().charAt(0) + ".mp3";
                        sound = loadMusic(alphasound);
                        sound.play();
                        // sound.dispose();
                        count++;
                        if (count == 3) {
                            addAction(Actions.sequence(Actions.delay(2f),
                                    Actions.run(onCountThreeFinishedRunnable)));
                        }
                    } else {
                        setAlphabetSprite(0, ((AlphabetSprite) payload.getDragActor()).getColpos(), false);
                    }
                }
            });
        }
    }
}

From source file:com.ore.infinium.HotbarInventoryView.java

License:Open Source License

public HotbarInventoryView(Stage stage, Skin skin, Inventory hotbarInventory, Inventory inventory,
        DragAndDrop dragAndDrop, OreClient client) {
    m_skin = skin;/*from w w w.j  a v  a2s . c  o  m*/
    m_inventory = inventory;
    m_client = client;
    m_stage = stage;

    m_hotbarInventory = hotbarInventory;
    //attach to the inventory model
    m_hotbarInventory.addListener(this);

    container = new Table(m_skin);
    container.setFillParent(true);
    container.top().left().setSize(800, 100);
    container.padLeft(10).padTop(10);

    container.defaults().space(4);

    stage.addActor(container);

    Image dragImage = new Image();
    dragImage.setSize(32, 32);

    for (byte i = 0; i < Inventory.maxHotbarSlots; ++i) {

        Image slotImage = new Image();

        SlotElement element = new SlotElement();
        m_slots[i] = element;

        element.itemImage = slotImage;

        Table slotTable = new Table(m_skin);
        element.table = slotTable;
        slotTable.setTouchable(Touchable.enabled);
        slotTable.addListener(new SlotClickListener(this, i));
        slotTable.addListener(new SlotInputListener(this, i));

        slotTable.add(slotImage);
        slotTable.background("default-pane");

        slotTable.row();

        Label itemCount = new Label(null, m_skin);
        slotTable.add(itemCount).bottom().fill();
        element.itemCountLabel = itemCount;

        //            container.add(slotTable).size(50, 50);
        container.add(slotTable).fill().size(50, 50);
        setHotbarSlotVisible(i, false);

        dragAndDrop.addSource(new HotbarDragSource(slotTable, i, dragImage, this));

        dragAndDrop.addTarget(new HotbarDragTarget(slotTable, i, this));
    }

    m_tooltip = new Label(null, m_skin);
    stage.addActor(m_tooltip);
}

From source file:com.ore.infinium.InventoryView.java

License:Open Source License

public InventoryView(Stage stage, Skin skin, Inventory hotbarInventory, Inventory inventory,
        DragAndDrop dragAndDrop, OreClient client) {
    m_skin = skin;/*  ww w .j  a  va2 s  .  c om*/
    m_inventory = inventory;
    m_client = client;

    //attach to the inventory model
    m_inventory.addListener(this);

    m_hotbarInventory = hotbarInventory;

    Table container = new Table(m_skin);
    container.setFillParent(true);
    container.center(); //top().right().setSize(800, 100);
    container.defaults().space(4);
    container.padLeft(10).padTop(10);

    m_window = new Window("Inventory", m_skin);
    //HACK;not centering or anythign, all hardcoded :(
    m_window.setPosition(900, 100);
    m_window.top().right().setSize(400, 500);
    //        window.defaults().space(4);
    //window.pack();
    m_window.add(container).fill().expand();

    TextureRegion region = m_client.m_world.m_tileRenderer.m_tilesAtlas.findRegion("dirt-00");
    Image dragImage = new Image(region);
    dragImage.setSize(32, 32);

    final int slotsPerRow = 5;
    byte i = 0;
    while (i < Inventory.maxSlots) {
        for (int slot = 0; slot < slotsPerRow && i < Inventory.maxSlots; ++slot, ++i) {
            Image slotImage = new Image();

            SlotElement element = new SlotElement();
            m_slots[i] = element;
            element.itemImage = slotImage;

            Table slotTable = new Table(m_skin);
            slotTable.setTouchable(Touchable.enabled);
            element.table = slotTable;

            slotTable.add(slotImage);
            slotTable.background("default-pane");

            slotTable.row();

            Label itemName = new Label(null, m_skin);
            slotTable.add(itemName).bottom().fill();
            element.itemCountLabel = itemName;

            container.add(slotTable).size(50, 50);
            //            window.add(slotTable).fill().size(50, 50);

            dragAndDrop.addSource(new InventoryDragSource(slotTable, i, dragImage, this));

            dragAndDrop.addTarget(new InventoryDragTarget(slotTable, i, this));
        }

        container.row();
    }

    stage.addActor(m_window);
    setVisible(false);
}

From source file:com.pixelscientists.gdx.inventory.InventoryActor.java

License:Open Source License

public InventoryActor(Inventory inventory, DragAndDrop dragAndDrop, Skin skin) {
    super("Inventory...", skin);

    TextButton closeButton = new TextButton("X", skin);
    closeButton.addListener(new HidingClickListener(this));
    getButtonTable().add(closeButton).height(getPadTop());

    setPosition(400, 100);/*w  w w  .  j  ava2s.  c o  m*/
    defaults().space(8);
    row().fill().expandX();

    int i = 0;
    for (Slot slot : inventory.getSlots()) {
        SlotActor slotActor = new SlotActor(skin, slot);
        dragAndDrop.addSource(new SlotSource(slotActor));
        dragAndDrop.addTarget(new SlotTarget(slotActor));
        add(slotActor);

        i++;
        if (i % 5 == 0) {
            row();
        }
    }

    pack();

    // it is hidden by default
    setVisible(false);
}

From source file:com.uwsoft.editor.gdx.ui.UILightBox.java

License:Apache License

public void initContent() {
    SceneLoader sceneLoader = stage.sceneLoader;

    CompositeItem ui = sceneLoader.getCompositeElementById("lightBox");
    ui.setX(7);//from   www .  j a v a2s.c  om
    ui.setY(getHeight() - ui.getHeight() - 14);
    mainLayer.addActor(ui);

    cPicker = new ColorPickerButton();
    cPicker.setX(100);
    cPicker.setY(getHeight() - 92);
    mainLayer.addActor(cPicker);

    float[] ambientColor = stage.getSandbox().sceneControl.getCurrentSceneVO().ambientColor;
    cPicker.setColorValue(new Color(ambientColor[0], ambientColor[1], ambientColor[2], ambientColor[3]));

    //System.out.println("ambient " +cPicker.getColorValue().toString());

    ImageItem bulb = ui.getImageById("bulbBtn");
    ImageItem cone = ui.getImageById("coneBtn");

    cPickerElems = new ColorPickerButton();
    cPickerElems.setX(85);
    cPickerElems.setY(getHeight() - 196);
    mainLayer.addActor(cPickerElems);

    //
    // Image coneThumb = new Image(TextureManager.getInstance().getEditorAsset("cone"));

    cPicker.addListener(new ClickListener() {
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            super.touchUp(event, x, y, pointer, button);

            ColorPicker picker = new ColorPicker(new ColorPickerAdapter() {
                @Override
                public void finished(Color newColor) {
                    cPicker.setColorValue(newColor);

                    // change scene ambient light
                    stage.getSandbox().setSceneAmbientColor(newColor);
                }
            });
            stage.addActor(picker.fadeIn());
        }
    });

    cPickerElems.addListener(new ClickListener() {
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            super.touchUp(event, x, y, pointer, button);
            ColorPicker picker = new ColorPicker(new ColorPickerAdapter() {
                @Override
                public void finished(Color newColor) {
                    cPickerElems.setColorValue(newColor);
                }
            });
            stage.addActor(picker.fadeIn());
        }
    });

    final DragAndDrop dragAndDropBulb = new DragAndDrop();
    dragAndDropBulb.addSource(new DragAndDrop.Source(bulb) {
        public DragAndDrop.Payload dragStart(InputEvent event, float x, float y, int pointer) {

            DragAndDrop.Payload payload = new DragAndDrop.Payload();
            Image bulbThumb = new Image(DataManager.getInstance().textureManager.getEditorAsset("bulb"));
            payload.setDragActor(bulbThumb);
            dragAndDropBulb.setDragActorPosition(-bulbThumb.getWidth() / 2, bulbThumb.getHeight() / 2);

            return payload;
        }
    });

    dragAndDropBulb.addTarget(new DragAndDrop.Target(stage.dummyTarget) {

        @Override
        public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y,
                int pointer) {
            if (disableLights.isChecked())
                return;
            LightVO vo = new LightVO();
            Color tint = cPickerElems.getColorValue();

            float[] clr = new float[4];
            clr[0] = tint.r;
            clr[1] = tint.g;
            clr[2] = tint.b;
            clr[3] = tint.a;
            vo.tint = clr;

            vo.type = LightType.POINT;
            stage.getSandbox().getUac().createLight(vo, x, y);
        }

        @Override
        public boolean drag(DragAndDrop.Source arg0, DragAndDrop.Payload arg1, float arg2, float arg3,
                int arg4) {

            return true;
        }
    });

    final DragAndDrop dragAndDropCone = new DragAndDrop();
    dragAndDropCone.addSource(new DragAndDrop.Source(cone) {
        public DragAndDrop.Payload dragStart(InputEvent event, float x, float y, int pointer) {

            DragAndDrop.Payload payload = new DragAndDrop.Payload();
            Image coneThumb = new Image(DataManager.getInstance().textureManager.getEditorAsset("cone"));
            payload.setDragActor(coneThumb);
            dragAndDropCone.setDragActorPosition(-coneThumb.getWidth() / 2, coneThumb.getHeight() / 2);

            return payload;
        }
    });

    dragAndDropCone.addTarget(new DragAndDrop.Target(stage.dummyTarget) {

        @Override
        public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y,
                int pointer) {
            if (disableLights.isChecked())
                return;
            LightVO vo = new LightVO();
            vo.type = LightType.CONE;
            Color tint = cPickerElems.getColorValue();

            float[] clr = new float[4];
            clr[0] = tint.r;
            clr[1] = tint.g;
            clr[2] = tint.b;
            clr[3] = tint.a;
            vo.tint = clr;
            stage.getSandbox().getUac().createLight(vo, x, y);
        }

        @Override
        public boolean drag(DragAndDrop.Source arg0, DragAndDrop.Payload arg1, float arg2, float arg3,
                int arg4) {

            return true;
        }
    });

    disableLights = ui.getCheckBoxById("disableLights");
    disableLights.addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            super.touchUp(event, x, y, pointer, button);
            stage.getSandbox().sceneControl.disableLights(disableLights.isChecked());
        }

    });

    disableAmbiance = ui.getCheckBoxById("disableAmbiance");
    disableAmbiance.addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            super.touchUp(event, x, y, pointer, button);
            stage.getSandbox().getSceneControl().disableAmbience(disableAmbiance.isChecked());
        }

    });
}

From source file:es.eucm.ead.editor.view.widgets.layouts.TrackLayout.java

License:Open Source License

/**
 * Creates a new {@link TrackLayout} with a dragNDrop {@link DragAndDrop}
 * and a background./*from  w  ww .  j a va 2 s  .c o  m*/
 * 
 * @param background
 * @param dragNDrop
 */
public TrackLayout(Drawable background, DragAndDrop dragNDrop) {
    super(true, background);

    this.scroll = null;

    this.dragNDrop = dragNDrop;

    dragNDrop.addTarget(new Target(this) {
        @Override
        public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
            return true;
        }

        @Override
        public void drop(Source source, Payload payload, float x, float y, int pointer) {
            if (payload.getDragActor() != null) {
                int index = 0;
                for (Actor actor : TrackLayout.this.getChildren()) {
                    if (!(actor instanceof FixedButton) && x > getLeftMargin(actor) + actor.getWidth() / 2) {
                        index++;
                    }
                }
                TrackLayout.this.add(index, payload.getDragActor(), x);
                layout();
            }
        }
    });
}