Пример #1
0
        private bool LoadClipBoardFromFile(string fileName)
        {
            XmlFile.Load(ref myClipBoard, fileName);

            foreach (ModuleView na in myClipBoard.modules)
            {
                if (na.TheModule != null)
                {
                    na.TheModule.SetUpAfterLoad();
                }
                {
                    try
                    {
                        na.TheModule.SetUpAfterLoad();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("SetupAfterLoad failed on module " + na.Label + ".   Message: " + e.Message);
                    }
                }
            }
            return(true);
        }
Пример #2
0
        //copy the selection to a clipboard
        public void CopyNeurons()
        {
            //get list of neurons to copy
            List <int> neuronsToCopy = theSelection.EnumSelectedNeurons();

            theSelection.GetSelectedBoundingRectangle(out int X1o, out int Y1o, out int X2o, out int Y2o);
            MainWindow.myClipBoard = new NeuronArray();
            NeuronArray myClipBoard;

            myClipBoard = MainWindow.myClipBoard;
            myClipBoard.Initialize((X2o - X1o + 1) * (Y2o - Y1o + 1), (Y2o - Y1o + 1), true);
            boundarySynapsesOut.Clear();
            boundarySynapsesIn.Clear();

            //copy the neurons
            foreach (int nID in neuronsToCopy)
            {
                int destId = GetClipboardId(X1o, Y1o, nID);
                //copy the source neuron to the clipboard
                Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID);
                Neuron destNeuron   = sourceNeuron.Clone();
                destNeuron.Owner        = myClipBoard;
                destNeuron.Id           = destId;
                destNeuron.Label        = sourceNeuron.Label;
                destNeuron.ToolTip      = sourceNeuron.ToolTip;
                destNeuron.ShowSynapses = sourceNeuron.ShowSynapses;
                myClipBoard.SetNeuron(destId, destNeuron);
            }

            //copy the synapses (this is two-pass so we make sure all neurons exist prior to copying
            foreach (int nID in neuronsToCopy)
            {
                Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID);
                if (MainWindow.useServers)
                {
                    sourceNeuron.synapses = NeuronClient.GetSynapses(sourceNeuron.id);
                }

                int    destId     = GetClipboardId(X1o, Y1o, nID);
                Neuron destNeuron = myClipBoard.GetNeuron(destId);
                destNeuron.Owner = myClipBoard;
                if (sourceNeuron.Synapses != null)
                {
                    foreach (Synapse s in sourceNeuron.Synapses)
                    {
                        //only copy synapses with both ends in the selection
                        if (neuronsToCopy.Contains(s.TargetNeuron))
                        {
                            destNeuron.AddSynapse(GetClipboardId(X1o, Y1o, s.TargetNeuron), s.Weight, s.model);
                        }
                        else
                        {
                            string targetName = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron).label;
                            if (targetName != "")
                            {
                                boundarySynapsesOut.Add(new BoundarySynapse
                                {
                                    innerNeuronID = destNeuron.id,
                                    outerNeuronID = s.targetNeuron,
                                    weight        = s.weight,
                                    model         = s.model
                                });
                            }
                        }
                    }
                }
                if (sourceNeuron.SynapsesFrom != null)
                {
                    foreach (Synapse s in sourceNeuron.SynapsesFrom)
                    {
                        if (!neuronsToCopy.Contains(s.TargetNeuron))
                        {
                            string sourceName = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron).label;
                            if (sourceName != "")
                            {
                                boundarySynapsesIn.Add(new BoundarySynapse
                                {
                                    innerNeuronID = destNeuron.id,
                                    outerNeuronID = s.targetNeuron,
                                    weight        = s.weight,
                                    model         = s.model
                                });;
                            }
                        }
                    }
                }
            }

            //copy modules
            foreach (ModuleView mv in MainWindow.theNeuronArray.modules)
            {
                if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0)
                {
                    ModuleView newMV = new ModuleView()
                    {
                        FirstNeuron   = GetClipboardId(X1o, Y1o, mv.FirstNeuron),
                        TheModule     = mv.TheModule,
                        Color         = mv.Color,
                        Height        = mv.Height,
                        Width         = mv.Width,
                        Label         = mv.Label,
                        ModuleTypeStr = mv.ModuleTypeStr,
                    };

                    myClipBoard.modules.Add(newMV);
                }
            }
            XmlFile.Save(myClipBoard, "ClipBoard");
        }
