-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
234 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.ewoudje.eggui | ||
|
||
interface EGGPass { | ||
data class Render(private val renderer: EGGRenderer) : EGGPass, EGGRenderer by renderer | ||
data class BuildAssets(private val assetBuilder: EGGAssets): EGGPass, EGGAssets by assetBuilder | ||
} | ||
interface EGGPass | ||
interface EGGInitPass: EGGPass | ||
|
||
data class RenderPass(private val renderer: EGGRenderer) : EGGPass, EGGRenderer by renderer | ||
data class BuildAssetsPass(private val assetBuilder: EGGAssets): EGGInitPass, EGGAssets by assetBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/commonMain/kotlin/com/ewoudje/eggui/components/containers/StatefulContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.ewoudje.eggui.components.containers | ||
|
||
import com.ewoudje.eggui.* | ||
import com.ewoudje.eggui.components.* | ||
import com.ewoudje.eggui.frontend.ChildBuilder | ||
import com.ewoudje.eggui.frontend.EGGBuilderMarker | ||
import com.ewoudje.eggui.frontend.invoke | ||
import kotlin.reflect.KProperty | ||
|
||
class StatefulContainer( | ||
val block: StatefulContainer.() -> Unit, | ||
override val parent: EGGContainerParent, | ||
override val childId: Int, | ||
): EGGSingleContainer { | ||
override val attachment: EGGContainerAttachment = EGGContainerAttachment() | ||
override var child: EGGChildComponent? = null | ||
private set | ||
private var childSize = Size.FILL | ||
private val map = mutableMapOf<KProperty<*>, Any?>() | ||
private val inits = mutableListOf<EGGInitPass>() | ||
private var dirty = false | ||
|
||
override fun <T : EGGChildComponent> setChild(child: EGGChildConstructor<T>): T = | ||
child(this, 0).also { this.child = it } | ||
|
||
override fun updateChildSize(size: Size) { | ||
this.childSize = size | ||
} | ||
|
||
override fun getChildSize(): Size = childSize | ||
|
||
override fun enter(context: EGGContext): EGGContext { | ||
if (dirty) updateChild(context.position, context.size) | ||
return context.new(listOf(context.position), listOf(context.size)) | ||
} | ||
|
||
fun <T> newState(v: T) = StatePreparer(v, this) | ||
|
||
private fun updateChild(pos: Pos, size: CalculatedSize) { | ||
child = null | ||
block() | ||
|
||
child?.let { | ||
for (pass in inits) { | ||
EGGContext.traverse(it, EGGContext(listOf(pos), listOf(size), pass)) | ||
} | ||
} | ||
dirty = false | ||
} | ||
|
||
class StatePreparer<T>(private val defaultVal: T, private val container: StatefulContainer) { | ||
operator fun provideDelegate( | ||
thisRef: Any?, | ||
prop: KProperty<*> | ||
): State<T> { | ||
val value = (container.map[prop] ?: defaultVal) as T | ||
return State(value, container) | ||
} | ||
} | ||
|
||
class State<T>( | ||
private var value: T, | ||
private val container: StatefulContainer | ||
) { | ||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value | ||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
this.value = value | ||
container.map[property] = value | ||
container.dirty = true | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
@EGGBuilderMarker | ||
fun <T: EGGContainer> T.stateful(block: StatefulContainer.() -> Unit): Unit = | ||
ChildBuilder(this) { parent, id -> StatefulContainer(block, parent, id) }.invoke { block() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/commonMain/kotlin/com/ewoudje/eggui/frontend/EGGEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ewoudje.eggui.frontend | ||
|
||
import com.ewoudje.eggui.EGGPass | ||
import com.ewoudje.eggui.components.EGGContainer | ||
import com.ewoudje.eggui.components.EGGElement | ||
|
||
fun EGGContainer.catchEnter(block: (EGGPass) -> Boolean) { | ||
val visit = attachment.onEnter | ||
attachment.onEnter = { block(it) || visit(it) } | ||
} | ||
|
||
fun EGGContainer.catchExit(block: (EGGPass) -> Boolean) { | ||
val visit = attachment.onExit | ||
attachment.onExit = { block(it) || visit(it) } | ||
} | ||
|
||
fun EGGElement.catchVisit(block: (EGGPass) -> Boolean): Unit { | ||
val visit = attachment.onVisit | ||
attachment.onVisit = { block(it) || visit(it) } | ||
} |
Oops, something went wrong.