rive_rs/shapes/paint/
fill.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::cell::RefCell;
6use std::rc::Rc;
7
8use crate::core::{Core, Object, ObjectRef, OnAdded, Property};
9use crate::math::Mat;
10use crate::renderer::Style;
11use crate::shapes::paint::{ShapePaint, ShapePaintMutator};
12use crate::shapes::path_space::PathSpace;
13use crate::shapes::{CommandPath, FillRule};
14use crate::{RenderPaint, Renderer};
15
16#[derive(Debug, Default)]
17pub struct Fill {
18    shape_paint: ShapePaint,
19    fill_rule: Property<FillRule>,
20}
21
22impl ObjectRef<'_, Fill> {
23    pub fn fill_rule(&self) -> FillRule {
24        self.fill_rule.get()
25    }
26
27    pub fn set_fill_rule(&self, fill_rule: FillRule) {
28        self.fill_rule.set(fill_rule);
29    }
30}
31
32impl ObjectRef<'_, Fill> {
33    pub fn init_render_paint(
34        &self,
35        mutator: Object<ShapePaintMutator>,
36    ) -> Option<Rc<RefCell<RenderPaint>>> {
37        let render_paint = self.cast::<ShapePaint>().init_render_paint(mutator).unwrap();
38        render_paint.borrow_mut().style = Style::Fill;
39
40        Some(render_paint)
41    }
42
43    pub fn path_space(&self) -> PathSpace {
44        PathSpace::LOCAL
45    }
46
47    pub fn draw(&self, renderer: &mut impl Renderer, path: &CommandPath, transform: Mat) {
48        if !self.cast::<ShapePaint>().is_visible() {
49            return;
50        }
51
52        let render_paint = self.cast::<ShapePaint>().render_paint();
53        render_paint.borrow_mut().fill_rule = self.fill_rule();
54
55        renderer.draw(path, transform, &*render_paint.borrow());
56    }
57}
58
59impl Core for Fill {
60    parent_types![(shape_paint, ShapePaint)];
61
62    properties![(40, fill_rule, set_fill_rule), shape_paint];
63}
64
65impl OnAdded for ObjectRef<'_, Fill> {
66    on_added!(ShapePaint);
67}