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(); }