Skip to content

Commit

Permalink
Moving decorators is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
dannypas00 committed Jun 6, 2020
1 parent 5bf79e9 commit 7b017ba
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 15 deletions.
4 changes: 3 additions & 1 deletion DrawingApp/CommandPattern/CommandClear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public void Execute()
{
for (int i = invoker.MainWindow.canvas.Children.Count - 1; i > -1; i--)
{
invoker.Map.Remove((Shape)invoker.MainWindow.canvas.Children[i]);
var item = invoker.MainWindow.canvas.Children[i];
if (item is Shape sItem) invoker.Map.Remove(sItem);
invoker.MainWindow.canvas.Children.RemoveAt(i);
}
invoker.UpdateGroups();
}

public void Redo()
Expand Down
1 change: 1 addition & 0 deletions DrawingApp/CommandPattern/CommandDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public CommandDraw(int x1, int y1, Shape shape)
shape.Stroke = shape.Fill = CommandInvoker.RandomColor();
shape.StrokeThickness = 3;
capDecorator = new CaptionDecorator(new DecoratorContext(canvShape.GetPosition(), "bottom", canvShape));
canvShape.decorator = capDecorator;
invoker.MainWindow.canvas.Children.Add(shape);
}

Expand Down
1 change: 1 addition & 0 deletions DrawingApp/CommandPattern/CommandMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void Execute()
int x = Convert.ToInt32(absolutePos.X - offset.X);
int y = Convert.ToInt32(absolutePos.Y - offset.Y);

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

Expand Down
2 changes: 2 additions & 0 deletions DrawingApp/CompositePattern/CanvasShape.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Drawing;
using System.Windows.Controls;
using System.Windows.Shapes;
using DrawingApp.DecoratorPattern;
using DrawingApp.VisitorPattern;
using Rectangle = System.Windows.Shapes.Rectangle;

Expand All @@ -18,6 +19,7 @@ public class CanvasShape : IGroupable
private readonly int depth = 0;
private ListBoxItem groupItem;
private Point position;
public IDecorator decorator;

public CanvasShape(Shape shape, Group parent)
{
Expand Down
6 changes: 4 additions & 2 deletions DrawingApp/CompositePattern/Caption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public Caption(Group parent)
{
this.parent = parent;
Canvas.SetZIndex(text, 5);
text.Background = Brushes.Transparent;
text.BorderBrush = Brushes.Transparent;
}

public void SetText(string text)
Expand All @@ -39,8 +41,8 @@ public void SetPosition(Point position)
{
this.position = position;
text.Margin = new Thickness(position.X, position.Y, 0, 0);
/*Canvas.SetLeft(text, position.X);
Canvas.SetTop(text, position.Y);*/
//Canvas.SetLeft(text, position.X);
//Canvas.SetTop(text, position.Y);
}

public Point GetPosition()
Expand Down
38 changes: 34 additions & 4 deletions DrawingApp/DecoratorPattern/CaptionDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,57 @@
using System.Diagnostics;
using System.Net.Mail;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using DrawingApp.CompositePattern;
using Point = System.Drawing.Point;

namespace DrawingApp.DecoratorPattern
{
class CaptionDecorator : IDecorator
{
private readonly DecoratorContext context;
private readonly Caption caption;
private System.Drawing.Point offset;
public IDecorator parent;

public CaptionDecorator(DecoratorContext context)
public CaptionDecorator(DecoratorContext context, IDecorator parent = null)
{
this.context = context;
this.caption = new Caption(context.parent.GetParent());
this.caption = new Caption(context.shape.GetParent());
caption.SetText("Yeet");
this.parent = parent;
context.MainWindow.canvas.Children.Add(caption.GetTextBox());
}

public void Draw()
{
caption.SetText("Yeet");
caption.SetPosition(context.parent.GetPosition());
if (caption.GetTextBox() != null || caption.GetTextBox().Width > 0 ||
caption.GetTextBox().Height > 0)
{
switch (context.CaptionPosition)
{
case "top":
offset = new Point(0, (int) MathF.Round((float) -20));
break;
case "bottom":
offset = new Point(0, (int) MathF.Round((float) context.shape.GetShape().Height));
break;
case "left":
offset = new Point(-8 * caption.GetTextBox().Text.Length, 0);
caption.GetTextBox().HorizontalContentAlignment = HorizontalAlignment.Right;
break;
case "right":
offset = new Point((int) MathF.Round((float) context.shape.GetShape().Width), 0);
break;
default:
offset = new Point(0, 0);
break;
}
}

//caption.SetText("Yeet");
caption.SetPosition(new Point(context.shape.GetPosition().X + offset.X, context.shape.GetPosition().Y + offset.Y));
}
}
}
13 changes: 6 additions & 7 deletions DrawingApp/DecoratorPattern/DecoratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ namespace DrawingApp.DecoratorPattern
{
public class DecoratorContext
{
public System.Drawing.Point ShapePosition;
public System.Drawing.Point DecoratorPosition;
public string CaptionPosition; //"top" "bottom" "left" "right"
public CanvasShape parent;
public CanvasShape shape;
public MainWindow MainWindow = CommandInvoker.GetInstance().MainWindow;
private readonly string[] positions = {"top", "bottom", "left", "right"};

public DecoratorContext(System.Drawing.Point ShapePosition = new System.Drawing.Point(), string CaptionPosition = "",
CanvasShape parent = null)
CanvasShape shape = null)
{
this.ShapePosition = ShapePosition;

this.CaptionPosition = CaptionPosition;
this.parent = parent;
this.CaptionPosition = "bottom";
this.shape = shape;
this.CaptionPosition = positions[CommandInvoker.Rnd.Next(0, 4)];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace DrawingApp.DecoratorPattern
{
interface IDecorator
public interface IDecorator
{
public void Draw();
}
Expand Down

0 comments on commit 7b017ba

Please sign in to comment.