index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff

Diffstat (limited to 'src/ui/mod.rs')

-rw-r--r-- src/ui/mod.rs 55
 1 file changed, 55 insertions(+)

diff --git a/src/ui/mod.rs b/src/ui/mod.rs
new file mode 100644
index 0000000..474e9f3 100644
--- /dev/null
+++ b/src/ui/mod.rs
@@ -1,0 +1,55 @@
use sdl2::{pixels::Color, rect::Rect};

use crate::{
    game_context::{GameEvent, Point},
    ui::{button::Button, text::Text},
};

pub mod button;
pub mod text;

pub struct Ui {
    pub buttons: Vec<Button>,
    pub texts: Vec<Text>,
}

impl Ui {
    pub fn new() -> Self {
        Self {
            buttons: vec![],
            texts: vec![],
        }
    }

    pub fn button(
        &mut self,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
        text: &str,
        event: GameEvent,
    ) -> &mut Button {
        self.buttons.push(Button {
            rect: Rect::new(x, y, width, height),
            color: Color::RGB(0, 120, 215),
            text: text.to_string(),
            event,
        });
        self.buttons.last_mut().unwrap()
    }

    pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
        self.texts.push(Text {
            text: text.to_string(),
            color,
            point,
        });
        self.texts.last_mut().unwrap()
    }

    pub fn clear(&mut self) {
        self.buttons.clear();
        self.texts.clear();
    }
}