From 0db4469cf56e50f241240ba2baedd511b61c8b83 Mon Sep 17 00:00:00 2001 From: Lukasz Baldyga Date: Wed, 14 Oct 2020 21:31:43 +0100 Subject: [PATCH] Draw Preview now draws from Text Editor directly --- .../Advanced Software Engineering/Command.cs | 38 +++++--- .../Commander.cs | 94 +++++++++++-------- .../DrawPreview.cs | 21 ++++- .../Text Editor.Designer.cs | 7 +- .../Text Editor.cs | 22 +++++ 5 files changed, 128 insertions(+), 54 deletions(-) diff --git a/Advanced Software Engineering/Advanced Software Engineering/Command.cs b/Advanced Software Engineering/Advanced Software Engineering/Command.cs index 6a7088d..4a8ddf8 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Command.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Command.cs @@ -1,13 +1,6 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Windows.Forms.VisualStyles; -using System.Xml.Serialization; namespace Advanced_Software_Engineering { class VerbFactory { @@ -70,7 +63,7 @@ namespace Advanced_Software_Engineering { public interface Verb { void ExecuteVerb(); - + string GetDescription(); } class MoveTo : Verb { @@ -92,15 +85,27 @@ namespace Advanced_Software_Engineering { this.drawer.MovePen(moveToPoint); } + public string GetDescription() { + return "Move origin to " + moveToPoint.X + ", " + moveToPoint.Y; + } + } - class DrawTo : MoveTo { - public DrawTo(Drawer drawer, int x, int y) : base(drawer, x, y) { } + class DrawTo : Verb { + protected Drawer drawer; + protected Point moveToPoint; - public DrawTo(Drawer drawer, Point point) : base(drawer, point) { } + public DrawTo(Drawer drawer, int x, int y) { + this.drawer = drawer; + moveToPoint = new Point(x, y); + } - public new void ExecuteVerb() { - this.drawer.DrawLine(moveToPoint); + public void ExecuteVerb() { + drawer.DrawLine(moveToPoint); + } + + public string GetDescription() { + return "Draw line " + moveToPoint.X + ", " + moveToPoint.Y; } } @@ -128,6 +133,8 @@ namespace Advanced_Software_Engineering { public void ExecuteVerb() { foreach (Verb verb in lineVerbs) verb.ExecuteVerb(); } + + public abstract string GetDescription(); } /// @@ -139,11 +146,16 @@ namespace Advanced_Software_Engineering { RegularPolygons(Drawer drawer, Point origin, int sides, int scale) { + } public void ExecuteVerb() { } + + public string GetDescription() { + return null; + } } abstract class Value { diff --git a/Advanced Software Engineering/Advanced Software Engineering/Commander.cs b/Advanced Software Engineering/Advanced Software Engineering/Commander.cs index 89874c6..84630e8 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Commander.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Commander.cs @@ -3,80 +3,77 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; -using System.Runtime.Remoting.Messaging; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; -namespace Advanced_Software_Engineering -{ - class Commander - { +namespace Advanced_Software_Engineering { + class Commander { Graphics graphics; List commands = new List(); Drawer drawer; - public void DrawAllCommands() - { - foreach (Verb verb in this.commands) - { + public void DrawAllCommands() { + foreach (Verb verb in this.commands) { verb.ExecuteVerb(); } } - public Commander(Graphics graphics) - { + public Commander(Graphics graphics) { this.graphics = graphics; this.drawer = new Drawer(this.graphics); } - public Commander(Graphics graphics, string rawCommands) - { + public Commander(Graphics graphics, string rawCommands) { this.graphics = graphics; this.drawer = new Drawer(this.graphics); this.ProcessCommands(rawCommands); } - public void AddCommand(Verb command) - { + public void AddCommand(Verb command) { commands.Add(command); } - public void ProcessCommands(string rawCommands) - { + public void ProcessCommands(string rawCommands) { //ignore case rawCommands = rawCommands.ToLower(); + Console.WriteLine("Processing:\n" + rawCommands); //isolate commands string[] commands = rawCommands.Split(Environment.NewLine.ToCharArray()); - foreach (string rawCommand in commands) - { - //handle errors later + Console.WriteLine("\nThe rawCommand processed:"); + foreach (string rawCommand in commands) { + if (rawCommand == "") continue; AddCommand(VerbFactory.MakeVerb(drawer, rawCommand)); + Console.WriteLine(rawCommand); + } + + Console.WriteLine("\n\nHere is what the program is going to do:"); + Console.WriteLine("Set origin to 0, 0"); + foreach(Verb verb in this.commands) { + Console.WriteLine(verb.GetDescription()); } } - public void ProcessCommandsAndExecute(string rawCommands) - { + public void ProcessCommandsAndExecute(string rawCommands) { int start = commands.Count; ProcessCommands(rawCommands); - for (; start < commands.Count; start++) - { + for (; start < commands.Count; start++) { commands[start].ExecuteVerb(); } } + public void RemoveAllCommands() { + commands = new List(); + } + } - class Drawer - { + class Drawer { protected static Color defaultColor = Color.Black; protected static float defaultWidth = 1f; protected bool fill = false; - protected Color fillColor = Color.Black; protected Brush brush; @@ -86,15 +83,13 @@ namespace Advanced_Software_Engineering protected Point penPosition; protected Graphics graphics; - public Drawer(Graphics graphics) - { + public Drawer(Graphics graphics) { this.graphics = graphics; pen = new Pen(defaultColor, defaultWidth); brush = new SolidBrush(defaultColor); } - public void MovePen(Point point) - { + public void MovePen(Point point) { this.penPosition = point; } @@ -103,15 +98,38 @@ namespace Advanced_Software_Engineering public void DisableFill() => fill = true; public void SetFill(bool down) => fill = down; - public void DrawLine(Point point) - { + public void SetPenColor(Color color) => pen.Color = color; + public void SetPenWidth(float width) => pen.Width = width; + + public void SetFillColor(Color color) => ((SolidBrush) brush).Color = color; + + public void DrawLine(Point point) { graphics.DrawLine(pen, penPosition, point); penPosition = point; } - public void DrawPoints(GraphicsPath path) - { - graphics.FillPath(brush, path); + public void DrawLines(Point[] points) { + if (points.Length < 2) { + throw new Exception("Cannot have less than two points in a line"); + } + + GraphicsPath path = new GraphicsPath(); + + for (int i = 1; i < points.Length; i++) { + path.AddLine(points[i - 1], points[i]); + } + + path.AddLine(points[0], points.Last()); + + DrawLines(path); + + } + + //Doesn't move pen + public void DrawLines(GraphicsPath path) { + path.Widen(pen); + if(fill) graphics.FillPath(brush, path); + graphics.DrawPath(pen, path); } } diff --git a/Advanced Software Engineering/Advanced Software Engineering/DrawPreview.cs b/Advanced Software Engineering/Advanced Software Engineering/DrawPreview.cs index 089e283..f8cd222 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/DrawPreview.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/DrawPreview.cs @@ -30,10 +30,21 @@ namespace Advanced_Software_Engineering commander = new Commander(graphics); } + public Draw_Preview(String commands) { + InitializeComponent(); + SettingsAndHelperFunctions.NumberOfWindows++; + graphics = panel1.CreateGraphics(); + commander = new Commander(graphics, commands); + + //disable components + textBox1.Enabled = false; + button1.Enabled = false; + } + private void DrawPreview_FormClosed(object sender, FormClosedEventArgs e) { SettingsAndHelperFunctions.WindowClosed(); - this.Dispose(); + Dispose(); } private void DrawPreviewPane_Paint(object sender, PaintEventArgs e) @@ -46,5 +57,13 @@ namespace Advanced_Software_Engineering commander.ProcessCommandsAndExecute(textBox1.Text); textBox1.Text = ""; } + + public void SubmitCommands(string comamnds) { + commander.ProcessCommandsAndExecute(comamnds); + } + + public void RemoveAllCommands() { + commander.RemoveAllCommands(); + } } } diff --git a/Advanced Software Engineering/Advanced Software Engineering/Text Editor.Designer.cs b/Advanced Software Engineering/Advanced Software Engineering/Text Editor.Designer.cs index 70a082c..9c31685 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Text Editor.Designer.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Text Editor.Designer.cs @@ -109,12 +109,14 @@ namespace Advanced_Software_Engineering this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.exitToolStripMenuItem.Text = "Exit"; + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // runToolStripMenuItem // this.runToolStripMenuItem.Name = "runToolStripMenuItem"; this.runToolStripMenuItem.Size = new System.Drawing.Size(40, 20); this.runToolStripMenuItem.Text = "Run"; + this.runToolStripMenuItem.Click += new System.EventHandler(this.runToolStripMenuItem_Click); // // helpToolStripMenuItem // @@ -128,13 +130,14 @@ namespace Advanced_Software_Engineering // aboutToolStripMenuItem // this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; - this.aboutToolStripMenuItem.Size = new System.Drawing.Size(107, 22); + this.aboutToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.aboutToolStripMenuItem.Text = "About"; + this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); // // helpToolStripMenuItem1 // this.helpToolStripMenuItem1.Name = "helpToolStripMenuItem1"; - this.helpToolStripMenuItem1.Size = new System.Drawing.Size(107, 22); + this.helpToolStripMenuItem1.Size = new System.Drawing.Size(180, 22); this.helpToolStripMenuItem1.Text = "Help"; // // Text_Editor diff --git a/Advanced Software Engineering/Advanced Software Engineering/Text Editor.cs b/Advanced Software Engineering/Advanced Software Engineering/Text Editor.cs index 162b1a8..2211216 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Text Editor.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Text Editor.cs @@ -12,6 +12,8 @@ namespace Advanced_Software_Engineering { public partial class Text_Editor : Form { const string DefaultTitleString = "Advanced Software Engineering - Text Editor "; + Draw_Preview DisplayForm = null; + /// /// The text editor is the main window that the user will be using for writing code. /// @@ -27,5 +29,25 @@ namespace Advanced_Software_Engineering { private void newFileToolStripMenuItem_Click(object sender, EventArgs e) { new Text_Editor().Show(); } + + private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { + new About_Window().Show(); + } + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) { + Close(); + } + + private void runToolStripMenuItem_Click(object sender, EventArgs e) { + if(DisplayForm == null) { + DisplayForm = new Draw_Preview(textBox1.Text); + } else { + Console.WriteLine("Removed all commands"); + DisplayForm.RemoveAllCommands(); + DisplayForm.SubmitCommands(textBox1.Text); + } + DisplayForm.Show(); + DisplayForm.Focus(); + } } }