Skip to content

Commit 6cee434

Browse files
committed
Fix issues with advancement progress in the assistant
1 parent a8f32c0 commit 6cee434

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

  • storyboard-core/src/commonMain/kotlin/dev/bnorm/storyboard/core
  • storyboard-easel/src/commonMain/kotlin/dev/bnorm/storyboard/easel/notes

storyboard-core/src/commonMain/kotlin/dev/bnorm/storyboard/core/StoryboardState.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ class StoryboardState(
6464
var targetIndex: Storyboard.Index by mutableStateOf(currentIndex)
6565
private set
6666

67-
val advancementProgress: Float
67+
val advancementDistance: Float
6868
get() {
6969
if (currentIndex == targetIndex) return 1f
70+
val start = byIndex.getValue(currentIndex).frameIndex
71+
val target = byIndex.getValue(targetIndex).frameIndex
72+
return abs(target - start).toFloat()
73+
}
7074

71-
val start = byIndex.getValue(currentIndex)
75+
val advancementProgress: Float
76+
get() {
77+
if (currentIndex == targetIndex) return 1f
78+
val start = byIndex.getValue(currentIndex).frameIndex
7279
val current = frameIndex.currentState
73-
val target = byIndex.getValue(targetIndex)
74-
75-
val progress = abs(current - start.frameIndex)
76-
val distance = abs(target.frameIndex - start.frameIndex)
77-
return progress + (frameIndex.fraction / distance)
80+
return abs(current - start) + frameIndex.fraction
7881
}
7982

8083
// TODO manage scope + job + launch internally? or have utilities for it?
@@ -101,8 +104,8 @@ class StoryboardState(
101104
} else {
102105
// Reverse directions.
103106
val tmp = currentIndex
104-
targetIndex = currentIndex
105-
currentIndex = tmp
107+
currentIndex = targetIndex
108+
targetIndex = tmp
106109

107110
frameIndex.animateTo(frameIndex.currentState)
108111
}

storyboard-easel/src/commonMain/kotlin/dev/bnorm/storyboard/easel/notes/ShowAssist.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,25 @@ fun StoryboardNotes(storyboard: StoryboardState, notes: StoryboardNotes, modifie
3333
}
3434

3535
Row(modifier = Modifier.fillMaxWidth().padding(top = 16.dp)) {
36-
val currentIndex = storyboard.currentIndex
3736
Column(modifier = Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
3837
Text("Current Frame")
39-
ClickableScenePreview(storyboard, currentIndex)
38+
ClickableScenePreview(storyboard, storyboard.currentIndex)
4039
SceneAnimationProgressIndicator(storyboard)
4140
}
4241
Spacer(Modifier.width(16.dp))
4342
Column(modifier = Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
4443
Text("Next Frame")
45-
val targetIndex = storyboard.targetIndex
46-
if (targetIndex != currentIndex) {
47-
ClickableScenePreview(storyboard, targetIndex)
48-
44+
val nextIndex by derivedStateOf {
45+
val i = storyboard.storyboard.indices.binarySearch(storyboard.currentIndex)
46+
require(i >= 0) { "targetIndex not found in storyboard" }
47+
storyboard.storyboard.indices.getOrNull(i + 1)
48+
}
49+
nextIndex?.let {
50+
ClickableScenePreview(storyboard, it)
4951
}
5052
}
53+
// TODO previous frame?
54+
// TODO highlight frame which is being advanced to?
5155
}
5256

5357
if (notes.tabs.isNotEmpty()) {
@@ -111,11 +115,22 @@ private fun ClickableScenePreview(
111115
@Composable
112116
private fun SceneAnimationProgressIndicator(storyboard: StoryboardState) {
113117
Row {
118+
val advancementDistance = storyboard.advancementDistance
114119
val advancementProgress = storyboard.advancementProgress
115-
val color = if (advancementProgress == 1f) Color.Green else Color.Red
116-
Spacer(Modifier.height(2.dp).weight(advancementProgress).background(color))
117-
if (advancementProgress < 1f) {
118-
Spacer(Modifier.weight(1f - advancementProgress))
120+
when {
121+
advancementProgress == advancementDistance -> {
122+
Spacer(Modifier.height(2.dp).weight(1f).background(Color.Green))
123+
}
124+
125+
else -> {
126+
val complete = advancementProgress.toInt() / advancementDistance
127+
val partial = (advancementProgress % 1f) / advancementDistance
128+
val remaining = 1f - complete - partial
129+
130+
if (complete > 0f) Spacer(Modifier.height(2.dp).weight(complete).background(Color.Green))
131+
if (partial > 0f) Spacer(Modifier.height(2.dp).weight(partial).background(Color.Red))
132+
if (remaining > 0f) Spacer(Modifier.height(2.dp).weight(remaining).background(Color.DarkGray))
133+
}
119134
}
120135
}
121136
}

0 commit comments

Comments
 (0)