rive_rs/shapes/paint/
solid_color.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
// 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 crate::component::Component;
use crate::core::{Core, CoreContext, ObjectRef, OnAdded, Property};
use crate::shapes::paint::{Color32, ShapePaintMutator};
use crate::status_code::StatusCode;
use crate::PaintColor;

#[derive(Debug)]
pub struct SolidColor {
    component: Component,
    shape_paint_mutator: ShapePaintMutator,
    color_value: Property<Color32>,
}

impl ObjectRef<'_, SolidColor> {
    pub fn color_value(&self) -> Color32 {
        self.color_value.get()
    }

    pub fn set_color_value(&self, color_value: Color32) {
        self.color_value.set(color_value);
        self.render_opacity_changed();
    }
}

impl ObjectRef<'_, SolidColor> {
    pub(crate) fn render_opacity_changed(&self) {
        let mutator = self.cast::<ShapePaintMutator>();
        mutator.render_paint().borrow_mut().color =
            PaintColor::Solid(self.color_value().mul_opacity(mutator.render_opacity()));
    }
}

impl Core for SolidColor {
    parent_types![(component, Component), (shape_paint_mutator, ShapePaintMutator)];

    properties![(37, color_value, set_color_value), component];
}

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

    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
        let component = self.cast::<Component>();

        let code = component.on_added_dirty(context);
        if code != StatusCode::Ok {
            return code;
        }

        let mutator = self.cast::<ShapePaintMutator>();
        if let Some(parent) = component.parent() {
            if mutator.init_paint_mutator(parent.cast()) {
                self.render_opacity_changed();
                return StatusCode::Ok;
            }
        }

        StatusCode::MissingObject
    }
}

impl Default for SolidColor {
    fn default() -> Self {
        Self {
            component: Component::default(),
            shape_paint_mutator: ShapePaintMutator::default(),
            color_value: Property::new(Color32::new(0xFF74_7474)),
        }
    }
}