1using BioImager.Properties;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Configuration;
10using System.Threading.Tasks;
11using System.Windows.Forms;
18 public partial class FunctionForm : Form
20 public static InputSimulator input =
new InputSimulator();
21 private Function func;
22 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
34 public FunctionForm(Function func)
36 InitializeComponent();
40 public FunctionForm(Function func,
string name)
42 InitializeComponent();
53 textBox.Text = func.Script;
55 funcsBox.Items.Clear();
56 foreach (Function f
in Function.Functions.Values)
58 funcsBox.Items.Add(f);
61 nameBox.Text = func.Name;
63 stateBox.Items.Clear();
64 foreach (Function.ButtonState val in Enum.GetValues(typeof(Function.ButtonState)))
66 stateBox.Items.Add(val);
68 stateBox.SelectedItem = func.State;
72 keysBox.Items.Clear();
73 foreach (VirtualKeyCode val
in Enum.GetValues(typeof(VirtualKeyCode)))
75 keysBox.Items.Add(val);
77 keysBox.SelectedIndex = ind;
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;
92 microBox.Items.Clear();
93 foreach (Function.FunctionType f in (Function.FunctionType[])Enum.GetValues(typeof(Function.FunctionType)))
95 microBox.Items.Add(f);
97 microBox.SelectedItem = func.FuncType;
100 recBox.Items.Clear();
101 foreach (Automation.Recording rec in Automation.Recordings.Values)
103 recBox.Items.Add(rec);
104 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
107 recBox.SelectedIndex = ind;
113 propBox.Items.Clear();
114 foreach (Automation.Recording rec in Automation.Properties.Values)
116 propBox.Items.Add(rec);
117 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
120 propBox.SelectedIndex = ind;
124 valBox.Value = (decimal)func.Value;
125 menuPath.Text = func.MenuPath;
126 contextMenuPath.Text = func.ContextPath;
129 private void UpdateItems()
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)
140 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
143 recBox.SelectedIndex = ind;
148 foreach (Automation.Recording rec in Automation.Properties.Values)
150 string s = System.IO.Path.GetFileNameWithoutExtension(rec.File);
153 propBox.SelectedIndex = ind;
157 valBox.Value = (decimal)func.Value;
158 if (func.FuncType == Function.FunctionType.ImageJ)
159 imageJRadioBut.Checked =
true;
161 imageJRadioBut.Checked =
false;
162 menuPath.Text = func.MenuPath;
163 contextMenuPath.Text = func.ContextPath;
170 private void keysBox_SelectedIndexChanged(
object sender, EventArgs e)
172 Func.Key = (VirtualKeyCode)keysBox.SelectedItem;
179 private void modifierBox_SelectedIndexChanged(
object sender, EventArgs e)
181 Func.Modifier = (VirtualKeyCode)modifierBox.SelectedItem;
188 private void stateBox_SelectedIndexChanged(
object sender, EventArgs e)
190 Func.State = (Function.ButtonState)stateBox.SelectedItem;
196 private void applyButton_Click(
object sender, EventArgs e)
198 func.Name = nameBox.Text;
199 this.DialogResult = DialogResult.OK;
200 if(!Function.Functions.ContainsKey(func.Name))
202 Function.Functions.Add(func.Name, func);
206 Function.Functions[func.Name] = func;
208 if (func.MenuPath !=
null && func.MenuPath !=
"")
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);
216 if (func.ContextPath !=
null && func.ContextPath !=
"")
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);
224 func.FuncType = (Function.FunctionType)microBox.SelectedItem;
234 private void propBox_SelectedIndexChanged(
object sender, EventArgs e)
236 Func.File = propBox.Text;
237 Func.FuncType = Function.FunctionType.Property;
244 private void recBox_SelectedIndexChanged(
object sender, EventArgs e)
246 Func.File = recBox.Text;
247 Func.FuncType = Function.FunctionType.Recording;
254 private void setMacroFileBut_Click(
object sender, EventArgs e)
256 Func.File = propBox.Text;
257 Func.FuncType = Function.FunctionType.ImageJ;
263 private void setScriptFileBut_Click(
object sender, EventArgs e)
265 Func.File = propBox.Text;
266 Func.FuncType = Function.FunctionType.Script;
273 private void valBox_ValueChanged(
object sender, EventArgs e)
275 Func.Value = (double)valBox.Value;
282 private void performBut_Click(
object sender, EventArgs e)
284 MessageBox.Show(func.PerformFunction(imageJRadioBut.Checked).ToString());
292 private void cancelBut_Click(
object sender, EventArgs e)
294 this.DialogResult = DialogResult.Cancel;
300 private void textBox_TextChanged(
object sender, EventArgs e)
302 func.Script = textBox.Text;
303 if (bioRadioBut.Checked)
304 func.FuncType = Function.FunctionType.Script;
306 func.FuncType = Function.FunctionType.ImageJ;
313 private void bioRadioBut_CheckedChanged(
object sender, EventArgs e)
315 if(bioRadioBut.Checked)
316 func.FuncType = Function.FunctionType.Script;
323 private void imageJRadioBut_CheckedChanged(
object sender, EventArgs e)
325 if (imageJRadioBut.Checked)
326 func.FuncType = Function.FunctionType.ImageJ;
333 private void menuPath_TextChanged(
object sender, EventArgs e)
335 func.MenuPath = menuPath.Text;
342 private void FunctionForm_Activated(
object sender, EventArgs e)
354 private void funcsBox_SelectedIndexChanged(
object sender, EventArgs e)
356 if (funcsBox.SelectedIndex == -1)
358 func = (Function)funcsBox.SelectedItem;
366 private void FunctionForm_FormClosing(
object sender, FormClosingEventArgs e)
376 private void contextMenuPath_TextChanged(
object sender, EventArgs e)
378 func.ContextPath = contextMenuPath.Text;
386 private void microBox_SelectedIndexChanged(
object sender, EventArgs e)
391 private void nameBox_TextChanged(
object sender, EventArgs e)
399 public static Dictionary<string, Function> Functions =
new Dictionary<string, Function>();
401 public enum FunctionType
423 PreviousSnapCoordinate,
431 public enum ButtonState
437 private ButtonState buttonState = ButtonState.Pressed;
439 public ButtonState State
451 private VirtualKeyCode key;
453 public VirtualKeyCode Key
465 private VirtualKeyCode modifier = VirtualKeyCode.NONAME;
466 public VirtualKeyCode Modifier
478 private FunctionType functionType;
479 public FunctionType FuncType
487 functionType = value;
495 set { file = value; }
498 private string script;
501 get {
return script; }
520 private string menuPath;
521 public string MenuPath
532 private string contextPath;
533 public string ContextPath
557 public override string ToString()
559 return name +
", " + MenuPath;
561 public static Function Parse(
string s)
567 return JsonConvert.DeserializeObject<
Function>(s);
575 public string Serialize()
577 return JsonConvert.SerializeObject(
this);
580 public static InputSimulator input =
new InputSimulator();
588 if(FuncType == FunctionType.Key && Script!=
"")
591 FuncType = FunctionType.ImageJ;
594 FuncType = FunctionType.Script;
595 if (FuncType ==
Function.FunctionType.Script)
599 if (FuncType ==
Function.FunctionType.ImageJ)
603 if (FuncType ==
Function.FunctionType.Property)
605 if (Name.StartsWith(
"Get"))
610 else if (Name.StartsWith(
"Set"))
613 r.Set(Value.ToString());
617 if (FuncType ==
Function.FunctionType.Key)
619 if (Modifier != VirtualKeyCode.NONAME)
621 input.Keyboard.ModifiedKeyStroke(Modifier, Key);
625 input.Keyboard.KeyPress(Key);
629 if (FuncType ==
Function.FunctionType.Recording)
634 if (FuncType ==
Function.FunctionType.Objective)
638 if (FuncType ==
Function.FunctionType.StoreCoordinate)
642 if (FuncType ==
Function.FunctionType.NextCoordinate)
646 if (FuncType ==
Function.FunctionType.PreviousCoordinate)
650 if (FuncType ==
Function.FunctionType.NextSnapCoordinate)
654 if (FuncType ==
Function.FunctionType.PreviousSnapCoordinate)
658 if (FuncType ==
Function.FunctionType.StageUp)
660 Microscope.MoveUp(Value);
662 if (FuncType ==
Function.FunctionType.StageRight)
664 Microscope.MoveRight(Value);
666 if (FuncType ==
Function.FunctionType.StageDown)
668 Microscope.MoveDown(Value);
670 if (FuncType ==
Function.FunctionType.StageLeft)
672 Microscope.MoveLeft(Value);
674 if (FuncType ==
Function.FunctionType.FocusUp)
676 Microscope.SetFocus(Value);
678 if (FuncType ==
Function.FunctionType.FocusDown)
680 Microscope.SetFocus(-Value);
682 if (FuncType ==
Function.FunctionType.StageFieldUp)
684 Microscope.MoveFieldUp();
686 if (FuncType ==
Function.FunctionType.StageFieldRight)
688 Microscope.MoveFieldRight();
690 if (FuncType ==
Function.FunctionType.StageFieldDown)
692 Microscope.MoveFieldDown();
694 if (FuncType ==
Function.FunctionType.StageFieldLeft)
696 Microscope.MoveFieldLeft();
698 if (FuncType ==
Function.FunctionType.TakeImage)
700 Microscope.TakeImage();
702 if (FuncType ==
Function.FunctionType.TakeImageStack)
704 Microscope.TakeImageStack();
706 if (FuncType ==
Function.FunctionType.RL)
708 Microscope.RLShutter.SetPosition((
int)Value);
710 if (FuncType ==
Function.FunctionType.TL)
712 Microscope.TLShutter.SetPosition((
int)Value);
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++)
726 string fs = System.IO.File.ReadAllText(sts[i]);
728 if(!Functions.ContainsKey(f.Name))
729 Functions.Add(f.Name, f);
737 string st = Application.StartupPath;
739 if(!Directory.Exists(st +
"/Functions"))
741 Directory.CreateDirectory(st +
"/Functions");
743 foreach (
Function f
in Functions.Values)
745 System.IO.File.WriteAllText(st +
"/Functions/" + f.Name +
".func",f.Serialize());
static void AddMenu(string menu, Function f)
static void AddContextMenu(string menu, Function f)
object PerformFunction(bool imagej)
void Save()
It saves all the functions in the Functions dictionary to a file.
static void RunOnImage(string con, bool headless, bool onTab, bool bioformats, bool newTab)
object PerformFunction(Function f)
static void RunString(string st)