Пример #3
0
        public void PasteNeurons()
        {
            NeuronArray myClipBoard = MainWindow.myClipBoard;

            if (!MainWindow.arrayView.dp.ShowNeurons())
            {
                MessageBox.Show("Pasting not allowed at this zoom factor");
                return;
            }
            if (targetNeuronIndex == -1)
            {
                return;
            }
            if (myClipBoard == null)
            {
                return;
            }

            if (!XmlFile.WindowsClipboardContainsNeuronArray())
            {
                return;
            }

            myClipBoard = MainWindow.myClipBoard;
            //We are pasting neurons from the clipboard.
            //The arrays have different sizes so we may by row-col.

            //first check to see if the destination is claar and warn
            List <int> targetNeurons = new List <int>();

            for (int i = 0; i < myClipBoard.arraySize; i++)
            {
                if (myClipBoard.GetNeuron(i, true) != null)
                {
                    targetNeurons.Add(GetNeuronArrayId(i));
                }
            }

            MainWindow.theNeuronArray.GetNeuronLocation(targetNeuronIndex, out int col, out int row);
            if (col + myClipBoard.Cols > MainWindow.theNeuronArray.Cols ||
                row + myClipBoard.rows > MainWindow.theNeuronArray.rows)
            {
                MessageBoxResult result = MessageBox.Show("Paste would exceed neuron array boundary!", "Error", MessageBoxButton.OK);
                return;
            }

            if (!IsDestinationClear(targetNeurons, 0, true))
            {
                MessageBoxResult result = MessageBox.Show("Some desination neurons are in use and will be overwritten, continue?", "Continue", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }

            MainWindow.SuspendEngine();
            MainWindow.theNeuronArray.SetUndoPoint();
            //now paste the neurons
            for (int i = 0; i < myClipBoard.arraySize; i++)
            {
                if (myClipBoard.GetNeuron(i) != null)
                {
                    int destID = GetNeuronArrayId(i);
                    MainWindow.theNeuronArray.GetNeuron(destID).AddUndoInfo();
                    Neuron n = myClipBoard.GetCompleteNeuron(i, true);
                    n.Owner    = myClipBoard;
                    n.synapses = myClipBoard.GetSynapsesList(i);

                    Neuron sourceNeuron = n.Clone();
                    sourceNeuron.id = destID;
                    while (sourceNeuron.label != "" && MainWindow.theNeuronArray.GetNeuron(sourceNeuron.label) != null)
                    {
                        int num        = 0;
                        int digitCount = 0;
                        while (sourceNeuron.label != "" && Char.IsDigit(sourceNeuron.label[sourceNeuron.label.Length - 1]))
                        {
                            int.TryParse(sourceNeuron.label[sourceNeuron.label.Length - 1].ToString(), out int digit);
                            num = num + (int)Math.Pow(10, digitCount) * digit;
                            digitCount++;
                            sourceNeuron.label = sourceNeuron.label.Substring(0, sourceNeuron.label.Length - 1);
                        }
                        num++;
                        sourceNeuron.label = sourceNeuron.label + num.ToString();
                    }
                    sourceNeuron.Owner        = MainWindow.theNeuronArray;
                    sourceNeuron.Label        = sourceNeuron.label;
                    sourceNeuron.ToolTip      = n.ToolTip;
                    sourceNeuron.ShowSynapses = n.ShowSynapses;
                    MainWindow.theNeuronArray.SetNeuron(destID, sourceNeuron);


                    foreach (Synapse s in n.Synapses)
                    {
                        MainWindow.theNeuronArray.GetNeuron(destID).
                        AddSynapseWithUndo(GetNeuronArrayId(s.TargetNeuron), s.Weight, s.model);
                    }
                }
            }

            //handle boundary synapses
            foreach (BoundarySynapse b in boundarySynapsesOut)
            {
                int    sourceID     = GetNeuronArrayId(b.innerNeuronID);
                Neuron targetNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID);
                if (targetNeuron != null)
                {
                    MainWindow.theNeuronArray.GetNeuron(sourceID).AddSynapseWithUndo(targetNeuron.id, b.weight, b.model);
                }
            }
            foreach (BoundarySynapse b in boundarySynapsesIn)
            {
                int    targetID     = GetNeuronArrayId(b.innerNeuronID);
                Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID);
                if (sourceNeuron != null)
                {
                    sourceNeuron.AddSynapseWithUndo(targetID, b.weight, b.model);
                }
            }

            //paste modules
            foreach (ModuleView mv in myClipBoard.modules)
            {
                ModuleView newMV = new ModuleView()
                {
                    FirstNeuron   = GetNeuronArrayId(mv.FirstNeuron),
                    TheModule     = mv.TheModule,
                    Color         = mv.Color,
                    Height        = mv.Height,
                    Width         = mv.Width,
                    Label         = mv.Label,
                    ModuleTypeStr = mv.ModuleTypeStr,
                };

                MainWindow.theNeuronArray.modules.Add(newMV);
            }
            MainWindow.ResumeEngine();
            Update();
        }
