Struct UiRenderer

Source
pub struct UiRenderer { /* private fields */ }

Implementations§

Source§

impl UiRenderer

Source

pub fn watch_shader_file(&mut self, path: &str)

Examples found in repository?
examples/ui.rs (line 40)
36    fn new(_config: Self::Config, mut deps: Self::Dependencies) -> anyhow::Result<Self> {
37        deps.bloom.settings_mut().activated = false;
38        deps.ui
39            .ui_renderer
40            .watch_shader_file("./src/modules/ui/ui.wgsl");
41
42        Ok(MyApp {
43            deps,
44            ui: Board::new(dvec2(800.0, 800.0)),
45        })
46    }
More examples
Hide additional examples
examples/ui_image.rs (line 39)
35    fn new(_config: Self::Config, mut deps: Self::Dependencies) -> anyhow::Result<Self> {
36        deps.bloom.settings_mut().activated = false;
37        deps.ui
38            .ui_renderer
39            .watch_shader_file("./src/modules/ui/ui.wgsl");
40
41        let img_bytes = std::fs::read("assets/test.png").unwrap();
42        let rgba = image::load_from_memory(&img_bytes).unwrap().to_rgba8();
43        let texture = Texture::from_image(&deps.ctx.device, &deps.ctx.queue, &rgba);
44
45        let bindable_texture = BindableTexture::new(&deps.ctx.device, texture);
46        let image_texture = deps.arenas.insert(bindable_texture);
47
48        Ok(MyApp {
49            deps,
50            ui: Board::new(dvec2(800.0, 800.0)),
51            image_key: image_texture,
52            value: 300.0,
53        })
54    }
Source

pub fn draw_ui_board(&mut self, board: &Board)

Warning: only call AFTER layout has been performed for this frame. (needs to be BillboardPhase::Rendering) Assumes all rects and text layouts are calculated.

