diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/component/LogArea.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/component/LogArea.kt index 57bfca869..04f10bf13 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/component/LogArea.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/component/LogArea.kt @@ -1,9 +1,14 @@ package org.hexworks.zircon.api.component +import korlibs.math.roundDecimalPlaces import org.hexworks.zircon.api.behavior.Clearable import org.hexworks.zircon.api.builder.component.buildHeader import org.hexworks.zircon.api.builder.component.buildListItem import org.hexworks.zircon.api.builder.component.buildParagraph +import org.hexworks.zircon.api.component.builder.base.withPreferredSize +import org.hexworks.zircon.api.graphics.TextWrap +import kotlin.math.ceil +import kotlin.math.roundToInt /** * A [LogArea] provides the possibility to display a stream of messages. The messages are composed @@ -43,11 +48,23 @@ interface LogArea : Component, Clearable { } fun LogArea.addParagraph(text: String) { - addRow(buildParagraph { +text }) + val width = minOf(this.contentSize.width, text.length) + val height = ceil(text.length.toDouble().div(width)).toInt() + + addRow(buildParagraph { + +text + withPreferredSize { + this.width = width + this.height = height + } + textWrap = TextWrap.WORD_WRAP + }) } fun LogArea.addHeader(text: String) { - addRow(buildHeader { +text }) + addRow(buildHeader { + +text + }) } fun LogArea.addListItem(text: String) { diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/data/base/BaseCharacterTile.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/data/base/BaseCharacterTile.kt index 4a7ed9aab..8fa229ac2 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/data/base/BaseCharacterTile.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/data/base/BaseCharacterTile.kt @@ -52,7 +52,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile { } else { characterTile { character = this@BaseCharacterTile.character - this.styleSet = style + styleSet = style } } } @@ -66,7 +66,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile { } else { return characterTile { character = this@BaseCharacterTile.character - this.styleSet = styleSet.withModifiers(modifiers) + styleSet = this@BaseCharacterTile.styleSet.withModifiers(modifiers) } } } @@ -80,7 +80,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile { } else { return characterTile { character = this@BaseCharacterTile.character - this.styleSet = styleSet.withAddedModifiers(modifiers) + styleSet = this@BaseCharacterTile.styleSet.withAddedModifiers(modifiers) } } } @@ -94,7 +94,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile { } else { return characterTile { character = this@BaseCharacterTile.character - this.styleSet = styleSet.withRemovedModifiers(modifiers) + styleSet = this@BaseCharacterTile.styleSet.withRemovedModifiers(modifiers) } } } @@ -105,7 +105,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile { } else { return characterTile { character = this@BaseCharacterTile.character - this.styleSet = styleSet.withNoModifiers() + styleSet = this@BaseCharacterTile.styleSet.withNoModifiers() } } } diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/modifier/SimpleModifiers.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/modifier/SimpleModifiers.kt index ba079ad0f..4af97afd5 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/modifier/SimpleModifiers.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/api/modifier/SimpleModifiers.kt @@ -27,4 +27,6 @@ sealed class SimpleModifiers : TextureModifier { override val cacheKey: String get() = "Modifier.${this::class.simpleName}" + + override fun toString() = cacheKey } diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/application/BaseApplication.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/application/BaseApplication.kt index 3174881f4..1c0227e83 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/application/BaseApplication.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/application/BaseApplication.kt @@ -24,12 +24,11 @@ abstract class BaseApplication( final override val config: AppConfig, final override val tileGrid: TileGrid, final override val eventBus: EventBus, + val renderer: Renderer, final override val eventScope: ZirconScope = ZirconScope(), private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob()) ) : InternalApplication { - abstract val renderer: Renderer - private val logger = LoggerFactory.getLogger(this::class) // 60 FPS by default...can go into app config if necessary private val renderIntervalMs = 1000.div(60) diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/DefaultSize.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/DefaultSize.kt index 5971e6b21..a5eb70c4e 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/DefaultSize.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/DefaultSize.kt @@ -7,7 +7,7 @@ data class DefaultSize( override val height: Int ) : BaseSize() { - override fun toString() = "(${width}X$height)" + override fun toString() = "(w=${width},h=$height)" init { require(width >= 0 && height >= 0) { diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/GridPosition.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/GridPosition.kt index 7aeb66388..d416b2b13 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/GridPosition.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/internal/data/GridPosition.kt @@ -15,7 +15,7 @@ data class GridPosition( override val y: Int ) : BasePosition() { - override fun toString() = "($x,$y)" + override fun toString() = "(x=$x,y=$y)" companion object { diff --git a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/renderer/virtual/VirtualApplication.kt b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/renderer/virtual/VirtualApplication.kt index ba4d28969..4d15f2b1a 100644 --- a/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/renderer/virtual/VirtualApplication.kt +++ b/zircon.core/src/commonMain/kotlin/org/hexworks/zircon/renderer/virtual/VirtualApplication.kt @@ -15,9 +15,8 @@ class VirtualApplication( config: AppConfig, eventBus: EventBus, tileGrid: DefaultTileGrid = DefaultTileGrid(config), -) : BaseApplication(config, tileGrid, eventBus) { +) : BaseApplication(config, tileGrid, eventBus, VirtualRenderer(tileGrid)) { - override val renderer = VirtualRenderer(tileGrid) override fun prepareRenderSurface(): Char { return ' ' } diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/behavior/impl/DefaultMovableTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/behavior/impl/DefaultMovableTest.kt index 37b8ba217..de7c3a443 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/behavior/impl/DefaultMovableTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/behavior/impl/DefaultMovableTest.kt @@ -14,7 +14,7 @@ class DefaultMovableTest { @Before fun setUp() { target = DefaultMovable( - size = TARGET_SIZE, + size = TARGET_SIZE_10x10, position = Position.defaultPosition() ) } @@ -30,7 +30,7 @@ class DefaultMovableTest { assertThat( target.containsPosition( target.position - .withRelative(Position.create(TARGET_SIZE.height, TARGET_SIZE.width)) + .withRelative(Position.create(TARGET_SIZE_10x10.height, TARGET_SIZE_10x10.width)) ) ) .isFalse() @@ -39,12 +39,12 @@ class DefaultMovableTest { @Test fun shouldKnowItsSizeCorrectly() { assertThat(target.size) - .isEqualTo(TARGET_SIZE) + .isEqualTo(TARGET_SIZE_10x10) } @Test fun shouldIntersectWhenIntersectIsCalledWithIntersectingBoundable() { - assertThat(target.intersects(DefaultMovable(TARGET_SIZE))) + assertThat(target.intersects(DefaultMovable(TARGET_SIZE_10x10))) .isTrue() } @@ -53,7 +53,8 @@ class DefaultMovableTest { assertThat( target.intersects( layer { - offset = NON_INTERSECTING_OFFSET + size = TARGET_SIZE_10x10 + offset = NON_INTERSECTING_OFFSET_20x20 } ) ) @@ -65,7 +66,7 @@ class DefaultMovableTest { assertThat( target.intersects( layer { - offset = INTERSECTION_OFFSET + offset = INTERSECTION_OFFSET_1x1 size = Size.one() } ) @@ -86,10 +87,10 @@ class DefaultMovableTest { } companion object { - const val DEFAULT_COLS = 10 - const val DEFAULT_ROWS = 10 - val TARGET_SIZE = Size.create(DEFAULT_COLS, DEFAULT_ROWS) - val INTERSECTION_OFFSET = Position.offset1x1() - val NON_INTERSECTING_OFFSET = Position.create(20, 20) + const val DEFAULT_COLS_10 = 10 + const val DEFAULT_ROWS_10 = 10 + val TARGET_SIZE_10x10 = Size.create(DEFAULT_COLS_10, DEFAULT_ROWS_10) + val INTERSECTION_OFFSET_1x1 = Position.offset1x1() + val NON_INTERSECTING_OFFSET_20x20 = Position.create(20, 20) } } diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultContainerTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultContainerTest.kt index d7a348945..bb1b29190 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultContainerTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultContainerTest.kt @@ -298,7 +298,7 @@ class DefaultContainerTest : CommonComponentTest() { .isEqualTo( "DefaultContainer(id=${ target.id.toString().substring(0, 4) - }, absolutePosition=(2,3), relativePosition=(2,3), size=(4X4), state=DEFAULT, disabled=false)" + }, absolutePosition=(x=2,y=3), relativePosition=(x=2,y=3), size=(w=4,h=4), state=DEFAULT, disabled=false)" ) } diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultLogAreaTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultLogAreaTest.kt index c2193e4ba..6ec8f010a 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultLogAreaTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/component/impl/DefaultLogAreaTest.kt @@ -76,9 +76,9 @@ class DefaultLogAreaTest : ComponentImplementationTest() { fun shouldProperlyAddNewText() { target.addRow(buildParagraph { +TEXT }) - val child = target.children.first() + val paragraph = target.children.first() - assertThat((child.children.first() as Paragraph).text).isEqualTo(TEXT) + assertThat((paragraph as Paragraph).text).isEqualTo(TEXT) } @Test @@ -99,7 +99,7 @@ class DefaultLogAreaTest : ComponentImplementationTest() { target.addInlineRow(listOf(testComponent)) - target.addNewRows(ROW_HISTORY_SIZE) + target.addNewRows(ROW_HISTORY_SIZE_10) target.addRow(buildParagraph { +TEXT }) @@ -130,7 +130,7 @@ class DefaultLogAreaTest : ComponentImplementationTest() { companion object { val POSITION_4_5 = Position.create(4, 5) val SIZE_40x10 = Size.create(40, 10) - const val ROW_HISTORY_SIZE = 15 + const val ROW_HISTORY_SIZE_10 = 10 const val TEXT = "This is my log row" const val ALTERNATE_TEXT = "This is my other log row" diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/renderer/TestRendererTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/renderer/TestRendererTest.kt index 4d21840ee..ee88e1bc1 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/renderer/TestRendererTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/renderer/TestRendererTest.kt @@ -5,9 +5,11 @@ import org.hexworks.zircon.api.ComponentDecorations.box import org.hexworks.zircon.api.application.AppConfig import org.hexworks.zircon.api.builder.component.buildVbox import org.hexworks.zircon.api.builder.component.paragraph +import org.hexworks.zircon.api.builder.data.size import org.hexworks.zircon.api.builder.graphics.tileGraphics import org.hexworks.zircon.api.builder.graphics.withSize import org.hexworks.zircon.api.component.builder.base.decorations +import org.hexworks.zircon.api.component.builder.base.withPreferredContentSize import org.hexworks.zircon.api.component.builder.base.withPreferredSize import org.hexworks.zircon.api.graphics.BoxType import org.hexworks.zircon.convertCharacterTilesToString @@ -43,20 +45,18 @@ class TestRendererTest { @Test fun rendersAsExpected() { val text = "Hello Zircon" - val graphics = tileGraphics { - withSize { + val size = size { width = text.length + 2 height = 4 - } + } + val graphics = tileGraphics { + this.size = size; } val testRenderer = TestRenderer(AppConfig.defaultAppConfig(), graphics).apply { withComponentContainer { addComponent( buildVbox { - withPreferredSize { - width = text.length - height = 4 - } + preferredSize = size decorations { +box(boxType = BoxType.TOP_BOTTOM_DOUBLE) } diff --git a/zircon.examples/src/commonMain/kotlin/main.kt b/zircon.examples/src/commonMain/kotlin/main.kt index 25658d9c6..6b3961078 100644 --- a/zircon.examples/src/commonMain/kotlin/main.kt +++ b/zircon.examples/src/commonMain/kotlin/main.kt @@ -1,16 +1,16 @@ -import korlibs.io.file.std.openAsZip -import org.hexworks.zircon.api.resource.Resource -import org.hexworks.zircon.api.resource.ResourceType -import org.hexworks.zircon.api.resource.loadResource +import org.hexworks.zircon.api.builder.application.appConfig +import org.hexworks.zircon.api.builder.application.withSize +import org.hexworks.zircon.api.createApplication +import org.hexworks.zircon.internal.resource.BuiltInCP437TilesetResource suspend fun main() { val tileSize = 16 val gridCols = 1920 / tileSize val gridRows = 1080 / tileSize - loadResource(Resource.create("rex_files/xptest.xp", ResourceType.PROJECT)).openAsZip() - - println("ok") +// loadResource(Resource.create("rex_files/xptest.xp", ResourceType.PROJECT)).openAsZip() +// +// println("ok") // Applications.createApplication(appConfig { // size = Size.create(gridCols, gridRows) @@ -18,4 +18,13 @@ suspend fun main() { // debugMode = true // }).rex().start() + createApplication(appConfig { + withSize { + width = gridCols + height = gridRows + } + defaultTileset = BuiltInCP437TilesetResource.valueOf("REX_PAINT_${tileSize}X${tileSize}") + debugMode = true + }).components().start() + } \ No newline at end of file