4

I'm trying to put together a drag and drop system in libGDX with the help of DragAndDrop class. I have the typical system in which you can drag something into a box.

So far it is working all dough I don't know if I am doing it in the most optimal way because but the problem is that one of the draggable object contains two more targets.

So say I have all draggable objects on the left and the boxes (the targets) on the right.

This is what I do for every draggable object:

dragAndDrop = new DragAndDrop();
dragAndDrop.addSource(new Source(icon) {
            @Override
            public Payload dragStart (InputEvent event, float x, float y, int pointer) {        
                Payload payload = new Payload();

                payload.setDragActor(drag);
                payload.setValidDragActor(valid);
                payload.setObject(icon);
                icon.addAction(Actions.alpha(0.7f));
                return payload;
            }
            @Override
            public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, Target target){
                icon.addAction(Actions.alpha(1));
                if(target!=null){
                    draggedInDragBox((CommandBin) target.getActor());
                }

            }
        });

The DraggableObject extends Group and contains Image icon. That's what I pass to the constructor of the source. (Maybe I should pass all the object [this]). I also have the images drag and valid in the DraggableObject. The actual Texture is the same for all icon, valid and drag but I think I need all 3 objects to make it work. Am I right?

Then in the boxes I do:

dragAndDrop.addTarget(new Target(this) {
        //Logic is in source
        public boolean drag (Source source, Payload payload, float x, float y, int pointer) {
            return true;
        }

        public void reset (Source source, Payload payload) {
        }

        public void drop (Source source, Payload payload, float x, float y, int pointer) {

        }
    });

So I pass the object DragBox to the constructor of the target. As you can see all the logic is in the Source. Ass you can tell there. What I do is. If target =! null it means that we dropped the object in a valid place so I want to add the DraggableObject to the DragBox (addActor).

public void draggedInDragBox(DraggableObject do){
    //We add the command in the box
    addCommand(_this); //This simply calls addActor(do) in DragBox
    //We create a new draggable object to be able to use it again
    DraggableObject drOb = new DraggableObject();
    northController.draggableObjectsContainer.addActor(drOb);
}

The difference between a normal DraggableObject and the one that displays subDragBoxes (which extends the first one) is this in the constructor:

public RedDraggableBox() {
    super();
    subdraggableBox[0] = new DraggableBox();
    subdraggableBox[1] = new DraggableBox();
}

And when it is dragged to a main box I just call:

public void showSubDraggableBoxes(){
    addActor(subdraggableBox[0]);
    addActor(subdraggableBox[1]);
    icon.toFront();
}

This is what I want to accomplish.

Say the red is a special DraggableObject that creates subdraggable boxes when placed in a draggable box. So when it is there in the box I add to the group DraggableObject the two boxes. Currently I can drag any DraggableObject to a main box. When I drag an object to a box that has already an object it destroys the current object and places the new one instead, which is exactly what I want it to do.

The problem with red DraggableObjects is that when I Drag something to one of its subDraggableBoxes it erases the red draggable object (with the boxes) and substitutes it by the new one in the main DraggableBox. So by the time a RedDragBox is visible: RedDragableBox.parent() -> RedDraggableObject, RedDraggableObject.parent() -> DraggableBox (the main one).

Thanks in advance.

yafrack
  • 163
  • 5

0 Answers0