OSDN Git Service

flip ok
authorunknown <hskwk@.(none)>
Fri, 19 Mar 2010 02:27:28 +0000 (11:27 +0900)
committerunknown <hskwk@.(none)>
Fri, 19 Mar 2010 02:27:28 +0000 (11:27 +0900)
dev3/psychlops/core/app/misc.cs
dev3/psychlops/core/device/hid.cs
dev3/psychlops/core/graphic/canvas.cs
dev3/psychlops/core/graphic/font.cs
dev3/psychlops/core/graphic/module.cs
dev3/psychlops/core/graphic/shape.cs
dev3/psychlops/extention/standard/figures.cs
test3/MainPage.xaml.cs
test3/PsychlopsMain.cs

index 581af3d..87aa503 100644 (file)
@@ -4,5 +4,8 @@
        public static class Main\r
        {\r
                public static Drawable drawable;\r
+               public static Canvas canvas;\r
+               public static System.Threading.Thread routine;\r
+               public static System.Threading.AutoResetEvent flag;\r
        }\r
 }\r
index a8bdc53..b71caeb 100644 (file)
@@ -11,19 +11,30 @@ namespace Psychlops
        {\r
                public struct Button\r
                {\r
+                       private static object locker;\r
+                       static Button()\r
+                       {\r
+                               locker = new object(); \r
+                       }\r
                        private bool pushed_, released_, pressed_;\r
                        public void down()\r
                        {\r
-                               pushed_ = true;\r
-                               pressed_ = true;\r
+                               lock (locker)\r
+                               {\r
+                                       pushed_ = true;\r
+                                       pressed_ = true;\r
+                               }\r
                        }\r
                        public void up()\r
                        {\r
-                               released_ = true;\r
-                               pressed_ = false;\r
+                               lock (locker)\r
+                               {\r
+                                       released_ = true;\r
+                                       pressed_ = false;\r
+                               }\r
                        }\r
-                       public bool pushed() { bool tmp = pushed_; pushed_ = false;  return tmp; }\r
-                       public bool released() { bool tmp = released_; released_ = false; return tmp; }\r
+                       public bool pushed() { lock (locker) { bool tmp = pushed_; pushed_ = false; return tmp; } }\r
+                       public bool released() { lock (locker) { bool tmp = released_; released_ = false; return tmp; } }\r
                        public bool pressed() { return pressed_; }\r
                }\r
        }\r
@@ -94,7 +105,6 @@ namespace Psychlops
                static private System.Collections.Generic.Dictionary<System.Windows.Input.Key, IKey> map;\r
                static public void Canvas_KeyUp(object sender, KeyEventArgs e)\r
                {\r
-                       Main.drawable.msg("hoge", 0,0,Color.green);\r
                        if (map.ContainsKey(e.Key)) map[e.Key].up();\r
                }\r
                static public void Canvas_KeyDown(object sender, KeyEventArgs e)\r
