Skip to content

Commit

Permalink
make BatchTiledMapRenderer extensions call original protected methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Klausner authored and Quillraven committed Mar 26, 2024
1 parent 72e94b7 commit 984b9c3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 92 deletions.
78 changes: 10 additions & 68 deletions tiled/src/main/kotlin/ktx/tiled/batchTiledMapRenderer.kt
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()
64 changes: 64 additions & 0 deletions tiled/src/main/kotlin/ktx/tiled/tiledMapRenderer.kt
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()
}
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()
}

}

0 comments on commit 984b9c3

Please sign in to comment.