Example #1
0
        private static void Mi_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            //find out which neuron this context menu is from
            ContextMenu cm = mi.Parent as ContextMenu;

            if (cm == null)
            {
                MenuItem mi2 = mi.Parent as MenuItem;
                cm = mi2.Parent as ContextMenu;
            }
            int    i = (int)cm.GetValue(NeuronIDProperty);
            Neuron n = MainWindow.theNeuronArray.GetNeuron(i);

            if ((string)mi.Header == "Always Fire")
            {
                if (n.FindSynapse(i) == null)
                {
                    n.AddSynapse(i, 1, MainWindow.theNeuronArray, true);
                }
                else
                {
                    n.DeleteSynapse(i);
                }
            }
            if ((string)mi.Header == "Paste Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.PasteNeurons();
                theNeuronArrayView.targetNeuronIndex = -1;
                cmCancelled = true;
            }
            if ((string)mi.Header == "Move Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.MoveNeurons();
                cmCancelled = true;
            }
            if ((string)mi.Header == "Connect to Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.ConnectToHere();
            }
            if ((string)mi.Header == "Connect from Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.ConnectFromHere();
            }
            if ((string)mi.Header == "Select as Target")
            {
                theNeuronArrayView.targetNeuronIndex = i;
            }
        }
Example #2
0
        //SYNAPSE
        private static void OpenSynapseContextMenu(FrameworkElement theShape)
        {
            int    source = (int)theShape.GetValue(SynapseView.SourceIDProperty);
            int    target = (int)theShape.GetValue(SynapseView.TargetIDProperty);
            float  weight = (float)theShape.GetValue(SynapseView.WeightValProperty);
            Neuron n1     = MainWindow.theNeuronArray.GetCompleteNeuron(source);

            n1 = MainWindow.theNeuronArray.AddSynapses(n1);
            Synapse s1 = n1.FindSynapse(target);

            theShape.ContextMenu = new ContextMenu();
            SynapseView.CreateContextMenu(source, s1, theShape.ContextMenu);
        }
Example #3
0
        private static void SynapseMenuItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            MenuItem    mi       = sender as MenuItem;
            MenuItem    mi2      = mi.Parent as MenuItem;
            ContextMenu cm       = mi2.Parent as ContextMenu;
            int         sourceID = (int)cm.GetValue(NeuronIDProperty);
            ContextMenu newCm    = new ContextMenu();

            int.TryParse(mi.Header.ToString().Substring(0, 8), out int targetID);
            Neuron  n = MainWindow.theNeuronArray.GetNeuron(sourceID);
            Synapse s = n.FindSynapse(targetID);

            if (s != null)
            {
                SynapseView.CreateContextMenu(sourceID, s, newCm);
                newCm.IsOpen = true;
                e.Handled    = true;
            }
        }
        public void MoveOneNeuron(Neuron n, Neuron nNewLocation)
        {
            //copy the neuron attributes and delete them from the old neuron.
            n.Copy(nNewLocation);


            //for all the synapses going out this neuron, change to going from new location
            //don't use a foreach here because the body of the loop may delete a list entry
            if (n.synapses != null)
            {
                for (int k = 0; k < n.synapses.Count; k++)
                {
                    Synapse s  = n.Synapses[k];
                    Synapse s1 = nNewLocation.AddSynapse(s.TargetNeuron, s.Weight);
                    if (s1 != null && s.IsHebbian)
                    {
                        s1.IsHebbian = true;
                    }
                }
            }

            //for all the synapses coming into this neuron, change the synapse target to new location
            if (n.synapses != null)
            {
                for (int k = 0; k < n.synapsesFrom.Count; k++)
                {
                    Synapse reverseSynapse = n.SynapsesFrom[k]; //(from synapses are sort-of backward
                    if (reverseSynapse.TargetNeuron != -1)
                    {
                        Neuron  sourceNeuron = MainWindow.theNeuronArray.GetNeuron(reverseSynapse.TargetNeuron);
                        Synapse s            = sourceNeuron.FindSynapse(n.Id);
                        if (s != null)
                        {
                            s.TargetNeuron = nNewLocation.Id;
                            nNewLocation.SynapsesFrom.Add(new Synapse(sourceNeuron.Id, s.Weight));
                        }
                    }
                }
            }
            n.Clear();
        }
        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);
            }
        }
