Implement AutomationBotFactory and UI for Bot selection.

This commit is contained in:
2019-11-01 00:27:57 +01:00
parent 3531b72793
commit 437366fd67
6 changed files with 110 additions and 22 deletions

View File

@@ -7,6 +7,8 @@ namespace VAR.ScreenAutomation.Bots
{
private int frameCount = 0;
public string Name => "Dummy";
public void Init(IOutputHandler output)
{
frameCount = 0;

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using VAR.ScreenAutomation.Bots;
using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation.Code
{
public class AutomationBotFactory
{
private static Dictionary<string, Type> _dictAutomationBots = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictAutomationBots != null)
{
return _dictAutomationBots;
}
Type iAutomationBot = typeof(IAutomationBot);
IEnumerable<Type> automationBotTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x =>
x.IsAbstract == false &&
x.IsInterface == false &&
iAutomationBot.IsAssignableFrom(x) &&
true);
_dictAutomationBots = automationBotTypes.ToDictionary(t =>
{
IAutomationBot automationBot = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IAutomationBot;
return automationBot.Name;
});
return _dictAutomationBots;
}
public static object[] GetAllAutomationBots()
{
Dictionary<string, Type> dict = GetDict();
string[] allAutomationBots = dict.Select(p => p.Key).ToArray();
return allAutomationBots;
}
public static IAutomationBot CreateFromName(string name)
{
Dictionary<string, Type> dict = GetDict();
if (string.IsNullOrEmpty(name))
{
return new DummyBot();
}
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Can't create IAutomationBot with this name: {0}", name));
}
Type proxyCmdExecutorType = dict[name];
IAutomationBot automationBot = Activator.CreateInstance(proxyCmdExecutorType) as IAutomationBot;
return automationBot;
}
}
}

View File

@@ -31,8 +31,9 @@
this.picCapturer = new System.Windows.Forms.PictureBox();
this.splitMain = new System.Windows.Forms.SplitContainer();
this.splitOutput = new System.Windows.Forms.SplitContainer();
this.picPreview = new VAR.ScreenAutomation.Controls.CtrImageViewer();
this.ddlAutomationBot = new System.Windows.Forms.ComboBox();
this.btnStartEnd = new System.Windows.Forms.Button();
this.picPreview = new VAR.ScreenAutomation.Controls.CtrImageViewer();
this.ctrOutput = new VAR.ScreenAutomation.Controls.CtrOutput();
((System.ComponentModel.ISupportInitialize)(this.picCapturer)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.splitMain)).BeginInit();
@@ -48,8 +49,8 @@
//
// picCapturer
//
this.picCapturer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
this.picCapturer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picCapturer.Location = new System.Drawing.Point(4, 4);
this.picCapturer.Margin = new System.Windows.Forms.Padding(4);
@@ -91,16 +92,41 @@
//
// splitOutput.Panel2
//
this.splitOutput.Panel2.Controls.Add(this.ddlAutomationBot);
this.splitOutput.Panel2.Controls.Add(this.btnStartEnd);
this.splitOutput.Panel2.Controls.Add(this.ctrOutput);
this.splitOutput.Size = new System.Drawing.Size(309, 816);
this.splitOutput.SplitterDistance = 371;
this.splitOutput.TabIndex = 4;
//
// ddlAutomationBot
//
this.ddlAutomationBot.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ddlAutomationBot.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ddlAutomationBot.FormattingEnabled = true;
this.ddlAutomationBot.Location = new System.Drawing.Point(13, 4);
this.ddlAutomationBot.Name = "ddlAutomationBot";
this.ddlAutomationBot.Size = new System.Drawing.Size(293, 24);
this.ddlAutomationBot.TabIndex = 4;
//
// btnStartEnd
//
this.btnStartEnd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnStartEnd.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStartEnd.Location = new System.Drawing.Point(15, 34);
this.btnStartEnd.Name = "btnStartEnd";
this.btnStartEnd.Size = new System.Drawing.Size(294, 39);
this.btnStartEnd.TabIndex = 3;
this.btnStartEnd.Text = "Start";
this.btnStartEnd.UseVisualStyleBackColor = true;
this.btnStartEnd.Click += new System.EventHandler(this.BtnStartEnd_Click);
//
// picPreview
//
this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picPreview.BackColor = System.Drawing.Color.Black;
this.picPreview.ImageShow = null;
@@ -110,25 +136,14 @@
this.picPreview.TabIndex = 1;
this.picPreview.TabStop = false;
//
// btnStartEnd
//
this.btnStartEnd.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStartEnd.Location = new System.Drawing.Point(12, 3);
this.btnStartEnd.Name = "btnStartEnd";
this.btnStartEnd.Size = new System.Drawing.Size(294, 39);
this.btnStartEnd.TabIndex = 3;
this.btnStartEnd.Text = "Start";
this.btnStartEnd.UseVisualStyleBackColor = true;
this.btnStartEnd.Click += new System.EventHandler(this.BtnStartEnd_Click);
//
// ctrOutput
//
this.ctrOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
this.ctrOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ctrOutput.Location = new System.Drawing.Point(12, 48);
this.ctrOutput.Location = new System.Drawing.Point(12, 79);
this.ctrOutput.Name = "ctrOutput";
this.ctrOutput.Size = new System.Drawing.Size(294, 380);
this.ctrOutput.Size = new System.Drawing.Size(294, 349);
this.ctrOutput.TabIndex = 2;
this.ctrOutput.Text = "ctrOutput1";
//
@@ -164,6 +179,7 @@
private System.Windows.Forms.SplitContainer splitMain;
private System.Windows.Forms.SplitContainer splitOutput;
private System.Windows.Forms.Button btnStartEnd;
private System.Windows.Forms.ComboBox ddlAutomationBot;
}
}

