1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
use super::*;
/// A `ValueAccessor` is used to set/get/animate a component value.
/// `bool` and `i64` types will be set to target directly when using `animate`.
///
/// Example:
/// ```
/// let ball_mesh = ...;
/// let ball_entity = Entity::create("ball");
/// ball_entity.render().mesh().set(ball_mesh);
/// ball_entity
/// .transform()
/// .position() // ValueAccessor returned here
/// .set(Vec3::ZERO);
/// let duration = 1.0; // Duration is in seconds
/// ball_entity
/// .transform()
/// .position() // ValueAccessor returned here
/// .animate(Vec3::new(0.0, 2.5, -40.0), duration);
/// ```
pub struct ValueAccessorReadWriteAnimate<T> {
id: Entity,
component: ComponentType,
param: u32,
_marker: std::marker::PhantomData<T>,
}
/// Struct describing an animation request.
#[allow(missing_docs)]
#[derive(Copy, Clone)]
pub struct AnimationRequest {
pub id: Entity,
pub component: ComponentType,
pub param: u32,
pub target: Value,
pub options: AnimationOptions,
pub enqueue: bool,
}
/// An `AnimationBuilder` is used to build an animation for a component value.
///
/// Set options on it such as curve / easing type, delay, target and finally submit the animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to their target directly,
/// however `delay` can be used to delay the set of the value.
pub struct AnimationBuilder<T> {
request: AnimationRequest,
_marker: std::marker::PhantomData<T>,
}
impl<T> AnimationBuilder<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Creates an `AnimationBuilder` from a `ValueAccessor`.
pub fn new(accessor: &ValueAccessorReadWriteAnimate<T>) -> Self {
Self {
_marker: std::marker::PhantomData,
request: AnimationRequest {
id: accessor.id,
component: accessor.component,
param: accessor.param,
options: AnimationOptions {
curve: AnimationCurve::Linear,
duration: 0.0,
loop_count: 1,
delay: 0.0,
smoothing: 0.0,
},
target: Value::none(),
enqueue: false,
},
}
}
/// Sets the delay of the animation (how long to wait before it starts)
pub fn delay(&mut self, seconds: f32) -> &mut Self {
self.request.options.delay = seconds;
self
}
/// Sets the `target` value that the animated value will approach.
pub fn target(&mut self, target: T, duration: f32) -> &mut Self {
self.request.target = <ValueConverter as ValueConverterTrait<T>>::into_value(target);
self.request.options.duration = duration;
self
}
/// Sets the curve shape of the value animation.
pub fn curve(&mut self, curve: AnimationCurve) -> &mut Self {
self.request.options.curve = curve;
self
}
/// Sets the number of times the animation will loop. 0 is currently undefined.
pub fn loop_count(&mut self, loop_count: u32) -> &mut Self {
self.request.options.loop_count = loop_count;
self
}
/// Sets the method of smoothing when the animation target changes.
pub fn smoothing(&mut self, smoothing: f32) -> &mut Self {
self.request.options.smoothing = smoothing;
self
}
/// Enqueues the animation instead of cancelling a currently on-going animation (if any).
pub fn enqueue(&mut self) -> &mut Self {
self.request.enqueue = true;
self
}
/// Finalizes the animation.
pub fn submit(&self) {
Animation::enqueue_request_in_global_queue(&self.request);
}
/// Finalizes the animation.
///
/// Also gives you the opportunity to pass in a `callback` that will be called
/// once the animation is finished. For this to work, you must call `entity_messenger().process_messages()`
/// from your `world_update` function.
pub fn submit_messenger(&self, callback: Option<Box<AnimationReplyFn<'static>>>) {
if let Some(callback) = callback {
EntityMessageDispatcher::global().animation_in_global_queue(&self.request, callback);
} else {
EntityMessenger::get()
.global_queue()
.animate_value_no_reply(&self.request);
}
}
#[allow(unused)]
#[inline]
fn build_request(&self) -> AnimationRequest {
self.request
}
}
impl<T> std::ops::Deref for AnimationBuilder<T>
where
ValueConverter: ValueConverterTrait<T>,
{
type Target = AnimationRequest;
#[inline]
fn deref(&self) -> &Self::Target {
&self.request
}
}
/// Value accessor with read-only access
pub struct ValueAccessorRead<T> {
va: ValueAccessorReadWriteAnimate<T>,
}
impl<T> ValueAccessorRead<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Create a new value accessor with read-only access
#[inline]
pub fn new(id: Entity, component: ComponentType, param: u32) -> Self {
Self {
va: ValueAccessorReadWriteAnimate::new(id, component, param),
}
}
/// Returns the current value.
#[inline]
pub fn get(&self) -> T {
self.va.get()
}
}
/// Value accessor with read-write-access
pub struct ValueAccessorReadWrite<T> {
va: ValueAccessorReadWriteAnimate<T>,
}
impl<T> ValueAccessorReadWrite<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Create a new value accessor with read-write access
#[inline]
pub fn new(id: Entity, component: ComponentType, param: u32) -> Self {
Self {
va: ValueAccessorReadWriteAnimate::new(id, component, param),
}
}
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
#[inline]
pub fn set(&self, v: T) {
self.va.set(v);
}
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// In contrast to `set` this function will return an error if it receives an
/// invalid value, such as a NaN or unnormalized quaternion.
/// Useful to use if you don't want to panic because of unknown user input.
#[inline]
pub fn try_set(&self, v: T) -> Result<(), EntityValueError> {
self.va.try_set(v)
}
/// Returns the current value.
#[inline]
pub fn get(&self) -> T {
self.va.get()
}
}
/// Value accessor with read-write-access to data objects.
pub struct ValueAccessorDataReadWrite<T> {
va: ValueAccessorReadWriteAnimate<T>,
}
impl<T> ValueAccessorDataReadWrite<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Create a new value accessor with read-write access
#[inline]
pub fn new(id: Entity, component: ComponentType, param: u32) -> Self {
Self {
va: ValueAccessorReadWriteAnimate::new(id, component, param),
}
}
/// Returns the current data handle.
#[inline]
fn get_handle(&self) -> DataHandle {
let v = World::get_entity_value(self.va.id, self.va.component, self.va.param);
// This will never retain
<ValueConverter as ValueConverterTrait<DataHandle>>::from_value(&v)
}
fn set_handle(&self, handle: DataHandle) {
World::set_entity_value(
self.va.id,
self.va.component,
self.va.param,
&Value::from_i64(handle.as_ffi() as i64),
);
}
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
pub fn set(&self, v: T) {
let old_handle = self.get_handle();
self.va.set(v); // moved here thus drop will never be called here.
let new_handle = self.get_handle();
if old_handle.is_valid() && (old_handle != new_handle) {
World::destroy_data(old_handle); // We release the data we just overwrote.
}
}
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// Useful to use if you don't want to panic because of unknown user input.
pub fn try_set(&self, v: T) -> Result<(), EntityValueError> {
let old_handle = self.get_handle();
self.va.try_set(v)?; // moved here thus drop will never be called here.
let new_handle = self.get_handle();
if old_handle.is_valid() && (old_handle != new_handle) {
World::destroy_data(old_handle); // We release the data we just overwrote.
}
Ok(())
}
/// Resets the value to an invalid data handle.
pub fn reset(&self) {
let old_handle = self.get_handle();
if old_handle.is_valid() {
World::destroy_data(old_handle); // We release the data we will overwrite.
}
self.set_handle(DataHandle::invalid());
}
/// Returns the current value.
#[inline]
pub fn get(&self) -> T {
self.va.get()
}
}
/// Trait for writing component values
pub trait ValueAccessorWriteTrait<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
fn set(&self, v: T);
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// In contrast to `set` this function will return an error if it receives an
/// invalid value, such as a NaN or unnormalized quaternion.
/// Useful to use if you don't want to panic because of unknown user input.
fn try_set(&self, v: T) -> Result<(), EntityValueError>;
}
/// Trait for reading component values
pub trait ValueAccessorReadTrait<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Returns the current value.
fn get(&self) -> T;
}
/// Trait for animating component values
pub trait ValueAccessorAnimateTrait<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Will linearly animate / transition the value from the current value to the
/// target `v` using `duration` (in seconds).
///
/// Will stop and continue from any ongoing animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to
/// their target directly, however delay can be used to delay the set of the value.
fn animate(&self, v: T, duration: f32);
/// Will linearly animate / transition the value from the current value to the
// target `v` using `duration` (in seconds) and apply a smooth filter to the output.
///
/// Will stop and continue from any ongoing animation.
/// A smoothing constant of 0 means no smoothing, higher value means more smoothing
/// (i.e. the value will take a longer time to change).
///
/// Useful if you want to smoothen out discontinuities when animating to new targets,
/// cancelling the old animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to their
/// target directly, however delay can be used to delay the set of the value.
fn animate_smooth(&self, v: T, duration: f32, smoothing: f32);
/// Starts setting up an animation of this value.
///
/// Call methods on the returned `AnimationBuilder` to configure it.
fn build_animation(&self) -> AnimationBuilder<T>;
}
impl<T> ValueAccessorReadWriteAnimate<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Create a new value accessor for a specific entity, component and parameter.
#[inline]
pub fn new(id: Entity, component: ComponentType, param: u32) -> Self
where
T: Sized,
{
Self {
id,
component,
param,
_marker: std::marker::PhantomData,
}
}
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
#[inline]
pub fn set(&self, v: T) {
World::set_entity_value(
self.id,
self.component,
self.param,
&<ValueConverter as ValueConverterTrait<T>>::into_value(v),
);
}
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// In contrast to `set` this function will return an error if it receives an
/// invalid value, such as a NaN or unnormalized quaternion.
/// Useful to use if you don't want to panic because of unknown user input.
#[inline]
pub fn try_set(&self, v: T) -> Result<(), EntityValueError> {
World::try_set_entity_value(
self.id,
self.component,
self.param,
&<ValueConverter as ValueConverterTrait<T>>::into_value(v),
)
}
/// Returns the current value.
#[inline]
pub fn get(&self) -> T {
let v = World::get_entity_value(self.id, self.component, self.param);
<ValueConverter as ValueConverterTrait<T>>::from_value(&v)
}
/// Will linearly animate / transition the value from the current value to the
/// target `v` using `duration` (in seconds).
///
/// Will stop and continue from any ongoing animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to
/// their target directly, however delay can be used to delay the set of the value.
pub fn animate(&self, v: T, duration: f32) {
AnimationBuilder::<T>::new(self)
.target(v, duration)
.submit();
}
/// Will linearly animate / transition the value from the current value to the
// target `v` using `duration` (in seconds) and apply a smooth filter to the output.
///
/// Will stop and continue from any ongoing animation.
/// A smoothing constant of 0 means no smoothing, higher value means more smoothing
/// (i.e. the value will take a longer time to change).
///
/// Useful if you want to smoothen out discontinuities when animating to new targets,
/// cancelling the old animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to their
/// target directly, however delay can be used to delay the set of the value.
pub fn animate_smooth(&self, v: T, duration: f32, smoothing: f32) {
AnimationBuilder::<T>::new(self)
.target(v, duration)
.smoothing(smoothing)
.submit();
}
/// Starts setting up an animation of this value.
///
/// Call methods on the returned `AnimationBuilder` to configure it.
pub fn build_animation(&self) -> AnimationBuilder<T> {
AnimationBuilder::<T>::new(self)
}
}
impl<T> ValueAccessorReadTrait<T> for ValueAccessorRead<T>
where
ValueConverter: ValueConverterTrait<T>,
{
#[inline]
fn get(&self) -> T {
self.va.get()
}
}
impl<T> ValueAccessorReadTrait<T> for ValueAccessorReadWrite<T>
where
ValueConverter: ValueConverterTrait<T>,
{
#[inline]
fn get(&self) -> T {
self.get()
}
}
/// Implementation of trait for reading component values
impl<T> ValueAccessorReadTrait<T> for ValueAccessorReadWriteAnimate<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Returns the current value.
#[inline]
fn get(&self) -> T {
self.get()
}
}
impl<T> ValueAccessorWriteTrait<T> for ValueAccessorReadWrite<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
#[inline]
fn set(&self, v: T) {
self.set(v);
}
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// In contrast to `set` this function will return an error if it receives an
/// invalid value, such as a NaN or unnormalized quaternion.
/// Useful to use if you don't want to panic because of unknown user input.
#[inline]
fn try_set(&self, v: T) -> Result<(), EntityValueError> {
self.try_set(v)
}
}
/// Implementation of trait for writing component values
impl<T> ValueAccessorWriteTrait<T> for ValueAccessorReadWriteAnimate<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Sets the value to `v`.
///
/// Will panic if it receives an invalid value, such as a NaN or unnormalized quaternion.
#[inline]
fn set(&self, v: T) {
self.set(v);
}
/// Tries to set the value to `v`, returns `EntityValueError` if it failed.
///
/// In contrast to `set` this function will return an error if it receives an
/// invalid value, such as a NaN or unnormalized quaternion.
/// Useful to use if you don't want to panic because of unknown user input.
#[inline]
fn try_set(&self, v: T) -> Result<(), EntityValueError> {
self.try_set(v)
}
}
/// Implementation of trait for animating component values
impl<T> ValueAccessorAnimateTrait<T> for ValueAccessorReadWriteAnimate<T>
where
ValueConverter: ValueConverterTrait<T>,
{
/// Will linearly animate / transition the value from the current value to the
/// target `v` using `duration` (in seconds).
///
/// Will stop and continue from any ongoing animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to
/// their target directly, however delay can be used to delay the set of the value.
fn animate(&self, v: T, duration: f32) {
self.animate(v, duration);
}
/// Will linearly animate / transition the value from the current value to the
// target `v` using `duration` (in seconds) and apply a smooth filter to the output.
///
/// Will stop and continue from any ongoing animation.
/// A smoothing constant of 0 means no smoothing, higher value means more smoothing
/// (i.e. the value will take a longer time to change).
///
/// Useful if you want to smoothen out discontinuities when animating to new targets,
/// cancelling the old animation.
///
/// Note: bools and integers (also all kinds of entity references) will be set to their
/// target directly, however delay can be used to delay the set of the value.
fn animate_smooth(&self, v: T, duration: f32, smoothing: f32) {
self.animate_smooth(v, duration, smoothing);
}
/// Starts setting up an animation of this value.
///
/// Call methods on the returned `AnimationBuilder` to configure it.
fn build_animation(&self) -> AnimationBuilder<T> {
self.build_animation()
}
}
#[macro_export]
/// Macro for implementing standard accessor trait
macro_rules! impl_world_accessor {
($(#[$attr: meta])* $component_type: ident, $param_name: ident, $param_type: ty, $accessor_name: ident, $visibility: ident) => {
$(#[$attr])*
#[inline]
pub fn $accessor_name(&self) -> $visibility<$param_type> {
use ffi::$component_type;
$visibility::new(self.id, ffi::ComponentType::$component_type, $component_type::$param_name.into())
}
};
}
#[macro_export]
/// Macro for implementing standard accessor trait, but with an index parameter.
macro_rules! impl_world_accessor_indexed {
($(#[$attr: meta])* $component_type: ident, $param_name: ident, $param_type: ty, $accessor_name: ident, $index_type: ty, $visibility: ident) => {
$(#[$attr])*
#[inline]
pub fn $accessor_name(&self, index: $index_type) -> $visibility<$param_type> {
use ffi::$component_type;
let param_index: u32 = $component_type::$param_name.into();
$visibility::new(self.id, ffi::ComponentType::$component_type, param_index + index as u32)
// TODO: If index is out of range, maybe do something?
}
};
}