Draw Preview now draws from Text Editor directly

This commit is contained in:
Lukasz Baldyga committed 2020-10-14 21:31:43 +01:00
1 parent 8a63dbae2f
commit 0db4469cf5
5 files changed
+128 -54

No files matched your search

@@ -1,13 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing; 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 { namespace Advanced_Software_Engineering {
class VerbFactory { class VerbFactory {
@@ -70,7 +63,7 @@ namespace Advanced_Software_Engineering {
public interface Verb { public interface Verb {
void ExecuteVerb(); void ExecuteVerb();
string GetDescription();
} }
class MoveTo : Verb { class MoveTo : Verb {
@@ -92,15 +85,27 @@ namespace Advanced_Software_Engineering {
this.drawer.MovePen(moveToPoint); this.drawer.MovePen(moveToPoint);
} }
public string GetDescription() {
return "Move origin to " + moveToPoint.X + ", " + moveToPoint.Y;
}
} }
class DrawTo : MoveTo { class DrawTo : Verb {
public DrawTo(Drawer drawer, int x, int y) : base(drawer, x, y) { } 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() { public void ExecuteVerb() {
this.drawer.DrawLine(moveToPoint); drawer.DrawLine(moveToPoint);
}
public string GetDescription() {
return "Draw line " + moveToPoint.X + ", " + moveToPoint.Y;
} }
} }
@@ -128,6 +133,8 @@ namespace Advanced_Software_Engineering {
public void ExecuteVerb() { public void ExecuteVerb() {
foreach (Verb verb in lineVerbs) verb.ExecuteVerb(); foreach (Verb verb in lineVerbs) verb.ExecuteVerb();
} }
public abstract string GetDescription();
} }
/// <summary> /// <summary>
@@ -139,11 +146,16 @@ namespace Advanced_Software_Engineering {
RegularPolygons(Drawer drawer, Point origin, int sides, int scale) { RegularPolygons(Drawer drawer, Point origin, int sides, int scale) {
} }
public void ExecuteVerb() { public void ExecuteVerb() {
} }
public string GetDescription() {
return null;
}
} }
abstract class Value { abstract class Value {
@@ -3,80 +3,77 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Linq; using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace Advanced_Software_Engineering namespace Advanced_Software_Engineering {
{ class Commander {
class Commander
{
Graphics graphics; Graphics graphics;
List<Verb> commands = new List<Verb>(); List<Verb> commands = new List<Verb>();
Drawer drawer; Drawer drawer;
public void DrawAllCommands() public void DrawAllCommands() {
{ foreach (Verb verb in this.commands) {
foreach (Verb verb in this.commands)
{
verb.ExecuteVerb(); verb.ExecuteVerb();
} }
} }
public Commander(Graphics graphics) public Commander(Graphics graphics) {
{
this.graphics = graphics; this.graphics = graphics;
this.drawer = new Drawer(this.graphics); this.drawer = new Drawer(this.graphics);
} }
public Commander(Graphics graphics, string rawCommands) public Commander(Graphics graphics, string rawCommands) {
{
this.graphics = graphics; this.graphics = graphics;
this.drawer = new Drawer(this.graphics); this.drawer = new Drawer(this.graphics);
this.ProcessCommands(rawCommands); this.ProcessCommands(rawCommands);
} }
public void AddCommand(Verb command) public void AddCommand(Verb command) {
{
commands.Add(command); commands.Add(command);
} }
public void ProcessCommands(string rawCommands) public void ProcessCommands(string rawCommands) {
{
//ignore case //ignore case
rawCommands = rawCommands.ToLower(); rawCommands = rawCommands.ToLower();
Console.WriteLine("Processing:\n" + rawCommands);
//isolate commands //isolate commands
string[] commands = rawCommands.Split(Environment.NewLine.ToCharArray()); string[] commands = rawCommands.Split(Environment.NewLine.ToCharArray());
foreach (string rawCommand in commands) Console.WriteLine("\nThe rawCommand processed:");
{ foreach (string rawCommand in commands) {
//handle errors later if (rawCommand == "") continue;
AddCommand(VerbFactory.MakeVerb(drawer, rawCommand)); 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; int start = commands.Count;
ProcessCommands(rawCommands); ProcessCommands(rawCommands);
for (; start < commands.Count; start++) for (; start < commands.Count; start++) {
{
commands[start].ExecuteVerb(); commands[start].ExecuteVerb();
} }
} }
public void RemoveAllCommands() {
commands = new List<Verb>();
}
} }
class Drawer class Drawer {
{
protected static Color defaultColor = Color.Black; protected static Color defaultColor = Color.Black;
protected static float defaultWidth = 1f; protected static float defaultWidth = 1f;
protected bool fill = false; protected bool fill = false;
protected Color fillColor = Color.Black;
protected Brush brush; protected Brush brush;
@@ -86,15 +83,13 @@ namespace Advanced_Software_Engineering
protected Point penPosition; protected Point penPosition;
protected Graphics graphics; protected Graphics graphics;
public Drawer(Graphics graphics) public Drawer(Graphics graphics) {
{
this.graphics = graphics; this.graphics = graphics;
pen = new Pen(defaultColor, defaultWidth); pen = new Pen(defaultColor, defaultWidth);
brush = new SolidBrush(defaultColor); brush = new SolidBrush(defaultColor);
} }
public void MovePen(Point point) public void MovePen(Point point) {
{
this.penPosition = point; this.penPosition = point;
} }
@@ -103,15 +98,38 @@ namespace Advanced_Software_Engineering
public void DisableFill() => fill = true; public void DisableFill() => fill = true;
public void SetFill(bool down) => fill = down; 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); graphics.DrawLine(pen, penPosition, point);
penPosition = point; penPosition = point;
} }
public void DrawPoints(GraphicsPath path) public void DrawLines(Point[] points) {
{ if (points.Length < 2) {
graphics.FillPath(brush, path); 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);
} }
} }
@@ -30,10 +30,21 @@ namespace Advanced_Software_Engineering
commander = new Commander(graphics); 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) private void DrawPreview_FormClosed(object sender, FormClosedEventArgs e)
{ {
SettingsAndHelperFunctions.WindowClosed(); SettingsAndHelperFunctions.WindowClosed();
this.Dispose(); Dispose();
} }
private void DrawPreviewPane_Paint(object sender, PaintEventArgs e) private void DrawPreviewPane_Paint(object sender, PaintEventArgs e)
@@ -46,5 +57,13 @@ namespace Advanced_Software_Engineering
commander.ProcessCommandsAndExecute(textBox1.Text); commander.ProcessCommandsAndExecute(textBox1.Text);
textBox1.Text = ""; textBox1.Text = "";
} }
public void SubmitCommands(string comamnds) {
commander.ProcessCommandsAndExecute(comamnds);
}
public void RemoveAllCommands() {
commander.RemoveAllCommands();
}
} }
} }
@@ -109,12 +109,14 @@ namespace Advanced_Software_Engineering
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.exitToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.exitToolStripMenuItem.Text = "Exit"; this.exitToolStripMenuItem.Text = "Exit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
// //
// runToolStripMenuItem // runToolStripMenuItem
// //
this.runToolStripMenuItem.Name = "runToolStripMenuItem"; this.runToolStripMenuItem.Name = "runToolStripMenuItem";
this.runToolStripMenuItem.Size = new System.Drawing.Size(40, 20); this.runToolStripMenuItem.Size = new System.Drawing.Size(40, 20);
this.runToolStripMenuItem.Text = "Run"; this.runToolStripMenuItem.Text = "Run";
this.runToolStripMenuItem.Click += new System.EventHandler(this.runToolStripMenuItem_Click);
// //
// helpToolStripMenuItem // helpToolStripMenuItem
// //
@@ -128,13 +130,14 @@ namespace Advanced_Software_Engineering
// aboutToolStripMenuItem // aboutToolStripMenuItem
// //
this.aboutToolStripMenuItem.Name = "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.Text = "About";
this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
// //
// helpToolStripMenuItem1 // helpToolStripMenuItem1
// //
this.helpToolStripMenuItem1.Name = "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"; this.helpToolStripMenuItem1.Text = "Help";
// //
// Text_Editor // Text_Editor
@@ -12,6 +12,8 @@ namespace Advanced_Software_Engineering {
public partial class Text_Editor : Form { public partial class Text_Editor : Form {
const string DefaultTitleString = "Advanced Software Engineering - Text Editor "; const string DefaultTitleString = "Advanced Software Engineering - Text Editor ";
Draw_Preview DisplayForm = null;
/// <summary> /// <summary>
/// The text editor is the main window that the user will be using for writing code. /// The text editor is the main window that the user will be using for writing code.
/// </summary> /// </summary>
@@ -27,5 +29,25 @@ namespace Advanced_Software_Engineering {
private void newFileToolStripMenuItem_Click(object sender, EventArgs e) { private void newFileToolStripMenuItem_Click(object sender, EventArgs e) {
new Text_Editor().Show(); 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();
}
} }
} }