rive_rs/shapes/
shape.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
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
// 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 std::rc::Rc;

use crate::component::Component;
use crate::component_dirt::ComponentDirt;
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded};
use crate::drawable::Drawable;
use crate::dyn_vec::DynVec;
use crate::math::Mat;
use crate::shapes::{Path, PathComposer, PathSpace, ShapePaintContainer};
use crate::status_code::StatusCode;
use crate::transform_component::TransformComponent;
use crate::Renderer;

#[derive(Debug, Default)]
pub struct Shape {
    drawable: Drawable,
    shape_paint_container: ShapePaintContainer,
    path_composer: Rc<PathComposer>,
    paths: DynVec<Object<Path>>,
    want_difference_path: Cell<bool>,
}

impl ObjectRef<'_, Shape> {
    pub fn paths(&self) -> impl Iterator<Item = Object<Path>> + '_ {
        self.paths.iter()
    }

    pub fn want_difference_path(&self) -> bool {
        self.want_difference_path.get()
    }

    pub fn push_path(&self, path: Object<Path>) {
        self.paths.push(path);
    }

    pub fn path_space(&self) -> PathSpace {
        self.cast::<ShapePaintContainer>().path_space()
    }

    pub fn path_changed(&self) {
        self.path_composer().cast::<Component>().add_dirt(ComponentDirt::PATH, true);

        self.cast::<ShapePaintContainer>().invalidate_stroke_effects();
    }

    pub fn path_composer(&self) -> ObjectRef<'_, PathComposer> {
        ObjectRef::from(Rc::clone(&self.path_composer))
    }

    pub fn draw(&self, renderer: &mut impl Renderer, transform: Mat) {
        let mut is_clipped = false;
        if let Some(path) = self.cast::<Drawable>().clip() {
            is_clipped = true;

            let layers = self
                .cast::<ShapePaintContainer>()
                .shape_paints()
                .filter(|shape_paint| shape_paint.as_ref().is_visible())
                .count();

            renderer.clip(&path, transform, layers);
        }

        let path_composer = self.path_composer();

        for shape_paint in self.cast::<ShapePaintContainer>().shape_paints() {
            let shape_paint = shape_paint.as_ref();

            if !shape_paint.is_visible() {
                continue;
            }

            shape_paint.set_is_clipped(is_clipped);

            if shape_paint.path_space() & PathSpace::LOCAL == PathSpace::LOCAL {
                let transform = transform * self.cast::<TransformComponent>().world_transform();
                path_composer.with_local_path(|path| {
                    shape_paint.draw(
                        renderer,
                        path.expect("local_path should already be set on PathComposer"),
                        transform,
                    );
                });
            } else {
                path_composer.with_world_path(|path| {
                    shape_paint.draw(
                        renderer,
                        path.expect("world_path should already be set on PathComposer"),
                        transform,
                    );
                });
            }
        }
    }

    pub fn build_dependencies(&self) {
        self.path_composer().cast::<Component>().build_dependencies();

        self.cast::<TransformComponent>().build_dependencies();

        // Set the blend mode on all the shape paints. If we ever animate this
        // property, we'll need to update it in the update cycle/mark dirty when the
        // blend mode changes.
        for paint in self.cast::<ShapePaintContainer>().shape_paints() {
            paint.as_ref().set_blend_mode(self.cast::<Drawable>().blend_mode());
        }
    }

    pub fn update(&self, value: ComponentDirt) {
        self.cast::<TransformComponent>().update(value);

        if Component::value_has_dirt(value, ComponentDirt::RENDER_OPACITY) {
            for paint in self.cast::<ShapePaintContainer>().shape_paints() {
                paint
                    .as_ref()
                    .set_render_opacity(self.cast::<TransformComponent>().render_opacity());
            }
        }
    }
}

impl Core for Shape {
    parent_types![(drawable, Drawable), (shape_paint_container, ShapePaintContainer)];

    properties!(drawable);
}

impl OnAdded for ObjectRef<'_, Shape> {
    on_added!([on_added_clean, import], Drawable);

    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
        self.path_composer.shape.set(Some(self.as_object()));

        self.cast::<TransformComponent>().on_added_dirty(context);
        ObjectRef::from(Rc::clone(&self.path_composer)).on_added_dirty(context)
    }
}