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 @@
-
-
- generateDebugAndroidTestSources
- generateDebugSources
-
+
+
@@ -89,6 +86,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 @@
-
-
- generateDebugAndroidTestSources
- generateDebugSources
-
+
+
@@ -71,8 +68,6 @@
-
-
@@ -90,9 +85,9 @@
-
-
-
+
+
+
\ No newline at end of file
diff --git a/sample/src/main/java/com/example/bugstick/MainActivity.java b/sample/src/main/java/com/example/bugstick/MainActivity.java
index e50e854..aadb86b 100644
--- a/sample/src/main/java/com/example/bugstick/MainActivity.java
+++ b/sample/src/main/java/com/example/bugstick/MainActivity.java
@@ -14,7 +14,8 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
+ // setContentView(R.layout.activity_main);
+ setContentView(R.layout.activity_rect);
final TextView angleView = (TextView) findViewById(R.id.tv_angle);
final TextView offsetView = (TextView) findViewById(R.id.tv_offset);
diff --git a/sample/src/main/res/drawable/bg_base_rect.xml b/sample/src/main/res/drawable/bg_base_rect.xml
new file mode 100644
index 0000000..aa75ba9
--- /dev/null
+++ b/sample/src/main/res/drawable/bg_base_rect.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/sample/src/main/res/layout/activity_rect.xml b/sample/src/main/res/layout/activity_rect.xml
new file mode 100644
index 0000000..7080876
--- /dev/null
+++ b/sample/src/main/res/layout/activity_rect.xml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml
index 7690b07..09a0515 100644
--- a/sample/src/main/res/values/dimens.xml
+++ b/sample/src/main/res/values/dimens.xml
@@ -4,5 +4,8 @@
16dp
16dp
+ 32dp
144dp
+
+ 72dp