input_example/
input_example.rs

1use spottedcat::{Context, Spot, Text, DrawOption, Key};
2
3struct InputExample {
4    x: f32,
5    y: f32,
6    speed: f32,
7}
8
9impl Spot for InputExample {
10    fn initialize(_: &mut Context) -> Self {
11        Self {
12            x: 200.0,
13            y: 200.0,
14            speed: 240.0,
15        }
16    }
17
18    fn update(&mut self, ctx: &mut Context, dt: std::time::Duration) {
19        let dt = dt.as_secs_f32();
20
21        if spottedcat::key_down(ctx, Key::W) || spottedcat::key_down(ctx, Key::Up) {
22            self.y -= self.speed * dt;
23        }
24        if spottedcat::key_down(ctx, Key::S) || spottedcat::key_down(ctx, Key::Down) {
25            self.y += self.speed * dt;
26        }
27        if spottedcat::key_down(ctx, Key::A) || spottedcat::key_down(ctx, Key::Left) {
28            self.x -= self.speed * dt;
29        }
30        if spottedcat::key_down(ctx, Key::D) || spottedcat::key_down(ctx, Key::Right) {
31            self.x += self.speed * dt;
32        }
33
34        if spottedcat::key_pressed(ctx, Key::Escape) {
35            self.x = 200.0;
36            self.y = 200.0;
37        }
38    }
39
40    fn draw(&mut self, context: &mut Context) {
41        const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
42        let font_data = spottedcat::load_font_from_bytes(FONT);
43
44        let mut opts = DrawOption::new();
45        opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(40.0)];
46        Text::new(
47            "Input Example (WASD / Arrow Keys to move, ESC to reset)",
48            font_data.clone(),
49        )
50        .with_font_size(spottedcat::Pt::from(24.0))
51        .with_color([1.0, 1.0, 1.0, 1.0])
52        .draw(context, opts);
53
54        let mut opts = DrawOption::new();
55        opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(80.0)];
56        Text::new(format!("Position: ({:.1}, {:.1})", self.x, self.y), font_data.clone())
57            .with_font_size(spottedcat::Pt::from(20.0))
58            .with_color([0.7, 0.9, 1.0, 1.0])
59            .draw(context, opts);
60
61        let mut opts = DrawOption::new();
62        opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(120.0)];
63        Text::new(
64            "Tip: hold keys for continuous movement; press ESC to reset.",
65            font_data,
66        )
67        .with_font_size(spottedcat::Pt::from(18.0))
68        .with_color([0.9, 0.9, 0.9, 1.0])
69        .draw(context, opts);
70    }
71
72    fn remove(&self) {}
73}
74
75fn main() {
76    spottedcat::run::<InputExample>(spottedcat::WindowConfig::default());
77}