From 0735c9c76b1343941f9443d02d30372bf9852ae0 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Sun, 14 Feb 2016 18:11:28 +0800 Subject: [PATCH 1/3] add base shape of rect, and value return [ [-1,1], [-1, 1] ] --- .idea/misc.xml | 5 +- .idea/uiDesigner.xml | 124 ++++++++++++++++++ Bugstick.iml | 1 - bugstick/bugstick.iml | 11 +- bugstick/build.gradle | 4 +- .../java/com/jmedeisis/bugstick/Joystick.java | 78 +++++++++++ .../jmedeisis/bugstick/JoystickListener.java | 7 + bugstick/src/main/res/values/attrs.xml | 4 + sample/build.gradle | 4 +- sample/sample.iml | 15 +-- .../com/example/bugstick/MainActivity.java | 3 +- sample/src/main/res/drawable/bg_base_rect.xml | 9 ++ sample/src/main/res/layout/activity_rect.xml | 48 +++++++ sample/src/main/res/values/dimens.xml | 3 + 14 files changed, 293 insertions(+), 23 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 sample/src/main/res/drawable/bg_base_rect.xml create mode 100644 sample/src/main/res/layout/activity_rect.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 2890ad8..7e43b83 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,9 @@ + + + - + diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Bugstick.iml b/Bugstick.iml index 3c11a96..e048caf 100644 --- a/Bugstick.iml +++ b/Bugstick.iml @@ -4,7 +4,6 @@ diff --git a/bugstick/bugstick.iml b/bugstick/bugstick.iml index 75aea07..4511464 100644 --- a/bugstick/bugstick.iml +++ b/bugstick/bugstick.iml @@ -13,11 +13,8 @@ \ No newline at end of file diff --git a/bugstick/build.gradle b/bugstick/build.gradle index dfd721b..cdfd974 100644 --- a/bugstick/build.gradle +++ b/bugstick/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'bintray-release' android { compileSdkVersion 23 - buildToolsVersion "23.0.1" + buildToolsVersion "23.0.2" defaultConfig { minSdkVersion 15 @@ -21,7 +21,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:support-annotations:23.0.1' + compile 'com.android.support:appcompat-v7:23.1.1' } buildscript { diff --git a/bugstick/src/main/java/com/jmedeisis/bugstick/Joystick.java b/bugstick/src/main/java/com/jmedeisis/bugstick/Joystick.java index 6da9b36..39b4041 100644 --- a/bugstick/src/main/java/com/jmedeisis/bugstick/Joystick.java +++ b/bugstick/src/main/java/com/jmedeisis/bugstick/Joystick.java @@ -52,7 +52,13 @@ public enum MotionConstraint { VERTICAL } + public enum BaseShape { + OVAL, + RECT + } + private MotionConstraint motionConstraint = MotionConstraint.NONE; + private BaseShape baseShape = BaseShape.OVAL; private JoystickListener listener; @@ -92,6 +98,10 @@ private void init(Context context, AttributeSet attrs) { } motionConstraint = MotionConstraint.values()[a.getInt(R.styleable.Joystick_motion_constraint, motionConstraint.ordinal())]; + + baseShape = BaseShape.values()[a.getInt(R.styleable.Joystick_base_shape, + baseShape.ordinal())]; + a.recycle(); } } @@ -377,7 +387,75 @@ private void onDragStop() { /** * Where most of the magic happens. What, basic trigonometry isn't magic?! */ +// private void onDrag(float dx, float dy) { +// float x = downX + dx - centerX; +// float y = downY + dy - centerY; +// +// switch (motionConstraint) { +// case HORIZONTAL: +// y = 0; +// break; +// case VERTICAL: +// x = 0; +// break; +// } +// +// float offset = (float) Math.sqrt(x * x + y * y); +// if (x * x + y * y > radius * radius) { +// x = radius * x / offset; +// y = radius * y / offset; +// offset = radius; +// } +// +// final double radians = Math.atan2(-y, x); +// final float degrees = (float) (180 * radians / Math.PI); +// +// if (null != listener) listener.onDrag(degrees, 0 == radius ? 0 : offset / radius); +// +// draggedChild.setTranslationX(x); +// draggedChild.setTranslationY(y); +// } + private void onDrag(float dx, float dy) { + if (baseShape == BaseShape.RECT) { + onDragRect(dx, dy); + } else { + onDragOval(dx, dy); + } + } + + private void onDragRect(float dx, float dy) { + float x = downX + dx - centerX; + float y = downY + dy - centerY; + + switch (motionConstraint) { + case HORIZONTAL: + y = 0; + break; + case VERTICAL: + x = 0; + break; + } + + if( x > radius ) x = radius; + if( x < -radius ) x = -radius; + + if( y > radius ) y = radius; + if( y < -radius ) y = -radius; + + float xOffset = x / radius; + float yOffset = y / radius; + + if (null != listener) listener.onDrag(xOffset, -yOffset); + + draggedChild.setTranslationX(x); + draggedChild.setTranslationY(y); + } + + /** + * Where most of the magic happens. What, basic trigonometry isn't magic?! + */ + private void onDragOval(float dx, float dy) { float x = downX + dx - centerX; float y = downY + dy - centerY; diff --git a/bugstick/src/main/java/com/jmedeisis/bugstick/JoystickListener.java b/bugstick/src/main/java/com/jmedeisis/bugstick/JoystickListener.java index 2801da1..5e7e8d9 100644 --- a/bugstick/src/main/java/com/jmedeisis/bugstick/JoystickListener.java +++ b/bugstick/src/main/java/com/jmedeisis/bugstick/JoystickListener.java @@ -7,8 +7,15 @@ public interface JoystickListener { void onDown(); /** + * If baseShape = oval + * * @param degrees -180 -> 180. * @param offset normalized, 0 -> 1. + * + * If baseShape = rect + * + * @param degrees = xOffset, -1 -> 1. + * @param offset = yOffset, -1 -> 1. */ void onDrag(float degrees, float offset); diff --git a/bugstick/src/main/res/values/attrs.xml b/bugstick/src/main/res/values/attrs.xml index 7fbc077..e7abe14 100644 --- a/bugstick/src/main/res/values/attrs.xml +++ b/bugstick/src/main/res/values/attrs.xml @@ -10,6 +10,10 @@ + + + + \ No newline at end of file diff --git a/sample/build.gradle b/sample/build.gradle index c10c152..00f3b34 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 23 - buildToolsVersion "23.0.1" + buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.bugstick" @@ -21,6 +21,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.0.1' + compile 'com.android.support:appcompat-v7:23.1.1' compile project(':bugstick') } diff --git a/sample/sample.iml b/sample/sample.iml index 80f01e8..7789133 100644 --- a/sample/sample.iml +++ b/sample/sample.iml @@ -13,11 +13,8 @@