ori_core/views/
transform.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
use ori_macro::example;

use crate::{
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Affine, Size, Space, Vector},
    rebuild::Rebuild,
    view::{Pod, PodState, View},
};

/// Create a new [`Transform`] view.
pub fn transform<V>(transform: Affine, view: V) -> Transform<V> {
    Transform::new(transform, view)
}

/// Create a new [`Transform`] view that translates its content.
pub fn translate<V>(translation: impl Into<Vector>, view: V) -> Transform<V> {
    Transform::new(Affine::translate(translation.into()), view)
}

/// Create a new [`Transform`] view that rotates its content.
pub fn rotate<V>(degrees: f32, view: V) -> Transform<V> {
    Transform::new(Affine::rotate(degrees), view)
}

/// Create a new [`Transform`] view that rotates its content in degrees.
pub fn rotate_degrees<V>(degrees: f32, view: V) -> Transform<V> {
    rotate(degrees.to_radians(), view)
}

/// Create a new [`Transform`] view that scales its content.
pub fn scale<V>(scale: impl Into<Vector>, view: V) -> Transform<V> {
    Transform::new(Affine::scale(scale.into()), view)
}

/// A view that transforms its content.
#[example(name = "transform", width = 400, height = 300)]
#[derive(Rebuild)]
pub struct Transform<V> {
    /// The content.
    pub content: Pod<V>,
    /// The transform.
    #[rebuild(layout)]
    pub transform: Affine,
}

impl<V> Transform<V> {
    /// Create a new [`Transform`] view.
    pub fn new(transform: Affine, content: V) -> Self {
        Self {
            content: Pod::new(content),
            transform,
        }
    }
}

impl<T, V: View<T>> View<T> for Transform<V> {
    type State = PodState<T, V>;

    fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
        self.content.build(cx, data)
    }

    fn rebuild(&mut self, state: &mut Self::State, cx: &mut RebuildCx, data: &mut T, old: &Self) {
        Rebuild::rebuild(self, cx, old);

        self.content.rebuild(state, cx, data, &old.content);
    }

    fn event(
        &mut self,
        state: &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        self.content.event(state, cx, data, event)
    }

    fn layout(
        &mut self,
        state: &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        let size = self.content.layout(state, cx, data, space);
        let center = Affine::translate(size.to_vector() / 2.0);
        state.set_transform(center * self.transform * center.inverse());

        size
    }

    fn draw(&mut self, state: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        self.content.draw(state, cx, data);
    }
}