1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! 時間を進める・skip・入力待ちから進める、と今の状態の問い合わせ。
use super::options::Motion;
use super::output::{StepKind, WaitKind};
use super::Reveal;
use crate::markup::TextEvent;
impl Reveal {
/// 入力待ちなら、その種類。文字送りの途中なら `None`。
pub fn wait_kind(&self) -> Option<WaitKind> {
if self.finished {
Some(WaitKind::End)
} else if !self.waiting {
None
} else if self.waiting_click() {
Some(WaitKind::Click)
} else {
Some(WaitKind::Page)
}
}
/// 動きの強さ([`RevealOptions::motion`](crate::RevealOptions::motion))。
pub fn motion(&self) -> Motion {
self.motion
}
/// 動きの強さを変える。時間割は変わらないので、文字送りの途中でもそのまま切り替わる。
pub fn set_motion(&mut self, motion: Motion) {
self.motion = motion;
}
/// `dt` 秒進め、その間に起きた出来事を返す。
///
/// 入力待ち・終了中も時刻は進む(エフェクトや猫の動きが止まらないように)が、
/// 文字と出来事は増えない。`dt` が負・NaN・無限大なら時刻は進めない。
pub fn advance(&mut self, dt: f32) -> Vec<TextEvent> {
// 負・NaN・無限大の経過時間では時刻を動かさない(巻き戻したり、NaN のまま戻らなくなったりしない)
if dt.is_finite() && dt > 0.0 {
self.time += dt;
}
if self.waiting || self.finished {
return Vec::new();
}
self.run_until(self.time)
}
/// 次の入力待ち(`[l]`・ページの終わり)の手前まで一度に表示する。途中の出来事も返す。
pub fn skip(&mut self) -> Vec<TextEvent> {
if self.waiting || self.finished {
return Vec::new();
}
let next_wait = self
.page_clicks()
.get(self.resumed.len())
.map(|c| c.step)
.unwrap_or_else(|| self.timelines[self.page].len().saturating_sub(1));
let end = self.step_time(next_wait);
self.time = self.time.max(end);
self.skipped = true;
let mut out = self.run_until(f32::INFINITY);
out.retain(|e| !matches!(e, TextEvent::Blip(_)));
out
}
/// 時刻 `until` までの出来事を返す。文字送りの音は最後の1つだけ残す。
pub(super) fn run_until(&mut self, until: f32) -> Vec<TextEvent> {
let mut out = self.run_steps(until);
if let Some(last) = out.iter().rposition(|e| matches!(e, TextEvent::Blip(_))) {
let mut i = 0;
out.retain(|e| {
let keep = !matches!(e, TextEvent::Blip(_)) || i == last;
i += 1;
keep
});
}
out
}
fn run_steps(&mut self, until: f32) -> Vec<TextEvent> {
let mut out = Vec::new();
while let Some(step) = self.timelines[self.page].get(self.next) {
if self.step_time(self.next) > until {
break;
}
let kind = step.kind.clone();
let index = self.next;
self.next += 1;
match kind {
StepKind::Show(i) => self.shown = i + 1,
StepKind::Emit(e) => {
match e {
TextEvent::WaitInput => self.waiting = true,
TextEvent::Finished => self.finished = true,
TextEvent::WaitClick => {
// 復元のときなど、もう進んだ [l] は待たない
let k = self
.page_clicks()
.iter()
.position(|c| c.step == index)
.unwrap_or(0);
if k < self.resumed.len() {
continue;
}
self.waiting = true;
out.push(e);
break;
}
_ => {}
}
out.push(e);
}
#[allow(unreachable_patterns)]
_ => {}
}
}
out
}
/// 入力待ちなら先へ進む。進んだら `true`。
///
/// ページの途中の入力待ち(`[l]`)では同じページの続きを、ページの終わりでは次のページを出す。
/// 続きの文字は、待った長さだけ後ろへずれる(`[l]` を待った長さの `[wait]` として扱う)。
pub fn proceed(&mut self) -> bool {
if !self.waiting {
return false;
}
if self.waiting_click() {
self.resumed.push(self.time);
self.waiting = false;
self.skipped = false;
return true;
}
self.waiting = false;
self.page += 1;
self.time = 0.0;
self.next = 0;
self.shown = 0;
self.skipped = false;
self.resumed.clear();
true
}
/// 今の区切り(ページの始めか、最後に進んだ `[l]` の後)で [`Reveal::skip`] を使ったか。
pub fn skipped(&self) -> bool {
self.skipped
}
/// ページの数(空の文でも 1)。
pub fn page_count(&self) -> usize {
self.timelines.len()
}
/// 今のページの番号(0 始まり)。
pub fn page(&self) -> usize {
self.page
}
/// 今のページで表示済みの文字数。
pub fn visible_count(&self) -> usize {
self.shown
}
/// ページの始めからの経過時間(秒)。
pub fn time(&self) -> f32 {
self.time
}
/// 入力待ち(`[l]`・`[p]`)か。種類は [`Reveal::wait_kind`]。
pub fn is_waiting(&self) -> bool {
self.waiting
}
/// すべて表示し終えたか。
pub fn is_finished(&self) -> bool {
self.finished
}
}