-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathinput_keyboard.rs
75 lines (60 loc) · 1.58 KB
/
input_keyboard.rs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use notan::draw::*;
use notan::prelude::*;
const MOVE_SPEED: f32 = 100.0;
#[derive(AppState)]
struct State {
font: Font,
x: f32,
y: f32,
last_key: Option<KeyCode>,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(DrawConfig)
.update(update)
.draw(draw)
.build()
}
fn setup(gfx: &mut Graphics) -> State {
let font = gfx
.create_font(include_bytes!("assets/Ubuntu-B.ttf"))
.unwrap();
State {
font,
x: 400.0,
y: 300.0,
last_key: None,
}
}
fn update(app: &mut App, state: &mut State) {
state.last_key = app.keyboard.last_key_released();
if app.keyboard.is_down(KeyCode::W) {
state.y -= MOVE_SPEED * app.timer.delta_f32();
}
if app.keyboard.is_down(KeyCode::A) {
state.x -= MOVE_SPEED * app.timer.delta_f32();
}
if app.keyboard.is_down(KeyCode::S) {
state.y += MOVE_SPEED * app.timer.delta_f32();
}
if app.keyboard.is_down(KeyCode::D) {
state.x += MOVE_SPEED * app.timer.delta_f32();
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.circle(50.0)
.position(state.x, state.y)
.color(Color::RED);
draw.text(&state.font, "Use WASD to move the circle")
.position(10.0, 10.0)
.size(20.0);
if let Some(key) = &state.last_key {
draw.text(&state.font, &format!("Last key: {key:?}"))
.position(10.0, 560.0)
.size(20.0);
}
gfx.render(&draw);
}