Skip to content

Commit

Permalink
Refactored iterables in Containers
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Jan 7, 2025
1 parent 364f605 commit 8d49ddf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ open class Area<T : GameComponentView>(
visual: Visual = Visual.EMPTY
) :
GameComponentContainer<T>(
posX = posX, posY = posY, width = width, height = height, visual = visual) {
/** Internal onRemove handler. */
override fun T.onRemove() = Unit
posX = posX, posY = posY, width = width, height = height, visual = visual
),
Iterable<T> {

/** Internal onAdd handler. */
override fun T.onAdd() = Unit
/** Internal onRemove handler. */
override fun T.onRemove() = Unit

/** Internal onAdd handler. */
override fun T.onAdd() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ open class CardStack<T : CardView>(
removePosListeners()
}

override fun iterator(): Iterator<T> = observableComponents.reversed().iterator()

private fun T.addPosListeners() {
posXProperty.setInternalListenerAndInvoke(0.0) { _, _ -> layoutX() }
posYProperty.setInternalListenerAndInvoke(0.0) { _, _ -> layoutY() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ sealed class GameComponentContainer<T : DynamicComponentView>(
visual: Visual
) :
DynamicComponentView(posX = posX, posY = posY, width = width, height = height, visual = visual),
Iterable<T>,
LayeredContainer<T> {
/**
* An [ObservableList] to store the [GameComponentView]s that are contained in this
Expand Down Expand Up @@ -292,7 +291,7 @@ sealed class GameComponentContainer<T : DynamicComponentView>(
*
* @return Iterator over the elements of this [GameComponentContainer].
*/
override fun iterator(): Iterator<T> = observableComponents.iterator()
open fun iterator(): Iterator<T> = observableComponents.iterator()

/**
* Puts the [component] to the front inside the [LayeredContainer] and Changes its [zIndex]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ open class LinearLayout<T : GameComponentView>(
) :
GameComponentContainer<T>(
posX = posX, posY = posY, width = width, height = height, visual = visual
) {
),
Iterable<T> {

/**
* [Property] for the spacing of [GameComponentView]s in this [LinearLayout].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ open class Satchel<T : GameComponentView>(
) :
GameComponentContainer<T>(
posX = posX, posY = posY, width = width, height = height, visual = visual
) {
),
Iterable<T> {

private val initialStates: HashMap<ComponentView, InitialState> = HashMap()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ internal object Application : BoardGameApplication(aspectRatio = AspectRatio.of(

init {
loadFont("Rubik.ttf")
// showGameScene(cardLayoutScene)
showGameScene(cardLayoutScene)
// showGameScene(hexGrid)
// showGameScene(animation)
// showGameScene(grid)
// showGameScene(dragDropScene)
showMenuScene(uiScene)
// showMenuScene(uiScene)
// showGameScene(visualScene)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools.aqua.bgw.main.view

import tools.aqua.bgw.components.container.CardStack
import tools.aqua.bgw.components.container.LinearLayout
import tools.aqua.bgw.components.gamecomponentviews.CardView
import tools.aqua.bgw.components.uicomponents.Button
Expand All @@ -13,6 +14,30 @@ import kotlin.random.Random

internal class CardLayoutScene : BoardGameScene() {

val cardStack = CardStack<CardView>(
width = 300,
height = 200,
posX = 900,
posY = 100,
alignment = Alignment.CENTER,
visual = ColorVisual.LIGHT_GRAY
).apply {
repeat(5) { i ->
val card = CardView(
posX = 0,
posY = 0,
width = i,
height = 200,
front = ColorVisual(Color(Random.nextInt(255), Random.nextInt(255), Random.nextInt(255)))
)
println(card.width)
this.add(card)
}

println(this.iterator().asSequence().map { it.width }.toList())
println(this.peek().width)
}

val button = Button(
width = 200,
height = 200,
Expand Down Expand Up @@ -68,7 +93,7 @@ internal class CardLayoutScene : BoardGameScene() {
}

init {
addComponents(layout, button)
addComponents(layout, button, cardStack)
repeat(5) {
val card = CardView(
posX = 0,
Expand Down

0 comments on commit 8d49ddf

Please sign in to comment.