Example #1
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ParameterListItem"/> class.
            /// </summary>
            /// <param name="parameter">The <see cref="NPCChatConditionalParameter"/>.</param>
            /// <exception cref="ArgumentNullException"><paramref name="parameter" /> is <c>null</c>.</exception>
            public ParameterListItem(NPCChatConditionalParameter parameter)
            {
                if (parameter == null)
                {
                    throw new ArgumentNullException("parameter");
                }

                _parameter = parameter;
            }
        /// <summary>
        /// Sets the Conditional.
        /// </summary>
        /// <param name="conditional">The new <see cref="NPCChatConditionalBase"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="conditional" /> is <c>null</c>.</exception>
        public void SetConditional(NPCChatConditionalBase conditional)
        {
            if (conditional == null)
            {
                throw new ArgumentNullException("conditional");
            }

            if (_conditional == conditional)
            {
                return;
            }

            // Set the new conditional
            _conditional = conditional;

            // Re-create all the parameters to the appropriate type for the new conditional
            var newParameters = new NPCChatConditionalParameter[Conditional.ParameterCount];

            for (var i = 0; i < Conditional.ParameterCount; i++)
            {
                var neededType = Conditional.GetParameter(i);
                if (i < _parameters.Count && _parameters[i].ValueType == neededType)
                {
                    // The type matches the old type, so just reuse it
                    newParameters[i] = _parameters[i];
                }
                else
                {
                    // Different type or out of range, so make a new one
                    newParameters[i] = NPCChatConditionalParameter.CreateParameter(neededType);
                }
            }

            // Set the new parameters
            _parameters.Clear();
            _parameters.AddRange(newParameters);

            if (Changed != null)
            {
                Changed.Raise(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// Tries the set one of the Parameters.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="value">The value.</param>
        /// <returns>True if set successfully; otherwise false.</returns>
        public bool TrySetParameter(int index, NPCChatConditionalParameter value)
        {
            if (index < 0)
            {
                return(false);
            }
            if (index >= _parameters.Count)
            {
                return(false);
            }
            if (value == null)
            {
                return(false);
            }

            _parameters[index] = value;

            if (Changed != null)
            {
                Changed.Raise(this, EventArgs.Empty);
            }

            return(true);
        }