repose_core/
indication.rs1use crate::{InteractionSource, Rect, Scene};
2
3pub trait Indication: std::fmt::Debug {}
5
6pub trait IndicationNodeFactory: Indication {
8 fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode>;
9}
10
11pub trait IndicationDrawNode {
14 fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32);
17}
18
19#[derive(Clone, Debug)]
21pub struct DebugIndication {
22 pub press_color: crate::Color,
23 pub hover_color: crate::Color,
24 pub focus_color: crate::Color,
25}
26
27impl Default for DebugIndication {
28 fn default() -> Self {
29 Self {
30 press_color: crate::Color(0, 0, 255, 40),
31 hover_color: crate::Color(0, 0, 255, 20),
32 focus_color: crate::Color(0, 0, 255, 10),
33 }
34 }
35}
36
37impl Indication for DebugIndication {}
38
39impl IndicationNodeFactory for DebugIndication {
40 fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode> {
41 Box::new(DebugIndicationDrawNode {
42 interaction_source: interaction_source.clone(),
43 press_color: self.press_color,
44 hover_color: self.hover_color,
45 focus_color: self.focus_color,
46 })
47 }
48}
49
50struct DebugIndicationDrawNode {
51 interaction_source: InteractionSource,
52 press_color: crate::Color,
53 hover_color: crate::Color,
54 focus_color: crate::Color,
55}
56
57impl IndicationDrawNode for DebugIndicationDrawNode {
58 fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32) {
59 let pressed = self.interaction_source.collect_is_pressed();
60 let hovered = self.interaction_source.collect_is_hovered();
61 let _focused = self.interaction_source.collect_is_focused();
62
63 if pressed {
65 scene.nodes.push(crate::SceneNode::Rect {
66 rect,
67 brush: self
68 .press_color
69 .with_alpha_f32(self.press_color.3 as f32 / 255.0 * alpha)
70 .into(),
71 radius: [0.0; 4],
72 });
73 }
74 if hovered && !pressed {
76 scene.nodes.push(crate::SceneNode::Rect {
77 rect,
78 brush: self
79 .hover_color
80 .with_alpha_f32(self.hover_color.3 as f32 / 255.0 * alpha)
81 .into(),
82 radius: [0.0; 4],
83 });
84 }
85 }
86}