📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AI_Life
{
enum CurrentSimulation
{
SteeringBehaviours,
EString,
Ants,
}
//this class deals with the main windows form and manages top-level objects
//of other classes
//some of the class components are to help EString work
public partial class mainForm : Form
{
//=========================================================//
public static bool behaviourChanged = false, identicalBehaviour = true, eStringRunning = true;
public static bool leaveTrail = false;
public static bool identicalVehicles = false;
internal static SB steeringBehaviour = SB.None;
//=========================================================//
private Point[] axisPoints = new Point[3];
private Point newPoint, oldPoint = new Point(80, 450 - 200);
private SBC sBC;
private Cosmos world;
private AboutBox aboutBox;
private Pen arrowPen = new Pen(Brushes.Red, 3), boldPen = new Pen(Brushes.PowderBlue, 2);
private CurrentSimulation CS;
private BufferedGraphicsContext context; //buffered display; check msdn for this class
private BufferedGraphics grafx;
private Random random = new Random();
private bool drawingSurfaceInitialized = false;
Graphics g;
//=========================================================//
private PointF eStringGraph = new PointF(100 + EString.generationCount, 500 - EString.errorCount);
public mainForm()
{
InitializeComponent();
arrowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
axisPoints[1] = new Point(80, 450);
axisPoints[0] = new Point(80, 150);
axisPoints[2] = new Point(380, 450);
CS = CurrentSimulation.SteeringBehaviours;
context = new BufferedGraphicsContext();
}
//main title
private void DrawTitle()
{
switch (CS)
{
case CurrentSimulation.SteeringBehaviours:
g.DrawString("Steering Behaviours", this.Font, Brushes.White, new PointF(mainPanel.Width - 140, 10));
break;
case CurrentSimulation.EString:
//g.DrawString("E Strings", this.Font, Brushes.White, new PointF(mainPanel.Width / 2, 10));
break;
case CurrentSimulation.Ants:
//g.DrawString("Sheeps", this.Font, Brushes.White, new PointF(mainPanel.Width / 2, 10));
break;
default:
break;
}
}
private void ClearScreen()
{
g.FillRectangle(Brushes.Black, mainPanel.ClientRectangle);
}
private void Start()
{
if (g != null)
{
timer1.Enabled = true;
if (CS == CurrentSimulation.EString)
{
listView1.Items.Clear();
if (textBoxTargetString.Text != "")
{
{
EString.target = textBoxTargetString.Text;
EString.Execute(g, listView1);
}
}
}
timer1.Enabled = true;
}
}
//to accommodate different resolutions
private void InitializeDrawingSurface()
{
listView1.Visible = false;
context.MaximumBuffer = mainPanel.ClientSize;
grafx = context.Allocate(mainPanel.CreateGraphics(), new Rectangle(0, 0, mainPanel.ClientSize.Width, mainPanel.ClientSize.Height));
g = grafx.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
drawingSurfaceInitialized = true;
}
private void DestroyAll()
{
leaveTrailToolStripMenuItem.Checked = false;
leaveTrail = false;
timer1.Enabled = false;
Cosmos.isRunning = false;
eStringRunning = false;
listView1.Visible = false;
sBC = null;
world = null;
}
#region Events
private void timer1_Tick(object sender, EventArgs e)
{
if (!leaveTrail)
{
ClearScreen();
}
switch (CS)
{
case CurrentSimulation.SteeringBehaviours:
if (sBC != null)
sBC.Step();
break;
case CurrentSimulation.EString:
newPoint = new Point(80 + (EString.generationCount * 2), 450 - (EString.errorCount * 2));
g.DrawLine(boldPen, oldPoint, newPoint);
oldPoint = newPoint;
g.DrawString("E Strings", new Font(FontFamily.GenericSansSerif, 14), Brushes.White, new PointF(20, 20));
g.DrawString("Target String: " + EString.target, new Font(FontFamily.GenericSansSerif, 20), Brushes.PaleGoldenrod, new PointF(20, 50));
g.DrawString("Error Graph", new Font(FontFamily.GenericSansSerif, 20), Brushes.Yellow, new PointF(180, 180));
g.DrawLine(arrowPen, axisPoints[1], axisPoints[0]);
g.DrawLine(arrowPen, axisPoints[1], axisPoints[2]);
g.DrawString("Y", this.Font, Brushes.White, new PointF(axisPoints[0].X - 30, axisPoints[0].Y));
g.DrawString("X", this.Font, Brushes.White, new PointF(axisPoints[2].X - 10, axisPoints[2].Y + 25));
g.DrawString("0", this.Font, Brushes.White, new PointF(axisPoints[1].X - 10, axisPoints[1].Y + 10));
break;
case CurrentSimulation.Ants:
if (world != null)
{
world.Step();
}
break;
default:
break;
}
DrawTitle();
grafx.Render(Graphics.FromHwnd(mainPanel.Handle));
}
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
Start();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
eStringRunning = false;
}
private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
{
Cosmos.isRunning = false;
eStringRunning = false;
timer1.Enabled = !timer1.Enabled;
}
private void initializeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!drawingSurfaceInitialized)
InitializeDrawingSurface();
DestroyAll();
listView1.Visible = false;
CS = CurrentSimulation.SteeringBehaviours;
sBC = new SBC(g, mainPanel.ClientSize, steeringBehaviour, int.Parse(numberOfCars.Text));
Start();
}
private void mainPanel_MouseClick(object sender, MouseEventArgs e)
{
SBC.targetPosition = new Vector2(e.X, e.Y);
}
private void initializeMainToolStripMenuItem_Click(object sender, EventArgs e)
{
listView1.Visible = false;
context.MaximumBuffer = mainPanel.ClientSize;
grafx = context.Allocate(mainPanel.CreateGraphics(), new Rectangle(0, 0, mainPanel.ClientSize.Width, mainPanel.ClientSize.Height));
g = grafx.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}
private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Seek;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Seek;
}
private void fleeToolStripMenuItem1_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Flee;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Flee;
}
private void arriveToolStripMenuItem1_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Arrive;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Arrive;
}
private void pursuitToolStripMenuItem_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Pursuit;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Pursuit;
}
private void evadeToolStripMenuItem1_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Evade;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Evade;
}
private void wanderToolStripMenuItem_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.Wander;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.Wander;
}
private void pathFollowingToolStripMenuItem1_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.PathFollowing;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.PathFollowing;
}
private void noneToolStripMenuItem_Click(object sender, EventArgs e)
{
behaviourChanged = true;
steeringBehaviour = SB.None;
if (sBC != null)
sBC.FirstVehicle.SteeringBehaviour = SB.None;
}
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
switch (CS)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -