use bitflags::bitflags;
use crate::Vector;
use crate::rendering::composition::animations::AnimationInstanceFactory;
use crate::rendering::composition::animations::ICompositionAnimation;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::ServerPropertyValue;
use crate::rendering::composition::server::props_of;
use crate::rendering::composition::transport::BatchStreamReader;
use crate::rendering::composition::transport::BatchStreamWriter;
use crate::rendering::composition::transport::BatchValue;
use crate::rendering::composition::transport::BatchValueReader;
use std::any::Any;
use std::any::TypeId;
use std::cell::Cell;
use std::sync::OnceLock;
use std::time::Duration;
use super::CompositionGeometryHooks;
use super::CompositionGeometryProps;
use super::ServerCompositionGeometryHooks;
use super::ServerCompositionGeometryProps;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CompositionLineGeometryChangedFields: u8 {
const START = 1;
const START_ANIMATED = 2;
const END = 4;
const END_ANIMATED = 8;
}
}
impl BatchValue for CompositionLineGeometryChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
CompositionLineGeometryChangedFields::from_bits_retain(u8::read_from(input))
}
}
pub trait CompositionLineGeometryHooks: CompositionGeometryHooks {
fn initialize_defaults_extra(&self) {}
fn validate_start_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_start_changed(&self) {}
fn on_start_changing(&self) {}
fn validate_end_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_end_changed(&self) {}
fn on_end_changing(&self) {}
}
pub struct CompositionLineGeometryProps {
base: CompositionGeometryProps,
changed_fields_of_composition_line_geometry: Cell<CompositionLineGeometryChangedFields>,
start: Cell<Vector>,
end: Cell<Vector>,
}
impl Default for CompositionLineGeometryProps {
fn default() -> Self {
Self::new()
}
}
impl CompositionLineGeometryProps {
pub fn new() -> Self {
Self {
base: CompositionGeometryProps::new(),
changed_fields_of_composition_line_geometry: Cell::new(CompositionLineGeometryChangedFields::empty()),
start: Cell::new(Default::default()),
end: Cell::new(Default::default()),
}
}
pub fn base(&self) -> &CompositionGeometryProps {
&self.base
}
pub fn initialize_defaults(&self, host: &dyn CompositionLineGeometryHooks) {
self.base.initialize_defaults(host);
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn CompositionLineGeometryHooks) {
CompositionLineGeometryHooks::initialize_defaults_extra(host);
}
pub fn start(&self) -> Vector {
self.start.get()
}
pub fn set_start(&self, host: &dyn CompositionLineGeometryHooks, value: Vector) {
let mut changed = false;
let old_value = self.start.get();
if old_value.differs(&value) {
CompositionLineGeometryHooks::validate_start_change(host, old_value, value);
CompositionLineGeometryHooks::on_start_changing(host);
changed = true;
self.start.set(value);
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionLineGeometryProps::id_of_start_property().base());
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() & !CompositionLineGeometryChangedFields::START_ANIMATED);
if let Some(animation) = host.implicit_animation("Start") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START_ANIMATED);
host.pending_animations().set(
ServerCompositionLineGeometryProps::id_of_start_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Start", ExpressionVariant::create(value));
}
}
self.start.set(value);
if changed {
CompositionLineGeometryHooks::on_start_changed(host);
}
}
pub fn end(&self) -> Vector {
self.end.get()
}
pub fn set_end(&self, host: &dyn CompositionLineGeometryHooks, value: Vector) {
let mut changed = false;
let old_value = self.end.get();
if old_value.differs(&value) {
CompositionLineGeometryHooks::validate_end_change(host, old_value, value);
CompositionLineGeometryHooks::on_end_changing(host);
changed = true;
self.end.set(value);
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionLineGeometryProps::id_of_end_property().base());
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() & !CompositionLineGeometryChangedFields::END_ANIMATED);
if let Some(animation) = host.implicit_animation("End") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END_ANIMATED);
host.pending_animations().set(
ServerCompositionLineGeometryProps::id_of_end_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "End", ExpressionVariant::create(value));
}
}
self.end.set(value);
if changed {
CompositionLineGeometryHooks::on_end_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn CompositionLineGeometryHooks,
property_name: &str,
animation: &dyn ICompositionAnimation,
final_value: Option<ExpressionVariant>,
) -> bool {
if property_name == "Start" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionLineGeometryProps::id_of_start_property().base(), server);
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::START_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "End" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionLineGeometryProps::id_of_end_property().base(), server);
self.changed_fields_of_composition_line_geometry.set(self.changed_fields_of_composition_line_geometry.get() | CompositionLineGeometryChangedFields::END_ANIMATED);
host.register_for_serialization();
return true;
}
self.base.start_animation(host, property_name, animation, final_value)
}
pub fn serialize_changes_core(&self, host: &dyn CompositionLineGeometryHooks, writer: &mut BatchStreamWriter<'_>) {
self.base.serialize_changes_core(host, writer);
let changed = self.changed_fields_of_composition_line_geometry.get();
writer.write(changed);
if changed.contains(CompositionLineGeometryChangedFields::START_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLineGeometryProps::id_of_start_property().base()));
} else if changed.contains(CompositionLineGeometryChangedFields::START) {
writer.write(self.start.get());
}
if changed.contains(CompositionLineGeometryChangedFields::END_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionLineGeometryProps::id_of_end_property().base()));
} else if changed.contains(CompositionLineGeometryChangedFields::END) {
writer.write(self.end.get());
}
self.changed_fields_of_composition_line_geometry.set(CompositionLineGeometryChangedFields::empty());
}
}
pub trait ServerCompositionLineGeometryHooks: ServerCompositionGeometryHooks {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: CompositionLineGeometryChangedFields) {}
}
pub struct ServerCompositionLineGeometryProps {
base: ServerCompositionGeometryProps,
start: Cell<Vector>,
end: Cell<Vector>,
}
impl Default for ServerCompositionLineGeometryProps {
fn default() -> Self {
Self::new()
}
}
impl ServerCompositionLineGeometryProps {
pub fn new() -> Self {
Self {
base: ServerCompositionGeometryProps::new(),
start: Cell::new(Default::default()),
end: Cell::new(Default::default()),
}
}
pub fn base(&self) -> &ServerCompositionGeometryProps {
&self.base
}
pub fn find_props(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
return Some(self);
}
self.base.find_props(type_id)
}
pub fn id_of_start_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionLineGeometryProps, Vector>(
"Start",
"ServerCompositionLineGeometry",
|obj| props_of::<ServerCompositionLineGeometryProps>(obj).start.get(),
|obj, v| props_of::<ServerCompositionLineGeometryProps>(obj).start.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLineGeometryProps>(obj).start.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionLineGeometryProps>(obj).start.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn start(&self) -> Vector {
self.start.get()
}
pub fn set_start(&self, host: &dyn ServerCompositionLineGeometryHooks, value: Vector) {
self.start.set(value);
host.remove_animation_for_property(Self::id_of_start_property().base());
}
pub fn id_of_end_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionLineGeometryProps, Vector>(
"End",
"ServerCompositionLineGeometry",
|obj| props_of::<ServerCompositionLineGeometryProps>(obj).end.get(),
|obj, v| props_of::<ServerCompositionLineGeometryProps>(obj).end.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionLineGeometryProps>(obj).end.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionLineGeometryProps>(obj).end.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn end(&self) -> Vector {
self.end.get()
}
pub fn set_end(&self, host: &dyn ServerCompositionLineGeometryHooks, value: Vector) {
self.end.set(value);
host.remove_animation_for_property(Self::id_of_end_property().base());
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerCompositionLineGeometryHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
self.base.deserialize_changes_core(host, reader, committed_at);
ServerCompositionLineGeometryHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<CompositionLineGeometryChangedFields>();
if changed.contains(CompositionLineGeometryChangedFields::START_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Start` without an animation instance");
};
host.set_animated_value(
Self::id_of_start_property().base(),
ExpressionVariant::create(self.start.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionLineGeometryChangedFields::START) {
let value = reader.read::<Vector>();
self.set_start(host, value);
}
if changed.contains(CompositionLineGeometryChangedFields::END_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `End` without an animation instance");
};
host.set_animated_value(
Self::id_of_end_property().base(),
ExpressionVariant::create(self.end.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionLineGeometryChangedFields::END) {
let value = reader.read::<Vector>();
self.set_end(host, value);
}
ServerCompositionLineGeometryHooks::on_fields_deserialized(host, changed);
}
pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
if name == "Start" {
return Some(Self::id_of_start_property().base());
}
if name == "End" {
return Some(Self::id_of_end_property().base());
}
ServerCompositionGeometryProps::get_composition_property(name)
}
}