BioImager  4.9.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
FunctionForm.cs
1using BioImager.Properties;
2using System;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Configuration;
6using System.Data;
7using System.Drawing;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows.Forms;
12using WindowsInput;
13using System.IO;
14using Newtonsoft.Json;
15
16namespace BioImager
17{
18 public partial class FunctionForm : Form
19 {
20 public static InputSimulator input = new InputSimulator();
21 private Function func;
22 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
23 public Function Func
24 {
25 get
26 {
27 return func;
28 }
29 set
30 {
31 func = value;
32 }
33 }
34 public FunctionForm(Function func)
35 {
36 InitializeComponent();
37 Func = func;
38 Init();
39 }
40 public FunctionForm(Function func, string name)
41 {
42 InitializeComponent();
43 Func = func;
44 func.Name = name;
45 Init();
46 }
48 private void Init()
49 {
50 int ind = 0;
51 int sel = 0;
52
53 textBox.Text = func.Script;
54
55 funcsBox.Items.Clear();
56 foreach (Function f in Function.Functions.Values)
57 {
58 funcsBox.Items.Add(f);
59 }
60
61 nameBox.Text = func.Name;
62 //We add button states to buttonState box.
63 stateBox.Items.Clear();
64 foreach (Function.ButtonState val in Enum.GetValues(typeof(Function.ButtonState)))
65 {
66 stateBox.Items.Add(val);
67 }
68 stateBox.SelectedItem = func.State;
69
70 //We add button states to buttonState box.
71 ind = 0;
72 keysBox.Items.Clear();
73 foreach (VirtualKeyCode val in Enum.GetValues(typeof(VirtualKeyCode)))
74 {
75 keysBox.Items.Add(val);
76 if (val == func.Key)
77 keysBox.SelectedIndex = ind;
78 ind++;
79 }
80 //We add the modifiers to modifierBox
81 modifierBox.Items.Clear();
82 modifierBox.Items.Add(VirtualKeyCode.NONAME);
83 modifierBox.Items.Add(VirtualKeyCode.RCONTROL);
84 modifierBox.Items.Add(VirtualKeyCode.LCONTROL);
85 modifierBox.Items.Add(VirtualKeyCode.RSHIFT);
86 modifierBox.Items.Add(VirtualKeyCode.LSHIFT);
87 modifierBox.Items.Add(VirtualKeyCode.MENU);
88 modifierBox.Items.Add(VirtualKeyCode.RWIN);
89 modifierBox.Items.Add(VirtualKeyCode.LWIN);
90 modifierBox.SelectedItem = func.Modifier;
91
92 microBox.Items.Clear();
93 foreach (Function.FunctionType f in (Function.FunctionType[])Enum.GetValues(typeof(Function.FunctionType)))
94 {
95 microBox.Items.Add(f);
96 }
97 microBox.SelectedItem = func.FuncType;
98
99 ind = 0;
100 recBox.Items.Clear();
101 foreach (Automation.Recording rec in Automation.Recordings.Values)
102 {
103 recBox.Items.Add(rec);
104 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
105 if(s == func.File)
106 {
107 recBox.SelectedIndex = ind;
108 }
109 ind++;
110 }
111
112 ind = 0;
113 propBox.Items.Clear();
114 foreach (Automation.Recording rec in Automation.Properties.Values)
115 {
116 propBox.Items.Add(rec);
117 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
118 if (s == func.File)
119 {
120 propBox.SelectedIndex = ind;
121 }
122 ind++;
123 }
124 valBox.Value = (decimal)func.Value;
125 menuPath.Text = func.MenuPath;
126 contextMenuPath.Text = func.ContextPath;
127 }
129 private void UpdateItems()
130 {
131 int ind = 0;
132 textBox.Text = func.Script;
133 nameBox.Text = func.Name;
134 stateBox.SelectedItem = func.State;
135 keysBox.SelectedItem = func.Key;
136 modifierBox.SelectedItem = func.Modifier;
137 microBox.SelectedItem = func.FuncType;
138 foreach (Automation.Recording rec in Automation.Recordings.Values)
139 {
140 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
141 if (s == func.File)
142 {
143 recBox.SelectedIndex = ind;
144 }
145 ind++;
146 }
147 ind = 0;
148 foreach (Automation.Recording rec in Automation.Properties.Values)
149 {
150 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
151 if (s == func.File)
152 {
153 propBox.SelectedIndex = ind;
154 }
155 ind++;
156 }
157 valBox.Value = (decimal)func.Value;
158 if (func.FuncType == Function.FunctionType.ImageJ)
159 imageJRadioBut.Checked = true;
160 else
161 imageJRadioBut.Checked = false;
162 menuPath.Text = func.MenuPath;
163 contextMenuPath.Text = func.ContextPath;
164 }
170 private void keysBox_SelectedIndexChanged(object sender, EventArgs e)
171 {
172 Func.Key = (VirtualKeyCode)keysBox.SelectedItem;
173 }
179 private void modifierBox_SelectedIndexChanged(object sender, EventArgs e)
180 {
181 Func.Modifier = (VirtualKeyCode)modifierBox.SelectedItem;
182 }
188 private void stateBox_SelectedIndexChanged(object sender, EventArgs e)
189 {
190 Func.State = (Function.ButtonState)stateBox.SelectedItem;
191 }
196 private void applyButton_Click(object sender, EventArgs e)
197 {
198 func.Name = nameBox.Text;
199 this.DialogResult = DialogResult.OK;
200 if(!Function.Functions.ContainsKey(func.Name))
201 {
202 Function.Functions.Add(func.Name, func);
203 }
204 else
205 {
206 Function.Functions[func.Name] = func;
207 }
208 if (func.MenuPath != null && func.MenuPath != "")
209 {
210 if (func.MenuPath.EndsWith("/"))
211 func.MenuPath = func.MenuPath.TrimEnd('/');
212 if (func.MenuPath.EndsWith(func.Name) && func.MenuPath.Contains("/"))
213 func.MenuPath.Remove(func.MenuPath.IndexOf('/'), func.MenuPath.Length - func.MenuPath.IndexOf('/'));
214 App.AddMenu(func.MenuPath, func);
215 }
216 if (func.ContextPath != null && func.ContextPath != "")
217 {
218 if (func.ContextPath.EndsWith("/"))
219 func.ContextPath = func.ContextPath.TrimEnd('/');
220 if (func.ContextPath.EndsWith(func.Name) && func.ContextPath.Contains("/"))
221 func.ContextPath.Remove(func.ContextPath.IndexOf('/'), func.ContextPath.Length - func.ContextPath.IndexOf('/'));
222 App.AddContextMenu(func.ContextPath, func);
223 }
224 func.FuncType = (Function.FunctionType)microBox.SelectedItem;
225 func.Save();
226 this.Close();
227 }
234 private void propBox_SelectedIndexChanged(object sender, EventArgs e)
235 {
236 Func.File = propBox.Text;
237 Func.FuncType = Function.FunctionType.Property;
238 }
244 private void recBox_SelectedIndexChanged(object sender, EventArgs e)
245 {
246 Func.File = recBox.Text;
247 Func.FuncType = Function.FunctionType.Recording;
248 }
254 private void setMacroFileBut_Click(object sender, EventArgs e)
255 {
256 Func.File = propBox.Text;
257 Func.FuncType = Function.FunctionType.ImageJ;
258 }
263 private void setScriptFileBut_Click(object sender, EventArgs e)
264 {
265 Func.File = propBox.Text;
266 Func.FuncType = Function.FunctionType.Script;
267 }
273 private void valBox_ValueChanged(object sender, EventArgs e)
274 {
275 Func.Value = (double)valBox.Value;
276 }
282 private void performBut_Click(object sender, EventArgs e)
283 {
284 MessageBox.Show(func.PerformFunction(imageJRadioBut.Checked).ToString());
285 }
286
292 private void cancelBut_Click(object sender, EventArgs e)
293 {
294 this.DialogResult = DialogResult.Cancel;
295 }
300 private void textBox_TextChanged(object sender, EventArgs e)
301 {
302 func.Script = textBox.Text;
303 if (bioRadioBut.Checked)
304 func.FuncType = Function.FunctionType.Script;
305 else
306 func.FuncType = Function.FunctionType.ImageJ;
307 }
308
313 private void bioRadioBut_CheckedChanged(object sender, EventArgs e)
314 {
315 if(bioRadioBut.Checked)
316 func.FuncType = Function.FunctionType.Script;
317 }
318
323 private void imageJRadioBut_CheckedChanged(object sender, EventArgs e)
324 {
325 if (imageJRadioBut.Checked)
326 func.FuncType = Function.FunctionType.ImageJ;
327 }
328
333 private void menuPath_TextChanged(object sender, EventArgs e)
334 {
335 func.MenuPath = menuPath.Text;
336 }
337
342 private void FunctionForm_Activated(object sender, EventArgs e)
343 {
344
345 }
346
354 private void funcsBox_SelectedIndexChanged(object sender, EventArgs e)
355 {
356 if (funcsBox.SelectedIndex == -1)
357 return;
358 func = (Function)funcsBox.SelectedItem;
359 UpdateItems();
360 }
361
366 private void FunctionForm_FormClosing(object sender, FormClosingEventArgs e)
367 {
368
369 }
370
376 private void contextMenuPath_TextChanged(object sender, EventArgs e)
377 {
378 func.ContextPath = contextMenuPath.Text;
379 }
380
386 private void microBox_SelectedIndexChanged(object sender, EventArgs e)
387 {
388 //func.FuncType = (Function.FunctionType)microBox.SelectedItem;
389 }
390
391 private void nameBox_TextChanged(object sender, EventArgs e)
392 {
393
394 }
395 }
396
397 public class Function
398 {
399 public static Dictionary<string, Function> Functions = new Dictionary<string, Function>();
400 /* Defining an enum. */
401 public enum FunctionType
402 {
403 Key,
404 StageUp,
405 StageRight,
406 StageDown,
407 StageLeft,
408 FocusUp,
409 FocusDown,
410 StageFieldUp,
411 StageFieldRight,
412 StageFieldDown,
413 StageFieldLeft,
414 TakeImage,
415 TakeImageStack,
416 RL,
417 TL,
418 Objective,
419 StoreCoordinate,
420 NextCoordinate,
421 PreviousCoordinate,
422 NextSnapCoordinate,
423 PreviousSnapCoordinate,
424 Recording,
425 Property,
426 ImageJ,
427 Script,
428 None
429 }
430 /* Defining an enumeration. */
431 public enum ButtonState
432 {
433 Pressed,
434 Released
435 }
436
437 private ButtonState buttonState = ButtonState.Pressed;
438 /* A property. */
439 public ButtonState State
440 {
441 get
442 {
443 return buttonState;
444 }
445 set
446 {
447 buttonState = value;
448 }
449 }
450
451 private VirtualKeyCode key;
452 /* A property of a class. */
453 public VirtualKeyCode Key
454 {
455 get
456 {
457 return key;
458 }
459 set
460 {
461 key = value;
462 }
463 }
464
465 private VirtualKeyCode modifier = VirtualKeyCode.NONAME;
466 public VirtualKeyCode Modifier
467 {
468 get
469 {
470 return modifier;
471 }
472 set
473 {
474 modifier = value;
475 }
476 }
477
478 private FunctionType functionType;
479 public FunctionType FuncType
480 {
481 get
482 {
483 return functionType;
484 }
485 set
486 {
487 functionType = value;
488 }
489 }
490
491 private string file;
492 public string File
493 {
494 get { return file; }
495 set { file = value; }
496 }
497
498 private string script;
499 public string Script
500 {
501 get { return script; }
502 set
503 {
504 script = value;
505 }
506 }
507
508 private string name;
509 public string Name
510 {
511 get
512 {
513 return name;
514 }
515 set
516 {
517 name = value;
518 }
519 }
520 private string menuPath;
521 public string MenuPath
522 {
523 get
524 {
525 return menuPath;
526 }
527 set
528 {
529 menuPath = value;
530 }
531 }
532 private string contextPath;
533 public string ContextPath
534 {
535 get
536 {
537 return menuPath;
538 }
539 set
540 {
541 menuPath = value;
542 }
543 }
544
545 private double val;
546 public double Value
547 {
548 get
549 {
550 return val;
551 }
552 set
553 {
554 val = value;
555 }
556 }
557 public override string ToString()
558 {
559 return name + ", " + MenuPath;
560 }
561 public static Function Parse(string s)
562 {
563 if (s == "")
564 return new Function();
565 try
566 {
567 return JsonConvert.DeserializeObject<Function>(s);
568 }
569 catch (Exception)
570 {
571 return new Function();
572 }
573 }
574
575 public string Serialize()
576 {
577 return JsonConvert.SerializeObject(this);
578 }
579
580 public static InputSimulator input = new InputSimulator();
586 public object PerformFunction(bool imagej)
587 {
588 if(FuncType == FunctionType.Key && Script!="")
589 if(imagej)
590 {
591 FuncType = FunctionType.ImageJ;
592 }
593 else
594 FuncType = FunctionType.Script;
595 if (FuncType == Function.FunctionType.Script)
596 {
597 Scripting.RunString(script);
598 }
599 if (FuncType == Function.FunctionType.ImageJ)
600 {
601 ImageJ.RunOnImage(script, false, BioConsole.onTab, BioConsole.useBioformats, BioConsole.newTab);
602 }
603 if (FuncType == Function.FunctionType.Property)
604 {
605 if (Name.StartsWith("Get"))
606 {
607 Automation.Recording r = (Automation.Recording)Automation.Properties[System.IO.Path.GetFileNameWithoutExtension(File)];
608 return r.Get();
609 }
610 else if (Name.StartsWith("Set"))
611 {
612 Automation.Recording r = (Automation.Recording)Automation.Properties[System.IO.Path.GetFileNameWithoutExtension(File)];
613 r.Set(Value.ToString());
614 }
615
616 }
617 if (FuncType == Function.FunctionType.Key)
618 {
619 if (Modifier != VirtualKeyCode.NONAME)
620 {
621 input.Keyboard.ModifiedKeyStroke(Modifier, Key);
622 }
623 else
624 {
625 input.Keyboard.KeyPress(Key);
626 }
627 return null;
628 }
629 if (FuncType == Function.FunctionType.Recording)
630 {
631 Automation.Recording r = (Automation.Recording)Automation.Recordings[System.IO.Path.GetFileNameWithoutExtension(File)];
632 r.Run();
633 }
634 if (FuncType == Function.FunctionType.Objective)
635 {
636 App.imager.PerformFunction(this);
637 }
638 if (FuncType == Function.FunctionType.StoreCoordinate)
639 {
640 App.imager.PerformFunction(this);
641 }
642 if (FuncType == Function.FunctionType.NextCoordinate)
643 {
644 App.imager.PerformFunction(this);
645 }
646 if (FuncType == Function.FunctionType.PreviousCoordinate)
647 {
648 App.imager.PerformFunction(this);
649 }
650 if (FuncType == Function.FunctionType.NextSnapCoordinate)
651 {
652 App.imager.PerformFunction(this);
653 }
654 if (FuncType == Function.FunctionType.PreviousSnapCoordinate)
655 {
656 App.imager.PerformFunction(this);
657 }
658 if (FuncType == Function.FunctionType.StageUp)
659 {
660 Microscope.MoveUp(Value);
661 }
662 if (FuncType == Function.FunctionType.StageRight)
663 {
664 Microscope.MoveRight(Value);
665 }
666 if (FuncType == Function.FunctionType.StageDown)
667 {
668 Microscope.MoveDown(Value);
669 }
670 if (FuncType == Function.FunctionType.StageLeft)
671 {
672 Microscope.MoveLeft(Value);
673 }
674 if (FuncType == Function.FunctionType.FocusUp)
675 {
676 Microscope.SetFocus(Value);
677 }
678 if (FuncType == Function.FunctionType.FocusDown)
679 {
680 Microscope.SetFocus(-Value);
681 }
682 if (FuncType == Function.FunctionType.StageFieldUp)
683 {
684 Microscope.MoveFieldUp();
685 }
686 if (FuncType == Function.FunctionType.StageFieldRight)
687 {
688 Microscope.MoveFieldRight();
689 }
690 if (FuncType == Function.FunctionType.StageFieldDown)
691 {
692 Microscope.MoveFieldDown();
693 }
694 if (FuncType == Function.FunctionType.StageFieldLeft)
695 {
696 Microscope.MoveFieldLeft();
697 }
698 if (FuncType == Function.FunctionType.TakeImage)
699 {
700 Microscope.TakeImage();
701 }
702 if (FuncType == Function.FunctionType.TakeImageStack)
703 {
704 Microscope.TakeImageStack();
705 }
706 if (FuncType == Function.FunctionType.RL)
707 {
708 Microscope.RLShutter.SetPosition((int)Value);
709 }
710 if (FuncType == Function.FunctionType.TL)
711 {
712 Microscope.TLShutter.SetPosition((int)Value);
713 }
714 return null;
715 }
718 public static void Initialize()
719 {
720 string st = Application.StartupPath + "/Functions";
721 if (!Directory.Exists(st))
722 Directory.CreateDirectory(st);
723 string[] sts = Directory.GetFiles(st);
724 for (int i = 0; i < sts.Length; i++)
725 {
726 string fs = System.IO.File.ReadAllText(sts[i]);
727 Function f = Function.Parse(fs);
728 if(!Functions.ContainsKey(f.Name))
729 Functions.Add(f.Name, f);
730 App.AddMenu(f.MenuPath, f);
731 App.AddContextMenu(f.ContextPath, f);
732 }
733 }
735 public void Save()
736 {
737 string st = Application.StartupPath;
738
739 if(!Directory.Exists(st + "/Functions"))
740 {
741 Directory.CreateDirectory(st + "/Functions");
742 }
743 foreach (Function f in Functions.Values)
744 {
745 System.IO.File.WriteAllText(st + "/Functions/" + f.Name + ".func",f.Serialize());
746 }
747 }
748 }
749
750}
static void AddMenu(string menu, Function f)
Definition App.cs:386
static void AddContextMenu(string menu, Function f)
Definition App.cs:394
object PerformFunction(bool imagej)
void Save()
It saves all the functions in the Functions dictionary to a file.
static void Initialize()
static void RunOnImage(string con, bool headless, bool onTab, bool bioformats, bool newTab)
Definition ImageJ.cs:189
object PerformFunction(Function f)
Definition Imager.cs:1343
static void RunString(string st)
Definition Scripting.cs:396