-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shape2D destructuring and MapRenderer use extension. #479
* #479: add math Shape2D destructure extensions and MapRenderer use extension * move Shape2D info to separate section * make BatchTiledMapRenderer extensions call original protected methods * remove warning info (not necessary anymore) --------- Co-authored-by: Simon Klausner <simon.klausner@nttdata.com>
- Loading branch information
1 parent
671f7e5
commit ddab0e7
Showing
15 changed files
with
395 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Circle | ||
|
||
/** | ||
* Operator function that allows to deconstruct this circle. | ||
* @return X component. | ||
*/ | ||
operator fun Circle.component1() = this.x | ||
|
||
/** | ||
* Operator function that allows to deconstruct this circle. | ||
* @return Y component. | ||
*/ | ||
operator fun Circle.component2() = this.y | ||
|
||
/** | ||
* Operator function that allows to deconstruct this circle. | ||
* @return Radius component. | ||
*/ | ||
operator fun Circle.component3() = this.radius |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Ellipse | ||
|
||
/** | ||
* Operator function that allows to deconstruct this ellipse. | ||
* @return X component. | ||
*/ | ||
operator fun Ellipse.component1() = this.x | ||
|
||
/** | ||
* Operator function that allows to deconstruct this ellipse. | ||
* @return Y component. | ||
*/ | ||
operator fun Ellipse.component2() = this.y | ||
|
||
/** | ||
* Operator function that allows to deconstruct this ellipse. | ||
* @return Width component. | ||
*/ | ||
operator fun Ellipse.component3() = this.width | ||
|
||
/** | ||
* Operator function that allows to deconstruct this ellipse. | ||
* @return Height component. | ||
*/ | ||
operator fun Ellipse.component4() = this.height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Polygon | ||
|
||
/** | ||
* Operator function that allows to deconstruct this polygon. | ||
* @return X component. | ||
*/ | ||
operator fun Polygon.component1() = this.x | ||
|
||
/** | ||
* Operator function that allows to deconstruct this polygon. | ||
* @return Y component. | ||
*/ | ||
operator fun Polygon.component2() = this.y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Polyline | ||
|
||
/** | ||
* Operator function that allows to deconstruct this polyline. | ||
* @return X component. | ||
*/ | ||
operator fun Polyline.component1() = this.x | ||
|
||
/** | ||
* Operator function that allows to deconstruct this polyline. | ||
* @return Y component. | ||
*/ | ||
operator fun Polyline.component2() = this.y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Rectangle | ||
|
||
/** | ||
* Operator function that allows to deconstruct this rectangle. | ||
* @return X component. | ||
*/ | ||
operator fun Rectangle.component1() = this.x | ||
|
||
/** | ||
* Operator function that allows to deconstruct this rectangle. | ||
* @return Y component. | ||
*/ | ||
operator fun Rectangle.component2() = this.y | ||
|
||
/** | ||
* Operator function that allows to deconstruct this rectangle. | ||
* @return Width component. | ||
*/ | ||
operator fun Rectangle.component3() = this.width | ||
|
||
/** | ||
* Operator function that allows to deconstruct this rectangle. | ||
* @return Height component. | ||
*/ | ||
operator fun Rectangle.component4() = this.height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Circle | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class CircleTest { | ||
|
||
@Test | ||
fun `should destructure Circle`() { | ||
val circle = Circle(1f, 2f, 3f) | ||
|
||
val (x, y, radius) = circle | ||
|
||
assertEquals(1f, x) | ||
assertEquals(2f, y) | ||
assertEquals(3f, radius) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Ellipse | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class EllipseTest { | ||
|
||
@Test | ||
fun `should destructure Ellipse`() { | ||
val ellipse = Ellipse(1f, 2f, 3f, 4f) | ||
|
||
val (x, y, w, h) = ellipse | ||
|
||
assertEquals(1f, x) | ||
assertEquals(2f, y) | ||
assertEquals(3f, w) | ||
assertEquals(4f, h) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Polygon | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class PolygonTest { | ||
|
||
@Test | ||
fun `should destructure Polygon`() { | ||
val polygon = Polygon().apply { setPosition(1f, 2f) } | ||
|
||
val (x, y) = polygon | ||
|
||
assertEquals(1f, x) | ||
assertEquals(2f, y) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Polyline | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class PolylineTest { | ||
|
||
@Test | ||
fun `should destructure Polyline`() { | ||
val polyline = Polyline().apply { setPosition(1f, 2f) } | ||
|
||
val (x, y) = polyline | ||
|
||
assertEquals(1f, x) | ||
assertEquals(2f, y) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ktx.math | ||
|
||
import com.badlogic.gdx.math.Rectangle | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class RectangleTest { | ||
|
||
@Test | ||
fun `should destructure Rectangle`() { | ||
val rect = Rectangle(1f, 2f, 3f, 4f) | ||
|
||
val (x, y, w, h) = rect | ||
|
||
assertEquals(1f, x) | ||
assertEquals(2f, y) | ||
assertEquals(3f, w) | ||
assertEquals(4f, h) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@file:Suppress("PackageDirectoryMismatch") | ||
|
||
package com.badlogic.gdx.maps.tiled.renderers | ||
|
||
/** | ||
* This file is used as a workaround for the tiledMapRenderer extensions. They need | ||
* to call [BatchTiledMapRenderer.beginRender] and [BatchTiledMapRenderer.endRender] methods | ||
* which are protected methods. Since this file matches the package of the [BatchTiledMapRenderer], | ||
* we can call protected methods of it and use our wrapper functions for public API extensions. | ||
*/ | ||
|
||
@PublishedApi | ||
internal fun BatchTiledMapRenderer.beginInternal() = this.beginRender() | ||
|
||
@PublishedApi | ||
internal fun BatchTiledMapRenderer.endInternal() = this.endRender() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ktx.tiled | ||
|
||
import com.badlogic.gdx.graphics.OrthographicCamera | ||
import com.badlogic.gdx.maps.tiled.renderers.BatchTiledMapRenderer | ||
import com.badlogic.gdx.maps.tiled.renderers.beginInternal | ||
import com.badlogic.gdx.maps.tiled.renderers.endInternal | ||
import com.badlogic.gdx.math.Matrix4 | ||
import com.badlogic.gdx.math.Rectangle | ||
|
||
/** | ||
* Automatically calls [BatchTiledMapRenderer.beginRender] and [BatchTiledMapRenderer.endRender]. | ||
* @param camera A camera to set on the renderer before [BatchTiledMapRenderer.beginRender]. | ||
* @param block inlined. Executed after [BatchTiledMapRenderer.beginRender] and before [BatchTiledMapRenderer.endRender]. | ||
*/ | ||
inline fun <T : BatchTiledMapRenderer> T.use( | ||
camera: OrthographicCamera, | ||
block: (T) -> Unit | ||
) { | ||
this.setView(camera) | ||
this.use(block) | ||
} | ||
|
||
/** | ||
* Automatically calls [BatchTiledMapRenderer.beginRender] and [BatchTiledMapRenderer.endRender]. | ||
* @param projection A projection matrix to set on the renderer before [BatchTiledMapRenderer.beginRender]. | ||
* @param x map render boundary x value. | ||
* @param y map render boundary y value. | ||
* @param width map render boundary width value. | ||
* @param height map render boundary height value. | ||
* @param block inlined. Executed after [BatchTiledMapRenderer.beginRender] and before [BatchTiledMapRenderer.endRender]. | ||
*/ | ||
inline fun <T : BatchTiledMapRenderer> T.use( | ||
projection: Matrix4, | ||
x: Float, y: Float, | ||
width: Float, height: Float, | ||
block: (T) -> Unit | ||
) { | ||
this.setView(projection, x, y, width, height) | ||
this.use(block) | ||
} | ||
|
||
/** | ||
* Automatically calls [BatchTiledMapRenderer.beginRender] and [BatchTiledMapRenderer.endRender]. | ||
* @param projection A projection matrix to set on the renderer before [BatchTiledMapRenderer.beginRender]. | ||
* @param mapBoundary map render boundary. | ||
* @param block inlined. Executed after [BatchTiledMapRenderer.beginRender] and before [BatchTiledMapRenderer.endRender]. | ||
*/ | ||
inline fun <T : BatchTiledMapRenderer> T.use( | ||
projection: Matrix4, | ||
mapBoundary: Rectangle, | ||
block: (T) -> Unit | ||
) = this.use(projection, mapBoundary.x, mapBoundary.y, mapBoundary.width, mapBoundary.height, block) | ||
|
||
/** | ||
* Automatically calls [BatchTiledMapRenderer.beginRender] and [BatchTiledMapRenderer.endRender]. | ||
* @param block inlined. Executed after [BatchTiledMapRenderer.beginRender] and before [BatchTiledMapRenderer.endRender]. | ||
*/ | ||
inline fun <T : BatchTiledMapRenderer> T.use(block: (T) -> Unit) { | ||
this.beginInternal() | ||
|
||
block(this) | ||
|
||
this.endInternal() | ||
} |
Oops, something went wrong.