Example usage for com.badlogic.gdx.scenes.scene2d.utils DragListener setButton

List of usage examples for com.badlogic.gdx.scenes.scene2d.utils DragListener setButton

Introduction

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

Prototype

public void setButton(int button) 

Source Link

Document

Sets the button to listen for, all other buttons are ignored.

Usage

From source file:com.kotcrab.vis.editor.util.scene2d.VisDragAndDrop.java

License:Apache License

public void addSource(final Source source) {
    DragListener listener = new DragListener() {
        public void dragStart(InputEvent event, float x, float y, int pointer) {
            if (activePointer != -1) {
                event.stop();//from   w ww. j a  v  a2 s .c o  m
                return;
            }

            activePointer = pointer;

            dragStartTime = System.currentTimeMillis();
            payload = source.dragStart(event, getTouchDownX(), getTouchDownY(), pointer);
            event.stop();

            if (cancelTouchFocus && payload != null)
                source.getActor().getStage().cancelTouchFocusExcept(this, source.getActor());
        }

        public void drag(InputEvent event, float x, float y, int pointer) {
            if (payload == null)
                return;
            if (pointer != activePointer)
                return;

            Stage stage = event.getStage();

            Touchable dragActorTouchable = null;
            if (dragActor != null) {
                dragActorTouchable = dragActor.getTouchable();
                dragActor.setTouchable(Touchable.disabled);
            }

            // Find target.
            Target newTarget = null;
            isValidTarget = false;
            float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
            Actor hit = event.getStage().hit(stageX, stageY, true); // Prefer touchable actors.
            if (hit == null)
                hit = event.getStage().hit(stageX, stageY, false);
            if (hit != null) {
                for (int i = 0, n = targets.size; i < n; i++) {
                    Target target = targets.get(i);
                    if (!target.getActor().isAscendantOf(hit))
                        continue;
                    newTarget = target;
                    target.getActor().stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                    break;
                }
            }
            //if over a new target, notify the former target that it's being left behind.
            if (newTarget != target) {
                if (target != null)
                    target.reset(source, payload);
                target = newTarget;
            }
            //with any reset out of the way, notify new targets of drag.
            if (newTarget != null) {
                isValidTarget = newTarget.drag(source, payload, tmpVector.x, tmpVector.y, pointer);
            }

            if (dragActor != null)
                dragActor.setTouchable(dragActorTouchable);

            // Add/remove and position the drag actor.
            Actor actor = null;
            if (target != null)
                actor = isValidTarget ? payload.getValidDragActor() : payload.getInvalidDragActor();
            if (actor == null)
                actor = payload.getDragActor();
            if (actor == null)
                return;
            if (dragActor != actor) {
                if (dragActor != null)
                    dragActor.remove();
                dragActor = actor;
                stage.addActor(actor);
            }
            float actorX = event.getStageX() + dragActorX;
            float actorY = event.getStageY() + dragActorY - actor.getHeight();
            if (keepWithinStage) {
                if (actorX < 0)
                    actorX = 0;
                if (actorY < 0)
                    actorY = 0;
                if (actorX + actor.getWidth() > stage.getWidth())
                    actorX = stage.getWidth() - actor.getWidth();
                if (actorY + actor.getHeight() > stage.getHeight())
                    actorY = stage.getHeight() - actor.getHeight();
            }

            actor.setPosition(actorX, actorY);

            if (currentSceneCamera != null && editingSettings.isSnapEnabledOrKeyPressed()) {
                float gridSize = gridSettings.config.gridSize;
                actor.setPosition(MathUtils.floor(currentSceneCamera.getInputX() / gridSize) * gridSize,
                        MathUtils.floor(currentSceneCamera.getInputY() / gridSize) * gridSize);

                Vector3 v = currentSceneCamera.project(tmpVector3.set(actor.getX(), actor.getY(), 0));
                actor.setPosition(v.x, v.y);
            }
        }

        public void dragStop(InputEvent event, float x, float y, int pointer) {
            if (pointer != activePointer)
                return;
            activePointer = -1;
            if (payload == null)
                return;

            if (System.currentTimeMillis() - dragStartTime < dragTime)
                isValidTarget = false;
            if (dragActor != null)
                dragActor.remove();
            if (isValidTarget) {
                float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
                target.getActor().stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                target.drop(source, payload, tmpVector.x, tmpVector.y, pointer);
            }
            source.dragStop(event, x, y, pointer, payload, isValidTarget ? target : null);
            if (target != null)
                target.reset(source, payload);
            payload = null;
            target = null;
            isValidTarget = false;
            dragActor = null;
        }
    };
    listener.setTapSquareSize(tapSquareSize);
    listener.setButton(button);
    source.getActor().addCaptureListener(listener);
    sourceListeners.put(source, listener);
}

