コード例 #1
0
        public static void CreateModule(string label, string commandLine, Color color, int firstNeuron, int width, int height)
        {
            ModuleView mv = new ModuleView(firstNeuron, width, height, label, commandLine, Utils.ColorToInt(color));

            if (mv.Width < mv.TheModule.MinWidth)
            {
                mv.Width = mv.TheModule.MinWidth;
            }
            if (mv.Height < mv.TheModule.MinHeight)
            {
                mv.Height = mv.TheModule.MinHeight;
            }
            MainWindow.theNeuronArray.modules.Add(mv);
            string[] Params = commandLine.Split(' ');
            if (mv.TheModule != null)
            {
                MainWindow.SuspendEngine();
                mv.TheModule.SetModuleView();
                mv.TheModule.Initialize();
                MainWindow.ResumeEngine();
            }
            else
            {
                //This should never be reached
                Type t1x = Type.GetType("BrainSimulator.Modules." + Params[0]);
                if (t1x != null && (mv.TheModule == null || mv.TheModule.GetType() != t1x))
                {
                    mv.TheModule = (ModuleBase)Activator.CreateInstance(t1x);
                    //  MainWindow.theNeuronArray.areas[i].TheModule.Initialize();
                }
            }
        }
コード例 #2
0
ファイル: ModuleViewMenu.cs プロジェクト: oakicode/BrainSimII
        public static void CreateModule(string label, string commandLine, Color color, int firstNeuron, int width, int height)
        {
            ModuleView na = new ModuleView(firstNeuron, width, height, label, commandLine, Utils.ColorToInt(color));

            if (na.Width < na.theModule.MinWidth)
            {
                na.Width = na.theModule.MinWidth;
            }
            if (na.Height < na.theModule.MinHeight)
            {
                na.Height = na.theModule.MinHeight;
            }
            MainWindow.theNeuronArray.modules.Add(na);
            string[] Params = commandLine.Split(' ');
            if (na.TheModule != null)
            {
                //MainWindow.theNeuronArray.areas[i].TheModule.Initialize(); //doesn't work with camera module
            }
            else
            {
                Type t1x = Type.GetType("BrainSimulator.Modules." + Params[0]);
                if (t1x != null && (na.TheModule == null || na.TheModule.GetType() != t1x))
                {
                    na.TheModule = (ModuleBase)Activator.CreateInstance(t1x);
                    //  MainWindow.theNeuronArray.areas[i].TheModule.Initialize();
                }
            }
        }
コード例 #3
0
        private void LoadFindMenus()
        {
            if (theNeuronArray == null)
            {
                return;
            }
            NeuronMenu.Items.Clear();

            List <string> neuronLabelList = theNeuronArray.GetValuesFromLabelCache();
            List <int>    neuronIdList    = theNeuronArray.GetKeysFromLabelCache();

            for (int i = 0; i < neuronLabelList.Count; i++)
            {
                string shortLabel = neuronLabelList[i];
                if (shortLabel.Length > 20)
                {
                    shortLabel = shortLabel.Substring(0, 20);
                }
                shortLabel        += " (" + neuronIdList[i] + ")";
                neuronLabelList[i] = shortLabel;
            }
            neuronLabelList.Sort();
            NeuronMenu.IsEnabled = (neuronLabelList.Count == 0) ? false : true;
            foreach (string s in neuronLabelList)
            {
                MenuItem mi = new MenuItem {
                    Header = s
                };
                mi.Click += NeuronMenu_Click;
                NeuronMenu.Items.Add(mi);
            }


            ModuleMenu.Items.Clear();
            List <string> moduleLabelList = new List <string>();

            for (int i = 0; i < theNeuronArray.Modules.Count; i++)
            {
                ModuleView theModule  = theNeuronArray.Modules[i];
                string     shortLabel = theModule.Label;
                if (shortLabel.Length > 10)
                {
                    shortLabel = shortLabel.Substring(0, 10);
                }
                shortLabel += " (" + theModule.FirstNeuron + ")";
                moduleLabelList.Add(shortLabel);
            }
            moduleLabelList.Sort();
            ModuleMenu.IsEnabled = (moduleLabelList.Count == 0) ? false : true;
            foreach (string s in moduleLabelList)
            {
                MenuItem mi = new MenuItem {
                    Header = s
                };
                mi.Click += NeuronMenu_Click;
                ModuleMenu.Items.Add(mi);
            }
        }
コード例 #4
0
        //MODULE
        private static void OpenModuleContextMenu(FrameworkElement theShape)
        {
            int        i  = (int)theShape.GetValue(ModuleView.AreaNumberProperty);
            ModuleView nr = MainWindow.theNeuronArray.Modules[i];

            theShape.ContextMenu = new ContextMenu();
            ModuleView.CreateContextMenu(i, nr, theShape, theShape.ContextMenu);
            theShape.ContextMenu.IsOpen = true;
        }
コード例 #5
0
        public static void DeleteModule(int i)
        {
            ModuleView mv = MainWindow.theNeuronArray.Modules[i];

            mv.theModule.CloseDlg();
            foreach (Neuron n in mv.Neurons())
            {
                n.Reset();
                n.DeleteAllSynapes();
            }
            MainWindow.theNeuronArray.Modules.RemoveAt(i);
        }
コード例 #6
0
 private void DeleteModulesInSelection()
 {
     for (int i = 0; i < MainWindow.theNeuronArray.modules.Count; i++)
     {
         ModuleView mv = MainWindow.theNeuronArray.modules[i];
         if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0)
         {
             MainWindow.theNeuronArray.modules.RemoveAt(i);
             i--;
         }
     }
 }
コード例 #7
0
 private void DeleteModulesInSelection()
 {
     lock (MainWindow.theNeuronArray.modules)
     {
         for (int i = 0; i < MainWindow.theNeuronArray.modules.Count; i++)
         {
             ModuleView mv = MainWindow.theNeuronArray.modules[i];
             if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0)
             {
                 MainWindow.theNeuronArray.AddModuleUndo(i, mv);
                 mv.TheModule.CloseDlg();
                 MainWindow.theNeuronArray.modules.RemoveAt(i);
                 i--;
             }
         }
     }
 }
コード例 #8
0
 private void UndoModule()
 {
     lock (MainWindow.theNeuronArray.modules)
     {
         if (moduleUndoInfo.Count == 0)
         {
             return;
         }
         ModuleUndo m1 = moduleUndoInfo.Last();
         if (m1.moduleState == null)  //the module was just added
         {
             ModuleView.DeleteModule(m1.index);
         }
         else
         {
             if (m1.index == -1) //the module was just deleted
             {
                 ModuleView mv = new ModuleView
                 {
                     Width         = m1.moduleState.Width,
                     Height        = m1.moduleState.Height,
                     FirstNeuron   = m1.moduleState.FirstNeuron,
                     Color         = m1.moduleState.Color,
                     ModuleTypeStr = m1.moduleState.ModuleTypeStr,
                     Label         = m1.moduleState.Label,
                 };
                 // modules.Add(mv);
                 ModuleView.CreateModule(mv.Label, mv.ModuleTypeStr, Utils.IntToColor(mv.Color), mv.FirstNeuron, mv.Width, mv.Height);
             }
             else
             {
                 modules[m1.index].FirstNeuron   = m1.moduleState.FirstNeuron;
                 modules[m1.index].Width         = m1.moduleState.Width;
                 modules[m1.index].Height        = m1.moduleState.Height;
                 modules[m1.index].Color         = m1.moduleState.Color;
                 modules[m1.index].ModuleTypeStr = m1.moduleState.ModuleTypeStr;
                 modules[m1.index].Label         = m1.moduleState.Label;
             }
         }
         moduleUndoInfo.RemoveAt(moduleUndoInfo.Count - 1);
     }
 }
コード例 #9
0
        public void LoadImage(ModuleView na)
        {
            if (countDown == 0)
            {
                if (fileCounter == 0)
                {
                    dirs = new List <string>(Directory.EnumerateFiles("E:\\Charlie\\Documents\\Brainsim\\Images"));
                }
                countDown = 3;
                bitmap1   = new Bitmap(dirs[fileCounter]);
                fileCounter++;
                if (fileCounter >= dirs.Count)
                {
                    fileCounter = 0;
                }
            }

            na.GetBounds(out int X1, out int Y1, out int X2, out int Y2);
            countDown--;

            for (int i = X1 + 1; i < X2 - 1; i++)
            {
                for (int j = Y1 + 1; j < Y2 - 1; j++)
                {
                    int    neuronIndex     = GetNeuronIndex(i, j);
                    Neuron n               = MainWindow.theNeuronArray.neuronArray[neuronIndex];
                    int    x               = (i - X1) * bitmap1.Width / (X2 - X1);
                    int    y               = (j - Y1) * bitmap1.Height / (Y2 - Y1);
                    System.Drawing.Color c = bitmap1.GetPixel(x, y);
                    if (c.R != 255 || c.G != 255 || c.B != 255)
                    {
                        n.CurrentCharge = n.LastCharge = 1 - (float)c.R / 255.0f;
                    }
                    else
                    {
                        n.CurrentCharge = n.LastCharge = 0;
                    }
                }
            }
        }
