From 437366fd678396cdd0e3c205038536576f9cde0c Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Fri, 1 Nov 2019 00:27:57 +0100 Subject: [PATCH] Implement AutomationBotFactory and UI for Bot selection. --- VAR.ScreenAutomation/Bots/DummyBot.cs | 2 + .../Code/AutomationBotFactory.cs | 63 +++++++++++++++++++ .../FrmScreenAutomation.Designer.cs | 56 +++++++++++------ VAR.ScreenAutomation/FrmScreenAutomation.cs | 9 ++- .../Interfaces/IAutomationBot.cs | 1 + .../VAR.ScreenAutomation.csproj | 1 + 6 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 VAR.ScreenAutomation/Code/AutomationBotFactory.cs diff --git a/VAR.ScreenAutomation/Bots/DummyBot.cs b/VAR.ScreenAutomation/Bots/DummyBot.cs index 824474f..e2c26e7 100644 --- a/VAR.ScreenAutomation/Bots/DummyBot.cs +++ b/VAR.ScreenAutomation/Bots/DummyBot.cs @@ -7,6 +7,8 @@ namespace VAR.ScreenAutomation.Bots { private int frameCount = 0; + public string Name => "Dummy"; + public void Init(IOutputHandler output) { frameCount = 0; diff --git a/VAR.ScreenAutomation/Code/AutomationBotFactory.cs b/VAR.ScreenAutomation/Code/AutomationBotFactory.cs new file mode 100644 index 0000000..2d4277a --- /dev/null +++ b/VAR.ScreenAutomation/Code/AutomationBotFactory.cs @@ -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 _dictAutomationBots = null; + + private static Dictionary GetDict() + { + if (_dictAutomationBots != null) + { + return _dictAutomationBots; + } + + Type iAutomationBot = typeof(IAutomationBot); + IEnumerable 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 dict = GetDict(); + string[] allAutomationBots = dict.Select(p => p.Key).ToArray(); + return allAutomationBots; + } + + public static IAutomationBot CreateFromName(string name) + { + Dictionary 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; + } + } +} diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs b/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs index ad40994..ed22c2d 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs +++ b/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs @@ -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; } } diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.cs b/VAR.ScreenAutomation/FrmScreenAutomation.cs index 96ef5d8..7520253 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.cs +++ b/VAR.ScreenAutomation/FrmScreenAutomation.cs @@ -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); diff --git a/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs b/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs index 674788c..1aea4a6 100644 --- a/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs +++ b/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs @@ -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(); diff --git a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj index 2fb3d62..faddfd1 100644 --- a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj +++ b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj @@ -50,6 +50,7 @@ +