Skip to main content

radiantkit_core/interactions/
interaction_manager.rs

1use crate::{BoundingBoxInteraction, RadiantNode, RadiantSceneMessage, ScreenDescriptor};
2use epaint::ClippedPrimitive;
3
4pub struct RadiantInteractionManager<M> {
5    pub bounding_box_interaction: BoundingBoxInteraction,
6    _phantom: std::marker::PhantomData<M>,
7}
8
9impl<M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage>> RadiantInteractionManager<M> {
10    pub fn new() -> Self {
11        Self {
12            bounding_box_interaction: BoundingBoxInteraction::new(),
13            _phantom: std::marker::PhantomData,
14        }
15    }
16
17    pub fn is_interaction(&self, id: u64) -> bool {
18        self.bounding_box_interaction.contains(id)
19    }
20
21    pub fn enable_interactions(
22        &mut self,
23        node: &impl RadiantNode,
24        screen_descriptor: &ScreenDescriptor,
25    ) {
26        self.bounding_box_interaction
27            .enable(node, screen_descriptor);
28    }
29
30    pub fn disable_interactions(&mut self) {
31        self.bounding_box_interaction.disable();
32    }
33
34    pub fn update_interactions(
35        &mut self,
36        node: &impl RadiantNode,
37        screen_descriptor: &ScreenDescriptor,
38    ) {
39        self.bounding_box_interaction
40            .update(node, screen_descriptor);
41    }
42
43    pub fn handle_interaction(&mut self, message: M) -> Option<M> {
44        match message.try_into() {
45            Ok(RadiantSceneMessage::TransformNode { id, position, .. })
46                if self.is_interaction(id) =>
47            {
48                if let Some(m) = self.bounding_box_interaction.handle(id, position) {
49                    Some(m.into())
50                } else {
51                    None
52                }
53            }
54            _ => None,
55        }
56    }
57
58    pub fn tessellate(
59        &mut self,
60        selection: bool,
61        screen_descriptor: &ScreenDescriptor,
62        fonts_manager: &epaint::text::Fonts,
63    ) -> Vec<ClippedPrimitive> {
64        self.bounding_box_interaction
65            .tessellate(selection, screen_descriptor, fonts_manager)
66    }
67}