Example #6
0
        private static void Mi_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            //find out which neuron this context menu is from
            ContextMenu cm = mi.Parent as ContextMenu;

            if (cm == null)
            {
                MenuItem mi2 = mi.Parent as MenuItem;
                if (mi2.Header.ToString().IndexOf("Synapses") == 0)
                {
                    int.TryParse(mi.Header.ToString().Substring(0, 8), out int newID);
                    Neuron n1 = MainWindow.theNeuronArray.GetNeuron(newID);
                    NeuronView.CreateContextMenu(n1.id, n1, new ContextMenu()
                    {
                        IsOpen = true,
                    });
                    return;
                }
                cm = mi2.Parent as ContextMenu;
            }
            int    i = (int)cm.GetValue(NeuronIDProperty);
            Neuron n = MainWindow.theNeuronArray.GetNeuron(i);

            if ((string)mi.Header == "Always Fire")
            {
                if (n.FindSynapse(i) == null)
                {
                    n.AddSynapse(i, 1, MainWindow.theNeuronArray, true);
                }
                else
                {
                    n.DeleteSynapse(i);
                }
            }
            if ((string)mi.Header == "Paste Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.PasteNeurons();
                theNeuronArrayView.targetNeuronIndex = -1;
                cmCancelled = true;
            }
            if ((string)mi.Header == "Move Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.MoveNeurons();
                cmCancelled = true;
            }
            if ((string)mi.Header == "Connect to Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.ConnectToHere();
            }
            if ((string)mi.Header == "Connect from Here")
            {
                theNeuronArrayView.targetNeuronIndex = i;
                theNeuronArrayView.ConnectFromHere();
            }
            if ((string)mi.Header == "Select as Target")
            {
                theNeuronArrayView.targetNeuronIndex = i;
            }
        }
 private static void SynapseEntry_MouseDown(object sender, MouseButtonEventArgs e)
 {
     if (sender is TextBlock tb0)
     {
         if (tb0.Parent is StackPanel sp)
         {
             if (sp.Parent is MenuItem mi)
             {
                 if (mi.Parent is ContextMenu cm)
                 {
                     if (tb0.Name == "weight")
                     {
                         int sourceID = (int)cm.GetValue(NeuronIDProperty);
                         if (sp.Children.Count > 1 && sp.Children[1] is TextBlock tb1)
                         {
                             int.TryParse(tb1.Text.Substring(0, 8), out int targetID);
                             if (mi.Header.ToString().Contains("In"))
                             {
                                 int temp = targetID;
                                 targetID = sourceID;
                                 sourceID = temp;
                             }
                             ContextMenu newCm = new ContextMenu();
                             Neuron      n     = MainWindow.theNeuronArray.GetNeuron(sourceID);
                             Synapse     s     = n.FindSynapse(targetID);
                             if (s != null)
                             {
                                 SynapseView.CreateContextMenu(sourceID, s, newCm);
                                 newCm.IsOpen = true;
                                 e.Handled    = true;
                             }
                         }
                     }
                     if (tb0.Name == "neuron")
                     {
                         int.TryParse(tb0.Text.Substring(0, 8), out int targetID);
                         Neuron      n1  = MainWindow.theNeuronArray.GetNeuron(targetID);
                         ContextMenu cm1 = NeuronView.CreateContextMenu(n1.id, n1, new ContextMenu()
                         {
                             IsOpen = true,
                         });
                         MainWindow.arrayView.targetNeuronIndex = targetID;
                         Point loc = dp.pointFromNeuron(targetID);
                         if (loc.X < 0 || loc.X > theCanvas.ActualWidth - cm.ActualWidth ||
                             loc.Y < 0 || loc.Y > theCanvas.ActualHeight - cm.ActualHeight)
                         {
                             MainWindow.arrayView.PanToNeuron(targetID);
                             loc = dp.pointFromNeuron(targetID);
                         }
                         loc.X += dp.NeuronDisplaySize / 2;
                         loc.Y += dp.NeuronDisplaySize / 2;
                         loc    = MainWindow.arrayView.theCanvas.PointToScreen(loc);
                         cm1.PlacementRectangle = new Rect(loc.X, loc.Y, 0, 0);
                         cm1.Placement          = System.Windows.Controls.Primitives.PlacementMode.AbsolutePoint;
                     }
                 }
             }
         }
         e.Handled = true;
     }
 }