From source file:com.uwsoft.editor.gdx.actors.CustomDragAndDrop.java

License:Apache License

public void addSource(final Source source) {
    DragListener listener = new DragListener() {
        public void dragStart(InputEvent event, float x, float y, int pointer) {
            if (activePointer != -1) {
                event.stop();//from  ww  w  .  j  a v  a2s  .c o m
                return;
            }

            activePointer = pointer;

            dragStartTime = System.currentTimeMillis();
            payload = source.dragStart(event, getTouchDownX(), getTouchDownY(), pointer);
            event.stop();
        }

        public void drag(InputEvent event, float x, float y, int pointer) {
            if (payload == null)
                return;
            if (pointer != activePointer)
                return;

            Stage stage = event.getStage();

            Touchable dragActorTouchable = null;
            if (dragActor != null) {
                dragActorTouchable = dragActor.getTouchable();
                dragActor.setTouchable(Touchable.disabled);
            }

            // Find target.
            Target newTarget = null;
            isValidTarget = false;
            float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
            Actor hit = event.getStage().hit(stageX, stageY, true); // Prefer touchable actors.
            if (hit == null)
                hit = event.getStage().hit(stageX, stageY, false);
            if (hit != null) {
                for (int i = 0, n = targets.size; i < n; i++) {
                    Target target = targets.get(i);
                    if (!target.actor.isAscendantOf(hit))
                        continue;
                    newTarget = target;
                    target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                    isValidTarget = target.drag(source, payload, tmpVector.x, tmpVector.y, pointer);
                    break;
                }
            }
            if (newTarget != target) {
                if (target != null)
                    target.reset(source, payload);
                target = newTarget;
            }

            if (dragActor != null)
                dragActor.setTouchable(dragActorTouchable);

            // Add/remove and position the drag actor.
            Actor actor = null;
            if (target != null)
                actor = isValidTarget ? payload.validDragActor : payload.invalidDragActor;
            if (actor == null)
                actor = payload.dragActor;
            if (actor == null)
                return;
            if (dragActor != actor) {
                if (dragActor != null)
                    dragActor.remove();
                dragActor = actor;
                stage.addActor(actor);
            }
            float actorX = event.getStageX() + dragActorX;
            float actorY = event.getStageY() + dragActorY - actor.getHeight();
            //            if (actorX < 0) actorX = 0;
            //            if (actorY < 0) actorY = 0;
            //            if (actorX + actor.getWidth() > stage.getWidth()) actorX = stage.getWidth() - actor.getWidth();
            //            if (actorY + actor.getHeight() > stage.getHeight()) actorY = stage.getHeight() - actor.getHeight();
            actor.setPosition(actorX, actorY);
        }

        public void dragStop(InputEvent event, float x, float y, int pointer) {
            if (pointer != activePointer)
                return;
            activePointer = -1;
            if (payload == null)
                return;

            if (System.currentTimeMillis() - dragStartTime < dragTime)
                isValidTarget = false;
            if (dragActor != null)
                dragActor.remove();
            if (isValidTarget) {
                float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
                target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                target.drop(source, payload, tmpVector.x, tmpVector.y, pointer);
            }
            source.dragStop(event, x, y, pointer, payload, isValidTarget ? target : null);
            if (target != null)
                target.reset(source, payload);
            payload = null;
            target = null;
            isValidTarget = false;
            dragActor = null;
        }
    };
    listener.setTapSquareSize(tapSquareSize);
    listener.setButton(button);
    source.actor.addCaptureListener(listener);
    sourceListeners.put(source, listener);
}