Example #1
0
        public IAnimatableProperty <T> AsAnimatable()
        {
            // Try to get existing animatable.
            IGamePropertyData untypedData = Owner.PropertyData.Get(_metadata.Id);
            AnimatableGamePropertyData <T> animatableData = untypedData as AnimatableGamePropertyData <T>;

            if (animatableData != null)
            {
                return(animatableData);
            }

            // Create new animatable data.
            animatableData = new AnimatableGamePropertyData <T>(_owner, _metadata, untypedData as GamePropertyData <T>);
            Owner.PropertyData.Set(_metadata.Id, animatableData);
            return(animatableData);
        }
Example #2
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        private GamePropertyData <T> GetOrCreateLocalData()
        {
            IGamePropertyData    untypedData = Owner.PropertyData.Get(_metadata.Id);
            GamePropertyData <T> data;

            if (untypedData != null)
            {
                // data found!
                data = (GamePropertyData <T>)untypedData;
            }
            else
            {
                // No data found! Create new data.
                data = new GamePropertyData <T>();
                Owner.PropertyData.Set(_metadata.Id, data);
            }
            return(data);
        }
Example #3
0
        /// <inheritdoc/>
        public void Reset()
        {
            // Get local data.
            IGamePropertyData untypedData = Owner.PropertyData.Get(_metadata.Id);

            // Nothing to do if we have no local data.
            if (untypedData == null)
            {
                return;
            }

            // Nothing to do if the local data uses the default value.
            if (!untypedData.HasLocalValue)
            {
                return;
            }

            // Assert: We have a local value and must remove it.

            var data = ((GamePropertyData <T>)untypedData);

            // If the value is animated, changing the BaseValue does not cause events.
            var animatableData = untypedData as AnimatableGamePropertyData <T>;

            // Remember current value.
            T    oldValue;
            bool isAnimated;

            if (animatableData != null)
            {
                oldValue   = animatableData.BaseValue;
                isAnimated = animatableData.IsAnimated;
            }
            else
            {
                oldValue   = data.Value;
                isAnimated = false;
            }

            // Get the target default value from template or metadata.
            T defaultValue;

            if (Owner.Template != null)
            {
                defaultValue = new GameProperty <T>(Owner.Template, _metadata).Value;
            }
            else
            {
                defaultValue = _metadata.DefaultValue;
            }

            if (isAnimated || EqualityComparer <T> .Default.Equals(oldValue, defaultValue))
            {
                // oldValue and defaultValue are the same. We only have to reset the flag.
                data.HasLocalValue = false;
                data.Value         = default(T);
                return;
            }

            var newValue = defaultValue;

            // Raise Changing event.
            data.OnChanging(this, oldValue, ref newValue);

            if (EqualityComparer <T> .Default.Equals(defaultValue, newValue))
            {
                // After coercion in Changing, the target value is still the default value.

                // Value has been reset.
                data.HasLocalValue = false;
                data.Value         = default(T);

                // Raise Changed event if oldValue is different from the newValue.
                if (!EqualityComparer <T> .Default.Equals(oldValue, defaultValue))
                {
                    data.OnChanged(this, oldValue, defaultValue);
                }
            }
            else
            {
                // Value was overwritten by Changing event handler! - We still have a local value!
                // Raise Changed event if oldValue is different from the newValue.
                if (!EqualityComparer <T> .Default.Equals(oldValue, newValue))
                {
                    data.Value = newValue;
                    data.OnChanged(this, oldValue, newValue);
                }
            }
        }