using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;
namespace AIEditor.Core
{
public class AIDraw
{
//Properties of visuals
private Brush drawingBrush = Brushes.AliceBlue;
private Brush selectedDrawingBrush = Brushes.LightGoldenrodYellow;
private Pen drawingPen = new Pen(Brushes.SteelBlue, 2);
//Visuals for drawing objects
DrawingVisual visual;
Visual grabLastVisual;
//Switchs to draw the current tool
bool drawarrow = false;
bool drawsquare = false;
bool drawactor = false;
bool cleanupFirstVisual = false;
//Mouse from and to point collision
Point fromMousePoint;
Point toMousePoint;
//Possible tools
public enum DrawingTools
{
Square,
Actor,
Arrow
}
//Current tool being used
public DrawingTools CurrentTool = DrawingTools.Arrow;
//Grid line collections
public List<Point[]> VerticalLines;
public List<Point[]> HorizontalLines;
DrawingCanvas drawScreen;
//Initialize Drawing
public AIDraw(DrawingCanvas drawScreen)
{
this.drawScreen = drawScreen;
VerticalLines = new List<Point[]>();
HorizontalLines = new List<Point[]>();
}
//Set the current tool
public void DrawCurrentTool(Point mousePosition)
{
fromMousePoint = mousePosition;
switch (CurrentTool)
{
case DrawingTools.Arrow:
drawarrow = true;
drawsquare = false;
drawactor = false;
break;
case DrawingTools.Square:
drawsquare = true;
drawactor = false;
drawarrow = false;
drawScreen.startTempVisual = true;
break;
case DrawingTools.Actor:
drawactor = true;
drawsquare = false;
drawarrow = false;
DrawActor();
break;
}
}
//Draw Grid
public void DrawBackground()
{
visual = new DrawingVisual();
int heightIncrements = (int)drawScreen.RenderSize.Height / 10;
int widthIncrements = (int)drawScreen.RenderSize.Width / 10;
int horizontalLength = (int)drawScreen.RenderSize.Width;
int verticalLength = (int)drawScreen.RenderSize.Height;
float verticalIncrement = 0;
float horizontalIncrement = 0;
float largerIndicator = 0;
using (DrawingContext dc = visual.RenderOpen())
{
//Draw Horizontal Lines
for (int h = 0; h < heightIncrements + 1; h++)
{
if (largerIndicator == 5)
{
Point fromPoint = new Point(0, verticalIncrement);
Point toPoint = new Point(horizontalLength, verticalIncrement);
HorizontalLines.Add(new Point[] { fromPoint, toPoint });
Pen pen = new Pen(Brushes.DarkKhaki, .5);
dc.DrawLine(pen, fromPoint, toPoint);
largerIndicator = 0;
}
else
{
Point fromPoint = new Point(0, verticalIncrement);
Point toPoint = new Point(horizontalLength, verticalIncrement);
HorizontalLines.Add(new Point[] { fromPoint, toPoint });
Pen pen = new Pen(Brushes.Gray, .5);
dc.DrawLine(pen, fromPoint, toPoint);
}
largerIndicator += 1;
verticalIncrement += 10;
}
largerIndicator = 0;
//Draw Vertical Lines
for (int w = 0; w < widthIncrements + 1; w++)
{
if (largerIndicator == 5)
{
Point fromPoint = new Point(horizontalIncrement, verticalLength);
Point toPoint = new Point(horizontalIncrement, 0);
VerticalLines.Add(new Point[] { fromPoint, toPoint });
Pen pen = new Pen(Brushes.DarkKhaki, .5);
dc.DrawLine(pen, fromPoint, toPoint);
largerIndicator = 0;
}
else
{
Point fromPoint = new Point(horizontalIncrement, verticalLength);
Point toPoint = new Point(horizontalIncrement, 0);
VerticalLines.Add(new Point[] { fromPoint, toPoint });
Pen pen = new Pen(Brushes.Gray, .5);
dc.DrawLine(pen, fromPoint, toPoint);
}
largerIndicator += 1;
horizontalIncrement += 10;
}
}
drawScreen.AddVisual(visual, false);
}
//Clean up the current drawing state
public void CleanUp()
{
cleanupFirstVisual = false;
if (drawsquare)
{
drawsquare = false;
drawScreen.startTempVisual = false;
drawScreen.DeleteVisual(visual, true);
drawScreen.AddVisual(grabLastVisual, false);
string squareName = "Square" + AIEngine.ScriptEngine.Globals.Count.ToString();
AIEngine.ScriptEngine.Globals.Add(squareName,
new AIObject(squareName, (DrawingVisual)grabLastVisual, fromMousePoint, toMousePoint));
CurrentTool = DrawingTools.Arrow;
}
if (drawactor)
{
drawactor = false;
drawScreen.startTempVisual = false;
drawScreen.DeleteVisual(visual, true);
drawScreen.AddVisual(grabLastVisual, false);
CurrentTool = DrawingTools.Arrow;
}
}
//Create an Actor
public void DrawActor()
{
if (cleanupFirstVisual)
drawScreen.DeleteVisual(visual, true);
visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
dc.DrawEllipse(drawingBrush, drawingPen,
fromMousePoint, 4, 4);
}
drawScreen.AddVisual(visual, true);
string name = "Actor" + AIEngine.ScriptEngine.Globals.Count.ToString();
AIEngine.ScriptEngine.Globals.Add(name, new AIActor(name, fromMousePoint, visual));
((AIActor)AIEngine.ScriptEngine.Globals[name]).AddTreeItem();
if (!cleanupFirstVisual)
cleanupFirstVisual = true;
grabLastVisual = visual;
}
//Create a Square
public void DrawSquare(Point ToPoint)
{
if (cleanupFirstVisual)
drawScreen.DeleteVisual(visual, true);
visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
Brush brush = drawingBrush;
dc.DrawRectangle(null, drawingPen,
new Rect(fromMousePoint, ToPoint));
}
drawScreen.AddVisual(visual, true);
grabLastVisual = visual;
if (!cleanupFirstVisual)
cleanupFirstVisual = true;
toMousePoint = ToPoint;
}
//While creating a square, this animates the dragging ability
public void DrawAnimation(Point MousePosition)
{
Point position = MousePosition;
if (drawsquare)
{
DrawSquare(MousePosition);
}
}
//Cancel the current drawing
public void CancelDrawing()
{
drawScreen.DeleteVisual(visual, true);
}
//If window is resized, redraw grid
public void DrawResizedBackground()
{
drawScreen.DeleteVisual(visual, false);
DrawBackground();
}
}
//The screen that holds and draws visuals. This is embedded
//in the XAML code of the MainWnd.
public class DrawingCanvas : Canvas
{
//The collection of visual(drawings) the screen has
private List<Visual> visuals = new List<Visual>();
//Temprorary visuals that are deleted periodicly.
//Such as drawing the square, in order to animate the
//dragging ability, we must delete visuals.
private List<Visual> tempvisuals = new List<Visual>();
//This tells teh AddVisual whether or not add the visual
//temprorary or in the visual collection.
public bool startTempVisual = false;
//Get the current visual count
protected override int VisualChildrenCount
{
get
{
if (!startTempVisual)
{
return visuals.Count;
}
else
{
return tempvisuals.Count;
}
}
}
//Get a visual
protected override Visual GetVisualChild(int index)
{
if (!startTempVisual)
{
return visuals[index];
}
else
{
return tempvisuals[index];
}
}
//Add a temporary or normal visual
public void AddVisual(Visual visual, bool tempVisual)
{
if (tempVisual)
{
tempvisuals.Add(visual);
}
else
{
visuals.Add(visual);
}
base.AddVisualChild(visual);
base.AddLogicalChild(visual);
}
//Delete a temporary or normal visual
public void DeleteVisual(Visual visual, bool tempVisual)
{
if (tempVisual)
{
tempvisuals.Clear();
}
else
{
visuals.Remove(visual);
}
base.RemoveVisualChild(visual);
base.RemoveLogicalChild(visual);
}
}
}