mirror of
https://github.com/etlegacy/JoyStick.git
synced 2024-11-24 21:11:15 +00:00
added direction, for d-pad
This commit is contained in:
parent
2ef51e7e4f
commit
7ef407ed7f
2 changed files with 54 additions and 23 deletions
|
@ -19,16 +19,10 @@ android {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
url "http://dl.bintray.com/erz05/maven"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
compile 'com.android.support:appcompat-v7:25.1.0'
|
compile 'com.android.support:appcompat-v7:25.1.0'
|
||||||
compile 'com.android.support:design:25.1.0'
|
compile 'com.android.support:design:25.1.0'
|
||||||
compile 'com.github.erz05:JoyStick:1.0.3'
|
compile project(':joysticklibrary')
|
||||||
}
|
}
|
|
@ -33,16 +33,27 @@ import android.view.View;
|
||||||
*/
|
*/
|
||||||
public class JoyStick extends 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;
|
||||||
|
|
||||||
private JoyStickListener listener;
|
private JoyStickListener listener;
|
||||||
private Paint paint;
|
private Paint paint;
|
||||||
|
private int direction = CENTER;
|
||||||
private float centerX;
|
private float centerX;
|
||||||
private float centerY;
|
private float centerY;
|
||||||
private float posX;
|
private float posX;
|
||||||
private float posY;
|
private float posY;
|
||||||
private float radius;
|
private float radius;
|
||||||
private float buttonRadius;
|
private float buttonRadius;
|
||||||
private double power = -1;
|
private double power = 0;
|
||||||
private double angle = -1;
|
private double angle = 0;
|
||||||
private RectF temp;
|
private RectF temp;
|
||||||
|
|
||||||
//Background Color
|
//Background Color
|
||||||
|
@ -64,7 +75,7 @@ public class JoyStick extends View {
|
||||||
private Bitmap buttonBitmap = null;
|
private Bitmap buttonBitmap = null;
|
||||||
|
|
||||||
public interface JoyStickListener {
|
public interface JoyStickListener {
|
||||||
void onMove(JoyStick joyStick, double angle, double power);
|
void onMove(JoyStick joyStick, double angle, double power, int direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoyStick(Context context) {
|
public JoyStick(Context context) {
|
||||||
|
@ -81,6 +92,7 @@ public class JoyStick extends View {
|
||||||
paint = new Paint();
|
paint = new Paint();
|
||||||
paint.setStyle(Paint.Style.FILL);
|
paint.setStyle(Paint.Style.FILL);
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
|
paint.setFilterBitmap(true);
|
||||||
|
|
||||||
temp = new RectF();
|
temp = new RectF();
|
||||||
|
|
||||||
|
@ -127,8 +139,7 @@ public class JoyStick extends View {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Canvas canvas) {
|
public void onDraw(Canvas canvas) {
|
||||||
super.draw(canvas);
|
|
||||||
if (canvas == null) return;
|
if (canvas == null) return;
|
||||||
if (padBGBitmap == null) {
|
if (padBGBitmap == null) {
|
||||||
paint.setColor(padColor);
|
paint.setColor(padColor);
|
||||||
|
@ -166,6 +177,17 @@ public class JoyStick extends View {
|
||||||
* (posX - centerX) + (posY - centerY)
|
* (posX - centerX) + (posY - centerY)
|
||||||
* (posY - centerY)) / radius);
|
* (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;
|
||||||
|
|
||||||
invalidate();
|
invalidate();
|
||||||
break;
|
break;
|
||||||
case MotionEvent.ACTION_UP:
|
case MotionEvent.ACTION_UP:
|
||||||
|
@ -173,6 +195,7 @@ public class JoyStick extends View {
|
||||||
if (!stayPut) {
|
if (!stayPut) {
|
||||||
posX = centerX;
|
posX = centerX;
|
||||||
posY = centerY;
|
posY = centerY;
|
||||||
|
direction = CENTER;
|
||||||
angle = 0;
|
angle = 0;
|
||||||
power = 0;
|
power = 0;
|
||||||
invalidate();
|
invalidate();
|
||||||
|
@ -181,19 +204,11 @@ public class JoyStick extends View {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.onMove(this, angle, power);
|
listener.onMove(this, angle, power, direction);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPadColor(int padColor) {
|
|
||||||
this.padColor = padColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setButtonColor(int buttonColor) {
|
|
||||||
this.buttonColor = buttonColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setListener(JoyStickListener listener) {
|
public void setListener(JoyStickListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
@ -210,8 +225,18 @@ public class JoyStick extends View {
|
||||||
return Math.toDegrees(angle);
|
return Math.toDegrees(angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enableStayPut(boolean enable) {
|
public int getDirection() {
|
||||||
this.stayPut = enable;
|
return direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Customization ----------------------------------------------------------------
|
||||||
|
|
||||||
|
public void setPadColor(int padColor) {
|
||||||
|
this.padColor = padColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setButtonColor(int buttonColor) {
|
||||||
|
this.buttonColor = buttonColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
//size of button is a percentage of the minimum(width, height)
|
//size of button is a percentage of the minimum(width, height)
|
||||||
|
@ -222,11 +247,23 @@ public class JoyStick extends View {
|
||||||
if (percentage < 25) percentage = 25;
|
if (percentage < 25) percentage = 25;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void enableStayPut(boolean enable) {
|
||||||
|
this.stayPut = enable;
|
||||||
|
}
|
||||||
|
|
||||||
public void setPadBackground(int resId) {
|
public void setPadBackground(int resId) {
|
||||||
this.padBGBitmap = BitmapFactory.decodeResource(getResources(), resId);
|
this.padBGBitmap = BitmapFactory.decodeResource(getResources(), resId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPadBackground(Bitmap bitmap) {
|
||||||
|
this.padBGBitmap = bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
public void setButtonDrawable(int resId) {
|
public void setButtonDrawable(int resId) {
|
||||||
this.buttonBitmap = BitmapFactory.decodeResource(getResources(), resId);
|
this.buttonBitmap = BitmapFactory.decodeResource(getResources(), resId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setButtonDrawable(Bitmap bitmap) {
|
||||||
|
this.buttonBitmap = bitmap;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue