/// <summary>
        ///     Applies the modifier to the provided value
        /// </summary>
        /// <param name="currentValue">The value to apply the modifier to, should be of the same type as the data binding target</param>
        /// <returns>The modified value</returns>
        public object Apply(object?currentValue)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DataBindingModifier");
            }

            if (ModifierType == null)
            {
                return(currentValue);
            }

            if (!ModifierType.SupportsParameter)
            {
                return(ModifierType.Apply(currentValue, null));
            }

            if (ParameterType == ProfileRightSideType.Dynamic && ParameterPath != null && ParameterPath.IsValid)
            {
                object?value = ParameterPath.GetValue();
                return(ModifierType.Apply(currentValue, value));
            }

            if (ParameterType == ProfileRightSideType.Static)
            {
                return(ModifierType.Apply(currentValue, ParameterStaticValue));
            }

            return(currentValue);
        }
        /// <inheritdoc />
        public void Save()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DataBindingModifier");
            }

            // Don't save an invalid state
            if (ParameterPath != null && !ParameterPath.IsValid)
            {
                return;
            }

            if (!DirectDataBinding.Entity.Modifiers.Contains(Entity))
            {
                DirectDataBinding.Entity.Modifiers.Add(Entity);
            }

            // Modifier
            if (ModifierType != null)
            {
                Entity.ModifierType           = ModifierType.GetType().Name;
                Entity.ModifierTypePluginGuid = ModifierType.PluginInfo.Guid;
            }

            // General
            Entity.Order         = Order;
            Entity.ParameterType = (int)ParameterType;

            // Parameter
            ParameterPath?.Save();
            Entity.ParameterPath = ParameterPath?.Entity;

            Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue);
        }
        /// <summary>
        ///     Updates the parameter of the modifier, makes the modifier static and re-compiles the expression
        /// </summary>
        /// <param name="staticValue">The static value to use as a parameter</param>
        public void UpdateParameterStatic(object staticValue)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DataBindingModifier");
            }

            ParameterType = ProfileRightSideType.Static;
            ParameterPath?.Dispose();
            ParameterPath = null;

            Type parameterType = ModifierType?.ParameterType ?? DirectDataBinding.DataBinding.GetTargetType();

            // If not null ensure the types match and if not, convert it
            if (staticValue != null && staticValue.GetType() == parameterType)
            {
                ParameterStaticValue = staticValue;
            }
            else if (staticValue != null)
            {
                ParameterStaticValue = Convert.ChangeType(staticValue, parameterType);
            }
            // If null create a default instance for value types or simply make it null for reference types
            else if (parameterType.IsValueType)
            {
                ParameterStaticValue = Activator.CreateInstance(parameterType);
            }
            else
            {
                ParameterStaticValue = null;
            }
        }
Beispiel #4
0
        private void ValidateParameter()
        {
            if (ModifierType == null)
            {
                return;
            }

            if (ParameterType == ProfileRightSideType.Dynamic)
            {
                if (ParameterPath == null || !ParameterPath.IsValid)
                {
                    return;
                }

                Type parameterType = ParameterPath.GetPropertyType() !;
                if (!ModifierType.SupportsType(parameterType, ModifierTypePart.Parameter))
                {
                    UpdateParameterDynamic(null);
                }
            }
            else
            {
                if (ParameterStaticValue == null)
                {
                    return;
                }

                if (!ModifierType.SupportsType(ParameterStaticValue.GetType(), ModifierTypePart.Parameter))
                {
                    UpdateParameterStatic(null);
                }
            }
        }
        /// <inheritdoc />
        public void Dispose()
        {
            _disposed = true;

            DataBindingModifierTypeStore.DataBindingModifierAdded   -= DataBindingModifierTypeStoreOnDataBindingModifierAdded;
            DataBindingModifierTypeStore.DataBindingModifierRemoved -= DataBindingModifierTypeStoreOnDataBindingModifierRemoved;

            ParameterPath?.Dispose();
        }
Beispiel #6
0
        public void Test1()
        {
            var container1 = new ParameterCollection("container1");
            var container2 = new ParameterCollection("container2");
            var container3 = new ParameterCollection("container3");
            var container4 = new ParameterCollection("container4");

#pragma warning disable 612 // for ParameterPath and ParameterListener
            var path     = new ParameterPath(PropertyContainer, PropertyContainer, PropertyContainer, PropertyFloat);
            var listener = new ParameterListener(container1, path);
#pragma warning restore 612

            int    updateCounter = 0;
            object currentValue  = null;

            listener.ParameterUpdated += (container, path2, newValue) => { updateCounter++; currentValue = newValue; };

            container1.Set(PropertyContainer, container2);
            container2.Set(PropertyContainer, container2);
            Assert.AreEqual(0, updateCounter);
            Assert.AreEqual(null, currentValue);

            // New value should have been detected (through cycle)
            container2.Set(PropertyFloat, 32.0f);
            Assert.AreEqual(1, updateCounter);
            Assert.AreEqual(32.0f, currentValue);

            // Setting it again should not trigger anything
            container2.Set(PropertyFloat, 32.0f);
            Assert.AreEqual(1, updateCounter);

            // Value should be resetted (because container3 doesn't point on any value)
            container2.Set(PropertyContainer, container3);
            Assert.AreEqual(2, updateCounter);
            Assert.AreEqual(null, currentValue);

            // New value should come from container3->container4
            container3.Set(PropertyContainer, container4);
            container4.Set(PropertyFloat, 48.0f);
            Assert.AreEqual(3, updateCounter);
            Assert.AreEqual(48.0f, currentValue);

            // Value should be again the one from container2
            container2.Set(PropertyContainer, container2);
            Assert.AreEqual(4, updateCounter);
            Assert.AreEqual(32.0f, currentValue);

            // Value should be resetted
            container2.Remove(PropertyFloat);
            Assert.AreEqual(5, updateCounter);
            Assert.AreEqual(null, currentValue);

            container1.Remove(PropertyContainer);
            Assert.AreEqual(5, updateCounter);
            Assert.AreEqual(null, currentValue);
        }
Beispiel #7
0
        /// <summary>
        ///     Releases the unmanaged resources used by the object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">
        ///     <see langword="true" /> to release both managed and unmanaged resources;
        ///     <see langword="false" /> to release only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _disposed = true;

                DataBindingModifierTypeStore.DataBindingModifierAdded   -= DataBindingModifierTypeStoreOnDataBindingModifierAdded;
                DataBindingModifierTypeStore.DataBindingModifierRemoved -= DataBindingModifierTypeStoreOnDataBindingModifierRemoved;

                ParameterPath?.Dispose();
            }
        }
        /// <summary>
        ///     Updates the parameter of the modifier and makes the modifier dynamic
        /// </summary>
        /// <param name="path">The path pointing to the parameter</param>
        public void UpdateParameterDynamic(DataModelPath?path)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DataBindingModifier");
            }

            if (path != null && !path.IsValid)
            {
                throw new ArtemisCoreException("Cannot update parameter of data binding modifier to an invalid path");
            }

            ParameterPath?.Dispose();
            ParameterPath = path != null ? new DataModelPath(path) : null;

            ParameterType = ProfileRightSideType.Dynamic;
        }