Skip to content

Commit

Permalink
Support binding the Mapper to a specific window (#82)
Browse files Browse the repository at this point in the history
* Refactor MapManager to render to a specific Window

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.

* Expose `judo.mapper.window` to scripting

Closes #73

This commit also renames the internal IJudoWindow.onSubmit to
`onSubmitFn`.  It's a little annoying, but it allows a simple
implementation of onSubmit for `IScriptWindow` that doesn't conflict
with `IJudoWindow`, so implementations of `IScriptWindow` can also
implement `IJudoWindow` for greater interop.
  • Loading branch information
dhleong committed Feb 20, 2019
1 parent 1cfafd3 commit ea6524f
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 63 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class TestableJudoCore(
null
}

window?.onSubmit?.let {
window?.onSubmitFn?.let {
it(text)
} ?: send(text, fromMap)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ fun createWindowMock(
else -> height
}

override var onSubmit: ((String) -> Unit)? = null
override var onSubmitFn: ((String) -> Unit)? = null

override var currentBuffer: IJudoBuffer
get() = buffer
Expand Down
6 changes: 2 additions & 4 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 Expand Up @@ -510,7 +508,7 @@ class JudoCore(
}

override fun submit(text: String, fromMap: Boolean) {
val onSubmit = renderer.currentTabpage.currentWindow.onSubmit
val onSubmit = renderer.currentTabpage.currentWindow.onSubmitFn
?: return send(text, fromMap)

onSubmit(text)
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
Expand Up @@ -25,7 +25,7 @@ abstract class BaseJudoWindow(

protected val search = BufferSearcher()

override var onSubmit: ((String) -> Unit)? = null
override var onSubmitFn: ((String) -> Unit)? = null

override fun append(text: FlavorableCharSequence) = currentBuffer.append(text)
override fun appendLine(line: FlavorableCharSequence) = currentBuffer.appendLine(line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class PrimaryJudoWindow(
override val statusCursor: Int
get() = promptWindow.statusCursor

override var onSubmit: ((String) -> Unit)?
override var onSubmitFn: ((String) -> Unit)?
get() = null
set(_) {
throw IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import net.dhleong.judo.alias.compileSimplePatternSpec
import net.dhleong.judo.event.IEventManager
import net.dhleong.judo.logging.ILogManager
import net.dhleong.judo.mapping.IJudoMap
import net.dhleong.judo.mapping.IMapManager
import net.dhleong.judo.mapping.IMapManagerPublic
import net.dhleong.judo.prompt.IPromptManager
import net.dhleong.judo.render.IJudoBuffer
Expand Down Expand Up @@ -109,7 +108,14 @@ abstract class Jsr223ScriptingEngine(
is IAliasManager -> object : IAliasManager by fromJava {}
is IEventManager -> object : IEventManager by fromJava {}
is ILogManager -> object : ILogManager by fromJava {}
is IMapManagerPublic -> object : IMapManagerPublic by fromJava {}
is IMapManagerPublic -> object : IMapManagerPublic by fromJava {
override var window: IJudoWindow?
get() = fromJava.window
set(value) {
// unpack for simplicity
fromJava.window = (value as? Jsr223Window)?.window ?: value
}
}
is IPromptManager -> object : IPromptManager by fromJava {}
is ITriggerManager -> object : ITriggerManager by fromJava {}
is JudoRendererInfo -> object : JudoRendererInfo by fromJava {}
Expand Down Expand Up @@ -137,8 +143,8 @@ class Jsr223JudoCore(
private val engine: Jsr223ScriptingEngine,
private val judo: IJudoCore
) : IScriptJudo, ICurrentJudoObjects {
override val mapper: IMapManager
get() = judo.mapper
override val mapper: IMapManagerPublic
get() = engine.toScript(judo.mapper) as IMapManagerPublic

override val current: ICurrentJudoObjects = this

Expand All @@ -165,8 +171,8 @@ class Jsr223JudoCore(
class Jsr223Window(
private val engine: Jsr223ScriptingEngine,
private val tabpage: IJudoTabpage,
private val window: IJudoWindow
) : IScriptWindow {
internal val window: IJudoWindow
) : IScriptWindow, IJudoWindow by window {

override val id: Int = window.id
override val width: Int = window.width
Expand All @@ -176,9 +182,9 @@ class Jsr223Window(

@Suppress("UNCHECKED_CAST")
override var onSubmit: Any?
get() = window.onSubmit?.let { engine.toScript(it) }
get() = window.onSubmitFn?.let { engine.toScript(it) }
set(value) {
window.onSubmit = value?.let { engine.callableToFunction1(it) as (String) -> Unit }
window.onSubmitFn = value?.let { engine.callableToFunction1(it) as (String) -> Unit }
}

override fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ internal fun createPyWindow(
"width" -> Py.java2py(window.width)
"id" -> Py.java2py(window.id)

"onSubmit" -> Py.java2py(window.onSubmit)
"onSubmit" -> Py.java2py(window.onSubmitFn)

// not used oft enough to cache
"close" -> asPyFn<Any, Unit>("close") {
Expand All @@ -573,7 +573,7 @@ internal fun createPyWindow(
@Suppress("UNCHECKED_CAST")
when (name) {
"onSubmit" -> {
window.onSubmit = when (value) {
window.onSubmitFn = when (value) {
null -> null
is PyNone -> null
else -> engine.callableToFunction1(value) as (String) -> Unit
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
Loading

0 comments on commit ea6524f

Please sign in to comment.