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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::cell::Cell;

use crate::{
    bones::{Bone, RootBone},
    component_dirt::ComponentDirt,
    container_component::ContainerComponent,
    core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property},
    math::{self, Mat},
    node::Node,
    option_cell::OptionCell,
    Component, StatusCode,
};

#[derive(Debug)]
pub struct TransformComponent {
    container_component: ContainerComponent,
    rotation: Property<f32>,
    scale_x: Property<f32>,
    scale_y: Property<f32>,
    opacity: Property<f32>,
    transform: Cell<Mat>,
    world_transform: Cell<Mat>,
    render_opacity: Cell<f32>,
    parent_transform_component: OptionCell<Object<Self>>,
}

impl ObjectRef<'_, TransformComponent> {
    pub fn rotation(&self) -> f32 {
        self.rotation.get()
    }

    pub fn set_rotation(&self, rotation: f32) {
        if self.rotation() == rotation {
            return;
        }

        self.rotation.set(rotation);
        self.mark_transform_dirty();
    }

    pub fn scale_x(&self) -> f32 {
        self.scale_x.get()
    }

    pub fn set_scale_x(&self, scale_x: f32) {
        if self.scale_x() == scale_x {
            return;
        }

        self.scale_x.set(scale_x);
        self.mark_transform_dirty();
    }

    pub fn scale_y(&self) -> f32 {
        self.scale_y.get()
    }

    pub fn set_scale_y(&self, scale_y: f32) {
        if self.scale_y() == scale_y {
            return;
        }

        self.scale_y.set(scale_y);
        self.mark_transform_dirty();
    }

    pub fn opacity(&self) -> f32 {
        self.opacity.get()
    }

    pub fn set_opacity(&self, opacity: f32) {
        if self.opacity() == opacity {
            return;
        }

        self.opacity.set(opacity);
        self.cast::<Component>().add_dirt(ComponentDirt::RENDER_OPACITY, true);
    }
}

impl ObjectRef<'_, TransformComponent> {
    pub fn render_opacity(&self) -> f32 {
        self.render_opacity.get()
    }

    pub fn world_transform(&self) -> Mat {
        self.world_transform.get()
    }

    pub fn mark_transform_dirty(&self) {
        if !self.cast::<Component>().add_dirt(ComponentDirt::TRANSFORM, false) {
            return;
        }

        self.mark_world_transform_dirty();
    }

    pub fn mark_world_transform_dirty(&self) {
        self.cast::<Component>().add_dirt(ComponentDirt::WORLD_TRANSFORM, true);
    }

    fn x(&self) -> f32 {
        if let Some(root_bone) = self.try_cast::<RootBone>() {
            return root_bone.x();
        }

        if let Some(bone) = self.try_cast::<Bone>() {
            return bone.x();
        }

        if let Some(node) = self.try_cast::<Node>() {
            return node.x();
        }

        unreachable!()
    }

    fn y(&self) -> f32 {
        if let Some(root_bone) = self.try_cast::<RootBone>() {
            return root_bone.y();
        }

        if let Some(bone) = self.try_cast::<Bone>() {
            return bone.y();
        }

        if let Some(node) = self.try_cast::<Node>() {
            return node.y();
        }

        unreachable!()
    }

    pub fn update_transform(&self) {
        let mut transform = if self.rotation() != 0.0 {
            Mat::from_rotation(self.rotation())
        } else {
            Mat::default()
        };

        transform.translate_x = self.x();
        transform.translate_y = self.y();

        transform = transform.scale(math::Vec::new(self.scale_x(), self.scale_y()));

        self.transform.set(transform);
    }

    pub fn update_world_transform(&self) {
        if let Some(parent_transform_component) = self.parent_transform_component.get() {
            let parent_transform = parent_transform_component.as_ref().world_transform.get();
            self.world_transform.set(parent_transform * self.transform.get());
        } else {
            self.world_transform.set(self.transform.get());
        }
    }

    pub fn build_dependencies(&self) {
        let component = self.cast::<Component>();
        if let Some(parent) = component.parent() {
            parent.cast::<Component>().as_ref().push_dependent(component.as_object());
        }
    }

    pub fn update(&self, value: ComponentDirt) {
        if Component::value_has_dirt(value, ComponentDirt::TRANSFORM) {
            self.update_transform();
        }

        if Component::value_has_dirt(value, ComponentDirt::WORLD_TRANSFORM) {
            self.update_world_transform();
        }

        if Component::value_has_dirt(value, ComponentDirt::RENDER_OPACITY) {
            self.render_opacity.set(self.opacity());

            if let Some(parent_transform_component) = self.parent_transform_component.get() {
                self.render_opacity.set(
                    self.render_opacity() * parent_transform_component.as_ref().render_opacity(),
                );
            }
        }
    }
}

impl Core for TransformComponent {
    parent_types![(container_component, ContainerComponent)];

    properties![
        (15, rotation, set_rotation),
        (16, scale_x, set_scale_x),
        (17, scale_y, set_scale_y),
        (18, opacity, set_opacity),
        container_component,
    ];
}

impl OnAdded for ObjectRef<'_, TransformComponent> {
    on_added!([on_added_dirty, import], ContainerComponent);

    fn on_added_clean(&self, _context: &dyn CoreContext) -> StatusCode {
        let parent = self.cast::<Component>().parent().and_then(|parent| parent.try_cast());
        self.parent_transform_component.set(parent);

        StatusCode::Ok
    }
}

impl Default for TransformComponent {
    fn default() -> Self {
        Self {
            container_component: ContainerComponent::default(),
            rotation: Property::new(0.0),
            scale_x: Property::new(1.0),
            scale_y: Property::new(1.0),
            opacity: Property::new(1.0),
            transform: Cell::new(Mat::default()),
            world_transform: Cell::new(Mat::default()),
            render_opacity: Cell::new(0.0),
            parent_transform_component: OptionCell::new(),
        }
    }
}