コード例 #10
0
        public void AddModuleUndo(int index, ModuleView currentModule)
        {
            ModuleUndo m1 = new ModuleUndo();

            m1.index = index;
            if (currentModule == null)
            {
                m1.moduleState = null;
            }
            else
            {
                m1.moduleState = new ModuleView()
                {
                    Width         = currentModule.Width,
                    Height        = currentModule.Height,
                    FirstNeuron   = currentModule.FirstNeuron,
                    Color         = currentModule.Color,
                    ModuleTypeStr = currentModule.TheModule.GetType().Name,
                    Label         = currentModule.Label
                }
            };
            moduleUndoInfo.Add(m1);
        }
コード例 #11
0
        public static void CreateModule(string label, string commandLine, Color color, int firstNeuron, int width, int height)
        {
            ModuleView mv = new ModuleView(firstNeuron, width, height, label, commandLine, Utils.ColorToInt(color));

            if (mv.Width < mv.TheModule.MinWidth)
            {
                mv.Width = mv.TheModule.MinWidth;
            }
            if (mv.Height < mv.TheModule.MinHeight)
            {
                mv.Height = mv.TheModule.MinHeight;
            }
            lock (MainWindow.theNeuronArray.modules)
            {
                MainWindow.theNeuronArray.modules.Add(mv);
            }
            MainWindow.SuspendEngine();
            mv.TheModule.SetModuleView();
            mv.TheModule.Initialize();
            MainWindow.ResumeEngine();
            MainWindow.Update();
            return;
        }
コード例 #12
0
        private void ResizeModule(FrameworkElement theShape, int currentNeuron)
        {
            lock (MainWindow.theNeuronArray.modules)
            {            //TODO: Add rearrangement of neurons
                         //TODO: Add clone of neurons to handle ALL properties
                int        index            = (int)theShape.GetValue(ModuleView.AreaNumberProperty);
                ModuleView theCurrentModule = MainWindow.theNeuronArray.modules[index];

                theCurrentModule.GetBounds(out int X1, out int Y1, out int X2, out int Y2);
                theCurrentModule.GetAbsNeuronLocation(prevModuleMouseLocation, out int Xf, out int Yf);
                theCurrentModule.GetAbsNeuronLocation(currentNeuron, out int Xc, out int Yc);
                int minHeight = theCurrentModule.TheModule.MinHeight;
                int minWidth  = theCurrentModule.TheModule.MinWidth;
                int maxHeight = theCurrentModule.TheModule.MaxHeight;
                int maxWidth  = theCurrentModule.TheModule.MaxWidth;

                //move the top?
                if (theCanvas.Cursor == Cursors.ScrollN || theCanvas.Cursor == Cursors.ScrollNE || theCanvas.Cursor == Cursors.ScrollNW)
                {
                    if (Yc != Yf)
                    {
                        int newTop = Y1 + Yc - Yf;
                        if (newTop <= Y2)
                        {
                            MainWindow.theNeuronArray.AddModuleUndo(index, theCurrentModule);
                            int newHeight = theCurrentModule.Height - (Yc - Yf);

                            if (newHeight < minHeight)
                            {
                                theCurrentModule.Height = minHeight;
                            }
                            else if (newHeight > maxHeight)
                            {
                                theCurrentModule.Height = maxHeight;
                            }
                            else
                            {
                                theCurrentModule.Height = newHeight;
                                MoveModule(theShape, currentNeuron);
                                //theCurrentModule.FirstNeuron += Yc - Yf;
                                prevModuleMouseLocation = currentNeuron;
                            }
                            SortAreas();
                            Update();
                        }
                    }
                }
                //move the left?
                if (theCanvas.Cursor == Cursors.ScrollW || theCanvas.Cursor == Cursors.ScrollNW || theCanvas.Cursor == Cursors.ScrollSW)
                {
                    if (Xc != Xf)
                    {
                        int newLeft = X1 + Xc - Xf;
                        if (newLeft <= X2)
                        {
                            MainWindow.theNeuronArray.AddModuleUndo(index, theCurrentModule);
                            int newWidth = theCurrentModule.Width - (Xc - Xf);

                            if (newWidth < minWidth)
                            {
                                theCurrentModule.Width = minWidth;
                            }
                            else if (newWidth > maxWidth)
                            {
                                theCurrentModule.Width = maxWidth;
                            }
                            else
                            {
                                theCurrentModule.Width = newWidth;
                                MoveModule(theShape, currentNeuron);
                                prevModuleMouseLocation = currentNeuron;
                            }
                            SortAreas();
                            Update();
                        }
                    }
                }
                //Move the Right
                if (theCanvas.Cursor == Cursors.ScrollE || theCanvas.Cursor == Cursors.ScrollNE || theCanvas.Cursor == Cursors.ScrollSE)
                {
                    if (Xc != Xf)
                    {
                        int newRight = X2 + Xc - Xf;
                        if (newRight >= X1)
                        {
                            MainWindow.theNeuronArray.AddModuleUndo(index, theCurrentModule);
                            int newWidth = theCurrentModule.Width + Xc - Xf;

                            if (newWidth < minWidth)
                            {
                                theCurrentModule.Width = minWidth;
                            }
                            else if (newWidth > maxWidth)
                            {
                                theCurrentModule.Width = maxWidth;
                            }
                            else
                            {
                                prevModuleMouseLocation = currentNeuron;
                                theCurrentModule.Width  = newWidth;
                            }
                            Update();
                        }
                    }
                }
                //Move the Bottom
                if (theCanvas.Cursor == Cursors.ScrollS || theCanvas.Cursor == Cursors.ScrollSE || theCanvas.Cursor == Cursors.ScrollSW)
                {
                    if (Yc != Yf)
                    {
                        int newBottom = Y2 + Yc - Yf;
                        if (newBottom >= Y1)
                        {
                            int newHeight = theCurrentModule.Height + Yc - Yf;

                            if (newHeight < minHeight)
                            {
                                theCurrentModule.Height = minHeight;
                            }
                            else if (newHeight > maxHeight)
                            {
                                theCurrentModule.Height = maxHeight;
                            }
                            else
                            {
                                prevModuleMouseLocation = currentNeuron;
                                theCurrentModule.Height = newHeight;
                            }
                            Update();
                        }
                    }
                }
            }
        }
