|
1 | | -[](https://github.com/JetBrains#jetbrains-on-github) |
| 1 | +# Compose Virtual Joystick Multiplatform |
2 | 2 |
|
3 | | -# Multiplatform library template |
| 3 | +[](https://central.sonatype.com/artifact/io.github.yoimerdr.compose/virtualjoystick) |
| 4 | +[](http://kotlinlang.org) |
| 5 | +[](https://github.com/JetBrains/compose-multiplatform) |
4 | 6 |
|
5 | | -## What is it? |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
6 | 11 |
|
7 | | -This repository contains a simple library project, intended to demonstrate a [Kotlin Multiplatform](https://kotlinlang.org/docs/multiplatform.html) library that is deployable to [Maven Central](https://central.sonatype.com/). |
| 12 | +<div style="text-align: center;"> |
| 13 | + <img src="./images/sample_android_app.mp4" height="500" alt="Sample Android Application"> |
| 14 | +</div> |
8 | 15 |
|
9 | | -The library has only one function: generate the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence) starting from platform-provided numbers. Also, it has a test for each platform just to be sure that tests run. |
| 16 | +A customizable virtual joystick library for **Compose Multiplatform** that provides touch-based directional controls with configurable dead zones, multiple direction types, and customizable visual styles. |
10 | 17 |
|
11 | | -Note that no other actions or tools usually required for the library development are set up, such as [tracking of backwards compatibility](https://kotlinlang.org/docs/jvm-api-guidelines-backward-compatibility.html#tools-designed-to-enforce-backward-compatibility), explicit API mode, licensing, contribution guideline, code of conduct and others. You can find a guide for best practices for designing Kotlin libraries [here](https://kotlinlang.org/docs/api-guidelines-introduction.html). |
| 18 | +## Features |
12 | 19 |
|
13 | | -## Guide |
| 20 | +- **Multiplatform Support**: Works on Android, iOS, Desktop (JVM), Web (JS), and WASM |
| 21 | +- **Fully Customizable**: Control appearance with custom drawing scopes |
| 22 | +- **Multiple Direction Types**: Support for 4-direction and 8-direction control schemes |
| 23 | +- **Dead Zone Configuration**: Adjustable invalid radius for precise control |
| 24 | +- **Event System**: Reactive event flow with move start, moving, held, and end states |
| 25 | +- **Built-in Backgrounds**: Multiple pre-built background styles including D-pad variants |
| 26 | +- **Compose-First**: Built with Jetpack Compose best practices |
14 | 27 |
|
15 | | -Please find the detailed guide [here](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-publish-libraries.html). |
| 28 | +## Installation |
16 | 29 |
|
17 | | -# Other resources |
18 | | -* [Publishing via the Central Portal](https://central.sonatype.org/publish-ea/publish-ea-guide/) |
19 | | -* [Gradle Maven Publish Plugin \- Publishing to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/) |
| 30 | +Add the dependency to your `build.gradle.kts`: |
| 31 | + |
| 32 | +```kotlin |
| 33 | +commonMain.dependencies { |
| 34 | + implementation("io.github.yoimerdr.compose:virtualjoystick:x.x.x") |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +### Simple Usage |
| 41 | + |
| 42 | +```kotlin |
| 43 | +@Composable |
| 44 | +fun SampleJoystick() { |
| 45 | + val joystickState = rememberJoystickState() |
| 46 | + |
| 47 | + VirtualJoystick( |
| 48 | + state = joystickState, |
| 49 | + modifier = Modifier.size(200.dp), |
| 50 | + properties = CircleDrawDefaults.properties() |
| 51 | + ) { snapshot -> |
| 52 | + // Handle joystick movement |
| 53 | + println("Direction: ${snapshot.direction}, Strength: ${snapshot.strength}") |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Usage with Events |
| 59 | + |
| 60 | +```kotlin |
| 61 | +@Composable |
| 62 | +fun SampleJoystick() { |
| 63 | + val joystickState = rememberJoystickState( |
| 64 | + invalidRadius = Radius.Ratio(0.15f), |
| 65 | + directionType = DirectionType.Complete |
| 66 | + ) |
| 67 | + val eventHolder = rememberJoystickEventHolder(joystickState) |
| 68 | + |
| 69 | + LaunchedEffect(eventHolder) { |
| 70 | + eventHolder.events.collect { event -> |
| 71 | + when (event) { |
| 72 | + is JoystickMoveStart -> println("Started: ${event.snapshot}") |
| 73 | + is JoystickMoving -> println("Moving: ${event.snapshot}") |
| 74 | + is JoystickMoveHeld -> println("Held: ${event.snapshot}") |
| 75 | + is JoystickMoveEnd -> println("Ended: ${event.snapshot}") |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + VirtualJoystick( |
| 81 | + state = joystickState, |
| 82 | + modifier = Modifier.size(200.dp), |
| 83 | + properties = CircleDrawDefaults.properties(), |
| 84 | + holder = eventHolder |
| 85 | + ) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Custom Appearance |
| 90 | + |
| 91 | +```kotlin |
| 92 | +@Composable |
| 93 | +fun CustomStyledJoystick() { |
| 94 | + val state = rememberJoystickState() |
| 95 | + |
| 96 | + BaseVirtualJoystick( |
| 97 | + state = state, |
| 98 | + modifier = Modifier.size(200.dp), |
| 99 | + onMove = { |
| 100 | + |
| 101 | + } |
| 102 | + ) { |
| 103 | + // Custom background |
| 104 | + Canvas( |
| 105 | + modifier = Modifier.fillMaxSize() |
| 106 | + ) { |
| 107 | + drawCircle( |
| 108 | + brush = SolidColor(Color.Gray.copy(alpha = 0.3f)), |
| 109 | + radius = state.radius |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + // Custom indicator |
| 114 | + JoystickCanvas { |
| 115 | + drawCircle( |
| 116 | + properties = CircleDrawDefaults.properties( |
| 117 | + brush = SolidColor(Color.Yellow), |
| 118 | + radius = Radius.Fixed(state.radius * 0.3f), |
| 119 | + ) |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Documentation |
| 127 | + |
| 128 | +### Direction Types |
| 129 | + |
| 130 | +- **`DirectionType.Complete`**: Returns all 8 directions (Right, DownRight, Down, DownLeft, Left, UpLeft, Up, UpRight, None) |
| 131 | +- **`DirectionType.Simple`**: Returns only 4 cardinal directions (Right, Down, Left, Up, None) |
| 132 | + |
| 133 | +### Background Types |
| 134 | + |
| 135 | +The library includes several pre-built background styles: |
| 136 | + |
| 137 | +- `BackgroundType.Default`: The default background style |
| 138 | +- `BackgroundType.Modern`: Modern styled background |
| 139 | +- `BackgroundType.Classic`: Standard circular background |
| 140 | +- `BackgroundType.DpadModern`: Modern D-pad style |
| 141 | +- `BackgroundType.DpadClassic`: Classic D-pad style |
| 142 | +- `BackgroundType.DpadStandard`: Standard D-pad style |
| 143 | + |
| 144 | +### Joystick State |
| 145 | + |
| 146 | +The `JoystickState` provides: |
| 147 | +- `offset`: Current joystick offset position |
| 148 | +- `direction`: Current direction enum value |
| 149 | +- `strength`: Movement strength (0.0 to 1.0) |
| 150 | +- `angle`: Current angle in degrees |
| 151 | +- `isInvalid`: Whether the joystick is in the dead zone |
| 152 | +- `goto`: Method for move the joystick to the specified position (or direction when use extension) |
| 153 | + |
| 154 | +### Event Types |
| 155 | + |
| 156 | +- **`JoystickMoveStart`**: Triggered when touch begins |
| 157 | +- **`JoystickMoving`**: Triggered during movement |
| 158 | +- **`JoystickMoveHeld`**: Triggered when held in position |
| 159 | +- **`JoystickMoveEnd`**: Triggered when touch ends |
| 160 | + |
| 161 | +## Sample App |
| 162 | + |
| 163 | +The `composeApp` module contains a comprehensive demo application showcasing: |
| 164 | +- Different drawing types (Circle, Arc, Wedge, etc.) |
| 165 | +- Customizable colors and styles |
| 166 | +- Event handling examples |
| 167 | +- Direction type switching |
| 168 | +- Real-time state visualization |
| 169 | + |
| 170 | +Run the sample: |
| 171 | +```bash |
| 172 | +./gradlew :composeApp:run |
| 173 | +``` |
| 174 | + |
| 175 | +## Platform Support |
| 176 | + |
| 177 | +| Platform | Status | |
| 178 | +|----------|-----------| |
| 179 | +| Android | API 21+ | |
| 180 | +| iOS | iOS 13+ * | |
| 181 | +| Desktop | JVM 11+ | |
| 182 | +| Web (JS) | Browser | |
| 183 | +| WASM | Browser | |
| 184 | + |
| 185 | +> **iOS Note:** The iOS 13+ minimum version is inherited from the default Kotlin Multiplatform configuration. The library has not been fully tested on all iOS versions, so compatibility with iOS 13+ is not 100% guaranteed. If you encounter any issues on specific iOS versions, please report them in the [issue tracker](https://github.com/yoimerdr/compose-virtualjoystick-multiplatform/issues). |
| 186 | +
|
| 187 | +## Building from Source |
| 188 | + |
| 189 | +```bash |
| 190 | +# Clone the repository |
| 191 | +git clone https://github.com/yoimerdr/compose-virtualjoystick-multiplatform.git |
| 192 | + |
| 193 | +# Build the library |
| 194 | +./gradlew :library:build |
| 195 | + |
| 196 | +# Run tests |
| 197 | +./gradlew :library:test |
| 198 | + |
| 199 | +# Publish to Maven Local |
| 200 | +./gradlew :library:publishToMavenLocal |
| 201 | +``` |
| 202 | + |
| 203 | +### Requirements |
| 204 | + |
| 205 | +- Kotlin 2.2.21+ |
| 206 | +- Compose Multiplatform 1.9.3+ |
| 207 | +- Android minSdk 21+ |
| 208 | +- iOS 13+ |
| 209 | +- JVM 11+ |
| 210 | + |
| 211 | +## Contributing |
| 212 | + |
| 213 | +Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change. |
| 214 | + |
| 215 | +1. Fork the repository |
| 216 | +2. Create your feature branch |
| 217 | +3. Commit your changes |
| 218 | +4. Push to the branch |
| 219 | +5. Open a Pull Request |
| 220 | + |
| 221 | +## License |
| 222 | + |
| 223 | +This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details. |
| 224 | + |
| 225 | +## Links |
| 226 | + |
| 227 | +- [GitHub Repository](https://github.com/yoimerdr/compose-virtualjoystick-multiplatform) |
| 228 | +- [Maven Central](https://central.sonatype.com/artifact/io.github.yoimerdr.compose/virtualjoystick) |
| 229 | +- [Issue Tracker](https://github.com/yoimerdr/compose-virtualjoystick-multiplatform/issues) |
| 230 | +- [Kotlin Multiplatform](https://kotlinlang.org/docs/multiplatform.html) |
| 231 | +- [Compose Multiplatform](https://www.jetbrains.com/lp/compose-multiplatform/) |
| 232 | + |
| 233 | +## Acknowledgments |
| 234 | + |
| 235 | +- Built with [Compose Multiplatform](https://www.jetbrains.com/lp/compose-multiplatform/) |
| 236 | +- Published using [Vanniktech Maven Publish Plugin](https://github.com/vanniktech/gradle-maven-publish-plugin) |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +**If you find this library useful, please consider giving it a ⭐ on GitHub!** |
0 commit comments