Пример #4
0
        //this enables and disables various menu entries based on circumstances
        private void MainMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            LoadMRUMenu();
            LoadFindMenus();
            if (theNeuronArray != null && !useServers)
            {
                ThreadCount.Text = theNeuronArray.GetThreadCount().ToString();
            }
            else
            {
                ThreadCount.Text = "";
            }
            if (theNeuronArray != null)
            {
                Refractory.Text = theNeuronArray.GetRefractoryDelay().ToString();
            }
            else
            {
                Refractory.Text = "";
            }

            if (currentFileName != "" &&
                XmlFile.CanWriteTo(currentFileName, out string message) &&
                theNeuronArray != null)
            {
                EnableMenuItem(MainMenu.Items, "_Save", true);
                SaveButton.IsEnabled = true;
            }
            else
            {
                EnableMenuItem(MainMenu.Items, "_Save", false);
                SaveButton.IsEnabled = false;
            }
            if (!engineIsPaused)
            {
                EnableMenuItem(MainMenu.Items, "Run", false);
                EnableMenuItem(MainMenu.Items, "Pause", true);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, "Run", true);
                EnableMenuItem(MainMenu.Items, "Pause", false);
            }
            if (useServers)
            {
                var tb0 = Utils.FindByName(MainMenu, "ThreadCount");
                if (tb0.Parent is UIElement ui)
                {
                    ui.Visibility = Visibility.Collapsed;
                }
                tb0 = Utils.FindByName(MainMenu, "Refractory");
                if (tb0.Parent is UIElement ui1)
                {
                    ui1.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                var tb0 = Utils.FindByName(MainMenu, "ThreadCount");
                if (tb0.Parent is UIElement ui)
                {
                    ui.Visibility = Visibility.Visible;
                }
                tb0 = Utils.FindByName(MainMenu, "Refractory");
                if (tb0.Parent is UIElement ui1)
                {
                    ui1.Visibility = Visibility.Visible;
                }
            }
            if (IsArrayEmpty())
            {
                EnableMenuItem(MainMenu.Items, "_Save", false);
                EnableMenuItem(MainMenu.Items, "Save _As", false);
                EnableMenuItem(MainMenu.Items, "_Insert", false);
                EnableMenuItem(MainMenu.Items, "_Properties", false);
                EnableMenuItem(MainMenu.Items, "_Notes", false);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, "Save _As", true);
                EnableMenuItem(MainMenu.Items, "_Insert", true);
                EnableMenuItem(MainMenu.Items, "_Properties", true);
                EnableMenuItem(MainMenu.Items, "_Notes", true);
                MenuItem mi = (MenuItem)Utils.FindByName(MainMenu, "ShowSynapses");
                if (mi != null)
                {
                    mi.IsChecked = theNeuronArray.ShowSynapses;
                }
            }
            if (theNeuronArrayView.theSelection.selectedRectangles.Count == 0)
            {
                EnableMenuItem(MainMenu.Items, " Cut", false);
                EnableMenuItem(MainMenu.Items, " Copy", false);
                EnableMenuItem(MainMenu.Items, " Delete", false);
                EnableMenuItem(MainMenu.Items, " Move", false);
                EnableMenuItem(MainMenu.Items, " Clear Selection", false);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, " Cut", true);
                EnableMenuItem(MainMenu.Items, " Copy", true);
                EnableMenuItem(MainMenu.Items, " Delete", true);
                if (arrayView.targetNeuronIndex < 0)
                {
                    EnableMenuItem(MainMenu.Items, " Move", false);
                }
                else
                {
                    EnableMenuItem(MainMenu.Items, " Move", true);
                }
                EnableMenuItem(MainMenu.Items, " Clear Selection", true);
            }
            if (arrayView.targetNeuronIndex < 0 || myClipBoard == null)
            {
                EnableMenuItem(MainMenu.Items, " Paste", false);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, " Paste", true);
            }
            if (theNeuronArray != null && theNeuronArray.UndoPossible())
            {
                EnableMenuItem(MainMenu.Items, " Undo", true);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, " Undo", false);
            }

            if (myClipBoard == null)
            {
                EnableMenuItem(MainMenu.Items, "Save Clipboard", false);
            }
            else
            {
                EnableMenuItem(MainMenu.Items, "Save Clipboard", true);
            }
        }
