diff --git a/build.gradle.kts b/build.gradle.kts index c320b1f..dfc934a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,7 @@ plugins { } group = "com.github.ekenstein" -version = "0.3.0" +version = "0.3.1" repositories { mavenCentral() diff --git a/dist/lib/sgf2gif.jar b/dist/lib/sgf2gif.jar index 67fd89c..c21bb79 100644 Binary files a/dist/lib/sgf2gif.jar and b/dist/lib/sgf2gif.jar differ diff --git a/src/main/kotlin/com/github/ekenstein/sgf2gif/AnimatedGifWriter.kt b/src/main/kotlin/com/github/ekenstein/sgf2gif/AnimatedGifWriter.kt index 89d6531..935032b 100644 --- a/src/main/kotlin/com/github/ekenstein/sgf2gif/AnimatedGifWriter.kt +++ b/src/main/kotlin/com/github/ekenstein/sgf2gif/AnimatedGifWriter.kt @@ -13,14 +13,14 @@ import kotlin.time.Duration import kotlin.time.DurationUnit interface GifSequenceWriter { - fun add(image: RenderedImage) + fun addFrame(image: RenderedImage) } private class GifSequenceWriterImpl( private val writer: ImageWriter, private val metaData: IIOMetadata, ) : GifSequenceWriter { - override fun add(image: RenderedImage) { + override fun addFrame(image: RenderedImage) { writer.writeToSequence( IIOImage(image, null, metaData), writer.defaultWriteParam diff --git a/src/main/kotlin/com/github/ekenstein/sgf2gif/BoardRenderer.kt b/src/main/kotlin/com/github/ekenstein/sgf2gif/BoardRenderer.kt index bb7ea6b..8325346 100644 --- a/src/main/kotlin/com/github/ekenstein/sgf2gif/BoardRenderer.kt +++ b/src/main/kotlin/com/github/ekenstein/sgf2gif/BoardRenderer.kt @@ -13,11 +13,7 @@ import java.awt.image.BufferedImage import javax.imageio.stream.ImageOutputStream import kotlin.time.Duration -data class Stone( - val point: SgfPoint, - val color: SgfColor, - val moveNumber: Int? -) +data class Stone(val point: SgfPoint, val color: SgfColor) interface BoardRenderer { fun drawEmptyBoard(g: Graphics2D) @@ -32,15 +28,18 @@ fun BoardRenderer.render( height: Int, delay: Duration, loop: Boolean, - removeCapturedStones: Boolean, progress: (Double) -> Unit ) { writeGif(outputStream, delay, loop) { - add( - image(width, height) { g -> - drawEmptyBoard(g) + val board = editor.goToRootNode().extractBoard() + val boardImage = image(width, height) { g -> + drawEmptyBoard(g) + board.stones.forEach { (point, color) -> + drawStone(g, Stone(point, color)) } - ) + } + + addFrame(boardImage) val totalNumberOfMoves = editor.getMoveNumber().toDouble() @@ -56,20 +55,17 @@ fun BoardRenderer.render( val image = image(width, height) { g -> drawStone(g, stone) - if (removeCapturedStones) { - capturedStones.forEach { (point, _) -> - clearPoint(g, point.x, point.y) - } + capturedStones.forEach { (point, _) -> + clearPoint(g, point.x, point.y) } } - add(image) + addFrame(image) addStones(move + 1, updatedBoard, stones.drop(1)) } } - val stones = editor.getStones().reversed() - val board = editor.goToRootNode().extractBoard() + val stones = editor.getMoves().reversed() addStones(0, board, stones) } } diff --git a/src/main/kotlin/com/github/ekenstein/sgf2gif/GoUtils.kt b/src/main/kotlin/com/github/ekenstein/sgf2gif/GoUtils.kt index 81576c6..a65c36c 100644 --- a/src/main/kotlin/com/github/ekenstein/sgf2gif/GoUtils.kt +++ b/src/main/kotlin/com/github/ekenstein/sgf2gif/GoUtils.kt @@ -5,7 +5,6 @@ import com.github.ekenstein.sgf.SgfColor import com.github.ekenstein.sgf.SgfPoint import com.github.ekenstein.sgf.SgfProperty import com.github.ekenstein.sgf.editor.SgfEditor -import com.github.ekenstein.sgf.editor.getMoveNumber import com.github.ekenstein.sgf.editor.goToPreviousNode import com.github.ekenstein.sgf.editor.goToRootNode import com.github.ekenstein.sgf.utils.MoveResult @@ -17,21 +16,19 @@ fun SgfEditor.boardSize() = goToRootNode().currentNode.property<SgfProperty.Root it.width to it.height } ?: (19 to 19) -fun SgfEditor.getStones(): List<Stone> { +fun SgfEditor.getMoves(): List<Stone> { tailrec fun SgfEditor.next(result: List<Stone>): List<Stone> { - val stones = currentNode.properties.flatMap { property -> + val stones = currentNode.properties.mapNotNull { property -> when (property) { - is SgfProperty.Setup.AB -> property.points.map { Stone(it, SgfColor.Black, null) } - is SgfProperty.Setup.AW -> property.points.map { Stone(it, SgfColor.White, null) } is SgfProperty.Move.B -> when (val move = property.move) { - Move.Pass -> emptyList() - is Move.Stone -> listOf(Stone(move.point, SgfColor.Black, getMoveNumber())) + Move.Pass -> null + is Move.Stone -> Stone(move.point, SgfColor.Black) } is SgfProperty.Move.W -> when (val move = property.move) { - Move.Pass -> emptyList() - is Move.Stone -> listOf(Stone(move.point, SgfColor.White, getMoveNumber())) + Move.Pass -> null + is Move.Stone -> Stone(move.point, SgfColor.White) } - else -> emptyList() + else -> null } } diff --git a/src/main/kotlin/com/github/ekenstein/sgf2gif/Main.kt b/src/main/kotlin/com/github/ekenstein/sgf2gif/Main.kt index 199277d..42a53ab 100644 --- a/src/main/kotlin/com/github/ekenstein/sgf2gif/Main.kt +++ b/src/main/kotlin/com/github/ekenstein/sgf2gif/Main.kt @@ -33,19 +33,10 @@ fun main(args: Array<String>) { Theme.NES -> Nes(options.width, options.height, boardWidth, boardHeight) } - renderer.render( - outputStream, - editor, - options.width, - options.height, - options.delay.seconds, - options.loop, - true - ) { + renderer.render(outputStream, editor, options.width, options.height, options.delay.seconds, options.loop) { print("\r${percentageFormat.format(it)}") } - println() } - println("Exported the SGF to ${options.output}") + println("\nExported the SGF to ${options.output}") }