Pass utterance IDs into signals, and return an utterance ID when speaking.

This commit is contained in:
Nolan Darilek 2020-09-30 13:45:20 -05:00
parent 68d1d9e011
commit c7563fa997
2 changed files with 20 additions and 11 deletions

6
TTS.gd
View File

@ -121,8 +121,9 @@ var normal_rate_percentage setget , _get_rate_percentage
func speak(text, interrupt := true):
var utterance
if tts != null:
tts.speak(text, interrupt)
utterance = tts.speak(text, interrupt)
elif OS.has_feature('JavaScript'):
var code = (
"""
@ -139,6 +140,7 @@ func speak(text, interrupt := true):
JavaScript.eval(code)
else:
print_debug("%s: %s" % [text, interrupt])
return utterance
func stop():
@ -236,8 +238,6 @@ func singular_or_plural(count, singular, plural):
func _on_utterance_begin(utterance):
print("Here")
print(utterance)
emit_signal("utterance_begin", utterance)

View File

@ -174,9 +174,17 @@ impl TTS {
}
#[export]
fn speak(&mut self, _owner: &Node, message: GodotString, interrupt: bool) {
fn speak(&mut self, _owner: &Node, message: GodotString, interrupt: bool) -> Option<Variant> {
let message = message.to_string();
self.0.speak(message, interrupt).unwrap();
if let Ok(id) = self.0.speak(message, interrupt) {
let utterance: Instance<Utterance, Unique> = Instance::new();
utterance
.map_mut(|u, _| u.0 = id)
.expect("Failed to set utterance ID");
Some(utterance.owned_to_variant())
} else {
None
}
}
#[export]
@ -208,16 +216,16 @@ impl TTS {
match msg {
Msg::UtteranceBegin(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
/*utterance
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");*/
.expect("Failed to set utterance ID");
owner.emit_signal("utterance_begin", &[utterance.owned_to_variant()]);
}
Msg::UtteranceEnd(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
/*utterance
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");*/
.expect("Failed to set utterance ID");
owner.emit_signal("utterance_end", &[utterance.owned_to_variant()]);
}
}
@ -227,6 +235,7 @@ impl TTS {
fn init(handle: InitHandle) {
env_logger::init();
handle.add_class::<Utterance>();
handle.add_class::<TTS>();
}