Implement stop signals.

This commit is contained in:
Nolan Darilek 2020-10-08 08:56:56 -05:00
parent 2c73b45300
commit 1dabd5afd3
2 changed files with 29 additions and 0 deletions

7
TTS.gd
View File

@ -5,6 +5,8 @@ signal utterance_begin(utterance)
signal utterance_end(utterance) signal utterance_end(utterance)
signal utterance_stop(utterance)
var TTS var TTS
var tts var tts
@ -23,6 +25,7 @@ func _init():
if self.are_utterance_callbacks_supported: if self.are_utterance_callbacks_supported:
tts.connect("utterance_begin", self, "_on_utterance_begin") tts.connect("utterance_begin", self, "_on_utterance_begin")
tts.connect("utterance_end", self, "_on_utterance_end") tts.connect("utterance_end", self, "_on_utterance_end")
tts.connect("utterance_stop", self, "_on_utterance_stop")
else: else:
print_debug("TTS not available!") print_debug("TTS not available!")
@ -245,6 +248,10 @@ func _on_utterance_end(utterance):
emit_signal("utterance_end", utterance) emit_signal("utterance_end", utterance)
func _on_utterance_stop(utterance):
emit_signal("utterance_stop", utterance)
func _exit_tree(): func _exit_tree():
if not tts or not TTS: if not tts or not TTS:
return return

View File

@ -17,6 +17,7 @@ impl Utterance {
enum Msg { enum Msg {
UtteranceBegin(UtteranceId), UtteranceBegin(UtteranceId),
UtteranceEnd(UtteranceId), UtteranceEnd(UtteranceId),
UtteranceStop(UtteranceId),
} }
#[derive(NativeClass)] #[derive(NativeClass)]
@ -36,6 +37,7 @@ impl TTS {
} = tts.supported_features(); } = tts.supported_features();
if utterance_callbacks { if utterance_callbacks {
let tx_end = tx.clone(); let tx_end = tx.clone();
let tx_stop = tx.clone();
tts.on_utterance_begin(Some(Box::new(move |utterance| { tts.on_utterance_begin(Some(Box::new(move |utterance| {
tx.send(Msg::UtteranceBegin(utterance)).unwrap(); tx.send(Msg::UtteranceBegin(utterance)).unwrap();
}))) })))
@ -44,6 +46,10 @@ impl TTS {
tx_end.send(Msg::UtteranceEnd(utterance)).unwrap(); tx_end.send(Msg::UtteranceEnd(utterance)).unwrap();
}))) })))
.expect("Failed to set utterance_end callback"); .expect("Failed to set utterance_end callback");
tts.on_utterance_stop(Some(Box::new(move |utterance| {
tx_stop.send(Msg::UtteranceStop(utterance)).unwrap();
})))
.expect("Failed to set utterance_stop callback");
} }
Self(tts, rx) Self(tts, rx)
} }
@ -171,6 +177,15 @@ impl TTS {
usage: PropertyUsage::DEFAULT, usage: PropertyUsage::DEFAULT,
}], }],
}); });
builder.add_signal(Signal {
name: "utterance_stop",
args: &[SignalArgument {
name: "utterance",
default: Variant::default(),
export_info: ExportInfo::new(VariantType::Object),
usage: PropertyUsage::DEFAULT,
}],
});
} }
#[export] #[export]
@ -231,6 +246,13 @@ impl TTS {
.expect("Failed to set utterance ID"); .expect("Failed to set utterance ID");
owner.emit_signal("utterance_end", &[utterance.owned_to_variant()]); owner.emit_signal("utterance_end", &[utterance.owned_to_variant()]);
} }
Msg::UtteranceStop(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");
owner.emit_signal("utterance_stop", &[utterance.owned_to_variant()]);
}
} }
} }
} }