megaui/widgets/
texture.rs1use crate::{types::Vector2, Layout, Rect, Ui};
2
3pub struct Texture {
4 position: Option<Vector2>,
5 w: f32,
6 h: f32,
7 texture: u32,
8}
9
10impl Texture {
11 pub fn new(texture: u32) -> Texture {
12 Texture {
13 position: None,
14 w: 100.,
15 h: 100.,
16 texture,
17 }
18 }
19
20 pub fn size(self, w: f32, h: f32) -> Self {
21 Texture { w, h, ..self }
22 }
23
24 pub fn position<P: Into<Option<Vector2>>>(self, position: P) -> Self {
25 let position = position.into();
26
27 Texture { position, ..self }
28 }
29
30 pub fn ui(self, ui: &mut Ui) -> bool {
31 let context = ui.get_active_window_context();
32
33 let size = Vector2::new(self.w, self.h);
34
35 let pos = context
36 .window
37 .cursor
38 .fit(size, self.position.map_or(Layout::Vertical, Layout::Free));
39 context
40 .window
41 .draw_commands
42 .draw_raw_texture(Rect::new(pos.x, pos.y, self.w, self.h), self.texture);
43
44 let rect = Rect::new(pos.x, pos.y, size.x as f32, size.y as f32);
45 let hovered = rect.contains(context.input.mouse_position);
46
47 context.focused && hovered && context.input.click_up
48 }
49}
50
51impl Ui {
52 pub fn texture(&mut self, texture: u32, w: f32, h: f32) -> bool {
53 Texture::new(texture).size(w, h).ui(self)
54 }
55}