gemini_engine/containers/
visibility_toggle.rs

1use crate::core::CanDraw;
2
3use super::CanCollide;
4
5/// `VisibilityToggle` is a container for a [`CanDraw`] with a property `visible`. When drawn to a `Canvas` the contained element will only appear if `visible` is `true`
6#[derive(Debug, Clone)]
7pub struct VisibilityToggle<E: CanDraw> {
8    /// The element held by the `VisibilityToggle`. Must implement [`CanDraw`]
9    pub element: E,
10    /// Whether the element is visible
11    pub visible: bool,
12}
13
14impl<E: CanDraw> VisibilityToggle<E> {
15    /// Creates a new `VisibilityToggle` with the visibility set to true
16    pub const fn new(element: E) -> Self {
17        Self {
18            element,
19            visible: true,
20        }
21    }
22}
23
24impl<E: CanDraw> CanDraw for VisibilityToggle<E> {
25    fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
26        if self.visible {
27            self.element.draw_to(canvas);
28        }
29    }
30}
31
32impl<E: CanDraw + CanCollide> CanCollide for VisibilityToggle<E> {
33    fn collides_with_pos(&self, pos: crate::core::Vec2D) -> bool {
34        if self.visible {
35            self.element.collides_with_pos(pos)
36        } else {
37            false
38        }
39    }
40}