Skip to content

Commit

Permalink
Completed ornaments
Browse files Browse the repository at this point in the history
  • Loading branch information
dannypas00 committed Jun 6, 2020
1 parent 7b017ba commit 9cee9ec
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 24 deletions.
10 changes: 9 additions & 1 deletion DrawingApp/CommandInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Shapes;
using DrawingApp.CommandPattern;
using DrawingApp.CompositePattern;
using DrawingApp.DecoratorPattern;
using ICommand = DrawingApp.CommandPattern.ICommand;

namespace DrawingApp
Expand All @@ -16,6 +17,7 @@ public class CommandInvoker
private readonly Stack<ICommand> actionsDone = new Stack<ICommand>();
private readonly Stack<ICommand> actionsUndone = new Stack<ICommand>();
private static readonly CommandInvoker Instance = new CommandInvoker();
private DecoratorContext decoratorContext;
public Dictionary<ListBoxItem, IGroupable> GroupMap = new Dictionary<ListBoxItem, IGroupable>();
public MainWindow MainWindow;
public Dictionary<Shape, CanvasShape> Map = new Dictionary<Shape, CanvasShape>();
Expand Down Expand Up @@ -107,7 +109,8 @@ public void AddGroup()
public void StartDraw(double x1, double y1, Shape shape)
{
//Rounding positions to int to comply with mandatory saving grammar
ICommand cmd = new CommandDraw((int) Math.Round(x1), (int) Math.Round(y1), shape);
ICommand cmd = new CommandDraw((int) Math.Round(x1), (int) Math.Round(y1), shape, decoratorContext);
decoratorContext = null;
actionsDone.Push(cmd);
}

Expand Down Expand Up @@ -197,5 +200,10 @@ public static CommandInvoker GetInstance()
{
return Instance;
}

public void Ornament()
{
this.decoratorContext = new DecoratorContext();
}
}
}
15 changes: 11 additions & 4 deletions DrawingApp/CommandPattern/CommandDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ internal class CommandDraw : ICommand
private readonly CommandInvoker invoker;
private readonly Shape shape;
private readonly CanvasShape canvShape;
private readonly IDecorator capDecorator;
public readonly IDecorator capDecorator;
private DecoratorContext context;

public CommandDraw(int x1, int y1, Shape shape)
public CommandDraw(int x1, int y1, Shape shape, DecoratorContext context = null)
{
//Set starting location for drawing the shape
this.x1 = x1;
this.y1 = y1;
this.invoker = CommandInvoker.GetInstance();
this.shape = shape;
this.context = context;
//Get the group that is selected in the group sidebar
Group selected = invoker.MainWindow.groups.SelectedItem != null ? (Group)invoker.GroupMap[(ListBoxItem)invoker.MainWindow.groups.SelectedItem] : (Group)invoker.MainWindow.groups.Items[0];
//Make a new CanvasShape for calling functions on the new shape
Expand All @@ -42,9 +44,14 @@ public CommandDraw(int x1, int y1, Shape shape)
//Setup visuals
shape.Stroke = shape.Fill = CommandInvoker.RandomColor();
shape.StrokeThickness = 3;
capDecorator = new CaptionDecorator(new DecoratorContext(canvShape.GetPosition(), "bottom", canvShape));
if (context != null)
{
context.shape = canvShape;
capDecorator = new CaptionDecorator(context);
}
canvShape.decorator = capDecorator;
invoker.MainWindow.canvas.Children.Add(shape);
invoker.MainWindow.AddOrnament.Background = Brushes.DimGray;
}

public void Execute()
Expand All @@ -62,7 +69,7 @@ public void Execute()
canvShape.SetPosition(pos);
shape.Width = w;
shape.Height = h;
capDecorator.Draw();
capDecorator?.Draw();

//Update group structure to represent added shape
invoker.UpdateGroups();
Expand Down
27 changes: 15 additions & 12 deletions DrawingApp/CommandPattern/CommandLoad.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using DrawingApp.CompositePattern;
using DrawingApp.DecoratorPattern;

