rive_rs/shapes/paint/
gradient_stop.rs
1use crate::component::Component;
6use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
7use crate::shapes::paint::{Color32, LinearGradient};
8use crate::status_code::StatusCode;
9
10#[derive(Debug)]
11pub struct GradientStop {
12 component: Component,
13 color: Property<Color32>,
14 position: Property<f32>,
15}
16
17impl ObjectRef<'_, GradientStop> {
18 pub fn color(&self) -> Color32 {
19 self.color.get()
20 }
21
22 pub fn set_color(&self, color: Color32) {
23 if self.color() == color {
24 return;
25 }
26
27 self.color.set(color);
28 self.linear_gradient().as_ref().mark_gradient_dirty();
29 }
30
31 pub fn position(&self) -> f32 {
32 self.position.get()
33 }
34
35 pub fn set_position(&self, position: f32) {
36 if self.position() == position {
37 return;
38 }
39
40 self.position.set(position);
41 self.linear_gradient().as_ref().mark_stops_dirty();
42 }
43}
44
45impl ObjectRef<'_, GradientStop> {
46 fn linear_gradient(&self) -> Object<LinearGradient> {
47 self.cast::<Component>().parent().expect("GradientStop should have a parent").cast()
48 }
49}
50
51impl Core for GradientStop {
52 parent_types![(component, Component)];
53
54 properties![(38, color, set_color), (39, position, set_position), component];
55}
56
57impl OnAdded for ObjectRef<'_, GradientStop> {
58 on_added!([on_added_clean, import], Component);
59
60 fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
61 let component = self.cast::<Component>();
62
63 let code = component.on_added_dirty(context);
64 if code != StatusCode::Ok {
65 return code;
66 }
67
68 if let Some(linear_gradient) =
69 component.parent().and_then(|parent| parent.try_cast::<LinearGradient>())
70 {
71 linear_gradient.as_ref().push_stop(self.as_object());
72 StatusCode::Ok
73 } else {
74 StatusCode::MissingObject
75 }
76 }
77}
78
79impl Default for GradientStop {
80 fn default() -> Self {
81 Self {
82 component: Component::default(),
83 color: Property::new(Color32::from(0xFFFF_FFFF)),
84 position: Property::new(0.0),
85 }
86 }
87}