godot-tts/src/lib.rs

60 lines
1.2 KiB
Rust
Raw Normal View History

use gdnative::*;
2018-06-13 12:14:03 +00:00
#[cfg(unix)]
use speech_dispatcher::{Connection, Mode, Priority};
#[cfg(windows)]
use tolk::Tolk;
#[derive(gdnative::NativeClass)]
#[inherit(gdnative::Node)]
struct TTS(
#[cfg(unix)]
Connection,
#[cfg(windows)]
Tolk,
);
#[methods]
impl TTS {
fn _init(_owner: gdnative::Node) -> Self {
#[cfg(unix)]
{
let connection = Connection::open("godot", "godot", "godot", Mode::Single);
Self(connection)
2018-06-13 12:14:03 +00:00
}
#[cfg(windows)]
Self(Tolk::new())
}
2018-06-13 12:14:03 +00:00
#[export]
fn speak(&mut self, _owner: Node, message: GodotString, interrupt: bool) {
let message = message.to_string();
println!("{}: {}", message, interrupt);
#[cfg(unix)]
{
if interrupt {
self.0.cancel();
}
if message != "" {
self.0.say(Priority::Important, message);
2018-06-13 12:14:03 +00:00
}
}
}
2018-06-13 12:14:03 +00:00
#[export]
fn stop(&mut self, _owner: Node) {
#[cfg(unix)]
{
self.0.cancel();
2018-06-13 12:14:03 +00:00
}
}
}
fn init(handle: gdnative::init::InitHandle) {
handle.add_class::<TTS>();
2018-06-13 12:14:03 +00:00
}
godot_gdnative_init!();
godot_nativescript_init!(init);
godot_gdnative_terminate!();