namespace DrawingApp.CommandPattern
{
Expand All @@ -28,6 +30,7 @@ public void Execute()
string[] fileLines = File.ReadAllLines(filePath);
int lineNr = 0;
Dictionary<int, Group> lineGroupmap = new Dictionary<int, Group>();
List<CaptionDecorator> nextCaptions = new List<CaptionDecorator>();
//Start off by clearing the old canvas
invoker.Clear();
invoker.MainWindow.groups.Items.Clear();
Expand Down Expand Up @@ -66,7 +69,7 @@ public void Execute()
for (int i = lineNr - 1; i > -1; i--)
{
//Find first item with a lower depth
if (fileLines[i].Contains("group") && lineGroupmap[i].GetDepth() < depth)
if (fileLines[i].Split(" ").Contains("group") && lineGroupmap[i].GetDepth() < depth)
{
invoker.MainWindow.groups.SelectedItem = lineGroupmap[i].GetGroupItem();
break;
Expand All @@ -83,8 +86,16 @@ public void Execute()
double y = Convert.ToInt32(splitted[2 + depth * 4]);
double w = Convert.ToInt32(splitted[3 + depth * 4]);
double h = Convert.ToInt32(splitted[4 + depth * 4]);
invoker.StartDraw(x, y, new Rectangle());
Rectangle rect = new Rectangle();
invoker.StartDraw(x, y, rect);
invoker.Draw(x + w, y + h);
foreach (CaptionDecorator deco in nextCaptions)
{
deco.context.shape = invoker.Map[rect];
deco.Draw();
invoker.Map[rect].decorator = deco.parent;
}
nextCaptions.Clear();
}
break;
case "ellipse":
Expand All @@ -102,16 +113,8 @@ public void Execute()
lineGroupmap.Add(lineNr, (Group)invoker.GroupMap[(ListBoxItem)invoker.MainWindow.groups.SelectedItem]);
break;
case "ornament":
Point relativeLocation = splitted[1 + depth * 4] switch
{
"top" => new Point(0, 1),
"bottom" => new Point(0, -1),
"left" => new Point(-1, 0),
"right" => new Point(1, 0),
_ => throw new InvalidDataException()
};
throw new NotImplementedException();
//break;
nextCaptions.Add(new CaptionDecorator(new DecoratorContext(default, splitted[1 + depth * 4], null, splitted[2] + depth * 4)));
break;
default:
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion DrawingApp/CommandPattern/CommandMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public void Execute()

int x = Convert.ToInt32(absolutePos.X - offset.X);
int y = Convert.ToInt32(absolutePos.Y - offset.Y);
System.Drawing.Point newPoint = new System.Drawing.Point(x, y);

mainWindow.SetCanvasOffset(newPoint, shape.GetShape());
shape.decorator.Draw();
mainWindow.SetCanvasOffset(new System.Drawing.Point(x, y), shape.GetShape());
}

public void Redo()
Expand Down
8 changes: 4 additions & 4 deletions DrawingApp/DecoratorPattern/CaptionDecorator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;
using System.Net.Mail;
using System.Text;
using System.Windows;
Expand All @@ -12,7 +13,7 @@ namespace DrawingApp.DecoratorPattern
{
class CaptionDecorator : IDecorator
{
private readonly DecoratorContext context;
public readonly DecoratorContext context;
private readonly Caption caption;
private System.Drawing.Point offset;
public IDecorator parent;
Expand All @@ -21,7 +22,7 @@ public CaptionDecorator(DecoratorContext context, IDecorator parent = null)
{
this.context = context;
this.caption = new Caption(context.shape.GetParent());
caption.SetText("Yeet");
caption.SetText(context.captionText);
this.parent = parent;
context.MainWindow.canvas.Children.Add(caption.GetTextBox());
}
Expand All @@ -40,7 +41,7 @@ public void Draw()
offset = new Point(0, (int) MathF.Round((float) context.shape.GetShape().Height));
break;
case "left":
offset = new Point(-8 * caption.GetTextBox().Text.Length, 0);
offset = new Point(-5 * caption.GetTextBox().Text.Length, 0);
caption.GetTextBox().HorizontalContentAlignment = HorizontalAlignment.Right;
break;
case "right":
Expand All @@ -51,7 +52,6 @@ public void Draw()
break;
}
}

//caption.SetText("Yeet");
caption.SetPosition(new Point(context.shape.GetPosition().X + offset.X, context.shape.GetPosition().Y + offset.Y));
}
Expand Down
5 changes: 3 additions & 2 deletions DrawingApp/DecoratorPattern/DecoratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public class DecoratorContext
public CanvasShape shape;
public MainWindow MainWindow = CommandInvoker.GetInstance().MainWindow;
private readonly string[] positions = {"top", "bottom", "left", "right"};
public string captionText;

public DecoratorContext(System.Drawing.Point ShapePosition = new System.Drawing.Point(), string CaptionPosition = "",
CanvasShape shape = null)
CanvasShape shape = null, string captionText = "Enter your text here...")
{

this.CaptionPosition = CaptionPosition;
this.shape = shape;
this.CaptionPosition = positions[CommandInvoker.Rnd.Next(0, 4)];
this.captionText = captionText;
}
}
}
2 changes: 2 additions & 0 deletions DrawingApp/DrawingApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<None Remove="Img\clear.png" />
<None Remove="Img\ellipse.png" />
<None Remove="Img\load.png" />
<None Remove="Img\ornament.png" />
<None Remove="Img\plus.png" />
<None Remove="Img\rectangle.png" />
<None Remove="Img\redo.png" />
Expand All @@ -26,6 +27,7 @@
<Resource Include="Img\load.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Img\ornament.png" />
<Resource Include="Img\plus.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
Expand Down
Binary file added DrawingApp/Img/ornament.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions DrawingApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<Button x:Name="AddGroup" Grid.Column="8" Margin="2,0" Click="AddGroup_Click" Background="DimGray">
<Image Source="Img/plus.png" Margin="0,3"/>
</Button>
<Button x:Name="AddOrnament" Grid.Column="9" Margin="2,0" Click="AddOrnament_Click" Background="DimGray">
<Image Source="Img/ornament.png" Margin="0,3"/>
</Button>
</Grid>
</Grid>
</Border>
Expand Down
9 changes: 9 additions & 0 deletions DrawingApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using DrawingApp.CompositePattern;
using DrawingApp.StrategyPattern;
Expand All @@ -29,6 +30,7 @@ public MainWindow()
{
file = new Group();
invoker = CommandInvoker.GetInstance();
invoker.MainWindow = this;
}

#region Mouse button handling
Expand Down Expand Up @@ -144,11 +146,18 @@ public void SetCanvasOffset(System.Drawing.Point offset, Shape shape)
{
Canvas.SetLeft(shape, offset.X);
Canvas.SetTop(shape, offset.Y);
invoker.Map[shape].SetPosition(offset);
}

public Group GetFile()
{
return file;
}

private void AddOrnament_Click(object sender, RoutedEventArgs e)
{
invoker.Ornament();
AddOrnament.Background = Brushes.DarkGray;
}
}
}

0 comments on commit 9cee9ec

Please sign in to comment.