animate/legacy/image.rs
1use crate::Content;
2use glib::{object::IsA, translate::*};
3use std::{fmt, ptr};
4
5glib_wrapper! {
6 pub struct Image(Object<ffi::ClutterImage, ffi::ClutterImageClass, ImageClass>) @implements Content;
7
8 match fn {
9 get_type => || ffi::clutter_image_get_type(),
10 }
11}
12
13impl Image {
14 /// Creates a new `Image` instance.
15 ///
16 /// # Returns
17 ///
18 /// the newly created `Image` instance.
19 /// Use `gobject::ObjectExt::unref` when done.
20 pub fn new() -> Option<Content> {
21 unsafe { from_glib_full(ffi::clutter_image_new()) }
22 }
23}
24
25// impl Default for Image {
26// fn default() -> Self {
27// Self::new()
28// }
29// }
30
31/// Trait containing all `Image` methods.
32///
33/// # Implementors
34///
35/// [`Image`](struct.Image.html)
36pub trait ImageExt: 'static {
37 //fn set_area(&self, data: &[u8], pixel_format: dx::PixelFormat, rect: &cairo::RectangleInt, row_stride: u32) -> Result<(), glib::Error>;
38
39 /// Sets the image data stored inside a `glib::Bytes` to be displayed by `self`.
40 ///
41 /// If the image data was successfully loaded, the `self` will be invalidated.
42 ///
43 /// In case of error, the `error` value will be set, and this function will
44 /// return `false`.
45 ///
46 /// The image data contained inside the `glib::Bytes` is copied in texture memory,
47 /// and no additional reference is acquired on the `data`.
48 /// ## `data`
49 /// the image data, as a `glib::Bytes`
50 /// ## `pixel_format`
51 /// the Cogl pixel format of the image data
52 /// ## `width`
53 /// the width of the image data
54 /// ## `height`
55 /// the height of the image data
56 /// ## `row_stride`
57 /// the length of each row inside `data`
58 ///
59 /// # Returns
60 ///
61 /// `true` if the image data was successfully loaded,
62 /// and `false` otherwise.
63 fn set_bytes(
64 &self,
65 data: &glib::Bytes,
66 pixel_format: dx::PixelFormat,
67 width: u32,
68 height: u32,
69 row_stride: u32,
70 ) -> Result<(), glib::Error>;
71
72 //fn set_data(&self, data: &[u8], pixel_format: dx::PixelFormat, width: u32, height: u32, row_stride: u32) -> Result<(), glib::Error>;
73}
74
75impl<O: IsA<Image>> ImageExt for O {
76 //fn set_area(&self, data: &[u8], pixel_format: dx::PixelFormat, rect: &cairo::RectangleInt, row_stride: u32) -> Result<(), glib::Error> {
77 // unsafe { TODO: call clutter_sys:clutter_image_set_area() }
78 //}
79
80 fn set_bytes(
81 &self,
82 data: &glib::Bytes,
83 pixel_format: dx::PixelFormat,
84 width: u32,
85 height: u32,
86 row_stride: u32,
87 ) -> Result<(), glib::Error> {
88 unsafe {
89 let mut error = ptr::null_mut();
90 let _ = ffi::clutter_image_set_bytes(
91 self.as_ref().to_glib_none().0,
92 data.to_glib_none().0,
93 pixel_format.to_glib(),
94 width,
95 height,
96 row_stride,
97 &mut error,
98 );
99 if error.is_null() {
100 Ok(())
101 } else {
102 Err(from_glib_full(error))
103 }
104 }
105 }
106
107 //fn set_data(&self, data: &[u8], pixel_format: dx::PixelFormat, width: u32, height: u32, row_stride: u32) -> Result<(), glib::Error> {
108 // unsafe { TODO: call clutter_sys:clutter_image_set_data() }
109 //}
110}
111
112impl fmt::Display for Image {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 write!(f, "Image")
115 }
116}