cogl/auto/
snippet.rs

1#![allow(
2    clippy::too_many_arguments,
3    clippy::let_and_return,
4    clippy::from_over_into
5)]
6
7use crate::{Object, SnippetHook};
8
9use glib::translate::*;
10use glib::GString;
11use std::fmt;
12
13glib_wrapper! {
14    pub struct Snippet(Object<ffi::CoglSnippet, SnippetClass>) @extends Object;
15
16    match fn {
17        get_type => || ffi::cogl_snippet_get_gtype(),
18    }
19}
20
21impl Snippet {
22    /// Allocates and initializes a new snippet with the given source strings.
23    /// ## `hook`
24    /// The point in the pipeline that this snippet will wrap around
25    ///  or replace.
26    /// ## `declarations`
27    /// The source code for the declarations for this
28    ///  snippet or `None`. See `Snippet::set_declarations`.
29    /// ## `post`
30    /// The source code to run after the hook point where this
31    ///  shader snippet is attached or `None`. See `Snippet::set_post`.
32    ///
33    /// # Returns
34    ///
35    /// a pointer to a new `Snippet`
36    pub fn new(hook: SnippetHook, declarations: &str, post: &str) -> Snippet {
37        unsafe {
38            from_glib_full(ffi::cogl_snippet_new(
39                hook.to_glib(),
40                declarations.to_glib_none().0,
41                post.to_glib_none().0,
42            ))
43        }
44    }
45
46    ///
47    /// # Returns
48    ///
49    /// the source string that was set with
50    ///  `Snippet::set_declarations` or `None` if none was set.
51    pub fn get_declarations(&self) -> Option<GString> {
52        unsafe { from_glib_none(ffi::cogl_snippet_get_declarations(self.to_glib_none().0)) }
53    }
54
55    ///
56    /// # Returns
57    ///
58    /// the hook that was set when `Snippet::new` was
59    ///  called.
60    pub fn get_hook(&self) -> SnippetHook {
61        unsafe { from_glib(ffi::cogl_snippet_get_hook(self.to_glib_none().0)) }
62    }
63
64    ///
65    /// # Returns
66    ///
67    /// the source string that was set with
68    ///  `Snippet::set_post` or `None` if none was set.
69    pub fn get_post(&self) -> Option<GString> {
70        unsafe { from_glib_none(ffi::cogl_snippet_get_post(self.to_glib_none().0)) }
71    }
72
73    ///
74    /// # Returns
75    ///
76    /// the source string that was set with
77    ///  `Snippet::set_pre` or `None` if none was set.
78    pub fn get_pre(&self) -> Option<GString> {
79        unsafe { from_glib_none(ffi::cogl_snippet_get_pre(self.to_glib_none().0)) }
80    }
81
82    ///
83    /// # Returns
84    ///
85    /// the source string that was set with
86    ///  `Snippet::set_replace` or `None` if none was set.
87    pub fn get_replace(&self) -> Option<GString> {
88        unsafe { from_glib_none(ffi::cogl_snippet_get_replace(self.to_glib_none().0)) }
89    }
90
91    /// Sets a source string that will be inserted in the global scope of
92    /// the generated shader when this snippet is used on a pipeline. This
93    /// string is typically used to declare uniforms, attributes or
94    /// functions that will be used by the other parts of the snippets.
95    ///
96    /// This function should only be called before the snippet is attached
97    /// to its first pipeline. After that the snippet should be considered
98    /// immutable.
99    /// ## `declarations`
100    /// The new source string for the declarations section
101    ///  of this snippet.
102    pub fn set_declarations(&self, declarations: &str) {
103        unsafe {
104            ffi::cogl_snippet_set_declarations(
105                self.to_glib_none().0,
106                declarations.to_glib_none().0,
107            );
108        }
109    }
110
111    /// Sets a source string that will be inserted after the hook point in
112    /// the generated shader for the pipeline that this snippet is attached
113    /// to. Please see the documentation of each hook point in
114    /// `Pipeline` for a description of how this string should be used.
115    ///
116    /// This function should only be called before the snippet is attached
117    /// to its first pipeline. After that the snippet should be considered
118    /// immutable.
119    /// ## `post`
120    /// The new source string for the post section of this snippet.
121    pub fn set_post(&self, post: &str) {
122        unsafe {
123            ffi::cogl_snippet_set_post(self.to_glib_none().0, post.to_glib_none().0);
124        }
125    }
126
127    /// Sets a source string that will be inserted before the hook point in
128    /// the generated shader for the pipeline that this snippet is attached
129    /// to. Please see the documentation of each hook point in
130    /// `Pipeline` for a description of how this string should be used.
131    ///
132    /// This function should only be called before the snippet is attached
133    /// to its first pipeline. After that the snippet should be considered
134    /// immutable.
135    /// ## `pre`
136    /// The new source string for the pre section of this snippet.
137    pub fn set_pre(&self, pre: &str) {
138        unsafe {
139            ffi::cogl_snippet_set_pre(self.to_glib_none().0, pre.to_glib_none().0);
140        }
141    }
142
143    /// Sets a source string that will be used instead of any generated
144    /// source code or any previous snippets for this hook point. Please
145    /// see the documentation of each hook point in `Pipeline` for a
146    /// description of how this string should be used.
147    ///
148    /// This function should only be called before the snippet is attached
149    /// to its first pipeline. After that the snippet should be considered
150    /// immutable.
151    /// ## `replace`
152    /// The new source string for the replace section of this snippet.
153    pub fn set_replace(&self, replace: &str) {
154        unsafe {
155            ffi::cogl_snippet_set_replace(self.to_glib_none().0, replace.to_glib_none().0);
156        }
157    }
158}
159
160impl fmt::Display for Snippet {
161    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162        write!(f, "Snippet")
163    }
164}