Bump GDNative dependency and refactor.

This commit is contained in:
Nolan Darilek 2020-03-19 13:16:56 -05:00
parent f364001480
commit 735c7ebfa6
2 changed files with 32 additions and 48 deletions

View File

@ -8,5 +8,5 @@ edition = "2018"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
gdnative = "0.7" gdnative = "0.8"
tts = "0.2" tts = "0.2"

View File

@ -1,56 +1,14 @@
use std::u8; use std::u8;
use gdnative::*;
use gdnative::init::*; use gdnative::init::*;
use gdnative::*;
use tts::{Features, TTS as Tts}; use tts::{Features, TTS as Tts};
#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::register_properties)]
struct TTS(Tts); struct TTS(Tts);
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,
hint: PropertyHint::Range {
range: 0.0..100.0,
step: 1.,
slider: true,
},
getter: |this: &TTS| {
match this.0.get_rate() {
Ok(rate) => rate / u8::MAX * 100,
_ => 0,
}
},
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();
}
},
usage: PropertyUsage::DEFAULT,
});
}
}
#[methods] #[methods]
impl TTS { impl TTS {
fn _init(_owner: gdnative::Node) -> Self { fn _init(_owner: gdnative::Node) -> Self {
@ -58,6 +16,31 @@ impl TTS {
Self(tts) Self(tts)
} }
fn register_properties(builder: &ClassBuilder<Self>) {
builder
.add_property::<u8>("rate")
.with_default(50)
.with_getter(|this: &TTS, _| match this.0.get_rate() {
Ok(rate) => rate / u8::MAX * 100,
_ => 0,
})
.with_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();
}
})
.done()
}
#[export] #[export]
fn speak(&mut self, _owner: Node, message: GodotString, interrupt: bool) { fn speak(&mut self, _owner: Node, message: GodotString, interrupt: bool) {
let message = message.to_string(); let message = message.to_string();
@ -72,7 +55,8 @@ impl TTS {
#[export] #[export]
fn is_rate_supported(&mut self, _owner: Node) -> bool { fn is_rate_supported(&mut self, _owner: Node) -> bool {
let Features { let Features {
rate: rate_supported, .. rate: rate_supported,
..
} = self.0.supported_features(); } = self.0.supported_features();
rate_supported rate_supported
} }