X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=dev3%2Fpsychlops%2Fcore%2Fgraphic%2Fcanvas.cs;h=9dfe0315c7be3cce13230306baa74a04ae35a3bb;hb=e22f217b876609c575411512e127062c35d49441;hp=4414a2431ba5402cd12e5816457e57e11696f7ed;hpb=daf3341826ecefb2489b15af040f8ed4dc9f3480;p=psychlops%2Fsilverlight.git diff --git a/dev3/psychlops/core/graphic/canvas.cs b/dev3/psychlops/core/graphic/canvas.cs index 4414a24..9dfe031 100644 --- a/dev3/psychlops/core/graphic/canvas.cs +++ b/dev3/psychlops/core/graphic/canvas.cs @@ -7,66 +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; - System.Windows.Controls.UserControl panel; - WriteableBitmap buffer; + //WriteableBitmap buffer; + //System.Windows.Controls.Image instance; + System.Windows.Controls.Canvas api_canvas; + System.Windows.Controls.UserControl panel; + Rectangle back_panel; + double width_, height_; public Canvas(int wid, int hei) { panel = default_panel; - instance = default_buffer_frame; + api_canvas = default_api_canvas; initialize(wid, hei); } - public Canvas(int wid, int hei, System.Windows.Controls.Image target, System.Windows.Controls.UserControl system) + public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system) { panel = system; - instance = target; + api_canvas = apicnvs; initialize(wid, hei); } protected void initialize(int wid, int hei) { - instance.Width = wid; - instance.Height = hei; - buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height); - instance.Source = buffer; - instance.MouseMove += Mouse.Canvas_MousePos; - instance.MouseLeftButtonDown += Mouse.Canvas_LDown; - instance.MouseLeftButtonUp += Mouse.Canvas_LUp; - instance.MouseWheel += Mouse.Canvas_MouseWheel; - Mouse._prime = instance; + + width_ = wid; + height_ = hei; + api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei); + Mouse._prime = api_canvas; Main.drawable = this; - default_buffer = buffer; - } - public Point getCenter() - { - return new Point(instance.Width/2.0, instance.Height/2.0, 0); - } + Main.canvas = this; - public void clear() - { - buffer.Clear(Color.black); + back_panel = new Rectangle(wid, hei); + + flipexec = new SimpleProcedure(executeFlip); } - public void clear(Color col) + protected void initialize__(int wid, int hei) { - buffer.Clear(col); - } + 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; - public void pix(int x, int y, Color col) - { - buffer.SetPixel(x, y, 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"); } + #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; @@ -79,7 +165,7 @@ namespace Psychlops static System.Windows.Media.TranslateTransform api_translation; static Canvas() { - api_line = new System.Windows.Shapes.Line(); + 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(); @@ -90,50 +176,291 @@ namespace Psychlops api_stroke = new System.Windows.Media.SolidColorBrush(); api_translation = new System.Windows.Media.TranslateTransform(); } + */ + #endregion + + + public override void clear(Color col) + { + 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) + { + nextIntervalFrame = 1; + chacked = 1; + } + Internal.Main.canvas_flag.WaitOne(); + } + public void executeFlip() + { + 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 + + + } + + + + #region primitive tokenizer + + partial struct Point + { + public static implicit operator System.Windows.Point(Point d) + { + return new System.Windows.Point(d.x, d.y); + } + } + + 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 }; + } + } + + 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; } + } - public void line(Line drawee, Color col) + partial class Rectangle + { + public Rectangle clone() { - buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col); + return (Rectangle)MemberwiseClone(); } - public void rect(Rectangle drawee, Color col) + public static implicit operator System.Windows.Rect(Rectangle d) { - //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col); - api_rect.Width = drawee.width; - api_rect.Height = drawee.height; - api_fill.Color = col; - api_rect.Fill = api_fill; - api_translation.X = drawee.left; - api_translation.Y = drawee.top; - buffer.Render(api_rect, api_translation); + return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y); } - public void ellipse(Ellipse drawee, Color col) + public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d) { - 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 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 void polygon(Polygon drawee, Color col) + public UIElement toNative() { return this; } + } + + partial class Ellipse + { + public Ellipse clone() { - int[] ps = new int[drawee.vertices.Count]; - int i=0; - foreach (Point p in drawee.vertices) + 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) { - ps[i] = (int)p.x; i++; - ps[i] = (int)p.y; i++; + tmp.Points.Add(p); } - buffer.DrawPolyline(ps, col); + System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y); + return tmp; } - public void image(Image drawee) + 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() { - buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None); + 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() + { + 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; } + } + 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 int frame; - public void flip() + public Image clone() + { + return (Image)MemberwiseClone(); + } + public static implicit operator System.Windows.Controls.Image(Image d) { - buffer.Invalidate(); - frame++; + 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