using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace Inteaktivni { public partial class Form1 : Form { int mult = 1; Net n = null; List netStates = null; string input = null; int state = -1; Size imgSize; Pen backWhite = new Pen(Brushes.White, 3); Font f = new Font("Arial", 7, FontStyle.Regular, GraphicsUnit.Point); public Form1() { InitializeComponent(); } private void ntnLoadXml_Click(object sender, EventArgs e) { if (ofd.ShowDialog(this) != DialogResult.OK) return; Program.MainPath = Path.GetDirectoryName(ofd.FileName); n = Net.ParseNet(Path.GetFileName(ofd.FileName)); netStates = new List(); if (n != null && input != null) go(); } private void go() { prepareNetStates(); state = 0; imgSize = getImgSize(); refreshState(); btnBack.Enabled = true; btnForward.Enabled = true; } private void redrawNet(Net n) { Bitmap bmp = new Bitmap(imgSize.Width, imgSize.Height); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; drawNetLines(g, n); foreach (Pin p in n.Inputs) drawPin(g, p); foreach (Pin p in n.Outputs) drawPin(g, p); foreach (Gate gate in n.Gates) drawGate(g, gate); pbImage.Image = bmp; } private void drawNetLines(Graphics g, Net n) { foreach (Pin p in n.Outputs) { Pin p2 = n.pins[p.Id]; drawLine(g, p.X + p.W / 2, p.Y + p.H / 2, p2.X + p2.W / 2, p2.Y + p2.H / 2); } foreach (Gate gate in n.Gates) { foreach (Pin p in gate.Inputs) { Pin p2 = n.pins[p.Id]; drawLine(g, p.X + p.W / 2, p.Y + p.H / 2, p2.X + p2.W / 2, p2.Y + p2.H / 2); } } } private void drawLine(Graphics g, int x1, int y1, int x2, int y2) { g.DrawLine(backWhite, x1 * mult, y1 * mult, x2 * mult, y2 * mult); g.DrawLine(Pens.Black, x1 * mult, y1 * mult, x2 * mult, y2 * mult); } private void drawPin(Graphics g, Pin p) { g.FillRectangle(Brushes.White, p.X * mult, p.Y * mult, p.W * mult, p.H * mult); g.DrawRectangle(Pens.Gray, p.X * mult, p.Y * mult, p.W * mult, p.H * mult); string txt = p.Id.ToString() + ": " + p.Value.ToString(); SizeF size = g.MeasureString(txt, f); g.DrawString(txt, f, Brushes.Black, new PointF((p.X + p.W / 2) * mult - size.Width / 2, (p.Y + p.H / 2) * mult - size.Height / 2)); } private void drawGate(Graphics g, Gate gate) { g.FillRectangle(Brushes.White, gate.X * mult - 2, gate.Y * mult - 2, gate.W * mult + 4, gate.H * mult + 4); g.DrawRectangle(Pens.DarkGray, gate.X * mult, gate.Y * mult, gate.W * mult, gate.H * mult); foreach (Pin p in gate.Inputs) drawPin(g, p); foreach (Pin p in gate.Outputs) drawPin(g, p); string txt = gate.GType.ToString(); SizeF size = g.MeasureString(txt, f); g.DrawString(txt, f, Brushes.Black, new PointF((gate.X + gate.W / 2) * mult - size.Width / 2, (gate.Y + gate.H / 2) * mult - size.Height / 2)); } private Size getImgSize() { int x = 0, y = 0; foreach (Pin p in n.Inputs) { if (p.X + p.W > x) x = p.X + p.W; if (p.Y + p.H > y) y = p.Y + p.H; } foreach (Pin p in n.Outputs) { if (p.X + p.W > x) x = p.X + p.W; if (p.Y + p.H > y) y = p.Y + p.H; } foreach (Gate g in n.Gates) { if (g.X + g.W > x) x = g.X + g.W; if (g.Y + g.H > y) y = g.Y + g.H; foreach (Pin p in g.Inputs) { if (p.X + p.W > x) x = p.X + p.W; if (p.Y + p.H > y) y = p.Y + p.H; } foreach (Pin p in g.Outputs) { if (p.X + p.W > x) x = p.X + p.W; if (p.Y + p.H > y) y = p.Y + p.H; } } x++; y++; x *= mult; y *= mult; pbImage.Width = x; pbImage.Height = y; return new Size(x, y); } private void prepareNetStates() { bool[] i = new bool[input.Length]; for (int j = 0; j < input.Length; j++) // naplnění vstupů i[j] = input[j] == '1' ? true : false; netStates = n.SolveInterativeNet(i); // vyřešení sítě n.resetNet(); // vymazání stavů } private void btnLoadInputs_Click(object sender, EventArgs e) { if (ofd.ShowDialog(this) != DialogResult.OK) return; string[] i = File.ReadAllLines(ofd.FileName); if (i.Length < 1) { MessageBox.Show(this, "Neplatny soubor se vstupy"); return; } input = i[0]; if (n != null && input != null) go(); } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { mult = (int)nudZoom.Value; if (state >= 0) { imgSize = getImgSize(); redrawNet(netStates[state]); } } private void btnBack_Click(object sender, EventArgs e) { if (state > 0) state--; refreshState(); } private void btnForward_Click(object sender, EventArgs e) { if (state < netStates.Count - 1) state++; refreshState(); } private void refreshState() { redrawNet(netStates[state]); lblState.Text = (state + 1).ToString() + "/" + netStates.Count.ToString(); } } }