コード例 #13
0
ファイル: ModuleViewMenu.cs プロジェクト: oakicode/BrainSimII
        public static void CreateContextMenu(int i, ModuleView nr, Rectangle r) //for a selection
        {
            ContextMenu cm = new ContextMenu();
            StackPanel  sp;

            cm.SetValue(AreaNumberProperty, i);
            MenuItem mi = new MenuItem();

            mi.Header = "Delete";
            mi.Click += Mi_Click;
            cm.Items.Add(mi);
            if (i < 0)
            {
                mi        = new MenuItem();
                mi.Header = "Reset Hebbian Weights";
                mi.Click += Mi_Click;
                cm.Items.Add(mi);
            }
            if (i >= 0)
            {
                mi        = new MenuItem();
                mi.Header = "Initialize";
                mi.Click += Mi_Click;
                cm.Items.Add(mi);
                mi = new MenuItem();
                if (nr.TheModule.ShortDescription != null || nr.TheModule.LongDescription != null)
                {
                    mi.Header = "Info...";
                    mi.Click += Mi_Click;
                    cm.Items.Add(mi);
                }
                sp = new StackPanel {
                    Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3)
                };
                sp.Children.Add(new Label {
                    Content = "Width: ", VerticalAlignment = VerticalAlignment.Center, Padding = new Thickness(0)
                });
                sp.Children.Add(new TextBox {
                    Text = nr.Width.ToString(), Width = 60, Name = "AreaWidth", VerticalAlignment = VerticalAlignment.Center
                });
                sp.Children.Add(new Label {
                    Content = "Height: "
                });
                sp.Children.Add(new TextBox {
                    Text = nr.Height.ToString(), Width = 60, Name = "AreaHeight", VerticalAlignment = VerticalAlignment.Center
                });
                cm.Items.Add(sp);

                sp = new StackPanel {
                    Orientation = Orientation.Horizontal
                };
                sp.Children.Add(new Label {
                    Content = "Name: ", Padding = new Thickness(0)
                });
                sp.Children.Add(new TextBox {
                    Text = nr.Label, Width = 140, Name = "AreaName", Padding = new Thickness(0)
                });
                cm.Items.Add(sp);
            }

            sp = new StackPanel {
                Orientation = Orientation.Horizontal
            };
            sp.Children.Add(new Label {
                Content = "Type: ", VerticalAlignment = VerticalAlignment.Center, Padding = new Thickness(0)
            });
            cm.Items.Add(sp);

            ComboBox cb = new ComboBox();
            //get list of available NEW modules...these are assignable to a "ModuleBase"
            var listOfBs = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                            from assemblyType in domainAssembly.GetTypes()
                            where typeof(ModuleBase).IsAssignableFrom(assemblyType)
                            orderby assemblyType.Name
                            select assemblyType
                            ).ToArray();

            foreach (var v in listOfBs)
            {
                if (v.Name != "ModuleBase")
                {
                    Type t = Type.GetType(v.FullName);
                    if (v.Name != "ModuleBase")
                    {
                        ComboBoxItem cbi = new ComboBoxItem
                        {
                            Content = v.Name,
                        };
                        cb.Items.Add(v.Name);
                    }
                }
            }
            if (nr.TheModule != null)
            {
                string cm1 = nr.TheModule.GetType().Name.ToString();
                if (cm1 != "")
                {
                    cb.SelectedValue = cm1;
                }
            }
            cb.Width             = 180;
            cb.Name              = "AreaType";
            cb.SelectionChanged += Cb_SelectionChanged;
            sp.Children.Add(cb);


            TextBox tb2 = new TextBox();

            tb2.Text = "";
            tb2.Name = "CommandParams";
            if (nr.CommandLine.IndexOf(" ") > 0)
            {
                tb2.Text = nr.CommandLine.Substring(nr.CommandLine.IndexOf(" ") + 1);
            }
            tb2.Width = 200;
            cm.Items.Add(tb2);

            if (i >= 0)
            {            //color picker
                Color c = Utils.IntToColor(nr.Color);
                cb       = new ComboBox();
                cb.Width = 200;
                cb.Name  = "AreaColor";
                PropertyInfo[] x1  = typeof(Colors).GetProperties();
                int            sel = -1;
                for (int i1 = 0; i1 < x1.Length; i1++)
                {
                    Color cc = (Color)ColorConverter.ConvertFromString(x1[i1].Name);
                    if (cc == c)

                    {
                        sel = i1;
                        break;
                    }
                }
                if (nr.Color == 0)
                {
                    sel = 3;
                }
                foreach (PropertyInfo s in x1)
                {
                    ComboBoxItem cbi = new ComboBoxItem()
                    {
                        Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(s.Name)),
                        Content    = s.Name
                    };
                    Rectangle r1 = new Rectangle()
                    {
                        Width  = 20,
                        Height = 20,
                        Fill   = new SolidColorBrush((Color)ColorConverter.ConvertFromString(s.Name)),
                        Margin = new Thickness(0, 0, 140, 0),
                    };
                    Grid g = new Grid();
                    g.Children.Add(r1);
                    g.Children.Add(new Label()
                    {
                        Content = s.Name, Margin = new Thickness(25, 0, 0, 0)
                    });
                    cbi.Content = g;
                    cbi.Width   = 200;
                    cb.Items.Add(cbi);
                }
                cb.SelectedIndex = sel;
                cm.Items.Add(cb);
            }
            if (i >= 0 && MainWindow.theNeuronArray.Modules[i].TheModule != null)
            {
                var  t  = MainWindow.theNeuronArray.Modules[i].TheModule.GetType();
                Type t1 = Type.GetType(t.ToString() + "Dlg");
                while (t1 == null && t.BaseType.Name != "ModuleBase")
                {
                    t  = t.BaseType;
                    t1 = Type.GetType(t.ToString() + "Dlg");
                }
                if (t1 != null)
                {
                    cm.Items.Add(new MenuItem {
                        Header = "Show Dialog"
                    });
                    ((MenuItem)cm.Items[cm.Items.Count - 1]).Click += Mi_Click;
                }
            }
            r.ContextMenu = cm;
            cm.Closed    += Cm_Closed;
        }
コード例 #14
0
        public static void CreateContextMenu(int i, ModuleView nr, FrameworkElement r, ContextMenu cm = null) //for a selection
        {
            cmCancelled = false;
            if (cm == null)
            {
                cm = new ContextMenu();
            }
            cm.SetValue(AreaNumberProperty, i);
            cm.PreviewKeyDown += Cm_PreviewKeyDown;

            StackPanel sp;
            MenuItem   mi = new MenuItem();

            mi        = new MenuItem();
            mi.Header = "Delete";
            mi.Click += Mi_Click;
            cm.Items.Add(mi);
            mi        = new MenuItem();
            mi.Header = "Initialize";
            mi.Click += Mi_Click;
            cm.Items.Add(mi);
            mi = new MenuItem();
            if (nr.TheModule.ShortDescription != null || nr.TheModule.LongDescription != null)
            {
                mi.Header = "Info...";
                mi.Click += Mi_Click;
                cm.Items.Add(mi);
            }
            sp = new StackPanel {
                Orientation = Orientation.Horizontal, Margin = new Thickness(0, 3, 3, 3)
            };
            sp.Children.Add(new Label {
                Content = "Width: ", VerticalAlignment = VerticalAlignment.Center, Padding = new Thickness(0)
            });
            TextBox tb0 = new TextBox {
                Text = nr.Width.ToString(), Width = 60, Name = "AreaWidth", VerticalAlignment = VerticalAlignment.Center
            };

            tb0.TextChanged += TextChanged;
            sp.Children.Add(tb0);
            sp.Children.Add(new Label {
                Content = "Height: "
            });
            TextBox tb1 = new TextBox {
                Text = nr.Height.ToString(), Width = 60, Name = "AreaHeight", VerticalAlignment = VerticalAlignment.Center
            };

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

            sp = new StackPanel {
                Orientation = Orientation.Horizontal
            };
            sp.Children.Add(new Label {
                Content = "Name: ", Padding = new Thickness(0)
            });
            sp.Children.Add(new TextBox {
                Text = nr.Label, Width = 140, Name = "AreaName", Padding = new Thickness(0)
            });
            cm.Items.Add(new MenuItem {
                Header = sp, StaysOpenOnClick = true
            });

            //color picker
            Color    c  = Utils.IntToColor(nr.Color);
            ComboBox cb = new ComboBox();

            cb.Width = 200;
            cb.Name  = "AreaColor";
            PropertyInfo[] x1  = typeof(Colors).GetProperties();
            int            sel = -1;

            for (int i1 = 0; i1 < x1.Length; i1++)
            {
                Color cc = (Color)ColorConverter.ConvertFromString(x1[i1].Name);
                if (cc == c)

                {
                    sel = i1;
                    break;
                }
            }
            if (nr.Color == 0)
            {
                sel = 3;
            }
            foreach (PropertyInfo s in x1)
            {
                ComboBoxItem cbi = new ComboBoxItem()
                {
                    Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(s.Name)),
                    Content    = s.Name
                };
                Rectangle r1 = new Rectangle()
                {
                    Width  = 20,
                    Height = 20,
                    Fill   = new SolidColorBrush((Color)ColorConverter.ConvertFromString(s.Name)),
                    Margin = new Thickness(0, 0, 140, 0),
                };
                Grid g = new Grid();
                g.Children.Add(r1);
                g.Children.Add(new Label()
                {
                    Content = s.Name, Margin = new Thickness(25, 0, 0, 0)
                });
                cbi.Content = g;
                cbi.Width   = 200;
                cb.Items.Add(cbi);
            }
            cb.SelectedIndex = sel;
            cm.Items.Add(new MenuItem {
                Header = cb, StaysOpenOnClick = true
            });

            if (MainWindow.theNeuronArray.Modules[i].TheModule != null)
            {
                var  t  = MainWindow.theNeuronArray.Modules[i].TheModule.GetType();
                Type t1 = Type.GetType(t.ToString() + "Dlg");
                while (t1 == null && t.BaseType.Name != "ModuleBase")
                {
                    t  = t.BaseType;
                    t1 = Type.GetType(t.ToString() + "Dlg");
                }
                if (t1 != null)
                {
                    cm.Items.Add(new MenuItem {
                        Header = "Show Dialog"
                    });
                    ((MenuItem)cm.Items[cm.Items.Count - 1]).Click += Mi_Click;
                }
            }


            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
            });

            cm.Closed += Cm_Closed;
        }
