Advertisement
Guest User

Untitled

a guest
May 11th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. public class InputManager extends InputAdapter {
  2.  
  3.     private Vector2 lastTouch = new Vector2();
  4.     private Queue<Integer> activeTouch = new Queue<Integer>();
  5.     private boolean xLeft, xRight, yDown, yUp;
  6.  
  7.     public InputManager() {
  8.         xLeft = false; xRight = false; yDown = false; yUp = false;
  9.     }
  10.  
  11.     @Override
  12.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  13.         activeTouch.addLast(pointer);
  14.         return super.touchDown(screenX, screenY, pointer, button);
  15.     }
  16.  
  17.     @Override
  18.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  19.         if (activeTouch.size > 0) {
  20.             activeTouch.removeValue(pointer, true);
  21.         }
  22.  
  23.         return super.touchUp(screenX, screenY, pointer, button);
  24.     }
  25.  
  26.     @Override
  27.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  28.         System.out.println("Dragging:" + pointer + " Queue: " + activeTouch);
  29.         if (pointer == activeTouch.first()) {
  30.             Vector2 newTouch = new Vector2(screenX, screenY);
  31.             Vector2 delta = newTouch.cpy().sub(lastTouch);
  32.             lastTouch = newTouch;
  33.  
  34.             if (delta.x < 0) {
  35.                 xLeft = true;
  36.                 xRight = false;
  37.             }
  38.             else if (delta.x > 0) {
  39.                 xRight = true;
  40.                 xLeft = false;
  41.             }
  42.             else {
  43.                 xLeft = false;
  44.                 xRight = false;
  45.             }
  46.  
  47.             if (delta.y < 0) {
  48.                 yDown = false;
  49.                 yUp = true;
  50.             }
  51.             else if (delta.y > 0) {
  52.                 yUp = false;
  53.                 yDown = true;
  54.             }
  55.             else {
  56.                 yUp = false;
  57.                 yDown = false;
  58.             }
  59.         }
  60.         return super.touchDragged(screenX, screenY, pointer);
  61.     }
  62.  
  63.     public boolean isRightMoved() {
  64.         return xRight;
  65.     }
  66.  
  67.     public boolean isLeftMoved() {
  68.         return xLeft;
  69.     }
  70.  
  71.     public boolean isUpMoved() {
  72.         return yUp;
  73.     }
  74.  
  75.     public boolean isDownMoved() {
  76.         return yDown;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement