-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make BatchTiledMapRenderer extensions call original protected methods
- Loading branch information
1 parent
72e94b7
commit 984b9c3
Showing
3 changed files
with
90 additions
and
92 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,16 @@ | ||
package ktx.tiled | ||
@file:Suppress("PackageDirectoryMismatch") | ||
|
||
import com.badlogic.gdx.graphics.OrthographicCamera | ||
import com.badlogic.gdx.maps.tiled.renderers.BatchTiledMapRenderer | ||
import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile | ||
import com.badlogic.gdx.math.Matrix4 | ||
import com.badlogic.gdx.math.Rectangle | ||
package com.badlogic.gdx.maps.tiled.renderers | ||
|
||
/** | ||
* 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]. | ||
* 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. | ||
*/ | ||
inline fun <T : BatchTiledMapRenderer> T.use( | ||
camera: OrthographicCamera, | ||
block: (T) -> Unit | ||
) { | ||
this.setView(camera) | ||
AnimatedTiledMapTile.updateAnimationBaseTime() | ||
this.batch.begin() | ||
|
||
block(this) | ||
@PublishedApi | ||
internal fun BatchTiledMapRenderer.beginInternal() = this.beginRender() | ||
|
||
this.batch.end() | ||
} | ||
|
||
/** | ||
* 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) | ||
AnimatedTiledMapTile.updateAnimationBaseTime() | ||
this.batch.begin() | ||
|
||
block(this) | ||
|
||
this.batch.end() | ||
} | ||
|
||
/** | ||
* 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) { | ||
AnimatedTiledMapTile.updateAnimationBaseTime() | ||
this.batch.begin() | ||
|
||
block(this) | ||
|
||
this.batch.end() | ||
} | ||
@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() | ||
} |
40 changes: 16 additions & 24 deletions
40
...in/ktx/tiled/BatchTiledMapRendererTest.kt → .../kotlin/ktx/tiled/TiledMapRendererTest.kt
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 |
---|---|---|
@@ -1,83 +1,75 @@ | ||
package ktx.tiled | ||
|
||
import com.badlogic.gdx.graphics.OrthographicCamera | ||
import com.badlogic.gdx.graphics.g2d.Batch | ||
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 | ||
import io.kotlintest.mock.mock | ||
import io.kotlintest.mock.`when` | ||
import org.junit.Assert.assertSame | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.never | ||
import org.mockito.kotlin.verify | ||
|
||
class BatchTiledMapRendererTest { | ||
class TiledMapRendererTest { | ||
|
||
@Test | ||
fun `should call beginRender and endRender without setView`() { | ||
val renderer = mock<BatchTiledMapRenderer>() | ||
val batch = mock<Batch>() | ||
`when`(renderer.batch).thenReturn(batch) | ||
|
||
renderer.use { | ||
verify(batch).begin() | ||
verify(renderer).beginInternal() | ||
assertSame(renderer, it) | ||
verify(batch, never()).end() | ||
verify(renderer, never()).endInternal() | ||
} | ||
verify(batch).end() | ||
verify(renderer).endInternal() | ||
verify(renderer, never()).setView(any()) | ||
} | ||
|
||
@Test | ||
fun `should call beginRender and endRender with camera setView`() { | ||
val renderer = mock<BatchTiledMapRenderer>() | ||
val batch = mock<Batch>() | ||
`when`(renderer.batch).thenReturn(batch) | ||
val camera = OrthographicCamera() | ||
|
||
renderer.use(camera) { | ||
verify(batch).begin() | ||
verify(renderer).beginInternal() | ||
verify(renderer).setView(camera) | ||
assertSame(renderer, it) | ||
verify(batch, never()).end() | ||
verify(renderer, never()).endInternal() | ||
} | ||
verify(batch).end() | ||
verify(renderer).endInternal() | ||
} | ||
|
||
@Test | ||
fun `should call beginRender and endRender with map boundary setView`() { | ||
val renderer = mock<BatchTiledMapRenderer>() | ||
val batch = mock<Batch>() | ||
`when`(renderer.batch).thenReturn(batch) | ||
val mat4 = Matrix4() | ||
val mapBoundary = Rectangle(1f, 2f, 3f, 4f) | ||
|
||
renderer.use(mat4, mapBoundary.x, mapBoundary.y, mapBoundary.width, mapBoundary.height) { | ||
verify(batch).begin() | ||
renderer.use(mat4, mapBoundary) { | ||
verify(renderer).beginInternal() | ||
verify(renderer).setView(mat4, mapBoundary.x, mapBoundary.y, mapBoundary.width, mapBoundary.height) | ||
assertSame(renderer, it) | ||
verify(batch, never()).end() | ||
verify(renderer, never()).endInternal() | ||
} | ||
verify(batch).end() | ||
verify(renderer).endInternal() | ||
} | ||
|
||
@Test | ||
fun `should call beginRender and endRender with map boundary rectangle setView`() { | ||
val renderer = mock<BatchTiledMapRenderer>() | ||
val batch = mock<Batch>() | ||
`when`(renderer.batch).thenReturn(batch) | ||
val mat4 = Matrix4() | ||
val mapBoundary = Rectangle(1f, 2f, 3f, 4f) | ||
|
||
renderer.use(mat4, mapBoundary) { | ||
verify(batch).begin() | ||
verify(renderer).beginInternal() | ||
verify(renderer).setView(mat4, mapBoundary.x, mapBoundary.y, mapBoundary.width, mapBoundary.height) | ||
assertSame(renderer, it) | ||
verify(batch, never()).end() | ||
verify(renderer, never()).endInternal() | ||
} | ||
verify(batch).end() | ||
verify(renderer).endInternal() | ||
} | ||
|
||
} |