コード例 #15
0
        private void MoveModule(FrameworkElement theShape, int currentNeuron)
        {
            Debug.WriteLine("currentNeuron: " + currentNeuron + " prevModuleMouseLocation:" + prevModuleMouseLocation);
            if (currentNeuron != prevModuleMouseLocation)
            {
                //which module?
                int        index            = (int)theShape.GetValue(ModuleView.AreaNumberProperty);
                ModuleView theCurrentModule = MainWindow.theNeuronArray.modules[index];
                MainWindow.theNeuronArray.AddModuleUndo(index, theCurrentModule);

                int delta    = currentNeuron - prevModuleMouseLocation;
                int newFirst = theCurrentModule.FirstNeuron + delta;
                int newLast  = theCurrentModule.LastNeuron + delta;
                MainWindow.theNeuronArray.GetNeuronLocation(newFirst, out int col0, out int row0);
                MainWindow.theNeuronArray.GetNeuronLocation(newLast, out int col1, out int row1);


                if (newFirst >= 0 && row1 > row0 && newLast < MainWindow.theNeuronArray.arraySize)
                {
                    //move all the neurons
                    List <int> neuronsToMove = new List <int>();
                    foreach (Neuron n in theCurrentModule.Neurons1)
                    {
                        neuronsToMove.Add(n.id);
                    }
                    if (!IsDestinationClear(neuronsToMove, delta))
                    {
                        MessageBoxResult result1 = MessageBox.Show("Some destination neurons are in use and will be overwritten, continue?", "Continue", MessageBoxButton.YesNo);
                        if (result1 != MessageBoxResult.Yes)
                        {
                            return;
                        }
                    }
                    if (delta > 0) //move all the nuerons...opposite order depending on the direction of the move
                    {
                        for (int i = theCurrentModule.NeuronCount - 1; i >= 0; i--)
                        {
                            Neuron src  = theCurrentModule.GetNeuronAt(i);
                            Neuron dest = MainWindow.theNeuronArray.GetNeuron(src.Id + delta);
                            MainWindow.thisWindow.theNeuronArrayView.MoveOneNeuron(src, dest);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < theCurrentModule.NeuronCount; i++)
                        {
                            Neuron src  = theCurrentModule.GetNeuronAt(i);
                            Neuron dest = MainWindow.theNeuronArray.GetNeuron(src.Id + delta);
                            MainWindow.thisWindow.theNeuronArrayView.MoveOneNeuron(src, dest);
                        }
                    }
                    //move the box
                    theCurrentModule.FirstNeuron += delta;
                    SortAreas();
                    Update();
                    prevModuleMouseLocation = currentNeuron;
                }
                else
                {
                    // MessageBox.Show("Module would be outside neuron array boundary.");
                }
            }
        }
コード例 #16
0
        public void PasteNeurons()
        {
            NeuronArray myClipBoard = MainWindow.myClipBoard;

            if (targetNeuronIndex == -1)
            {
                return;
            }
            if (myClipBoard == null)
            {
                return;
            }

            //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.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;
                    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,
                    CommandLine = mv.CommandLine,
                };

                MainWindow.theNeuronArray.modules.Add(newMV);
            }

            Update();
        }
コード例 #17
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;
                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,
                        CommandLine = mv.CommandLine,
                    };

                    myClipBoard.modules.Add(newMV);
                }
            }
        }
コード例 #18
0
 private static void Mi_Click(object sender, RoutedEventArgs e)
 {
     //Handle delete  & initialize commands
     if (sender is MenuItem mi)
     {
         if (mi.Header is StackPanel sp && sp.Children[0] is Label l && l.Content.ToString().StartsWith("Random"))
         {
             if (sp.Children[1] is TextBox tb0)
             {
                 if (int.TryParse(tb0.Text, out int count))
                 {
                     MainWindow.arrayView.CreateRandomSynapses(count);
                     MainWindow.theNeuronArray.ShowSynapses = true;
                     MainWindow.thisWindow.SetShowSynapsesCheckBox(true);
                     MainWindow.Update();
                 }
             }
             return;
         }
         if ((string)mi.Header == "Cut")
         {
             MainWindow.arrayView.CutNeurons();
             MainWindow.Update();
         }
         if ((string)mi.Header == "Copy")
         {
             MainWindow.arrayView.CopyNeurons();
         }
         if ((string)mi.Header == "Clear Selection")
         {
             MainWindow.arrayView.ClearSelection();
             MainWindow.Update();
         }
         if ((string)mi.Header == "Mutual Suppression")
         {
             MainWindow.arrayView.MutualSuppression();
             MainWindow.theNeuronArray.ShowSynapses = true;
             MainWindow.thisWindow.SetShowSynapsesCheckBox(true);
             MainWindow.Update();
         }
         if ((string)mi.Header == "Delete")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
                 MainWindow.arrayView.DeleteSelection();
             }
             else
             {
                 MainWindow.theNeuronArray.SetUndoPoint();
                 MainWindow.theNeuronArray.AddModuleUndo(-1, MainWindow.theNeuronArray.modules[i]);
                 DeleteModule(i);
                 deleted = true;
             }
         }
         if ((string)mi.Header == "Initialize")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 {
                     try
                     {
                         MainWindow.theNeuronArray.Modules[i].TheModule.Initialize();
                     }
                     catch (Exception e1)
                     {
                         MessageBox.Show("Initialize failed on module " + MainWindow.theNeuronArray.Modules[i].Label + ".   Message: " + e1.Message);
                     }
                 }
             }
         }
         if ((string)mi.Header == "Show Dialog")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 MainWindow.theNeuronArray.Modules[i].TheModule.ShowDialog();
             }
         }
         if ((string)mi.Header == "Info...")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 ModuleView        m  = MainWindow.theNeuronArray.Modules[i];
                 ModuleBase        m1 = m.TheModule;
                 ModuleDescription md = new ModuleDescription(m1.ShortDescription, m1.LongDescription);
                 md.ShowDialog();
             }
         }
         if ((string)mi.Header == "Reset Hebbian Weights")
         {
             MainWindow.theNeuronArray.SetUndoPoint();
             foreach (SelectionRectangle sr in MainWindow.arrayView.theSelection.selectedRectangles)
             {
                 foreach (int Id in sr.NeuronInRectangle())
                 {
                     Neuron n = MainWindow.theNeuronArray.GetNeuron(Id);
                     foreach (Synapse s in n.Synapses)
                     {
                         if (s.model != Synapse.modelType.Fixed)
                         {
                             //TODO: Add some UI for this:
                             //s.model = Synapse.modelType.Hebbian2;
                             n.AddSynapseWithUndo(s.targetNeuron, 0, s.model);
                             s.Weight = 0;
                         }
                     }
                 }
             }
             MainWindow.Update();
         }
     }
 }
コード例 #19
0
 private static void TextChanged(object sender, TextChangedEventArgs e)
 {
     if (sender is TextBox tb)
     {
         if (tb.Parent is StackPanel sp)
         {
             if (sp.Parent is MenuItem mi)
             {
                 if (mi.Parent is ContextMenu cm)
                 {
                     if (tb.Name == "AreaWidth")
                     {
                         int        i             = (int)cm.GetValue(AreaNumberProperty);
                         ModuleView theModuleView = MainWindow.theNeuronArray.modules[i];
                         MainWindow.theNeuronArray.GetNeuronLocation(MainWindow.theNeuronArray.modules[i].firstNeuron, out int col, out int row);
                         if (!float.TryParse(tb.Text, out float width))
                         {
                             tb.Background = new SolidColorBrush(Colors.Pink);
                         }
                         else
                         {
                             if (width < theModuleView.TheModule.MinWidth)
                             {
                                 tb.Background = new SolidColorBrush(Colors.Pink);
                             }
                             else
                             {
                                 if (width + col > MainWindow.theNeuronArray.Cols)
                                 {
                                     tb.Background = new SolidColorBrush(Colors.Pink);
                                 }
                                 else
                                 {
                                     tb.Background = new SolidColorBrush(Colors.LightGreen);
                                 }
                             }
                         }
                     }
                     if (tb.Name == "AreaHeight")
                     {
                         int        i             = (int)cm.GetValue(AreaNumberProperty);
                         ModuleView theModuleView = MainWindow.theNeuronArray.modules[i];
                         MainWindow.theNeuronArray.GetNeuronLocation(MainWindow.theNeuronArray.modules[i].firstNeuron, out int col, out int row);
                         if (!float.TryParse(tb.Text, out float height))
                         {
                             tb.Background = new SolidColorBrush(Colors.Pink);
                         }
                         else
                         {
                             if (height < theModuleView.TheModule.MinHeight)
                             {
                                 tb.Background = new SolidColorBrush(Colors.Pink);
                             }
                             else
                             {
                                 if (height + row > MainWindow.theNeuronArray.rows)
                                 {
                                     tb.Background = new SolidColorBrush(Colors.Pink);
                                 }
                                 else
                                 {
                                     tb.Background = new SolidColorBrush(Colors.LightGreen);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #20
0
        public void PasteNeurons(bool pasteSynapses = true)
        {
            NeuronArray myClipBoard;

            myClipBoard = MainWindow.myClipBoard;

            if (targetNeuronIndex == -1)
            {
                return;
            }
            if (myClipBoard == null)
            {
                return;
            }
            //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) != 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 is are in use and will be overwritten, continue?", "Continue", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }

            //now past the neurons
            for (int i = 0; i < myClipBoard.arraySize; i++)
            {
                if (myClipBoard.GetNeuron(i) != null)
                {
                    int destID = GetNeuronArrayId(i);

                    Neuron sourceNeuron = myClipBoard.GetNeuron(i).Clone();
                    sourceNeuron.id = destID;
                    MainWindow.theNeuronArray.SetNeuron(destID, sourceNeuron);
                    //MainWindow.theNeuronArray.GetNeuron(destID).Id = destID;
                    if (pasteSynapses)
                    {
                        foreach (Synapse s in myClipBoard.GetNeuron(i).Synapses)
                        {
                            MainWindow.theNeuronArray.GetNeuron(destID).AddSynapse(GetNeuronArrayId(s.TargetNeuron), s.Weight);
                        }
                    }
                }
            }

            //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,
                    CommandLine = mv.CommandLine,
                };

                MainWindow.theNeuronArray.modules.Add(newMV);
            }

            Update();
        }
コード例 #21
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));

            //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;
                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);
                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.isHebbian);
                        }
                    }
                }
            }

            //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,
                        CommandLine = mv.CommandLine,
                    };

                    myClipBoard.modules.Add(newMV);
                }
            }
        }
コード例 #22
0
ファイル: ModuleViewMenu.cs プロジェクト: oakicode/BrainSimII
 private static void Mi_Click(object sender, RoutedEventArgs e)
 {
     //Handle delete  & initialize commands
     if (sender is MenuItem mi)
     {
         if ((string)mi.Header == "Delete")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
                 i = -i - 1;
                 MainWindow.arrayView.theSelection.selectedRectangles.RemoveAt(i);
                 deleted = true;
             }
             else
             {
                 ModuleView mv = MainWindow.theNeuronArray.Modules[i];
                 foreach (Neuron n in mv.Neurons())
                 {
                     n.Reset();
                     n.DeleteAllSynapes();
                 }
                 MainWindow.theNeuronArray.Modules.RemoveAt(i);
                 deleted = true;
             }
         }
         if ((string)mi.Header == "Initialize")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 MainWindow.theNeuronArray.Modules[i].TheModule.Initialize();
             }
         }
         if ((string)mi.Header == "Show Dialog")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 MainWindow.theNeuronArray.Modules[i].TheModule.ShowDialog();
             }
         }
         if ((string)mi.Header == "Info...")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 ModuleView        m  = MainWindow.theNeuronArray.Modules[i];
                 ModuleBase        m1 = m.TheModule;
                 ModuleDescription md = new ModuleDescription(m1.ShortDescription, m1.LongDescription);
                 md.ShowDialog();
             }
         }
         if ((string)mi.Header == "Reset Hebbian Weights")
         {
             foreach (NeuronSelectionRectangle sr in MainWindow.arrayView.theSelection.selectedRectangles)
             {
                 foreach (int Id in sr.NeuronInRectangle())
                 {
                     Neuron n = MainWindow.theNeuronArray.GetNeuron(Id);
                     foreach (Synapse s in n.Synapses)
                     {
                         if (s.IsHebbian)
                         {
                             s.Weight = 0;
                         }
                     }
                 }
             }
             MainWindow.Update();
         }
     }
 }
コード例 #23
0
 private static void Mi_Click(object sender, RoutedEventArgs e)
 {
     //Handle delete  & initialize commands
     if (sender is MenuItem mi)
     {
         if ((string)mi.Header == "Cut")
         {
             MainWindow.arrayView.CutNeurons();
             MainWindow.Update();
         }
         if ((string)mi.Header == "Copy")
         {
             MainWindow.arrayView.CopyNeurons();
         }
         if ((string)mi.Header == "Clear Selection")
         {
             MainWindow.arrayView.ClearSelection();
             MainWindow.Update();
         }
         if ((string)mi.Header == "Mutual Suppression")
         {
             MainWindow.arrayView.MutualSuppression();
             MainWindow.Update();
         }
         if ((string)mi.Header == "Delete")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
                 MainWindow.arrayView.DeleteSelection();
             }
             else
             {
                 ModuleView mv = MainWindow.theNeuronArray.Modules[i];
                 foreach (Neuron n in mv.Neurons())
                 {
                     n.Reset();
                     n.DeleteAllSynapes();
                 }
                 MainWindow.theNeuronArray.Modules.RemoveAt(i);
                 deleted = true;
             }
         }
         if ((string)mi.Header == "Initialize")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 MainWindow.theNeuronArray.Modules[i].TheModule.Initialize();
             }
         }
         if ((string)mi.Header == "Show Dialog")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 MainWindow.theNeuronArray.Modules[i].TheModule.ShowDialog();
             }
         }
         if ((string)mi.Header == "Info...")
         {
             int i = (int)mi.Parent.GetValue(AreaNumberProperty);
             if (i < 0)
             {
             }
             else
             {
                 ModuleView        m  = MainWindow.theNeuronArray.Modules[i];
                 ModuleBase        m1 = m.TheModule;
                 ModuleDescription md = new ModuleDescription(m1.ShortDescription, m1.LongDescription);
                 md.ShowDialog();
             }
         }
         if ((string)mi.Header == "Reset Hebbian Weights")
         {
             MainWindow.theNeuronArray.SetUndoPoint();
             foreach (NeuronSelectionRectangle sr in MainWindow.arrayView.theSelection.selectedRectangles)
             {
                 foreach (int Id in sr.NeuronInRectangle())
                 {
                     Neuron n = MainWindow.theNeuronArray.GetNeuron(Id);
                     foreach (Synapse s in n.Synapses)
                     {
                         if (s.model != Synapse.modelType.Fixed)
                         {
                             //TODO: Add some UI for this:
                             //s.model = Synapse.modelType.Hebbian2;
                             n.AddSynapseWithUndo(s.targetNeuron, 0, s.model);
                             s.Weight = 0;
                         }
                     }
                 }
             }
             MainWindow.Update();
         }
     }
 }
