index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff
snake-rs / src / ui / mod.rs
-rw-r--r-- blob 474e9f34c29cb7cbbb0c91fd9a35e1d4755e62d3 1129 bytes (plain)
1
use sdl2::{pixels::Color, rect::Rect};
2
3
use crate::{
4
    game_context::{GameEvent, Point},
5
    ui::{button::Button, text::Text},
6
};
7
8
pub mod button;
9
pub mod text;
10
11
pub struct Ui {
12
    pub buttons: Vec<Button>,
13
    pub texts: Vec<Text>,
14
}
15
16
impl Ui {
17
    pub fn new() -> Self {
18
        Self {
19
            buttons: vec![],
20
            texts: vec![],
21
        }
22
    }
23
24
    pub fn button(
25
        &mut self,
26
        x: i32,
27
        y: i32,
28
        width: u32,
29
        height: u32,
30
        text: &str,
31
        event: GameEvent,
32
    ) -> &mut Button {
33
        self.buttons.push(Button {
34
            rect: Rect::new(x, y, width, height),
35
            color: Color::RGB(0, 120, 215),
36
            text: text.to_string(),
37
            event,
38
        });
39
        self.buttons.last_mut().unwrap()
40
    }
41
42
    pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
43
        self.texts.push(Text {
44
            text: text.to_string(),
45
            color,
46
            point,
47
        });
48
        self.texts.last_mut().unwrap()
49
    }
50
51
    pub fn clear(&mut self) {
52
        self.buttons.clear();
53
        self.texts.clear();
54
    }
55
}