-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathanimation.rs
109 lines (93 loc) · 3.64 KB
/
animation.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
99
100
101
102
103
104
105
106
107
108
109
use cstr_core::CString;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl;
use lvgl::input_device::{
pointer::{Pointer, PointerInputData},
InputDriver,
};
use lvgl::misc::anim::{AnimRepeatCount, Animation};
use lvgl::style::Style;
use lvgl::widgets::{Btn, Label};
use lvgl::{Align, Color, Display, DrawBuffer, LvError, Part, Widget};
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;
#[allow(unused_assignments)]
fn main() -> Result<(), LvError> {
const HOR_RES: u32 = 240;
const VER_RES: u32 = 240;
let mut sim_display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(HOR_RES, VER_RES));
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Button Example", &output_settings);
let buffer = DrawBuffer::<{ (HOR_RES * VER_RES) as usize }>::default();
let display = Display::register(buffer, HOR_RES, VER_RES, |refresh| {
sim_display.draw_iter(refresh.as_pixels()).unwrap();
})?;
// Define the initial state of your input
let mut latest_touch_status = PointerInputData::Touch(Point::new(0, 0)).released().once();
// Register a new input device that's capable of reading the current state of the input
let _touch_screen = Pointer::register(|| latest_touch_status, &display)?;
// Create screen and widgets
let mut screen = display.get_scr_act()?;
let mut screen_style = Style::default();
screen_style.set_bg_color(Color::from_rgb((0, 0, 0)));
screen.add_style(Part::Main, &mut screen_style);
// Create the button
let mut button = Btn::create(&mut screen)?;
button.set_align(Align::LeftMid, 30, 0);
button.set_size(180, 80);
let mut btn_lbl = Label::create(&mut button)?;
btn_lbl.set_text(CString::new("Click me!").unwrap().as_c_str())?;
let mut btn_state = false;
let mut anim = Animation::new(&mut button, Duration::from_secs(1), 0, 60, |obj, val| {
obj.set_align(Align::LeftMid, val, 0)
})?;
anim.set_repeat_count(AnimRepeatCount::Infinite);
anim.start();
button.on_event(|_btn, event| {
println!("Button received event: {:?}", event);
if let lvgl::Event::Clicked = event {
if btn_state {
let nt = CString::new("Click me!").unwrap();
btn_lbl.set_text(nt.as_c_str()).unwrap();
} else {
let nt = CString::new("Clicked!").unwrap();
btn_lbl.set_text(nt.as_c_str()).unwrap();
}
btn_state = !btn_state;
}
})?;
'running: loop {
let start = Instant::now();
lvgl::task_handler();
window.update(&sim_display);
let events = window.events().peekable();
for event in events {
match event {
SimulatorEvent::MouseButtonDown {
mouse_btn: _,
point,
} => {
println!("Clicked on: {:?}", point);
latest_touch_status = PointerInputData::Touch(point).pressed().once();
}
SimulatorEvent::MouseButtonUp {
mouse_btn: _,
point,
} => {
latest_touch_status = PointerInputData::Touch(point).released().once();
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
sleep(Duration::from_millis(5));
lvgl::tick_inc(Instant::now().duration_since(start));
}
Ok(())
}