using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Psychlops { public class Canvas : Drawable { public static System.Windows.Controls.Image default_buffer_frame; public static System.Windows.Controls.UserControl default_panel; public static System.Windows.Controls.Canvas default_api_canvas; public static WriteableBitmap default_buffer; System.Windows.Controls.Image instance; System.Windows.Controls.Canvas api_canvas; System.Windows.Controls.UserControl panel; WriteableBitmap buffer; System.Windows.Shapes.Rectangle back_panel; System.Windows.Media.SolidColorBrush back_panel_color; public Canvas(int wid, int hei) { panel = default_panel; api_canvas = default_api_canvas; instance = default_buffer_frame; initialize(wid, hei); } public Canvas(int wid, int hei, System.Windows.Controls.Image target, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system) { panel = system; api_canvas = apicnvs; instance = target; initialize(wid, hei); } protected void initialize(int wid, int hei) { instance.Width = wid; instance.Height = hei; instance.Source = buffer; api_canvas.Width = wid; api_canvas.Height = hei; buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height); api_canvas.MouseMove += Mouse.Canvas_MousePos; api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown; api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp; api_canvas.MouseWheel += Mouse.Canvas_MouseWheel; Mouse._prime = api_canvas; Main.drawable = this; default_buffer = buffer; back_panel = new System.Windows.Shapes.Rectangle(); back_panel.Width = wid; back_panel.Height = hei; back_panel_color = new SolidColorBrush(); back_panel.Fill = back_panel_color; } public Point getCenter() { return new Point(instance.Width/2.0, instance.Height/2.0, 0); } public void clear() { buffer.Clear(Color.black); } public void clear(Color col) { api_canvas.Children.Clear(); back_panel_color.Color = col; api_canvas.Children.Add(back_panel); //buffer.Clear(col); } public void pix(int x, int y, Color col) { buffer.SetPixel(x, y, col); } static System.Windows.Shapes.Line api_line; static System.Windows.Shapes.Path api_curve; static System.Windows.Shapes.Rectangle api_rect; static System.Windows.Shapes.Ellipse api_ellipse; static System.Windows.Shapes.Polygon api_polygon; static System.Windows.Shapes.Polyline api_polyline; static System.Windows.Media.Color api_color; static System.Windows.Media.SolidColorBrush api_fill; static System.Windows.Media.SolidColorBrush api_stroke; static System.Windows.Media.TranslateTransform api_translation; static Canvas() { api_line = new System.Windows.Shapes.Line(); api_curve = new System.Windows.Shapes.Path(); api_rect = new System.Windows.Shapes.Rectangle(); api_ellipse = new System.Windows.Shapes.Ellipse(); api_polygon = new System.Windows.Shapes.Polygon(); api_polyline = new System.Windows.Shapes.Polyline(); api_color = new System.Windows.Media.Color(); api_fill = new System.Windows.Media.SolidColorBrush(); api_stroke = new System.Windows.Media.SolidColorBrush(); api_translation = new System.Windows.Media.TranslateTransform(); } public void line(Line drawee, Color col) { //buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col); var zapi_shape = new System.Windows.Shapes.Line(); var zapi_fill = new System.Windows.Media.SolidColorBrush(); zapi_shape.X1 = (int)drawee.begin.x; zapi_shape.Y1 = (int)drawee.begin.y; zapi_shape.X2 = (int)drawee.end.x; zapi_shape.Y2 = (int)drawee.end.y; zapi_fill.Color = col; zapi_fill.Opacity = 1.0; zapi_shape.Stroke = zapi_fill; zapi_shape.StrokeThickness = 1; api_canvas.Children.Add(zapi_shape); //System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left); //System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top); } public void rect(Rectangle drawee, Color col) { //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col); var zapi_shape = new System.Windows.Shapes.Rectangle(); var zapi_fill = new System.Windows.Media.SolidColorBrush(); zapi_shape.Width = drawee.width; zapi_shape.Height = drawee.height; zapi_fill.Color = col; zapi_shape.Fill = zapi_fill; api_canvas.Children.Add(zapi_shape); System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top); } public void ellipse(Ellipse drawee, Color col) { //buffer.DrawEllipse((int)(drawee.datum.x - drawee.xdiameter / 2), (int)(drawee.datum.y - drawee.ydiameter / 2), (int)(drawee.datum.x + drawee.xdiameter / 2), (int)(drawee.datum.y + drawee.ydiameter / 2), col); var zapi_shape = new System.Windows.Shapes.Ellipse(); var zapi_fill = new System.Windows.Media.SolidColorBrush(); zapi_shape.Width = drawee.width; zapi_shape.Height = drawee.height; zapi_fill.Color = col; zapi_shape.Fill = zapi_fill; api_canvas.Children.Add(zapi_shape); System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top); } public void polygon(Polygon drawee, Color col) { /* int[] ps = new int[drawee.vertices.Count]; buffer.DrawPolyline(ps, col); */ var zapi_shape = new System.Windows.Shapes.Polygon(); var zapi_fill = new System.Windows.Media.SolidColorBrush(); int i = 0; foreach (Point p in drawee.vertices) { zapi_shape.Points.Add(p); } zapi_fill.Color = col; zapi_shape.Fill = zapi_fill; api_canvas.Children.Add(zapi_shape); System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y); } public void letters(Letters drawee, Color col) { //var zapi_shape = new System.Windows.Documents.Glyphs(); var zapi_shape = new System.Windows.Controls.TextBlock(); var zapi_fill = new System.Windows.Media.SolidColorBrush(); zapi_shape.MaxWidth = width; zapi_shape.MaxHeight = height; zapi_shape.Text = drawee.str; //zapi_shape.FontFamily = ; zapi_shape.FontSize = drawee.font.size; zapi_shape.FontStyle = drawee.font.style==Font.Style.normal_style ? System.Windows.FontStyles.Normal : System.Windows.FontStyles.Italic; zapi_shape.FontWeight = drawee.font.weight>400 ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Normal; zapi_shape.TextAlignment = drawee.align==Letters.HorizontalAlign.TEXT_ALIGN_LEFT ? TextAlignment.Left : TextAlignment.Center; zapi_fill.Color = col; zapi_shape.Foreground = zapi_fill; api_canvas.Children.Add(zapi_shape); System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y); } public void image(Image drawee) { //buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None); var zapi_shape = new System.Windows.Controls.Image(); zapi_shape.Source = drawee.buffer; api_canvas.Children.Add(zapi_shape); System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y); } public int frame; public void flip() { buffer.Invalidate(); frame++; } public double width { get { return api_canvas.Width; } } public double height { get { return api_canvas.Height; } } } }