rive_rs/shapes/paint/
trim_path.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
143
144
145
146
147
148
149
150
151
// 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::mem;
use std::rc::Rc;

use crate::component::Component;
use crate::component_dirt::ComponentDirt;
use crate::core::{Core, CoreContext, ObjectRef, OnAdded, Property};
use crate::option_cell::OptionCell;
use crate::shapes::paint::stroke_effect::StrokeEffect;
use crate::shapes::paint::Stroke;
use crate::shapes::{CommandPath, CommandPathBuilder, MetricsPath};
use crate::status_code::StatusCode;

#[derive(Debug, Default)]
pub struct TrimPath {
    component: Component,
    start: Property<f32>,
    end: Property<f32>,
    offset: Property<f32>,
    mode_value: Property<u64>,
    command_path: OptionCell<Rc<CommandPath>>,
}

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

    pub fn set_start(&self, start: f32) {
        self.start.set(start);
        self.invalidate_effect();
    }

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

    pub fn set_end(&self, end: f32) {
        self.end.set(end);
        self.invalidate_effect();
    }

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

    pub fn set_offset(&self, offset: f32) {
        self.offset.set(offset);
        self.invalidate_effect();
    }

    pub fn mode_value(&self) -> u64 {
        self.mode_value.get()
    }

    pub fn set_mode_value(&self, mode_value: u64) {
        self.mode_value.set(mode_value);
        self.invalidate_effect();
    }
}

impl StrokeEffect for ObjectRef<'_, TrimPath> {
    fn effect_path(&self, source: &mut MetricsPath) -> Rc<CommandPath> {
        if let Some(command_path) = self.command_path.get() {
            return command_path;
        }

        let mut render_offset = self.offset().fract();
        if render_offset.is_sign_negative() {
            render_offset += 1.0;
        }

        // todo!("implement mode 2");

        let total_len = source.compute_length();
        let mut start_len = total_len * (self.start() + render_offset);
        let mut end_len = total_len * (self.end() + render_offset);

        if end_len < start_len {
            mem::swap(&mut start_len, &mut end_len);
        }

        if start_len > total_len {
            start_len -= total_len;
            end_len -= total_len;
        }

        let mut builder = CommandPathBuilder::new();

        while end_len > 0.0 {
            if start_len < total_len {
                source.trimmed(&mut builder, start_len, end_len, true);
                end_len -= total_len;
                start_len = 0.0;
            } else {
                start_len -= total_len;
                end_len -= total_len;
            }
        }

        let command_path = Rc::new(builder.build());
        self.command_path.set(Some(command_path.clone()));

        command_path
    }

    fn invalidate_effect(&self) {
        self.command_path.set(None);
        let stroke = self.cast::<Component>().parent().unwrap().cast::<Stroke>();

        stroke.as_ref().outlined_stroke.set(None);
        stroke
            .cast::<Component>()
            .as_ref()
            .parent()
            .unwrap()
            .cast::<Component>()
            .as_ref()
            .add_dirt(ComponentDirt::PAINT, false);
    }
}

impl Core for TrimPath {
    parent_types![(component, Component)];

    properties![
        (114, start, set_start),
        (115, end, set_end),
        (116, offset, set_offset),
        (117, mode_value, set_mode_value),
        component,
    ];
}

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

    fn on_added_clean(&self, _context: &dyn CoreContext) -> StatusCode {
        if let Some(stroke) =
            self.cast::<Component>().parent().and_then(|parent| parent.try_cast::<Stroke>())
        {
            stroke.as_ref().set_stroke_effect(Some(self.as_object().into()));
            StatusCode::Ok
        } else {
            StatusCode::InvalidObject
        }
    }
}