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 PointLightChangedFields: u32 {
const COLOR = 1;
const COLOR_ANIMATED = 2;
const CONSTANT_ATTENUATION = 4;
const CONSTANT_ATTENUATION_ANIMATED = 8;
const LINEAR_ATTENUATION = 16;
const LINEAR_ATTENUATION_ANIMATED = 32;
const QUADRATIC_ATTENUATION = 64;
const QUADRATIC_ATTENUATION_ANIMATED = 128;
const COORDINATE_SPACE = 256;
const OFFSET = 512;
const OFFSET_ANIMATED = 1024;
const INTENSITY = 2048;
const INTENSITY_ANIMATED = 4096;
const MIN_ATTENUATION_CUTOFF = 8192;
const MIN_ATTENUATION_CUTOFF_ANIMATED = 16384;
const MAX_ATTENUATION_CUTOFF = 32768;
const MAX_ATTENUATION_CUTOFF_ANIMATED = 65536;
}
}
impl BatchValue for PointLightChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
PointLightChangedFields::from_bits_retain(u32::read_from(input))
}
}
pub trait PointLightHooks: 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_constant_attenuation_change(&self, _old_value: f32, _new_value: f32) {}
fn on_constant_attenuation_changed(&self) {}
fn on_constant_attenuation_changing(&self) {}
fn validate_linear_attenuation_change(&self, _old_value: f32, _new_value: f32) {}
fn on_linear_attenuation_changed(&self) {}
fn on_linear_attenuation_changing(&self) {}
fn validate_quadratic_attenuation_change(&self, _old_value: f32, _new_value: f32) {}
fn on_quadratic_attenuation_changed(&self) {}
fn on_quadratic_attenuation_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_offset_change(&self, _old_value: Vector3D, _new_value: Vector3D) {}
fn on_offset_changed(&self) {}
fn on_offset_changing(&self) {}
fn validate_intensity_change(&self, _old_value: f32, _new_value: f32) {}
fn on_intensity_changed(&self) {}
fn on_intensity_changing(&self) {}
fn validate_min_attenuation_cutoff_change(&self, _old_value: f32, _new_value: f32) {}
fn on_min_attenuation_cutoff_changed(&self) {}
fn on_min_attenuation_cutoff_changing(&self) {}
fn validate_max_attenuation_cutoff_change(&self, _old_value: f32, _new_value: f32) {}
fn on_max_attenuation_cutoff_changed(&self) {}
fn on_max_attenuation_cutoff_changing(&self) {}
}
pub struct PointLightProps {
base: CompositionLightProps,
changed_fields_of_point_light: Cell<PointLightChangedFields>,
color: Cell<Color>,
constant_attenuation: Cell<f32>,
linear_attenuation: Cell<f32>,
quadratic_attenuation: Cell<f32>,
coordinate_space: RefCell<Option<Rc<dyn ICompositionObject>>>,
offset: Cell<Vector3D>,
intensity: Cell<f32>,
min_attenuation_cutoff: Cell<f32>,
max_attenuation_cutoff: Cell<f32>,
}
impl Default for PointLightProps {
fn default() -> Self {
Self::new()
}
}
impl PointLightProps {
pub fn new() -> Self {
Self {
base: CompositionLightProps::new(),
changed_fields_of_point_light: Cell::new(PointLightChangedFields::empty()),
color: Cell::new(Default::default()),
constant_attenuation: Cell::new(Default::default()),
linear_attenuation: Cell::new(Default::default()),
quadratic_attenuation: Cell::new(Default::default()),
coordinate_space: RefCell::new(None),
offset: Cell::new(Default::default()),
intensity: Cell::new(Default::default()),
min_attenuation_cutoff: Cell::new(Default::default()),
max_attenuation_cutoff: Cell::new(Default::default()),
}
}
pub fn base(&self) -> &CompositionLightProps {
&self.base
}
pub fn initialize_defaults(&self, host: &dyn PointLightHooks) {
self.base.initialize_defaults(host);
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn PointLightHooks) {
PointLightHooks::initialize_defaults_extra(host);
self.set_color(host, crate::media::Colors::WHITE);
self.set_constant_attenuation(host, 1.0);
self.set_intensity(host, 1.0);
self.set_max_attenuation_cutoff(host, f32::MAX);
}
pub fn color(&self) -> Color {
self.color.get()
}
pub fn set_color(&self, host: &dyn PointLightHooks, value: Color) {
let mut changed = false;
let old_value = self.color.get();
if old_value.differs(&value) {
PointLightHooks::validate_color_change(host, old_value, value);
PointLightHooks::on_color_changing(host);
changed = true;
self.color.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::COLOR);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_color_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::COLOR_ANIMATED);
if let Some(animation) = host.implicit_animation("Color") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::COLOR_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::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 {
PointLightHooks::on_color_changed(host);
}
}
pub fn constant_attenuation(&self) -> f32 {
self.constant_attenuation.get()
}
pub fn set_constant_attenuation(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.constant_attenuation.get();
if old_value.differs(&value) {
PointLightHooks::validate_constant_attenuation_change(host, old_value, value);
PointLightHooks::on_constant_attenuation_changing(host);
changed = true;
self.constant_attenuation.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::CONSTANT_ATTENUATION);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_constant_attenuation_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::CONSTANT_ATTENUATION_ANIMATED);
if let Some(animation) = host.implicit_animation("ConstantAttenuation") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::CONSTANT_ATTENUATION_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::id_of_constant_attenuation_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "ConstantAttenuation", ExpressionVariant::create(value));
}
}
self.constant_attenuation.set(value);
if changed {
PointLightHooks::on_constant_attenuation_changed(host);
}
}
pub fn linear_attenuation(&self) -> f32 {
self.linear_attenuation.get()
}
pub fn set_linear_attenuation(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.linear_attenuation.get();
if old_value.differs(&value) {
PointLightHooks::validate_linear_attenuation_change(host, old_value, value);
PointLightHooks::on_linear_attenuation_changing(host);
changed = true;
self.linear_attenuation.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::LINEAR_ATTENUATION);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_linear_attenuation_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::LINEAR_ATTENUATION_ANIMATED);
if let Some(animation) = host.implicit_animation("LinearAttenuation") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::LINEAR_ATTENUATION_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::id_of_linear_attenuation_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "LinearAttenuation", ExpressionVariant::create(value));
}
}
self.linear_attenuation.set(value);
if changed {
PointLightHooks::on_linear_attenuation_changed(host);
}
}
pub fn quadratic_attenuation(&self) -> f32 {
self.quadratic_attenuation.get()
}
pub fn set_quadratic_attenuation(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.quadratic_attenuation.get();
if old_value.differs(&value) {
PointLightHooks::validate_quadratic_attenuation_change(host, old_value, value);
PointLightHooks::on_quadratic_attenuation_changing(host);
changed = true;
self.quadratic_attenuation.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::QUADRATIC_ATTENUATION);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_quadratic_attenuation_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::QUADRATIC_ATTENUATION_ANIMATED);
if let Some(animation) = host.implicit_animation("QuadraticAttenuation") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::QUADRATIC_ATTENUATION_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::id_of_quadratic_attenuation_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "QuadraticAttenuation", ExpressionVariant::create(value));
}
}
self.quadratic_attenuation.set(value);
if changed {
PointLightHooks::on_quadratic_attenuation_changed(host);
}
}
pub fn coordinate_space(&self) -> Option<Rc<dyn ICompositionObject>> {
self.coordinate_space.borrow().clone()
}
pub fn set_coordinate_space(&self, host: &dyn PointLightHooks, value: Option<Rc<dyn ICompositionObject>>) {
let mut changed = false;
let old_value = self.coordinate_space.borrow().clone();
if old_value.differs(&value) {
PointLightHooks::validate_coordinate_space_change(host, &old_value, &value);
PointLightHooks::on_coordinate_space_changing(host);
changed = true;
*self.coordinate_space.borrow_mut() = value.clone();
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::COORDINATE_SPACE);
host.register_for_serialization();
}
*self.coordinate_space.borrow_mut() = value;
if changed {
PointLightHooks::on_coordinate_space_changed(host);
}
}
pub fn offset(&self) -> Vector3D {
self.offset.get()
}
pub fn set_offset(&self, host: &dyn PointLightHooks, value: Vector3D) {
let mut changed = false;
let old_value = self.offset.get();
if old_value.differs(&value) {
PointLightHooks::validate_offset_change(host, old_value, value);
PointLightHooks::on_offset_changing(host);
changed = true;
self.offset.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::OFFSET);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_offset_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::OFFSET_ANIMATED);
if let Some(animation) = host.implicit_animation("Offset") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::OFFSET_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::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 {
PointLightHooks::on_offset_changed(host);
}
}
pub fn intensity(&self) -> f32 {
self.intensity.get()
}
pub fn set_intensity(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.intensity.get();
if old_value.differs(&value) {
PointLightHooks::validate_intensity_change(host, old_value, value);
PointLightHooks::on_intensity_changing(host);
changed = true;
self.intensity.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::INTENSITY);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_intensity_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::INTENSITY_ANIMATED);
if let Some(animation) = host.implicit_animation("Intensity") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::INTENSITY_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::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 {
PointLightHooks::on_intensity_changed(host);
}
}
pub fn min_attenuation_cutoff(&self) -> f32 {
self.min_attenuation_cutoff.get()
}
pub fn set_min_attenuation_cutoff(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.min_attenuation_cutoff.get();
if old_value.differs(&value) {
PointLightHooks::validate_min_attenuation_cutoff_change(host, old_value, value);
PointLightHooks::on_min_attenuation_cutoff_changing(host);
changed = true;
self.min_attenuation_cutoff.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MIN_ATTENUATION_CUTOFF);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_min_attenuation_cutoff_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::MIN_ATTENUATION_CUTOFF_ANIMATED);
if let Some(animation) = host.implicit_animation("MinAttenuationCutoff") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MIN_ATTENUATION_CUTOFF_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::id_of_min_attenuation_cutoff_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "MinAttenuationCutoff", ExpressionVariant::create(value));
}
}
self.min_attenuation_cutoff.set(value);
if changed {
PointLightHooks::on_min_attenuation_cutoff_changed(host);
}
}
pub fn max_attenuation_cutoff(&self) -> f32 {
self.max_attenuation_cutoff.get()
}
pub fn set_max_attenuation_cutoff(&self, host: &dyn PointLightHooks, value: f32) {
let mut changed = false;
let old_value = self.max_attenuation_cutoff.get();
if old_value.differs(&value) {
PointLightHooks::validate_max_attenuation_cutoff_change(host, old_value, value);
PointLightHooks::on_max_attenuation_cutoff_changing(host);
changed = true;
self.max_attenuation_cutoff.set(value);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MAX_ATTENUATION_CUTOFF);
host.register_for_serialization();
host.pending_animations().remove(ServerPointLightProps::id_of_max_attenuation_cutoff_property().base());
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() & !PointLightChangedFields::MAX_ATTENUATION_CUTOFF_ANIMATED);
if let Some(animation) = host.implicit_animation("MaxAttenuationCutoff") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MAX_ATTENUATION_CUTOFF_ANIMATED);
host.pending_animations().set(
ServerPointLightProps::id_of_max_attenuation_cutoff_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "MaxAttenuationCutoff", ExpressionVariant::create(value));
}
}
self.max_attenuation_cutoff.set(value);
if changed {
PointLightHooks::on_max_attenuation_cutoff_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn PointLightHooks,
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(ServerPointLightProps::id_of_color_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::COLOR_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "ConstantAttenuation" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_constant_attenuation_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::CONSTANT_ATTENUATION_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "LinearAttenuation" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_linear_attenuation_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::LINEAR_ATTENUATION_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "QuadraticAttenuation" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_quadratic_attenuation_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::QUADRATIC_ATTENUATION_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Offset" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_offset_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::OFFSET_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "Intensity" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_intensity_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::INTENSITY_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "MinAttenuationCutoff" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_min_attenuation_cutoff_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MIN_ATTENUATION_CUTOFF_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "MaxAttenuationCutoff" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerPointLightProps::id_of_max_attenuation_cutoff_property().base(), server);
self.changed_fields_of_point_light.set(self.changed_fields_of_point_light.get() | PointLightChangedFields::MAX_ATTENUATION_CUTOFF_ANIMATED);
host.register_for_serialization();
return true;
}
false
}
pub fn serialize_changes_core(&self, host: &dyn PointLightHooks, writer: &mut BatchStreamWriter<'_>) {
self.base.serialize_changes_core(host, writer);
let changed = self.changed_fields_of_point_light.get();
writer.write(changed);
if changed.contains(PointLightChangedFields::COLOR_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_color_property().base()));
} else if changed.contains(PointLightChangedFields::COLOR) {
writer.write(self.color.get());
}
if changed.contains(PointLightChangedFields::CONSTANT_ATTENUATION_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_constant_attenuation_property().base()));
} else if changed.contains(PointLightChangedFields::CONSTANT_ATTENUATION) {
writer.write(self.constant_attenuation.get());
}
if changed.contains(PointLightChangedFields::LINEAR_ATTENUATION_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_linear_attenuation_property().base()));
} else if changed.contains(PointLightChangedFields::LINEAR_ATTENUATION) {
writer.write(self.linear_attenuation.get());
}
if changed.contains(PointLightChangedFields::QUADRATIC_ATTENUATION_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_quadratic_attenuation_property().base()));
} else if changed.contains(PointLightChangedFields::QUADRATIC_ATTENUATION) {
writer.write(self.quadratic_attenuation.get());
}
if changed.contains(PointLightChangedFields::COORDINATE_SPACE) {
writer.write_server_object(self.coordinate_space.borrow().clone().map(|o| o.server()));
}
if changed.contains(PointLightChangedFields::OFFSET_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_offset_property().base()));
} else if changed.contains(PointLightChangedFields::OFFSET) {
writer.write(self.offset.get());
}
if changed.contains(PointLightChangedFields::INTENSITY_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_intensity_property().base()));
} else if changed.contains(PointLightChangedFields::INTENSITY) {
writer.write(self.intensity.get());
}
if changed.contains(PointLightChangedFields::MIN_ATTENUATION_CUTOFF_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_min_attenuation_cutoff_property().base()));
} else if changed.contains(PointLightChangedFields::MIN_ATTENUATION_CUTOFF) {
writer.write(self.min_attenuation_cutoff.get());
}
if changed.contains(PointLightChangedFields::MAX_ATTENUATION_CUTOFF_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerPointLightProps::id_of_max_attenuation_cutoff_property().base()));
} else if changed.contains(PointLightChangedFields::MAX_ATTENUATION_CUTOFF) {
writer.write(self.max_attenuation_cutoff.get());
}
self.changed_fields_of_point_light.set(PointLightChangedFields::empty());
}
}
pub trait ServerPointLightHooks: ServerCompositionLightHooks {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: PointLightChangedFields) {}
fn on_coordinate_space_changed(&self) {}
fn on_coordinate_space_changing(&self) {}
}
pub struct ServerPointLightProps {
base: ServerCompositionLightProps,
color: Cell<Color>,
constant_attenuation: Cell<f32>,
linear_attenuation: Cell<f32>,
quadratic_attenuation: Cell<f32>,
coordinate_space: RefCell<Option<Rc<dyn IServerObject>>>,
offset: Cell<Vector3D>,
intensity: Cell<f32>,
min_attenuation_cutoff: Cell<f32>,
max_attenuation_cutoff: Cell<f32>,
}
impl Default for ServerPointLightProps {
fn default() -> Self {
Self::new()
}
}
impl ServerPointLightProps {
pub fn new() -> Self {
Self {
base: ServerCompositionLightProps::new(),
color: Cell::new(Default::default()),
constant_attenuation: Cell::new(Default::default()),
linear_attenuation: Cell::new(Default::default()),
quadratic_attenuation: Cell::new(Default::default()),
coordinate_space: RefCell::new(None),
offset: Cell::new(Default::default()),
intensity: Cell::new(Default::default()),
min_attenuation_cutoff: Cell::new(Default::default()),
max_attenuation_cutoff: 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::<ServerPointLightProps, Color>(
"Color",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).color.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).color.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).color.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).color.set(v.cast_or_default::<Color>())),
)
})
}
pub fn color(&self) -> Color {
self.color.get()
}
pub fn set_color(&self, host: &dyn ServerPointLightHooks, value: Color) {
self.color.set(value);
host.remove_animation_for_property(Self::id_of_color_property().base());
}
pub fn id_of_constant_attenuation_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"ConstantAttenuation",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).constant_attenuation.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).constant_attenuation.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).constant_attenuation.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).constant_attenuation.set(v.cast_or_default::<f32>())),
)
})
}
pub fn constant_attenuation(&self) -> f32 {
self.constant_attenuation.get()
}
pub fn set_constant_attenuation(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.constant_attenuation.set(value);
host.remove_animation_for_property(Self::id_of_constant_attenuation_property().base());
}
pub fn id_of_linear_attenuation_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"LinearAttenuation",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).linear_attenuation.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).linear_attenuation.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).linear_attenuation.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).linear_attenuation.set(v.cast_or_default::<f32>())),
)
})
}
pub fn linear_attenuation(&self) -> f32 {
self.linear_attenuation.get()
}
pub fn set_linear_attenuation(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.linear_attenuation.set(value);
host.remove_animation_for_property(Self::id_of_linear_attenuation_property().base());
}
pub fn id_of_quadratic_attenuation_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"QuadraticAttenuation",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).quadratic_attenuation.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).quadratic_attenuation.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).quadratic_attenuation.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).quadratic_attenuation.set(v.cast_or_default::<f32>())),
)
})
}
pub fn quadratic_attenuation(&self) -> f32 {
self.quadratic_attenuation.get()
}
pub fn set_quadratic_attenuation(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.quadratic_attenuation.set(value);
host.remove_animation_for_property(Self::id_of_quadratic_attenuation_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::<ServerPointLightProps, Option<Rc<dyn IServerObject>>>(
"CoordinateSpace",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).coordinate_space.borrow().clone(),
|obj, v| *props_of::<ServerPointLightProps>(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 ServerPointLightHooks, value: Option<Rc<dyn IServerObject>>) {
let old_value = self.coordinate_space.borrow().clone();
let mut changed = false;
if old_value.differs(&value) {
ServerPointLightHooks::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 {
ServerPointLightHooks::on_coordinate_space_changed(host);
}
}
pub fn id_of_offset_property() -> &'static CompositionPropertyOf<Vector3D> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector3D>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, Vector3D>(
"Offset",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).offset.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).offset.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).offset.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).offset.set(v.cast_or_default::<Vector3D>())),
)
})
}
pub fn offset(&self) -> Vector3D {
self.offset.get()
}
pub fn set_offset(&self, host: &dyn ServerPointLightHooks, value: Vector3D) {
self.offset.set(value);
host.remove_animation_for_property(Self::id_of_offset_property().base());
}
pub fn id_of_intensity_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"Intensity",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).intensity.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).intensity.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).intensity.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).intensity.set(v.cast_or_default::<f32>())),
)
})
}
pub fn intensity(&self) -> f32 {
self.intensity.get()
}
pub fn set_intensity(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.intensity.set(value);
host.remove_animation_for_property(Self::id_of_intensity_property().base());
}
pub fn id_of_min_attenuation_cutoff_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"MinAttenuationCutoff",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).min_attenuation_cutoff.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).min_attenuation_cutoff.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).min_attenuation_cutoff.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).min_attenuation_cutoff.set(v.cast_or_default::<f32>())),
)
})
}
pub fn min_attenuation_cutoff(&self) -> f32 {
self.min_attenuation_cutoff.get()
}
pub fn set_min_attenuation_cutoff(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.min_attenuation_cutoff.set(value);
host.remove_animation_for_property(Self::id_of_min_attenuation_cutoff_property().base());
}
pub fn id_of_max_attenuation_cutoff_property() -> &'static CompositionPropertyOf<f32> {
static PROPERTY: OnceLock<CompositionPropertyOf<f32>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerPointLightProps, f32>(
"MaxAttenuationCutoff",
"ServerPointLight",
|obj| props_of::<ServerPointLightProps>(obj).max_attenuation_cutoff.get(),
|obj, v| props_of::<ServerPointLightProps>(obj).max_attenuation_cutoff.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerPointLightProps>(obj).max_attenuation_cutoff.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerPointLightProps>(obj).max_attenuation_cutoff.set(v.cast_or_default::<f32>())),
)
})
}
pub fn max_attenuation_cutoff(&self) -> f32 {
self.max_attenuation_cutoff.get()
}
pub fn set_max_attenuation_cutoff(&self, host: &dyn ServerPointLightHooks, value: f32) {
self.max_attenuation_cutoff.set(value);
host.remove_animation_for_property(Self::id_of_max_attenuation_cutoff_property().base());
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerPointLightHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
let compositor = host.server_compositor();
self.base.deserialize_changes_core(host, reader, committed_at);
ServerPointLightHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<PointLightChangedFields>();
if changed.contains(PointLightChangedFields::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(PointLightChangedFields::COLOR) {
let value = reader.read::<Color>();
self.set_color(host, value);
}
if changed.contains(PointLightChangedFields::CONSTANT_ATTENUATION_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `ConstantAttenuation` without an animation instance");
};
host.set_animated_value(
Self::id_of_constant_attenuation_property().base(),
ExpressionVariant::create(self.constant_attenuation.get()),
committed_at,
animation,
);
} else if changed.contains(PointLightChangedFields::CONSTANT_ATTENUATION) {
let value = reader.read::<f32>();
self.set_constant_attenuation(host, value);
}
if changed.contains(PointLightChangedFields::LINEAR_ATTENUATION_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `LinearAttenuation` without an animation instance");
};
host.set_animated_value(
Self::id_of_linear_attenuation_property().base(),
ExpressionVariant::create(self.linear_attenuation.get()),
committed_at,
animation,
);
} else if changed.contains(PointLightChangedFields::LINEAR_ATTENUATION) {
let value = reader.read::<f32>();
self.set_linear_attenuation(host, value);
}
if changed.contains(PointLightChangedFields::QUADRATIC_ATTENUATION_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `QuadraticAttenuation` without an animation instance");
};
host.set_animated_value(
Self::id_of_quadratic_attenuation_property().base(),
ExpressionVariant::create(self.quadratic_attenuation.get()),
committed_at,
animation,
);
} else if changed.contains(PointLightChangedFields::QUADRATIC_ATTENUATION) {
let value = reader.read::<f32>();
self.set_quadratic_attenuation(host, value);
}
if changed.contains(PointLightChangedFields::COORDINATE_SPACE) {
let value = read_server_object(reader, compositor.as_ref());
self.set_coordinate_space(host, value);
}
if changed.contains(PointLightChangedFields::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(PointLightChangedFields::OFFSET) {
let value = reader.read::<Vector3D>();
self.set_offset(host, value);
}
if changed.contains(PointLightChangedFields::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(PointLightChangedFields::INTENSITY) {
let value = reader.read::<f32>();
self.set_intensity(host, value);
}
if changed.contains(PointLightChangedFields::MIN_ATTENUATION_CUTOFF_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `MinAttenuationCutoff` without an animation instance");
};
host.set_animated_value(
Self::id_of_min_attenuation_cutoff_property().base(),
ExpressionVariant::create(self.min_attenuation_cutoff.get()),
committed_at,
animation,
);
} else if changed.contains(PointLightChangedFields::MIN_ATTENUATION_CUTOFF) {
let value = reader.read::<f32>();
self.set_min_attenuation_cutoff(host, value);
}
if changed.contains(PointLightChangedFields::MAX_ATTENUATION_CUTOFF_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `MaxAttenuationCutoff` without an animation instance");
};
host.set_animated_value(
Self::id_of_max_attenuation_cutoff_property().base(),
ExpressionVariant::create(self.max_attenuation_cutoff.get()),
committed_at,
animation,
);
} else if changed.contains(PointLightChangedFields::MAX_ATTENUATION_CUTOFF) {
let value = reader.read::<f32>();
self.set_max_attenuation_cutoff(host, value);
}
ServerPointLightHooks::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 == "ConstantAttenuation" {
return Some(Self::id_of_constant_attenuation_property().base());
}
if name == "LinearAttenuation" {
return Some(Self::id_of_linear_attenuation_property().base());
}
if name == "QuadraticAttenuation" {
return Some(Self::id_of_quadratic_attenuation_property().base());
}
if name == "Offset" {
return Some(Self::id_of_offset_property().base());
}
if name == "Intensity" {
return Some(Self::id_of_intensity_property().base());
}
if name == "MinAttenuationCutoff" {
return Some(Self::id_of_min_attenuation_cutoff_property().base());
}
if name == "MaxAttenuationCutoff" {
return Some(Self::id_of_max_attenuation_cutoff_property().base());
}
ServerCompositionLightProps::get_composition_property(name)
}
}