Skip to content

Commit

Permalink
Add TweenTouchInput to tween engine
Browse files Browse the repository at this point in the history
Make "enabled" property of TouchInputComponent changeable by the tween engine.
  • Loading branch information
jobe-m committed Aug 5, 2024
1 parent beaab2a commit c01c266
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.Serializable

@Serializable @SerialName("TouchInput")
data class TouchInputComponent(
var enabled: Boolean = true,
var pressed: Boolean = false,
var triggerImmediately: Boolean = false,
var entity: Entity = Entity.NONE, // If touch was triggered than below EntityConfig will be executed for this Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ data class TweenPropertyComponent (

EventPublish(componentTypeOf<TweenPropertyComponent>()),
EventReset(componentTypeOf<TweenPropertyComponent>()),
EventSubscribe(componentTypeOf<TweenPropertyComponent>())
EventSubscribe(componentTypeOf<TweenPropertyComponent>()),

TouchInputEnable(componentTypeOf<TweenPropertyComponent>())
}

companion object {
Expand Down Expand Up @@ -120,6 +121,8 @@ data class TweenPropertyComponent (
val TweenEventPublishComponent = TweenProperty.EventPublish.type
val TweenEventResetComponent = TweenProperty.EventReset.type
val TweenEventSubscribeComponent = TweenProperty.EventSubscribe.type

val TweenTouchInputEnableComponent = TweenProperty.TouchInputEnable.type
}

// Author's hint: Check if deep copy is needed on any change in the component!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ data class TweenSequenceComponent(
)
}

@Serializable @SerialName("TweenTouchInput")
data class TweenTouchInput(
var enabled: Boolean? = null,

override var entity: Entity,
override var delay: Float? = null,
override var duration: Float? = null,
@Serializable(with = EasingAsString::class) override var easing: Easing? = null
) : TweenBase {
override fun clone(): TweenTouchInput =
this.copy(
entity = entity.clone(),
easing = easing
)
}

companion object : ComponentType<TweenSequenceComponent>()

// Author's hint: Check if deep copy is needed on any change in the component!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import kotlin.coroutines.*

const val snapshotFps = 30

//fun <T> mutableListWithCapacityOf(capacity: Int) : MutableList<T> = ArrayList(capacity)

/**
* This system operates on a world snapshot of Fleks and stores it in an array for (fast) rewind and forward.
*
Expand Down Expand Up @@ -85,11 +83,6 @@ class SnapshotSerializerSystem(module: SerializersModule) : IntervalSystem(
}
}

// Delete old snapshots which are older than 30 seconds (to free some memory on Android devices)
// if (recording.size > 30f * snapshotFps) {
// recording.removeFirst()
// }

// Store copy of word snapshot
recording.add(snapshotCopy)
rewindSeek = recording.size - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import korlibs.korge.fleks.components.TweenPropertyComponent.Companion.TweenSwit
import korlibs.korge.fleks.components.TweenPropertyComponent.Companion.TweenTextFieldTextComponent
import korlibs.korge.fleks.components.TweenPropertyComponent.Companion.TweenTextFieldTextRangeEndComponent
import korlibs.korge.fleks.components.TweenPropertyComponent.Companion.TweenTextFieldTextRangeStartComponent
import korlibs.korge.fleks.components.TweenPropertyComponent.Companion.TweenTouchInputEnableComponent
import korlibs.korge.fleks.entity.*
import kotlin.jvm.JvmName
import kotlin.reflect.KMutableProperty0
Expand Down Expand Up @@ -214,6 +215,17 @@ class TweenEventSystem : IteratingSystem(
}
}

class TweenTouchInputSystem : IteratingSystem(
family {
all(TouchInputComponent)
.any(TweenTouchInputEnableComponent) },
interval = EachFrame
) {
override fun onTickEntity(entity: Entity) {
val touchInputComponent = entity[TouchInputComponent]
updateProperty(entity, TweenTouchInputEnableComponent, touchInputComponent::enabled)
}
}

//class AnimateLifeCycleSystem : IteratingSystem(
// family { all(LifeCycleComponent).any(TweenLifeCycleHealthCounter) },
Expand All @@ -239,6 +251,7 @@ fun SystemConfiguration.setupTweenEngineSystems() {
add(TweenSoundSystem())
add(TweenTextFieldSystem())
add(TweenEventSystem())
add(TweenTouchInputSystem())
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class TweenSequenceSystem : IteratingSystem(
tween.textRangeStart?.let { end -> createTweenPropertyComponent(TextFieldTextRangeStart, start.textRangeStart, end - start.textRangeStart ) }
tween.textRangeEnd?.let { end -> createTweenPropertyComponent(TextFieldTextRangeEnd, start.textRangeEnd, end - start.textRangeEnd ) }
}
is TweenTouchInput -> tween.entity.getOrWarning(TouchInputComponent)?.let {
tween.enabled?.let { value -> createTweenPropertyComponent(TouchInputEnable, value) }
}
// Creates a new entity (or uses the given entity from the tween) and configures it by running the config-function
is SpawnEntity -> {
val spawnedEntity = if (tween.entity == Entity.NONE) world.entity("SpawnEntity: ${tween.entityConfig}") else tween.entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ internal val internalModule = SerializersModule {
subclass(TweenSpawner::class)
subclass(TweenSound::class)
subclass(TweenTextField::class)
subclass(TweenTouchInput::class)
}
}

Expand Down

0 comments on commit c01c266

Please sign in to comment.