BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
Recordings.cs
Go to the documentation of this file.
1using Newtonsoft.Json;
2using System;
3using System.Drawing;
4using System.IO;
5using System.Linq;
6using System.Windows.Forms;
7
8namespace BioImager
9{
10 public partial class Recordings : Form
11 {
12 public Recordings()
13 {
14 InitializeComponent();
15 if (!Directory.Exists("Recordings"))
16 Directory.CreateDirectory("Recordings");
17 foreach (string file in Directory.GetFiles("Recordings"))
18 {
19 if (file.EndsWith("reco"))
20 OpenRecording(file);
21 else if (file.EndsWith("pro"))
22 OpenProperty(file);
23 }
24 foreach (Automation.Action.ValueType val in (Automation.Action.ValueType[])Enum.GetValues(typeof(Automation.Action.ValueType)))
25 {
26 propBox.Items.Add(val);
27 }
28
29 }
30 public void InitElements()
31 {
32 view.Nodes.Clear();
33 foreach (Automation.Recording rec in Automation.Recordings.Values)
34 {
35 TreeNode tr = new TreeNode();
36 Node n = new Node(rec, tr, Node.Type.recording);
37 tr.Tag = n;
38 tr.Text = rec.Name;
39 foreach (Automation.Action item in rec.List)
40 {
41 try
42 {
43 TreeNode tn = new TreeNode();
44 tn.Text = item.ToString();
45 Node no = new Node(item, tn, Node.Type.action);
46 no.recording = rec;
47 tn.Tag = no;
48 tr.Nodes.Add(tn);
49 }
50 catch (Exception)
51 {
52
53 }
54 }
55 view.Nodes.Add(tr);
56 }
57
58 propView.Nodes.Clear();
59 foreach (Automation.Recording rec in Automation.Properties.Values)
60 {
61 TreeNode tr = new TreeNode();
62 Node n = new Node(rec, tr, Node.Type.recording);
63 tr.Tag = n;
64 tr.Text = rec.Name;
65 foreach (Automation.Action item in rec.List)
66 {
67 try
68 {
69 TreeNode tn = new TreeNode();
70 tn.Text = item.ToString();
71
72 Node no = new Node(item, tn, Node.Type.action);
73 no.recording = rec;
74 tn.Tag = no;
75 tr.Nodes.Add(tn);
76 }
77 catch (Exception)
78 {
79
80 }
81 }
82 propView.Nodes.Add(tr);
83 }
84 }
85 public void UpdateElements()
86 {
87 foreach (TreeNode rec in view.Nodes)
88 {
89 rec.Nodes.Clear();
90 Node n = (Node)rec.Tag;
91 foreach (Automation.Action item in n.recording.List)
92 {
93 try
94 {
95 TreeNode tr = new TreeNode();
96 tr.Text = item.ToString();
97 Node no = new Node(item, rec, Node.Type.action);
98 no.recording = n.recording;
99 tr.Tag = no;
100 rec.Nodes.Add(tr);
101 }
102 catch (Exception)
103 {
104
105 }
106 }
107 }
108 foreach (TreeNode rec in propView.Nodes)
109 {
110 rec.Nodes.Clear();
111 Node n = (Node)rec.Tag;
112 foreach (Automation.Action item in n.recording.List)
113 {
114 try
115 {
116 TreeNode tr = new TreeNode();
117 tr.Text = item.ToString();
118 Node no = new Node(item, rec, Node.Type.action);
119 no.recording = n.recording;
120 tr.Tag = no;
121 rec.Nodes.Add(tr);
122 }
123 catch (Exception)
124 {
125
126 }
127 }
128 }
129 }
130 public class Node
131 {
134 public Type type;
135 public enum Type
136 {
137 recording,
138 action
139 }
140
141 public TreeNode node;
142 public Node(Automation.Action el, TreeNode n, Type t)
143 {
144 action = el;
145 node = n;
146 type = t;
148 recording.List.Add(el);
149 //items = Automation.AutomationHelpers.GetAllChildren(el.element);
150 }
151 public Node(Automation.Recording rec, TreeNode n, Type t)
152 {
153 recording = rec;
154 node = n;
155 type = t;
156 //items = Automation.AutomationHelpers.GetAllChildren(el.element);
157 }
158 public override string ToString()
159 {
160 return action.Name + ", " + action.AutomationID + ", " + action.ClassName.ToString() + ", " + action.ActionType;
161 }
162 }
163
164 public static void Perform(string act)
165 {
167 rec.Run();
168 Recorder.AddLine("Recordings.Perform(" + rec.Name + ");");
169 }
170 public static object GetProperty(Automation.Action.ValueType automation, string pro)
171 {
172 Automation.Recording rec = null;
173 if (Automation.Properties.ContainsKey(pro))
175 else
176 return null;
177 Recorder.AddLine("Recordings.Get(" + rec.Name + ");");
178 return rec.Get();
179 }
180
181 private void view_AfterSelect(object sender, TreeViewEventArgs e)
182 {
183
184 }
185
186 private void startBut_Click(object sender, EventArgs e)
187 {
188 TextInput ti = new TextInput("Property" + propBox.Items.Count);
189 ti.Text = "Set Property Name";
190 if (ti.ShowDialog() != DialogResult.OK)
191 return;
192 recordStatusLabel.Text = "Recording: Started";
193 if (Automation.IsRecording)
194 return;
195 Automation.StartRecording();
196 }
197
198 private void stopBut_Click(object sender, EventArgs e)
199 {
200 recordStatusLabel.Text = "Recording: Stopped";
201 if (!Automation.IsRecording)
202 return;
203 Automation.StopRecording();
204 if (Automation.Recordings.Count > 0)
205 {
206 //actions = Automation.Recordings[0].List;
207 InitElements();
208 }
209 }
210
211 private void playBut_Click(object sender, EventArgs e)
212 {
213 if (view.SelectedNode == null)
214 return;
215 Node n = (Node)view.SelectedNode.Tag;
216 if (n.type == Node.Type.action)
217 return; ;
218 n.recording.Run();
219 }
220
221 private void view_BeforeExpand(object sender, TreeViewCancelEventArgs e)
222 {
223
224 }
225
226 private void Elements_FormClosing(object sender, FormClosingEventArgs e)
227 {
228 this.Hide();
229 e.Cancel = true;
230 }
231 private void performToolStripMenuItem_Click(object sender, EventArgs e)
232 {
233 TreeNode tr = view.SelectedNode;
234 Node no = (Node)tr.Tag;
235 if (no.type == Node.Type.action)
236 no.action.Perform();
237 else
238 no.recording.Run();
239 }
240 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
241 {
242 if (saveRecDialog.ShowDialog() != DialogResult.OK)
243 return;
244 SaveRecording(saveRecDialog.FileName);
245 }
246 private void SaveRecording(string file)
247 {
248 Automation.Recording n;
249 if (view.SelectedNode == null)
250 return;
251 else
252 {
253 n = ((Node)view.SelectedNode.Tag).recording;
254 }
255 string s = Path.GetFileNameWithoutExtension(saveRecDialog.FileName);
256 n.Name = s;
257 string j = JsonConvert.SerializeObject(n.List, Formatting.None);
258 File.WriteAllText(file, j);
259 InitElements();
260 }
261 private void SaveProperty(string file)
262 {
263 Automation.Recording n;
264 if (propView.SelectedNode == null)
265 return;
266 else
267 {
268 n = ((Node)propView.SelectedNode.Tag).recording;
269 }
270 string s = Path.GetFileNameWithoutExtension(savePropDialog.FileName);
271 n.Name = s;
272
273 string j = JsonConvert.SerializeObject(n.List, Formatting.None);
274 File.WriteAllText(file, j);
275 InitElements();
276 }
277 private Automation.Recording OpenRec(string file)
278 {
279 string st = File.ReadAllText(file);
280 Automation.Recording rec = new Automation.Recording();
281 rec.Name = Path.GetFileNameWithoutExtension(file);
282 rec.File = file;
283 Newtonsoft.Json.Linq.JArray ar = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(st);
284 for (int i = 0; i < ar.Count; i++)
285 {
286 Automation.Action.Type t = (Automation.Action.Type)Enum.Parse(typeof(Automation.Action.Type), ar[i].ElementAt(0).First.ToString());
287 if (t == Automation.Action.Type.keydown || t == Automation.Action.Type.keyup)
288 {
289 Keys k = (Keys)int.Parse(ar[i].ElementAt(2).First.ToString());
290 KeyEventArgs kea = new KeyEventArgs(k);
291 int ind = int.Parse(ar[i].ElementAt(9).First.ToString());
292 Automation.Action ac = new Automation.Action(t, ar[i].ElementAt(8).First.ToString(), ar[i].ElementAt(9).First.ToString(), ind, ar[i].ElementAt(3).First.ToString(), ar[i].ElementAt(4).First.ToString(), ar[i].ElementAt(5).First.ToString(), kea);
293 rec.List.Add(ac);
294 }
295 if (t == Automation.Action.Type.mousedown || t == Automation.Action.Type.mouseup)
296 {
297 MouseButtons mb = (MouseButtons)int.Parse(ar[i].ElementAt(2).First.ToString());
298 string ps = ar[i].ElementAt(3).First.ToString();
299 string xs = ps.Substring(0, ps.IndexOf(','));
300 string ys = ps.Substring(ps.IndexOf(',') + 1, ps.Length - (ps.IndexOf(',') + 1));
301 int ind = int.Parse(ar[i].ElementAt(9).First.ToString());
302 System.Drawing.Point po = new Point(int.Parse(xs), int.Parse(ys));
303 MouseEventArgs mo = new MouseEventArgs(mb, 1, po.X, po.Y, 0);
304 Automation.Action ac = new Automation.Action(t, ar[i].ElementAt(7).First.ToString(), ar[i].ElementAt(8).First.ToString(), ind, ar[i].ElementAt(4).First.ToString(), ar[i].ElementAt(5).First.ToString(), ar[i].ElementAt(6).First.ToString(), mo);
305 rec.List.Add(ac);
306 }
307 }
308 return rec;
309 }
310 public void OpenRecording(string file)
311 {
312 Automation.Recording rec = OpenRec(file);
313 Automation.Recordings.Add(rec.Name, rec);
314 InitElements();
315 }
316 public void OpenProperty(string file)
317 {
318 Automation.Recording rec = OpenRec(file);
319 Automation.Properties.Add(rec.Name, rec);
320 InitElements();
321 }
322
323 private void openToolStripMenuItem_Click(object sender, EventArgs e)
324 {
325 openRecDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
326 if (openRecDialog.ShowDialog() != DialogResult.OK)
327 return;
328 OpenRecording(openRecDialog.FileName);
329 }
330
331 private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
332 {
333 Node n = (Node)view.SelectedNode.Tag;
334 if (n.type == Node.Type.recording)
335 {
336 Automation.Recordings.Remove(n.recording.Name);
337 File.Delete(n.recording.File);
338 InitElements();
339 }
340 else if (n.type == Node.Type.action)
341 {
342 n.recording.List.Remove(n.action);
344 }
345 }
346
347 private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
348 {
349 InitElements();
350 }
351
352 private void startPropBut_Click(object sender, EventArgs e)
353 {
354 TextInput ti = new TextInput("Property" + propBox.Items.Count);
355 ti.Text = "Set Property Name";
356 ti.TopMost = true;
357 if (ti.ShowDialog() != DialogResult.OK)
358 return;
359 propRecStatusLabel.Text = "Property Recording: Started";
360 Automation.StartPropertyRecording(ti.TextValue);
361 }
362
363 private void stopPropBut_Click(object sender, EventArgs e)
364 {
365 propRecStatusLabel.Text = "Property Recording: Stopped";
366 if (!Automation.IsRecording)
367 return;
368 Automation.StopPropertyRecording();
369 if (Automation.Properties.Count > 0)
370 {
371 //actions = Automation.Recordings[0].List;
372 InitElements();
373 }
374 }
375
376 private void getPropBut_Click(object sender, EventArgs e)
377 {
378 if (propView.SelectedNode == null)
379 return;
380 Node n = (Node)propView.SelectedNode.Tag;
381 if (n.type == Node.Type.action)
382 return;
383 if (n.recording.List.Last().Value == Automation.Action.ValueType.Image)
384 Clipboard.SetImage((Bitmap)Automation.GetProperty(n.recording.Name));
385 else
386 {
387 if (n.recording.List.Last().Value == Automation.Action.ValueType.TogglePattern)
388 {
389 bool b = (bool)Automation.GetProperty(n.recording.Name);
390 MessageBox.Show(b.ToString());
391 }
392 else
393 {
394 string s = (string)Automation.GetProperty(n.recording.Name);
395 MessageBox.Show(s);
396 }
397 }
398 }
399 private void setPropBut_Click(object sender, EventArgs e)
400 {
401 TextInput ti = new TextInput("Property" + propBox.Items.Count);
402 ti.Text = "Set Text To Set";
403 ti.TopMost = true;
404 if (propView.SelectedNode == null)
405 return;
406 Node n = (Node)propView.SelectedNode.Tag;
407 if (n.type == Node.Type.action)
408 return;
409 Automation.SetProperty(n.recording.Name, ti.TextValue);
410 }
411 private void saveSelectedValueToolStripMenuItem_Click(object sender, EventArgs e)
412 {
413 savePropDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
414 if (savePropDialog.ShowDialog() != DialogResult.OK)
415 return;
416 SaveProperty(savePropDialog.FileName);
417 }
418
419 private void openPropertyToolStripMenuItem_Click(object sender, EventArgs e)
420 {
421 openPropDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
422 if (openPropDialog.ShowDialog() != DialogResult.OK)
423 return;
424 OpenProperty(openPropDialog.FileName);
425 }
426
427 private void getMenuItem_Click(object sender, EventArgs e)
428 {
429 getPropBut.PerformClick();
430 }
431
432 private void deletePropMenuItem_Click(object sender, EventArgs e)
433 {
434 Node n = (Node)propView.SelectedNode.Tag;
435 if (n.type == Node.Type.recording)
436 {
437 Automation.Properties.Remove(n.recording.Name);
438 if (n.recording.File != null)
439 File.Delete(n.recording.File);
440 InitElements();
441 }
442 else if (n.type == Node.Type.action)
443 {
444 n.recording.List.Remove(n.action);
446 }
447 }
448
449
450 private void propView_AfterSelect(object sender, TreeViewEventArgs e)
451 {
452 if (propView.SelectedNode == null)
453 return;
454 Node n = (Node)propView.SelectedNode.Tag;
455 if (n.type == Node.Type.recording)
456 return;
457 propBox.SelectedIndex = (int)n.action.Value;
458 }
459
460 private void propBox_SelectedIndexChanged(object sender, EventArgs e)
461 {
462 if (propView.SelectedNode == null)
463 return;
464 Node n = (Node)propView.SelectedNode.Tag;
465 if (n.type == Node.Type.recording)
466 return;
467 n.action.Value = (Automation.Action.ValueType)propBox.SelectedItem;
468 }
469
470 private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
471 {
472 if (view.SelectedNode == null)
473 return;
474 Node n = (Node)view.SelectedNode.Tag;
475 if (n.type == Node.Type.recording)
476 return;
477 int oldindex = n.recording.List.IndexOf(n.action);
478 n.recording.List.RemoveAt(oldindex);
479 int newindex = oldindex - 1;
480 if (newindex > oldindex) newindex--;
481 // the actual index could have shifted due to the removal
482 n.recording.List.Insert(newindex, n.action);
484 }
485
486 private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
487 {
488 if (view.SelectedNode == null)
489 return;
490 Node n = (Node)view.SelectedNode.Tag;
491 if (n.type == Node.Type.recording)
492 return;
493 int oldindex = n.recording.List.IndexOf(n.action);
494 n.recording.List.RemoveAt(oldindex);
495 int newindex = oldindex - 1;
496 // the actual index could have shifted due to the removal
497 n.recording.List.Insert(newindex, n.action);
499 }
500
501 private void moveUpToolStripMenuItem1_Click(object sender, EventArgs e)
502 {
503 if (propView.SelectedNode == null)
504 return;
505 Node n = (Node)propView.SelectedNode.Tag;
506 if (n.type == Node.Type.recording)
507 return;
508 int oldindex = n.recording.List.IndexOf(n.action);
509 n.recording.List.RemoveAt(oldindex);
510 int newindex = oldindex - 1;
511 // the actual index could have shifted due to the removal
512 n.recording.List.Insert(newindex, n.action);
514 }
515
516 private void moveDownToolStripMenuItem1_Click(object sender, EventArgs e)
517 {
518 if (propView.SelectedNode == null)
519 return;
520 Node n = (Node)propView.SelectedNode.Tag;
521 if (n.type == Node.Type.recording)
522 return;
523 int oldindex = n.recording.List.IndexOf(n.action);
524 n.recording.List.RemoveAt(oldindex);
525 int newindex = (oldindex) + 1;
526 // the actual index could have shifted due to the removal
527 n.recording.List.Insert(newindex, n.action);
529 }
530
531 private void renameToolStripMenuItem_Click(object sender, EventArgs e)
532 {
533 if (propView.SelectedNode == null)
534 return;
535 Node n = (Node)propView.SelectedNode.Tag;
536 if (n.type == Node.Type.recording)
537 return;
538 n.action.Value = (Automation.Action.ValueType)propBox.SelectedItem;
539 }
540
541 private void topMostBox_CheckedChanged(object sender, EventArgs e)
542 {
543 this.TopMost = topMostBox.Checked;
544 }
545
546 private void startPropBut_Click_1(object sender, EventArgs e)
547 {
548
549 }
550 }
551}
AForge.Bitmap Bitmap
AForge.Point Point
System.Windows.Forms.Keys Keys
Definition ImageView.cs:17
static Dictionary< string, Recording > Recordings
Definition Automation.cs:14
static Dictionary< string, Recording > Properties
Definition Automation.cs:15
static void AddLine(string s)
Definition Recorder.cs:16
Automation.Action action
Node(Automation.Action el, TreeNode n, Type t)
override string ToString()
Automation.Recording recording
Node(Automation.Recording rec, TreeNode n, Type t)
void OpenProperty(string file)
void OpenRecording(string file)
static object GetProperty(Automation.Action.ValueType automation, string pro)
static void Perform(string act)