diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fab4dcf..9956674 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,17 +10,17 @@ jobs: fail-fast: false matrix: java-version: - - 11 + - 17 steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Validate Gradle Wrapper uses: gradle/wrapper-validation-action@v1 - name: Configure JDK - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: ${{ matrix.java-version }} diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index 652f945..688cd70 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -15,12 +15,12 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - - name: Install JDK 11 - uses: actions/setup-java@v1 + - name: Install JDK + uses: actions/setup-java@v4 with: - java-version: 11 + java-version: 17 - name: Set version run: sed -i "s/VERSION_NAME=0.0.1/VERSION_NAME=$GITHUB_REF_NAME/" gradle.properties diff --git a/README.md b/README.md index 526c106..f22bf80 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Each view contained within the layout has a physics configuration that it uses t app:layout_density="0.5" /> ``` or alternatively, the Physics definition can be made programmatically: -```java +``` val circleView = findViewById(R.id.circle) val config = PhysicsConfig( shape = Shape.CIRCLE, @@ -96,7 +96,7 @@ This library was designed with the intention of allowing for playful animations License -------- - Copyright 2022 John Carlson + Copyright 2024 John Carlson Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/app/build.gradle b/app/build.gradle index 3876e06..49b081c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,12 +2,13 @@ apply plugin: "com.android.application" apply plugin: "kotlin-android" android { - compileSdkVersion 32 + namespace = "com.jawnnypoo.physicslayout.sample" + compileSdk 34 defaultConfig { applicationId "com.jawnnypoo.physicslayout.sample" - minSdkVersion 21 - targetSdkVersion 32 + minSdk 21 + targetSdk 34 versionCode 101 versionName "1.0.1" } @@ -23,33 +24,23 @@ android { } } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } - - lintOptions { - abortOnError false - } } -tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { - kotlinOptions { - jvmTarget = "1.8" - } +kotlin { + jvmToolchain 17 } -def coroutinesVersion = "1.6.0" +def coroutinesVersion = "1.7.1" def retrofitVersion = "2.9.0" dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion") - implementation("androidx.activity:activity-ktx:1.4.0") - implementation("androidx.appcompat:appcompat:1.4.1") - implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1") + implementation("androidx.activity:activity-ktx:1.8.2") + implementation("androidx.appcompat:appcompat:1.6.1") + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0") - implementation("com.google.android.material:material:1.5.0") + implementation("com.google.android.material:material:1.11.0") implementation("de.hdodenhof:circleimageview:3.1.0") diff --git a/app/src/main/java/com/jawnnypoo/physicslayout/sample/AboutActivity.kt b/app/src/main/java/com/jawnnypoo/physicslayout/sample/AboutActivity.kt index aad0487..aab7977 100644 --- a/app/src/main/java/com/jawnnypoo/physicslayout/sample/AboutActivity.kt +++ b/app/src/main/java/com/jawnnypoo/physicslayout/sample/AboutActivity.kt @@ -36,7 +36,7 @@ class AboutActivity : AppCompatActivity() { private lateinit var binding: ActivityAboutBinding private lateinit var sensorManager: SensorManager - private lateinit var gravitySensor: Sensor + private var gravitySensor: Sensor? = null private lateinit var gimbal: Gimbal private val sensorEventListener = object : SensorEventListener { @@ -61,18 +61,22 @@ class AboutActivity : AppCompatActivity() { val drawerArrowDrawable = DrawerArrowDrawable(this) drawerArrowDrawable.progress = 1.0f binding.toolbar.navigationIcon = drawerArrowDrawable - binding.toolbar.setNavigationOnClickListener { onBackPressed() } + binding.toolbar.setNavigationOnClickListener { finish() } sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) val model: ContributorsViewModel by viewModels() - model.getContributors().observe(this, { contributors -> + model.getContributors().observe(this) { contributors -> addContributors(contributors) - }) + } } override fun onResume() { super.onResume() - sensorManager.registerListener(sensorEventListener, gravitySensor, SensorManager.SENSOR_DELAY_GAME) + sensorManager.registerListener( + sensorEventListener, + gravitySensor, + SensorManager.SENSOR_DELAY_GAME + ) } override fun onPause() { @@ -88,8 +92,9 @@ class AboutActivity : AppCompatActivity() { val contributor = contributors[i] val imageView = CircleImageView(this) val llp = FlowLayout.LayoutParams( - imageSize, - imageSize) + imageSize, + imageSize + ) imageView.layoutParams = llp imageView.borderWidth = borderSize imageView.borderColor = Color.BLACK @@ -108,8 +113,11 @@ class AboutActivity : AppCompatActivity() { try { startActivity(intent) } catch (e: ActivityNotFoundException) { - Toast.makeText(this, "You don't have a browser... What are you doing?", Toast.LENGTH_LONG) - .show() + Toast.makeText( + this, + "You don't have a browser... What are you doing?", + Toast.LENGTH_LONG + ).show() } } diff --git a/app/src/main/java/com/jawnnypoo/physicslayout/sample/MainActivity.kt b/app/src/main/java/com/jawnnypoo/physicslayout/sample/MainActivity.kt index 0b7f5c3..9be8e9a 100644 --- a/app/src/main/java/com/jawnnypoo/physicslayout/sample/MainActivity.kt +++ b/app/src/main/java/com/jawnnypoo/physicslayout/sample/MainActivity.kt @@ -45,7 +45,7 @@ class MainActivity : AppCompatActivity() { if (!isChecked) { for (i in 0 until binding.physicsLayout.childCount) { binding.physicsLayout.getChildAt(i) - .animate().translationY(0f).translationX(0f).rotation(0f) + .animate().translationY(0f).translationX(0f).rotation(0f) } } } @@ -54,14 +54,14 @@ class MainActivity : AppCompatActivity() { } binding.impulseButton.setOnClickListener { binding.physicsLayout.physics.giveRandomImpulse() } val circlePhysicsConfig = PhysicsConfig( - shape = Shape.CIRCLE + shape = Shape.CIRCLE ) binding.addViewButton.setOnClickListener { val imageView = ImageView(this) imageView.setImageResource(R.drawable.ic_logo) val layoutParams = LinearLayout.LayoutParams( - resources.getDimensionPixelSize(R.dimen.square_size), - resources.getDimensionPixelSize(R.dimen.square_size) + resources.getDimensionPixelSize(R.dimen.square_size), + resources.getDimensionPixelSize(R.dimen.square_size) ) imageView.layoutParams = layoutParams imageView.id = index @@ -87,13 +87,15 @@ class MainActivity : AppCompatActivity() { override fun onCollisionExited(viewIdA: Int, viewIdB: Int) {} }) - binding.physicsLayout.physics.addOnPhysicsProcessedListener(object : Physics.OnPhysicsProcessedListener { + binding.physicsLayout.physics.addOnPhysicsProcessedListener(object : + Physics.OnPhysicsProcessedListener { override fun onPhysicsProcessed(physics: Physics, world: World) { Log.d(TAG, "onPhysicsProcessed") } }) - binding.physicsLayout.physics.setOnBodyCreatedListener(object : Physics.OnBodyCreatedListener { + binding.physicsLayout.physics.setOnBodyCreatedListener(object : + Physics.OnBodyCreatedListener { override fun onBodyCreated(view: View, body: Body) { Log.d(TAG, "Body created for view ${view.id}") } diff --git a/app/src/main/java/com/jawnnypoo/physicslayout/sample/PhysicsFlowLayout.kt b/app/src/main/java/com/jawnnypoo/physicslayout/sample/PhysicsFlowLayout.kt index 9d55020..d7f891c 100644 --- a/app/src/main/java/com/jawnnypoo/physicslayout/sample/PhysicsFlowLayout.kt +++ b/app/src/main/java/com/jawnnypoo/physicslayout/sample/PhysicsFlowLayout.kt @@ -27,7 +27,11 @@ class PhysicsFlowLayout : FlowLayout { init(attrs) } - constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { init(attrs) } @@ -63,7 +67,8 @@ class PhysicsFlowLayout : FlowLayout { return LayoutParams(context, attrs) } - class LayoutParams(c: Context, attrs: AttributeSet) : FlowLayout.LayoutParams(c, attrs), PhysicsLayoutParams { + class LayoutParams(c: Context, attrs: AttributeSet) : FlowLayout.LayoutParams(c, attrs), + PhysicsLayoutParams { override var config: PhysicsConfig = PhysicsLayoutParamsProcessor.process(c, attrs) } } diff --git a/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/Contributor.kt b/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/Contributor.kt index 3a1dbd5..38bd343 100644 --- a/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/Contributor.kt +++ b/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/Contributor.kt @@ -2,7 +2,7 @@ package com.jawnnypoo.physicslayout.sample.github import com.squareup.moshi.Json -data class Contributor ( +data class Contributor( @Json(name = "avatar_url") var avatarUrl: String? = null ) diff --git a/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/GitHubClient.kt b/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/GitHubClient.kt index 3f68785..3c0c1a1 100644 --- a/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/GitHubClient.kt +++ b/app/src/main/java/com/jawnnypoo/physicslayout/sample/github/GitHubClient.kt @@ -11,12 +11,12 @@ object GitHubClient { private val github: GitHub by lazy { val moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .build() + .add(KotlinJsonAdapterFactory()) + .build() val retrofit = Retrofit.Builder() - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .baseUrl("https://api.github.com/") - .build() + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .baseUrl("https://api.github.com/") + .build() retrofit.create(GitHub::class.java) } @@ -27,8 +27,8 @@ object GitHubClient { interface GitHub { @GET("/repos/{owner}/{repo}/contributors") suspend fun contributors( - @Path("owner") owner: String, - @Path("repo") repo: String + @Path("owner") owner: String, + @Path("repo") repo: String ): List } } diff --git a/build.gradle b/build.gradle index 62ed51e..fca175a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,20 +1,16 @@ buildscript { - ext.kotlin_version = "1.6.10" + ext.kotlin_version = "1.9.10" repositories { google() mavenCentral() } dependencies { - classpath "com.android.tools.build:gradle:7.1.1" + classpath 'com.android.tools.build:gradle:8.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.vanniktech:gradle-maven-publish-plugin:0.18.0" } } -plugins { - id "com.github.ben-manes.versions" version "0.42.0" -} - allprojects { repositories { google() diff --git a/gradle.properties b/gradle.properties index 5fb2266..d1536a0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -39,4 +39,5 @@ POM_DEVELOPER_ID=Jawnnypoo POM_DEVELOPER_NAME=John Carlson POM_DEVELOPER_URL=https://github.com/Jawnnypoo/ +# This gets updated by CI VERSION_NAME=0.0.1 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b1159fc..89aa556 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/physicslayout/build.gradle b/physicslayout/build.gradle index 4e91600..b907703 100644 --- a/physicslayout/build.gradle +++ b/physicslayout/build.gradle @@ -4,21 +4,20 @@ apply plugin: "com.vanniktech.maven.publish" apply from: "../publish.gradle" android { - compileSdkVersion 32 + namespace = "com.jawnnypoo.physicslayout" + compileSdk 34 defaultConfig { - minSdkVersion 15 - targetSdkVersion 32 - versionCode 1 - versionName "1.0" - } - lintOptions { - abortOnError false + minSdk 15 + targetSdk 34 } } +kotlin { + jvmToolchain 17 +} + dependencies { - api("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") api("org.jbox2d:jbox2d-library:2.2.1.1") api("com.commit451:translationviewdraghelper:2.0.3") } diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/Physics.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/Physics.kt index 440d62e..5d34b77 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/Physics.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/Physics.kt @@ -15,9 +15,13 @@ import org.jbox2d.collision.Manifold import org.jbox2d.collision.shapes.CircleShape import org.jbox2d.collision.shapes.PolygonShape import org.jbox2d.common.Vec2 -import org.jbox2d.dynamics.* +import org.jbox2d.dynamics.Body +import org.jbox2d.dynamics.BodyDef +import org.jbox2d.dynamics.BodyType +import org.jbox2d.dynamics.FixtureDef +import org.jbox2d.dynamics.World import org.jbox2d.dynamics.contacts.Contact -import java.util.* +import java.util.Random import kotlin.math.max /** @@ -25,7 +29,10 @@ import kotlin.math.max * layouts without requiring further extension (LinearLayout, RelativeLayout, etc.) */ @Suppress("unused", "MemberVisibilityCanBePrivate") -class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: AttributeSet? = null) { +class Physics @JvmOverloads constructor( + private val viewGroup: ViewGroup, + attrs: AttributeSet? = null +) { companion object { private val TAG = Physics::class.java.simpleName @@ -118,59 +125,58 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: private val contactListener: ContactListener = object : ContactListener { override fun beginContact(contact: Contact) { - if (onCollisionListener != null) { - onCollisionListener!!.onCollisionEntered(contact.fixtureA.m_userData as Int, - contact.fixtureB.m_userData as Int) - } + onCollisionListener?.onCollisionEntered( + contact.fixtureA.m_userData as Int, + contact.fixtureB.m_userData as Int, + ) } override fun endContact(contact: Contact) { - if (onCollisionListener != null) { - onCollisionListener!!.onCollisionExited(contact.fixtureA.m_userData as Int, - contact.fixtureB.m_userData as Int) - } + onCollisionListener?.onCollisionExited( + contact.fixtureA.m_userData as Int, + contact.fixtureB.m_userData as Int, + ) } override fun preSolve(contact: Contact, oldManifold: Manifold) {} override fun postSolve(contact: Contact, impulse: ContactImpulse) {} } - private val viewDragHelperCallback: TranslationViewDragHelper.Callback = object : TranslationViewDragHelper.Callback() { - override fun tryCaptureView(child: View, pointerId: Int): Boolean { - return true - } + private val viewDragHelperCallback: TranslationViewDragHelper.Callback = + object : TranslationViewDragHelper.Callback() { + override fun tryCaptureView(child: View, pointerId: Int): Boolean { + return true + } - override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int { - return left - } + override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int { + return left + } - override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int { - return top - } + override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int { + return top + } - override fun onViewCaptured(capturedChild: View, activePointerId: Int) { - super.onViewCaptured(capturedChild, activePointerId) - viewBeingDragged = capturedChild - val body = viewBeingDragged?.getTag(R.id.physics_layout_body_tag) as? Body - if (body != null) { - body.angularVelocity = 0f - body.linearVelocity = Vec2(0f, 0f) + override fun onViewCaptured(capturedChild: View, activePointerId: Int) { + super.onViewCaptured(capturedChild, activePointerId) + viewBeingDragged = capturedChild + val body = viewBeingDragged?.getTag(R.id.physics_layout_body_tag) as? Body + body?.angularVelocity = 0f + body?.linearVelocity = Vec2(0f, 0f) + onFlingListener?.onGrabbed(capturedChild) } - onFlingListener?.onGrabbed(capturedChild) - } - override fun onViewReleased(releasedChild: View?, xvel: Float, yvel: Float) { - super.onViewReleased(releasedChild, xvel, yvel) - viewBeingDragged = null - val body = releasedChild?.getTag(R.id.physics_layout_body_tag) as? Body - if (body != null) { - translateBodyToView(body, releasedChild) - body.linearVelocity = Vec2(pixelsToMeters(xvel), pixelsToMeters(yvel)) - body.isAwake = true + override fun onViewReleased(releasedChild: View?, xvel: Float, yvel: Float) { + super.onViewReleased(releasedChild, xvel, yvel) + viewBeingDragged = null + val body = releasedChild?.getTag(R.id.physics_layout_body_tag) as? Body + if (body != null) { + translateBodyToView(body, releasedChild) + body.linearVelocity = Vec2(pixelsToMeters(xvel), pixelsToMeters(yvel)) + body.isAwake = true + } + onFlingListener?.onReleased(releasedChild) } - onFlingListener?.onReleased(releasedChild) } - } init { viewDragHelper = TranslationViewDragHelper.create(viewGroup, 1.0f, viewDragHelperCallback) @@ -178,7 +184,7 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: density = viewGroup.resources.displayMetrics.density if (attrs != null) { val a = viewGroup.context - .obtainStyledAttributes(attrs, R.styleable.Physics) + .obtainStyledAttributes(attrs, R.styleable.Physics) isPhysicsEnabled = a.getBoolean(R.styleable.Physics_physics, isPhysicsEnabled) gravityX = a.getFloat(R.styleable.Physics_gravityX, gravityX) gravityY = a.getFloat(R.styleable.Physics_gravityY, gravityY) @@ -186,11 +192,13 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: boundsSize = a.getDimension(R.styleable.Physics_boundsSize, BOUND_SIZE_DP * density) isFlingEnabled = a.getBoolean(R.styleable.Physics_fling, isFlingEnabled) velocityIterations = a - .getInt(R.styleable.Physics_velocityIterations, velocityIterations) + .getInt(R.styleable.Physics_velocityIterations, velocityIterations) positionIterations = a - .getInt(R.styleable.Physics_positionIterations, positionIterations) - pixelsPerMeter = a.getFloat(R.styleable.Physics_pixelsPerMeter, viewGroup.resources - .getDimensionPixelSize(R.dimen.physics_layout_dp_per_meter).toFloat()) + .getInt(R.styleable.Physics_positionIterations, positionIterations) + pixelsPerMeter = a.getFloat( + R.styleable.Physics_pixelsPerMeter, viewGroup.resources + .getDimensionPixelSize(R.dimen.physics_layout_dp_per_meter).toFloat() + ) a.recycle() } } @@ -290,19 +298,20 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: when (config.shape) { Shape.RECTANGLE -> { canvas.drawRect( - metersToPixels(body.position.x) - view.width / 2, - metersToPixels(body.position.y) - view.height / 2, - metersToPixels(body.position.x) + view.width / 2, - metersToPixels(body.position.y) + view.height / 2, - debugPaint + metersToPixels(body.position.x) - view.width / 2, + metersToPixels(body.position.y) - view.height / 2, + metersToPixels(body.position.x) + view.width / 2, + metersToPixels(body.position.y) + view.height / 2, + debugPaint ) } + Shape.CIRCLE -> { canvas.drawCircle( - metersToPixels(body.position.x), - metersToPixels(body.position.y), - config.radius, - debugPaint + metersToPixels(body.position.x), + metersToPixels(body.position.y), + config.radius, + debugPaint ) } } @@ -355,39 +364,44 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: private fun createBounds() { val top = createBound( - widthInPixels = width.toFloat(), - heightInPixels = boundsSize, - id = R.id.physics_layout_bound_top, - side = Bound.Side.TOP + widthInPixels = width.toFloat(), + heightInPixels = boundsSize, + id = R.id.physics_layout_bound_top, + side = Bound.Side.TOP ) bounds.add(top) val bottom = createBound( - widthInPixels = width.toFloat(), - heightInPixels = boundsSize, - id = R.id.physics_layout_bound_bottom, - side = Bound.Side.BOTTOM + widthInPixels = width.toFloat(), + heightInPixels = boundsSize, + id = R.id.physics_layout_bound_bottom, + side = Bound.Side.BOTTOM ) bounds.add(bottom) val left = createBound( - widthInPixels = boundsSize, - heightInPixels = height.toFloat(), - id = R.id.physics_layout_bound_left, - side = Bound.Side.LEFT + widthInPixels = boundsSize, + heightInPixels = height.toFloat(), + id = R.id.physics_layout_bound_left, + side = Bound.Side.LEFT ) bounds.add(left) val right = createBound( - widthInPixels = boundsSize, - heightInPixels = height.toFloat(), - id = R.id.physics_layout_bound_right, - side = Bound.Side.RIGHT + widthInPixels = boundsSize, + heightInPixels = height.toFloat(), + id = R.id.physics_layout_bound_right, + side = Bound.Side.RIGHT ) bounds.add(right) } - private fun createBound(widthInPixels: Float, heightInPixels: Float, id: Int, side: Bound.Side): Bound { + private fun createBound( + widthInPixels: Float, + heightInPixels: Float, + id: Int, + side: Bound.Side + ): Bound { val bodyDef = BodyDef() bodyDef.type = BodyType.STATIC val box = PolygonShape() @@ -405,10 +419,10 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: val body = world!!.createBody(bodyDef) body.createFixture(fixtureDef) return Bound( - widthInPixels = widthInPixels, - heightInPixels = heightInPixels, - body = body, - side = side + widthInPixels = widthInPixels, + heightInPixels = heightInPixels, + body = body, + side = side ) } @@ -434,7 +448,8 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: view.setTag(R.id.physics_layout_config_tag, config) } val bodyDef = config.bodyDef - bodyDef.position[pixelsToMeters(view.x + view.width / 2)] = pixelsToMeters(view.y + view.height / 2) + bodyDef.position[pixelsToMeters(view.x + view.width / 2)] = + pixelsToMeters(view.y + view.height / 2) if (oldBody != null) { bodyDef.angle = oldBody.angle bodyDef.angularVelocity = oldBody.angularVelocity @@ -445,7 +460,11 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: bodyDef.angularVelocity = degreesToRadians(view.rotation) } val fixtureDef = config.fixtureDef - fixtureDef.shape = if (config.shape == Shape.RECTANGLE) createBoxShape(view) else createCircleShape(view, config) + fixtureDef.shape = + if (config.shape == Shape.RECTANGLE) createBoxShape(view) else createCircleShape( + view, + config + ) fixtureDef.userData = view.id val body = world!!.createBody(bodyDef) body.createFixture(fixtureDef) @@ -481,9 +500,7 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: */ fun findBodyById(id: Int): Body? { val view = viewGroup.findViewById(id) - return if (view != null) { - view.getTag(R.id.physics_layout_body_tag) as? Body - } else null + return view?.getTag(R.id.physics_layout_body_tag) as? Body? } /** @@ -495,7 +512,10 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: var impulse: Vec2 val random = Random() for (i in 0 until viewGroup.childCount) { - impulse = Vec2((random.nextInt(1000) - 1000).toFloat(), (random.nextInt(1000) - 1000).toFloat()) + impulse = Vec2( + (random.nextInt(1000) - 1000).toFloat(), + (random.nextInt(1000) - 1000).toFloat() + ) body = viewGroup.getChildAt(i).getTag(R.id.physics_layout_body_tag) as? Body body?.applyLinearImpulse(impulse, body.position) } @@ -503,9 +523,12 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs: private fun translateBodyToView(body: Body, view: View) { body.setTransform( - Vec2(pixelsToMeters(view.x + view.width / 2), - pixelsToMeters(view.y + view.height / 2)), - body.angle) + Vec2( + pixelsToMeters(view.x + view.width / 2), + pixelsToMeters(view.y + view.height / 2) + ), + body.angle + ) } /** diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsConfig.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsConfig.kt index 5f7a2cc..85d0228 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsConfig.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsConfig.kt @@ -41,7 +41,7 @@ data class PhysicsConfig( fun createDefaultBodyDef(): BodyDef { val bodyDef = BodyDef() - bodyDef.type = BodyType.DYNAMIC //movable by default + bodyDef.type = BodyType.DYNAMIC // movable by default return bodyDef } } diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsFrameLayout.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsFrameLayout.kt index 03197cd..c9cf51f 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsFrameLayout.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsFrameLayout.kt @@ -25,12 +25,21 @@ class PhysicsFrameLayout : FrameLayout { init(attrs) } - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { init(attrs) } @TargetApi(21) - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { + constructor( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int, + defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { init(attrs) } @@ -67,7 +76,8 @@ class PhysicsFrameLayout : FrameLayout { return LayoutParams(context, attrs) } - class LayoutParams(c: Context, attrs: AttributeSet?) : FrameLayout.LayoutParams(c, attrs), PhysicsLayoutParams { + class LayoutParams(c: Context, attrs: AttributeSet?) : FrameLayout.LayoutParams(c, attrs), + PhysicsLayoutParams { override var config: PhysicsConfig = PhysicsLayoutParamsProcessor.process(c, attrs) } } diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLayoutParamsProcessor.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLayoutParamsProcessor.kt index a97e096..9d89cf3 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLayoutParamsProcessor.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLayoutParamsProcessor.kt @@ -36,18 +36,21 @@ object PhysicsLayoutParamsProcessor { config.shape = shape } if (array.hasValue(R.styleable.Physics_Layout_layout_circleRadius)) { - val radius = array.getDimensionPixelSize(R.styleable.Physics_Layout_layout_circleRadius, -1) + val radius = + array.getDimensionPixelSize(R.styleable.Physics_Layout_layout_circleRadius, -1) config.radius = radius.toFloat() } } private fun processBodyDef(array: TypedArray, config: PhysicsConfig) { if (array.hasValue(R.styleable.Physics_Layout_layout_bodyType)) { - val type = array.getInt(R.styleable.Physics_Layout_layout_bodyType, BodyType.DYNAMIC.ordinal) - config.bodyDef.type = BodyType.values()[type] + val type = + array.getInt(R.styleable.Physics_Layout_layout_bodyType, BodyType.DYNAMIC.ordinal) + config.bodyDef.type = BodyType.entries.toTypedArray()[type] } if (array.hasValue(R.styleable.Physics_Layout_layout_fixedRotation)) { - val fixedRotation = array.getBoolean(R.styleable.Physics_Layout_layout_fixedRotation, false) + val fixedRotation = + array.getBoolean(R.styleable.Physics_Layout_layout_fixedRotation, false) config.bodyDef.fixedRotation = fixedRotation } } diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLinearLayout.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLinearLayout.kt index 90d8ce4..71d2cf4 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLinearLayout.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsLinearLayout.kt @@ -25,12 +25,21 @@ class PhysicsLinearLayout : LinearLayout { init(attrs) } - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { init(attrs) } @TargetApi(21) - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { + constructor( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int, + defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { init(attrs) } @@ -67,7 +76,8 @@ class PhysicsLinearLayout : LinearLayout { return LayoutParams(context, attrs) } - class LayoutParams(c: Context, attrs: AttributeSet?) : LinearLayout.LayoutParams(c, attrs), PhysicsLayoutParams { + class LayoutParams(c: Context, attrs: AttributeSet?) : LinearLayout.LayoutParams(c, attrs), + PhysicsLayoutParams { override var config: PhysicsConfig = PhysicsLayoutParamsProcessor.process(c, attrs) } } diff --git a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsRelativeLayout.kt b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsRelativeLayout.kt index 8782902..f349055 100644 --- a/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsRelativeLayout.kt +++ b/physicslayout/src/main/java/com/jawnnypoo/physicslayout/PhysicsRelativeLayout.kt @@ -25,12 +25,21 @@ class PhysicsRelativeLayout : RelativeLayout { init(attrs) } - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { init(attrs) } @TargetApi(21) - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { + constructor( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int, + defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { init(attrs) } @@ -67,7 +76,8 @@ class PhysicsRelativeLayout : RelativeLayout { return LayoutParams(context, attrs) } - class LayoutParams(c: Context, attrs: AttributeSet?) : RelativeLayout.LayoutParams(c, attrs), PhysicsLayoutParams { + class LayoutParams(c: Context, attrs: AttributeSet?) : RelativeLayout.LayoutParams(c, attrs), + PhysicsLayoutParams { override var config: PhysicsConfig = PhysicsLayoutParamsProcessor.process(c, attrs) } }