embedded_menu/selection_indicator/style/
mod.rs1use embedded_graphics::{
2 prelude::DrawTarget,
3 primitives::{ContainsPoint, Rectangle},
4 transform::Transform,
5};
6
7use crate::{interaction::InputState, selection_indicator::Insets, theme::Theme};
8
9pub mod animated_triangle;
10pub mod border;
11pub mod line;
12pub mod rectangle;
13pub mod triangle;
14
15pub use animated_triangle::AnimatedTriangle;
17pub use border::Border;
18pub use line::Line;
19pub use triangle::Triangle;
20
21pub const fn interpolate(value: u32, x_min: u32, x_max: u32, y_min: u32, y_max: u32) -> u32 {
22 let x_range = x_max - x_min;
23 let y_range = y_max - y_min;
24
25 if x_range == 0 {
26 y_min
27 } else {
28 let x = value - x_min;
29 let y = x * y_range / x_range;
30
31 y + y_min
32 }
33}
34
35pub trait IndicatorStyle: Copy {
36 type Shape: ContainsPoint + Transform + Clone;
37 type State: Default + Copy;
38
39 fn on_target_changed(&self, _state: &mut Self::State) {}
40
41 fn update(&self, _state: &mut Self::State, _input_state: InputState) -> bool {
47 false
48 }
49 fn padding(&self, state: &Self::State, height: i32) -> Insets;
50 fn shape(&self, state: &Self::State, bounds: Rectangle, fill_width: u32) -> Self::Shape;
51 fn draw<T, D>(
52 &self,
53 state: &Self::State,
54 input_state: InputState,
55 theme: &T,
56 display: &mut D,
57 ) -> Result<Self::Shape, D::Error>
58 where
59 T: Theme,
60 D: DrawTarget<Color = T::Color>;
61}
62
63#[test]
64fn interpolate_basic() {
65 assert_eq!(interpolate(0, 0, 100, 0, 100), 0);
66 assert_eq!(interpolate(50, 0, 100, 0, 100), 50);
67 assert_eq!(interpolate(100, 0, 100, 0, 100), 100);
68 assert_eq!(interpolate(100, 0, 10, 0, 100), 1000);
69}