From 63a60959c32773bff2d7d2dceef458f2920e8ea1 Mon Sep 17 00:00:00 2001 From: edgar Date: Sun, 22 Jan 2017 00:03:10 -0800 Subject: [PATCH] Not Used. added type, 2-axis, 4-axis, 8-axis --- .../java/com/erz/joystick/MainActivity.java | 2 +- .../com/erz/joysticklibrary/JoyStick.java | 59 ++++++++++++------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/erz/joystick/MainActivity.java b/app/src/main/java/com/erz/joystick/MainActivity.java index ac57d30..5e2016e 100644 --- a/app/src/main/java/com/erz/joystick/MainActivity.java +++ b/app/src/main/java/com/erz/joystick/MainActivity.java @@ -28,7 +28,7 @@ public class MainActivity extends AppCompatActivity implements JoyStick.JoyStick } @Override - public void onMove(JoyStick joyStick, double angle, double power) { + public void onMove(JoyStick joyStick, double angle, double power, int direction) { switch (joyStick.getId()) { case R.id.joy1: gameView.move(angle, power); diff --git a/joysticklibrary/src/main/java/com/erz/joysticklibrary/JoyStick.java b/joysticklibrary/src/main/java/com/erz/joysticklibrary/JoyStick.java index a2658c5..8379fbb 100644 --- a/joysticklibrary/src/main/java/com/erz/joysticklibrary/JoyStick.java +++ b/joysticklibrary/src/main/java/com/erz/joysticklibrary/JoyStick.java @@ -33,19 +33,25 @@ import android.view.View; */ public class JoyStick extends View { - public static final int CENTER = -1; - public static final int LEFT = 0; - public static final int LEFT_UP = 1; - public static final int UP = 2; - public static final int UP_RIGHT = 3; - public static final int RIGHT = 4; - public static final int RIGHT_DOWN = 5; - public static final int DOWN = 6; - public static final int DOWN_LEFT = 7; + public static final int DIRECTION_CENTER = -1; + public static final int DIRECTION_LEFT = 0; + public static final int DIRECTION_LEFT_UP = 1; + public static final int DIRECTION_UP = 2; + public static final int DIRECTION_UP_RIGHT = 3; + public static final int DIRECTION_RIGHT = 4; + public static final int DIRECTION_RIGHT_DOWN = 5; + public static final int DIRECTION_DOWN = 6; + public static final int DIRECTION_DOWN_LEFT = 7; + + public static final int TYPE_8_AXIS = 11; + public static final int TYPE_4_AXIS = 22; + public static final int TYPE_2_AXIS_LEFT_RIGHT = 33; + public static final int TYPE_2_AXIS_UP_DOWN = 44; private JoyStickListener listener; private Paint paint; - private int direction = CENTER; + private int direction = DIRECTION_CENTER; + private int type = TYPE_8_AXIS; private float centerX; private float centerY; private float posX; @@ -177,16 +183,7 @@ public class JoyStick extends View { * (posX - centerX) + (posY - centerY) * (posY - centerY)) / radius); - double degrees = Math.toDegrees(angle); - if ((degrees >= 0 && degrees < 22.5) || (degrees < 0 && degrees > -22.5)) direction = LEFT; - else if (degrees >= 22.5 && degrees < 67.5) direction = LEFT_UP; - else if (degrees >= 67.5 && degrees < 112.5) direction = UP; - else if (degrees >= 112.5 && degrees < 157.5) direction = UP_RIGHT; - else if ((degrees >= 157.5 && degrees <= 180) || (degrees >= -180 && degrees < -157.5)) direction = RIGHT; - else if (degrees >= -157.5 && degrees < -112.5) direction = RIGHT_DOWN; - else if (degrees >= -112.5 && degrees < -67.5) direction = DOWN; - else if (degrees >= -67.5 && degrees < -22.5) direction = DOWN_LEFT; - else direction = CENTER; + direction = calculateDirection(Math.toDegrees(angle)); invalidate(); break; @@ -195,7 +192,7 @@ public class JoyStick extends View { if (!stayPut) { posX = centerX; posY = centerY; - direction = CENTER; + direction = DIRECTION_CENTER; angle = 0; power = 0; invalidate(); @@ -209,6 +206,18 @@ public class JoyStick extends View { return true; } + private static int calculateDirection(double degrees) { + if ((degrees >= 0 && degrees < 22.5) || (degrees < 0 && degrees > -22.5)) return DIRECTION_LEFT; + else if (degrees >= 22.5 && degrees < 67.5) return DIRECTION_LEFT_UP; + else if (degrees >= 67.5 && degrees < 112.5) return DIRECTION_UP; + else if (degrees >= 112.5 && degrees < 157.5) return DIRECTION_UP_RIGHT; + else if ((degrees >= 157.5 && degrees <= 180) || (degrees >= -180 && degrees < -157.5)) return DIRECTION_RIGHT; + else if (degrees >= -157.5 && degrees < -112.5) return DIRECTION_RIGHT_DOWN; + else if (degrees >= -112.5 && degrees < -67.5) return DIRECTION_DOWN; + else if (degrees >= -67.5 && degrees < -22.5) return DIRECTION_DOWN_LEFT; + else return DIRECTION_CENTER; + } + public void setListener(JoyStickListener listener) { this.listener = listener; } @@ -229,6 +238,10 @@ public class JoyStick extends View { return direction; } + public int getType() { + return type; + } + //Customization ---------------------------------------------------------------- public void setPadColor(int padColor) { @@ -266,4 +279,8 @@ public class JoyStick extends View { public void setButtonDrawable(Bitmap bitmap) { this.buttonBitmap = bitmap; } + + public void setType(int type) { + this.type = type; + } } \ No newline at end of file