rive_rs/shapes/paint/
solid_color.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 crate::component::Component;
6use crate::core::{Core, CoreContext, ObjectRef, OnAdded, Property};
7use crate::shapes::paint::{Color32, ShapePaintMutator};
8use crate::status_code::StatusCode;
9use crate::PaintColor;
10
11#[derive(Debug)]
12pub struct SolidColor {
13    component: Component,
14    shape_paint_mutator: ShapePaintMutator,
15    color_value: Property<Color32>,
16}
17
18impl ObjectRef<'_, SolidColor> {
19    pub fn color_value(&self) -> Color32 {
20        self.color_value.get()
21    }
22
23    pub fn set_color_value(&self, color_value: Color32) {
24        self.color_value.set(color_value);
25        self.render_opacity_changed();
26    }
27}
28
29impl ObjectRef<'_, SolidColor> {
30    pub(crate) fn render_opacity_changed(&self) {
31        let mutator = self.cast::<ShapePaintMutator>();
32        mutator.render_paint().borrow_mut().color =
33            PaintColor::Solid(self.color_value().mul_opacity(mutator.render_opacity()));
34    }
35}
36
37impl Core for SolidColor {
38    parent_types![(component, Component), (shape_paint_mutator, ShapePaintMutator)];
39
40    properties![(37, color_value, set_color_value), component];
41}
42
43impl OnAdded for ObjectRef<'_, SolidColor> {
44    on_added!([on_added_clean, import], Component);
45
46    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
47        let component = self.cast::<Component>();
48
49        let code = component.on_added_dirty(context);
50        if code != StatusCode::Ok {
51            return code;
52        }
53
54        let mutator = self.cast::<ShapePaintMutator>();
55        if let Some(parent) = component.parent() {
56            if mutator.init_paint_mutator(parent.cast()) {
57                self.render_opacity_changed();
58                return StatusCode::Ok;
59            }
60        }
61
62        StatusCode::MissingObject
63    }
64}
65
66impl Default for SolidColor {
67    fn default() -> Self {
68        Self {
69            component: Component::default(),
70            shape_paint_mutator: ShapePaintMutator::default(),
71            color_value: Property::new(Color32::new(0xFF74_7474)),
72        }
73    }
74}