62 lines
1.4 KiB
Rust
62 lines
1.4 KiB
Rust
use galactica_content::{resolve_edge_as_edge, Content};
|
|
use log::error;
|
|
use rhai::{CustomType, TypeBuilder};
|
|
use std::{cell::RefCell, rc::Rc};
|
|
|
|
use crate::ui::util::Sprite;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SpriteElement {
|
|
pub sprite: Rc<RefCell<Sprite>>,
|
|
pub ct: Rc<Content>,
|
|
}
|
|
|
|
unsafe impl Send for SpriteElement {}
|
|
unsafe impl Sync for SpriteElement {}
|
|
|
|
impl SpriteElement {
|
|
pub fn new(ct: Rc<Content>, sprite: Rc<RefCell<Sprite>>) -> Self {
|
|
Self { ct, sprite }
|
|
}
|
|
|
|
// This MUST be &mut, or rhai won't recognize it.
|
|
pub fn has_name(&mut self, s: String) -> bool {
|
|
self.sprite.as_ref().borrow().name == s
|
|
}
|
|
|
|
pub fn take_edge(&mut self, edge_name: &str, duration: f32) {
|
|
let sprite_handle = self.sprite.as_ref().borrow().anim.get_sprite();
|
|
let sprite = self.ct.get_sprite(sprite_handle);
|
|
|
|
let edge = resolve_edge_as_edge(edge_name, duration, |x| {
|
|
sprite.get_section_handle_by_name(x)
|
|
});
|
|
let edge = match edge {
|
|
Err(x) => {
|
|
error!(
|
|
"UI script reference invalid section `{}` in sprite `{}`, skipping",
|
|
edge_name, sprite.name
|
|
);
|
|
error!("error: {:?}", x);
|
|
return;
|
|
}
|
|
Ok(s) => s,
|
|
};
|
|
|
|
self.sprite
|
|
.as_ref()
|
|
.borrow_mut()
|
|
.anim
|
|
.jump_to(&self.ct, edge);
|
|
}
|
|
}
|
|
|
|
impl CustomType for SpriteElement {
|
|
fn build(mut builder: TypeBuilder<Self>) {
|
|
builder
|
|
.with_name("SpriteElement")
|
|
.with_fn("has_name", Self::has_name)
|
|
.with_fn("take_edge", Self::take_edge);
|
|
}
|
|
}
|