/// <summary>
 /// When overridden in the derived class, sets the values read from the Read method.
 /// </summary>
 /// <param name="conditional">The conditional.</param>
 /// <param name="not">The Not value.</param>
 /// <param name="parameters">The parameters.</param>
 protected override void SetReadValues(NPCChatConditionalBase conditional, bool not,
                                       NPCChatConditionalParameter[] parameters)
 {
     _conditional = conditional;
     _not         = not;
     _parameters  = parameters;
 }
 /// <summary>
 /// When overridden in the derived class, sets the values read from the Read method.
 /// </summary>
 /// <param name="conditional">The conditional.</param>
 /// <param name="not">The Not value.</param>
 /// <param name="parameters">The parameters.</param>
 protected override void SetReadValues(NPCChatConditionalBase conditional, bool not,
                                       NPCChatConditionalParameter[] parameters)
 {
     _conditional = conditional;
     _not = not;
     _parameters = parameters;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="EditorNPCChatConditionalCollectionItem"/> class.
        /// </summary>
        /// <param name="conditional">The conditional to use.</param>
        /// <exception cref="ArgumentNullException"><paramref name="conditional" /> is <c>null</c>.</exception>
        public EditorNPCChatConditionalCollectionItem(NPCChatConditionalBase conditional)
        {
            if (conditional == null)
                throw new ArgumentNullException("conditional");

            _not = false;
            SetConditional(conditional);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="NPCChatConditionalEditorForm"/> class.
        /// </summary>
        /// <param name="conditionalItem">The conditional item.</param>
        /// <param name="conditionals">The conditionals.</param>
        /// <exception cref="ArgumentNullException"><paramref name="conditionalItem" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="conditionals" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="conditionals"/> array cannot be empty.</exception>
        public NPCChatConditionalEditorForm(EditorNPCChatConditionalCollectionItem conditionalItem,
                                            NPCChatConditionalBase[] conditionals)
        {
            if (conditionalItem == null)
                throw new ArgumentNullException("conditionalItem");
            if (conditionals == null)
                throw new ArgumentNullException("conditionals");
            if (conditionals.Length == 0)
                throw new ArgumentException("Conditionals array cannot be empty.", "conditionals");

            _item = conditionalItem;
            _conditionals = conditionals;

            InitializeComponent();
        }
        /// <summary>
        /// Reads the values for this <see cref="NPCChatConditionalCollectionItemBase"/> from an <see cref="IValueReader"/>.
        /// </summary>
        /// <param name="reader"><see cref="IValueReader"/> to read the values from.</param>
        /// <exception cref="ArgumentException">The read <see cref="NPCChatConditionalBase"/> was invalid.</exception>
        protected void Read(IValueReader reader)
        {
            var not             = reader.ReadBool("Not");
            var conditionalName = reader.ReadString("ConditionalName");
            var parameters      = reader.ReadManyNodes("Parameters", NPCChatConditionalParameter.Read);

            var conditional = NPCChatConditionalBase.GetConditional(conditionalName);

            if (conditional == null)
            {
                const string errmsg = "Failed to get conditional `{0}`.";
                throw new ArgumentException(string.Format(errmsg, conditionalName), "reader");
            }

            SetReadValues(conditional, not, parameters);
        }
        /// <summary>
        /// When overridden in the derived class, sets the values read from the Read method.
        /// </summary>
        /// <param name="conditional">The conditional.</param>
        /// <param name="not">The Not value.</param>
        /// <param name="parameters">The parameters.</param>
        protected override void SetReadValues(NPCChatConditionalBase conditional, bool not,
                                              NPCChatConditionalParameter[] parameters)
        {
            _conditional = conditional;
            _not = not;
            _parameters.Clear();
            _parameters.AddRange(parameters);

            if (Changed != null)
                Changed.Raise(this, EventArgs.Empty);
        }
        /// <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 to get the <see cref="NPCChatConditionalBase"/> with the given <paramref name="name"/>.
 /// </summary>
 /// <param name="name">The name of the <see cref="NPCChatConditionalBase"/> to get.</param>
 /// <param name="value">If the method returns true, contains the <see cref="NPCChatConditionalBase"/>
 /// with the given <paramref name="name"/>.</param>
 /// <returns>True if the <paramref name="value"/> was successfully acquired; otherwise false.</returns>
 public static bool TryGetResponseAction(string name, out NPCChatConditionalBase value)
 {
     return _instances.TryGetValue(name, out value);
 }
 /// <summary>
 /// When overridden in the derived class, sets the values read from the Read method.
 /// </summary>
 /// <param name="conditional">The conditional.</param>
 /// <param name="not">The Not value.</param>
 /// <param name="parameters">The parameters.</param>
 protected abstract void SetReadValues(NPCChatConditionalBase conditional, bool not,
                                       NPCChatConditionalParameter[] parameters);
 /// <summary>
 /// When overridden in the derived class, sets the values read from the Read method.
 /// </summary>
 /// <param name="conditional">The conditional.</param>
 /// <param name="not">The Not value.</param>
 /// <param name="parameters">The parameters.</param>
 protected abstract void SetReadValues(NPCChatConditionalBase conditional, bool not,
                                       NPCChatConditionalParameter[] parameters);
        /// <summary>
        /// Edits the SelectedConditionalItem.
        /// </summary>
        /// <param name="npcChatConditionals">The NPCChatConditionalBases that can be chosen.</param>
        public void EditCurrentItem(NPCChatConditionalBase[] npcChatConditionals)
        {
            if (SelectedConditionalItem == null)
                return;

            var conditionalEditorForm = new NPCChatConditionalEditorForm(SelectedConditionalItem, npcChatConditionals)
            { StartPosition = FormStartPosition.CenterScreen };
            conditionalEditorForm.Show();
            conditionalEditorForm.FormClosed += conditionalEditorForm_FormClosed;
        }
 /// <summary>
 /// Tries to get the <see cref="NPCChatConditionalBase"/> with the given <paramref name="name"/>.
 /// </summary>
 /// <param name="name">The name of the <see cref="NPCChatConditionalBase"/> to get.</param>
 /// <param name="value">If the method returns true, contains the <see cref="NPCChatConditionalBase"/>
 /// with the given <paramref name="name"/>.</param>
 /// <returns>True if the <paramref name="value"/> was successfully acquired; otherwise false.</returns>
 public static bool TryGetResponseAction(string name, out NPCChatConditionalBase value)
 {
     return(_instances.TryGetValue(name, out value));
 }