コード例 #24
0
        public void theCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            Point         pt     = e.GetPosition((UIElement)sender);
            HitTestResult result = VisualTreeHelper.HitTest(theCanvas, pt);

            if (mouseRepeatTimer != null)
            {
                if (mouseRepeatTimer.IsEnabled && mouseRepeatTimer.Interval == new TimeSpan(0, 0, 0, 0, 100))
                {
                    return;
                }
                mouseRepeatTimer.Stop();
            }
            if (MainWindow.theNeuronArray == null)
            {
                return;
            }
            if (e.RightButton == MouseButtonState.Pressed)
            {
                return;
            }

            Point currentPosition = e.GetPosition(theCanvas);

            LimitMousePostion(ref currentPosition);
            int currentNeuron = dp.NeuronFromPoint(currentPosition);

            //are we dragging a synapse? rubber-band it
            if (e.LeftButton == MouseButtonState.Pressed && theCanvas.Cursor == Cursors.UpArrow && dragging)
            {
                if (mouseDownNeuronIndex > -1)
                {
                    if (synapseShape != null || (mouseDownNeuronIndex != currentNeuron))
                    {
                        if (synapseShape != null)
                        {
                            theCanvas.Children.Remove(synapseShape);
                        }
                        Shape l = SynapseView.GetSynapseShape
                                      (dp.pointFromNeuron(mouseDownNeuronIndex),
                                      dp.pointFromNeuron(currentNeuron),
                                      this,
                                      lastSynapseModel
                                      );
                        l.Stroke = new SolidColorBrush(Utils.RainbowColorFromValue(lastSynapseWeight));
                        theCanvas.Children.Add(l);
                        synapseShape = l;
                    }
                }
            }
            else if (e.LeftButton != MouseButtonState.Pressed) //we may have missed a mouse-up event...clear out the rubber-banding
            {
                synapseShape         = null;
                mouseDownNeuronIndex = -1;
            }

            if (theCanvas.Cursor == Cursors.Cross || theCanvas.Cursor == Cursors.ScrollN || theCanvas.Cursor == Cursors.ScrollS || theCanvas.Cursor == Cursors.ScrollE ||
                theCanvas.Cursor == Cursors.ScrollW || theCanvas.Cursor == Cursors.ScrollNW || theCanvas.Cursor == Cursors.ScrollNE ||
                theCanvas.Cursor == Cursors.ScrollSW || theCanvas.Cursor == Cursors.ScrollSE || theCanvas.Cursor == Cursors.ScrollAll)
            {
                //set the cursor if are we inside an existing module rectangle?
                if (e.LeftButton != MouseButtonState.Pressed)
                {
                    bool cursorSet = false;
                    na = null;
                    ////is the mouse in a module?
                    for (int i = 0; i < MainWindow.theNeuronArray.modules.Count; i++)
                    {
                        Rectangle r    = MainWindow.theNeuronArray.modules[i].GetRectangle(dp);
                        double    left = Canvas.GetLeft(r);
                        double    top  = Canvas.GetTop(r);
                        if (SetScrollCursor(currentPosition, r, left, top))
                        {
                            cursorSet           = true;
                            na                  = MainWindow.theNeuronArray.modules[i];
                            firstSelectedNeuron = currentNeuron;
                        }
                    }
                    //is the mouse in a selection?
                    foreach (NeuronSelectionRectangle nsr in theSelection.selectedRectangles)
                    {
                        Rectangle r    = nsr.GetRectangle(dp);
                        double    left = Canvas.GetLeft(r);
                        double    top  = Canvas.GetTop(r);
                        if (currentPosition.X >= left &&
                            currentPosition.X <= left + r.Width &&
                            currentPosition.Y >= top &&
                            currentPosition.Y <= top + r.Height)
                        {
                            theCanvas.Cursor = Cursors.ScrollAll;
                            cursorSet        = true;
                        }
                    }
                    if (!cursorSet)
                    {
                        theCanvas.Cursor = Cursors.Cross;
                    }
                }

                //handle the creation/updating of a selection rectangle
                if (e.LeftButton == MouseButtonState.Pressed && dragRectangle != null)
                {
                    //Get the first & last selected neurons
                    SetFirstLastSelectedNeurons(currentNeuron);

                    //update graphic rectangle
                    Point p1 = dp.pointFromNeuron(firstSelectedNeuron);
                    Point p2 = dp.pointFromNeuron(lastSelectedNeuron);
                    dragRectangle.Width  = p2.X - p1.X + dp.NeuronDisplaySize;
                    dragRectangle.Height = p2.Y - p1.Y + dp.NeuronDisplaySize;
                    Canvas.SetLeft(dragRectangle, p1.X);
                    Canvas.SetTop(dragRectangle, p1.Y);
                    if (!theCanvas.Children.Contains(dragRectangle))
                    {
                        theCanvas.Children.Add(dragRectangle);
                    }
                }
            }
            //handle moving a selection
            if (e.LeftButton == MouseButtonState.Pressed && theCanvas.Cursor == Cursors.ScrollAll && na == null)
            {
                if (currentNeuron != mouseDownNeuronIndex)
                {
                    if (theSelection.selectedRectangles.Count > 0)
                    {
                        MainWindow.theNeuronArray.AddSelectionUndo();
                        int offset = currentNeuron - mouseDownNeuronIndex;
                        targetNeuronIndex = theSelection.selectedRectangles[0].FirstSelectedNeuron + offset;
                        MoveNeurons(true);
                    }
                }
                mouseDownNeuronIndex = currentNeuron;
            }

            //handle moving of a module
            if (e.LeftButton == MouseButtonState.Pressed && theCanvas.Cursor == Cursors.ScrollAll && na != null)
            {
                if (currentNeuron != firstSelectedNeuron)
                {
                    int newFirst = na.FirstNeuron + currentNeuron - firstSelectedNeuron;
                    int newLast  = na.LastNeuron + currentNeuron - firstSelectedNeuron;
                    na.GetAbsNeuronLocation(newFirst, out int xf, out int yf);
                    na.GetAbsNeuronLocation(newLast, out int xl, out int yl);

                    if (newFirst >= 0 && newLast < MainWindow.theNeuronArray.arraySize &&
                        xf <= xl && yf <= yl)
                    {
                        //move all the neurons
                        int delta = currentNeuron - firstSelectedNeuron;
                        if (delta > 0) //move all the nerons...opposite order depending on the direction of the move
                        {
                            for (int i = na.NeuronCount - 1; i >= 0; i--)
                            {
                                Neuron src  = na.GetNeuronAt(i);
                                Neuron dest = MainWindow.theNeuronArray.GetNeuron(src.Id + delta);
                                MainWindow.thisWindow.theNeuronArrayView.MoveOneNeuron(src, dest);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < na.NeuronCount; i++)
                            {
                                Neuron src  = na.GetNeuronAt(i);
                                Neuron dest = MainWindow.theNeuronArray.GetNeuron(src.Id + delta);
                                MainWindow.thisWindow.theNeuronArrayView.MoveOneNeuron(src, dest);
                            }
                        }

                        //move the box
                        na.FirstNeuron += currentNeuron - firstSelectedNeuron;
                        SortAreas();
                        Update();
                    }
                    firstSelectedNeuron = currentNeuron;
                }
            }
            //handle sizing of a module
            if (e.LeftButton == MouseButtonState.Pressed && na != null &&
                (theCanvas.Cursor == Cursors.ScrollN ||
                 theCanvas.Cursor == Cursors.ScrollS ||
                 theCanvas.Cursor == Cursors.ScrollE ||
                 theCanvas.Cursor == Cursors.ScrollW ||
                 theCanvas.Cursor == Cursors.ScrollNW ||
                 theCanvas.Cursor == Cursors.ScrollNE ||
                 theCanvas.Cursor == Cursors.ScrollSW ||
                 theCanvas.Cursor == Cursors.ScrollSE)
                )
            {
                //TODO: Add rearrangement of neurons
                //TODO: Add clone of neurons to handle ALL properties
                dragging = true;
                na.GetBounds(out int X1, out int Y1, out int X2, out int Y2);
                na.GetAbsNeuronLocation(firstSelectedNeuron, out int Xf, out int Yf);
                na.GetAbsNeuronLocation(currentNeuron, out int Xc, out int Yc);
                na.GetAbsNeuronLocation(na.LastNeuron, out int Xl, out int Yl);
                int minHeight = na.TheModule.MinHeight;
                int minWidth  = na.TheModule.MinWidth;

                //move the top?
                if (theCanvas.Cursor == Cursors.ScrollN || theCanvas.Cursor == Cursors.ScrollNE || theCanvas.Cursor == Cursors.ScrollNW)
                {
                    if (Yc != Yf)
                    {
                        int newTop = Y1 + Yc - Yf;
                        if (newTop <= Y2)
                        {
                            na.Height -= Yc - Yf;
                            if (na.Height < minHeight)
                            {
                                na.Height = minHeight;
                            }
                            else
                            {
                                na.FirstNeuron     += Yc - Yf;
                                firstSelectedNeuron = currentNeuron;
                            }
                            SortAreas();
                            Update();
                        }
                    }
                }
                //move the left?
                if (theCanvas.Cursor == Cursors.ScrollW || theCanvas.Cursor == Cursors.ScrollNW || theCanvas.Cursor == Cursors.ScrollSW)
                {
                    if (Xc != Xf)
                    {
                        int newLeft = X1 + Xc - Xf;
                        if (newLeft <= X2)
                        {
                            na.Width -= Xc - Xf;
                            if (na.Width < minWidth)
                            {
                                na.Width = minWidth;
                            }
                            else
                            {
                                na.FirstNeuron     += (Xc - Xf) * MainWindow.theNeuronArray.rows;
                                firstSelectedNeuron = currentNeuron;
                            }
                            SortAreas();
                            Update();
                        }
                    }
                }
                //Move the Right
                if (theCanvas.Cursor == Cursors.ScrollE || theCanvas.Cursor == Cursors.ScrollNE || theCanvas.Cursor == Cursors.ScrollSE)
                {
                    if (Xc != Xf)
                    {
                        int newRight = X2 + Xc - Xf;
                        if (newRight >= X1)
                        {
                            na.Width += Xc - Xf;
                            if (na.Width < minWidth)
                            {
                                na.Width = minWidth;
                            }
                            else
                            {
                                firstSelectedNeuron = currentNeuron;
                            }
                            Update();
                        }
                    }
                }
                //Move the Bottom
                if (theCanvas.Cursor == Cursors.ScrollS || theCanvas.Cursor == Cursors.ScrollSE || theCanvas.Cursor == Cursors.ScrollSW)
                {
                    if (Yc != Yf)
                    {
                        int newBottom = Y2 + Yc - Yf;
                        if (newBottom >= Y1)
                        {
                            na.Height += Yc - Yf;
                            if (na.Height < minHeight)
                            {
                                na.Height = minHeight;
                            }
                            else
                            {
                                firstSelectedNeuron = currentNeuron;
                            }
                            Update();
                        }
                    }
                }
            }

            if (theCanvas.Cursor == Cursors.Hand)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    ContinuePan(e.GetPosition((UIElement)theCanvas.Parent));
                    //lastPositionOnGrid = e.GetPosition((UIElement)theCanvas.Parent);
                }
                else
                {
                    lastPositionOnCanvas = new Point(0, 0);
                }
            }
        }
