Remove duplicated CtrOutput and IOutputHandler

This commit is contained in:
2022-04-09 02:49:55 +02:00
parent 4a3ae05f39
commit ef30383abb
13 changed files with 34 additions and 221 deletions

View File

@@ -12,12 +12,12 @@ namespace VAR.ScreenAutomation.Bots
return null;
}
public void Init(IOutputHandler output, IConfiguration config)
public void Init(VAR.Toolbox.Code.IOutputHandler output, IConfiguration config)
{
output.Clean();
}
public Bitmap Process(Bitmap bmpInput, IOutputHandler output)
public Bitmap Process(Bitmap bmpInput, VAR.Toolbox.Code.IOutputHandler output)
{
return bmpInput;
}

View File

@@ -6,8 +6,8 @@ namespace VAR.ScreenAutomation.Interfaces
{
string Name { get; }
IConfiguration GetDefaultConfiguration();
void Init(IOutputHandler output, IConfiguration config);
Bitmap Process(Bitmap bmpInput, IOutputHandler output);
void Init(VAR.Toolbox.Code.IOutputHandler output, IConfiguration config);
Bitmap Process(Bitmap bmpInput, VAR.Toolbox.Code.IOutputHandler output);
string ResponseKeys();
}
}

View File

@@ -1,8 +0,0 @@
namespace VAR.ScreenAutomation.Interfaces
{
public interface IOutputHandler
{
void Clean();
void AddLine(string line, object data = null);
}
}

View File

@@ -39,7 +39,7 @@ namespace VAR.ScreenAutomation.Bots
return defaultConfiguration;
}
public void Init(IOutputHandler output, IConfiguration config)
public void Init(VAR.Toolbox.Code.IOutputHandler output, IConfiguration config)
{
int gridWidth = config.Get("GridWidth", DefaultGridWidth);
int gridHeight = config.Get("GridHeight", DefaultGridHeight);
@@ -60,7 +60,7 @@ namespace VAR.ScreenAutomation.Bots
output.AddLine($"TetrisBot: Starting {DateTime.UtcNow:s}");
}
public Bitmap Process(Bitmap bmpInput, IOutputHandler output)
public Bitmap Process(Bitmap bmpInput, VAR.Toolbox.Code.IOutputHandler output)
{
_grid.SampleFromBitmap(bmpInput);
SearchShape();
@@ -550,7 +550,7 @@ namespace VAR.ScreenAutomation.Bots
}
}
public void Print(IOutputHandler output)
public void Print(VAR.Toolbox.Code.IOutputHandler output)
{
for (int y = 0; y < ShapeSize; y++)
{

View File

@@ -2,6 +2,7 @@
{
public interface IOutputHandler
{
void OutputLine(string line);
void Clean();
void AddLine(string line, object data = null);
}
}

View File

@@ -17,7 +17,7 @@
public bool ExecuteCmd(string cmdString, IOutputHandler outputHandler)
{
outputHandler.OutputLine($"DummyExecution: {cmdString} | {_config}");
outputHandler.AddLine($"DummyExecution: {cmdString} | {_config}");
return true;
}
}

View File

@@ -25,7 +25,7 @@ namespace VAR.Toolbox.Code.ProxyCmdExecutors
while (reader.Read())
{
string output = Convert.ToString(reader[0]);
outputHandler.OutputLine(output);
outputHandler.AddLine(output);
}
cnx.Close();

View File

@@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using VAR.Toolbox.Code;
namespace VAR.Toolbox.Controls
{
public class CtrOutput : Control
public class CtrOutput : Control, IOutputHandler
{
private ListBoxMonospace _listBox;

View File

@@ -56,7 +56,7 @@ namespace VAR.Toolbox.UI.Tools
txtInput.Text = string.Empty;
Application.DoEvents();
txtInput.Text = string.Empty;
OutputLine(cmd);
AddLine(cmd);
PrepareProxyCmdExecutor();
new Thread(() => ExecuteCmd(cmd)).Start();
}
@@ -170,23 +170,36 @@ namespace VAR.Toolbox.UI.Tools
catch (Exception ex)
{
Logger.Log(ex);
OutputLine(ex.Message);
AddLine(ex.Message);
}
Monitor.Exit(_executionLock);
}
public void OutputLine(string line)
#endregion Private methods
#region IOutputHandler
public void Clean()
{
BeginInvoke(new MethodInvoker(delegate
{
ctrOutput.AddLine(line);
ctrOutput.Clean();
Application.DoEvents();
}));
}
#endregion Private methods
public void AddLine(string line, object data = null)
{
BeginInvoke(new MethodInvoker(delegate
{
ctrOutput.AddLine(line, data);
Application.DoEvents();
}));
}
#endregion IOutputHandler
#region Config
public void LoadConfig()

View File

@@ -1,190 +0,0 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using VAR.ScreenAutomation.Interfaces;
// ReSharper disable InconsistentNaming
namespace VAR.ScreenAutomation.Controls
{
public class CtrOutput : Control, IOutputHandler
{
private ListBox _listBox;
private Timer _timer;
private class OutputItem
{
public string Text { get; set; }
public object Data { get; set; }
public override string ToString()
{
return Text;
}
}
public new event EventHandler DoubleClick;
public CtrOutput()
{
InitializeControls();
}
private void InitializeControls()
{
_listBox = new ListBox
{
Dock = DockStyle.Fill,
FormattingEnabled = true,
Font = new Font("Consolas", 9),
BackColor = Color.Black,
ForeColor = Color.Gray,
SelectionMode = SelectionMode.MultiExtended,
};
_listBox.MouseDoubleClick += ListBox_MouseDoubleClick;
_listBox.KeyDown += ListBox_KeyDown;
Controls.Add(_listBox);
_timer = new Timer
{
Interval = 100,
Enabled = true
};
_timer.Tick += Timer_Tick;
Disposed += CtrOutput_Disposed;
}
private void CtrOutput_Disposed(object sender, EventArgs e)
{
_timer.Stop();
_timer.Enabled = false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.C) == Keys.C)
{
CopyToClipboard();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
CopyToClipboard();
}
}
private void CopyToClipboard()
{
StringBuilder sbText = new StringBuilder();
foreach (OutputItem item in _listBox.SelectedItems)
{
sbText.AppendLine(item.Text);
}
if (sbText.Length > 0)
{
Clipboard.SetText(sbText.ToString());
}
}
private void ListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
DoubleClick?.Invoke(sender, e);
}
private void Timer_Tick(object sender, EventArgs e)
{
if (_updated)
{
UpdatePosition();
}
}
private bool _updated;
private readonly List<OutputItem> _pendingOutput = new List<OutputItem>();
private void UpdatePosition()
{
lock (_pendingOutput)
{
EnableRepaint(new HandleRef(_listBox, _listBox.Handle), false);
_listBox.SuspendLayout();
foreach (OutputItem item in _pendingOutput)
{
_listBox.Items.Add(item);
}
_pendingOutput.Clear();
_listBox.ResumeLayout();
int visibleItems = _listBox.ClientSize.Height / _listBox.ItemHeight;
_listBox.TopIndex = Math.Max(_listBox.Items.Count - visibleItems + 1, 0);
_updated = false;
EnableRepaint(new HandleRef(_listBox, _listBox.Handle), true);
_listBox.Invalidate();
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam);
private static void EnableRepaint(HandleRef handle, bool enable)
{
const int WM_SETREDRAW = 0x000B;
SendMessage(handle, WM_SETREDRAW, new IntPtr(enable ? 1 : 0), IntPtr.Zero);
}
public void Clean()
{
if (_listBox.InvokeRequired)
{
_listBox.Invoke((MethodInvoker)(() =>
{
_listBox.Items.Clear();
_updated = true;
}));
}
else
{
_listBox.Items.Clear();
_updated = true;
}
}
public void AddLine(string line, object data = null)
{
lock (_pendingOutput)
{
_pendingOutput.Add(new OutputItem { Text = line, Data = data, });
_updated = true;
}
}
public string GetCurrentText()
{
if (_listBox.SelectedItems.Count == 0) { return null; }
OutputItem item = (OutputItem)_listBox.SelectedItems[0];
return item?.Text;
}
public object GetCurrentData()
{
if (_listBox.SelectedItems.Count == 0) { return null; }
OutputItem item = (OutputItem)_listBox.SelectedItems[0];
return item?.Data;
}
}
}

