using System.Windows.Media.Animation;\r
using System.Windows.Media.Imaging;\r
using System.Windows.Shapes;\r
+using System.Windows.Browser;\r
\r
\r
\r
namespace Psychlops\r
{\r
\r
- public class Canvas : Drawable\r
+ namespace Templates\r
{\r
- public static System.Windows.Controls.Image default_instance;\r
+\r
+ public class StackableDrawable : Drawable\r
+ {\r
+ protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
+\r
+ public StackableDrawable()\r
+ {\r
+ stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
+ }\r
+\r
+ public void clear()\r
+ {\r
+ clear(Color.black);\r
+ }\r
+ public virtual void clear(Color col)\r
+ {\r
+ //rect(back_panel, col);\r
+ }\r
+\r
+ public virtual void pix(int x, int y, Color col)\r
+ {\r
+ }\r
+\r
+ public virtual void line(Line drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public virtual void rect(Rectangle drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public virtual void ellipse(Ellipse drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public virtual void polygon(Polygon drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public virtual void letters(Letters drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public virtual void image(Image drawee)\r
+ {\r
+ stack.Enqueue(drawee.clone());\r
+ }\r
+\r
+ public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
+ public virtual void msg(string str, double x, double y, Color col)\r
+ {\r
+ var let = new Letters(str);\r
+ let.locate(x, y);\r
+ let.fill = col;\r
+ this.letters(let);\r
+ }\r
+ public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
+ public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
+\r
+ public virtual Point getCenter() { return new Point(0, 0, 0); }\r
+ }\r
+\r
+ }\r
+\r
+ public class Canvas : Templates.StackableDrawable\r
+ {\r
+\r
+ #region initializer\r
+\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 WriteableBitmap default_buffer; \r
- System.Windows.Controls.Image instance;\r
- System.Windows.Controls.UserControl panel; \r
- WriteableBitmap buffer;\r
+ public static System.Windows.Controls.Canvas default_api_canvas;\r
+ public static WriteableBitmap default_buffer;\r
+ //WriteableBitmap buffer;\r
+ //System.Windows.Controls.Image instance;\r
+ System.Windows.Controls.Canvas api_canvas;\r
+ System.Windows.Controls.UserControl panel;\r
+ Rectangle back_panel;\r
+ double width_, height_;\r
\r
public Canvas(int wid, int hei)\r
{\r
panel = default_panel;\r
- instance = default_instance;\r
- instance.Width = wid;\r
- instance.Height = hei;\r
- buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
- instance.Source = buffer;\r
- instance.MouseMove += Mouse.Canvas_MousePos;\r
- instance.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
- instance.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
- instance.MouseWheel += Mouse.Canvas_MouseWheel;\r
- Mouse._prime = instance;\r
- Main.drawable = this;\r
- default_buffer = buffer;\r
+ api_canvas = default_api_canvas;\r
+ initialize(wid, hei);\r
}\r
- public Canvas(int wid, int hei, System.Windows.Controls.Image target, 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
- instance = target;\r
- instance.Width = wid;\r
- instance.Height = hei;\r
- buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
- instance = target;\r
- instance.Source = buffer;\r
- instance.MouseMove += Mouse.Canvas_MousePos;\r
- instance.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
- instance.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
- instance.MouseWheel += Mouse.Canvas_MouseWheel;\r
- Mouse._prime = instance;\r
+ api_canvas = apicnvs;\r
+ initialize(wid, hei);\r
+ }\r
+ protected void initialize(int wid, int hei)\r
+ {\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
+ flipexec = new SimpleProcedure(executeFlip);\r
}\r
- public Point getCenter()\r
+ protected void initialize__(int wid, int hei)\r
{\r
- return new Point(instance.Width/2.0, instance.Height/2.0, 0);\r
+ api_canvas.Width = wid;\r
+ api_canvas.Height = hei;\r
+ api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
+ api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
+ api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
+ api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
+ panel.KeyDown += Keyboard.Canvas_KeyDown;\r
+ panel.KeyUp += Keyboard.Canvas_KeyUp;\r
+\r
+ HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
+ //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
+ htmlHost.SetStyleAttribute("width", (200+wid).ToString()+"px");\r
+ htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
+ htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
}\r
\r
- public void clear()\r
+ #endregion\r
+\r
+ #region static initializer\r
+ /*\r
+ static System.Windows.Shapes.Line api_line;\r
+ static System.Windows.Shapes.Path api_curve;\r
+ static System.Windows.Shapes.Rectangle api_rect;\r
+ static System.Windows.Shapes.Ellipse api_ellipse;\r
+ static System.Windows.Shapes.Polygon api_polygon;\r
+ static System.Windows.Shapes.Polyline api_polyline;\r
+ static System.Windows.Media.Color api_color;\r
+ static System.Windows.Media.SolidColorBrush api_fill;\r
+ static System.Windows.Media.SolidColorBrush api_stroke;\r
+ static System.Windows.Media.TranslateTransform api_translation;\r
+ static Canvas()\r
{\r
- buffer.Clear(Color.black);\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
+ api_ellipse = new System.Windows.Shapes.Ellipse();\r
+ api_polygon = new System.Windows.Shapes.Polygon();\r
+ api_polyline = new System.Windows.Shapes.Polyline();\r
+ api_color = new System.Windows.Media.Color();\r
+ 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
- public void clear(Color col)\r
+ */\r
+ #endregion\r
+\r
+\r
+ public override void clear(Color col)\r
{\r
- buffer.Clear(col);\r
+ back_panel.fill = col;\r
+ rect(back_panel);\r
}\r
\r
- public void pix(int x, int y, Color col)\r
+ int nextIntervalFrame = 1;\r
+ public void flip()\r
{\r
- buffer.SetPixel(x, y, col);\r
+ //api_canvas.Dispatcher.BeginInvoke(flipexec);\r
+ //flipexec();\r
+ //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);\r
+ lock (this)\r
+ {\r
+ nextIntervalFrame = 1;\r
+ }\r
+ Internal.Main.canvas_flag.WaitOne();\r
}\r
+ public void executeFlip()\r
+ {\r
+ lock (this)\r
+ {\r
+ nextIntervalFrame--;\r
+ }\r
+ if (nextIntervalFrame <= 0)\r
+ {\r
+ var tmp = new System.Windows.Controls.Canvas();\r
+ if (stack.Count > 0)\r
+ {\r
+ foreach (Internal.PrimitiveFigure f in stack)\r
+ {\r
+ tmp.Children.Add(f.toNative());\r
+ }\r
+ }\r
+ api_canvas.Children.Clear();\r
+ api_canvas.Children.Add(tmp);\r
+ stack.Clear();\r
+ Psychlops.Internal.Main.canvas_flag.Set();\r
+ }\r
+ System.Threading.Thread.Sleep(0);\r
+ }\r
+\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 override 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
- public void line(Line drawee, Color col)\r
+\r
+ #region primitive tokenizer\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
- buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
+ return new SolidColorBrush { Color = d.color };\r
}\r
- public void rect(Rectangle drawee, Color col)\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
- buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col);\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 void ellipse(Ellipse drawee, Color col)\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
- 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
+ return (Ellipse)MemberwiseClone();\r
}\r
- public void polygon(Polygon drawee, Color col)\r
+ public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
{\r
- int[] ps = new int[drawee.vertices.Count];\r
- int i=0;\r
- foreach (Point p in drawee.vertices)\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
- ps[i] = (int)p.x; i++;\r
- ps[i] = (int)p.y; i++;\r
+ tmp.Points.Add(p);\r
}\r
- buffer.DrawPolyline(ps, col);\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 void image(Image drawee)\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
- buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\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 void flip()\r
+ public Image clone()\r
+ {\r
+ return (Image)MemberwiseClone();\r
+ }\r
+ public static implicit operator System.Windows.Controls.Image(Image d)\r
{\r
- buffer.Invalidate();\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
+ #endregion\r
+\r
\r
}
\ No newline at end of file