示例#1
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);
        }
示例#2
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;
            }
        }
示例#3
0
 private void DragSynapse(int currentNeuron)
 {
     if (mouseDownNeuronIndex > -1 && (currentNeuron != mouseDownNeuronIndex || synapseShape != null))
     {
         if (synapseShape != null)
         {
             theCanvas.Children.Remove(synapseShape);
         }
         Shape l = SynapseView.GetSynapseShape
                       (dp.pointFromNeuron(mouseDownNeuronIndex),
                       dp.pointFromNeuron(currentNeuron),
                       lastSynapseModel
                       );
         l.Stroke = new SolidColorBrush(Utils.RainbowColorFromValue(lastSynapseWeight));
         if (!(l is Ellipse))
         {
             l.Fill = l.Stroke;
         }
         theCanvas.Children.Add(l);
         synapseShape = l;
     }
 }
        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);
            }
        }
        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);
                }
            }
        }
示例#6
0
 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;
     }
 }