[171] | 1 | using System; |
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | using System.ComponentModel; |
---|
| 4 | using System.Data; |
---|
| 5 | using System.Drawing; |
---|
| 6 | using System.Linq; |
---|
| 7 | using System.Text; |
---|
| 8 | using System.Windows.Forms; |
---|
| 9 | using System.IO; |
---|
| 10 | |
---|
| 11 | namespace VisualBCM |
---|
| 12 | { |
---|
| 13 | public partial class StimulusForm : Form |
---|
| 14 | { |
---|
| 15 | BCMToolbox.StimulatorBase __stim = null; |
---|
| 16 | public StimulusForm(BCMToolbox.StimulatorBase stim) |
---|
| 17 | { |
---|
| 18 | InitializeComponent(); |
---|
| 19 | |
---|
| 20 | __stim = stim; |
---|
| 21 | |
---|
| 22 | // prepare stimulus screen |
---|
| 23 | string[] props = __stim.GetParameters(); |
---|
| 24 | StringBuilder rtext = new StringBuilder(); |
---|
| 25 | foreach (string prop in props) |
---|
| 26 | { |
---|
| 27 | rtext.AppendFormat("{0} = {1}", prop, BCMToolbox.StimulatorBase.STIMULUS_NULL); |
---|
| 28 | rtext.AppendLine(""); |
---|
| 29 | } |
---|
| 30 | this.richTextBox1.Text = rtext.ToString(); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public BCMToolbox.StimulatorBase Stimulator |
---|
| 34 | { |
---|
| 35 | get |
---|
| 36 | { |
---|
| 37 | return __stim; |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | public event EventHandler ApplyStimulus; |
---|
| 43 | |
---|
| 44 | void SetParameters() |
---|
| 45 | { |
---|
| 46 | Dictionary<string, object> stimParameters = new Dictionary<string, object>(); |
---|
| 47 | foreach (string line in this.richTextBox1.Lines) |
---|
| 48 | { |
---|
| 49 | if (!line.Contains('=')) |
---|
| 50 | continue; |
---|
| 51 | |
---|
| 52 | string[] linetokens = line.Split('='); |
---|
| 53 | string key = linetokens[0].Trim(); |
---|
| 54 | string value = linetokens[1].Trim(); |
---|
| 55 | object parsedValue = null; |
---|
| 56 | |
---|
| 57 | if (value.Contains(';')) // this is an array of int |
---|
| 58 | { |
---|
| 59 | string[] arrTokens = value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); |
---|
| 60 | List<int> pValue = new List<int>(); |
---|
| 61 | foreach (string tok in arrTokens) |
---|
| 62 | pValue.Add(Int32.Parse(tok)); |
---|
| 63 | parsedValue = pValue.ToArray(); |
---|
| 64 | } |
---|
| 65 | else if (value.Contains('.')) // this is a double |
---|
| 66 | parsedValue = Double.Parse(value); |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | try |
---|
| 70 | { |
---|
| 71 | parsedValue = Int32.Parse(value); |
---|
| 72 | } |
---|
| 73 | catch |
---|
| 74 | { |
---|
| 75 | if (value != "null") |
---|
| 76 | parsedValue = value; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | stimParameters.Add(key, parsedValue); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | __stim.SetParameters(stimParameters); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | private void button1_Click(object sender, EventArgs e) |
---|
| 87 | { |
---|
| 88 | SetParameters(); |
---|
| 89 | if (this.ApplyStimulus != null) |
---|
| 90 | this.ApplyStimulus(this, EventArgs.Empty); |
---|
| 91 | this.Close(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | private void button2_Click(object sender, EventArgs e) |
---|
| 95 | { |
---|
| 96 | this.Close(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | private void saveToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 100 | { |
---|
| 101 | if (this.saveFileDialog1.ShowDialog() != DialogResult.OK) |
---|
| 102 | return; |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName, false, Encoding.UTF8)) |
---|
| 106 | { |
---|
| 107 | foreach (string line in richTextBox1.Lines) |
---|
| 108 | sw.WriteLine(line); |
---|
| 109 | sw.Close(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | System.Media.SystemSounds.Beep.Play(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | private void loadToolStripMenuItem_Click(object sender, EventArgs e) |
---|
| 116 | { |
---|
| 117 | if (this.openFileDialog1.ShowDialog() != DialogResult.OK) |
---|
| 118 | return; |
---|
| 119 | |
---|
| 120 | using (StreamReader sr = new StreamReader(this.openFileDialog1.FileName, Encoding.UTF8)) |
---|
| 121 | { |
---|
| 122 | this.richTextBox1.Text = sr.ReadToEnd(); |
---|
| 123 | sr.Close(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | System.Media.SystemSounds.Beep.Play(); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|