コード例 #25
0
ファイル: ModuleViewMenu.cs プロジェクト: oakicode/BrainSimII
        private static void Cm_Closed(object sender, RoutedEventArgs e)
        {
            if ((Keyboard.GetKeyStates(Key.Escape) & KeyStates.Down) > 0)
            {
                return;
            }
            if (deleted)
            {
                deleted = false;
            }
            else if (sender is ContextMenu cm)
            {
                int    i = (int)cm.GetValue(AreaNumberProperty);
                string label = "";
                string commandLine = "";
                Color  color = Colors.Wheat;
                int    width = 1, height = 1;

                Control cc = Utils.FindByName(cm, "AreaName");
                if (cc is TextBox tb)
                {
                    label = tb.Text;
                }
                cc = Utils.FindByName(cm, "AreaWidth");
                if (cc is TextBox tb1)
                {
                    int.TryParse(tb1.Text, out width);
                }
                cc = Utils.FindByName(cm, "AreaHeight");
                if (cc is TextBox tb2)
                {
                    int.TryParse(tb2.Text, out height);
                }
                cc = Utils.FindByName(cm, "AreaType");
                if (cc is ComboBox cb && cb.SelectedValue != null)
                {
                    commandLine = (string)cb.SelectedValue;
                }
                if (commandLine == "")
                {
                    return;                   //something went wrong
                }
                cc = Utils.FindByName(cm, "CommandParams");
                if (cc is TextBox tb3)
                {
                    commandLine += " " + tb3.Text;
                }
                if ((label == "new" || label == "") && commandLine != "")
                {
                    label = commandLine;
                }
                cc = Utils.FindByName(cm, "AreaColor");
                if (cc is ComboBox cb1)
                {
                    color = ((SolidColorBrush)((ComboBoxItem)cb1.SelectedValue).Background).Color;
                }
                if (label == " " && commandLine == " ")
                {
                    return;
                }
                if (i >= 0)
                {
                    ModuleView theModuleView = MainWindow.theNeuronArray.modules[i];
                    //update the existing module
                    theModuleView.Label       = label;
                    theModuleView.CommandLine = commandLine;
                    theModuleView.Color       = Utils.ColorToInt(color);

                    //did we change the module type?
                    string[] Params = commandLine.Split(' ');
                    Type     t1x    = Type.GetType("BrainSimulator.Modules." + Params[0]);
                    if (t1x != null && (MainWindow.theNeuronArray.modules[i].TheModule == null || MainWindow.theNeuronArray.modules[i].TheModule.GetType() != t1x))
                    {
                        MainWindow.theNeuronArray.modules[i].TheModule = (ModuleBase)Activator.CreateInstance(t1x);
                        MainWindow.theNeuronArray.modules[i].label     = Params[0];
                    }

                    MainWindow.theNeuronArray.GetNeuronLocation(MainWindow.theNeuronArray.modules[i].firstNeuron, out int col, out int row);
                    if (width < theModuleView.TheModule.MinWidth)
                    {
                        width = theModuleView.TheModule.MinWidth;
                    }
                    if (height < theModuleView.TheModule.MinHeight)
                    {
                        height = theModuleView.TheModule.MinHeight;
                    }

                    bool dimsChanged = false;

                    if (width + col >= MainWindow.theNeuronArray.Cols)
                    {
                        width       = MainWindow.theNeuronArray.Cols - col;
                        dimsChanged = true;
                    }
                    if (height + row >= MainWindow.theNeuronArray.rows)
                    {
                        height      = MainWindow.theNeuronArray.rows - row;
                        dimsChanged = true;
                    }
                    if (dimsChanged)
                    {
                        MessageBox.Show("Dimensions reduced to stay within neuron array bondary.", "Warning", MessageBoxButton.OK);
                    }
                    theModuleView.Width  = width;
                    theModuleView.Height = height;
                }
                else
                {
                    //convert a selection rectangle to a module
                    i      = -i - 1;
                    width  = MainWindow.arrayView.theSelection.selectedRectangles[i].Width;
                    height = MainWindow.arrayView.theSelection.selectedRectangles[i].Height;
                    NeuronSelectionRectangle nsr = MainWindow.arrayView.theSelection.selectedRectangles[i];
                    MainWindow.arrayView.theSelection.selectedRectangles.RemoveAt(i);
                    CreateModule(label, commandLine, color, nsr.FirstSelectedNeuron, width, height);
                }
            }
            MainWindow.Update();
        }
コード例 #26
0
        public void theCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //set the cursor before checking for busy so the wait cursor can be set
            shapeType theShapeType = SetMouseCursorShape(e, out FrameworkElement theShape);

            if (MainWindow.Busy())
            {
                return;
            }

            if (MainWindow.theNeuronArray == null)
            {
                return;
            }
            MainWindow.theNeuronArray.SetUndoPoint();
            Debug.WriteLine("theCanvas_MouseDown" + MainWindow.theNeuronArray.Generation + theShape + theShapeType);
            Point currentPosition = e.GetPosition(theCanvas);

            LimitMousePostion(ref currentPosition);
            mouseDownNeuronIndex = dp.NeuronFromPoint(currentPosition);

            if (e.RightButton == MouseButtonState.Pressed)
            {
                if (sender is Image img) //TODO: fix this, it should be a selection hit
                {
                    int        i  = (int)img.GetValue(ModuleView.AreaNumberProperty);
                    int        i1 = -i - 1;
                    ModuleView nr = new ModuleView
                    {
                        Label         = "new",
                        Width         = theSelection.selectedRectangles[i1].Width,
                        Height        = theSelection.selectedRectangles[i1].Height,
                        Color         = Utils.ColorToInt(Colors.Aquamarine),
                        ModuleTypeStr = ""
                    };
                    img.ContextMenu = new ContextMenu();
                    ModuleView.CreateContextMenu(i, nr, img, img.ContextMenu);
                    img.ContextMenu.IsOpen = true;
                }
                else if (theShapeType == shapeType.Neuron)
                {
                    OpenNeuronContextMenu(theShape);
                }
                else if (theShapeType == shapeType.Synapse)
                {
                    OpenSynapseContextMenu(theShape);
                }
                else if (theShapeType == shapeType.Selection)
                {
                    OpenSelectionContextMenu(theShape);
                }
                else if (theShapeType == shapeType.Module)
                {
                    OpenModuleContextMenu(theShape);
                }
                return;
            } //end of right-button pressed

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (theShapeType == shapeType.Neuron)
                {
                    Neuron n = null;
                    if (mouseDownNeuronIndex >= 0 && mouseDownNeuronIndex < MainWindow.theNeuronArray.arraySize)
                    {
                        n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex) as Neuron;
                    }

                    int clickCount = e.ClickCount;
                    n = NeuronMouseDown(n, clickCount);
                }

                if (theShapeType == shapeType.Synapse)
                {
                    StartSynapseDragging(theShape);
                }

                //start a new selection rectangle drag
                if (theShapeType == shapeType.Canvas && labelCursor == null)
                {
                    currentPosition = StartNewSelectionDrag();
                }

                //either a module or a selection, we're dragging it
                if (theShapeType == shapeType.Module && theCanvas.Cursor == Cursors.ScrollAll)
                {
                    StartaMovingModule(theShape, mouseDownNeuronIndex);
                }
                if (theShapeType == shapeType.Module && theCanvas.Cursor != Cursors.ScrollAll)
                {
                    StartaSizingModule(theShape, mouseDownNeuronIndex);
                }

                if (theShapeType == shapeType.Selection)
                {
                    StartMovingSelection(theShape);
                }
                //starting pan
                if (theCanvas.Cursor == Cursors.Hand)
                {
                    StartPan(e.GetPosition((UIElement)theCanvas.Parent));
                }
                if (currentOperation == CurrentOperation.insertingModule)
                {
                    InsertModule(mouseDownNeuronIndex);
                    Mouse.Capture(null);
                }
                else
                {
                    Mouse.Capture(theCanvas); //if you don't do this, you might lose the mouseup event
                }
            }//end of left-button down
        }
