mouse_click_example/
mouse_click_example.rs

1use spottedcat::{Context, MouseButton, Pt, Spot, Text, DrawOption};
2
3struct MouseClickExample {
4    last_click: Option<(Pt, Pt)>,
5}
6
7impl Spot for MouseClickExample {
8    fn initialize(_: &mut Context) -> Self {
9        Self { last_click: None }
10    }
11
12    fn update(&mut self, ctx: &mut Context, _dt: std::time::Duration) {
13        if let Some((x, y)) = spottedcat::mouse_button_pressed_position(ctx, MouseButton::Left) {
14            self.last_click = Some((x, y));
15        }
16    }
17
18    fn draw(&mut self, context: &mut Context) {
19        const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
20        let font_data = spottedcat::load_font_from_bytes(FONT);
21
22        let mut title_opts = DrawOption::new();
23        title_opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(40.0)];
24        Text::new(
25            "Mouse Click Example (Left click to record position)",
26            font_data.clone(),
27        )
28        .with_font_size(spottedcat::Pt::from(24.0))
29        .with_color([1.0, 1.0, 1.0, 1.0])
30        .draw(context, title_opts);
31
32        let mut pos_opts = DrawOption::new();
33        pos_opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(90.0)];
34
35        let text = match self.last_click {
36            Some((x, y)) => format!("Last left click: ({:.1}, {:.1})", x.as_f32(), y.as_f32()),
37            None => "Last left click: (none)".to_string(),
38        };
39
40        Text::new(text, font_data)
41            .with_font_size(spottedcat::Pt::from(20.0))
42            .with_color([0.7, 0.9, 1.0, 1.0])
43            .draw(context, pos_opts);
44    }
45
46    fn remove(&self) {}
47}
48
49fn main() {
50    spottedcat::run::<MouseClickExample>(spottedcat::WindowConfig::default());
51}