Пример #5
0
        private async void LoadFile(string fileName)
        {
            CloseAllModuleDialogs();
            CloseHistoryWindow();
            CloseNotesWindow();
            theNeuronArrayView.theSelection.selectedRectangles.Clear();
            CloseAllModuleDialogs();
            SuspendEngine();

            bool success = false;
            await Task.Run(delegate { success = XmlFile.Load(ref theNeuronArray, fileName); });

            if (!success)
            {
                CreateEmptyNetwork();
                Properties.Settings.Default["CurrentFile"] = currentFileName;
                Properties.Settings.Default.Save();
                ResumeEngine();
                return;
            }
            currentFileName = fileName;

            if (XmlFile.CanWriteTo(currentFileName))
            {
                SaveButton.IsEnabled = true;
            }
            else
            {
                SaveButton.IsEnabled = false;
            }

            SetTitleBar();
            await Task.Delay(1000).ContinueWith(t => ShowDialogs());

            foreach (ModuleView na in theNeuronArray.modules)
            {
                if (na.TheModule != null)
                {
                    na.TheModule.SetUpAfterLoad();
                }
            }
            theNeuronArray.LoadComplete = true;

            if (theNeuronArray.displayParams != null)
            {
                theNeuronArrayView.Dp = theNeuronArray.displayParams;
            }

            AddFileToMRUList(currentFileName);
            Properties.Settings.Default["CurrentFile"] = currentFileName;
            Properties.Settings.Default.Save();

            Update();
            SetShowSynapsesCheckBox(theNeuronArray.ShowSynapses);
            SetPlayPauseButtonImage(theNeuronArray.EngineIsPaused);
            SetSliderPosition(theNeuronArray.EngineSpeed);

            engineIsPaused = theNeuronArray.EngineIsPaused;

            engineSpeedStack.Clear();
            engineSpeedStack.Push(theNeuronArray.EngineSpeed);

            if (!engineIsPaused)
            {
                ResumeEngine();
            }
        }
