5#include <simplesquirrel/simplesquirrel.hpp>
17 if (!lineId.empty()) readLines_.insert(lineId);
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;
29 const auto* entry = historyAt(
index);
30 return entry ? entry->lineId : std::string{};
34 const auto* entry = historyAt(
index);
35 return entry ? entry->speaker : std::string{};
39 const auto* entry = historyAt(
index);
40 return entry ? entry->text : std::string{};
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" ||
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))) {
65 out.push_back(richText[i++]);
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;
89 if (!autoMode_ || voicePlaying)
return false;
90 autoElapsed_ +=
dt > 0.f ?
dt : 0.f;
91 if (autoElapsed_ < autoDelay_)
return false;
97 if (mode !=
"off" && mode !=
"read" && mode !=
"all")
return false;
103 if (!lineId.empty()) readLines_.insert(lineId);
107 return !lineId.empty() && readLines_.find(lineId) != readLines_.end();
111 return skipMode_ ==
"all" || (skipMode_ ==
"read" &&
isRead(lineId));
114void DialogueUX::expose(ssq::Table& table) {
115 auto cls = table.addClass(
name, DialogueUX::create,
false);
119void DialogueUX::expose(ssq::Class&
cls) {
151std::string DialogueVoice::key(
const std::string& lineId,
const std::string& locale) {
return lineId +
"\n" + locale; }
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;
160 clip.envelope.clear();
161 std::istringstream
input(envelope);
163 while (std::getline(
input, item,
',')) {
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));
173 if (!
source || lineId.empty())
return false;
174 Clip&
clip = clips_[
key(lineId, locale)];
175 clip.lineId = lineId;
176 clip.locale = locale;
178 clip.duration =
static_cast<float>(
source->getDuration());
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;
189const DialogueVoice::Clip* DialogueVoice::resolve(
const std::string& lineId,
const std::string& locale)
const {
190 return const_cast<DialogueVoice*
>(
this)->resolve(lineId, locale);
194 return resolve(lineId, locale) !=
nullptr;
204 current_ = resolve(lineId, locale);
205 if (!current_)
return false;
206 currentLineId_ = lineId;
208 simulatedPlaying_ = current_->source ==
nullptr && current_->duration > 0.f;
209 if (current_->source) current_->source->play();
214 if (current_ && current_->source) current_->source->stop();
216 currentLineId_.clear();
218 simulatedPlaying_ =
false;
222 if (!current_ || current_->source)
return;
223 elapsed_ += std::max(
dt, 0.f);
224 if (elapsed_ >= current_->duration) simulatedPlaying_ =
false;
228 return current_ && (current_->source ? current_->source->isPlaying() : simulatedPlaying_);
232 return current_ && current_->source ?
static_cast<float>(current_->source->tell()) : elapsed_;
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];
246 envelopeRate_ = samplesPerSecond > 0.f ? samplesPerSecond : 1.f;
249void DialogueVoice::expose(ssq::Table& table) {
250 auto cls = table.addClass(
name, DialogueVoice::create,
false);
254void DialogueVoice::expose(ssq::Class&
cls) {
#define Module_IMPL(ModuleName, newExpr)
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...
Presentation state shared by default and custom dialogue UIs.
bool isAutoMode() const
True when auto mode.
void setAutoMode(bool enabled)
Sets the auto mode.
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.
bool shouldSkip(const std::string &lineId) const
Should skip.
float getAutoDelay() const
Returns the auto delay.
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.
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.