View File

@@ -37,7 +37,7 @@
this.btnStartEnd = new System.Windows.Forms.Button();
this.chkKeepToplevel = new System.Windows.Forms.CheckBox();
this.picPreview = new VAR.ScreenAutomation.Controls.CtrImageViewer();
this.ctrOutput = new VAR.ScreenAutomation.Controls.CtrOutput();
this.ctrOutput = new VAR.Toolbox.Controls.CtrOutput();
this.chkClick = new System.Windows.Forms.CheckBox();
((System.ComponentModel.ISupportInitialize)(this.picCapturer)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.splitMain)).BeginInit();
@@ -252,7 +252,7 @@
private System.Windows.Forms.PictureBox picCapturer;
private Controls.CtrImageViewer picPreview;
private Controls.CtrOutput ctrOutput;
private VAR.Toolbox.Controls.CtrOutput ctrOutput;
private System.Windows.Forms.SplitContainer splitMain;
private System.Windows.Forms.SplitContainer splitOutput;
private System.Windows.Forms.Button btnStartEnd;

View File

@@ -9,7 +9,7 @@ using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation
{
public partial class FrmScreenAutomation : Form, VAR.Toolbox.UI.IToolForm
public partial class FrmScreenAutomation : Form, Toolbox.UI.IToolForm
{
public string ToolName => "ScreenAutomation";

View File

@@ -78,7 +78,6 @@
<Compile Include="Code\BaseFactory.cs" />
<Compile Include="Code\Bots\DummyBot.cs" />
<Compile Include="Code\Bots\IAutomationBot.cs" />
<Compile Include="Code\Bots\IOutputHandler.cs" />
<Compile Include="Code\Bots\Mouse.cs" />
<Compile Include="Code\Bots\Screenshoter.cs" />
<Compile Include="Code\Bots\TetrisBot.cs" />
@@ -258,9 +257,6 @@
<Compile Include="UI\Tools\ScreenAutomation\CtrImageViewer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="UI\Tools\ScreenAutomation\CtrOutput.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="UI\Tools\ScreenAutomation\FrmAutomationBotParams.cs">
<SubType>Form</SubType>
</Compile>