rive_rs/
draw_rules.rs
1use crate::component::Component;
6use crate::component_dirt::ComponentDirt;
7use crate::container_component::ContainerComponent;
8use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
9use crate::draw_target::DrawTarget;
10use crate::option_cell::OptionCell;
11use crate::status_code::StatusCode;
12
13#[derive(Debug, Default)]
14pub struct DrawRules {
15 container_component: ContainerComponent,
16 draw_target_id: Property<u64>,
17 active_target: OptionCell<Object<DrawTarget>>,
18}
19
20impl ObjectRef<'_, DrawRules> {
21 pub fn draw_target_id(&self) -> u64 {
22 self.draw_target_id.get()
23 }
24
25 pub fn set_draw_target_id(&self, draw_target_id: u64) {
26 if self.draw_target_id() == draw_target_id {
27 return;
28 }
29
30 self.draw_target_id.set(draw_target_id);
31
32 if let Some(artboard) = self.cast::<Component>().artboard() {
33 let draw_target = artboard
34 .as_ref()
35 .resolve(self.draw_target_id() as usize)
36 .and_then(|core| core.try_cast());
37
38 self.active_target.set(draw_target);
39
40 artboard.cast::<Component>().as_ref().add_dirt(ComponentDirt::DRAW_ORDER, false);
41 }
42 }
43}
44
45impl ObjectRef<'_, DrawRules> {
46 pub fn active_target(&self) -> Option<Object<DrawTarget>> {
47 self.active_target.get()
48 }
49}
50
51impl Core for DrawRules {
52 parent_types![(container_component, ContainerComponent)];
53
54 properties![(121, draw_target_id, set_draw_target_id), container_component];
55}
56
57impl OnAdded for ObjectRef<'_, DrawRules> {
58 on_added!([on_added_clean, import], ContainerComponent);
59
60 fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
61 let code = self.cast::<Component>().on_added_dirty(context);
62 if code != StatusCode::Ok {
63 return code;
64 }
65
66 let draw_target =
67 context.resolve(self.draw_target_id() as usize).and_then(|core| core.try_cast());
68
69 self.active_target.set(draw_target);
70
71 StatusCode::Ok
72 }
73}