Facebook
From Beige Gibbon, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 205
  1. public class JoystickView extends SurfaceView
  2.         implements SurfaceHolder.Callback, View.OnTouchListener {
  3.  
  4.     private float centerX;
  5.     private float centerY;
  6.     private float baseRadius;
  7.     private float hatRadius;
  8.     private RightJoystickListener rightJoystickListener;
  9.     private LeftJoystickListener leftJoystickListener;
  10.  
  11.     private void setupDimensions() {
  12.         centerX = getWidth() / 2;
  13.         centerY = getHeight() / 2;
  14.         baseRadius = Math.min(getWidth(), getHeight()) / 3;
  15.         hatRadius = Math.min(getWidth(), getHeight()) / 6;
  16.  
  17.     }
  18.  
  19.     public JoystickView(Context context) {
  20.         super(context);
  21.         getHolder().addCallback(this);
  22.         setOnTouchListener(this);
  23.         if(context instanceof RightJoystickListener) {
  24.             rightJoystickListener = (RightJoystickListener) context;
  25.         }
  26.         if(context instanceof LeftJoystickListener) {
  27.             leftJoystickListener = (LeftJoystickListener) context;
  28.         }
  29.  
  30.         getHolder().setFormat(PixelFormat.TRANSLUCENT);
  31.     }
  32.  
  33.     public JoystickView(Context context, AttributeSet attrs) {
  34.         super(context, attrs);
  35.         getHolder().addCallback(this);
  36.         setOnTouchListener(this);
  37.         if(context instanceof RightJoystickListener) {
  38.             rightJoystickListener = (RightJoystickListener) context;
  39.         }
  40.         if(context instanceof LeftJoystickListener) {
  41.             leftJoystickListener = (LeftJoystickListener) context;
  42.         }
  43.  
  44.         getHolder().setFormat(PixelFormat.TRANSLUCENT);
  45.     }
  46.  
  47.     public JoystickView(Context context, AttributeSet attrs, int defStyleAttr) {
  48.         super(context, attrs, defStyleAttr);
  49.         getHolder().addCallback(this);
  50.         setOnTouchListener(this);
  51.         if(context instanceof RightJoystickListener) {
  52.             rightJoystickListener = (RightJoystickListener) context;
  53.         }
  54.         if(context instanceof LeftJoystickListener) {
  55.             leftJoystickListener = (LeftJoystickListener) context;
  56.         }
  57.     }
  58.  
  59.     private void drawJoystick(float newX, float newY) {
  60.         if (getHolder().getSurface().isValid()) {
  61.             Canvas myCanvas = this.getHolder().lockCanvas();
  62.             Paint colors = new Paint();
  63.             myCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
  64.             colors.setARGB(100, 193, 193, 193);
  65.             myCanvas.drawCircle(centerX, centerY, baseRadius, colors);
  66.             colors.setARGB(255, 193, 193, 193);
  67.             myCanvas.drawCircle(newX, newY, hatRadius, colors);
  68.             getHolder().unlockCanvasAndPost(myCanvas);
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public void surfaceCreated(SurfaceHolder holder) {
  74.         setupDimensions();
  75.         drawJoystick(centerX, centerY);
  76.     }
  77.  
  78.     @Override
  79.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  80.  
  81.     }
  82.  
  83.     @Override
  84.     public void surfaceDestroyed(SurfaceHolder holder) {
  85.  
  86.     }
  87.  
  88.     public boolean onTouch(View v, MotionEvent e) {
  89.         if(v.equals(this)) {
  90.             if(e.getAction() != e.ACTION_UP) {
  91.                 float displacement = (float)Math.sqrt((Math.pow(e.getX() - centerX, 2)) + Math.pow(e.getY() - centerY, 2));
  92.                 if(displacement < baseRadius) {
  93.                     drawJoystick(e.getX(), e.getY());
  94.  
  95.                 } else {
  96.                     float ratio = baseRadius / displacement;
  97.                     float constrainedX = centerX + (e.getX() - centerX) * ratio;
  98.                     float constrainedY = centerY + (e.getY() - centerY) * ratio;
  99.                     drawJoystick(constrainedX, constrainedY);
  100.                     rightJoystickListener.onRightJoystickMoved(
  101.                             (constrainedX - centerX) / baseRadius, (constrainedY - centerY) / baseRadius
  102.                             , getId());
  103.                     leftJoystickListener.onLeftJoystickMoved(
  104.                             (constrainedX - centerX) / baseRadius, (constrainedY - centerY) / baseRadius
  105.                             , getId());
  106.                 }
  107.             } else {
  108.                 drawJoystick(centerX, centerY);
  109.                 leftJoystickListener.onLeftJoystickMoved(0,0, getId());
  110.                 rightJoystickListener.onRightJoystickMoved(0,0, getId());
  111.             }
  112.         }
  113.         return true;
  114.     }
  115.     public interface RightJoystickListener {
  116.         void onRightJoystickMoved(float xPercent, float yPercent, int id);
  117.     }
  118.     public interface LeftJoystickListener {
  119.         void onLeftJoystickMoved(float xPercent, float yPercent, int id);
  120.     }
  121.  
  122. }