载入中...
搜索中...
未找到
DialogueUX.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cctype>
4#include <cstdlib>
5#include <simplesquirrel/simplesquirrel.hpp>
6#include <sstream>
7#include "audio/Source.h"
9
10// DialogueUX.cpp
11namespace eve::dialogue {
12
14
15void DialogueUX::record(const std::string& lineId, const std::string& speaker, const std::string& text) {
16 history_.push_back({lineId, speaker, plainText(text)});
17 if (!lineId.empty()) readLines_.insert(lineId);
18}
19
20void DialogueUX::clearHistory() { history_.clear(); }
21
22int DialogueUX::getHistoryCount() const { return static_cast<int>(history_.size()); }
23
24const DialogueUX::HistoryEntry* DialogueUX::historyAt(int index) const {
25 return index >= 0 && static_cast<size_t>(index) < history_.size() ? &history_[static_cast<size_t>(index)] : nullptr;
26}
27
28std::string DialogueUX::getHistoryLineId(int index) const {
29 const auto* entry = historyAt(index);
30 return entry ? entry->lineId : std::string{};
31}
32
33std::string DialogueUX::getHistorySpeaker(int index) const {
34 const auto* entry = historyAt(index);
35 return entry ? entry->speaker : std::string{};
36}
37
38std::string DialogueUX::getHistoryText(int index) const {
39 const auto* entry = historyAt(index);
40 return entry ? entry->text : std::string{};
41}
42
43namespace {
44
45bool isControlTag(const std::string& tag) {
46 const size_t equal = tag.find('=');
47 const std::string name = tag.substr(0, equal);
48 return name == "pause" || name == "speed" || name == "color" || name == "/color" || name == "shake" ||
49 name == "/shake";
50}
51
52} // namespace
53
54std::string DialogueUX::plainText(const std::string& richText) const {
55 std::string out;
56 out.reserve(richText.size());
57 for (size_t i = 0; i < richText.size();) {
58 if (richText[i] == '[') {
59 const size_t close = richText.find(']', i + 1);
60 if (close != std::string::npos && isControlTag(richText.substr(i + 1, close - i - 1))) {
61 i = close + 1;
62 continue;
63 }
64 }
65 out.push_back(richText[i++]);
66 }
67 return out;
68}
69
70int DialogueUX::getTextActionCount(const std::string& richText) const {
71 int count = 0;
72 for (size_t i = 0; i < richText.size();) {
73 if (richText[i] == '[') {
74 const size_t close = richText.find(']', i + 1);
75 if (close != std::string::npos) {
76 if (isControlTag(richText.substr(i + 1, close - i - 1))) ++count;
77 i = close + 1;
78 continue;
79 }
80 }
81 ++i;
82 }
83 return count;
84}
85
86void DialogueUX::setAutoDelay(float seconds) { autoDelay_ = seconds < 0.f ? 0.f : seconds; }
87
88bool DialogueUX::updateAuto(float dt, bool voicePlaying) {
89 if (!autoMode_ || voicePlaying) return false;
90 autoElapsed_ += dt > 0.f ? dt : 0.f;
91 if (autoElapsed_ < autoDelay_) return false;
92 autoElapsed_ = 0.f;
93 return true;
94}
95
96bool DialogueUX::setSkipMode(const std::string& mode) {
97 if (mode != "off" && mode != "read" && mode != "all") return false;
98 skipMode_ = mode;
99 return true;
100}
101
102void DialogueUX::markRead(const std::string& lineId) {
103 if (!lineId.empty()) readLines_.insert(lineId);
104}
105
106bool DialogueUX::isRead(const std::string& lineId) const {
107 return !lineId.empty() && readLines_.find(lineId) != readLines_.end();
108}
109
110bool DialogueUX::shouldSkip(const std::string& lineId) const {
111 return skipMode_ == "all" || (skipMode_ == "read" && isRead(lineId));
112}
113
114void DialogueUX::expose(ssq::Table& table) {
115 auto cls = table.addClass(name, DialogueUX::create, false);
116 expose(cls);
117}
118
119void DialogueUX::expose(ssq::Class& cls) {
120 cls.addFunc("getName", &DialogueUX::getName);
121 cls.addFunc("record", &DialogueUX::record);
122 cls.addFunc("clearHistory", &DialogueUX::clearHistory);
123 cls.addFunc("getHistoryCount", &DialogueUX::getHistoryCount);
124 cls.addFunc("getHistoryLineId", &DialogueUX::getHistoryLineId);
125 cls.addFunc("getHistorySpeaker", &DialogueUX::getHistorySpeaker);
126 cls.addFunc("getHistoryText", &DialogueUX::getHistoryText);
127 cls.addFunc("plainText", &DialogueUX::plainText);
128 cls.addFunc("getTextActionCount", &DialogueUX::getTextActionCount);
129 cls.addFunc("setAutoMode", &DialogueUX::setAutoMode);
130 cls.addFunc("isAutoMode", &DialogueUX::isAutoMode);
131 cls.addFunc("setAutoDelay", &DialogueUX::setAutoDelay);
132 cls.addFunc("getAutoDelay", &DialogueUX::getAutoDelay);
133 cls.addFunc("updateAuto", &DialogueUX::updateAuto);
134 cls.addFunc("resetAutoTimer", &DialogueUX::resetAutoTimer);
135 cls.addFunc("setSkipMode", &DialogueUX::setSkipMode);
136 cls.addFunc("getSkipMode", &DialogueUX::getSkipMode);
137 cls.addFunc("markRead", &DialogueUX::markRead);
138 cls.addFunc("isRead", &DialogueUX::isRead);
139 cls.addFunc("shouldSkip", &DialogueUX::shouldSkip);
140}
141
142} // namespace eve::dialogue
143
144// DialogueVoice.cpp
145namespace eve::dialogue {
146
148
150
151std::string DialogueVoice::key(const std::string& lineId, const std::string& locale) { return lineId + "\n" + locale; }
152
153bool DialogueVoice::registerClip(const std::string& lineId, const std::string& locale, float duration,
154 const std::string& envelope) {
155 if (lineId.empty() || duration < 0.f) return false;
156 Clip& clip = clips_[key(lineId, locale)];
157 clip.lineId = lineId;
158 clip.locale = locale;
159 clip.duration = duration;
160 clip.envelope.clear();
161 std::istringstream input(envelope);
162 std::string item;
163 while (std::getline(input, item, ',')) {
164 char* end = nullptr;
165 const float value = std::strtof(item.c_str(), &end);
166 if (end == item.c_str()) return false;
167 clip.envelope.push_back(std::clamp(value, 0.f, 1.f));
168 }
169 return true;
170}
171
172bool DialogueVoice::bindSource(const std::string& lineId, const std::string& locale, audio::Source* source) {
173 if (!source || lineId.empty()) return false;
174 Clip& clip = clips_[key(lineId, locale)];
175 clip.lineId = lineId;
176 clip.locale = locale;
177 clip.source = source;
178 clip.duration = static_cast<float>(source->getDuration());
179 return true;
180}
181
182DialogueVoice::Clip* DialogueVoice::resolve(const std::string& lineId, const std::string& locale) {
183 const auto exact = clips_.find(key(lineId, locale));
184 if (exact != clips_.end()) return &exact->second;
185 const auto fallback = clips_.find(key(lineId, ""));
186 return fallback == clips_.end() ? nullptr : &fallback->second;
187}
188
189const DialogueVoice::Clip* DialogueVoice::resolve(const std::string& lineId, const std::string& locale) const {
190 return const_cast<DialogueVoice*>(this)->resolve(lineId, locale);
191}
192
193bool DialogueVoice::hasClip(const std::string& lineId, const std::string& locale) const {
194 return resolve(lineId, locale) != nullptr;
195}
196
198 stop();
199 clips_.clear();
200}
201
202bool DialogueVoice::play(const std::string& lineId, const std::string& locale) {
203 stop();
204 current_ = resolve(lineId, locale);
205 if (!current_) return false;
206 currentLineId_ = lineId;
207 elapsed_ = 0.f;
208 simulatedPlaying_ = current_->source == nullptr && current_->duration > 0.f;
209 if (current_->source) current_->source->play();
210 return true;
211}
212
214 if (current_ && current_->source) current_->source->stop();
215 current_ = nullptr;
216 currentLineId_.clear();
217 elapsed_ = 0.f;
218 simulatedPlaying_ = false;
219}
220
222 if (!current_ || current_->source) return;
223 elapsed_ += std::max(dt, 0.f);
224 if (elapsed_ >= current_->duration) simulatedPlaying_ = false;
225}
226
228 return current_ && (current_->source ? current_->source->isPlaying() : simulatedPlaying_);
229}
230
232 return current_ && current_->source ? static_cast<float>(current_->source->tell()) : elapsed_;
233}
234
235float DialogueVoice::getDuration() const { return current_ ? current_->duration : 0.f; }
236
238 if (!current_ || current_->envelope.empty()) return 0.f;
239 const size_t index = std::min(static_cast<size_t>(getTime() * envelopeRate_), current_->envelope.size() - 1);
240 return current_->envelope[index];
241}
242
243bool DialogueVoice::shouldAutoAdvance() const { return current_ && !isPlaying() && getTime() >= current_->duration; }
244
245void DialogueVoice::setEnvelopeRate(float samplesPerSecond) {
246 envelopeRate_ = samplesPerSecond > 0.f ? samplesPerSecond : 1.f;
247}
248
249void DialogueVoice::expose(ssq::Table& table) {
250 auto cls = table.addClass(name, DialogueVoice::create, false);
251 expose(cls);
252}
253
254void DialogueVoice::expose(ssq::Class& cls) {
255 cls.addFunc("getName", &DialogueVoice::getName);
256 cls.addFunc("registerClip", &DialogueVoice::registerClip);
257 cls.addFunc("bindSource", &DialogueVoice::bindSource);
258 cls.addFunc("hasClip", &DialogueVoice::hasClip);
259 cls.addFunc("clear", &DialogueVoice::clear);
260 cls.addFunc("play", &DialogueVoice::play);
261 cls.addFunc("stop", &DialogueVoice::stop);
262 cls.addFunc("update", &DialogueVoice::update);
263 cls.addFunc("isPlaying", &DialogueVoice::isPlaying);
264 cls.addFunc("getTime", &DialogueVoice::getTime);
265 cls.addFunc("getDuration", &DialogueVoice::getDuration);
266 cls.addFunc("getAmplitude", &DialogueVoice::getAmplitude);
267 cls.addFunc("shouldAutoAdvance", &DialogueVoice::shouldAutoAdvance);
268 cls.addFunc("getCurrentLineId", &DialogueVoice::getCurrentLineId);
269 cls.addFunc("setEnvelopeRate", &DialogueVoice::setEnvelopeRate);
270 cls.addFunc("getEnvelopeRate", &DialogueVoice::getEnvelopeRate);
271}
272
273} // namespace eve::dialogue
float duration
HSQOBJECT cls
Definition ECS.cpp:39
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
EvpackChunkInput input
Definition Evpack.cpp:170
glm::vec4 clip
const std::string * tag
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
double dt
Object::Ptr value
std::string text
ScriptStructure out
std::string_view source
size_t end
int count
uint32_t index
virtual std::string getName() const =0
Returns the name.
A playable audio source (static buffer or streaming decoder). Not thread-safe for playback control ex...
Definition Source.h:30
Presentation state shared by default and custom dialogue UIs.
Definition DialogueUX.h:14
bool isAutoMode() const
True when auto mode.
Definition DialogueUX.h:39
void setAutoMode(bool enabled)
Sets the auto mode.
Definition DialogueUX.h:37
std::string getHistoryText(int index) const
Returns the history text.
int getTextActionCount(const std::string &richText) const
Number of pause/speed/style actions in rich text.
void clearHistory()
Clears history.
void record(const std::string &lineId, const std::string &speaker, const std::string &text)
Record a completed line in the player-visible backlog.
void resetAutoTimer()
Resets auto timer.
Definition DialogueUX.h:47
bool shouldSkip(const std::string &lineId) const
Should skip.
float getAutoDelay() const
Returns the auto delay.
Definition DialogueUX.h:43
bool isRead(const std::string &lineId) const
True when read.
void setAutoDelay(float seconds)
Sets the auto delay.
bool setSkipMode(const std::string &mode)
Set skip mode: off, read, or all.
std::string getHistoryLineId(int index) const
Returns the history line id.
int getHistoryCount() const
Returns the history count.
bool updateAuto(float dt, bool voicePlaying)
Advance the auto timer; voice playback holds the timer.
std::string getSkipMode() const
Returns the skip mode.
Definition DialogueUX.h:52
void markRead(const std::string &lineId)
Mark read.
std::string getHistorySpeaker(int index) const
Returns the history speaker.
std::string plainText(const std::string &richText) const
Strip supported control tags for plain UI widgets.
Localized voice assets, duration-based advance, and amplitude lip sync.
float getTime() const
Returns the time.
~DialogueVoice() override
Dialogue voice.
float getEnvelopeRate() const
Returns the envelope rate.
void setEnvelopeRate(float samplesPerSecond)
Sets the envelope rate.
float getAmplitude() const
Returns the amplitude.
bool bindSource(const std::string &lineId, const std::string &locale, audio::Source *source)
Bind a playable Audio Source; the caller retains source ownership.
bool shouldAutoAdvance() const
Should auto advance.
void update(float dt)
Updates .
std::string getCurrentLineId() const
Returns the current line id.
bool hasClip(const std::string &lineId, const std::string &locale) const
True when clip.
bool isPlaying() const
True when playing.
bool play(const std::string &lineId, const std::string &locale)
Play.
bool registerClip(const std::string &lineId, const std::string &locale, float duration, const std::string &envelope)
Register clip metadata; envelope is comma-separated 0..1 amplitudes.
float getDuration() const
Returns the duration.