aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/ui.rs
blob: 557800aa7e742a2be2b4167a280ff96da277dbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::render::Renderer;
use hurrycurry_protocol::glam::Vec2;

#[derive(Default, Debug)]
pub struct UiState {
    focus: usize,
}

pub struct Ui<'a, 'b> {
    cursor: Vec2,
    cross_height: f32,
    direction_horizontal: bool,
    renderer: &'a mut Renderer<'b>,
    state: &'a mut UiState,
}

impl UiState {
    pub fn draw(&mut self, renderer: &mut Renderer, ui: impl FnOnce(&mut Ui)) {
        let mut u = Ui {
            cursor: Vec2::ZERO,
            direction_horizontal: false,
            renderer,
            state: self,
            cross_height: 0.,
        };
        ui(&mut u);
    }
}

impl<'a, 'b> Ui<'a, 'b> {
    pub fn text(&mut self, text: &str) {
        let size = self.renderer.draw_text(self.cursor, text);
        self.advance(size);
    }
    pub fn button(&mut self, label: &str) -> bool {
        let size = self.renderer.draw_text(self.cursor, label);
        self.advance(size);
        false
    }

    pub fn advance(&mut self, size: Vec2) {
        if self.direction_horizontal {
            self.cursor.x += size.x;
            self.cross_height = self.cross_height.max(size.y);
        } else {
            self.cursor.y += size.y;
            self.cross_height = self.cross_height.max(size.x);
        }
    }
}