示例#1
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_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);
                }
            }
        }