lotus_script/
graphics.rs

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    /// A texture that can be manipulated and displayed on script texture slots.
10    #[derive(Debug)]
11    pub struct Texture(TextureHandle);
12
13    impl Texture {
14        /// Create a new texture.
15        #[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        /// Add an action to the texture. You may want to call the helper methods
27        /// instead of this.
28        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        /// Draw a rectangle on the texture.
35        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        /// Clear the texture with a color.
44        pub fn clear(&mut self, color: Color) {
45            self.add_action(TextureAction::Clear(color));
46        }
47
48        /// Get the color of a pixel on the texture.
49        #[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        /// For better performance, use [`Self::set_pixels`] instead with all the pixels you want to set.
56        /// This is only here for convenience.
57        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        /// Set multiple pixels on the texture.
65        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        /// Draws another texture on top of this one.
75        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        /// Call this once for every game-texture you want to apply this to. You can define the name in the content tool.
83        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        /// Only call this if you need your actions to be applied immediately.
89        /// Cause of streaming assets, this method will return false if the actions are not yet applied.
90        /// Just call this method again until it returns true.
91        pub fn flush(&mut self) -> bool {
92            unsafe { lotus_script_sys::textures::flush_actions(self.0.id()) == 1 }
93        }
94
95        /// Get the handle of the texture.
96        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        // TODO: Support content textures.
109        // Content(ContentId),
110        Script(TextureHandle),
111    }
112
113    // impl From<ContentId> for DrawableTexture {
114    //     fn from(content: ContentId) -> Self {
115    //         Self::Content(content)
116    //     }
117    // }
118
119    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}