#[cfg(feature = "internal")]
use lotus_script_sys::FfiObject;
pub use lotus_shared::graphics::*;
pub mod textures {
use lotus_script_sys::FfiObject;
use lotus_shared::{content::ContentId, graphics::Color, math::UVec2};
pub use lotus_shared::graphics::textures::*;
#[derive(Debug)]
pub struct Texture(TextureHandle);
impl Texture {
#[must_use]
pub fn create<'a>(options: impl Into<TextureCreationOptions<'a>>) -> Self {
let options = options.into();
let options = FfiObject::new(&options);
unsafe {
Self(TextureHandle::new(lotus_script_sys::textures::create(
options.packed(),
)))
}
}
pub fn add_action(&mut self, action: TextureAction) {
let action = FfiObject::new(&action);
unsafe { lotus_script_sys::textures::add_action(self.0.id(), action.packed()) }
}
pub fn draw_rect(&mut self, start: impl Into<UVec2>, end: impl Into<UVec2>, color: Color) {
self.add_action(TextureAction::DrawRect {
start: start.into(),
end: end.into(),
color,
});
}
pub fn clear(&mut self, color: Color) {
self.add_action(TextureAction::Clear(color));
}
#[inline]
pub fn read_pixel(&self, x: u32, y: u32) -> Color {
let packed = unsafe { lotus_script_sys::textures::get_pixel(self.0.id(), x, y) };
packed.into()
}
pub fn draw_pixels<P>(&mut self, pixels: &[P])
where
P: Into<DrawPixel> + Copy,
{
let pixels = pixels.iter().map(|p| (*p).into()).collect();
self.add_action(TextureAction::DrawPixels(pixels));
}
pub fn draw_texture(&mut self, other: &Texture, options: DrawTextureOpts) {
self.add_action(TextureAction::DrawScriptTexture {
handle: other.handle(),
options,
});
}
pub fn apply_to(&mut self, name: &str) {
let name = FfiObject::new(&name);
unsafe { lotus_script_sys::textures::apply_to(self.0.id(), name.packed()) }
}
pub fn flush(&mut self) -> bool {
unsafe { lotus_script_sys::textures::flush_actions(self.0.id()) == 1 }
}
pub fn draw_script_texture(&mut self, other: &Texture, options: DrawTextureOpts) {
self.add_action(TextureAction::DrawScriptTexture {
handle: other.handle(),
options,
});
}
pub fn draw_text(
&mut self,
font: ContentId,
text: impl Into<String>,
top_left: impl Into<UVec2>,
letter_spacing: u32,
full_color: impl Into<Option<Color>>,
alpha_mode: AlphaMode,
) {
self.add_action(TextureAction::DrawText {
font,
text: text.into(),
top_left: top_left.into(),
letter_spacing,
full_color: full_color.into(),
alpha_mode,
});
}
pub fn handle(&self) -> TextureHandle {
self.0
}
pub fn forget(mut self) {
self.0 = TextureHandle::new(u32::MAX);
}
}
impl Drop for Texture {
fn drop(&mut self) {
if self.0.id() != u32::MAX {
unsafe { lotus_script_sys::textures::dispose(self.0.id()) }
}
}
}
pub enum DrawableTexture {
Script(TextureHandle),
}
impl From<&Texture> for DrawableTexture {
fn from(texture: &Texture) -> Self {
Self::Script(texture.handle())
}
}
impl From<TextureHandle> for DrawableTexture {
fn from(handle: TextureHandle) -> Self {
Self::Script(handle)
}
}
}
#[cfg(feature = "internal")]
pub fn fetch_drawable_texture_properties() -> Vec<DrawableTextureProperties> {
let properties = unsafe { lotus_script_sys::textures::fetch_drawable_texture_properties() };
FfiObject::from_packed(properties).deserialize()
}