Skip to content

Commit

Permalink
Fixed multiple bug found during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Feb 27, 2025
1 parent e7604b3 commit 4c30c4e
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import tools.aqua.bgw.core.Alignment
import tools.aqua.bgw.core.BoardGameApplication
import tools.aqua.bgw.core.Color
import tools.aqua.bgw.core.MenuScene
import tools.aqua.bgw.style.BorderRadius
import tools.aqua.bgw.util.Font
import tools.aqua.bgw.visual.ColorVisual

Expand All @@ -41,7 +40,7 @@ class UIComponentExample : BoardGameApplication("UIComponent Example", width = 8
text = "I am a Label.",
alignment = Alignment.CENTER,
isWrapText = true,
visual = ColorVisual.GREEN.apply { style.borderRadius = BorderRadius.XXL })
visual = ColorVisual.GREEN)

init {
menuScene.addComponents(outputLabel)
Expand Down
18 changes: 11 additions & 7 deletions bgw-gui/src/commonMain/kotlin/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ private val module = SerializersModule {
subclass(DialogData::class)
subclass(FileDialogData::class)
// ANIMATIONS
subclass(DelayAnimationData::class)
subclass(DiceAnimationData::class)
subclass(FadeAnimationData::class)
subclass(FlipAnimationData::class)
subclass(MovementAnimationData::class)
subclass(ParallelAnimationData::class)
subclass(RandomizeAnimationData::class)
subclass(RotationAnimationData::class)
subclass(ScaleAnimationData::class)
subclass(FlipAnimationData::class)
subclass(SequentialAnimationData::class)
subclass(ParallelAnimationData::class)
subclass(RandomizeAnimationData::class)
subclass(DiceAnimationData::class)
subclass(DelayAnimationData::class)
}
polymorphic(LayoutViewData::class) {
subclass(PaneData::class)
Expand Down Expand Up @@ -180,12 +180,16 @@ private val module = SerializersModule {
}
// ANIMATIONS
polymorphic(AnimationData::class) {
subclass(DelayAnimationData::class)
subclass(DiceAnimationData::class)
subclass(FadeAnimationData::class)
subclass(FlipAnimationData::class)
subclass(MovementAnimationData::class)
subclass(ParallelAnimationData::class)
subclass(RandomizeAnimationData::class)
subclass(RotationAnimationData::class)
subclass(ScaleAnimationData::class)
subclass(FlipAnimationData::class)
subclass(DelayAnimationData::class)
subclass(SequentialAnimationData::class)
}
}

Expand Down
10 changes: 10 additions & 0 deletions bgw-gui/src/jsMain/kotlin/tools/aqua/bgw/Animator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ internal class Animator {
val animations = animation.animations

animations.forEach {
if (it is DelayAnimationData ||
it is SequentialAnimationData ||
it is ParallelAnimationData ||
it is SteppedComponentAnimationData)
return@forEach
val component = it as? ComponentAnimationData ?: return
val componentId = component.componentView?.id.toString()

Expand All @@ -132,6 +137,11 @@ internal class Animator {
val animations = animation.animations

animations.forEach {
if (it is DelayAnimationData ||
it is SequentialAnimationData ||
it is ParallelAnimationData ||
it is SteppedComponentAnimationData)
return@forEach
val component = it as? ComponentAnimationData ?: return
val componentId = component.componentView?.id.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ internal val PasswordField =

val elementRef = useRef<Element>(null)

val (inputValue, setInputValue) = useState(props.data.text)

bgwPasswordField {
id = props.data.id
className = ClassName("passwordField")
Expand All @@ -71,7 +73,7 @@ internal val PasswordField =
placeholder = props.data.prompt
type = InputType.password
defaultValue = props.data.text
value = props.data.text
value = inputValue
spellCheck = false
css {
fontBuilder(props.data)
Expand All @@ -84,6 +86,7 @@ internal val PasswordField =
}
onChange = {
val value = it.target.value
setInputValue(value)
JCEFEventDispatcher.dispatchEvent(
TextInputChangedEventData(value).apply { id = props.data.id })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ internal val TextArea =

val elementRef = useRef<Element>(null)

val (inputValue, setInputValue) = useState(props.data.text)

bgwTextArea {
id = props.data.id
className = ClassName("textArea")
Expand All @@ -68,7 +70,7 @@ internal val TextArea =
textarea {
placeholder = props.data.prompt
defaultValue = props.data.text
value = props.data.text
value = inputValue
spellCheck = false
css {
fontBuilder(props.data)
Expand All @@ -82,6 +84,7 @@ internal val TextArea =
}
onChange = {
val value = it.target.value
setInputValue(value)
JCEFEventDispatcher.dispatchEvent(
TextInputChangedEventData(value).apply { id = props.data.id })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ internal val TextField =

val elementRef = useRef<Element>(null)

val (inputValue, setInputValue) = useState(props.data.text)

bgwTextField {
id = props.data.id
className = ClassName("textField")
Expand All @@ -68,7 +70,7 @@ internal val TextField =
input {
placeholder = props.data.prompt
defaultValue = props.data.text
value = props.data.text
value = inputValue
spellCheck = false
css {
inputBuilder(props.data)
Expand All @@ -81,6 +83,7 @@ internal val TextField =
}
onChange = {
val value = it.target.value
setInputValue(value)
JCEFEventDispatcher.dispatchEvent(
TextInputChangedEventData(value).apply { id = props.data.id })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ data class SequentialAnimation(
* Creates a new [SequentialAnimation] Additional constructor that enables the use of varargs for
* the animations.
*
* @param animation The [Animation]s that this [ParallelAnimation] should contain.
* @param animation The [Animation]s that this [SequentialAnimation] should contain.
*
* @see Animation
* @see ParallelAnimation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import tools.aqua.bgw.util.Coordinate

internal object Constants {
val PORT = ServerSocket(0).use { it.localPort }
// val PORT = 5173
val DEBUG = false
}

internal class JCEFApplication : Application {
Expand All @@ -79,7 +79,7 @@ internal class JCEFApplication : Application {
override fun start(onClose: () -> Unit, callback: (Any) -> Unit) {
println("[BGW] Starting BGW Runtime (${Constants.PORT})")
EventQueue.invokeLater {
frame = MainFrame(loadCallback = callback)
frame = MainFrame(loadCallback = callback, debugLogging = Constants.DEBUG)
JCEFApplication::class
.java
.getResource("/icon.png")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ internal fun enqueueUpdate(data: AppData) {
}
}

internal fun forceUpdate() {
debounceJob?.cancel()
CoroutineScope(Dispatchers.IO).launch { debounceMutex.withLock { processLastUpdate() } }
}

private suspend fun processLastUpdate() {
val lastUpdate = updateStack.lastOrNull()
updateStack.clear()
Expand Down
5 changes: 4 additions & 1 deletion bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/core/Frontend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ import ActionProp
import PropData
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import java.lang.Runnable
import java.util.*
import jsonMapper
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import tools.aqua.bgw.animation.Animation
import tools.aqua.bgw.application.Application
import tools.aqua.bgw.application.Constants
import tools.aqua.bgw.application.JCEFApplication
import tools.aqua.bgw.binding.componentChannel
import tools.aqua.bgw.binding.forceUpdate
import tools.aqua.bgw.binding.markDirty
import tools.aqua.bgw.binding.module
import tools.aqua.bgw.builder.SceneBuilder
Expand Down Expand Up @@ -153,6 +155,7 @@ internal class Frontend {
}

internal fun sendAnimation(animation: Animation) {
forceUpdate()
val animationData = AnimationMapper.map(animation)
val json = jsonMapper.encodeToString(PropData(animationData))
runBlocking { componentChannel.sendToAllClients(json) }
Expand Down
21 changes: 19 additions & 2 deletions bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/core/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package tools.aqua.bgw.core

import tools.aqua.bgw.animation.Animation
import tools.aqua.bgw.animation.ParallelAnimation
import tools.aqua.bgw.animation.SequentialAnimation
import tools.aqua.bgw.components.ComponentView
import tools.aqua.bgw.components.RootComponent
import tools.aqua.bgw.event.KeyEvent
Expand Down Expand Up @@ -169,15 +171,30 @@ sealed class Scene<T : ComponentView>(width: Number, height: Number, background:
}

/**
* Plays given [Animation].
* Plays given [Animation] non-blocking. This means that any subsequent updates may be executed
* before the [Animation] actually starts playing.
*
* @param animation [Animation] to play.
*/
fun playAnimation(animation: Animation) {
animations.add(animation)
addAnimationRecursively(animation)
Frontend.sendAnimation(animation)
}

private fun addAnimationRecursively(animation: Animation) {
when (animation) {
is SequentialAnimation -> {
animation.animations.forEach { addAnimationRecursively(it) }
animations.add(animation)
}
is ParallelAnimation -> {
animation.animations.forEach { addAnimationRecursively(it) }
animations.add(animation)
}
else -> animations.add(animation)
}
}

/**
* Searches [node] recursively through the visual tree and logs path where the [node] appears as
* first component and the [rootNode] as last.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package tools.aqua.bgw.mapper

import AnimationData
import RecursiveMapper
import data.animation.*
import tools.aqua.bgw.animation.*

Expand All @@ -34,7 +35,7 @@ internal object AnimationMapper {
): ComponentAnimationData {
return this.apply {
id = componentAnimation.id
componentView = ComponentMapper.map(componentAnimation.componentView)
componentView = RecursiveMapper.map(componentAnimation.componentView)
duration = componentAnimation.duration
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ internal object ComponentMapper {
}
is ProgressBar ->
(mapSpecific(componentView) as ProgressBarData).apply {
progress = componentView.progress
progress = componentView.progress.coerceIn(0.0, 1.0)
barColor =
"rgba(${componentView.barColor.red}, ${componentView.barColor.green}, ${componentView.barColor.blue}, ${componentView.barColor.alpha})"
}
Expand Down

0 comments on commit 4c30c4e

Please sign in to comment.