diff --git a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt index da9a38f..da55cc6 100644 --- a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt +++ b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt @@ -13,5 +13,6 @@ data class WarlockStyle(val name: String, val annotation: Pair? val Thought = WarlockStyle("thought") val Watching = WarlockStyle("watching") val Whisper = WarlockStyle("whisper") + val Default = WarlockStyle("") } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt index 448f358..2141c6f 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt @@ -8,6 +8,7 @@ import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.plus import kotlinx.collections.immutable.toPersistentHashSet +import kotlinx.collections.immutable.toPersistentList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -73,15 +74,16 @@ import warlockfe.warlock3.stormfront.protocol.StormfrontModeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontNavEvent import warlockfe.warlock3.stormfront.protocol.StormfrontOutputEvent import warlockfe.warlock3.stormfront.protocol.StormfrontParseErrorEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontProgressBarEvent import warlockfe.warlock3.stormfront.protocol.StormfrontPromptEvent import warlockfe.warlock3.stormfront.protocol.StormfrontPropertyEvent import warlockfe.warlock3.stormfront.protocol.StormfrontProtocolHandler +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontRoundTimeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontSettingsInfoEvent import warlockfe.warlock3.stormfront.protocol.StormfrontStreamEvent import warlockfe.warlock3.stormfront.protocol.StormfrontStreamWindowEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontTimeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontUnhandledTagEvent import warlockfe.warlock3.stormfront.stream.StormfrontWindow @@ -94,6 +96,7 @@ import java.net.Socket import java.net.SocketException import java.net.SocketTimeoutException import java.nio.charset.Charset +import java.util.* import java.util.concurrent.ConcurrentHashMap import kotlin.math.max @@ -240,7 +243,7 @@ class StormfrontClient( private var parseText = true private var currentStream: TextStream = mainStream - private var currentStyle: WarlockStyle? = null + private val styleStack = Stack() // Output style is for echo style! Not received text private var outputStyle: WarlockStyle? = null @@ -337,12 +340,15 @@ class StormfrontClient( is StormfrontOutputEvent -> outputStyle = event.style - is StormfrontStyleEvent -> - currentStyle = event.style + is StormfrontPushStyleEvent -> + styleStack.push(event.style) + + StormfrontPopStyleEvent -> + styleStack.pop() is StormfrontPromptEvent -> { currentTypeAhead.update { max(0, it - 1) } - currentStyle = null + styleStack.clear() currentStream = mainStream if (!isPrompting) { mainStream.appendPartial( @@ -418,7 +424,7 @@ class StormfrontClient( is StormfrontComponentDefinitionEvent -> { // Should not happen on main stream, so don't clear prompt - val styles = currentStyle?.let { persistentListOf(it) } ?: persistentListOf() + val styles = styleStack.toPersistentList() bufferText( text = StyledString( persistentListOf( @@ -535,7 +541,7 @@ class StormfrontClient( private fun bufferText(text: StyledString) { var styledText = text - currentStyle?.let { styledText = styledText.applyStyle(it) } + styleStack.asReversed().forEach { styledText = styledText.applyStyle(it) } outputStyle?.let { styledText = styledText.applyStyle(it) } buffer = buffer?.plus(styledText) ?: styledText } diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt index 2f22e5c..27e0ea4 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt @@ -14,7 +14,8 @@ data class StormfrontClearStreamEvent(val id: String) : StormfrontEvent data class StormfrontModeEvent(val id: String?) : StormfrontEvent data class StormfrontAppEvent(val character: String?, val game: String?) : StormfrontEvent data class StormfrontOutputEvent(val style: WarlockStyle?) : StormfrontEvent -data class StormfrontStyleEvent(val style: WarlockStyle?) : StormfrontEvent +data class StormfrontPushStyleEvent(val style: WarlockStyle) : StormfrontEvent +data object StormfrontPopStyleEvent : StormfrontEvent data class StormfrontPromptEvent(val text: String) : StormfrontEvent data class StormfrontTimeEvent(val time: String) : StormfrontEvent data class StormfrontRoundTimeEvent(val time: String) : StormfrontEvent diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt index 07c882d..d9a73db 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt @@ -1,9 +1,11 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class AHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { @@ -14,10 +16,10 @@ class AHandler : BaseElementListener() { // TODO handle GS stuff here null } - return StormfrontStyleEvent(WarlockStyle.Link(action)) + return StormfrontPushStyleEvent(WarlockStyle.Link(action)) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt index 234b2a6..03ab567 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt @@ -1,16 +1,18 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class BHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(WarlockStyle.Bold) + return StormfrontPushStyleEvent(WarlockStyle.Bold) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt index d9e2bd1..26f1396 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt @@ -1,11 +1,12 @@ package warlockfe.warlock3.stormfront.protocol.elements -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent class PopBoldHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt index cf73c25..7ee370d 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt @@ -1,17 +1,19 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class PresetHandler : BaseElementListener() { - override fun startElement(element: StartElement): StormfrontStyleEvent { + override fun startElement(element: StartElement): StormfrontEvent { val style = element.attributes["id"]?.let { WarlockStyle(it) } - return StormfrontStyleEvent(style) + return StormfrontPushStyleEvent(style ?: WarlockStyle.Default) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt index 1035fd4..8b1126a 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt @@ -4,10 +4,10 @@ import warlockfe.warlock3.core.text.WarlockStyle import warlockfe.warlock3.stormfront.protocol.BaseElementListener import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class PushBoldHandler() : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(WarlockStyle.Bold) + return StormfrontPushStyleEvent(WarlockStyle.Bold) } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt index 040b95d..5278565 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt @@ -3,11 +3,17 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle import warlockfe.warlock3.stormfront.protocol.BaseElementListener import warlockfe.warlock3.stormfront.protocol.StartElement -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class StyleHandler : BaseElementListener() { - override fun startElement(element: StartElement): StormfrontStyleEvent { - val style = element.attributes["id"]?.let { WarlockStyle(it) } - return StormfrontStyleEvent(style) + override fun startElement(element: StartElement): StormfrontPushStyleEvent { + return element.attributes["id"] + ?.let { WarlockStyle(it) } + .let { StormfrontPushStyleEvent(it ?: WarlockStyle.Default) } + } + + override fun endElement(): StormfrontPopStyleEvent { + return StormfrontPopStyleEvent } } \ No newline at end of file