-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathapp_drop_file.rs
98 lines (87 loc) · 2.39 KB
/
app_drop_file.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use notan::app::Event;
use notan::draw::*;
use notan::prelude::*;
#[derive(AppState)]
struct State {
font: Font,
dragging: usize,
asset: Option<Asset<Texture>>,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(DrawConfig)
.add_config(notan::log::LogConfig::new(notan::log::LevelFilter::Debug))
.draw(draw)
.event(event)
.build()
}
fn setup(gfx: &mut Graphics) -> State {
let font = gfx
.create_font(include_bytes!("assets/Ubuntu-B.ttf"))
.unwrap();
State {
font,
dragging: 0,
asset: None,
}
}
fn event(assets: &mut Assets, state: &mut State, evt: Event) {
match evt {
Event::DragEnter { .. } => {
state.dragging += 1;
}
Event::DragLeft => {
state.dragging = 0;
}
Event::Drop(file) => {
state.dragging = 0;
// Start loading the file if it's a png
if file.name.contains(".png") {
state.asset = Some(assets.load_dropped_file::<Texture>(&file).unwrap());
}
}
_ => {}
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
// Display the image if it's already loaded
if let Some(asset) = &state.asset {
if let Some(img) = asset.lock() {
draw.image(&img).position(30.0, 30.0).scale(0.5, 0.5);
}
}
// Just UI Text
if state.dragging == 0 {
let text = match &state.asset {
None => "Drop a PNG here",
Some(asset) => {
if asset.is_loaded() {
"Drop another PNG here"
} else {
"Loading..."
}
}
};
draw.text(&state.font, text)
.color(Color::ORANGE)
.size(30.0)
.v_align_middle()
.h_align_center()
.position(400.0, 300.0);
} else {
draw.rect((10.0, 10.0), (780.0, 580.0))
.color(Color::WHITE)
.stroke(6.0);
let text = format!("You're dragging {} files", state.dragging);
draw.text(&state.font, &text)
.size(30.0)
.color(Color::GRAY)
.v_align_middle()
.h_align_center()
.position(400.0, 300.0);
}
gfx.render(&draw);
}