//mouse down in a neuron...it's firing OR the beginning of a synapse drag private Neuron NeuronMouseDown(Neuron n, int clickCount) { if (clickCount == 2) { //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(); currentOperation = CurrentOperation.draggingSynapse; targetNeuronIndex = mouseDownNeuronIndex; return(n); }
private void UndoSynapse() { if (synapseUndoInfo.Count == 0) { return; } SynapseUndo s = synapseUndoInfo.Last(); synapseUndoInfo.RemoveAt(synapseUndoInfo.Count - 1); Neuron n = GetNeuron(s.source); if (s.newSynapse) //the synapse was added so delete it { n.DeleteSynapse(s.target); } else if (s.delSynapse) //the synapse was deleted so add it back { n.AddSynapse(s.target, s.weight, s.model); } else //weight/type changed { n.AddSynapse(s.target, s.weight, s.model); } n.Update(); }
public void DeleteSelection(bool deleteBoundarySynapses = true) { MainWindow.theNeuronArray.SetUndoPoint(); List <int> neuronsToDelete = theSelection.EnumSelectedNeurons(); foreach (int nID in neuronsToDelete) { Neuron n = MainWindow.theNeuronArray.GetNeuron(nID); if (deleteBoundarySynapses) { foreach (Synapse s in n.synapsesFrom) { Neuron source = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron); if (source != null && theSelection.NeuronInSelection(source.id) == 0) { source.DeleteSynapseWithUndo(n.id); } } } for (int i = 0; i < n.synapses.Count; i++) { n.DeleteSynapseWithUndo(n.synapses[i].targetNeuron); } n.AddUndoInfo(); n.CurrentCharge = 0; n.LastCharge = 0; n.Model = Neuron.modelType.IF; n.Label = ""; n.Update(); } DeleteModulesInSelection(); Update(); }
private void UndoNeuron() { if (neuronUndoInfo.Count == 0) { return; } NeuronUndo n = neuronUndoInfo.Last(); neuronUndoInfo.RemoveAt(neuronUndoInfo.Count - 1); Neuron n1 = n.previousNeuron.Copy(); if (n1.Label != "") { RemoveLabelFromCache(n1.Id); } if (n1.label != "") { AddLabelToCache(n1.Id, n1.label); } if (n.neuronIsShowingSynapses) { MainWindow.arrayView.AddShowSynapses(n1.id); } n1.Update(); }
private void NeuronMouseUp(MouseButtonEventArgs e) { if (mouseDownNeuronIndex == -1) { return; } Neuron n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex); if (n != null) { if (n.Model == Neuron.modelType.Random || n.model == Neuron.modelType.Always) { if (n.LeakRate < 0) { n.LeakRate = -n.LeakRate; } else { n.CurrentCharge = 0; n.LastCharge = 0; n.LeakRate = -n.LeakRate; } } else if (n.Model != Neuron.modelType.Color) { if (n.LastCharge < .99) { n.CurrentCharge = 1; n.LastCharge = 1; } else { n.CurrentCharge = 0; n.LastCharge = 0; } } else { if (n.LastChargeInt == 0) { n.LastChargeInt = 0xffffff; } else { n.LastChargeInt = 0; } } n.Update(); e.Handled = true; Update(); } }
//get here if the user moved the slider for an Always firing neuron private static void Sl_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { Slider sl = sender as Slider; StackPanel sp = sl.Parent as StackPanel; MenuItem mi = sp.Parent as MenuItem; ContextMenu cm = mi.Parent as ContextMenu; int neuronID = (int)cm.GetValue(NeuronIDProperty); Control cc = Utils.FindByName(cm, "AxonDelay"); if (cc is ComboBox tb3) { int.TryParse(tb3.Text, out int axonDelay); int newDelay = (int)(axonDelay * (float)sl.Value / 10.0f); Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronID); n.AxonDelay = newDelay; n.Update(); } }
public void DeleteSelection(bool deleteSynapses = true) { List <int> neuronsToDelete = theSelection.EnumSelectedNeurons(); foreach (int nID in neuronsToDelete) { Neuron n = MainWindow.theNeuronArray.GetNeuron(nID); n.CurrentCharge = 0; n.LastCharge = 0; n.Model = Neuron.modelType.Std; if (deleteSynapses) { n.DeleteAllSynapes(); } n.Label = ""; n.Update(); } DeleteModulesInSelection(); Update(); }
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_MouseUp(object sender, MouseButtonEventArgs e) { if (mouseRepeatTimer != null) { mouseRepeatTimer.Stop(); } if (MainWindow.theNeuronArray == null) { return; } //Debug.WriteLine("theCanvas_MouseUp" + MainWindow.theNeuronArray.Generation); if (e.ChangedButton == MouseButton.Right) { return; } Point currentPosition = e.GetPosition(theCanvas); LimitMousePostion(ref currentPosition); int mouseUpNeuronIndex = dp.NeuronFromPoint(currentPosition); dragging = false; if (theCanvas.Cursor == Cursors.Cross) { FinishSelection(); Update(); } //we clicked on a neuron...is it a fire or a synapse drag? if (theCanvas.Cursor == Cursors.UpArrow) { if (synapseShape == null && mouseDownNeuronIndex > -1) //if no synapseshape exists, then were just clicking in a nueron { Neuron n = MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex); if (n != null) { if (n.Model == Neuron.modelType.Random || n.model == Neuron.modelType.Always) { if (n.LeakRate < 0) { n.LeakRate = 0; } else { n.LeakRate = -1; } } else if (n.Model != Neuron.modelType.Color) { if (n.LastCharge < .99) { n.CurrentCharge = 1; n.LastCharge = 1; } else { n.CurrentCharge = 0; n.LastCharge = 0; } } else { if (n.LastChargeInt == 0) { n.LastChargeInt = 0xffffff; } else { n.LastChargeInt = 0; } } n.Update(); e.Handled = true; Update(); } } else { if (mouseDownNeuronIndex > -1) { Point p1 = e.GetPosition(theCanvas); LimitMousePostion(ref p1); int index = dp.NeuronFromPoint(p1); MainWindow.theNeuronArray.SetUndoPoint(); MainWindow.arrayView.AddShowSynapses(mouseDownNeuronIndex); MainWindow.theNeuronArray.GetNeuron(mouseDownNeuronIndex).AddSynapseWithUndo(index, lastSynapseWeight, lastSynapseModel); } synapseShape = null; mouseDownNeuronIndex = -1; e.Handled = true; Update(); } } if (theCanvas.Cursor == Cursors.Hand) { FinishPan(); e.Handled = true; } mouseDownNeuronIndex = -1; dragging = false; Mouse.Capture(null); }
private static void SetValueInSelectedNeurons(Neuron n, string property) { bool neuronInSelection = NeuronInSelection(n.id); if (neuronInSelection) { List <int> theNeurons = theNeuronArrayView.theSelection.EnumSelectedNeurons(); //special case for label because they are auto-incremented, //clear all the labels first to avoid collisions if (property == "label") { for (int i = 0; i < theNeurons.Count; i++) { Neuron n1 = MainWindow.theNeuronArray.GetNeuron(theNeurons[i]); if (n1.id != n.id) { n1.label = ""; n1.Update(); } } } for (int i = 0; i < theNeurons.Count; i++) { Neuron n1 = MainWindow.theNeuronArray.GetNeuron(theNeurons[i]); n1.AddUndoInfo(); switch (property) { case "currentCharge": if (n.model == Neuron.modelType.Color) { n1.SetValueInt(n.LastChargeInt); } else { n1.currentCharge = n.currentCharge; n1.lastCharge = n.currentCharge; } break; case "leakRate": n1.leakRate = n.leakRate; break; case "axonDelay": n1.axonDelay = n.axonDelay; break; case "model": n1.model = n.model; break; case "enable": n1.leakRate = n.leakRate; break; case "history": if (FiringHistory.NeuronIsInFiringHistory(n.id)) { FiringHistory.AddNeuronToHistoryWindow(n1.id); OpenHistoryWindow(); } else { FiringHistory.RemoveNeuronFromHistoryWindow(n1.id); } break; case "synapses": if (MainWindow.arrayView.IsShowingSnapses(n.id)) { MainWindow.arrayView.AddShowSynapses(n1.id); } else { MainWindow.arrayView.RemoveShowSynapses(n1.id); } break; case "label": if (n.label == "") { n1.label = ""; } else if (n.id != n1.id) { string newLabel = n.label; while (MainWindow.theNeuronArray.GetNeuron(newLabel) != null) { int num = 0; int digitCount = 0; while (Char.IsDigit(newLabel[newLabel.Length - 1])) { int.TryParse(newLabel[newLabel.Length - 1].ToString(), out int digit); num = num + (int)Math.Pow(10, digitCount) * digit; digitCount++; newLabel = newLabel.Substring(0, newLabel.Length - 1); } num++; newLabel = newLabel + num.ToString(); } n1.label = newLabel; } break; } n1.Update(); } } }
private static void Cm_Closed(object sender, RoutedEventArgs e) { if (sender is ContextMenu cm) { if (!cm.IsOpen) { return; } cm.IsOpen = false; if (cmCancelled) { MainWindow.Update(); return; } MainWindow.theNeuronArray.SetUndoPoint(); int neuronID = (int)cm.GetValue(NeuronIDProperty); Neuron n = MainWindow.theNeuronArray.GetNeuron(neuronID); n.AddUndoInfo(); bool applyToAll = false; Control cc = Utils.FindByName(cm, "ApplyToSelection"); if (cc is CheckBox cb) { if (cb.IsChecked == true) { applyToAll = true; } } cc = Utils.FindByName(cm, "Label"); if (cc is TextBox tb) { string newLabel = tb.Text; if (int.TryParse(newLabel, out int dummy)) { newLabel = "_" + newLabel; } if (labelChanged) { MainWindow.theNeuronArray.SetUndoPoint(); n.Label = newLabel; if (applyToAll) { SetValueInSelectedNeurons(n, "label"); } } } cc = Utils.FindByName(cm, "Model"); if (cc is ComboBox cb0) { ListBoxItem lbi = (ListBoxItem)cb0.SelectedItem; Neuron.modelType nm = (Neuron.modelType)System.Enum.Parse(typeof(Neuron.modelType), lbi.Content.ToString()); if (modelChanged) { n.model = nm; if (applyToAll) { SetValueInSelectedNeurons(n, "model"); } } } cc = Utils.FindByName(cm, "CurrentCharge"); if (cc is ComboBox tb1) { if (n.model == Neuron.modelType.Color) { try { uint newCharge = Convert.ToUInt32(tb1.Text, 16); if (chargeChanged) { n.SetValueInt((int)newCharge); n.lastCharge = newCharge; if (applyToAll) { SetValueInSelectedNeurons(n, "currentCharge"); } Utils.AddToValues(newCharge, colorValues); } } catch { }; } else { float.TryParse(tb1.Text, out float newCharge); if (chargeChanged) { n.SetValue(newCharge); n.lastCharge = newCharge; if (applyToAll) { SetValueInSelectedNeurons(n, "currentCharge"); } Utils.AddToValues(newCharge, currentChargeValues); } } } cc = Utils.FindByName(cm, "LeakRate"); if (cc is ComboBox tb2) { float.TryParse(tb2.Text, out float leakRate); if (leakRateChanged) { n.LeakRate = leakRate; if (applyToAll) { SetValueInSelectedNeurons(n, "leakRate"); } if (n.model == Neuron.modelType.LIF) { Utils.AddToValues(leakRate, leakRateValues); } if (n.model == Neuron.modelType.Random) { Utils.AddToValues(leakRate, stdDevValues); } if (n.model == Neuron.modelType.Burst) { Utils.AddToValues(leakRate, axonDelayValues); } } } else { n.leakRate = 0; } cc = Utils.FindByName(cm, "AxonDelay"); if (cc is ComboBox tb3) { int.TryParse(tb3.Text, out int axonDelay); if (axonDelayChanged) { n.axonDelay = axonDelay; if (applyToAll) { SetValueInSelectedNeurons(n, "axonDelay"); } if (n.model == Neuron.modelType.Random) { Utils.AddToValues(axonDelay, meanValues); } else if (n.model == Neuron.modelType.Always) { Utils.AddToValues(axonDelay, alwaysDelayValues); } else if (n.model == Neuron.modelType.Burst) { Utils.AddToValues(axonDelay, alwaysDelayValues); } else { Utils.AddToValues(axonDelay, axonDelayValues); } } } cc = Utils.FindByName(cm, "Synapses"); if (cc is CheckBox cb2) { if (synapsesChanged) { if (cb2.IsChecked == true) { MainWindow.arrayView.AddShowSynapses(n.id); } else { MainWindow.arrayView.RemoveShowSynapses(n.id); } if (applyToAll) { SetValueInSelectedNeurons(n, "synapses"); } } } cc = Utils.FindByName(cm, "Enabled"); if (cc is CheckBox cb1) { if (enabledChanged) { if (cb1.IsChecked == true) { n.leakRate = Math.Abs(n.leakRate); } else { n.leakRate = Math.Abs(n.leakRate) * -1.0f; } if (applyToAll) { SetValueInSelectedNeurons(n, "enable"); } } } cc = Utils.FindByName(cm, "History"); if (cc is CheckBox cb3) { if (historyChanged) { if (cb3.IsChecked == true) { FiringHistory.AddNeuronToHistoryWindow(n.id); OpenHistoryWindow(); } else { FiringHistory.RemoveNeuronFromHistoryWindow(n.id); } if (applyToAll) { SetValueInSelectedNeurons(n, "history"); } } } n.Update(); } MainWindow.Update(); }