Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-arold committed Jan 16, 2024
1 parent 0825ea6 commit 1ddb07a
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class BaseCharacterTile : BaseTile(), CharacterTile {
} else {
characterTile {
character = this@BaseCharacterTile.character
this.styleSet = style
styleSet = style
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ sealed class SimpleModifiers : TextureModifier {

override val cacheKey: String
get() = "Modifier.${this::class.simpleName}"

override fun toString() = cacheKey
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ abstract class BaseApplication<R : Any, A : Application, V>(
final override val config: AppConfig,
final override val tileGrid: TileGrid,
final override val eventBus: EventBus,
val renderer: Renderer<R, A, V>,
final override val eventScope: ZirconScope = ZirconScope(),
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
) : InternalApplication {

abstract val renderer: Renderer<R, A, V>

private val logger = LoggerFactory.getLogger(this::class)
// 60 FPS by default...can go into app config if necessary
private val renderIntervalMs = 1000.div(60)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class VirtualApplication(
config: AppConfig,
eventBus: EventBus,
tileGrid: DefaultTileGrid = DefaultTileGrid(config),
) : BaseApplication<Char, VirtualApplication, VirtualView>(config, tileGrid, eventBus) {
) : BaseApplication<Char, VirtualApplication, VirtualView>(config, tileGrid, eventBus, VirtualRenderer(tileGrid)) {

override val renderer = VirtualRenderer(tileGrid)
override fun prepareRenderSurface(): Char {
return ' '
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DefaultMovableTest {
@Before
fun setUp() {
target = DefaultMovable(
size = TARGET_SIZE,
size = TARGET_SIZE_10x10,
position = Position.defaultPosition()
)
}
Expand All @@ -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()
Expand All @@ -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()
}

Expand All @@ -53,7 +53,8 @@ class DefaultMovableTest {
assertThat(
target.intersects(
layer {
offset = NON_INTERSECTING_OFFSET
size = TARGET_SIZE_10x10
offset = NON_INTERSECTING_OFFSET_20x20
}
)
)
Expand All @@ -65,7 +66,7 @@ class DefaultMovableTest {
assertThat(
target.intersects(
layer {
offset = INTERSECTION_OFFSET
offset = INTERSECTION_OFFSET_1x1
size = Size.one()
}
)
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class DefaultContainerTest : CommonComponentTest<DefaultContainer>() {
.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)"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class DefaultLogAreaTest : ComponentImplementationTest<DefaultLogArea>() {
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
Expand All @@ -99,7 +99,7 @@ class DefaultLogAreaTest : ComponentImplementationTest<DefaultLogArea>() {

target.addInlineRow(listOf(testComponent))

target.addNewRows(ROW_HISTORY_SIZE)
target.addNewRows(ROW_HISTORY_SIZE_10)

target.addRow(buildParagraph { +TEXT })

Expand Down Expand Up @@ -130,7 +130,7 @@ class DefaultLogAreaTest : ComponentImplementationTest<DefaultLogArea>() {
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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 16 additions & 7 deletions zircon.examples/src/commonMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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)
// defaultTileset = BuiltInCP437TilesetResource.valueOf("REX_PAINT_${tileSize}X${tileSize}")
// debugMode = true
// }).rex().start()

createApplication(appConfig {
withSize {
width = gridCols
height = gridRows
}
defaultTileset = BuiltInCP437TilesetResource.valueOf("REX_PAINT_${tileSize}X${tileSize}")
debugMode = true
}).components().start()

}

0 comments on commit 1ddb07a

Please sign in to comment.