/// <summary>
        /// Handles the Click event of the variableAddButton control.
        /// Creates and adds the new named variable, if the name of named variable is valid.
        /// </summary>
        private void variableAddButton_Click(object sender, EventArgs e)
        {
            VariableTypeItem selected = variableTypeComboBox.SelectedItem as VariableTypeItem;

            if (selected != null)
            {
                Debug.Assert(ScriptingComponent != null, "Scripting Component is not set.");

                if (!IsNameValid(variableNameTextBox.Text, null, true))
                {
                    return;
                }

                // create new named variable
                NamedVariable variable = new NamedVariable(ScriptingComponent, selected.VariableType);

                // set name
                variable.Name            = variableNameTextBox.Text;
                variableNameTextBox.Text = String.Empty;

                ScriptingComponent.Variables.Add(variable);

                Messages.ShowInfo("Variable added.");
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        protected Variable(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            if (ctxt.State == StreamingContextStates.Clone)
            {
                _actorIdForDeserialization = info.GetInt32("ActorId");

                if (_actorIdForDeserialization != -1)
                {
                    _namedVariableNameForDeserialization   = info.GetString("NamedVariableName");
                    _namedVariableFindAtForDeserialization = info.GetInt32("FindAt");
                }
                else
                {
                    _value = (IVariable)info.GetValue("Value", typeof(IVariable));
                }
            }
            else if (ctxt.State == StreamingContextStates.Persistence)
            {
                _namedVariable = (NamedVariable)info.GetValue("NamedVariable", typeof(NamedVariable));

                if (_namedVariable == null)
                {
                    _value = (IVariable)info.GetValue("Value", typeof(IVariable));
                }
                else
                {
                    _value = _namedVariable.Value;
                }
            }
        }
        /// <summary>
        /// Handles the CellContentClick event of the table control.
        /// When clicked on the remove button then the named variable is removed.
        /// When clicked on the button for adding to the scene the named variable is added to the scene.
        /// </summary>
        private void table_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                NamedVariable variable = settingsView.table.Rows[e.RowIndex].Tag as NamedVariable;

                if (variable != null)
                {
                    if (ScriptingComponent == null)
                    {
                        throw new Exception("Scripting Component is not set.");
                    }

                    if (new ConsistentDeletionForm(new ConsistentDeletionHelper.ScriptNamedVariableForDeletion(variable)).ShowDialog() == DialogResult.OK)
                    {
                        Messages.ShowInfo("Variable deleted.");
                    }
                }
            }
            else if (e.ColumnIndex == 4)
            {
                NamedVariable variable = settingsView.table.Rows[e.RowIndex].Tag as NamedVariable;

                if (variable != null)
                {
                    if (VariableToScene != null)
                    {
                        VariableToScene(variable);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handles the VariableToScene event of the namedVariablesView control.
        /// Adds the specified named variable to the scripting screen.
        /// </summary>
        private void namedVariablesView_VariableToScene(NamedVariable variable)
        {
            if (variable != null)
            {
                scriptingScreen.AddSceneNodeToCenter((new Variable(scriptingScreen.State, variable)).CreateView());

                scriptingScreen.Invalidate();
            }
        }
        /// <summary>
        /// Finds the named variable in the DataGridView.
        /// </summary>
        /// <param name="namedVariable">The named variable to find.</param>
        /// <returns>Item of DataGridView if found; otherwise null.</returns>
        private DataGridViewRow FindVariable(NamedVariable namedVariable)
        {
            foreach (DataGridViewRow row in settingsView.table.Rows)
            {
                if (row.Tag == namedVariable)
                {
                    return(row);
                }
            }

            return(null);
        }
        /// <summary>
        /// Determines whether the specified name is the unique name in the list of named variables.
        /// </summary>
        /// <param name="name">The name to check.</param>
        /// <param name="currentVariable">The current named variable.</param>
        /// <returns><c>true</c> if the specified name is the unique name in the list of named variables; otherwise <c>false</c>.</returns>
        private bool UniqueName(string name, NamedVariable currentVariable)
        {
            Debug.Assert(ScriptingComponent != null, "Scripting Component is not set.");

            foreach (NamedVariable variable in ScriptingComponent.Variables)
            {
                if (variable.Name == name && variable != currentVariable)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Handles the Click event of the addVariableToolStripMenuItem control.
        /// Adds the selected named variable to the scriting screen.
        /// </summary>
        private void addVariableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;

            if (item != null)
            {
                NamedVariable variable = item.Tag as NamedVariable;

                if (variable != null)
                {
                    scriptingScreen.AddSceneNodeToCenter((new Variable(scriptingScreen.State, variable)).CreateView());

                    scriptingScreen.Invalidate();
                }
            }
        }
        /// <summary>
        /// Determines whether the specified name for the specified named variable is valid.
        /// </summary>
        /// <param name="name">The name to check.</param>
        /// <param name="currentVariable">The current named variable.</param>
        /// <param name="showMessage">If set to <c>true</c> message is shown to the user by <see cref="Messages"/> system, if the name is not valid.</param>
        /// <returns><c>true</c> if the specified name for the specified named variable is valid; otherwise <c>false</c>.</returns>
        private bool IsNameValid(string name, NamedVariable currentVariable, bool showMessage = false)
        {
            if (String.IsNullOrWhiteSpace(name))
            {
                if (showMessage)
                {
                    Messages.ShowWarning("No variable name set.");
                }
                return(false);
            }
            else if (!UniqueName(name, currentVariable))
            {
                if (showMessage)
                {
                    Messages.ShowWarning("Variable with the same name already exists.");
                }
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Handles the CellValidating event of the table control.
        /// Checks if the specified named variable has valid name.
        /// </summary>
        private void table_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            // check correctness of the variable name
            if (e.ColumnIndex == 0)
            {
                NamedVariable variable = settingsView.table.Rows[e.RowIndex].Tag as NamedVariable;

                Debug.Assert(variable != null, "Cell does not contain variable data.");
                Debug.Assert(e.FormattedValue != null, "Cell value should not be null.");

                if (variable != null)
                {
                    if (!IsNameValid(e.FormattedValue.ToString(), variable, true))
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Messages.ShowInfo("Ready");
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <inheritdoc />
        public void OnDeserialization(object sender)
        {
            if (_actorIdForDeserialization != -1)
            {
                IList <NamedVariable> namedVariablesList = null;

                // named variable of the actor at the scene
                if (_namedVariableFindAtForDeserialization == 0)
                {
                    Actor actor = (Actor)Project.Singleton.Scenes.SelectedScene.FindActorById(_actorIdForDeserialization);
                    Debug.Assert(actor != null, "Actor not found.");
                    namedVariablesList = actor.Scripting.Variables;
                }
                // named variable of the prototype actor
                else if (_namedVariableFindAtForDeserialization == 1)
                {
                    Actor actor = Actor.FindById(_actorIdForDeserialization, Project.Singleton.Prototypes);
                    Debug.Assert(actor != null, "Actor not found.");
                    namedVariablesList = actor.Scripting.Variables;
                }
                // global script named variable
                else if (_namedVariableFindAtForDeserialization == 2)
                {
                    namedVariablesList = Project.Singleton.Scenes.SelectedScene.GlobalScript.Variables;
                }

                Debug.Assert(namedVariablesList != null, "Named Variables List not found.");

                _namedVariable = NamedVariable.FindByName(_namedVariableNameForDeserialization, namedVariablesList);
                Debug.Assert(_namedVariable != null, "Named Variable not found.");
            }

            if (_namedVariable != null)
            {
                _value = _namedVariable.Value;
            }
        }
        /// <summary>
        /// Shows the specified named variable in the DataGridView.
        /// </summary>
        /// <param name="variable">The named variable to show.</param>
        private void ShowVariable(NamedVariable variable)
        {
            Debug.Assert(variable != null, "Variable cannot be null.");

            StringVar nameVar = new StringVar()
            {
                Value = variable.Name
            };

            nameVar.ValueChanged += (object sender, EventArgs e) => { if (IsNameValid(nameVar.Value, variable))
                                                                      {
                                                                          variable.Name = nameVar.Value;
                                                                      }
            };

            DataGridViewRow row = new DataGridViewRow()
            {
                Tag = variable
            };

            row.Cells.Add(nameVar.GetGridCell());
            row.Cells.Add(variable.Value.GetGridCell());
            row.Cells.Add(new DataGridViewTextBoxCell()
            {
                Value = VariableTypeHelper.FriendlyName(variable.Value.VariableType)
            });
            row.Cells.Add(new DataGridViewButtonCell()
            {
                Value = "X"
            });
            row.Cells.Add(new DataGridViewButtonCell()
            {
                Value = ">"
            });

            settingsView.table.Rows.Add(row);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Variable"/> class.
 /// </summary>
 /// <param name="state">The state where the script variable will be used.</param>
 /// <param name="namedVariable">The named variable that will be used for the variable.</param>
 public Variable(State state, NamedVariable namedVariable)
     : base(state)
 {
     _namedVariable = namedVariable;
     _value         = _namedVariable.Value;
 }