mirror of
https://github.com/jie65535/CodeMatrix.git
synced 2025-06-01 17:29:10 +08:00
增加 目标序列代码部分
增加 在代码矩阵中光标悬停时,目标序列中相同代码自动高亮 增加 在目标序列中输入是否正确的判断 修改 Readme中的动图
This commit is contained in:
parent
c985adacc5
commit
c0ba5ba3eb
@ -46,6 +46,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Common.cs" />
|
||||
<Compile Include="FormMain.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
@ -61,6 +62,9 @@
|
||||
<Compile Include="UCCodeQueue.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="UCCodeTarget.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<EmbeddedResource Include="FormMain.resx">
|
||||
<DependentUpon>FormMain.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
15
CodeMatrix/Common.cs
Normal file
15
CodeMatrix/Common.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CodeMatrix
|
||||
{
|
||||
public static class Common
|
||||
{
|
||||
public static byte[] Codes { get; } = new byte[] { 0x55, 0x1C, 0xBD, 0xE9, 0x7A };
|
||||
|
||||
public static Random Random { get; } = new Random();
|
||||
}
|
||||
}
|
@ -19,14 +19,37 @@ namespace CodeMatrix
|
||||
BackColor = Styles.Default.BackColor;
|
||||
|
||||
var codeMatrix = new UCCodeMatrix();
|
||||
var codeQueue = new UCCodeQueue();
|
||||
var codeQueue = new UCCodeQueue
|
||||
{
|
||||
Location = new Point(codeMatrix.Size.Width, 0)
|
||||
};
|
||||
var codeTargets = new UCCodeTarget[3];
|
||||
for (int i = 0, y = codeQueue.Size.Height; i < codeTargets.Length; y += codeTargets[i++].Size.Height)
|
||||
{
|
||||
codeTargets[i] = new UCCodeTarget
|
||||
{
|
||||
Location = new Point(codeQueue.Location.X, y)
|
||||
};
|
||||
}
|
||||
|
||||
codeMatrix.HoverValueChangedEvent += (_, value) =>
|
||||
{
|
||||
codeQueue.HoverCode = value;
|
||||
foreach (var codeTarget in codeTargets)
|
||||
codeTarget.HoverCode = value;
|
||||
};
|
||||
codeMatrix.CodeSelectedEvent += (_, value) =>
|
||||
{
|
||||
codeQueue.InputCode(value);
|
||||
foreach (var codeTarget in codeTargets)
|
||||
codeTarget.InputCode(value);
|
||||
};
|
||||
|
||||
|
||||
codeMatrix.HoverValueChangedEvent += (_, value) => codeQueue.HoverCode = value;
|
||||
codeMatrix.CodeSelectedEvent += (_, value) => codeQueue.InputCode(value);
|
||||
|
||||
codeQueue.Location = new Point(codeMatrix.Size.Width, 0);
|
||||
Controls.Add(codeMatrix);
|
||||
Controls.Add(codeQueue);
|
||||
Controls.AddRange(codeTargets);
|
||||
|
||||
var size = codeMatrix.Size;
|
||||
size.Width += codeQueue.Width;
|
||||
|
@ -10,38 +10,47 @@ namespace CodeMatrix
|
||||
{
|
||||
CodeFontA = new Font("微软雅黑", 14);
|
||||
CodeFontB = new Font("微软雅黑", 12);
|
||||
CellSizeA = new Size(40, 40);
|
||||
CellSizeB = new Size(25, 25);
|
||||
BackColor = Color.FromArgb(18, 13, 25);
|
||||
CodeColor = Color.FromArgb(208, 236, 92);
|
||||
SelectColor = Color.FromArgb(109, 232, 228);
|
||||
CodeBrush = new SolidBrush(CodeColor);
|
||||
SelectBrush = new SolidBrush(SelectColor);
|
||||
ToBeSelectBrush = Brushes.White;
|
||||
EmptyCellBrush = new SolidBrush(Color.FromArgb(65, 52, 76));
|
||||
DefaultLineBackColor = new SolidBrush(Color.FromArgb(42, 43, 60));
|
||||
SelectLineBackColor = new SolidBrush(Color.FromArgb(32, 31, 28));
|
||||
DefaultLineBrush = new SolidBrush(Color.FromArgb(42, 43, 60));
|
||||
SelectLineBrush = new SolidBrush(Color.FromArgb(32, 31, 28));
|
||||
PassBrush = new SolidBrush(Color.FromArgb(31, 248, 136));
|
||||
RejectBrush = new SolidBrush(Color.FromArgb(255, 100, 92));
|
||||
CurrIndexBrush = new SolidBrush(Color.FromArgb(26, 27, 46));
|
||||
SelectCellBorderPen = new Pen(SelectColor, 1);
|
||||
EmptyCellBorderPen = new Pen(Color.FromArgb(116, 144, 0), 1)
|
||||
{
|
||||
DashStyle = System.Drawing.Drawing2D.DashStyle.Custom,
|
||||
DashPattern = new float[2] { 4, 4 }
|
||||
};
|
||||
SelectedCellBorderPen = new Pen(CodeColor, 1)
|
||||
{
|
||||
DashStyle = System.Drawing.Drawing2D.DashStyle.Custom,
|
||||
DashPattern = new float[2] { 3, 3 }
|
||||
DashPattern = new float[2] { 6, 6 },
|
||||
DashOffset = 3F
|
||||
};
|
||||
SelectedCellBorderPen = new Pen(CodeColor, 1);
|
||||
DefaultBorderPen = new Pen(CodeColor, 1);
|
||||
}
|
||||
|
||||
public Font CodeFontA { get; set; }
|
||||
public Font CodeFontB { get; set; }
|
||||
public Size CellSizeA { get; set; }
|
||||
public Size CellSizeB { get; set; }
|
||||
public Color BackColor { get; set; }
|
||||
public Color CodeColor { get; set; }
|
||||
public Color SelectColor { get; set; }
|
||||
public Brush CodeBrush { get; set; }
|
||||
public Brush SelectBrush { get; set; }
|
||||
public Brush ToBeSelectBrush { get; set; }
|
||||
public Brush EmptyCellBrush { get; set; }
|
||||
public Brush DefaultLineBackColor { get; set; }
|
||||
public Brush SelectLineBackColor { get; set; }
|
||||
public Brush DefaultLineBrush { get; set; }
|
||||
public Brush SelectLineBrush { get; set; }
|
||||
public Brush PassBrush { get; set; }
|
||||
public Brush RejectBrush { get; set; }
|
||||
public Brush CurrIndexBrush { get; set; }
|
||||
public Pen SelectCellBorderPen { get; set; }
|
||||
public Pen EmptyCellBorderPen { get; set; }
|
||||
public Pen SelectedCellBorderPen { get; set; }
|
||||
|
@ -6,10 +6,7 @@ namespace CodeMatrix
|
||||
{
|
||||
internal class UCCodeMatrix : Control
|
||||
{
|
||||
private static readonly byte[] Codes = new byte[] { 0x55, 0x1C, 0xBD, 0xE9, 0x7A };
|
||||
|
||||
private static readonly Random Random = new Random();
|
||||
public SizeF CellSize { get; set; }
|
||||
public Size CellSize { get; set; }
|
||||
public int Columns { get; set; }
|
||||
public int Rows { get; set; }
|
||||
public byte[,] Matrix { get; set; }
|
||||
@ -58,16 +55,16 @@ namespace CodeMatrix
|
||||
|
||||
private void InitData()
|
||||
{
|
||||
Rows = 7;
|
||||
Columns = 7;
|
||||
CellSize = new SizeF(40, 40);
|
||||
Rows = 5;
|
||||
Columns = 5;
|
||||
_currDir = Directions.Horizontal;
|
||||
HoverPoint = new Point(-1, -1);
|
||||
}
|
||||
|
||||
private void InitComponent()
|
||||
{
|
||||
MinimumSize = new Size((int)(CellSize.Width * Columns + 100), (int)(CellSize.Height * Rows + 10));
|
||||
CellSize = Styles.Default.CellSizeA;
|
||||
MinimumSize = new Size(CellSize.Width * Columns + 100, CellSize.Height * Rows + 10);
|
||||
Margin = Padding.Empty;
|
||||
DoubleBuffered = true;
|
||||
|
||||
@ -93,7 +90,7 @@ namespace CodeMatrix
|
||||
Matrix = new byte[Columns, Rows];
|
||||
for (int col = 0; col < Columns; col++)
|
||||
for (int row = 0; row < Rows; row++)
|
||||
Matrix[col, row] = Codes[Random.Next(Codes.Length)];
|
||||
Matrix[col, row] = Common.Codes[Common.Random.Next(Common.Codes.Length)];
|
||||
|
||||
var blockSize = new SizeF(Columns*CellSize.Width, Rows*CellSize.Height);
|
||||
var blockOffset = new PointF((Width-blockSize.Width)/2, (Height-blockSize.Height)/2);
|
||||
@ -108,9 +105,9 @@ namespace CodeMatrix
|
||||
|
||||
var offset = new PointF(SelectPoint.X*CellSize.Width, SelectPoint.Y*CellSize.Height);
|
||||
if (_currDir == Directions.Horizontal)
|
||||
e.Graphics.FillRectangle(Styles.Default.DefaultLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
|
||||
e.Graphics.FillRectangle(Styles.Default.DefaultLineBrush, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
|
||||
else if (_currDir == Directions.Vertical)
|
||||
e.Graphics.FillRectangle(Styles.Default.DefaultLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
|
||||
e.Graphics.FillRectangle(Styles.Default.DefaultLineBrush, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
|
||||
|
||||
if (HoverPoint.X >= 0)
|
||||
{
|
||||
@ -118,18 +115,18 @@ namespace CodeMatrix
|
||||
if (_currDir == Directions.Horizontal)
|
||||
{
|
||||
offset.X = HoverPoint.X * CellSize.Width;
|
||||
e.Graphics.FillRectangle(Styles.Default.SelectLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
|
||||
e.Graphics.FillRectangle(Styles.Default.SelectLineBrush, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
|
||||
}
|
||||
else if (_currDir == Directions.Vertical)
|
||||
{
|
||||
offset.Y = HoverPoint.Y * CellSize.Height;
|
||||
e.Graphics.FillRectangle(Styles.Default.SelectLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
|
||||
e.Graphics.FillRectangle(Styles.Default.SelectLineBrush, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
|
||||
}
|
||||
|
||||
offset.X += CodeMatrixRect.X;
|
||||
offset.Y += CodeMatrixRect.Y;
|
||||
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X, offset.Y, CellSize.Width - 1, CellSize.Height - 1);
|
||||
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X + 4, offset.Y + 4, CellSize.Width - 8 - 1, CellSize.Height - 8 - 1);
|
||||
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X + 3, offset.Y + 3, CellSize.Width - 6 - 1, CellSize.Height - 6 - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -180,14 +177,15 @@ namespace CodeMatrix
|
||||
if (CursorPoint != current)
|
||||
{
|
||||
CursorPoint = current;
|
||||
var hoverPoint = new Point(-1, -1);
|
||||
if (Matrix[CursorPoint.X, CursorPoint.Y] != 0)
|
||||
{
|
||||
if (_currDir == Directions.Horizontal && CursorPoint.Y == SelectPoint.Y)
|
||||
hoverPoint = CursorPoint;
|
||||
else if (_currDir == Directions.Vertical && CursorPoint.X == SelectPoint.X)
|
||||
hoverPoint = CursorPoint;
|
||||
}
|
||||
Point hoverPoint;
|
||||
if (_currDir == Directions.Vertical)
|
||||
hoverPoint = new Point(SelectPoint.X, CursorPoint.Y);
|
||||
else
|
||||
hoverPoint = new Point(CursorPoint.X, SelectPoint.Y);
|
||||
|
||||
if (Matrix[hoverPoint.X, hoverPoint.Y] == 0)
|
||||
hoverPoint = new Point(-1, -1);
|
||||
|
||||
if (HoverPoint != hoverPoint)
|
||||
{
|
||||
HoverPoint = hoverPoint;
|
||||
@ -211,9 +209,9 @@ namespace CodeMatrix
|
||||
if (HoverPoint.X >= 0)
|
||||
{
|
||||
SelectPoint = HoverPoint;
|
||||
HoverPoint = new Point(-1, -1);
|
||||
_currDir = _currDir == Directions.Horizontal ? Directions.Vertical : Directions.Horizontal;
|
||||
CodeSelectedEvent?.Invoke(this, Matrix[SelectPoint.X, SelectPoint.Y]);
|
||||
HoverPoint = CursorPoint = new Point(-1, -1);
|
||||
Matrix[SelectPoint.X, SelectPoint.Y] = 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
@ -34,15 +34,14 @@ namespace CodeMatrix
|
||||
|
||||
private void InitData()
|
||||
{
|
||||
CellSize = new Size(25, 25);
|
||||
BufferSize = 7;
|
||||
|
||||
BufferSize = 4;
|
||||
CurrIndex = 0;
|
||||
HoverCode = 0;
|
||||
}
|
||||
|
||||
private void InitComponent()
|
||||
{
|
||||
CellSize = Styles.Default.CellSizeB;
|
||||
Margin = Padding.Empty;
|
||||
Padding = new Padding(10);
|
||||
CellMargin = new Padding(3);
|
||||
@ -75,18 +74,20 @@ namespace CodeMatrix
|
||||
|
||||
if (CurrIndex < BufferSize)
|
||||
{
|
||||
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, cellOffset);
|
||||
int i = CurrIndex;
|
||||
if (HoverCode > 0)
|
||||
{
|
||||
i++;
|
||||
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, cellOffset);
|
||||
var code = HoverCode.ToString("X2");
|
||||
var codeSize = e.Graphics.MeasureString(code, Font);
|
||||
var codeOffset = new PointF((CellSize.Width-codeSize.Width)/2, (CellSize.Height-codeSize.Height)/2);
|
||||
var codePoint = new PointF(codeOffset.X+cellOffset.X, codeOffset.Y+cellOffset.Y);
|
||||
e.Graphics.DrawString(code, Font, Styles.Default.SelectBrush, codePoint);
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
}
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
|
||||
for (int i = CurrIndex+1; i < BufferSize; i++)
|
||||
for (; i < BufferSize; i++)
|
||||
{
|
||||
e.Graphics.DrawRectangle(Styles.Default.EmptyCellBorderPen, cellOffset);
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
@ -102,10 +103,7 @@ namespace CodeMatrix
|
||||
public void InputCode(byte code)
|
||||
{
|
||||
if (CurrIndex < BufferSize)
|
||||
{
|
||||
Buffer[CurrIndex++] = code;
|
||||
HoverCode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
|
162
CodeMatrix/UCCodeTarget.cs
Normal file
162
CodeMatrix/UCCodeTarget.cs
Normal file
@ -0,0 +1,162 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace CodeMatrix
|
||||
{
|
||||
public class UCCodeTarget : Control
|
||||
{
|
||||
public Size CellSize { get; private set; }
|
||||
public Padding CellMargin { get; set; }
|
||||
private byte _HoverCode;
|
||||
public byte HoverCode
|
||||
{
|
||||
get => _HoverCode;
|
||||
set
|
||||
{
|
||||
if (_HoverCode != value)
|
||||
{
|
||||
_HoverCode = value;
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
public event EventHandler<byte> HoverCodeEvent;
|
||||
public int BufferSize { get; set; }
|
||||
public int TargetLength { get; set; }
|
||||
public int OffsetIndex { get; private set; }
|
||||
public int CurrIndex { get; private set; }
|
||||
public byte[] TargetCodes { get; } = new byte[32];
|
||||
|
||||
public enum State
|
||||
{
|
||||
Input,
|
||||
Pass,
|
||||
Reject,
|
||||
}
|
||||
public State CurrState { get; private set; }
|
||||
|
||||
public UCCodeTarget()
|
||||
{
|
||||
InitData();
|
||||
InitComponent();
|
||||
}
|
||||
|
||||
|
||||
private void InitData()
|
||||
{
|
||||
BufferSize = 4;
|
||||
TargetLength = Common.Random.Next(2) + 2;
|
||||
OffsetIndex = 0;
|
||||
CurrIndex = 0;
|
||||
HoverCode = 0;
|
||||
CurrState = State.Input;
|
||||
for (int i = 0; i < TargetLength; i++)
|
||||
TargetCodes[i] = Common.Codes[Common.Random.Next(Common.Codes.Length)];
|
||||
}
|
||||
|
||||
private void InitComponent()
|
||||
{
|
||||
CellSize = Styles.Default.CellSizeB;
|
||||
Margin = Padding.Empty;
|
||||
Padding = new Padding(10, 5, 10, 5);
|
||||
CellMargin = new Padding(3);
|
||||
MinimumSize = new Size((CellSize.Width + CellMargin.Horizontal) * BufferSize + Padding.Horizontal,
|
||||
CellSize.Height + CellMargin.Vertical + Padding.Vertical);
|
||||
DoubleBuffered = true;
|
||||
|
||||
Font = Styles.Default.CodeFontB;
|
||||
BackColor = Styles.Default.BackColor;
|
||||
ForeColor = Styles.Default.CodeColor;
|
||||
}
|
||||
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
if (CurrState == State.Pass)
|
||||
{
|
||||
e.Graphics.FillRectangle(Styles.Default.PassBrush, ClientRectangle);
|
||||
}
|
||||
else if (CurrState == State.Reject)
|
||||
{
|
||||
e.Graphics.FillRectangle(Styles.Default.RejectBrush, ClientRectangle);
|
||||
}
|
||||
else
|
||||
{
|
||||
var cellOffset = new Rectangle(Padding.Left + CellMargin.Left,
|
||||
Padding.Top + CellMargin.Top,
|
||||
CellSize.Width-1,
|
||||
CellSize.Height-1);
|
||||
var cellOffsetWidth = CellSize.Width + CellMargin.Horizontal;
|
||||
cellOffset.X += cellOffsetWidth * OffsetIndex;
|
||||
int i = 0;
|
||||
for (; i < CurrIndex && i < TargetLength; i++)
|
||||
{
|
||||
DrawCode(e.Graphics, TargetCodes[i], Font, Styles.Default.CodeBrush, cellOffset, Styles.Default.SelectedCellBorderPen);
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
}
|
||||
|
||||
e.Graphics.FillRectangle(Styles.Default.CurrIndexBrush, cellOffset.X, 0, cellOffset.Width, this.Height);
|
||||
|
||||
if (i == CurrIndex && HoverCode == TargetCodes[i])
|
||||
{
|
||||
DrawCode(e.Graphics, TargetCodes[CurrIndex], Font, Styles.Default.SelectBrush, cellOffset, Styles.Default.SelectCellBorderPen);
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
i++;
|
||||
}
|
||||
|
||||
for (; i < TargetLength; i++)
|
||||
{
|
||||
DrawCode(e.Graphics, TargetCodes[i], Font, Styles.Default.ToBeSelectBrush, cellOffset);
|
||||
cellOffset.X += cellOffsetWidth;
|
||||
}
|
||||
}
|
||||
base.OnPaint(e);
|
||||
}
|
||||
|
||||
private void DrawCode(Graphics g, byte code, Font font, Brush brush, Rectangle cellRect, Pen borderPen = null)
|
||||
{
|
||||
if (borderPen != null)
|
||||
g.DrawRectangle(borderPen, cellRect);
|
||||
var codeStr = code.ToString("X2");
|
||||
var codeSize = g.MeasureString(codeStr, font);
|
||||
var codeOffset = new PointF((CellSize.Width-codeSize.Width)/2, (CellSize.Height-codeSize.Height)/2);
|
||||
var codePoint = new PointF(codeOffset.X+cellRect.X, codeOffset.Y+cellRect.Y);
|
||||
g.DrawString(codeStr, font, brush, codePoint);
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
|
||||
//HoverCodeEvent?.Invoke(this, _HoverCode);
|
||||
|
||||
|
||||
base.OnMouseMove(e);
|
||||
}
|
||||
|
||||
public void InputCode(byte code)
|
||||
{
|
||||
if (CurrState == State.Input)
|
||||
{
|
||||
if (code == TargetCodes[CurrIndex])
|
||||
CurrIndex++;
|
||||
else
|
||||
OffsetIndex++;
|
||||
|
||||
if (OffsetIndex + TargetLength > BufferSize)
|
||||
CurrState = State.Reject;
|
||||
else if (CurrIndex == TargetLength)
|
||||
CurrState = State.Pass;
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
CurrIndex = 0;
|
||||
HoverCode = 0;
|
||||
}
|
||||
}
|
||||
}
|
BIN
Images/CodeMatrix2.gif
Normal file
BIN
Images/CodeMatrix2.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 95 KiB |
BIN
Images/CodeMatrix3.gif
Normal file
BIN
Images/CodeMatrix3.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
Loading…
Reference in New Issue
Block a user