index fcfd6e7..f621e31 100644 (file)
@@ -15,6 +15,14 @@ namespace Psychlops
 \r
        public class Canvas : Drawable\r
        {\r
+\r
+               #region initializer\r
+\r
+               System.Collections.Generic.Queue<Figure> stock;\r
+               internal delegate void TwoIntProcedure(int x, int y);\r
+               internal delegate void SimpleProcedure();\r
+               SimpleProcedure flipexec;\r
+\r
                //public static System.Windows.Controls.Image default_buffer_frame;\r
                public static System.Windows.Controls.UserControl default_panel;\r
                public static System.Windows.Controls.Canvas default_api_canvas;\r
@@ -23,30 +31,38 @@ namespace Psychlops
                //System.Windows.Controls.Image instance;\r
                System.Windows.Controls.Canvas api_canvas;\r
                System.Windows.Controls.UserControl panel;\r
-               System.Windows.Shapes.Rectangle back_panel;\r
-               System.Windows.Media.SolidColorBrush back_panel_color;\r
+               Rectangle back_panel;\r
+               double width_, height_;\r
 \r
                public Canvas(int wid, int hei)\r
                {\r
                        panel = default_panel;\r
                        api_canvas = default_api_canvas;\r
-                       //instance = default_buffer_frame;\r
                        initialize(wid, hei);\r
                }\r
-               public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs,  System.Windows.Controls.UserControl system)\r
+               public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)\r
                {\r
                        panel = system;\r
                        api_canvas = apicnvs;\r
-                       //instance = target;\r
                        initialize(wid, hei);\r
                }\r
                protected void initialize(int wid, int hei)\r
                {\r
-                       //instance.Width = wid;\r
-                       //instance.Height = hei;\r
-                       //instance.Source = buffer;\r
-                       //buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
-                       //default_buffer = buffer;\r
+\r
+                       width_ = wid;\r
+                       height_ = hei;\r
+                       api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
+                       Mouse._prime = api_canvas;\r
+                       Main.drawable = this;\r
+                       Main.canvas = this;\r
+\r
+                       back_panel = new Rectangle(wid, hei);\r
+\r
+                       stock = new System.Collections.Generic.Queue<Figure>();\r
+                       flipexec = new SimpleProcedure(executeFlip);\r
+               }\r
+               protected void initialize__(int wid, int hei)\r
+               {\r
                        api_canvas.Width = wid;\r
                        api_canvas.Height = hei;\r
                        api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
@@ -55,37 +71,9 @@ namespace Psychlops
                        api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
                        panel.KeyDown += Keyboard.Canvas_KeyDown;\r
                        panel.KeyUp += Keyboard.Canvas_KeyUp;\r
-                       Mouse._prime = api_canvas;\r
-                       Main.drawable = this;\r
-\r
-                       back_panel = new System.Windows.Shapes.Rectangle();\r
-                       back_panel.Width = wid;\r
-                       back_panel.Height = hei;\r
-                       back_panel_color = new SolidColorBrush();\r
-                       back_panel.Fill = back_panel_color;\r
-               }\r
-               public Point getCenter()\r
-               {\r
-                       return new Point(api_canvas.Width / 2.0, api_canvas.Height / 2.0, 0);\r
-               }\r
-\r
-               public void clear()\r
-               {\r
-                       //buffer.Clear(Color.black);\r
-                       clear(Color.black);\r
-               }\r
-               public void clear(Color col)\r
-               {\r
-                       api_canvas.Children.Clear();\r
-                       back_panel_color.Color = col;\r
-                       api_canvas.Children.Add(back_panel);\r
-                       //buffer.Clear(col);\r
                }\r
 \r
-               public void pix(int x, int y, Color col)\r
-               {\r
-                       //buffer.SetPixel(x, y, col);\r
-               }\r
+               #endregion\r
 \r
                #region static initializer\r
                /*\r
@@ -99,26 +87,8 @@ namespace Psychlops
                static System.Windows.Media.SolidColorBrush api_fill;\r
                static System.Windows.Media.SolidColorBrush api_stroke;\r
                static System.Windows.Media.TranslateTransform api_translation;\r
-               */\r
-               protected static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
-               protected static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
-               protected static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
                static Canvas()\r
                {\r
-                       FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
-                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
-                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
-                       FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
-                       LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);\r
-\r
-                       /*\r
                        api_line = new System.Windows.Shapes.Line();\r
                        api_curve    = new System.Windows.Shapes.Path();\r
                        api_rect     = new System.Windows.Shapes.Rectangle();\r
@@ -129,108 +99,80 @@ namespace Psychlops
                        api_fill        = new System.Windows.Media.SolidColorBrush();\r
                        api_stroke      = new System.Windows.Media.SolidColorBrush();\r
                        api_translation = new System.Windows.Media.TranslateTransform();\r
-                       */\r
                }\r
+               */\r
                #endregion\r
 \r