コード例 #27
0
        public void theCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (MainWindow.theNeuronArray == null)
            {
                return;
            }
            Debug.WriteLine("theCanvas_MouseDown" + MainWindow.theNeuronArray.Generation);
            Point currentPosition = e.GetPosition(theCanvas);

            LimitMousePostion(ref currentPosition);
            mouseDownNeuronIndex = dp.NeuronFromPoint(currentPosition);

            if (e.RightButton == MouseButtonState.Pressed)
            {
                if (sender is Image img)
                {
                    int        i  = (int)img.GetValue(ModuleView.AreaNumberProperty);
                    int        i1 = -i - 1;
                    ModuleView nr = new ModuleView
                    {
                        Label       = "new",
                        Width       = theSelection.selectedRectangles[i1].Width,
                        Height      = theSelection.selectedRectangles[i1].Height,
                        Color       = Utils.ColorToInt(Colors.Aquamarine),
                        CommandLine = ""
                    };
                    img.ContextMenu = new ContextMenu();
                    ModuleView.CreateContextMenu(i, nr, img, img.ContextMenu);
                    img.ContextMenu.IsOpen = true;
                    e.Handled = true;
                }
                else if (sender is Rectangle r)
                {
                    int i = (int)r.GetValue(ModuleView.AreaNumberProperty);
                    if (i >= 0)
                    {
                        ModuleView nr = MainWindow.theNeuronArray.Modules[i];
                        r.ContextMenu = new ContextMenu();
                        ModuleView.CreateContextMenu(i, nr, r, r.ContextMenu);
                        r.ContextMenu.IsOpen = true;
                        e.Handled            = true;
                    }
                    if (i < 0)
                    {
                        int        i1 = -i - 1;
                        ModuleView nr = new ModuleView
                        {
                            Label       = "new",
                            Width       = theSelection.selectedRectangles[i1].Width,
                            Height      = theSelection.selectedRectangles[i1].Height,
                            Color       = Utils.ColorToInt(Colors.Aquamarine),
                            CommandLine = ""
                        };
                        r.ContextMenu = new ContextMenu();
                        ModuleView.CreateContextMenu(i, nr, r, r.ContextMenu);
                        r.ContextMenu.IsOpen = true;
                        e.Handled            = true;
                    }
                }
                else if (sender is Label l)
                {
                    l.ContextMenu = new ContextMenu();
                    Neuron n1 = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex);
                    NeuronView.CreateContextMenu(mouseDownNeuronIndex, n1, l.ContextMenu);
                    l.ContextMenu.IsOpen = true;
                    e.Handled            = true;
                }
                else if (sender is Shape s)
                {
                    if ((s is Path || s is Line ||
                         (s is Ellipse && (int)s.GetValue(SynapseView.SourceIDProperty) != 0))) // a synapse
                    {
                        int    source = (int)s.GetValue(SynapseView.SourceIDProperty);
                        int    target = (int)s.GetValue(SynapseView.TargetIDProperty);
                        float  weight = (float)s.GetValue(SynapseView.WeightValProperty);
                        Neuron n1     = MainWindow.theNeuronArray.GetCompleteNeuron(source);
                        n1 = MainWindow.theNeuronArray.AddSynapses(n1);
                        Synapse s1 = n1.FindSynapse(target);
                        s.ContextMenu = new ContextMenu();
                        SynapseView.CreateContextMenu(source, s1, s.ContextMenu);
                    }
                    else if (s is Ellipse) // a neuron
                    {
                        s.ContextMenu = new ContextMenu();
                        Neuron n1 = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex);
                        NeuronView.CreateContextMenu(mouseDownNeuronIndex, n1, s.ContextMenu);
                        targetNeuronIndex = mouseDownNeuronIndex;
                    }
                    if (s.ContextMenu != null)
                    {
                        s.ContextMenu.IsOpen = true;
                    }
                    e.Handled = true;
                }
                else
                {
                }
                return;
            }

            Neuron n = null;

            if (mouseDownNeuronIndex >= 0 && mouseDownNeuronIndex < MainWindow.theNeuronArray.arraySize)
            {
                n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex) as Neuron;
            }

            if (theCanvas.Cursor == Cursors.Cross)
            {
                //          Debug.WriteLine("dragStart" + MainWindow.theNeuronArray.Generation);
                if (dragRectangle != null)
                {
                    theCanvas.Children.Remove(dragRectangle);
                }

                MainWindow.theNeuronArray.SetUndoPoint();
                MainWindow.theNeuronArray.AddSelectionUndo();
                if (!MainWindow.ctrlPressed)
                {
                    theSelection.selectedRectangles.Clear();
                }
                else
                {
                    Update();
                }

                //snap to neuron point
                currentPosition = dp.pointFromNeuron(mouseDownNeuronIndex);

                //build the draggable selection rectangle
                dragRectangle              = new Rectangle();
                dragRectangle.Width        = dragRectangle.Height = dp.NeuronDisplaySize;
                dragRectangle.Stroke       = new SolidColorBrush(Colors.Red);
                dragRectangle.Fill         = new SolidColorBrush(Colors.Red);
                dragRectangle.Fill.Opacity = 0.5;
                Canvas.SetLeft(dragRectangle, currentPosition.X);
                Canvas.SetTop(dragRectangle, currentPosition.Y);
                theCanvas.Children.Add(dragRectangle);
                firstSelectedNeuron = mouseDownNeuronIndex;
                lastSelectedNeuron  = mouseDownNeuronIndex;
                Mouse.Capture(theCanvas);
            }

            if (theCanvas.Cursor == Cursors.ScrollAll)
            {
                dragging = true;
                MainWindow.theNeuronArray.SetUndoPoint();
            }
            if (theCanvas.Cursor == Cursors.UpArrow)
            {
                if (e.ClickCount == 2 && sender is Canvas)
                {
                    //double-click detected
                    n          = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex);
                    n.leakRate = -n.leakRate;
                    n.Update();
                }

                Mouse.Capture(theCanvas);
                if (mouseRepeatTimer == null)
                {
                    mouseRepeatTimer = new DispatcherTimer();
                }
                if (mouseRepeatTimer.IsEnabled)
                {
                    mouseRepeatTimer.Stop();
                }
                mouseRepeatTimer.Interval = new TimeSpan(0, 0, 0, 0, 250);
                mouseRepeatTimer.Tick    += MouseRepeatTimer_Tick;
                mouseRepeatTimer.Start();
                dragging          = true;
                targetNeuronIndex = mouseDownNeuronIndex;
            }
            if (theCanvas.Cursor == Cursors.Hand)
            {
                StartPan(e.GetPosition((UIElement)theCanvas.Parent));
                Mouse.Capture(theCanvas);
            }
        }
コード例 #28
0
        private static void Cm_Closed(object sender, RoutedEventArgs e)
        {
            if ((Keyboard.GetKeyStates(Key.Escape) & KeyStates.Down) > 0)
            {
                MainWindow.Update();
                return;
            }
            if (deleted)
            {
                deleted = false;
            }
            else if (sender is ContextMenu cm)
            {
                if (!cm.IsOpen)
                {
                    return;
                }
                cm.IsOpen = false;
                if (cmCancelled)
                {
                    return;
                }

                int    i = (int)cm.GetValue(AreaNumberProperty);
                string label = "";
                string theModuleTypeStr = "";
                Color  color = Colors.Wheat;
                int    width = 1, height = 1;

                Control cc = Utils.FindByName(cm, "AreaName");
                if (cc is TextBox tb)
                {
                    label = tb.Text;
                }
                cc = Utils.FindByName(cm, "Enabled");
                bool isEnabled = true;
                if (cc is CheckBox cb2)
                {
                    isEnabled = (bool)cb2.IsChecked;
                }

                cc = Utils.FindByName(cm, "AreaWidth");
                if (cc is TextBox tb1)
                {
                    int.TryParse(tb1.Text, out width);
                }
                cc = Utils.FindByName(cm, "AreaHeight");
                if (cc is TextBox tb2)
                {
                    int.TryParse(tb2.Text, out height);
                }
                cc = Utils.FindByName(cm, "AreaType");
                if (cc is ComboBox cb && cb.SelectedValue != null)
                {
                    theModuleTypeStr = "Module" + (string)cb.SelectedValue;
                    if (theModuleTypeStr == "")
                    {
                        return;                        //something went wrong
                    }
                    label = (string)cb.SelectedValue;
                }

                cc = Utils.FindByName(cm, "AreaColor");
                if (cc is ComboBox cb1)
                {
                    color = ((SolidColorBrush)((ComboBoxItem)cb1.SelectedValue).Background).Color;
                }
                if (label == "" && theModuleTypeStr == "")
                {
                    return;
                }

                ModuleView theModuleView = MainWindow.theNeuronArray.modules[i];
                MainWindow.theNeuronArray.SetUndoPoint();
                MainWindow.theNeuronArray.AddModuleUndo(i, theModuleView);
                //update the existing module
                theModuleView.Label               = label;
                theModuleView.ModuleTypeStr       = theModuleTypeStr;
                theModuleView.Color               = Utils.ColorToInt(color);
                theModuleView.TheModule.isEnabled = isEnabled;

                //did we change the module type?
                Type t1x = Type.GetType("BrainSimulator.Modules." + theModuleTypeStr);
                if (t1x != null && (MainWindow.theNeuronArray.modules[i].TheModule == null || MainWindow.theNeuronArray.modules[i].TheModule.GetType() != t1x))
                {
                    MainWindow.theNeuronArray.modules[i].TheModule = (ModuleBase)Activator.CreateInstance(t1x);
                    MainWindow.theNeuronArray.modules[i].label     = theModuleTypeStr;
                }

                MainWindow.theNeuronArray.GetNeuronLocation(MainWindow.theNeuronArray.modules[i].firstNeuron, out int col, out int row);
                if (width < theModuleView.TheModule.MinWidth)
                {
                    width = theModuleView.TheModule.MinWidth;
                }
                if (height < theModuleView.TheModule.MinHeight)
                {
                    height = theModuleView.TheModule.MinHeight;
                }

                bool dimsChanged = false;

                if (width + col > MainWindow.theNeuronArray.Cols)
                {
                    width       = MainWindow.theNeuronArray.Cols - col;
                    dimsChanged = true;
                }
                if (height + row > MainWindow.theNeuronArray.rows)
                {
                    height      = MainWindow.theNeuronArray.rows - row;
                    dimsChanged = true;
                }
                if (dimsChanged)
                {
                    MessageBox.Show("Dimensions reduced to stay within neuron array bondary.", "Warning", MessageBoxButton.OK);
                }
                theModuleView.Width  = width;
                theModuleView.Height = height;
            }
            MainWindow.Update();
        }