Search code examples
javalibgdxdrag-and-dropscene2d

Drag and drop only triggers from the bottom of the Actor Libgdx


I have a table which contains in every cell another table. For example that table can have inside 8 other tables. I added a drag and drop so I can drag the smaller tables. It works but only partially. The drag and drop only gets detected if I touch on the bottom of the smaller tables, it doesn't trigger if I touch on the middle of them or on the top. I tried to set the width and height of the smaller tables, and then the drag and drop also triggers from a point on the top, but still not from everywhere it should? What could be wrong?

My code: In code, collectionTable is the bigger table and TestCard are the types of the smaller tables that get added to collectionTable.

dnd = new DragAndDrop();
dnd.setDragTime(50);
for (final Actor x : collectionTable.getChildren()) {
            TestCard y = (TestCard) x;
            y.setWidth(Gdx.graphics.getWidth() / (7 * scale));
            y.setHeight(Gdx.graphics.getHeight() / (3.3f * scale));
            dnd.addSource(new CardSource(y)); 
dnd.addTarget(new Target(selectedDeck);
//not adding the code from the target as it works as intended

    class CardSource extends Source {

        final Payload payload = new Payload();

        TestCard card;

        public CardSource(Actor actor) {
            super(actor);
        }

        public CardSource(TestCard card) {
            super(card);
            this.card = card;
            card.setWidth(Gdx.graphics.getWidth() / (7 * scale));
            card.setHeight(Gdx.graphics.getHeight() / (3.3f * scale));
        }

        public Payload dragStart(InputEvent event, float x, float y, int pointer) {
            payload.setObject(card.);
            payload.setDragActor(new TestCard(card));
            //payload.setInvalidDragActor(invalidActor);
            payload.setValidDragActor(new TestCard(card));
            return payload;
        }
    }

Solution

  • use: setTouchabe(Touchable.enabled)

    on the parent table. By default a 'group' (WidgetGroup or Table) only register touch on actual visible actors like buttons and labels.