-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdraw_nine_slice.rs
70 lines (59 loc) · 1.48 KB
/
draw_nine_slice.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
use notan::draw::*;
use notan::prelude::*;
#[derive(AppState)]
struct State {
img1: Texture,
img2: Texture,
count: f32,
multi: f32,
}
impl State {
pub fn count(&mut self, value: f32) {
if self.count >= 200.0 || self.count <= 0.0 {
self.multi *= -1.0;
}
self.count += value * self.multi;
}
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(init)
.add_config(DrawConfig)
.update(update)
.draw(draw)
.build()
}
fn init(gfx: &mut Graphics) -> State {
let texture1 = gfx
.create_texture()
.from_image(include_bytes!("assets/green_panel.png"))
.build()
.unwrap();
let texture2 = gfx
.create_texture()
.from_image(include_bytes!("assets/grey_button.png"))
.build()
.unwrap();
State {
img1: texture1,
img2: texture2,
count: 1.0,
multi: 1.0,
}
}
fn update(app: &mut App, state: &mut State) {
state.count(60.0 * app.timer.delta_f32());
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.nine_slice(&state.img1).position(50.0, 50.0).size(
state.img1.width() + state.count,
state.img1.height() + state.count,
);
draw.nine_slice(&state.img2).position(450.0, 50.0).size(
state.img2.width() + state.count,
state.img2.height() + state.count,
);
gfx.render(&draw);
}