1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.scenes.scene2d.utils; 18 19 import com.badlogic.gdx.Input.Buttons; 20 import com.badlogic.gdx.scenes.scene2d.InputEvent; 21 import com.badlogic.gdx.scenes.scene2d.InputListener; 22 23 /** Detects mouse or finger touch drags on an actor. A touch must go down over the actor and a drag won't start until it is moved 24 * outside the {@link #setTapSquareSize(float) tap square}. Any touch (not just the first) will trigger this listener. While 25 * pressed, other touch downs are ignored. 26 * @author Nathan Sweet */ 27 public class DragListener extends InputListener { 28 private float tapSquareSize = 14, touchDownX = -1, touchDownY = -1, stageTouchDownX = -1, stageTouchDownY = -1; 29 private int pressedPointer = -1; 30 private int button; 31 private boolean dragging; 32 private float deltaX, deltaY; 33 touchDown(InputEvent event, float x, float y, int pointer, int button)34 public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 35 if (pressedPointer != -1) return false; 36 if (pointer == 0 && this.button != -1 && button != this.button) return false; 37 pressedPointer = pointer; 38 touchDownX = x; 39 touchDownY = y; 40 stageTouchDownX = event.getStageX(); 41 stageTouchDownY = event.getStageY(); 42 return true; 43 } 44 touchDragged(InputEvent event, float x, float y, int pointer)45 public void touchDragged (InputEvent event, float x, float y, int pointer) { 46 if (pointer != pressedPointer) return; 47 if (!dragging && (Math.abs(touchDownX - x) > tapSquareSize || Math.abs(touchDownY - y) > tapSquareSize)) { 48 dragging = true; 49 dragStart(event, x, y, pointer); 50 deltaX = x; 51 deltaY = y; 52 } 53 if (dragging) { 54 deltaX -= x; 55 deltaY -= y; 56 drag(event, x, y, pointer); 57 deltaX = x; 58 deltaY = y; 59 } 60 } 61 touchUp(InputEvent event, float x, float y, int pointer, int button)62 public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 63 if (pointer == pressedPointer) { 64 if (dragging) dragStop(event, x, y, pointer); 65 cancel(); 66 } 67 } 68 dragStart(InputEvent event, float x, float y, int pointer)69 public void dragStart (InputEvent event, float x, float y, int pointer) { 70 } 71 drag(InputEvent event, float x, float y, int pointer)72 public void drag (InputEvent event, float x, float y, int pointer) { 73 } 74 dragStop(InputEvent event, float x, float y, int pointer)75 public void dragStop (InputEvent event, float x, float y, int pointer) { 76 } 77 78 /* If a drag is in progress, no further drag methods will be called until a new drag is started. */ cancel()79 public void cancel () { 80 dragging = false; 81 pressedPointer = -1; 82 } 83 84 /** Returns true if a touch has been dragged outside the tap square. */ isDragging()85 public boolean isDragging () { 86 return dragging; 87 } 88 setTapSquareSize(float halfTapSquareSize)89 public void setTapSquareSize (float halfTapSquareSize) { 90 tapSquareSize = halfTapSquareSize; 91 } 92 getTapSquareSize()93 public float getTapSquareSize () { 94 return tapSquareSize; 95 } 96 getTouchDownX()97 public float getTouchDownX () { 98 return touchDownX; 99 } 100 getTouchDownY()101 public float getTouchDownY () { 102 return touchDownY; 103 } 104 getStageTouchDownX()105 public float getStageTouchDownX () { 106 return stageTouchDownX; 107 } 108 getStageTouchDownY()109 public float getStageTouchDownY () { 110 return stageTouchDownY; 111 } 112 113 /** Returns the amount on the x axis that the touch has been dragged since the last drag event. */ getDeltaX()114 public float getDeltaX () { 115 return deltaX; 116 } 117 118 /** Returns the amount on the y axis that the touch has been dragged since the last drag event. */ getDeltaY()119 public float getDeltaY () { 120 return deltaY; 121 } 122 getButton()123 public int getButton () { 124 return button; 125 } 126 127 /** Sets the button to listen for, all other buttons are ignored. Default is {@link Buttons#LEFT}. Use -1 for any button. */ setButton(int button)128 public void setButton (int button) { 129 this.button = button; 130 } 131 } 132