rive_rs/shapes/paint/
fill.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
// 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::RefCell;
use std::rc::Rc;

use crate::core::{Core, Object, ObjectRef, OnAdded, Property};
use crate::math::Mat;
use crate::renderer::Style;
use crate::shapes::paint::{ShapePaint, ShapePaintMutator};
use crate::shapes::path_space::PathSpace;
use crate::shapes::{CommandPath, FillRule};
use crate::{RenderPaint, Renderer};

#[derive(Debug, Default)]
pub struct Fill {
    shape_paint: ShapePaint,
    fill_rule: Property<FillRule>,
}

impl ObjectRef<'_, Fill> {
    pub fn fill_rule(&self) -> FillRule {
        self.fill_rule.get()
    }

    pub fn set_fill_rule(&self, fill_rule: FillRule) {
        self.fill_rule.set(fill_rule);
    }
}

impl ObjectRef<'_, Fill> {
    pub fn init_render_paint(
        &self,
        mutator: Object<ShapePaintMutator>,
    ) -> Option<Rc<RefCell<RenderPaint>>> {
        let render_paint = self.cast::<ShapePaint>().init_render_paint(mutator).unwrap();
        render_paint.borrow_mut().style = Style::Fill;

        Some(render_paint)
    }

    pub fn path_space(&self) -> PathSpace {
        PathSpace::LOCAL
    }

    pub fn draw(&self, renderer: &mut impl Renderer, path: &CommandPath, transform: Mat) {
        if !self.cast::<ShapePaint>().is_visible() {
            return;
        }

        let render_paint = self.cast::<ShapePaint>().render_paint();
        render_paint.borrow_mut().fill_rule = self.fill_rule();

        renderer.draw(path, transform, &*render_paint.borrow());
    }
}

impl Core for Fill {
    parent_types![(shape_paint, ShapePaint)];

    properties![(40, fill_rule, set_fill_rule), shape_paint];
}

impl OnAdded for ObjectRef<'_, Fill> {
    on_added!(ShapePaint);
}