+\r
+               public void clear()\r
+               {\r
+                       clear(Color.black);\r
+               }\r
+               public void clear(Color col)\r
+               {\r
+                       //api_canvas.Children.Clear();\r
+                       rect(back_panel, col);\r
+               }\r
+\r
+               public void pix(int x, int y, Color col)\r
+               {\r
+                       //buffer.SetPixel(x, y, col);\r
+               }\r
+\r
                public void line(Line drawee, Color col)\r
                {\r
-                       //buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
-                       var zapi_shape = new System.Windows.Shapes.Line();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.X1 = (int)drawee.begin.x;\r
-                       zapi_shape.Y1 = (int)drawee.begin.y;\r
-                       zapi_shape.X2 = (int)drawee.end.x;\r
-                       zapi_shape.Y2 = (int)drawee.end.y;\r
-                       zapi_fill.Color = col;\r
-                       zapi_fill.Opacity = 1.0;\r
-                       zapi_shape.Stroke = zapi_fill;\r
-                       zapi_shape.StrokeThickness = 1;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       //System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       //System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
+                       var tmp = drawee.clone();\r
+                       tmp.fill = col;\r
+                       stock.Enqueue(tmp);\r
+               }\r
+               public void line(Line drawee)\r
+               {\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
                public void rect(Rectangle drawee, Color col)\r
                {\r
-                       //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col);\r
-\r
-                       var zapi_shape = new System.Windows.Shapes.Rectangle();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = drawee.width;\r
-                       zapi_shape.Height = drawee.height;\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
+                       var tmp = drawee.clone();\r
+                       tmp.fill = col;\r
+                       stock.Enqueue(tmp);\r
+               }\r
+               public void rect(Rectangle drawee)\r
+               {\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
                public void ellipse(Ellipse drawee, Color col)\r
                {\r
-                       //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);\r
-\r
-                       var zapi_shape = new System.Windows.Shapes.Ellipse();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = drawee.width;\r
-                       zapi_shape.Height = drawee.height;\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
+                       var tmp = drawee.clone();\r
+                       tmp.fill = col;\r
+                       stock.Enqueue(tmp);\r
+               }\r
+               public void ellipse(Ellipse drawee)\r
+               {\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
                public void polygon(Polygon drawee, Color col)\r
                {\r
-                       /*\r
-                       int[] ps = new int[drawee.vertices.Count];\r
-                       buffer.DrawPolyline(ps, col);\r
-                       */\r
-                       var zapi_shape = new System.Windows.Shapes.Polygon();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       int i = 0;\r
-                       foreach (Point p in drawee.vertices)\r
-                       {\r
-                               zapi_shape.Points.Add(p);\r
-                       }\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
+                       var tmp = drawee.clone();\r
+                       tmp.fill = col;\r
+                       stock.Enqueue(tmp);\r
+               }\r
+               public void polygon(Polygon drawee)\r
+               {\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
                public void letters(Letters drawee, Color col)\r
                {\r
-                       //var zapi_shape = new System.Windows.Documents.Glyphs();\r
-                       var zapi_shape = new System.Windows.Controls.TextBlock();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = width;\r
-                       zapi_shape.Height = height;\r
-                       zapi_shape.Text = drawee.str;\r
-                       //zapi_shape.FontFamily = ;\r
-                       zapi_shape.FontSize = drawee.font.size;\r
-                       zapi_shape.FontStyle = FONT_STYLE_BRIDGE[drawee.font.style];\r
-                       zapi_shape.FontWeight = FONT_WEIGHT_BRIDGE[drawee.font.weight];\r
-                       zapi_shape.TextAlignment = LETTERS_H_ALIGN_BRIDGE[drawee.align];\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Foreground = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       double left = 0;\r
-                       switch(drawee.align)\r
-                       {\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = zapi_shape.Width/2; break;\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = zapi_shape.Width; break;\r
-                       }\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x - left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y - drawee.font.size);\r
+                       var tmp = drawee.clone();\r
+                       tmp.fill = col;\r
+                       stock.Enqueue(tmp);\r
+               }\r
+               public void letters(Letters drawee)\r
+               {\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
                public void image(Image drawee)\r
                {\r
                        //buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\r
-                       var zapi_shape = new System.Windows.Controls.Image();\r
-                       zapi_shape.Source = drawee.buffer;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
+                       stock.Enqueue(drawee.clone());\r
                }\r
 \r
 \r
@@ -247,11 +189,252 @@ namespace Psychlops
 \r
                public void flip()\r
                {\r
-                       //buffer.Invalidate();\r
+                       //api_canvas.Dispatcher.BeginInvoke(flipexec);\r
+                       //flipexec();\r
+                       //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);\r
+                       Main.flag.WaitOne();\r
+               }\r
+               \r
+               public void executeFlip()\r
+               {\r
+                       api_canvas.Children.Clear();\r
+                       foreach (Figure f in stock)\r
+                       {\r
+                               api_canvas.Children.Add(f.toNative());\r
+                       }\r
+                       stock.Clear();\r
                }\r
 \r
-               public double width { get { return api_canvas.Width; } }\r
-               public double height { get { return api_canvas.Height; } }\r
+               #region Properties\r
+\r
+               public double width { get { return width_; } }\r
+               public double height { get { return height_; } }\r
+               public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+               public Point getCenter() { return center; }\r
+               public double getHCenter() { return width / 2; }\r
+               public double getVCenter() { return height / 2; }\r
+               public double getRefreshRate() { return 60; }\r
+\r
+               #endregion\r
+\r
+\r
+       }\r
+\r
+\r
+\r
+\r
+       partial struct Point\r
+       {\r
+               public static implicit operator System.Windows.Point(Point d)\r
+               {\r
+                       return new System.Windows.Point(d.x, d.y);\r
+               }\r
+       }\r
+\r
+       partial struct Color\r
+       {\r
+               public static implicit operator System.Windows.Media.Color(Color d)\r
+               {\r
+                       return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
+               }\r
+               public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
+               {\r
+                       return new SolidColorBrush { Color = d };\r
+               }\r
+       }\r
+\r
+       partial struct Stroke\r
+       {\r
+               public void apply(System.Windows.Shapes.Shape target)\r
+               {\r
+                       target.Stroke = this;\r
+                       //target.StrokeDashArray\r
+                       target.StrokeThickness = thick;\r
+               }\r
+               public static implicit operator SolidColorBrush(Stroke d)\r
+               {\r
+                       return new SolidColorBrush { Color = d.color };\r
+               }\r
+       }\r
+\r
+       partial class Line\r
+       {\r
+               public Line clone()\r
+               {\r
+                       return (Line)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Line(Line d)\r
+               {\r
+                       var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
+                       if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
+                       else d.stroke.apply(tmp);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Rectangle\r
+       {\r
+               public Rectangle clone()\r
+               {\r
+                       return (Rectangle)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Rect(Rectangle d)\r
+               {\r
+                       return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Ellipse\r
+       {\r
+               public Ellipse clone()\r
+               {\r
+                       return (Ellipse)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Polygon\r
+       {\r
+               public Polygon clone()\r
+               {\r
+                       return (Polygon)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       foreach (Point p in d.vertices)\r
+                       {\r
+                               tmp.Points.Add(p);\r
+                       }\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+       \r
+       partial class Letters\r
+       {\r
+               #region static initializer\r
+               internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
+               internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
+               internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
+               static Letters()\r
+               {\r
+                       FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
+                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
+                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
+                       FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
+                       LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);\r
+               }\r
+               #endregion\r
+               public Letters clone()\r
+               {\r
+                       return (Letters)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
+               {\r
+                       //var zapi_shape = new System.Windows.Documents.Glyphs();\r
+                       var tmp = new System.Windows.Controls.TextBlock {\r
+                               Text = d.str, Width = 500, Height = 500,\r
+                               FontSize = d.font.size,\r
+                               //tmp.FontFamily = ,\r
+                               FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
+                               FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
+                               TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
+                               Foreground = d.fill\r
+                       };\r
+                       double left = 0;\r
+                       switch (d.align)\r
+                       {\r
+                               case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;\r
+                               case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = tmp.Width / 2; break;\r
+                               case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = tmp.Width; break;\r
+                       }\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Image\r
+       {\r
+               internal void initialize__(int wid, int hei)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
+                       //buffer = new WriteableBitmap(wid, hei);\r
+               }\r
+               internal void create__(int wid, int hei)\r
+               {\r
+                       buffer = new WriteableBitmap(wid, hei);\r
+               }\r
+               delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
+               delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
+               public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
+                       //buffer.ForEach(func);\r
+               }\r
+               public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
+                       //buffer.ForEach(func);\r
+               }\r
+               public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
+               {\r
+                       buffer.ForEach(func);\r
+               }\r
+               public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
+               {\r
+                       buffer.ForEach(func);\r
+               }\r
+\r
+               public Image clone()\r
+               {\r
+                       return (Image)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Controls.Image(Image d)\r
+               {\r
+                       var tmp = new System.Windows.Controls.Image();\r
+                       tmp.Source = d.buffer;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       internal class CanvasTokenizer\r
+       {\r
 \r
        }\r
 \r
index 4984b3a..8c8140c 100644 (file)
@@ -54,7 +54,7 @@ namespace Psychlops
                }\r
        }\r
 \r
-       public class Letters : Shape\r
+       public partial class Letters : Shape\r
        {\r
                protected string str_;\r
                protected Font font_;\r
@@ -120,12 +120,16 @@ namespace Psychlops
 \r
                public void draw()\r
                {\r
-                       Main.drawable.letters(this, Color.white);\r
+                       Main.drawable.letters(this);\r
                }\r
                public void draw(Color c)\r
                {\r
                        Main.drawable.letters(this, c);\r
                }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+\r
        }\r
 \r
 }\r
index 67bf68f..43b7caa 100644 (file)
@@ -13,7 +13,7 @@ using System.Windows.Shapes;
 namespace Psychlops\r
 {\r
 \r
-       public struct Point\r
+       public partial struct Point\r
        {\r
                public double x, y, z;\r
                public Point(double dx, double dy, double dz)\r
@@ -51,14 +51,10 @@ namespace Psychlops
                {\r
                        return new Point(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);\r
                }\r
-               public static implicit operator System.Windows.Point(Point d)\r
-               {\r
-                       return new System.Windows.Point(d.x, d.y);\r
-               }\r
        }\r
 \r
 \r
-       public struct Color\r
+       public partial struct Color\r
        {\r
                public double r, g, b, a;\r
                public Color(double lum)\r
@@ -99,10 +95,6 @@ namespace Psychlops
                        b = blue;\r
                        a = alpha;\r
                }\r
-               public static implicit operator System.Windows.Media.Color(Color d)\r
-               {\r
-                       return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
-               }\r
 \r
                public static readonly Color\r
                        black = new Color(0, 0, 0, 1),\r
@@ -123,11 +115,21 @@ namespace Psychlops
                Point getCenter();\r
                void clear(Color col);\r
                void pix(int x, int y, Color col);\r
+               void line(Line drawee);\r
                void line(Line drawee, Color col);\r
+               //void line(Line drawee, Stroke strk);\r
+               void rect(Rectangle drawee);\r
                void rect(Rectangle drawee, Color col);\r
+               //void rect(Rectangle drawee, Stroke strk);\r
+               void ellipse(Ellipse drawee);\r
                void ellipse(Ellipse drawee, Color col);\r
+               //void ellipse(Ellipse drawee, Stroke strk);\r
+               void polygon(Polygon drawee);\r
                void polygon(Polygon drawee, Color col);\r
+               //void polygon(Polygon drawee, Stroke strk);\r
+               void letters(Letters drawee);\r
                void letters(Letters drawee, Color col);\r
+               //void letters(Letters drawee, Stroke strk);\r
                void image(Image drawee);\r
                void msg(string s, double x, double y, Color c);\r
        }\r
@@ -139,6 +141,7 @@ namespace Psychlops
                Figure shift(Point p);\r
                Figure centering(Point p);\r
                void draw();\r
+               UIElement toNative();\r
        }\r
        public static class FigureExtention\r
        {\r
@@ -156,7 +159,7 @@ namespace Psychlops
                }\r
        }\r
 \r
-       public class Image : Figure\r
+       public partial class Image : Figure\r
        {\r
                public WriteableBitmap buffer;\r
                public Point datum;\r
@@ -164,7 +167,7 @@ namespace Psychlops
 \r
                public Image(int wid, int hei)\r
                {\r
-                       buffer = new WriteableBitmap(wid, hei);\r
+                       initialize__(wid, hei);\r
                        self_rect = new Rectangle(wid, hei);\r
                }\r
 \r
@@ -194,11 +197,13 @@ namespace Psychlops
 \r
                public void field(System.Func<int,int,System.Windows.Media.Color> func)\r
                {\r
-                       buffer.ForEach(func);\r
+                       field__(func);\r
+                       //buffer.ForEach(func);\r
                }\r
                public void field(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
                {\r
-                       buffer.ForEach(func);\r
+                       field__(func);\r
+                       //buffer.ForEach(func);\r
                }\r
 \r
                public void draw()\r
index 9533e8e..2933260 100644 (file)
@@ -15,6 +15,8 @@ namespace Psychlops{
        public interface Shape : Figure\r
        {\r
                void draw(Color c);\r
+               Color fill { get; set; }\r
+               Stroke stroke { get; set; }\r
        }\r
        public static class ShapeExtention\r
        {\r
@@ -26,12 +28,18 @@ namespace Psychlops{
                */\r
        }\r
 \r
-       public struct Stroke\r
+       public partial struct Stroke\r
        {\r
-\r
+               public double thick;\r
+               public Color color;\r
+               public void set(Color c, double t)\r
+               {\r
+                       color = c;\r
+                       thick = t;\r
+               }\r
        }\r
 \r
-       public class Line : Shape\r
+       public partial class Line : Shape\r
        {\r
                public Point begin, end;\r
 \r
@@ -76,7 +84,7 @@ namespace Psychlops{
                }\r
                public void draw()\r
                {\r
-                       Main.drawable.line(this, Color.white);\r
+                       Main.drawable.line(this);\r
                }\r
 \r
                public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
@@ -86,19 +94,12 @@ namespace Psychlops{
                public double width { get { return Math.abs(begin.x - end.x); } }\r
                public double height { get { return Math.abs(begin.y - end.y); } }\r
 \r
-               public static implicit operator System.Windows.Shapes.Line(Line d)\r
-               {\r
-                       var tmp = new System.Windows.Shapes.Line();\r
-                       tmp.X1 = d.begin.x;\r
-                       tmp.Y1 = d.begin.y;\r
-                       tmp.X2 = d.end.x;\r
-                       tmp.Y2 = d.end.y;\r
-                       return tmp;\r
-               }\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
        }\r
 \r
 \r
-       public class Rectangle : Shape\r
+       public partial class Rectangle : Shape\r
        {\r
                public Point v1, v2;\r
 \r
@@ -140,7 +141,7 @@ namespace Psychlops{
                }\r
                public void draw()\r
                {\r
-                       Main.drawable.rect(this, Color.white);\r
+                       Main.drawable.rect(this);\r
                }\r
 \r
                public double left   { get { return v1.x; } }\r
@@ -151,15 +152,13 @@ namespace Psychlops{
                public double height { get { return Math.abs(v1.y - v2.y); } }\r
 \r
 \r
-               public static implicit operator System.Windows.Rect(Rectangle d)\r
-               {\r
-                       return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
-               }\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
 \r
        }\r
 \r
 \r
-       public class Ellipse : Shape\r
+       public partial class Ellipse : Shape\r
        {\r
                public Point datum;\r
                public double xdiameter, ydiameter;\r
@@ -196,7 +195,7 @@ namespace Psychlops{
                }\r
                public void draw()\r
                {\r
-                       Main.drawable.ellipse(this, Color.white);\r
+                       Main.drawable.ellipse(this);\r
                }\r
 \r
                public double left { get { return datum.x - xdiameter/2.0; } }\r
@@ -205,10 +204,13 @@ namespace Psychlops{
                public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
                public double width { get { return Math.abs(xdiameter); } }\r
                public double height { get { return Math.abs(ydiameter); } }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
        }\r
 \r
 \r
-       public class Polygon : Shape\r
+       public partial class Polygon : Shape\r
        {\r
                public Point datum;\r
                public System.Collections.Generic.List<Point> vertices;\r
@@ -257,8 +259,11 @@ namespace Psychlops{
                }\r
                public void draw()\r
                {\r
-                       Main.drawable.polygon(this, Color.white);\r
+                       Main.drawable.polygon(this);\r
                }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
        }\r
 \r
 }
\ No newline at end of file
index dd26370..a7a8273 100644 (file)
@@ -6,14 +6,6 @@ namespace Psychlops
     public static class Figures\r
     {\r
 \r
-               public static void drawGrating(double wavelength, double contrast, double orientation, double phase)\r
-               {\r
-                       double freq = 2 * Math.PI / wavelength;\r
-                       Canvas.default_buffer.ForEach(\r
-                               (x, y) => new Color(.5 + .5 * Math.sin(phase + (Math.sin(orientation) * x - Math.cos(orientation) * y) * freq))\r
-                       );\r
-               }\r
-\r
                public static void drawGrating(ref Image img, int width, int height, double wavelength, double contrast, double orientation, double phase)\r
            {\r
                        double width_half = width / 2.0, height_half = height / 2.0;\r
index dcfdd2d..f2d8960 100644 (file)
@@ -4,7 +4,7 @@ namespace PsychlopsSilverlight3test
 {\r
        public partial class MainPage : System.Windows.Controls.UserControl\r
        {\r
-               System.Collections.Generic.IEnumerator<int> main_routine;\r
+               //System.Collections.Generic.IEnumerator<int> main_routine;\r
                public System.Windows.Controls.Image master;\r
                public PsychlopsMain main;\r
 #if DEBUG\r
@@ -30,9 +30,10 @@ namespace PsychlopsSilverlight3test
 \r
                        main = new PsychlopsMain();\r
                        //main.initialize();\r
-                       //main_routine = new System.Threading.Thread(ThreadTest2);\r
-                       //main_routine.Start();\r
-                       main_routine = main.psychlops_main();\r
+                       Main.flag = new System.Threading.AutoResetEvent(false);\r
+                       Main.routine = new System.Threading.Thread(main.psychlops_main);\r
+                       Main.routine.Start();\r
+                       //main_routine = main.psychlops_main();\r
 #if DEBUG\r
                        DebugConsole = new System.Windows.Controls.TextBlock();\r
                        TotalRoot.Children.Add(DebugConsole);\r
@@ -45,11 +46,13 @@ namespace PsychlopsSilverlight3test
                int nextIntervalFrame = 1;\r
                private void CompositionTarget_Rendering(object sender, System.EventArgs e)\r
                {\r
+                       if (Main.canvas != null) Main.canvas.executeFlip();\r
+                       Main.flag.Set();\r
                        nextIntervalFrame--;\r
                        if (nextIntervalFrame<=0)\r
                        {\r
-                               main_routine.MoveNext();\r
-                               nextIntervalFrame = main_routine.Current;\r
+                               //main_routine.MoveNext();\r
+                               //nextIntervalFrame = main_routine.Current;\r
                        }\r
                }\r
 \r
index fabaa51..30683f8 100644 (file)
@@ -2,6 +2,7 @@
 \r
 namespace PsychlopsSilverlight3test\r
 {\r
+\r
        public class PsychlopsMain\r
        {\r
                Canvas cnvs;\r
@@ -13,37 +14,46 @@ namespace PsychlopsSilverlight3test
                double tfreq = 1;\r
                int frames;\r
 \r
-               public System.Collections.Generic.IEnumerator<int> psychlops_main()\r
+               public void psychlops_main()\r
                {\r
                        cnvs = new Canvas(500, 500);\r
                        img = new Image(isize * 2, isize * 2);\r
                        fixation = new Rectangle(10, 10);\r
-                       var poly = new Letters("日本語");\r
+                       fixation.fill = Color.red;\r
+                       //var poly = new Letters("日本語");\r
+                       var poly = new Polygon(); poly.append(0, 100); poly.append(-100, 0); poly.append(0, -100); poly.append(100, 0);\r
+                       //var poly = new Ellipse(100, 100);\r
+                       //var poly = new Line(0,0,100, 0);\r
+                       poly.fill = Color.red;\r
+                       poly.stroke.set(Color.green, 10);\r
                        shape = poly;\r
-                       cnvs.clear(Color.gray);\r
+\r
 \r
                        while(true) {\r
                                frames++;\r
                                \r
-                               cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.25));\r
+                               cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.5));\r
                                col.set(Math.random(1.0));\r
 \r
                                fixation.centering();\r
                                fixation.draw(Color.red);\r
 \r
-                               Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60);\r
+                               //Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60);\r
                                //Figures.drawGaussian(ref img, 20, 1);\r
-                               //Figures.drawGrating(10, 1, 2, cnvs.frame * 2.0 * Math.PI / tfreq / 60);\r
+                               Figures.drawGrating(ref img, 500, 500, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60);\r
                                img.centering(Mouse.position);\r
                                img.draw();\r
 \r
-                               shape.centering(Mouse.position);\r
-                               shape.draw(Color.red);\r
+                               shape.centering(Mouse.position).shift(100,0);\r
+                               shape.draw();\r
                                if (Keyboard.spc.pressed()) cnvs.var(Mouse.x, 100, 100);\r
 \r
                                cnvs.var(frames, 10, 20);\r
 \r
-                               yield return 3;\r
+                               fixation.shift(100,100);\r
+                               fixation.draw(Color.blue);\r
+\r
+                               cnvs.flip();\r
                        }\r
                }\r
        }\r