godot-tts/src/lib.rs

88 lines
2.1 KiB
Rust
Raw Normal View History

use std::u8;
use gdnative::*;
2019-09-27 18:03:19 +00:00
use gdnative::init::*;
use tts::{Features, TTS as Tts};
2018-06-13 12:14:03 +00:00
2019-09-10 15:09:04 +00:00
struct TTS(Tts);
2019-09-27 18:03:19 +00:00
impl NativeClass for TTS {
type Base = Node;
type UserData = user_data::MutexData<TTS>;
fn class_name() -> &'static str {
"TTS"
}
fn init(owner: Self::Base) -> Self {
Self::_init(owner)
}
fn register_properties(builder: &ClassBuilder<Self>) {
builder.add_property(Property {
name: "rate",
default: 50,
2019-09-27 18:03:19 +00:00
hint: PropertyHint::Range {
range: 0.0..100.0,
2019-09-27 18:03:19 +00:00
step: 1.,
slider: true,
},
getter: |this: &TTS| {
match this.0.get_rate() {
Ok(rate) => rate / u8::MAX * 100,
_ => 0,
}
2019-09-27 18:03:19 +00:00
},
setter: |this: &mut TTS, mut v: u8| {
if v > 100 {
v = 100;
}
let mut v = v as f32;
v = v * u8::MAX as f32 / 100.;
let Features {
rate: rate_supported, ..
} = this.0.supported_features();
if rate_supported {
this.0.set_rate(v as u8).unwrap();
}
2019-09-27 18:03:19 +00:00
},
usage: PropertyUsage::DEFAULT,
});
}
}
#[methods]
impl TTS {
fn _init(_owner: gdnative::Node) -> Self {
2019-09-10 15:09:04 +00:00
let tts = Tts::default().unwrap();
Self(tts)
}
2018-06-13 12:14:03 +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();
}
2018-06-13 12:14:03 +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
}
#[export]
fn is_rate_supported(&mut self, _owner: Node) -> bool {
let Features {
rate: rate_supported, ..
} = self.0.supported_features();
rate_supported
}
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!();