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; //WriteableBitmap buffer; //System.Windows.Controls.Image instance; System.Windows.Controls.Canvas api_canvas; System.Windows.Controls.UserControl panel; 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.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; //buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height); //default_buffer = buffer; api_canvas.Width = wid; api_canvas.Height = hei; api_canvas.MouseMove += Mouse.Canvas_MousePos; api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown; api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp; api_canvas.MouseWheel += Mouse.Canvas_MouseWheel; panel.KeyDown += Keyboard.Canvas_KeyDown; panel.KeyUp += Keyboard.Canvas_KeyUp; Mouse._prime = api_canvas; Main.drawable = this; 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(api_canvas.Width / 2.0, api_canvas.Height / 2.0, 0); } public void clear() { //buffer.Clear(Color.black); 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); } #region static initializer /* 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; */ protected static System.Collections.Generic.Dictionary FONT_WEIGHT_BRIDGE; protected static System.Collections.Generic.Dictionary FONT_STYLE_BRIDGE; protected static System.Collections.Generic.Dictionary LETTERS_H_ALIGN_BRIDGE; static Canvas() { FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary(); FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal); FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold); FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary(); FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal); FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic); FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic); LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary(); LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left); LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center); LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right); LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left); /* 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(); */ } #endregion 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.Width = width; zapi_shape.Height = height; zapi_shape.Text = drawee.str; //zapi_shape.FontFamily = ; zapi_shape.FontSize = drawee.font.size; zapi_shape.FontStyle = FONT_STYLE_BRIDGE[drawee.font.style]; zapi_shape.FontWeight = FONT_WEIGHT_BRIDGE[drawee.font.weight]; zapi_shape.TextAlignment = LETTERS_H_ALIGN_BRIDGE[drawee.align]; zapi_fill.Color = col; zapi_shape.Foreground = zapi_fill; api_canvas.Children.Add(zapi_shape); double left = 0; switch(drawee.align) { case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break; case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = zapi_shape.Width/2; break; case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = zapi_shape.Width; break; } System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x - left); System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y - drawee.font.size); } 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 void msg(string str, double x, double y) { msg(str, x, y, Color.white); } public void msg(string str, double x, double y, Color col) { var let = new Letters(str); let.locate(x, y); this.letters(let, col); } public void var(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); } public void var(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); } public void flip() { //buffer.Invalidate(); } public double width { get { return api_canvas.Width; } } public double height { get { return api_canvas.Height; } } } }