From 2d76f63033cd4044624e9fcf1330389f515d9c8c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 19 Mar 2010 11:27:28 +0900 Subject: [PATCH] flip ok --- dev3/psychlops/core/app/misc.cs | 3 + dev3/psychlops/core/device/hid.cs | 24 +- dev3/psychlops/core/graphic/canvas.cs | 465 +++++++++++++++++++-------- dev3/psychlops/core/graphic/font.cs | 8 +- dev3/psychlops/core/graphic/module.cs | 33 +- dev3/psychlops/core/graphic/shape.cs | 51 +-- dev3/psychlops/extention/standard/figures.cs | 8 - test3/MainPage.xaml.cs | 15 +- test3/PsychlopsMain.cs | 28 +- 9 files changed, 425 insertions(+), 210 deletions(-) diff --git a/dev3/psychlops/core/app/misc.cs b/dev3/psychlops/core/app/misc.cs index 581af3d..87aa503 100644 --- a/dev3/psychlops/core/app/misc.cs +++ b/dev3/psychlops/core/app/misc.cs @@ -4,5 +4,8 @@ public static class Main { public static Drawable drawable; + public static Canvas canvas; + public static System.Threading.Thread routine; + public static System.Threading.AutoResetEvent flag; } } diff --git a/dev3/psychlops/core/device/hid.cs b/dev3/psychlops/core/device/hid.cs index a8bdc53..b71caeb 100644 --- a/dev3/psychlops/core/device/hid.cs +++ b/dev3/psychlops/core/device/hid.cs @@ -11,19 +11,30 @@ namespace Psychlops { public struct Button { + private static object locker; + static Button() + { + locker = new object(); + } private bool pushed_, released_, pressed_; public void down() { - pushed_ = true; - pressed_ = true; + lock (locker) + { + pushed_ = true; + pressed_ = true; + } } public void up() { - released_ = true; - pressed_ = false; + lock (locker) + { + released_ = true; + pressed_ = false; + } } - public bool pushed() { bool tmp = pushed_; pushed_ = false; return tmp; } - public bool released() { bool tmp = released_; released_ = false; return tmp; } + public bool pushed() { lock (locker) { bool tmp = pushed_; pushed_ = false; return tmp; } } + public bool released() { lock (locker) { bool tmp = released_; released_ = false; return tmp; } } public bool pressed() { return pressed_; } } } @@ -94,7 +105,6 @@ namespace Psychlops static private System.Collections.Generic.Dictionary map; static public void Canvas_KeyUp(object sender, KeyEventArgs e) { - Main.drawable.msg("hoge", 0,0,Color.green); if (map.ContainsKey(e.Key)) map[e.Key].up(); } static public void Canvas_KeyDown(object sender, KeyEventArgs e) diff --git a/dev3/psychlops/core/graphic/canvas.cs b/dev3/psychlops/core/graphic/canvas.cs index fcfd6e7..f621e31 100644 --- a/dev3/psychlops/core/graphic/canvas.cs +++ b/dev3/psychlops/core/graphic/canvas.cs @@ -15,6 +15,14 @@ namespace Psychlops public class Canvas : Drawable { + + #region initializer + + System.Collections.Generic.Queue
stock; + 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; @@ -23,30 +31,38 @@ namespace Psychlops //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; + 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.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; - //buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height); - //default_buffer = 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); + + stock = new System.Collections.Generic.Queue
(); + flipexec = new SimpleProcedure(executeFlip); + } + protected void initialize__(int wid, int hei) + { api_canvas.Width = wid; api_canvas.Height = hei; api_canvas.MouseMove += Mouse.Canvas_MousePos; @@ -55,37 +71,9 @@ namespace Psychlops 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); - } + #endregion #region static initializer /* @@ -99,26 +87,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(); @@ -129,108 +99,80 @@ namespace Psychlops api_fill = new System.Windows.Media.SolidColorBrush(); api_stroke = new System.Windows.Media.SolidColorBrush(); api_translation = new System.Windows.Media.TranslateTransform(); - */ } + */ #endregion + + public void clear() + { + clear(Color.black); + } + public void clear(Color col) + { + //api_canvas.Children.Clear(); + rect(back_panel, col); + } + + public void pix(int x, int y, Color col) + { + //buffer.SetPixel(x, y, col); + } + 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); + var tmp = drawee.clone(); + tmp.fill = col; + stock.Enqueue(tmp); + } + public void line(Line drawee) + { + stock.Enqueue(drawee.clone()); } 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); + var tmp = drawee.clone(); + tmp.fill = col; + stock.Enqueue(tmp); + } + public void rect(Rectangle drawee) + { + stock.Enqueue(drawee.clone()); } 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); + var tmp = drawee.clone(); + tmp.fill = col; + stock.Enqueue(tmp); + } + public void ellipse(Ellipse drawee) + { + stock.Enqueue(drawee.clone()); } 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); + var tmp = drawee.clone(); + tmp.fill = col; + stock.Enqueue(tmp); + } + public void polygon(Polygon drawee) + { + stock.Enqueue(drawee.clone()); } 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); + var tmp = drawee.clone(); + tmp.fill = col; + stock.Enqueue(tmp); + } + public void letters(Letters drawee) + { + stock.Enqueue(drawee.clone()); } 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); + stock.Enqueue(drawee.clone()); } @@ -247,11 +189,252 @@ namespace Psychlops public void flip() { - //buffer.Invalidate(); + //api_canvas.Dispatcher.BeginInvoke(flipexec); + //flipexec(); + //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); + Main.flag.WaitOne(); + } + + public void executeFlip() + { + api_canvas.Children.Clear(); + foreach (Figure f in stock) + { + api_canvas.Children.Add(f.toNative()); + } + stock.Clear(); } - public double width { get { return api_canvas.Width; } } - public double height { get { return api_canvas.Height; } } + #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 Point getCenter() { return center; } + public double getHCenter() { return width / 2; } + public double getVCenter() { return height / 2; } + public double getRefreshRate() { return 60; } + + #endregion + + + } + + + + + 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; } + } + + 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.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); + } + #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.TEXT_ALIGN_LEFT: break; + case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = tmp.Width / 2; break; + case Letters.HorizontalAlign.TEXT_ALIGN_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 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; } + } + + internal class CanvasTokenizer + { } diff --git a/dev3/psychlops/core/graphic/font.cs b/dev3/psychlops/core/graphic/font.cs index 4984b3a..8c8140c 100644 --- a/dev3/psychlops/core/graphic/font.cs +++ b/dev3/psychlops/core/graphic/font.cs @@ -54,7 +54,7 @@ namespace Psychlops } } - public class Letters : Shape + public partial class Letters : Shape { protected string str_; protected Font font_; @@ -120,12 +120,16 @@ namespace Psychlops public void draw() { - Main.drawable.letters(this, Color.white); + Main.drawable.letters(this); } public void draw(Color c) { Main.drawable.letters(this, c); } + + public Color fill { get; set; } + public Stroke stroke { get; set; } + } } diff --git a/dev3/psychlops/core/graphic/module.cs b/dev3/psychlops/core/graphic/module.cs index 67bf68f..43b7caa 100644 --- a/dev3/psychlops/core/graphic/module.cs +++ b/dev3/psychlops/core/graphic/module.cs @@ -13,7 +13,7 @@ using System.Windows.Shapes; namespace Psychlops { - public struct Point + public partial struct Point { public double x, y, z; public Point(double dx, double dy, double dz) @@ -51,14 +51,10 @@ namespace Psychlops { return new Point(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z); } - public static implicit operator System.Windows.Point(Point d) - { - return new System.Windows.Point(d.x, d.y); - } } - public struct Color + public partial struct Color { public double r, g, b, a; public Color(double lum) @@ -99,10 +95,6 @@ namespace Psychlops b = blue; a = alpha; } - 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 readonly Color black = new Color(0, 0, 0, 1), @@ -123,11 +115,21 @@ namespace Psychlops Point getCenter(); void clear(Color col); void pix(int x, int y, Color col); + void line(Line drawee); void line(Line drawee, Color col); + //void line(Line drawee, Stroke strk); + void rect(Rectangle drawee); void rect(Rectangle drawee, Color col); + //void rect(Rectangle drawee, Stroke strk); + void ellipse(Ellipse drawee); void ellipse(Ellipse drawee, Color col); + //void ellipse(Ellipse drawee, Stroke strk); + void polygon(Polygon drawee); void polygon(Polygon drawee, Color col); + //void polygon(Polygon drawee, Stroke strk); + void letters(Letters drawee); void letters(Letters drawee, Color col); + //void letters(Letters drawee, Stroke strk); void image(Image drawee); void msg(string s, double x, double y, Color c); } @@ -139,6 +141,7 @@ namespace Psychlops Figure shift(Point p); Figure centering(Point p); void draw(); + UIElement toNative(); } public static class FigureExtention { @@ -156,7 +159,7 @@ namespace Psychlops } } - public class Image : Figure + public partial class Image : Figure { public WriteableBitmap buffer; public Point datum; @@ -164,7 +167,7 @@ namespace Psychlops public Image(int wid, int hei) { - buffer = new WriteableBitmap(wid, hei); + initialize__(wid, hei); self_rect = new Rectangle(wid, hei); } @@ -194,11 +197,13 @@ namespace Psychlops public void field(System.Func func) { - buffer.ForEach(func); + field__(func); + //buffer.ForEach(func); } public void field(System.Func func) { - buffer.ForEach(func); + field__(func); + //buffer.ForEach(func); } public void draw() diff --git a/dev3/psychlops/core/graphic/shape.cs b/dev3/psychlops/core/graphic/shape.cs index 9533e8e..2933260 100644 --- a/dev3/psychlops/core/graphic/shape.cs +++ b/dev3/psychlops/core/graphic/shape.cs @@ -15,6 +15,8 @@ namespace Psychlops{ public interface Shape : Figure { void draw(Color c); + Color fill { get; set; } + Stroke stroke { get; set; } } public static class ShapeExtention { @@ -26,12 +28,18 @@ namespace Psychlops{ */ } - public struct Stroke + public partial struct Stroke { - + public double thick; + public Color color; + public void set(Color c, double t) + { + color = c; + thick = t; + } } - public class Line : Shape + public partial class Line : Shape { public Point begin, end; @@ -76,7 +84,7 @@ namespace Psychlops{ } public void draw() { - Main.drawable.line(this, Color.white); + Main.drawable.line(this); } public double left { get { return begin.x < end.x ? begin.x : end.x; } } @@ -86,19 +94,12 @@ namespace Psychlops{ public double width { get { return Math.abs(begin.x - end.x); } } public double height { get { return Math.abs(begin.y - end.y); } } - public static implicit operator System.Windows.Shapes.Line(Line d) - { - var tmp = new System.Windows.Shapes.Line(); - tmp.X1 = d.begin.x; - tmp.Y1 = d.begin.y; - tmp.X2 = d.end.x; - tmp.Y2 = d.end.y; - return tmp; - } + public Color fill { get; set; } + public Stroke stroke { get; set; } } - public class Rectangle : Shape + public partial class Rectangle : Shape { public Point v1, v2; @@ -140,7 +141,7 @@ namespace Psychlops{ } public void draw() { - Main.drawable.rect(this, Color.white); + Main.drawable.rect(this); } public double left { get { return v1.x; } } @@ -151,15 +152,13 @@ namespace Psychlops{ public double height { get { return Math.abs(v1.y - v2.y); } } - 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 Color fill { get; set; } + public Stroke stroke { get; set; } } - public class Ellipse : Shape + public partial class Ellipse : Shape { public Point datum; public double xdiameter, ydiameter; @@ -196,7 +195,7 @@ namespace Psychlops{ } public void draw() { - Main.drawable.ellipse(this, Color.white); + Main.drawable.ellipse(this); } public double left { get { return datum.x - xdiameter/2.0; } } @@ -205,10 +204,13 @@ namespace Psychlops{ public double bottom { get { return datum.y + ydiameter / 2.0; } } public double width { get { return Math.abs(xdiameter); } } public double height { get { return Math.abs(ydiameter); } } + + public Color fill { get; set; } + public Stroke stroke { get; set; } } - public class Polygon : Shape + public partial class Polygon : Shape { public Point datum; public System.Collections.Generic.List vertices; @@ -257,8 +259,11 @@ namespace Psychlops{ } public void draw() { - Main.drawable.polygon(this, Color.white); + Main.drawable.polygon(this); } + + public Color fill { get; set; } + public Stroke stroke { get; set; } } } \ No newline at end of file diff --git a/dev3/psychlops/extention/standard/figures.cs b/dev3/psychlops/extention/standard/figures.cs index dd26370..a7a8273 100644 --- a/dev3/psychlops/extention/standard/figures.cs +++ b/dev3/psychlops/extention/standard/figures.cs @@ -6,14 +6,6 @@ namespace Psychlops public static class Figures { - public static void drawGrating(double wavelength, double contrast, double orientation, double phase) - { - double freq = 2 * Math.PI / wavelength; - Canvas.default_buffer.ForEach( - (x, y) => new Color(.5 + .5 * Math.sin(phase + (Math.sin(orientation) * x - Math.cos(orientation) * y) * freq)) - ); - } - public static void drawGrating(ref Image img, int width, int height, double wavelength, double contrast, double orientation, double phase) { double width_half = width / 2.0, height_half = height / 2.0; diff --git a/test3/MainPage.xaml.cs b/test3/MainPage.xaml.cs index dcfdd2d..f2d8960 100644 --- a/test3/MainPage.xaml.cs +++ b/test3/MainPage.xaml.cs @@ -4,7 +4,7 @@ namespace PsychlopsSilverlight3test { public partial class MainPage : System.Windows.Controls.UserControl { - System.Collections.Generic.IEnumerator main_routine; + //System.Collections.Generic.IEnumerator main_routine; public System.Windows.Controls.Image master; public PsychlopsMain main; #if DEBUG @@ -30,9 +30,10 @@ namespace PsychlopsSilverlight3test main = new PsychlopsMain(); //main.initialize(); - //main_routine = new System.Threading.Thread(ThreadTest2); - //main_routine.Start(); - main_routine = main.psychlops_main(); + Main.flag = new System.Threading.AutoResetEvent(false); + Main.routine = new System.Threading.Thread(main.psychlops_main); + Main.routine.Start(); + //main_routine = main.psychlops_main(); #if DEBUG DebugConsole = new System.Windows.Controls.TextBlock(); TotalRoot.Children.Add(DebugConsole); @@ -45,11 +46,13 @@ namespace PsychlopsSilverlight3test int nextIntervalFrame = 1; private void CompositionTarget_Rendering(object sender, System.EventArgs e) { + if (Main.canvas != null) Main.canvas.executeFlip(); + Main.flag.Set(); nextIntervalFrame--; if (nextIntervalFrame<=0) { - main_routine.MoveNext(); - nextIntervalFrame = main_routine.Current; + //main_routine.MoveNext(); + //nextIntervalFrame = main_routine.Current; } } diff --git a/test3/PsychlopsMain.cs b/test3/PsychlopsMain.cs index fabaa51..30683f8 100644 --- a/test3/PsychlopsMain.cs +++ b/test3/PsychlopsMain.cs @@ -2,6 +2,7 @@ namespace PsychlopsSilverlight3test { + public class PsychlopsMain { Canvas cnvs; @@ -13,37 +14,46 @@ namespace PsychlopsSilverlight3test double tfreq = 1; int frames; - public System.Collections.Generic.IEnumerator psychlops_main() + public void psychlops_main() { cnvs = new Canvas(500, 500); img = new Image(isize * 2, isize * 2); fixation = new Rectangle(10, 10); - var poly = new Letters("日本語"); + fixation.fill = Color.red; + //var poly = new Letters("日本語"); + var poly = new Polygon(); poly.append(0, 100); poly.append(-100, 0); poly.append(0, -100); poly.append(100, 0); + //var poly = new Ellipse(100, 100); + //var poly = new Line(0,0,100, 0); + poly.fill = Color.red; + poly.stroke.set(Color.green, 10); shape = poly; - cnvs.clear(Color.gray); + while(true) { frames++; - cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.25)); + cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.5)); col.set(Math.random(1.0)); fixation.centering(); fixation.draw(Color.red); - Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60); + //Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60); //Figures.drawGaussian(ref img, 20, 1); - //Figures.drawGrating(10, 1, 2, cnvs.frame * 2.0 * Math.PI / tfreq / 60); + Figures.drawGrating(ref img, 500, 500, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60); img.centering(Mouse.position); img.draw(); - shape.centering(Mouse.position); - shape.draw(Color.red); + shape.centering(Mouse.position).shift(100,0); + shape.draw(); if (Keyboard.spc.pressed()) cnvs.var(Mouse.x, 100, 100); cnvs.var(frames, 10, 20); - yield return 3; + fixation.shift(100,100); + fixation.draw(Color.blue); + + cnvs.flip(); } } } -- 2.11.0