├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── sample
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable
│ │ │ ├── bg_base.xml
│ │ │ ├── bg_stick_pressed.xml
│ │ │ ├── bg_stick_unpressed.xml
│ │ │ └── bg_stick.xml
│ │ ├── values
│ │ │ ├── styles.xml
│ │ │ ├── strings.xml
│ │ │ └── dimens.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── bugstick
│ │ ├── MainActivity.java
│ │ └── BugView.java
├── proguard-rules.pro
├── build.gradle
└── sample.iml
├── bugstick
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ ├── strings.xml
│ │ │ └── attrs.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── jmedeisis
│ │ └── bugstick
│ │ ├── JoystickListener.java
│ │ └── Joystick.java
├── proguard-rules.pro
├── build.gradle
└── bugstick.iml
├── settings.gradle
├── graphics
└── sample_animated.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── Bugstick.iml
├── LICENSE.txt
├── gradlew.bat
├── README.md
└── gradlew
/.idea/.name:
--------------------------------------------------------------------------------
1 | Bugstick
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/bugstick/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':bugstick'
2 |
--------------------------------------------------------------------------------
/graphics/sample_animated.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/graphics/sample_animated.gif
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/bugstick/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Bugstick
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | /captures
8 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/justasm/Bugstick/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/bg_base.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/bg_stick_pressed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/bg_stick_unpressed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/bugstick/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Bugstick
3 |
4 | Angle: %1$.2f
5 | %1$.2f :Offset
6 | Angle: none
7 | none :Offset
8 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 | 16dp
7 | 144dp
8 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/bg_stick.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/bugstick/src/main/java/com/jmedeisis/bugstick/JoystickListener.java:
--------------------------------------------------------------------------------
1 | package com.jmedeisis.bugstick;
2 |
3 | /**
4 | * Interface definition for joystick callbacks from user touch interactions.
5 | */
6 | public interface JoystickListener {
7 | void onDown();
8 |
9 | /**
10 | * @param degrees -180 -> 180.
11 | * @param offset normalized, 0 -> 1.
12 | */
13 | void onDrag(float degrees, float offset);
14 |
15 | void onUp();
16 | }
17 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/bugstick/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/bugstick/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.1"
6 |
7 | defaultConfig {
8 | applicationId "com.example.bugstick"
9 | minSdkVersion 16
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:23.0.1'
25 | compile project(':bugstick')
26 | }
27 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/Bugstick.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Justas Medeisis
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/bugstick/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'bintray-release'
3 |
4 | android {
5 | compileSdkVersion 23
6 | buildToolsVersion "23.0.1"
7 |
8 | defaultConfig {
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 2
12 | versionName "0.2.2"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:support-annotations:23.0.1'
25 | }
26 |
27 | buildscript {
28 | repositories {
29 | jcenter()
30 | }
31 | dependencies {
32 | classpath 'com.novoda:bintray-release:0.3.4'
33 | }
34 | }
35 |
36 | // see https://github.com/novoda/bintray-release/wiki/Configuration-of-the-publish-closure
37 | publish {
38 | userOrg = 'justasm'
39 | groupId = 'com.jmedeisis'
40 | artifactId = 'bugstick'
41 | version = "0.2.2"
42 | licences = ['MIT']
43 | desc = "Joystick widget for Android."
44 | website = 'https://github.com/justasm/Bugstick'
45 | }
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
18 |
19 |
23 |
24 |
25 |
26 |
31 |
32 |
38 |
39 |
46 |
47 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/example/bugstick/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.bugstick;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.TextView;
6 |
7 | import com.jmedeisis.bugstick.Joystick;
8 | import com.jmedeisis.bugstick.JoystickListener;
9 |
10 | public class MainActivity extends AppCompatActivity {
11 |
12 | private static final float MAX_BUG_SPEED_DP_PER_S = 300f;
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 |
19 | final TextView angleView = (TextView) findViewById(R.id.tv_angle);
20 | final TextView offsetView = (TextView) findViewById(R.id.tv_offset);
21 |
22 | final BugView bugView = (BugView) findViewById(R.id.bugview);
23 |
24 | final String angleNoneString = getString(R.string.angle_value_none);
25 | final String angleValueString = getString(R.string.angle_value);
26 | final String offsetNoneString = getString(R.string.offset_value_none);
27 | final String offsetValueString = getString(R.string.offset_value);
28 |
29 | Joystick joystick = (Joystick) findViewById(R.id.joystick);
30 | joystick.setJoystickListener(new JoystickListener() {
31 | @Override
32 | public void onDown() {
33 | }
34 |
35 | @Override
36 | public void onDrag(float degrees, float offset) {
37 | angleView.setText(String.format(angleValueString, degrees));
38 | offsetView.setText(String.format(offsetValueString, offset));
39 |
40 | bugView.setVelocity(
41 | (float) Math.cos(degrees * Math.PI / 180f) * offset * MAX_BUG_SPEED_DP_PER_S,
42 | -(float) Math.sin(degrees * Math.PI / 180f) * offset * MAX_BUG_SPEED_DP_PER_S);
43 | }
44 |
45 | @Override
46 | public void onUp() {
47 | angleView.setText(angleNoneString);
48 | offsetView.setText(offsetNoneString);
49 |
50 | bugView.setVelocity(0, 0);
51 | }
52 | });
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Bugstick
2 | ========
3 | Flexible joystick widget for Android.
4 |
5 | 
6 |
7 | Why Bugstick?
8 | -------------
9 | [Other][zerokol-joystickview] [joystick][anarchy-joystickview] [widgets][sphero-joystickview]
10 | are a hassle to include in modern Gradle-based Android projects, support only limited visual
11 | customization, and suffer from overly prescriptive output.
12 |
13 | So how does Bugstick solve these issues?
14 |
15 | - *Painless Dependency* - try it out quickly with a simple Gradle dependency.
16 |
17 | - *Configurable Look* - the joystick base and stick are a completely decoupled, standard
18 | ViewGroup - View pair. Use an `ImageView`, `Button`, `LinearLayout`, or any other View as
19 | the stick, and use standard `Drawable`s to theme Bugstick so it looks at home in your app.
20 |
21 | - *Unopinionated Output* - the widget reports proportional offset of the stick from its center as
22 | well as the current angle via a standard listener interface. Choose to interpret these raw outputs
23 | in the way that suits your use case.
24 |
25 | Usage
26 | -----
27 | Add it to your project using Gradle:
28 |
29 | ```groovy
30 | compile 'com.jmedeisis:bugstick:0.2.2'
31 | ```
32 |
33 | Example XML layout file:
34 |
35 | ```xml
36 |
41 |
42 |
43 |
47 |
48 |
49 | ```
50 |
51 | Note that the `Joystick` ViewGroup supports only one direct child, but that child can be another
52 | ViewGroup such as a `FrameLayout` with multiple children.
53 |
54 | After inflating the layout, you will typically listen for joystick events using a
55 | `JoystickListener`:
56 |
57 | ```java
58 | Joystick joystick = (Joystick) findViewById(R.id.joystick);
59 | joystick.setJoystickListener(new JoystickListener() {
60 | @Override
61 | public void onDown() {
62 | // ..
63 | }
64 |
65 | @Override
66 | public void onDrag(float degrees, float offset) {
67 | // ..
68 | }
69 |
70 | @Override
71 | public void onUp() {
72 | // ..
73 | }
74 | });
75 | ```
76 |
77 | Please refer to the included [sample project](sample/) for a thorough example.
78 |
79 | Configuration
80 | -------------
81 | You can configure the following attributes for the `Joystick` class:
82 |
83 | - `start_on_first_touch` - If true (default), the stick activates immediately on the initial touch.
84 | If false, the user must begin to drag their finger across the joystick for the stick to activate.
85 |
86 | - `force_square` - If true (default), the joystick always measures itself to force a square layout.
87 |
88 | - `radius` - If specified, this is the maximum physical offset from the center that the stick is
89 | allowed to move. If not specified (default), the radius is determined based on the dimensions of
90 | the base and the stick.
91 |
92 | - `motion_constraint` - One of `None` (default), `Horizontal`, or `Vertical`. Specifies whether the
93 | stick motion should be constrained to a particular direction. If `None`, the stick is allowed to
94 | move freely around the center of the base.
95 |
96 | Example configuration:
97 |
98 | ```xml
99 |
108 |
109 |
110 |
111 |
112 | ```
113 |
114 | Development
115 | -----------
116 | Pull requests are welcome and encouraged for bugfixes and features such as:
117 |
118 | - accessibility support
119 | - more powerful motion constraints, e.g. constrain to arbitrary path
120 |
121 | License
122 | -------
123 | Bugstick is licensed under the terms of the [MIT License](LICENSE.txt).
124 |
125 | [zerokol-joystickview]: https://github.com/zerokol/JoystickView
126 | [anarchy-joystickview]: https://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView
127 | [sphero-joystickview]: https://github.com/orbotix/Sphero-Android-SDK/tree/master/samples/UISample#joystick-view
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/example/bugstick/BugView.java:
--------------------------------------------------------------------------------
1 | package com.example.bugstick;
2 |
3 | import android.animation.TimeAnimator;
4 | import android.annotation.SuppressLint;
5 | import android.annotation.TargetApi;
6 | import android.content.Context;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.DashPathEffect;
10 | import android.graphics.Paint;
11 | import android.graphics.Path;
12 | import android.graphics.PathEffect;
13 | import android.graphics.PathMeasure;
14 | import android.graphics.PointF;
15 | import android.os.Build;
16 | import android.util.AttributeSet;
17 | import android.view.View;
18 |
19 | /**
20 | * A simple toy view for demo purposes.
21 | * Displays a circle and a trail that can be controlled via {@link #setVelocity(float, float)}.
22 | */
23 | public class BugView extends View implements TimeAnimator.TimeListener {
24 |
25 | private static final float BUG_RADIUS_DP = 4f;
26 | private static final float BUG_TRAIL_DP = 200f;
27 |
28 | private Paint paint;
29 | private TimeAnimator animator;
30 |
31 | private float density;
32 | private int width, height;
33 | private PointF position;
34 | private PointF velocity;
35 | private Path path;
36 | private PathMeasure pathMeasure;
37 |
38 | public BugView(Context context) {
39 | super(context);
40 | init(context);
41 | }
42 |
43 | public BugView(Context context, AttributeSet attrs) {
44 | super(context, attrs);
45 | init(context);
46 | }
47 |
48 | public BugView(Context context, AttributeSet attrs, int defStyleAttr) {
49 | super(context, attrs, defStyleAttr);
50 | init(context);
51 | }
52 |
53 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
54 | public BugView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
55 | super(context, attrs, defStyleAttr, defStyleRes);
56 | init(context);
57 | }
58 |
59 | private void init(Context context) {
60 | animator = new TimeAnimator();
61 | animator.setTimeListener(this);
62 |
63 | paint = new Paint();
64 | paint.setColor(Color.WHITE);
65 |
66 | density = getResources().getDisplayMetrics().density;
67 |
68 | path = new Path();
69 | pathMeasure = new PathMeasure();
70 | position = new PointF();
71 | velocity = new PointF();
72 | }
73 |
74 | /**
75 | * Start applying velocity as soon as view is on-screen.
76 | */
77 | @Override
78 | public void onAttachedToWindow() {
79 | super.onAttachedToWindow();
80 | animator.start();
81 | }
82 |
83 | /**
84 | * Stop animations when the view hierarchy is torn down.
85 | */
86 | @Override
87 | public void onDetachedFromWindow() {
88 | animator.cancel();
89 | super.onDetachedFromWindow();
90 | }
91 |
92 | @Override
93 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
94 | super.onSizeChanged(w, h, oldw, oldh);
95 | width = w;
96 | height = h;
97 |
98 | position.set(width / 2, height / 2);
99 | path.rewind();
100 | path.moveTo(position.x, position.y);
101 | }
102 |
103 | /**
104 | * Set bug velocity in dips.
105 | */
106 | public void setVelocity(float vxDps, float vyDps) {
107 | velocity.set(vxDps * density, vyDps * density);
108 | }
109 |
110 | @Override
111 | public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
112 | final float dt = deltaTime / 1000f; // seconds
113 |
114 | position.x += velocity.x * dt;
115 | position.y += velocity.y * dt;
116 |
117 | bound();
118 |
119 | path.lineTo(position.x, position.y);
120 |
121 | invalidate();
122 | }
123 |
124 | /**
125 | * Bound position and reflect velocity.
126 | */
127 | private void reflect() {
128 | boolean flipX = false, flipY = false;
129 | if (position.x > width) {
130 | position.x = position.x - 2 * (position.x - width);
131 | flipX = true;
132 | } else if (position.x < 0) {
133 | position.x = -position.x;
134 | flipX = true;
135 | }
136 | if (position.y > height) {
137 | position.y = position.y - 2 * (position.y - height);
138 | flipY = true;
139 | } else if (position.y < 0) {
140 | position.y = -position.y;
141 | flipY = true;
142 | }
143 | if (flipX) velocity.x *= -1;
144 | if (flipY) velocity.y *= -1;
145 | }
146 |
147 | /**
148 | * Bound position.
149 | */
150 | private void bound() {
151 | if (position.x > width) {
152 | position.x = width;
153 | } else if (position.x < 0) {
154 | position.x = 0;
155 | }
156 | if (position.y > height) {
157 | position.y = height;
158 | } else if (position.y < 0) {
159 | position.y = 0;
160 | }
161 | }
162 |
163 | @Override
164 | public void onDraw(Canvas canvas) {
165 | super.onDraw(canvas);
166 |
167 | canvas.drawColor(Color.BLACK);
168 |
169 | pathMeasure.setPath(path, false);
170 | float length = pathMeasure.getLength();
171 |
172 | if (length > BUG_TRAIL_DP * density) {
173 | // Note - this is likely a poor way to accomplish the result. Just for demo purposes.
174 | @SuppressLint("DrawAllocation")
175 | PathEffect effect = new DashPathEffect(new float[]{length, length}, -length + BUG_TRAIL_DP * density);
176 | paint.setPathEffect(effect);
177 | }
178 |
179 | paint.setStyle(Paint.Style.STROKE);
180 | canvas.drawPath(path, paint);
181 |
182 | paint.setStyle(Paint.Style.FILL);
183 | canvas.drawCircle(position.x, position.y, BUG_RADIUS_DP * density, paint);
184 | }
185 | }
186 |
--------------------------------------------------------------------------------
/bugstick/bugstick.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/sample/sample.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/bugstick/src/main/java/com/jmedeisis/bugstick/Joystick.java:
--------------------------------------------------------------------------------
1 | package com.jmedeisis.bugstick;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.content.res.TypedArray;
6 | import android.os.Build;
7 | import android.support.annotation.NonNull;
8 | import android.util.AttributeSet;
9 | import android.util.Log;
10 | import android.view.Gravity;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewConfiguration;
14 | import android.view.ViewGroup;
15 | import android.view.animation.DecelerateInterpolator;
16 | import android.view.animation.Interpolator;
17 | import android.widget.FrameLayout;
18 |
19 | /**
20 | * A simple and flexible joystick.
21 | * Extends FrameLayout and should host one direct child to act as the draggable stick.
22 | * Use {@link #setJoystickListener(JoystickListener)} to observe user inputs.
23 | */
24 | public class Joystick extends FrameLayout {
25 | private static final String LOG_TAG = Joystick.class.getSimpleName();
26 |
27 | private static final int STICK_SETTLE_DURATION_MS = 100;
28 | private static final Interpolator STICK_SETTLE_INTERPOLATOR = new DecelerateInterpolator();
29 |
30 | private int touchSlop;
31 |
32 | private float centerX, centerY;
33 | private float radius;
34 |
35 | private View draggedChild;
36 | private boolean detectingDrag;
37 | private boolean dragInProgress;
38 |
39 | private float downX, downY;
40 | private static final int INVALID_POINTER_ID = -1;
41 | private int activePointerId = INVALID_POINTER_ID;
42 |
43 | private boolean locked;
44 |
45 | private boolean startOnFirstTouch = true;
46 | private boolean forceSquare = true;
47 | private boolean hasFixedRadius = false;
48 |
49 | public enum MotionConstraint {
50 | NONE,
51 | HORIZONTAL,
52 | VERTICAL
53 | }
54 |
55 | private MotionConstraint motionConstraint = MotionConstraint.NONE;
56 |
57 | private JoystickListener listener;
58 |
59 | public Joystick(Context context) {
60 | super(context);
61 | init(context, null);
62 | }
63 |
64 | public Joystick(Context context, AttributeSet attrs) {
65 | super(context, attrs);
66 | init(context, attrs);
67 | }
68 |
69 | public Joystick(Context context, AttributeSet attrs, int defStyleAttr) {
70 | super(context, attrs, defStyleAttr);
71 | init(context, attrs);
72 | }
73 |
74 | @SuppressWarnings("unused")
75 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
76 | public Joystick(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
77 | super(context, attrs, defStyleAttr, defStyleRes);
78 | init(context, attrs);
79 | }
80 |
81 | private void init(Context context, AttributeSet attrs) {
82 | final ViewConfiguration configuration = ViewConfiguration.get(context);
83 | touchSlop = configuration.getScaledTouchSlop();
84 |
85 | if (null != attrs) {
86 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Joystick);
87 | startOnFirstTouch = a.getBoolean(R.styleable.Joystick_start_on_first_touch, startOnFirstTouch);
88 | forceSquare = a.getBoolean(R.styleable.Joystick_force_square, forceSquare);
89 | hasFixedRadius = a.hasValue(R.styleable.Joystick_radius);
90 | if (hasFixedRadius) {
91 | radius = a.getDimensionPixelOffset(R.styleable.Joystick_radius, (int) radius);
92 | }
93 | motionConstraint = MotionConstraint.values()[a.getInt(R.styleable.Joystick_motion_constraint,
94 | motionConstraint.ordinal())];
95 | a.recycle();
96 | }
97 | }
98 |
99 | @Override
100 | public boolean shouldDelayChildPressedState() {
101 | return true;
102 | }
103 |
104 | @Override
105 | public void onSizeChanged(int w, int h, int oldw, int oldh) {
106 | super.onSizeChanged(w, h, oldw, oldh);
107 |
108 | centerX = (float) w / 2;
109 | centerY = (float) h / 2;
110 | }
111 |
112 | @Override
113 | public void onLayout(boolean changed, int left, int top, int right, int bottom) {
114 | super.onLayout(changed, left, top, right, bottom);
115 |
116 | if (changed && !hasFixedRadius) {
117 | recalculateRadius(right - left, bottom - top);
118 | }
119 | }
120 |
121 | private void recalculateRadius(int width, int height) {
122 | float stickHalfWidth = 0;
123 | float stickHalfHeight = 0;
124 | if (hasStick()) {
125 | final View stick = getChildAt(0);
126 | stickHalfWidth = (float) stick.getWidth() / 2;
127 | stickHalfHeight = (float) stick.getHeight() / 2;
128 | }
129 |
130 | switch (motionConstraint) {
131 | case NONE:
132 | radius = (float) Math.min(width, height) / 2 - Math.max(stickHalfWidth, stickHalfHeight);
133 | break;
134 | case HORIZONTAL:
135 | radius = (float) width / 2 - stickHalfWidth;
136 | break;
137 | case VERTICAL:
138 | radius = (float) height / 2 - stickHalfHeight;
139 | break;
140 | }
141 | }
142 |
143 | public void setJoystickListener(JoystickListener listener) {
144 | this.listener = listener;
145 |
146 | if (!hasStick()) {
147 | Log.w(LOG_TAG, LOG_TAG + " has no draggable stick, and is therefore not functional. " +
148 | "Consider adding a child view to act as the stick.");
149 | }
150 | }
151 |
152 | /**
153 | * Locks the stick position when next the user releases it.
154 | * Note that {@link JoystickListener#onUp()} will not be called upon release.
155 | * Resets to unlocked state after subsequent touch.
156 | */
157 | @SuppressWarnings("unused")
158 | public void lock() {
159 | locked = true;
160 | }
161 |
162 | /**
163 | * @return Distance in pixels a touch can wander before the joystick thinks the user is
164 | * manipulating the stick.
165 | */
166 | @SuppressWarnings("unused")
167 | public int getTouchSlop() {
168 | return touchSlop;
169 | }
170 |
171 | /**
172 | * @param touchSlop Distance in pixels a touch can wander before the joystick thinks the user is
173 | * manipulating the stick.
174 | */
175 | @SuppressWarnings("unused")
176 | public void setTouchSlop(int touchSlop) {
177 | this.touchSlop = touchSlop;
178 | }
179 |
180 | @SuppressWarnings("unused")
181 | public MotionConstraint getMotionConstraint() {
182 | return motionConstraint;
183 | }
184 |
185 | @SuppressWarnings("unused")
186 | public void setMotionConstraint(MotionConstraint motionConstraint) {
187 | this.motionConstraint = motionConstraint;
188 |
189 | if (!hasFixedRadius) recalculateRadius(getWidth(), getHeight());
190 | }
191 |
192 | @SuppressWarnings("unused")
193 | public float getRadius() {
194 | return radius;
195 | }
196 |
197 | /**
198 | * @param radius The maximum offset in pixels from the center that the stick is allowed to move.
199 | */
200 | @SuppressWarnings("unused")
201 | public void setRadius(float radius) {
202 | this.radius = radius;
203 | }
204 |
205 | @SuppressWarnings("unused")
206 | public boolean isStartOnFirstTouch() {
207 | return startOnFirstTouch;
208 | }
209 |
210 | /**
211 | * @param startOnFirstTouch If true, the stick activates immediately on the initial touch.
212 | * Else, the user must begin to drag their finger across the joystick
213 | * for the stick to activate.
214 | */
215 | @SuppressWarnings("unused")
216 | public void setStartOnFirstTouch(boolean startOnFirstTouch) {
217 | this.startOnFirstTouch = startOnFirstTouch;
218 | }
219 |
220 | /*
221 | TOUCH EVENT HANDLING
222 | */
223 | @Override
224 | public boolean onInterceptTouchEvent(MotionEvent event) {
225 | if (!isEnabled()) return false;
226 |
227 | switch (event.getActionMasked()) {
228 | case MotionEvent.ACTION_DOWN: {
229 | if (detectingDrag || !hasStick()) return false;
230 |
231 | downX = event.getX(0);
232 | downY = event.getY(0);
233 | activePointerId = event.getPointerId(0);
234 |
235 | onStartDetectingDrag();
236 | break;
237 | }
238 | case MotionEvent.ACTION_MOVE: {
239 | if (INVALID_POINTER_ID == activePointerId) break;
240 | if (detectingDrag && dragExceedsSlop(event)) {
241 | onDragStart();
242 | return true;
243 | }
244 | break;
245 | }
246 | case MotionEvent.ACTION_POINTER_UP: {
247 | final int pointerIndex = event.getActionIndex();
248 | final int pointerId = event.getPointerId(pointerIndex);
249 |
250 | if (pointerId != activePointerId)
251 | break; // if active pointer, fall through and cancel!
252 | }
253 | case MotionEvent.ACTION_CANCEL:
254 | case MotionEvent.ACTION_UP: {
255 | onTouchEnded();
256 |
257 | onStopDetectingDrag();
258 | break;
259 | }
260 | }
261 |
262 | return false;
263 | }
264 |
265 | @Override
266 | public boolean onTouchEvent(@NonNull MotionEvent event) {
267 | if (!isEnabled()) return false;
268 |
269 | switch (event.getActionMasked()) {
270 | case MotionEvent.ACTION_DOWN: {
271 | if (!detectingDrag) return false;
272 | if (startOnFirstTouch) onDragStart();
273 | return true;
274 | }
275 | case MotionEvent.ACTION_MOVE: {
276 | if (INVALID_POINTER_ID == activePointerId) break;
277 |
278 | if (dragInProgress) {
279 | int pointerIndex = event.findPointerIndex(activePointerId);
280 | float latestX = event.getX(pointerIndex);
281 | float latestY = event.getY(pointerIndex);
282 |
283 | float deltaX = latestX - downX;
284 | float deltaY = latestY - downY;
285 |
286 | onDrag(deltaX, deltaY);
287 | return true;
288 | } else if (detectingDrag && dragExceedsSlop(event)) {
289 | onDragStart();
290 | return true;
291 | }
292 | break;
293 | }
294 | case MotionEvent.ACTION_POINTER_UP: {
295 | final int pointerIndex = event.getActionIndex();
296 | final int pointerId = event.getPointerId(pointerIndex);
297 |
298 | if (pointerId != activePointerId)
299 | break; // if active pointer, fall through and cancel!
300 | }
301 | case MotionEvent.ACTION_CANCEL:
302 | case MotionEvent.ACTION_UP: {
303 | onTouchEnded();
304 |
305 | if (dragInProgress) {
306 | onDragStop();
307 | } else {
308 | onStopDetectingDrag();
309 | }
310 | return true;
311 | }
312 | }
313 |
314 | return false;
315 | }
316 |
317 | private boolean dragExceedsSlop(MotionEvent event) {
318 | final int pointerIndex = event.findPointerIndex(activePointerId);
319 | final float x = event.getX(pointerIndex);
320 | final float y = event.getY(pointerIndex);
321 | final float dx = Math.abs(x - downX);
322 | final float dy = Math.abs(y - downY);
323 |
324 | switch (motionConstraint) {
325 | case NONE:
326 | return dx * dx + dy * dy > touchSlop * touchSlop;
327 | case HORIZONTAL:
328 | return dx > touchSlop;
329 | case VERTICAL:
330 | return dy > touchSlop;
331 | }
332 | return false;
333 | }
334 |
335 | private void onTouchEnded() {
336 | activePointerId = INVALID_POINTER_ID;
337 | }
338 |
339 | private boolean hasStick() {
340 | return getChildCount() > 0;
341 | }
342 |
343 | private void onStartDetectingDrag() {
344 | detectingDrag = true;
345 | if (null != listener) listener.onDown();
346 | }
347 |
348 | private void onStopDetectingDrag() {
349 | detectingDrag = false;
350 | if (!locked && null != listener) listener.onUp();
351 |
352 | locked = false;
353 | }
354 |
355 | private void onDragStart() {
356 | dragInProgress = true;
357 | draggedChild = getChildAt(0);
358 | draggedChild.animate().cancel();
359 | onDrag(0, 0);
360 | }
361 |
362 | private void onDragStop() {
363 | dragInProgress = false;
364 |
365 | if (!locked) {
366 | draggedChild.animate()
367 | .translationX(0).translationY(0)
368 | .setDuration(STICK_SETTLE_DURATION_MS)
369 | .setInterpolator(STICK_SETTLE_INTERPOLATOR)
370 | .start();
371 | }
372 |
373 | onStopDetectingDrag();
374 | draggedChild = null;
375 | }
376 |
377 | /**
378 | * Where most of the magic happens. What, basic trigonometry isn't magic?!
379 | */
380 | private void onDrag(float dx, float dy) {
381 | float x = downX + dx - centerX;
382 | float y = downY + dy - centerY;
383 |
384 | switch (motionConstraint) {
385 | case HORIZONTAL:
386 | y = 0;
387 | break;
388 | case VERTICAL:
389 | x = 0;
390 | break;
391 | }
392 |
393 | float offset = (float) Math.sqrt(x * x + y * y);
394 | if (x * x + y * y > radius * radius) {
395 | x = radius * x / offset;
396 | y = radius * y / offset;
397 | offset = radius;
398 | }
399 |
400 | final double radians = Math.atan2(-y, x);
401 | final float degrees = (float) (180 * radians / Math.PI);
402 |
403 | if (null != listener) listener.onDrag(degrees, 0 == radius ? 0 : offset / radius);
404 |
405 | draggedChild.setTranslationX(x);
406 | draggedChild.setTranslationY(y);
407 | }
408 |
409 | /*
410 | FORCE SQUARE
411 | */
412 | @Override
413 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
414 | if (!forceSquare) {
415 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
416 | return;
417 | }
418 |
419 | int widthMode = MeasureSpec.getMode(widthMeasureSpec);
420 | int widthSize = MeasureSpec.getSize(widthMeasureSpec);
421 | int heightMode = MeasureSpec.getMode(heightMeasureSpec);
422 | int heightSize = MeasureSpec.getSize(heightMeasureSpec);
423 |
424 | int size;
425 | if (widthMode == MeasureSpec.EXACTLY && widthSize > 0) {
426 | size = widthSize;
427 | } else if (heightMode == MeasureSpec.EXACTLY && heightSize > 0) {
428 | size = heightSize;
429 | } else {
430 | size = widthSize < heightSize ? widthSize : heightSize;
431 | }
432 |
433 | int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
434 | super.onMeasure(finalMeasureSpec, finalMeasureSpec);
435 | }
436 |
437 | /*
438 | CENTER CHILD BY DEFAULT
439 | */
440 | @Override
441 | public LayoutParams generateLayoutParams(AttributeSet attrs) {
442 | LayoutParams params = new LayoutParams(getContext(), attrs);
443 | params.gravity = Gravity.CENTER;
444 | return params;
445 | }
446 |
447 | @Override
448 | protected ViewGroup.LayoutParams generateLayoutParams(@NonNull ViewGroup.LayoutParams p) {
449 | LayoutParams params = new LayoutParams(p);
450 | params.gravity = Gravity.CENTER;
451 | return params;
452 | }
453 |
454 | @Override
455 | public LayoutParams generateDefaultLayoutParams() {
456 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
457 | }
458 |
459 | @Override
460 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
461 | return p instanceof LayoutParams;
462 | }
463 |
464 | /*
465 | ENSURE MAX ONE DIRECT CHILD
466 | */
467 | @Override
468 | public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
469 | if (getChildCount() > 0) {
470 | throw new IllegalStateException(LOG_TAG + " can host only one direct child");
471 | }
472 |
473 | super.addView(child, index, params);
474 | }
475 | }
476 |
--------------------------------------------------------------------------------