use bitflags::bitflags;
use crate::Matrix;
use crate::Vector;
use crate::rendering::composition::ICompositionObjectHost;
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::IServerAnimatedPropertyHost;
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;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CompositionClipChangedFields: u16 {
const ANCHOR_POINT = 1;
const ANCHOR_POINT_ANIMATED = 2;
const CENTER_POINT = 4;
const CENTER_POINT_ANIMATED = 8;
const OFFSET = 16;
const OFFSET_ANIMATED = 32;
const ROTATION_ANGLE = 64;
const ROTATION_ANGLE_ANIMATED = 128;
const SCALE = 256;
const SCALE_ANIMATED = 512;
const TRANSFORM_MATRIX = 1024;
const TRANSFORM_MATRIX_ANIMATED = 2048;
}
}
impl BatchValue for CompositionClipChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
CompositionClipChangedFields::from_bits_retain(u16::read_from(input))
}
}
pub trait CompositionClipHooks: ICompositionObjectHost {
fn initialize_defaults_extra(&self) {}
fn validate_anchor_point_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_anchor_point_changed(&self) {}
fn on_anchor_point_changing(&self) {}
fn validate_center_point_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_center_point_changed(&self) {}
fn on_center_point_changing(&self) {}
fn validate_offset_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_offset_changed(&self) {}
fn on_offset_changing(&self) {}
fn validate_rotation_angle_change(&self, _old_value: f32, _new_value: f32) {}
fn on_rotation_angle_changed(&self) {}
fn on_rotation_angle_changing(&self) {}
fn validate_scale_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_scale_changed(&self) {}
fn on_scale_changing(&self) {}
fn validate_transform_matrix_change(&self, _old_value: Matrix, _new_value: Matrix) {}
fn on_transform_matrix_changed(&self) {}
fn on_transform_matrix_changing(&self) {}
}
pub struct CompositionClipProps {
changed_fields_of_composition_clip: Cell<CompositionClipChangedFields>,
anchor_point: Cell<Vector>,
center_point: Cell<Vector>,
offset: Cell<Vector>,
rotation_angle: Cell<f32>,
scale: Cell<Vector>,
transform_matrix: Cell<Matrix>,
}
impl Default for CompositionClipProps {
fn default() -> Self {
Self::new()
}
}
impl CompositionClipProps {
pub fn new() -> Self {
Self {
changed_fields_of_composition_clip: Cell::new(CompositionClipChangedFields::empty()),
anchor_point: Cell::new(Default::default()),
center_point: Cell::new(Default::default()),
offset: Cell::new(Default::default()),
rotation_angle: Cell::new(Default::default()),
scale: Cell::new(Default::default()),
transform_matrix: Cell::new(Default::default()),
}
}
pub fn initialize_defaults(&self, host: &dyn CompositionClipHooks) {
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn CompositionClipHooks) {
CompositionClipHooks::initialize_defaults_extra(host);
self.set_scale(host, Vector::new(1.0, 1.0));
self.set_transform_matrix(host, Matrix::IDENTITY);
}
pub fn anchor_point(&self) -> Vector {
self.anchor_point.get()
}
pub fn set_anchor_point(&self, host: &dyn CompositionClipHooks, value: Vector) {
let mut changed = false;
let old_value = self.anchor_point.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_anchor_point_change(host, old_value, value);
CompositionClipHooks::on_anchor_point_changing(host);
changed = true;
self.anchor_point.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ANCHOR_POINT);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_anchor_point_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::ANCHOR_POINT_ANIMATED);
if let Some(animation) = host.implicit_animation("AnchorPoint") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ANCHOR_POINT_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_anchor_point_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "AnchorPoint", ExpressionVariant::create(value));
}
}
self.anchor_point.set(value);
if changed {
CompositionClipHooks::on_anchor_point_changed(host);
}
}
pub fn center_point(&self) -> Vector {
self.center_point.get()
}
pub fn set_center_point(&self, host: &dyn CompositionClipHooks, value: Vector) {
let mut changed = false;
let old_value = self.center_point.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_center_point_change(host, old_value, value);
CompositionClipHooks::on_center_point_changing(host);
changed = true;
self.center_point.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::CENTER_POINT);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_center_point_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::CENTER_POINT_ANIMATED);
if let Some(animation) = host.implicit_animation("CenterPoint") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::CENTER_POINT_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_center_point_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "CenterPoint", ExpressionVariant::create(value));
}
}
self.center_point.set(value);
if changed {
CompositionClipHooks::on_center_point_changed(host);
}
}
pub fn offset(&self) -> Vector {
self.offset.get()
}
pub fn set_offset(&self, host: &dyn CompositionClipHooks, value: Vector) {
let mut changed = false;
let old_value = self.offset.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_offset_change(host, old_value, value);
CompositionClipHooks::on_offset_changing(host);
changed = true;
self.offset.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::OFFSET);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_offset_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::OFFSET_ANIMATED);
if let Some(animation) = host.implicit_animation("Offset") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::OFFSET_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_offset_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Offset", ExpressionVariant::create(value));
}
}
self.offset.set(value);
if changed {
CompositionClipHooks::on_offset_changed(host);
}
}
pub fn rotation_angle(&self) -> f32 {
self.rotation_angle.get()
}
pub fn set_rotation_angle(&self, host: &dyn CompositionClipHooks, value: f32) {
let mut changed = false;
let old_value = self.rotation_angle.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_rotation_angle_change(host, old_value, value);
CompositionClipHooks::on_rotation_angle_changing(host);
changed = true;
self.rotation_angle.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ROTATION_ANGLE);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_rotation_angle_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::ROTATION_ANGLE_ANIMATED);
if let Some(animation) = host.implicit_animation("RotationAngle") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ROTATION_ANGLE_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_rotation_angle_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "RotationAngle", ExpressionVariant::create(value));
}
}
self.rotation_angle.set(value);
if changed {
CompositionClipHooks::on_rotation_angle_changed(host);
}
}
pub fn scale(&self) -> Vector {
self.scale.get()
}
pub fn set_scale(&self, host: &dyn CompositionClipHooks, value: Vector) {
let mut changed = false;
let old_value = self.scale.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_scale_change(host, old_value, value);
CompositionClipHooks::on_scale_changing(host);
changed = true;
self.scale.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::SCALE);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_scale_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::SCALE_ANIMATED);
if let Some(animation) = host.implicit_animation("Scale") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::SCALE_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_scale_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Scale", ExpressionVariant::create(value));
}
}
self.scale.set(value);
if changed {
CompositionClipHooks::on_scale_changed(host);
}
}
pub fn transform_matrix(&self) -> Matrix {
self.transform_matrix.get()
}
pub fn set_transform_matrix(&self, host: &dyn CompositionClipHooks, value: Matrix) {
let mut changed = false;
let old_value = self.transform_matrix.get();
if old_value.differs(&value) {
CompositionClipHooks::validate_transform_matrix_change(host, old_value, value);
CompositionClipHooks::on_transform_matrix_changing(host);
changed = true;
self.transform_matrix.set(value);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::TRANSFORM_MATRIX);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionClipProps::id_of_transform_matrix_property().base());
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() & !CompositionClipChangedFields::TRANSFORM_MATRIX_ANIMATED);
if let Some(animation) = host.implicit_animation("TransformMatrix") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::TRANSFORM_MATRIX_ANIMATED);
host.pending_animations().set(
ServerCompositionClipProps::id_of_transform_matrix_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "TransformMatrix", ExpressionVariant::create(value));
}
}
self.transform_matrix.set(value);
if changed {
CompositionClipHooks::on_transform_matrix_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn CompositionClipHooks,
property_name: &str,
animation: &dyn ICompositionAnimation,
final_value: Option<ExpressionVariant>,
) -> bool {
if property_name == "AnchorPoint" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_anchor_point_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ANCHOR_POINT_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "CenterPoint" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_center_point_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::CENTER_POINT_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Offset" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_offset_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::OFFSET_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "RotationAngle" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_rotation_angle_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::ROTATION_ANGLE_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Scale" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_scale_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::SCALE_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "TransformMatrix" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionClipProps::id_of_transform_matrix_property().base(), server);
self.changed_fields_of_composition_clip.set(self.changed_fields_of_composition_clip.get() | CompositionClipChangedFields::TRANSFORM_MATRIX_ANIMATED);
host.register_for_serialization();
return true;
}
false
}
pub fn serialize_changes_core(&self, host: &dyn CompositionClipHooks, writer: &mut BatchStreamWriter<'_>) {
let changed = self.changed_fields_of_composition_clip.get();
writer.write(changed);
if changed.contains(CompositionClipChangedFields::ANCHOR_POINT_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_anchor_point_property().base()));
} else if changed.contains(CompositionClipChangedFields::ANCHOR_POINT) {
writer.write(self.anchor_point.get());
}
if changed.contains(CompositionClipChangedFields::CENTER_POINT_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_center_point_property().base()));
} else if changed.contains(CompositionClipChangedFields::CENTER_POINT) {
writer.write(self.center_point.get());
}
if changed.contains(CompositionClipChangedFields::OFFSET_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_offset_property().base()));
} else if changed.contains(CompositionClipChangedFields::OFFSET) {
writer.write(self.offset.get());
}
if changed.contains(CompositionClipChangedFields::ROTATION_ANGLE_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_rotation_angle_property().base()));
} else if changed.contains(CompositionClipChangedFields::ROTATION_ANGLE) {
writer.write(self.rotation_angle.get());
}
if changed.contains(CompositionClipChangedFields::SCALE_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_scale_property().base()));
} else if changed.contains(CompositionClipChangedFields::SCALE) {
writer.write(self.scale.get());
}
if changed.contains(CompositionClipChangedFields::TRANSFORM_MATRIX_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionClipProps::id_of_transform_matrix_property().base()));
} else if changed.contains(CompositionClipChangedFields::TRANSFORM_MATRIX) {
writer.write(self.transform_matrix.get());
}
self.changed_fields_of_composition_clip.set(CompositionClipChangedFields::empty());
}
}
pub trait ServerCompositionClipHooks: IServerAnimatedPropertyHost {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: CompositionClipChangedFields) {}
}
pub struct ServerCompositionClipProps {
anchor_point: Cell<Vector>,
center_point: Cell<Vector>,
offset: Cell<Vector>,
rotation_angle: Cell<f32>,
scale: Cell<Vector>,
transform_matrix: Cell<Matrix>,
}
impl Default for ServerCompositionClipProps {
fn default() -> Self {
Self::new()
}
}
impl ServerCompositionClipProps {
pub fn new() -> Self {
Self {
anchor_point: Cell::new(Default::default()),
center_point: Cell::new(Default::default()),
offset: Cell::new(Default::default()),
rotation_angle: Cell::new(Default::default()),
scale: Cell::new(Default::default()),
transform_matrix: Cell::new(Default::default()),
}
}
pub fn find_props(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
return Some(self);
}
None
}
pub fn id_of_anchor_point_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, Vector>(
"AnchorPoint",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).anchor_point.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).anchor_point.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).anchor_point.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).anchor_point.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn anchor_point(&self) -> Vector {
self.anchor_point.get()
}
pub fn set_anchor_point(&self, host: &dyn ServerCompositionClipHooks, value: Vector) {
self.anchor_point.set(value);
host.remove_animation_for_property(Self::id_of_anchor_point_property().base());
}
pub fn id_of_center_point_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, Vector>(
"CenterPoint",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).center_point.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).center_point.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).center_point.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).center_point.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn center_point(&self) -> Vector {
self.center_point.get()
}
pub fn set_center_point(&self, host: &dyn ServerCompositionClipHooks, value: Vector) {
self.center_point.set(value);
host.remove_animation_for_property(Self::id_of_center_point_property().base());
}
pub fn id_of_offset_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, Vector>(
"Offset",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).offset.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).offset.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).offset.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).offset.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn offset(&self) -> Vector {
self.offset.get()
}
pub fn set_offset(&self, host: &dyn ServerCompositionClipHooks, value: Vector) {
self.offset.set(value);
host.remove_animation_for_property(Self::id_of_offset_property().base());
}
pub fn id_of_rotation_angle_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, f32>(
"RotationAngle",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).rotation_angle.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).rotation_angle.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).rotation_angle.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).rotation_angle.set(v.cast_or_default::<f32>())),
)
})
}
pub fn rotation_angle(&self) -> f32 {
self.rotation_angle.get()
}
pub fn set_rotation_angle(&self, host: &dyn ServerCompositionClipHooks, value: f32) {
self.rotation_angle.set(value);
host.remove_animation_for_property(Self::id_of_rotation_angle_property().base());
}
pub fn id_of_scale_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, Vector>(
"Scale",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).scale.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).scale.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).scale.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).scale.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn scale(&self) -> Vector {
self.scale.get()
}
pub fn set_scale(&self, host: &dyn ServerCompositionClipHooks, value: Vector) {
self.scale.set(value);
host.remove_animation_for_property(Self::id_of_scale_property().base());
}
pub fn id_of_transform_matrix_property() -> &'static CompositionPropertyOf<Matrix> {
static PROPERTY: OnceLock<CompositionPropertyOf<Matrix>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionClipProps, Matrix>(
"TransformMatrix",
"ServerCompositionClip",
|obj| props_of::<ServerCompositionClipProps>(obj).transform_matrix.get(),
|obj, v| props_of::<ServerCompositionClipProps>(obj).transform_matrix.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionClipProps>(obj).transform_matrix.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionClipProps>(obj).transform_matrix.set(v.cast_or_default::<Matrix>())),
)
})
}
pub fn transform_matrix(&self) -> Matrix {
self.transform_matrix.get()
}
pub fn set_transform_matrix(&self, host: &dyn ServerCompositionClipHooks, value: Matrix) {
self.transform_matrix.set(value);
host.remove_animation_for_property(Self::id_of_transform_matrix_property().base());
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerCompositionClipHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
ServerCompositionClipHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<CompositionClipChangedFields>();
if changed.contains(CompositionClipChangedFields::ANCHOR_POINT_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `AnchorPoint` without an animation instance");
};
host.set_animated_value(
Self::id_of_anchor_point_property().base(),
ExpressionVariant::create(self.anchor_point.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::ANCHOR_POINT) {
let value = reader.read::<Vector>();
self.set_anchor_point(host, value);
}
if changed.contains(CompositionClipChangedFields::CENTER_POINT_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `CenterPoint` without an animation instance");
};
host.set_animated_value(
Self::id_of_center_point_property().base(),
ExpressionVariant::create(self.center_point.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::CENTER_POINT) {
let value = reader.read::<Vector>();
self.set_center_point(host, value);
}
if changed.contains(CompositionClipChangedFields::OFFSET_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Offset` without an animation instance");
};
host.set_animated_value(
Self::id_of_offset_property().base(),
ExpressionVariant::create(self.offset.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::OFFSET) {
let value = reader.read::<Vector>();
self.set_offset(host, value);
}
if changed.contains(CompositionClipChangedFields::ROTATION_ANGLE_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `RotationAngle` without an animation instance");
};
host.set_animated_value(
Self::id_of_rotation_angle_property().base(),
ExpressionVariant::create(self.rotation_angle.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::ROTATION_ANGLE) {
let value = reader.read::<f32>();
self.set_rotation_angle(host, value);
}
if changed.contains(CompositionClipChangedFields::SCALE_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Scale` without an animation instance");
};
host.set_animated_value(
Self::id_of_scale_property().base(),
ExpressionVariant::create(self.scale.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::SCALE) {
let value = reader.read::<Vector>();
self.set_scale(host, value);
}
if changed.contains(CompositionClipChangedFields::TRANSFORM_MATRIX_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `TransformMatrix` without an animation instance");
};
host.set_animated_value(
Self::id_of_transform_matrix_property().base(),
ExpressionVariant::create(self.transform_matrix.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionClipChangedFields::TRANSFORM_MATRIX) {
let value = reader.read::<Matrix>();
self.set_transform_matrix(host, value);
}
ServerCompositionClipHooks::on_fields_deserialized(host, changed);
}
pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
if name == "AnchorPoint" {
return Some(Self::id_of_anchor_point_property().base());
}
if name == "CenterPoint" {
return Some(Self::id_of_center_point_property().base());
}
if name == "Offset" {
return Some(Self::id_of_offset_property().base());
}
if name == "RotationAngle" {
return Some(Self::id_of_rotation_angle_property().base());
}
if name == "Scale" {
return Some(Self::id_of_scale_property().base());
}
if name == "TransformMatrix" {
return Some(Self::id_of_transform_matrix_property().base());
}
None
}
}