View File

@@ -2,7 +2,6 @@
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using VAR.ScreenAutomation.Bots;
using VAR.ScreenAutomation.Code;
using VAR.ScreenAutomation.Interfaces;
@@ -11,7 +10,7 @@ namespace VAR.ScreenAutomation
public partial class FrmScreenAutomation : Form
{
private bool _running = false;
private IAutomationBot _automationBot = new DummyBot();
private IAutomationBot _automationBot = null;
private Timer timTicker;
private Bitmap bmpScreen = null;
@@ -31,6 +30,11 @@ namespace VAR.ScreenAutomation
TransparencyKey = Color.LimeGreen;
picCapturer.BackColor = Color.LimeGreen;
ddlAutomationBot.Items.AddRange(AutomationBotFactory.GetAllAutomationBots());
ddlAutomationBot.SelectedIndex = 0;
if (components == null) { components = new Container(); }
timTicker = new Timer(components)
{
@@ -80,6 +84,7 @@ namespace VAR.ScreenAutomation
if (_running) { return; }
_running = true;
btnStartEnd.Text = "End";
_automationBot = AutomationBotFactory.CreateFromName((string)ddlAutomationBot.SelectedItem);
_automationBot?.Init(ctrOutput);
Point pointCapturerCenter = picCapturer.PointToScreen(new Point(picCapturer.Width / 2, picCapturer.Height / 2));
Mouse.SetPosition((uint)pointCapturerCenter.X, (uint)pointCapturerCenter.Y);

View File

@@ -4,6 +4,7 @@ namespace VAR.ScreenAutomation.Interfaces
{
public interface IAutomationBot
{
string Name { get; }
void Init(IOutputHandler output);
Bitmap Process(Bitmap bmpInput, IOutputHandler output);
string ResponseKeys();

View File

@@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Bots\DummyBot.cs" />
<Compile Include="Code\AutomationBotFactory.cs" />
<Compile Include="Code\Mouse.cs" />
<Compile Include="Code\Screenshoter.cs" />
<Compile Include="Code\WindowHandling.cs" />