Guest User

SimpleDragListener

a guest
Jan 18th, 2018
706
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ogurecapps.listeners;
  2.  
  3. import com.badlogic.gdx.scenes.scene2d.Actor;
  4. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  5. import com.badlogic.gdx.scenes.scene2d.actions.Actions;
  6. import com.badlogic.gdx.scenes.scene2d.utils.DragListener;
  7.  
  8. /**
  9.  * Created by coder1cv8 on 28.03.2017.
  10.  */
  11.  
  12. public class SimpleDragListener extends DragListener {
  13.  
  14.     public float offsetX;
  15.     public float offsetY;
  16.  
  17.     @Override
  18.     public void dragStart(InputEvent event, float x, float y, int pointer) {
  19.         final Actor actor = event.getTarget();
  20.  
  21.         offsetX = event.getStageX() - actor.getX();
  22.         offsetY = event.getStageY() - actor.getY();
  23.     }
  24.  
  25.     @Override
  26.     public void drag(InputEvent event, float x, float y, int pointer) {
  27.         float setX = event.getStageX() - offsetX;
  28.         float setY = event.getStageY() - offsetY;
  29.  
  30.         final Actor actor = event.getTarget();
  31.  
  32.         float borderLeft = 0;
  33.         float borderRight = 1280f - actor.getWidth();
  34.         float borderTop = 840f - actor.getHeight();
  35.         float borderBottom = 120f;
  36.  
  37.         if (setX < borderLeft) {
  38.             setX = borderLeft;
  39.         } else if (setX > borderRight) {
  40.             setX = borderRight;
  41.         }
  42.  
  43.         if (setY > borderTop) {
  44.             setY = borderTop;
  45.         } else if (setY < borderBottom) {
  46.             setY = borderBottom;
  47.         }
  48.  
  49.         actor.setPosition(setX, setY);
  50.     }
  51. }
RAW Paste Data