X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=dev3%2Fpsychlops%2Fcore%2Fgraphic%2Fcanvas.cs;h=9dfe0315c7be3cce13230306baa74a04ae35a3bb;hb=e22f217b876609c575411512e127062c35d49441;hp=965314270ec313ec05ba6ccccdc77d7c7fd074ad;hpb=ee118c1cb2979154a0c1ed80bf1c13e7fe4e8970;p=psychlops%2Fsilverlight.git diff --git a/dev3/psychlops/core/graphic/canvas.cs b/dev3/psychlops/core/graphic/canvas.cs index 9653142..9dfe031 100644 --- a/dev3/psychlops/core/graphic/canvas.cs +++ b/dev3/psychlops/core/graphic/canvas.cs @@ -7,85 +7,152 @@ using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; +using System.Windows.Browser; namespace Psychlops { - public class Canvas : Drawable + namespace Templates { - public static System.Windows.Controls.Image default_buffer_frame; + + public class StackableDrawable : Drawable + { + protected System.Collections.Generic.Queue stack; + + public StackableDrawable() + { + stack = new System.Collections.Generic.Queue(); + } + + public void clear() + { + clear(Color.black); + } + public virtual void clear(Color col) + { + //rect(back_panel, col); + } + + public virtual void pix(int x, int y, Color col) + { + } + + public virtual void line(Line drawee) + { + stack.Enqueue(drawee.clone()); + } + + public virtual void rect(Rectangle drawee) + { + stack.Enqueue(drawee.clone()); + } + + public virtual void ellipse(Ellipse drawee) + { + stack.Enqueue(drawee.clone()); + } + + public virtual void polygon(Polygon drawee) + { + stack.Enqueue(drawee.clone()); + } + + public virtual void letters(Letters drawee) + { + stack.Enqueue(drawee.clone()); + } + + public virtual void image(Image drawee) + { + stack.Enqueue(drawee.clone()); + } + + public void msg(string str, double x, double y) { msg(str, x, y, Color.white); } + public virtual void msg(string dstr, double x, double y, Color col) + { + var let = new Letters(dstr); + let.locate(x, y); + let.fill = col; + this.letters(let); + } + 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 virtual Point getCenter() { return new Point(0, 0, 0); } + } + + } + + public class Canvas : Templates.StackableDrawable + { + + #region initializer + + internal delegate void TwoIntProcedure(int x, int y); + internal delegate void SimpleProcedure(); + SimpleProcedure flipexec; + + //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; + //WriteableBitmap 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; + Rectangle back_panel; + double width_, height_; 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) + 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; + + width_ = wid; + height_ = hei; + api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei); + Mouse._prime = api_canvas; + Main.drawable = this; + Main.canvas = this; + + back_panel = new Rectangle(wid, hei); + + flipexec = new SimpleProcedure(executeFlip); + } + protected void initialize__(int wid, int hei) + { 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; panel.KeyDown += Keyboard.Canvas_KeyDown; panel.KeyUp += Keyboard.Canvas_KeyUp; - 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); + HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost"); + //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null"); + htmlHost.SetStyleAttribute("width", (200+wid).ToString()+"px"); + htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px"); + htmlHost.SetStyleAttribute("margin", "2em auto auto auto"); } - public void pix(int x, int y, Color col) - { - buffer.SetPixel(x, y, col); - } + #endregion + #region static initializer + /* static System.Windows.Shapes.Line api_line; static System.Windows.Shapes.Path api_curve; static System.Windows.Shapes.Rectangle api_rect; @@ -96,24 +163,8 @@ namespace Psychlops 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(); @@ -125,129 +176,291 @@ namespace Psychlops 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) + public override void clear(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) + back_panel.fill = col; + rect(back_panel); + } + + int nextIntervalFrame = 1, chacked = 0; + public void flip() + { + //api_canvas.Dispatcher.BeginInvoke(flipexec); + //flipexec(); + //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); + lock (this) { - 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; + nextIntervalFrame = 1; + chacked = 1; } - System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x - left); - System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y - drawee.font.size); + Internal.Main.canvas_flag.WaitOne(); } - public void image(Image drawee) + public void executeFlip() { - //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); + lock (this) + { + nextIntervalFrame--; + } + if (nextIntervalFrame <= 0) + { + //Psychlops.AppState.statusBar = chacked.ToString(); + if (chacked > 0) + { + var tmp = new System.Windows.Controls.Canvas(); + if (stack.Count > 0) + { + foreach (Internal.PrimitiveFigure f in stack) + { + tmp.Children.Add(f.toNative()); + } + } + api_canvas.Children.Clear(); + api_canvas.Children.Add(tmp); + stack.Clear(); + lock (this) + { + chacked = 0; + } + Psychlops.Internal.Main.canvas_flag.Set(); + } + } + System.Threading.Thread.Sleep(0); } + #region Properties + + public double width { get { return width_; } } + public double height { get { return height_; } } + public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } } + public double getWidth() { return width; } + public double getHeight() { return height; } + public override Point getCenter() { return center; } + public double getHCenter() { return width / 2; } + public double getVCenter() { return height / 2; } + public double getRefreshRate() { return 60; } + + #endregion + + + } + - 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) + + #region primitive tokenizer + + partial struct Point + { + public static implicit operator System.Windows.Point(Point d) { - var let = new Letters(str); - let.locate(x, y); - this.letters(let, col); + return new System.Windows.Point(d.x, d.y); } - 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); } + } + partial struct Color + { + public static implicit operator System.Windows.Media.Color(Color d) + { + return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255)); + } + public static implicit operator System.Windows.Media.SolidColorBrush(Color d) + { + return new SolidColorBrush { Color = d }; + } + } - public int frame; - public void flip() + partial struct Stroke + { + public void apply(System.Windows.Shapes.Shape target) + { + target.Stroke = this; + //target.StrokeDashArray + target.StrokeThickness = thick; + } + public static implicit operator SolidColorBrush(Stroke d) + { + return new SolidColorBrush { Color = d.color }; + } + } + + partial class Line + { + public Line clone() + { + return (Line)MemberwiseClone(); + } + public static implicit operator System.Windows.Shapes.Line(Line d) + { + var tmp = new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y }; + if (d.stroke.thick == 0.0) tmp.Stroke = d.fill; + else d.stroke.apply(tmp); + return tmp; + } + public UIElement toNative() { return this; } + } + + partial class Rectangle + { + public Rectangle clone() + { + return (Rectangle)MemberwiseClone(); + } + public static implicit operator System.Windows.Rect(Rectangle d) + { + return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y); + } + public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d) + { + var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill }; + d.stroke.apply(tmp); + System.Windows.Controls.Canvas.SetLeft(tmp, d.left); + System.Windows.Controls.Canvas.SetTop(tmp, d.top); + return tmp; + } + public UIElement toNative() { return this; } + } + + partial class Ellipse + { + public Ellipse clone() + { + return (Ellipse)MemberwiseClone(); + } + public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d) + { + var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill }; + d.stroke.apply(tmp); + System.Windows.Controls.Canvas.SetLeft(tmp, d.left); + System.Windows.Controls.Canvas.SetTop(tmp, d.top); + return tmp; + } + public UIElement toNative() { return this; } + } + + partial class Polygon + { + public Polygon clone() + { + return (Polygon)MemberwiseClone(); + } + public static implicit operator System.Windows.Shapes.Polygon(Polygon d) + { + var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill }; + d.stroke.apply(tmp); + foreach (Point p in d.vertices) + { + tmp.Points.Add(p); + } + System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y); + return tmp; + } + public UIElement toNative() { return this; } + } + + partial class Letters + { + #region static initializer + internal static System.Collections.Generic.Dictionary FONT_WEIGHT_BRIDGE; + internal static System.Collections.Generic.Dictionary FONT_STYLE_BRIDGE; + internal static System.Collections.Generic.Dictionary LETTERS_H_ALIGN_BRIDGE; + static Letters() + { + 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.left, TextAlignment.Left); + LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center); + LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right); + LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left); + } + #endregion + public Letters clone() { - buffer.Invalidate(); - frame++; + return (Letters)MemberwiseClone(); } + public static implicit operator System.Windows.Controls.TextBlock(Letters d) + { + //var zapi_shape = new System.Windows.Documents.Glyphs(); + var tmp = new System.Windows.Controls.TextBlock { + Text = d.str, Width = 500, Height = 500, + FontSize = d.font.size, + //tmp.FontFamily = , + FontStyle = FONT_STYLE_BRIDGE[d.font.style], + FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight], + TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align], + Foreground = d.fill + }; + double left = 0; + switch (d.align) + { + case Letters.HorizontalAlign.left: break; + case Letters.HorizontalAlign.center: left = tmp.Width / 2; break; + case Letters.HorizontalAlign.right: left = tmp.Width; break; + } + System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left); + System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size); + return tmp; + } + public UIElement toNative() { return this; } + } - public double width { get { return api_canvas.Width; } } - public double height { get { return api_canvas.Height; } } + partial class Image + { + internal void initialize__(int wid, int hei) + { + Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei); + //buffer = new WriteableBitmap(wid, hei); + } + internal void create__(int wid, int hei) + { + buffer = new WriteableBitmap(wid, hei); + } + delegate void FieldFunc1(System.Func func); + delegate void FieldFunc2(System.Func func); + public void field__(System.Func func) + { + Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func); + //buffer.ForEach(func); + } + public void field__(System.Func func) + { + Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func); + //buffer.ForEach(func); + } + public void field___(System.Func func) + { + buffer.ForEach(func); + } + public void field___(System.Func func) + { + buffer.ForEach(func); + } + + public Image clone() + { + return (Image)MemberwiseClone(); + } + public static implicit operator System.Windows.Controls.Image(Image d) + { + var tmp = new System.Windows.Controls.Image(); + tmp.Source = d.buffer; + System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y); + return tmp; + } + public UIElement toNative() { return this; } } + #endregion + } \ No newline at end of file