Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Apr 25, 2020
1 parent 92ae0f5 commit b23c800
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
android.useAndroidX=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions physicslayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
api("org.jbox2d:jbox2d-library:2.2.1.1")
api("com.github.Commit451:TranslationViewDragHelper:1.1.0")
api("com.github.Commit451:TranslationViewDragHelper:2.0.1")
}

apply from: "https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.0.0/gradle-android-javadocs.gradle"
apply from: "https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle"
85 changes: 43 additions & 42 deletions physicslayout/src/main/java/com/jawnnypoo/physicslayout/Physics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs:
const val MOON_GRAVITY = 1.6f
const val EARTH_GRAVITY = 9.8f
const val JUPITER_GRAVITY = 24.8f

// Size in DP of the bounds (world walls) of the view
private const val BOUND_SIZE_DP = 20
private const val FRAME_RATE = 1 / 60f
Expand Down Expand Up @@ -117,14 +118,14 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs:
override fun beginContact(contact: Contact) {
if (onCollisionListener != null) {
onCollisionListener!!.onCollisionEntered(contact.fixtureA.m_userData as Int,
contact.fixtureB.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)
contact.fixtureB.m_userData as Int)
}
}

Expand All @@ -147,18 +148,18 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs:
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
val body = viewBeingDragged?.getTag(R.id.physics_layout_body_tag) as? Body
if (body != null) {
body.angularVelocity = 0f
body.linearVelocity = Vec2(0f, 0f)
}
onFlingListener?.onGrabbed(capturedChild)
}

override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
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
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))
Expand All @@ -174,19 +175,19 @@ 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)
hasBounds = a.getBoolean(R.styleable.Physics_bounds, hasBounds)
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)
.getInt(R.styleable.Physics_positionIterations, positionIterations)
pixelsPerMeter = a.getFloat(R.styleable.Physics_pixelsPerMeter, viewGroup.resources
.getDimensionPixelSize(R.dimen.physics_layout_dp_per_meter).toFloat())
.getDimensionPixelSize(R.dimen.physics_layout_dp_per_meter).toFloat())
a.recycle()
}
}
Expand Down Expand Up @@ -247,7 +248,7 @@ class Physics @JvmOverloads constructor(private val viewGroup: ViewGroup, attrs:
* Call this in your ViewGroup if you plan on using fling
* @return true if consumed, false otherwise
*/
fun onTouchEvent(ev: MotionEvent?): Boolean {
fun onTouchEvent(ev: MotionEvent): Boolean {
if (!isFlingEnabled) {
return false
}
Expand Down Expand Up @@ -286,19 +287,19 @@ 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
)
}
}
Expand Down Expand Up @@ -351,34 +352,34 @@ 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)
}
Expand All @@ -401,10 +402,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
)
}

Expand Down Expand Up @@ -499,9 +500,9 @@ 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)
}

/**
Expand Down

0 comments on commit b23c800

Please sign in to comment.