-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathinput_keyboard_char.rs
63 lines (53 loc) · 1.26 KB
/
input_keyboard_char.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
use notan::app::Event;
use notan::draw::*;
use notan::prelude::*;
#[derive(AppState)]
struct State {
font: Font,
msg: String,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(DrawConfig)
.event(event)
.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,
msg: String::from(""),
}
}
fn event(state: &mut State, event: Event) {
match event {
Event::ReceivedCharacter(c) if c != '\u{7f}' => {
state.msg.push(c);
}
_ => {}
}
}
fn update(app: &mut App, state: &mut State) {
if app.keyboard.was_pressed(KeyCode::Back) && !state.msg.is_empty() {
state.msg.pop();
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.text(&state.font, "Type anything:")
.position(10.0, 10.0)
.color(Color::YELLOW)
.size(20.0);
draw.text(&state.font, &state.msg)
.position(20.0, 50.0)
.max_width(760.0)
.color(Color::WHITE)
.size(20.0);
gfx.render(&draw);
}