1pub use lotus_shared::graphics::*;
2
3pub mod textures {
4 use lotus_script_sys::FfiObject;
5 use lotus_shared::{graphics::Color, math::UVec2};
6
7 pub use lotus_shared::graphics::textures::*;
8
9 #[derive(Debug)]
11 pub struct Texture(TextureHandle);
12
13 impl Texture {
14 #[must_use]
16 pub fn create(options: TextureCreationOptions) -> Self {
17 let options = FfiObject::new(&options);
18
19 unsafe {
20 Self(TextureHandle::new(lotus_script_sys::textures::create(
21 options.packed(),
22 )))
23 }
24 }
25
26 pub fn add_action(&mut self, action: TextureAction) {
29 let action = FfiObject::new(&action);
30
31 unsafe { lotus_script_sys::textures::add_action(self.0.id(), action.packed()) }
32 }
33
34 pub fn draw_rect(&mut self, start: impl Into<UVec2>, end: impl Into<UVec2>, color: Color) {
36 self.add_action(TextureAction::DrawRect {
37 start: start.into(),
38 end: end.into(),
39 color,
40 });
41 }
42
43 pub fn clear(&mut self, color: Color) {
45 self.add_action(TextureAction::Clear(color));
46 }
47
48 #[inline]
50 pub fn get_pixel(&self, x: u32, y: u32) -> Color {
51 let packed = unsafe { lotus_script_sys::textures::get_pixel(self.0.id(), x, y) };
52 packed.into()
53 }
54
55 pub fn set_pixel<P>(&mut self, draw_pixel: P)
58 where
59 P: Into<DrawPixel> + Copy,
60 {
61 self.set_pixels(&[draw_pixel]);
62 }
63
64 pub fn set_pixels<P>(&mut self, pixels: &[P])
66 where
67 P: Into<DrawPixel> + Copy,
68 {
69 let pixels = pixels.iter().map(|p| (*p).into()).collect();
70
71 self.add_action(TextureAction::DrawPixels(pixels));
72 }
73
74 pub fn draw_texture(&mut self, other: &Texture, options: DrawTextureOpts) {
76 self.add_action(TextureAction::DrawScriptTexture {
77 handle: other.handle(),
78 options,
79 });
80 }
81
82 pub fn apply_to(&mut self, name: &str) {
84 let name = FfiObject::new(&name);
85 unsafe { lotus_script_sys::textures::apply_to(self.0.id(), name.packed()) }
86 }
87
88 pub fn flush(&mut self) -> bool {
92 unsafe { lotus_script_sys::textures::flush_actions(self.0.id()) == 1 }
93 }
94
95 pub fn handle(&self) -> TextureHandle {
97 self.0
98 }
99 }
100
101 impl Drop for Texture {
102 fn drop(&mut self) {
103 unsafe { lotus_script_sys::textures::dispose(self.0.id()) }
104 }
105 }
106
107 pub enum DrawableTexture {
108 Script(TextureHandle),
111 }
112
113 impl From<&Texture> for DrawableTexture {
120 fn from(texture: &Texture) -> Self {
121 Self::Script(texture.handle())
122 }
123 }
124
125 impl From<TextureHandle> for DrawableTexture {
126 fn from(handle: TextureHandle) -> Self {
127 Self::Script(handle)
128 }
129 }
130}