Skip to content

Commit

Permalink
Refactor MapManager to render to a specific Window
Browse files Browse the repository at this point in the history
Refs #73

Rather than defaulting to whatever window is current, it selects
the "current" window on init (eg: createEmpty) and uses that forever
unless another window is specified.

If provided a non-primary Window, that window will be cleared before
the map is rendered into it.

Furthermore, we simplify the resize mechanics to match *the specific
window* we're rendering into.
  • Loading branch information
dhleong committed Feb 20, 2019
1 parent 1cfafd3 commit a00b0ca
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package net.dhleong.judo

import assertk.Assert
import assertk.assertions.isEqualTo
import assertk.assertions.isTrue
import assertk.assertions.support.expected
import assertk.assertions.support.show
import net.dhleong.judo.render.FlavorableCharSequence
import net.dhleong.judo.render.IJudoBuffer

/**
Expand All @@ -25,6 +27,12 @@ fun Assert<IJudoBuffer>.hasLinesSomewhere(vararg expectedStrings: String) {
expected("buffer of ${show(actualStrings)} to contain the sequence: ${show(expectedStrings)}")
}

fun Assert<IJudoBuffer>.doesNotHaveLine(line: FlavorableCharSequence) {
for (i in 0..actual.lastIndex) {
assert(actual[i] != line, "line $i doesn't match `$line`").isTrue()
}
}

fun Assert<IJudoBuffer>.hasSize(size: Int) {
if (actual.size == size) return
expected("size = ${show(size)} but was ${show(actual.size)}")
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/kotlin/net/dhleong/judo/JudoCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ class JudoCore(
updateStatusLine(currentMode)
updateInputLine()

tabpage.currentWindow.let {
mapper.resize(width = it.width)
}
mapper.onResize()
}

private fun onBlockingEcho() {
Expand Down
19 changes: 15 additions & 4 deletions core/src/main/kotlin/net/dhleong/judo/mapping/MapManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.dhleong.judo.MAP_AUTOROOM
import net.dhleong.judo.StateMap
import net.dhleong.judo.event.EVENT_GMCP_ENABLED
import net.dhleong.judo.event.EVENT_MSDP_ENABLED
import net.dhleong.judo.inTransaction
import net.dhleong.judo.render.IJudoWindow
import java.io.File

Expand All @@ -20,6 +21,7 @@ class MapManager(
) : IMapManager {

override var current: IJudoMap? = null
override var window: IJudoWindow? = null

private var currentFormat: String? = null
private var currentFile: File? = null
Expand Down Expand Up @@ -64,14 +66,19 @@ class MapManager(
init()
}

override fun render(intoWindow: IJudoWindow?) {
override fun render(intoWindow: IJudoWindow) {
current?.let {
mapRenderer.renderMap(it, intoWindow)
mapRenderer.resize(intoWindow.width, intoWindow.visibleHeight)

judo.renderer.inTransaction {
mapRenderer.renderMap(it, intoWindow)
}
}
}

override fun resize(width: Int, height: Int) {
mapRenderer.resize(width, height)
override fun onResize() {
val w = window ?: return
mapRenderer.resize(w.width, w.visibleHeight)
}

override fun save() {
Expand Down Expand Up @@ -126,6 +133,10 @@ class MapManager(
}

private fun init() {

// use the current window by default
window = judo.renderer.currentTabpage.currentWindow

if (!settings[MAP_AUTOMAGIC]) return

val mapper = AutomagicMapper(judo, this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package net.dhleong.judo.mapping.renderer

import net.dhleong.judo.JudoRenderer
import net.dhleong.judo.inTransaction
import net.dhleong.judo.mapping.DEFAULT_MIN_MAP_HEIGHT
import net.dhleong.judo.mapping.DEFAULT_MIN_MAP_WIDTH
import net.dhleong.judo.mapping.IJudoMap
import net.dhleong.judo.mapping.MapGrid
import net.dhleong.judo.mapping.MapRenderer
import net.dhleong.judo.render.IJudoAppendable
import net.dhleong.judo.render.IJudoWindow
import net.dhleong.judo.render.PrimaryJudoWindow

/**
* Base class for [MapRenderer] implementations that
Expand All @@ -17,7 +16,6 @@ import net.dhleong.judo.render.IJudoWindow
* @author dhleong
*/
abstract class BufferMapRenderer(
protected val renderer: JudoRenderer,
protected var mapGrid: MapGrid
) : MapRenderer {

Expand All @@ -30,19 +28,21 @@ abstract class BufferMapRenderer(
*/
abstract fun appendGridInto(map: IJudoMap, grid: MapGrid, window: IJudoAppendable)

override fun renderMap(map: IJudoMap, window: IJudoWindow?) {
renderer.inTransaction {
map.currentRoom?.let { room ->
mapGrid.buildAround(map, room)

// if provided a window, use it; otherwise, try to use
// the current window
appendGridInto(
map,
mapGrid,
(window ?: renderer.currentTabpage.currentWindow)
)
}
override fun renderMap(map: IJudoMap, window: IJudoWindow) {
if (window !is PrimaryJudoWindow) {
window.currentBuffer.clear()
}

map.currentRoom?.let { room ->
mapGrid.buildAround(map, room)

// if provided a window, use it; otherwise, try to use
// the current window
appendGridInto(
map,
mapGrid,
window
)
}
}

Expand All @@ -57,6 +57,12 @@ abstract class BufferMapRenderer(
if (height == -1) mapGrid.height
else height
)

if (newWidth == mapGrid.width && newHeight == mapGrid.height) {
// nop
return
}

mapGrid = MapGrid(newWidth / charsPerX, newHeight / charsPerY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DelegateMapRenderer(
delegate.resize(width, height)
}

override fun renderMap(map: IJudoMap, window: IJudoWindow?) {
override fun renderMap(map: IJudoMap, window: IJudoWindow) {
delegate.renderMap(map, window)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.dhleong.judo.mapping.renderer

import net.dhleong.judo.JudoRenderer
import net.dhleong.judo.mapping.DEFAULT_MIN_MAP_HEIGHT
import net.dhleong.judo.mapping.DEFAULT_MIN_MAP_WIDTH
import net.dhleong.judo.mapping.IJudoMap
Expand All @@ -17,9 +16,8 @@ import net.dhleong.judo.render.SimpleFlavor
* @author dhleong
*/
class SimpleBufferMapRenderer(
renderer: JudoRenderer,
mapGrid: MapGrid = MapGrid(DEFAULT_MIN_MAP_WIDTH, DEFAULT_MIN_MAP_HEIGHT)
) : BufferMapRenderer(renderer, mapGrid) {
) : BufferMapRenderer(mapGrid) {

override val name: String = "simple"
override val charsPerX = 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.dhleong.judo.mapping

import net.dhleong.judo.TestableJudoCore
import net.dhleong.judo.TestableJudoRenderer
import net.dhleong.judo.assertThat
import org.junit.Before
import org.junit.Test
Expand All @@ -16,7 +17,7 @@ class DirectionalMapStrategyTest {
lateinit var map: JudoMap

@Before fun setUp() {
judo = TestableJudoCore()
judo = TestableJudoCore(TestableJudoRenderer())
mapper = AutomagicMapper(judo, judo.mapper)
strategy = DirectionalMapStrategy(mapper)

Expand Down
80 changes: 80 additions & 0 deletions core/src/test/kotlin/net/dhleong/judo/mapping/MapManagerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package net.dhleong.judo.mapping

import assertk.all
import assertk.assert
import net.dhleong.judo.IJudoCore
import net.dhleong.judo.StateMap
import net.dhleong.judo.TestableJudoCore
import net.dhleong.judo.TestableJudoRenderer
import net.dhleong.judo.bufferOf
import net.dhleong.judo.doesNotHaveLine
import net.dhleong.judo.hasSize
import net.dhleong.judo.mapping.renderer.SimpleBufferMapRenderer
import net.dhleong.judo.render.toFlavorable
import org.junit.Before
import org.junit.Test

/**
* @author dhleong
*/
class MapManagerTest {

private lateinit var judo: IJudoCore
private lateinit var mapper: IMapManager
private lateinit var map: IJudoMap

@Before fun setUp() {
judo = TestableJudoCore(TestableJudoRenderer())
mapper = MapManager(judo, StateMap(), SimpleBufferMapRenderer())

mapper.createEmpty()
map = mapper.current as JudoMap
map.inRoom = 0
map.add(JudoRoom(0, "0"))
}

@Test fun `Render to primary window by default`() {
val buffer = judo.renderer.currentTabpage.currentWindow.currentBuffer
assert(buffer).hasSize(0)

mapper.render()
assert(buffer).hasSize(judo.renderer.windowHeight)
}

@Test fun `Don't clear the primary window`() {
val buffer = judo.renderer.currentTabpage.currentWindow.currentBuffer
buffer.appendLine("Test".toFlavorable())
assert(buffer).hasSize(1)

mapper.render()
assert(buffer).doesNotHaveLine("Test".toFlavorable())
}

@Test fun `Render to and control provided window`() {
val primaryWindow = judo.renderer.currentTabpage.currentWindow
val primary = primaryWindow.currentBuffer
assert(primary).hasSize(0)

val buffer = bufferOf("""
You can't take
The skies from me
""".trimIndent())
val window = judo.tabpage.hsplit(10, buffer)
assert(buffer).hasSize(2)

// swap back to primary window
judo.renderer.currentTabpage.currentWindow = primaryWindow

mapper.window = window
mapper.render()
assert(buffer).all {
// non-primary window's buffer gets cleared
hasSize(9)
doesNotHaveLine("You can't take".toFlavorable())
}

// still nothing in the primary buffer
assert(primary).hasSize(0)
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.dhleong.judo.mapping.renderer

import assertk.assert
import net.dhleong.judo.TestableJudoRenderer
import net.dhleong.judo.hasLinesSomewhere
import net.dhleong.judo.mapping.JudoMap
import net.dhleong.judo.mapping.JudoRoom
Expand All @@ -16,20 +15,18 @@ import org.junit.Test
* @author dhleong
*/
class SimpleBufferMapRendererTest {
val ids = IdManager()
val buffer = JudoBuffer(ids)
private val ids = IdManager()
private val buffer = JudoBuffer(ids)

lateinit var renderer: SimpleBufferMapRenderer
lateinit var roadColor: Flavor
lateinit var stairColor: Flavor
lateinit var wallColor: Flavor
lateinit var hereColor: Flavor
lateinit var noColor: Flavor
private lateinit var renderer: SimpleBufferMapRenderer
private lateinit var roadColor: Flavor
private lateinit var stairColor: Flavor
private lateinit var wallColor: Flavor
private lateinit var hereColor: Flavor
private lateinit var noColor: Flavor

@Before fun setUp() {
renderer = SimpleBufferMapRenderer(
TestableJudoRenderer()
)
renderer = SimpleBufferMapRenderer()
buffer.clear()

roadColor = renderer.roadColor
Expand Down
2 changes: 1 addition & 1 deletion judo/src/main/kotlin/net/dhleong/judo/JudoMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun main(args: Array<String>) {
renderer.validate()

val mapRenderer: MapRenderer = DelegateMapRenderer(
SimpleBufferMapRenderer(renderer)
SimpleBufferMapRenderer()
)

// clean up after ourselves
Expand Down
10 changes: 7 additions & 3 deletions model/src/main/kotlin/net/dhleong/judo/mapping/IMapManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface IMapManagerPublic {
}

val current: IJudoMap?
var window: IJudoWindow?

/**
* Create a new, empty Map with at least the given capacity
Expand Down Expand Up @@ -54,10 +55,13 @@ interface IMapManagerPublic {
/**
* Render the current map
*/
fun render() = render(null)
fun render(intoWindow: IJudoWindow?)
fun render() = render(window ?: throw IllegalStateException("No window to render into"))
fun render(intoWindow: IJudoWindow)

fun resize(width: Int = -1, height: Int = -1)
/**
* Called when the attached window may have changed in size
*/
fun onResize()

/**
* Write the current map to disk, overwriting the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ import net.dhleong.judo.render.IJudoWindow
*/
interface MapRenderer {
fun resize(width: Int = -1, height: Int = -1)
fun renderMap(map: IJudoMap, window: IJudoWindow? = null)

/**
* Will be called in a [net.dhleong.judo.JudoRenderer] transaction for you
*/
fun renderMap(map: IJudoMap, window: IJudoWindow)
}

0 comments on commit a00b0ca

Please sign in to comment.