use bitflags::bitflags;
use crate::Vector3D;
use crate::media::Color;
use crate::rendering::composition::ICompositionObject;
use crate::rendering::composition::animations::AnimationInstanceFactory;
use crate::rendering::composition::animations::ICompositionAnimation;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::AsServerRenderResource;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::IServerObject;
use crate::rendering::composition::server::ServerPropertyValue;
use crate::rendering::composition::server::ServerValueChange;
use crate::rendering::composition::server::props_of;
use crate::rendering::composition::server::read_server_object;
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::cell::RefCell;
use std::rc::Rc;
use std::sync::OnceLock;
use std::time::Duration;
use super::CompositionLightHooks;
use super::CompositionLightProps;
use super::ServerCompositionLightHooks;
use super::ServerCompositionLightProps;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DistantLightChangedFields: u8 {
const COLOR = 1;
const COLOR_ANIMATED = 2;
const COORDINATE_SPACE = 4;
const DIRECTION = 8;
const DIRECTION_ANIMATED = 16;
const INTENSITY = 32;
const INTENSITY_ANIMATED = 64;
}
}
impl BatchValue for DistantLightChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
DistantLightChangedFields::from_bits_retain(u8::read_from(input))
}
}
pub trait DistantLightHooks: CompositionLightHooks {
fn initialize_defaults_extra(&self) {}
fn validate_color_change(&self, _old_value: Color, _new_value: Color) {}
fn on_color_changed(&self) {}
fn on_color_changing(&self) {}
fn validate_coordinate_space_change(&self, _old_value: &Option<Rc<dyn ICompositionObject>>, _new_value: &Option<Rc<dyn ICompositionObject>>) {}
fn on_coordinate_space_changed(&self) {}
fn on_coordinate_space_changing(&self) {}
fn validate_direction_change(&self, _old_value: Vector3D, _new_value: Vector3D) {}
fn on_direction_changed(&self) {}
fn on_direction_changing(&self) {}
fn validate_intensity_change(&self, _old_value: f32, _new_value: f32) {}
fn on_intensity_changed(&self) {}
fn on_intensity_changing(&self) {}
}
pub struct DistantLightProps {
base: CompositionLightProps,
changed_fields_of_distant_light: Cell<DistantLightChangedFields>,
color: Cell<Color>,
coordinate_space: RefCell<Option<Rc<dyn ICompositionObject>>>,
direction: Cell<Vector3D>,
intensity: Cell<f32>,
}
impl Default for DistantLightProps {
fn default() -> Self {
Self::new()
}
}
impl DistantLightProps {
pub fn new() -> Self {
Self {
base: CompositionLightProps::new(),
changed_fields_of_distant_light: Cell::new(DistantLightChangedFields::empty()),
color: Cell::new(Default::default()),
coordinate_space: RefCell::new(None),
direction: Cell::new(Default::default()),
intensity: Cell::new(Default::default()),
}
}
pub fn base(&self) -> &CompositionLightProps {
&self.base
}
pub fn initialize_defaults(&self, host: &dyn DistantLightHooks) {
self.base.initialize_defaults(host);
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn DistantLightHooks) {
DistantLightHooks::initialize_defaults_extra(host);
self.set_color(host, crate::media::Colors::WHITE);
self.set_direction(host, Vector3D::new(0.0, 0.0, -1.0));
self.set_intensity(host, 1.0);
}
pub fn color(&self) -> Color {
self.color.get()
}
pub fn set_color(&self, host: &dyn DistantLightHooks, value: Color) {
let mut changed = false;
let old_value = self.color.get();
if old_value.differs(&value) {
DistantLightHooks::validate_color_change(host, old_value, value);
DistantLightHooks::on_color_changing(host);
changed = true;
self.color.set(value);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::COLOR);
host.register_for_serialization();
host.pending_animations().remove(ServerDistantLightProps::id_of_color_property().base());
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() & !DistantLightChangedFields::COLOR_ANIMATED);
if let Some(animation) = host.implicit_animation("Color") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::COLOR_ANIMATED);
host.pending_animations().set(
ServerDistantLightProps::id_of_color_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Color", ExpressionVariant::create(value));
}
}
self.color.set(value);
if changed {
DistantLightHooks::on_color_changed(host);
}
}
pub fn coordinate_space(&self) -> Option<Rc<dyn ICompositionObject>> {
self.coordinate_space.borrow().clone()
}
pub fn set_coordinate_space(&self, host: &dyn DistantLightHooks, value: Option<Rc<dyn ICompositionObject>>) {
let mut changed = false;
let old_value = self.coordinate_space.borrow().clone();
if old_value.differs(&value) {
DistantLightHooks::validate_coordinate_space_change(host, &old_value, &value);
DistantLightHooks::on_coordinate_space_changing(host);
changed = true;
*self.coordinate_space.borrow_mut() = value.clone();
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::COORDINATE_SPACE);
host.register_for_serialization();
}
*self.coordinate_space.borrow_mut() = value;
if changed {
DistantLightHooks::on_coordinate_space_changed(host);
}
}
pub fn direction(&self) -> Vector3D {
self.direction.get()
}
pub fn set_direction(&self, host: &dyn DistantLightHooks, value: Vector3D) {
let mut changed = false;
let old_value = self.direction.get();
if old_value.differs(&value) {
DistantLightHooks::validate_direction_change(host, old_value, value);
DistantLightHooks::on_direction_changing(host);
changed = true;
self.direction.set(value);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::DIRECTION);
host.register_for_serialization();
host.pending_animations().remove(ServerDistantLightProps::id_of_direction_property().base());
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() & !DistantLightChangedFields::DIRECTION_ANIMATED);
if let Some(animation) = host.implicit_animation("Direction") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::DIRECTION_ANIMATED);
host.pending_animations().set(
ServerDistantLightProps::id_of_direction_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Direction", ExpressionVariant::create(value));
}
}
self.direction.set(value);
if changed {
DistantLightHooks::on_direction_changed(host);
}
}
pub fn intensity(&self) -> f32 {
self.intensity.get()
}
pub fn set_intensity(&self, host: &dyn DistantLightHooks, value: f32) {
let mut changed = false;
let old_value = self.intensity.get();
if old_value.differs(&value) {
DistantLightHooks::validate_intensity_change(host, old_value, value);
DistantLightHooks::on_intensity_changing(host);
changed = true;
self.intensity.set(value);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::INTENSITY);
host.register_for_serialization();
host.pending_animations().remove(ServerDistantLightProps::id_of_intensity_property().base());
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() & !DistantLightChangedFields::INTENSITY_ANIMATED);
if let Some(animation) = host.implicit_animation("Intensity") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::INTENSITY_ANIMATED);
host.pending_animations().set(
ServerDistantLightProps::id_of_intensity_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "Intensity", ExpressionVariant::create(value));
}
}
self.intensity.set(value);
if changed {
DistantLightHooks::on_intensity_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn DistantLightHooks,
property_name: &str,
animation: &dyn ICompositionAnimation,
final_value: Option<ExpressionVariant>,
) -> bool {
if property_name == "Color" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerDistantLightProps::id_of_color_property().base(), server);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::COLOR_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Direction" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerDistantLightProps::id_of_direction_property().base(), server);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::DIRECTION_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Intensity" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerDistantLightProps::id_of_intensity_property().base(), server);
self.changed_fields_of_distant_light.set(self.changed_fields_of_distant_light.get() | DistantLightChangedFields::INTENSITY_ANIMATED);
host.register_for_serialization();
return true;
}
false
}
pub fn serialize_changes_core(&self, host: &dyn DistantLightHooks, writer: &mut BatchStreamWriter<'_>) {
self.base.serialize_changes_core(host, writer);
let changed = self.changed_fields_of_distant_light.get();
writer.write(changed);
if changed.contains(DistantLightChangedFields::COLOR_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerDistantLightProps::id_of_color_property().base()));
} else if changed.contains(DistantLightChangedFields::COLOR) {
writer.write(self.color.get());
}
if changed.contains(DistantLightChangedFields::COORDINATE_SPACE) {
writer.write_server_object(self.coordinate_space.borrow().clone().map(|o| o.server()));
}
if changed.contains(DistantLightChangedFields::DIRECTION_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerDistantLightProps::id_of_direction_property().base()));
} else if changed.contains(DistantLightChangedFields::DIRECTION) {
writer.write(self.direction.get());
}
if changed.contains(DistantLightChangedFields::INTENSITY_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerDistantLightProps::id_of_intensity_property().base()));
} else if changed.contains(DistantLightChangedFields::INTENSITY) {
writer.write(self.intensity.get());
}
self.changed_fields_of_distant_light.set(DistantLightChangedFields::empty());
}
}
pub trait ServerDistantLightHooks: ServerCompositionLightHooks {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: DistantLightChangedFields) {}
fn on_coordinate_space_changed(&self) {}
fn on_coordinate_space_changing(&self) {}
}
pub struct ServerDistantLightProps {
base: ServerCompositionLightProps,
color: Cell<Color>,
coordinate_space: RefCell<Option<Rc<dyn IServerObject>>>,
direction: Cell<Vector3D>,
intensity: Cell<f32>,
}
impl Default for ServerDistantLightProps {
fn default() -> Self {
Self::new()
}
}
impl ServerDistantLightProps {
pub fn new() -> Self {
Self {
base: ServerCompositionLightProps::new(),
color: Cell::new(Default::default()),
coordinate_space: RefCell::new(None),
direction: Cell::new(Default::default()),
intensity: Cell::new(Default::default()),
}
}
pub fn base(&self) -> &ServerCompositionLightProps {
&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_color_property() -> &'static CompositionPropertyOf<Color> {
static PROPERTY: OnceLock<CompositionPropertyOf<Color>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerDistantLightProps, Color>(
"Color",
"ServerDistantLight",
|obj| props_of::<ServerDistantLightProps>(obj).color.get(),
|obj, v| props_of::<ServerDistantLightProps>(obj).color.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerDistantLightProps>(obj).color.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerDistantLightProps>(obj).color.set(v.cast_or_default::<Color>())),
)
})
}
pub fn color(&self) -> Color {
self.color.get()
}
pub fn set_color(&self, host: &dyn ServerDistantLightHooks, value: Color) {
self.color.set(value);
host.remove_animation_for_property(Self::id_of_color_property().base());
}
pub fn id_of_coordinate_space_property() -> &'static CompositionPropertyOf<Option<Rc<dyn IServerObject>>> {
static PROPERTY: OnceLock<CompositionPropertyOf<Option<Rc<dyn IServerObject>>>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerDistantLightProps, Option<Rc<dyn IServerObject>>>(
"CoordinateSpace",
"ServerDistantLight",
|obj| props_of::<ServerDistantLightProps>(obj).coordinate_space.borrow().clone(),
|obj, v| *props_of::<ServerDistantLightProps>(obj).coordinate_space.borrow_mut() = v,
None,
None,
)
})
}
pub fn coordinate_space(&self) -> Option<Rc<dyn IServerObject>> {
self.coordinate_space.borrow().clone()
}
pub fn set_coordinate_space(&self, host: &dyn ServerDistantLightHooks, value: Option<Rc<dyn IServerObject>>) {
let old_value = self.coordinate_space.borrow().clone();
let mut changed = false;
if old_value.differs(&value) {
ServerDistantLightHooks::on_coordinate_space_changing(host);
changed = true;
}
host.set_value(
Self::id_of_coordinate_space_property().base(),
ServerValueChange {
equal: old_value.default_equals(&value),
old_resource: old_value.as_server_render_resource(),
new_resource: value.as_server_render_resource(),
assign: &mut || *self.coordinate_space.borrow_mut() = value.clone(),
},
);
if changed {
ServerDistantLightHooks::on_coordinate_space_changed(host);
}
}
pub fn id_of_direction_property() -> &'static CompositionPropertyOf<Vector3D> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector3D>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerDistantLightProps, Vector3D>(
"Direction",
"ServerDistantLight",
|obj| props_of::<ServerDistantLightProps>(obj).direction.get(),
|obj, v| props_of::<ServerDistantLightProps>(obj).direction.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerDistantLightProps>(obj).direction.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerDistantLightProps>(obj).direction.set(v.cast_or_default::<Vector3D>())),
)
})
}
pub fn direction(&self) -> Vector3D {
self.direction.get()
}
pub fn set_direction(&self, host: &dyn ServerDistantLightHooks, value: Vector3D) {
self.direction.set(value);
host.remove_animation_for_property(Self::id_of_direction_property().base());
}
pub fn id_of_intensity_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerDistantLightProps, f32>(
"Intensity",
"ServerDistantLight",
|obj| props_of::<ServerDistantLightProps>(obj).intensity.get(),
|obj, v| props_of::<ServerDistantLightProps>(obj).intensity.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerDistantLightProps>(obj).intensity.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerDistantLightProps>(obj).intensity.set(v.cast_or_default::<f32>())),
)
})
}
pub fn intensity(&self) -> f32 {
self.intensity.get()
}
pub fn set_intensity(&self, host: &dyn ServerDistantLightHooks, value: f32) {
self.intensity.set(value);
host.remove_animation_for_property(Self::id_of_intensity_property().base());
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerDistantLightHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
let compositor = host.server_compositor();
self.base.deserialize_changes_core(host, reader, committed_at);
ServerDistantLightHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<DistantLightChangedFields>();
if changed.contains(DistantLightChangedFields::COLOR_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Color` without an animation instance");
};
host.set_animated_value(
Self::id_of_color_property().base(),
ExpressionVariant::create(self.color.get()),
committed_at,
animation,
);
} else if changed.contains(DistantLightChangedFields::COLOR) {
let value = reader.read::<Color>();
self.set_color(host, value);
}
if changed.contains(DistantLightChangedFields::COORDINATE_SPACE) {
let value = read_server_object(reader, compositor.as_ref());
self.set_coordinate_space(host, value);
}
if changed.contains(DistantLightChangedFields::DIRECTION_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Direction` without an animation instance");
};
host.set_animated_value(
Self::id_of_direction_property().base(),
ExpressionVariant::create(self.direction.get()),
committed_at,
animation,
);
} else if changed.contains(DistantLightChangedFields::DIRECTION) {
let value = reader.read::<Vector3D>();
self.set_direction(host, value);
}
if changed.contains(DistantLightChangedFields::INTENSITY_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `Intensity` without an animation instance");
};
host.set_animated_value(
Self::id_of_intensity_property().base(),
ExpressionVariant::create(self.intensity.get()),
committed_at,
animation,
);
} else if changed.contains(DistantLightChangedFields::INTENSITY) {
let value = reader.read::<f32>();
self.set_intensity(host, value);
}
ServerDistantLightHooks::on_fields_deserialized(host, changed);
}
pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
if name == "Color" {
return Some(Self::id_of_color_property().base());
}
if name == "Direction" {
return Some(Self::id_of_direction_property().base());
}
if name == "Intensity" {
return Some(Self::id_of_intensity_property().base());
}
ServerCompositionLightProps::get_composition_property(name)
}
}