Examples found in repository?
examples/ui_padding.rs (line 84)
49    fn update(&mut self) {
50        self.deps.gizmos.draw_xyz();
51        self.deps
52            .color_mesh
53            .draw_cubes(&[Transform::new(1.0, 1.0, 1.0)], None);
54        self.ui.start_frame(
55            BoardInput::from_input_module(&self.deps.input),
56            self.deps.ctx.size_dvec2(),
57        );
58
59        let mut parent = self.ui.add_div("Parent", None);
60        parent.width(Len::PARENT);
61        parent.height(Len::PARENT);
62        parent.main_align = MainAlign::Center;
63        parent.cross_align = Align::Center;
64        parent.color = Color::RED;
65        let parent = Some(parent.id);
66
67        let mut rect = self.ui.add_div("rect", parent);
68        rect.width(Len::px(400.0));
69        rect.height(Len::px(400.0));
70        rect.padding = Padding::new()
71            .left(Len::px(50.0))
72            .top(Len::px(50.0))
73            .right(Len::parent(0.5));
74        rect.color = Color::BLACK;
75        rect.main_align = MainAlign::End;
76        let rect = Some(rect.id);
77
78        let mut inner = self.ui.add_div("inner", rect);
79        inner.width(Len::PARENT);
80        inner.height(Len::px(200.0));
81        inner.color = Color::WHITE;
82
83        self.ui.end_frame(&mut self.deps.ui.fonts);
84        self.deps.ui.ui_renderer.draw_ui_board(&self.ui);
85    }
More examples
Hide additional examples
examples/ui_image.rs (line 100)
65    fn update(&mut self) {
66        self.deps.gizmos.draw_xyz();
67        self.deps
68            .color_mesh
69            .draw_cubes(&[Transform::new(1.0, 1.0, 1.0)], None);
70
71        let size = self.deps.ctx.size;
72        self.ui.start_frame(
73            BoardInput::from_input_module(&self.deps.input),
74            dvec2(size.width as f64, size.height as f64),
75        );
76
77        let mut parent = self.ui.add_div("Parent", None);
78        parent.width(Len::PARENT);
79        parent.height(Len::PARENT);
80        parent.axis = Axis::X;
81        parent.main_align = MainAlign::Center;
82        parent.cross_align = Align::Center;
83        // parent.color = Color::RED.alpha(0.0);
84        let parent = Some(parent.id);
85
86        // show some image in the UI
87        let mut img = self.ui.add_div("img", parent);
88        img.width(Len::px(200.0));
89        img.height(Len::px(200.0));
90        img.texture = Some(DivTexture {
91            texture: self.image_key.key(),
92            uv: Aabb::UNIT,
93        });
94
95        // sow the slider
96        self.ui
97            .add(Slider::new(&mut self.value, 0.0, 400.0), "slider", parent);
98
99        self.ui.end_frame(&mut self.deps.ui.fonts);
100        self.deps.ui.ui_renderer.draw_ui_board(&self.ui);
101
102        // compare to Egui Widget (obviously a bit more polished)
103        let mut egui = self.deps.egui.context();
104        egui::Window::new("Value").show(&mut egui, |ui| {
105            ui.add(egui::Slider::new(&mut self.value, 0.0..=400.0));
106        });
107        // std::thread::sleep(Duration::from_millis(150));
108    }
examples/ui.rs (line 255)
57    fn update(&mut self) {
58        self.deps.gizmos.draw_xyz();
59        self.deps
60            .color_mesh
61            .draw_cubes(&[Transform::new(1.0, 1.0, 1.0)], None);
62
63        let size = self.deps.ctx.size;
64        self.ui.start_frame(
65            BoardInput::from_input_module(&self.deps.input),
66            dvec2(size.width as f64, size.height as f64),
67        );
68
69        self.deps.world_rects.draw_textured_rect(
70            UiRect {
71                pos: Rect::new(0.0, 0.0, 1024.0, 1024.0),
72                uv: Rect::UNIT,
73                color: Color::WHITE,
74                border_radius: Default::default(),
75            },
76            Transform::default(),
77            self.deps.ui.fonts.atlas_texture(),
78        );
79
80        let mut parent = self.ui.add_div("Parent", None);
81
82        parent.width(Len::PARENT);
83        parent.axis = Axis::X;
84        parent.main_align = MainAlign::SpaceBetween;
85        parent.cross_align = Align::Center;
86        parent.color = Color::RED.alpha(0.2);
87
88        let parent = Some(parent.id);
89
90        // let text = self.ui.add_text_div(
91        //     DivProps::default(),
92        //     DivStyle::default(),
93        //     Text {
94        //         color: Color::WHITE,
95        //         string: "This is all the text that we need".into(),
96        //         font: None,
97        //         size: FontSize(80),
98        //         offset_x: Len::ZERO,
99        //         offset_y: Len::ZERO,
100        //     },
101        //     "sasass",
102        //     Some(parent),
103        // );
104
105        // let text2 = self.ui.add_text_div(
106        //     DivProps::default(),
107        //     DivStyle::default(),
108        //     Text {
109        //         color: Color::RED,
110        //         string: "This is some other text".into(),
111        //         font: None,
112        //         size: FontSize(20),
113        //         offset_x: Len::ZERO,
114        //         offset_y: Len::ZERO,
115        //     },
116        //     "asdsadsadsasdsad",
117        //     Some(parent),
118        // );
119
120        let mut purp_parent = self.ui.add_div("Purple Parent", parent);
121        purp_parent.width(Len::px(100.0));
122        purp_parent.height(Len::px(200.0));
123        purp_parent.axis = Axis::Y;
124        purp_parent.main_align = MainAlign::Center;
125        purp_parent.cross_align = Align::Center;
126        purp_parent.color = Color::PURPLE.alpha(0.5);
127        purp_parent.border_radius = BorderRadius::all(20.0);
128        purp_parent.border_color = Color::GREEN;
129        purp_parent.border_thickness = 6.0;
130        purp_parent.border_softness = 1.0;
131        let purp_parent = Some(purp_parent.id);
132
133        let mut c1 = self.ui.add_div("child 1 in purple", purp_parent);
134        c1.width(Len::px(50.0));
135        c1.height(Len::px(50.0));
136        c1.main_align = MainAlign::Center;
137        c1.cross_align = Align::Center;
138        c1.color = Color::GREEN;
139
140        let mut c2 = self.ui.add_div("child 2 in purple", purp_parent);
141        c2.width(Len::px(70.0));
142        c2.height(Len::px(30.0));
143        c2.main_align = MainAlign::Center;
144        c2.cross_align = Align::Center;
145        c2.color = Color::WHITE;
146
147        let mut other = self.ui.add_div("other", parent);
148        other.width(Len::px(100.0));
149        other.height(Len::px(20.0));
150        other.color = Color::BLACK;
151
152        let mut text_div = self.ui.add_text_div(
153            Text {
154                color: Color::new(6.0, 2.0, 2.0),
155                string: "Hover me please, I will show you something!".into(),
156                size: FontSize(48),
157                offset_x: Len::px(30.0),
158                offset_y: Len::px(30.0),
159                ..Default::default()
160            },
161            "text div",
162            parent,
163        );
164
165        text_div.width(Len::px(300.0));
166        text_div.height(Len::px(400.0));
167
168        text_div.color = Color::YELLOW;
169        text_div.border_radius = BorderRadius::all(20.0);
170        text_div.border_thickness = 20.0;
171
172        // can immediately edit the style and text without a 1-frame lag:
173        // 1 frame lag only applies to the layout rect (DivProps) itself.
174        if text_div.mouse_in_rect() {
175            let style = text_div.style();
176            style.color = Color::BLUE;
177            style.border_color = Color::GREEN;
178            style.border_thickness = 6.0;
179            // style.border_radius = BorderRadius::new(40.0, 40.0, 40.0, 40.0);
180            text_div.text().color = Color::BLACK;
181        }
182
183        let total_time = self.deps.time.total().as_secs_f64() * 4.0;
184        let total_time2 = self.deps.time.total().as_secs_f64() * 9.7;
185        if text_div.mouse_in_rect() {
186            let mut green_square = self.ui.add_div(2112213232, parent);
187
188            green_square.width(Len::px(40.0));
189            green_square.height(Len::px(40.0));
190            green_square.color = Color::GREEN;
191            green_square.offset_x = Len::px(total_time.sin() * 20.0);
192            green_square.offset_y = Len::px(total_time2.cos() * 20.0);
193        }
194
195        let mut container2 = self.ui.add_div("Container 2", parent);
196        container2.height(Len::PARENT);
197        container2.main_align = MainAlign::SpaceAround;
198        container2.cross_align = Align::Center;
199
200        let container2 = Some(container2.id);
201
202        {
203            let clicked = self
204                .ui
205                .add(
206                    Button {
207                        text: "Click".into(),
208                        ..Default::default()
209                    },
210                    "my button",
211                    container2,
212                )
213                .clicked;
214            if clicked {
215                println!("Hello 1");
216            }
217        }
218        {
219            let clicked = self
220                .ui
221                .add(
222                    Button {
223                        text: "Button 2".into(),
224                        ..Default::default()
225                    },
226                    "my button 2",
227                    container2,
228                )
229                .clicked;
230            if clicked {
231                println!("Hello 2");
232            }
233        }
234        {
235            let clicked = self
236                .ui
237                .add(
238                    Button {
239                        text: "Button 3".into(),
240                        ..Default::default()
241                    },
242                    "my button 3",
243                    container2,
244                )
245                .clicked;
246            if clicked {
247                println!("Hello 3");
248            }
249        }
250
251        // let mut ctx = self.deps.egui.context();
252        // egui_inspect_board(&mut ctx, &mut self.ui);
253
254        self.ui.end_frame(&mut self.deps.ui.fonts);
255        self.deps.ui.ui_renderer.draw_ui_board(&self.ui);
256        // std::thread::sleep(Duration::from_millis(150));
257    }

Trait Implementations§

Source§

impl Module for UiRenderer

Source§

type Config = ()

Some initial data that configures the module. Provided by the User when adding a module to an app.
Source§

type Dependencies = Deps

Other modules that are expected to be part of the app. Provided automatically during app setup, where the program resolves which dependencies each module has.
Source§

fn new(_config: Self::Config, deps: Self::Dependencies) -> Result<Self>

creates this module
Source§

fn intialize(handle: Handle<Self>) -> Result<()>

Is run once, after all modules have been initialized. This function is optional, it is given a handle to the module itself. other modules can be accessed if you cache the handles to them in the new() function. E.g. the LineRenderer could register its own handle (Handle) with a general Renderer module, if a Handle<Renderer> was part of the Self::Dependencies and cached in the new function. E.g. LineRenderer could have a field renderer: Handle<Renderer> that is populated in new.
Source§

impl Prepare for UiRenderer

Source§

fn prepare( &mut self, device: &Device, queue: &Queue, _encoder: &mut CommandEncoder, )

Source§

impl SdrSurfaceRenderer for UiRenderer

Source§

fn render<'e>(&'e self, encoder: &'e mut CommandEncoder, view: &TextureView)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more