-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
file_drop.rs
48 lines (38 loc) 路 1.21 KB
/
file_drop.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
use std::fs;
use tetra::graphics::text::{Font, Text};
use tetra::graphics::{self, Color};
use tetra::math::Vec2;
use tetra::{Context, ContextBuilder, Event, State, TetraError};
struct GameState {
file: Text,
}
impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
Ok(GameState {
file: Text::new(
"Drop a file onto this window to view the contents.",
Font::vector(ctx, "./examples/resources/DejaVuSansMono.ttf", 16.0)?,
),
})
}
}
impl State for GameState {
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));
self.file.draw(ctx, Vec2::new(16.0, 16.0));
Ok(())
}
fn event(&mut self, _: &mut Context, event: Event) -> tetra::Result {
if let Event::FileDropped { path } = event {
let new_content = fs::read_to_string(&path)
.map_err(|e| TetraError::FailedToLoadAsset { reason: e, path })?;
self.file.set_content(new_content);
}
Ok(())
}
}
fn main() -> tetra::Result {
ContextBuilder::new("File Dropping", 1280, 720)
.build()?
.run(GameState::new)
}