Example #1
0
 private void control_MouseDown(object sender, MouseEventArgs e)
 {
     //if an Arc is currently looking for a target
     if (!foundTransitionTarget || !foundPlaceTarget)
     {
         //if an Arc is looking for a Transition as target and a Transition was clicked
         if (!foundTransitionTarget && sender is Transition)
         {
             //check if there already is an Arc with the same input and output
             arcExists = false;
             for (int i = placeSelected.OutputCount() - 1; i >= 0; i--)
             {
                 if (placeSelected.getOutput(i) == sender as Transition)
                 {
                     arcExists = true;
                     break;
                 }
             }
             //if there is no Arc with the same input and output
             if (!arcExists)
             {
                 arcSelected.setOutput(sender as Transition);
                 transitionSelected = sender as Transition;
                 transitionSelected.InputAdd(arcSelected.InputFrom as Place);
                 placeSelected.OutputAdd(sender as Transition);
                 changingSelection        = true;
                 namingBox.Text           = transitionSelected.transitionName.Text;
                 addPlace.Visibility      = Visibility.Visible;
                 addTransition.Visibility = Visibility.Visible;
                 nameBox.Visibility       = Visibility.Visible;
                 numberBox.Visibility     = Visibility.Hidden;
                 addArc.Visibility        = Visibility.Visible;
                 deleteObject.Visibility  = Visibility.Visible;
                 runNet.Visibility        = Visibility.Visible;
                 transitionActive         = true;
                 placeActive = false;
                 arcActive   = false;
             }
         }
         //if an Arc is looking for a Place as target and a Place was clicked
         if (!foundPlaceTarget && sender is Place)
         {
             //check if there already is an Arc with the same input and output
             arcExists = false;
             for (int i = transitionSelected.OutputCount() - 1; i >= 0; i--)
             {
                 if (transitionSelected.getOutput(i) == sender as Place)
                 {
                     arcExists = true;
                     break;
                 }
             }
             //if there is no Arc with the same input and output
             if (!arcExists)
             {
                 arcSelected.setOutput(sender as Place);
                 placeSelected = sender as Place;
                 placeSelected.InputAdd(arcSelected.InputFrom as Transition);
                 transitionSelected.OutputAdd(sender as Place);
                 changingSelection        = true;
                 namingBox.Text           = placeSelected.placeName.Text;
                 changingSelection        = true;
                 NUDTextBox.Text          = placeSelected.numberToken.Text;
                 addPlace.Visibility      = Visibility.Visible;
                 addTransition.Visibility = Visibility.Visible;
                 nameBox.Visibility       = Visibility.Visible;
                 numberBox.Visibility     = Visibility.Visible;
                 addArc.Visibility        = Visibility.Visible;
                 deleteObject.Visibility  = Visibility.Visible;
                 runNet.Visibility        = Visibility.Visible;
                 placeActive      = true;
                 transitionActive = false;
                 arcActive        = false;
             }
         }
     }
     //if an Arc is not currently looking for a target and a Place was clicked
     else if (sender is Place)
     {
         placeSelected           = sender as Place;
         changingSelection       = true;
         namingBox.Text          = placeSelected.placeName.Text;
         changingSelection       = true;
         NUDTextBox.Text         = placeSelected.numberToken.Text;
         nameBox.Visibility      = Visibility.Visible;
         numberBox.Visibility    = Visibility.Visible;
         addArc.Visibility       = Visibility.Visible;
         deleteObject.Visibility = Visibility.Visible;
         placeActive             = true;
         transitionActive        = false;
         arcActive = false;
     }
     //if an Arc is not currently looking for a target and a Transition was clicked
     else if (sender is Transition)
     {
         transitionSelected      = sender as Transition;
         changingSelection       = true;
         namingBox.Text          = transitionSelected.transitionName.Text;
         nameBox.Visibility      = Visibility.Visible;
         numberBox.Visibility    = Visibility.Hidden;
         addArc.Visibility       = Visibility.Visible;
         deleteObject.Visibility = Visibility.Visible;
         transitionActive        = true;
         placeActive             = false;
         arcActive = false;
     }
     //if an Arc is not currently looking for a target and an Arc was clicked
     else if (sender is Line)
     {
         //find the Arc that was clicked
         foreach (Arc arc in AllArcs)
         {
             if (sender as Line == arc.connection)
             {
                 arcSelected = arc;
                 break;
             }
         }
         changingSelection       = true;
         NUDTextBox.Text         = arcSelected.arcWeight.Text;
         nameBox.Visibility      = Visibility.Hidden;
         numberBox.Visibility    = Visibility.Visible;
         addArc.Visibility       = Visibility.Hidden;
         deleteObject.Visibility = Visibility.Visible;
         transitionActive        = false;
         placeActive             = false;
         arcActive = true;
     }
     if (e.LeftButton == MouseButtonState.Pressed)
     {
         //save coordinates of the click
         Point position = Mouse.GetPosition(MainArea);
         MouseOriginX = (int)Convert.ChangeType(position.X, typeof(int));
         MouseOriginY = (int)Convert.ChangeType(position.Y, typeof(int));
         //bring the clicked object into the foreground and save its original coordinates
         if (placeActive)
         {
             Canvas.SetZIndex(placeSelected, 1);
             ControlOriginX = (int)Convert.ChangeType(Canvas.GetLeft(placeSelected), typeof(int));
             ControlOriginY = (int)Convert.ChangeType(Canvas.GetTop(placeSelected), typeof(int));
         }
         else if (transitionActive)
         {
             Canvas.SetZIndex(transitionSelected, 1);
             ControlOriginX = (int)Convert.ChangeType(Canvas.GetLeft(transitionSelected), typeof(int));
             ControlOriginY = (int)Convert.ChangeType(Canvas.GetTop(transitionSelected), typeof(int));
         }
     }
 }
Example #2
0
 private void DeleteControl_Click(object sender, RoutedEventArgs e)
 {
     //check which control is selected and execute the proper delete-commands
     if (placeActive)
     {
         //go through all the Arcs and delete the ones connected to the active Place
         for (int i = AllArcs.Count - 1; i >= 0; i--)
         {
             if (AllArcs[i].InputFrom == placeSelected || AllArcs[i].OutputTo == placeSelected)
             {
                 MainArea.Children.Remove(AllArcs[i]);
                 AllArcs.Remove(AllArcs[i]);
             }
         }
         //go through all the Transitions and remove the connections to the active Place
         for (int i = AllTransitions.Count - 1; i >= 0; i--)
         {
             for (int j = AllTransitions[i].OutputCount() - 1; j >= 0; j--)
             {
                 if (AllTransitions[i].getOutput(j) == placeSelected)
                 {
                     AllTransitions[i].OutputRemove(AllTransitions[i].getOutput(j));
                 }
             }
             for (int j = AllTransitions[i].InputCount() - 1; j >= 0; j--)
             {
                 if (AllTransitions[i].getInput(j) == placeSelected)
                 {
                     AllTransitions[i].InputRemove(AllTransitions[i].getInput(j));
                 }
             }
         }
         MainArea.Children.Remove(placeSelected);
         AllPlaces.Remove(placeSelected);
         placeActive = false;
     }
     else if (transitionActive)
     {
         //go through all the Arcs and delete the ones connected to the active Transition
         for (int i = AllArcs.Count - 1; i >= 0; i--)
         {
             if (AllArcs[i].InputFrom == transitionSelected || AllArcs[i].OutputTo == transitionSelected)
             {
                 MainArea.Children.Remove(AllArcs[i]);
                 AllArcs.Remove(AllArcs[i]);
             }
         }
         //go through all the Places and remove the connections to the active Transition
         for (int i = AllPlaces.Count - 1; i >= 0; i--)
         {
             for (int j = AllPlaces[i].OutputCount() - 1; j >= 0; j--)
             {
                 if (AllPlaces[i].getOutput(j) == transitionSelected)
                 {
                     AllPlaces[i].OutputRemove(AllPlaces[i].getOutput(j));
                 }
             }
             for (int j = AllPlaces[i].InputCount() - 1; j >= 0; j--)
             {
                 if (AllPlaces[i].getInput(j) == transitionSelected)
                 {
                     AllPlaces[i].InputRemove(AllPlaces[i].getInput(j));
                 }
             }
         }
         MainArea.Children.Remove(transitionSelected);
         AllTransitions.Remove(transitionSelected);
         transitionActive = false;
     }
     else if (arcActive)
     {
         //in the controls the Arc is connected to, remove the connection
         if (arcSelected.InputFrom is Place)
         {
             placeSelected = arcSelected.InputFrom as Place;
             if (!(arcSelected.OutputTo == null))
             {
                 transitionSelected = arcSelected.OutputTo as Transition;
                 for (int i = transitionSelected.InputCount() - 1; i >= 0; i--)
                 {
                     if (transitionSelected.getInput(i) == arcSelected.InputFrom)
                     {
                         transitionSelected.InputRemove(transitionSelected.getInput(i));
                     }
                 }
             }
             for (int i = placeSelected.OutputCount() - 1; i >= 0; i--)
             {
                 if (placeSelected.getOutput(i) == arcSelected.OutputTo)
                 {
                     placeSelected.OutputRemove(placeSelected.getOutput(i));
                 }
             }
             foundTransitionTarget = true;
         }
         else if (arcSelected.InputFrom is Transition)
         {
             transitionSelected = arcSelected.InputFrom as Transition;
             if (!(arcSelected.OutputTo == null))
             {
                 placeSelected = arcSelected.OutputTo as Place;
                 for (int i = placeSelected.InputCount() - 1; i >= 0; i--)
                 {
                     if (placeSelected.getInput(i) == arcSelected.InputFrom)
                     {
                         placeSelected.InputRemove(placeSelected.getInput(i));
                     }
                 }
             }
             for (int i = transitionSelected.InputCount() - 1; i >= 0; i--)
             {
                 if (transitionSelected.getOutput(i) == arcSelected.OutputTo)
                 {
                     transitionSelected.OutputRemove(transitionSelected.getOutput(i));
                 }
             }
             foundPlaceTarget = true;
         }
         MainArea.Children.Remove(arcSelected);
         AllArcs.Remove(arcSelected);
         arcActive = false;
     }
     addPlace.Visibility      = Visibility.Visible;
     addTransition.Visibility = Visibility.Visible;
     addArc.Visibility        = Visibility.Hidden;
     deleteObject.Visibility  = Visibility.Hidden;
     nameBox.Visibility       = Visibility.Hidden;
     numberBox.Visibility     = Visibility.Hidden;
 }