Пример #6
0
        public static ContextMenu CreateContextMenu(int i, Neuron n, ContextMenu cm)
        {
            cmCancelled = false;

            labelChanged     = false;
            toolTipChanged   = false;
            modelChanged     = false;
            enabledChanged   = false;
            historyChanged   = false;
            synapsesChanged  = false;
            chargeChanged    = false;
            leakRateChanged  = false;
            axonDelayChanged = false;

            n = MainWindow.theNeuronArray.AddSynapses(n);
            cm.SetValue(NeuronIDProperty, n.Id);
            cm.Closed         += Cm_Closed;
            cm.Opened         += Cm_Opened;
            cm.PreviewKeyDown += Cm_PreviewKeyDown;
            cmCancelled        = false;
            cm.StaysOpen       = true;
            cm.Width           = 300;

            //The neuron label
            MenuItem mi1 = new MenuItem {
                Header = "ID: " + n.id, Padding = new Thickness(0)
            };

            cm.Items.Add(mi1);

            //apply to all in selection
            CheckBox cbApplyToSelection = new CheckBox
            {
                IsChecked = true,
                Content   = "Apply changes to all neurons in selection",
                Name      = "ApplyToSelection",
            };

            cbApplyToSelection.Checked   += CbCheckedChanged;
            cbApplyToSelection.Unchecked += CbCheckedChanged;
            cm.Items.Add(new MenuItem {
                StaysOpenOnClick = true, Header = cbApplyToSelection
            });
            if (MainWindow.arrayView.theSelection.selectedRectangles.Count > 0)
            {
                cbApplyToSelection.IsEnabled = true;
                cbApplyToSelection.IsChecked = NeuronInSelection(n.id);
            }
            else
            {
                cbApplyToSelection.IsChecked = false;
                cbApplyToSelection.IsEnabled = false;
            }

            //label
            StackPanel sp = new StackPanel {
                Orientation = Orientation.Horizontal,
            };

            sp.Children.Add(new Label {
                Content = "Label: ", Padding = new Thickness(0), VerticalAlignment = VerticalAlignment.Center,
            });;
            TextBox tb = Utils.ContextMenuTextBox(n.Label, "Label", 170);

            tb.TextChanged += Tb_TextChanged;
            sp.Children.Add(tb);
            sp.Children.Add(new Label {
                Content = "Warning: Duplicate Label", FontSize = 8, Name = "DupWarn", Visibility = Visibility.Hidden
            });
            mi1 = new MenuItem {
                StaysOpenOnClick = true, Header = sp
            };
            cm.Items.Add(mi1);

            //tooltip
            if (n.Label != "" || n.ToolTip != "") //add the tooltip textbox if needed
            {
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };
                sp.Children.Add(new Label {
                    Content = "Tooltip: ", VerticalAlignment = VerticalAlignment.Center, Padding = new Thickness(0)
                });
                tb              = Utils.ContextMenuTextBox(n.ToolTip, "ToolTip", 150);
                tb.TextChanged += Tb_TextChanged;
                sp.Children.Add(tb);
                cm.Items.Add(sp);
            }

            //The neuron model
            sp = new StackPanel {
                Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3)
            };
            sp.Children.Add(new Label {
                Content = "Model: ", Padding = new Thickness(0)
            });
            ComboBox cb = new ComboBox()
            {
                Width = 80, Name = "Model"
            };

            for (int index = 0; index < Enum.GetValues(typeof(Neuron.modelType)).Length; index++)
            {
                Neuron.modelType model = (Neuron.modelType)index;
                cb.Items.Add(new ListBoxItem()
                {
                    Content = model.ToString(),
                    ToolTip = Neuron.modelToolTip[index],
                    Width   = 100,
                });
            }
            cb.SelectedIndex     = (int)n.Model;
            cb.SelectionChanged += Cb_SelectionChanged;
            sp.Children.Add(cb);
            cm.Items.Add(new MenuItem {
                StaysOpenOnClick = true, Header = sp
            });

            cm.Items.Add(new Separator {
                Visibility = Visibility.Collapsed
            });
            cm.Items.Add(new Separator {
                Visibility = Visibility.Collapsed
            });


            MenuItem mi             = new MenuItem();
            CheckBox cbEnableNeuron = new CheckBox
            {
                IsChecked = (n.leakRate > 0) || float.IsPositiveInfinity(1.0f / n.leakRate),
                Content   = "Enabled",
                Name      = "Enabled",
            };

            cbEnableNeuron.Checked   += CbCheckedChanged;
            cbEnableNeuron.Unchecked += CbCheckedChanged;
            cm.Items.Add(new MenuItem {
                StaysOpenOnClick = true, Header = cbEnableNeuron
            });

            CheckBox cbShowSynapses = new CheckBox
            {
                IsChecked = MainWindow.arrayView.IsShowingSynapses(n.id),
                Content   = "Show Synapses",
                Name      = "Synapses",
            };

            cbShowSynapses.Checked   += CbCheckedChanged;
            cbShowSynapses.Unchecked += CbCheckedChanged;
            cm.Items.Add(new MenuItem {
                StaysOpenOnClick = true, Header = cbShowSynapses
            });

            mi = new MenuItem();
            CheckBox cbHistory = new CheckBox
            {
                IsChecked = FiringHistory.NeuronIsInFiringHistory(n.id),
                Content   = "Record Firing History",
                Name      = "History",
            };

            cbHistory.Checked   += CbCheckedChanged;
            cbHistory.Unchecked += CbCheckedChanged;
            cm.Items.Add(new MenuItem {
                StaysOpenOnClick = true, Header = cbHistory
            });

            mi = new MenuItem {
                Header = "Clear Synapses"
            };
            mi.Click += Mi_Click;
            cm.Items.Add(mi);

            cm.Items.Add(new Separator());
            cm.Items.Add(new Separator());

            mi        = new MenuItem();
            mi.Header = "Synapses Out";
            mi.Width  = 250;
            mi.HorizontalAlignment = HorizontalAlignment.Left;
            foreach (Synapse s in n.Synapses)
            {
                AddSynapseEntryToMenu(mi, s);
            }
            cm.Items.Add(mi);

            mi        = new MenuItem();
            mi.Header = "Synapses In";
            mi.Width  = 250;
            mi.HorizontalAlignment = HorizontalAlignment.Left;
            foreach (Synapse s in n.SynapsesFrom)
            {
                AddSynapseEntryToMenu(mi, s);
            }
            cm.Items.Add(mi);

            mi = new MenuItem {
                Header = "Paste Here"
            };
            if (!XmlFile.WindowsClipboardContainsNeuronArray())
            {
                mi.IsEnabled = false;
            }
            mi.Click += Mi_Click;
            cm.Items.Add(mi);

            mi = new MenuItem {
                Header = "Move Here"
            };
            if (MainWindow.arrayView.theSelection.selectedRectangles.Count == 0)
            {
                mi.IsEnabled = false;
            }
            mi.Click += Mi_Click;
            cm.Items.Add(mi);


            mi        = new MenuItem();
            mi.Header = "Connect Multiple Synapses";
            mi.Width  = 250;
            mi.HorizontalAlignment = HorizontalAlignment.Left;
            mi.Click += Mi_Click;
            mi.Items.Add(new MenuItem()
            {
                Header = "From Selection to Here"
            });
            ((MenuItem)mi.Items[mi.Items.Count - 1]).Click += Mi_Click;
            mi.Items.Add(new MenuItem()
            {
                Header = "From Here to Selection"
            });
            ((MenuItem)mi.Items[mi.Items.Count - 1]).Click += Mi_Click;
            mi.Items.Add(new MenuItem()
            {
                Header = "Mutual Suppression"
            });
            ((MenuItem)mi.Items[mi.Items.Count - 1]).Click += Mi_Click;
            cm.Items.Add(mi);

            sp = new StackPanel {
                Orientation = Orientation.Horizontal
            };
            Button b0 = new Button {
                Content = "OK", Width = 100, Height = 25, Margin = new Thickness(10)
            };

            b0.Click += B0_Click;
            sp.Children.Add(b0);
            b0 = new Button {
                Content = "Cancel", Width = 100, Height = 25, Margin = new Thickness(10)
            };
            b0.Click += B0_Click;
            sp.Children.Add(b0);

            cm.Items.Add(new MenuItem {
                Header = sp, StaysOpenOnClick = true
            });

            SetCustomCMItems(cm, n, n.model);

            return(cm);
        }