2019-09-27 18:03:19 +00:00
|
|
|
use gdnative::init::*;
|
2020-03-19 18:16:56 +00:00
|
|
|
use gdnative::*;
|
2019-10-09 21:10:14 +00:00
|
|
|
use tts::{Features, TTS as Tts};
|
2018-06-13 12:14:03 +00:00
|
|
|
|
2020-03-19 18:16:56 +00:00
|
|
|
#[derive(NativeClass)]
|
|
|
|
#[inherit(Node)]
|
|
|
|
#[register_with(Self::register_properties)]
|
2019-09-10 15:09:04 +00:00
|
|
|
struct TTS(Tts);
|
2019-09-03 13:54:36 +00:00
|
|
|
|
2020-03-19 18:16:56 +00:00
|
|
|
#[methods]
|
|
|
|
impl TTS {
|
|
|
|
fn _init(_owner: gdnative::Node) -> Self {
|
|
|
|
let tts = Tts::default().unwrap();
|
|
|
|
Self(tts)
|
2019-09-27 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn register_properties(builder: &ClassBuilder<Self>) {
|
2020-03-19 18:16:56 +00:00
|
|
|
builder
|
2020-05-19 12:49:28 +00:00
|
|
|
.add_property("rate")
|
2020-03-19 18:16:56 +00:00
|
|
|
.with_getter(|this: &TTS, _| match this.0.get_rate() {
|
2020-05-19 12:49:28 +00:00
|
|
|
Ok(rate) => rate,
|
|
|
|
_ => 0.,
|
|
|
|
})
|
|
|
|
.with_setter(|this: &mut TTS, _, v: f32| {
|
|
|
|
let Features {
|
|
|
|
rate: rate_supported,
|
|
|
|
..
|
|
|
|
} = this.0.supported_features();
|
|
|
|
if rate_supported {
|
|
|
|
let mut v = v;
|
|
|
|
if v < this.0.min_rate() {
|
|
|
|
v = this.0.min_rate();
|
|
|
|
} else if v > this.0.max_rate() {
|
|
|
|
v = this.0.max_rate();
|
|
|
|
}
|
|
|
|
this.0.set_rate(v).unwrap();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.done();
|
|
|
|
builder
|
|
|
|
.add_property("min_rate")
|
|
|
|
.with_getter(|this: &TTS, _| {
|
|
|
|
let Features {
|
|
|
|
rate: rate_supported,
|
|
|
|
..
|
|
|
|
} = this.0.supported_features();
|
|
|
|
if rate_supported {
|
|
|
|
this.0.min_rate()
|
|
|
|
} else {
|
|
|
|
0.
|
|
|
|
}
|
2020-03-19 18:16:56 +00:00
|
|
|
})
|
2020-05-19 12:49:28 +00:00
|
|
|
.done();
|
|
|
|
builder
|
|
|
|
.add_property("max_rate")
|
|
|
|
.with_getter(|this: &TTS, _| {
|
|
|
|
let Features {
|
|
|
|
rate: rate_supported,
|
|
|
|
..
|
|
|
|
} = this.0.supported_features();
|
|
|
|
if rate_supported {
|
|
|
|
this.0.max_rate()
|
|
|
|
} else {
|
|
|
|
0.
|
2019-10-09 21:12:47 +00:00
|
|
|
}
|
2020-05-19 12:49:28 +00:00
|
|
|
})
|
|
|
|
.done();
|
|
|
|
builder
|
|
|
|
.add_property("normal_rate")
|
|
|
|
.with_getter(|this: &TTS, _| {
|
2019-10-17 13:30:16 +00:00
|
|
|
let Features {
|
2020-03-19 18:16:56 +00:00
|
|
|
rate: rate_supported,
|
|
|
|
..
|
2019-10-17 13:30:16 +00:00
|
|
|
} = this.0.supported_features();
|
|
|
|
if rate_supported {
|
2020-05-19 12:49:28 +00:00
|
|
|
this.0.normal_rate()
|
|
|
|
} else {
|
|
|
|
0.
|
2019-10-17 13:30:16 +00:00
|
|
|
}
|
2020-03-19 18:16:56 +00:00
|
|
|
})
|
2020-05-19 12:49:28 +00:00
|
|
|
.done();
|
2019-09-03 13:54:36 +00:00
|
|
|
}
|
2018-06-13 12:14:03 +00:00
|
|
|
|
2019-09-03 13:54:36 +00:00
|
|
|
#[export]
|
|
|
|
fn speak(&mut self, _owner: Node, message: GodotString, interrupt: bool) {
|
|
|
|
let message = message.to_string();
|
2019-09-10 15:09:04 +00:00
|
|
|
self.0.speak(message, interrupt).unwrap();
|
2019-09-03 13:54:36 +00:00
|
|
|
}
|
2018-06-13 12:14:03 +00:00
|
|
|
|
2019-09-03 13:54:36 +00:00
|
|
|
#[export]
|
|
|
|
fn stop(&mut self, _owner: Node) {
|
2019-09-10 15:09:04 +00:00
|
|
|
self.0.stop().unwrap();
|
2018-06-13 12:14:03 +00:00
|
|
|
}
|
2019-10-09 21:10:14 +00:00
|
|
|
|
|
|
|
#[export]
|
|
|
|
fn is_rate_supported(&mut self, _owner: Node) -> bool {
|
|
|
|
let Features {
|
2020-03-19 18:16:56 +00:00
|
|
|
rate: rate_supported,
|
|
|
|
..
|
2019-10-09 21:10:14 +00:00
|
|
|
} = self.0.supported_features();
|
2019-10-17 13:30:16 +00:00
|
|
|
rate_supported
|
2019-10-09 21:10:14 +00:00
|
|
|
}
|
2018-06-13 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 13:54:36 +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!();
|