2 using System.Windows;
\r
3 using System.Windows.Controls;
\r
4 using System.Windows.Documents;
\r
5 using System.Windows.Input;
\r
6 using System.Windows.Media;
\r
7 using System.Windows.Media.Animation;
\r
8 using System.Windows.Media.Imaging;
\r
9 using System.Windows.Shapes;
\r
10 using System.Windows.Browser;
\r
20 public class StackableDrawable : Drawable
\r
22 // protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;
\r
23 internal Internal.PrimitiveFigure[] stack;
\r
24 internal int stackN;
\r
25 internal Line[] lineStack;
\r
26 internal int lineStackN;
\r
27 internal Rectangle[] rectStack;
\r
28 internal int rectStackN;
\r
31 public StackableDrawable()
\r
33 // stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();
\r
34 stack = new Internal.PrimitiveFigure[40000];
\r
35 lineStack = new Line[10000];
\r
36 rectStack = new Rectangle[10000];
\r
37 for (int i = 0; i < 10000; i++)
\r
39 lineStack[i] = new Line(0,0,0,0);
\r
40 rectStack[i] = new Rectangle();
\r
48 public virtual void clear(Color col)
\r
50 //rect(back_panel, col);
\r
53 public virtual void pix(int x, int y, Color col)
\r
57 public virtual void line(Line drawee)
\r
59 // stack.Enqueue(drawee.clone());
\r
62 public virtual void rect(Rectangle drawee)
\r
64 //stack.Enqueue(drawee.clone());
\r
65 // stack.Enqueue(drawee.copyToStack(this));
\r
66 drawee.copyToStack(this);
\r
69 public virtual void ellipse(Ellipse drawee)
\r
71 // stack.Enqueue(drawee.clone());
\r
74 public virtual void polygon(Polygon drawee)
\r
76 // stack.Enqueue(drawee.clone());
\r
79 public virtual void letters(Letters drawee)
\r
81 // stack.Enqueue(drawee.clone());
\r
84 public virtual void image(Image drawee)
\r
86 // stack.Enqueue(drawee.clone());
\r
89 public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }
\r
90 public virtual void msg(string dstr, double x, double y, Color col)
\r
92 var let = new Letters(dstr);
\r
97 public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }
\r
98 public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }
\r
100 public virtual Point getCenter() { return new Point(0, 0, 0); }
\r
105 public class Canvas : Templates.StackableDrawable
\r
108 internal System.Windows.Controls.Canvas masterPool, prevPool;
\r
109 internal System.Windows.Point[] pointPool;
\r
110 internal int pointPoolN;
\r
111 internal SolidColorBrush[] brushPool;
\r
112 internal int brushPoolN;
\r
113 internal System.Windows.Shapes.Line[] linePool;
\r
114 internal int linePoolN;
\r
115 internal System.Windows.Shapes.Rectangle[] rectPool;
\r
116 internal int rectPoolN;
\r
118 #region initializer
\r
120 internal delegate void TwoIntProcedure(int x, int y);
\r
121 internal delegate void SimpleProcedure();
\r
122 SimpleProcedure flipexec;
\r
124 //public static System.Windows.Controls.Image default_buffer_frame;
\r
125 public static System.Windows.Controls.UserControl default_panel;
\r
126 public static System.Windows.Controls.Canvas default_api_canvas;
\r
127 public static WriteableBitmap default_buffer;
\r
128 //WriteableBitmap buffer;
\r
129 //System.Windows.Controls.Image instance;
\r
130 System.Windows.Controls.Canvas api_canvas;
\r
131 System.Windows.Controls.UserControl panel;
\r
132 Rectangle back_panel;
\r
133 double width_, height_;
\r
135 public Canvas(int wid, int hei)
\r
137 panel = default_panel;
\r
138 api_canvas = default_api_canvas;
\r
139 initialize(wid, hei);
\r
141 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)
\r
144 api_canvas = apicnvs;
\r
145 initialize(wid, hei);
\r
147 protected void initialize(int wid, int hei)
\r
151 api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);
\r
152 Mouse._prime = api_canvas;
\r
153 Main.drawable = this;
\r
154 Main.canvas = this;
\r
156 back_panel = new Rectangle(wid, hei);
\r
158 flipexec = new SimpleProcedure(executeFlip);
\r
160 protected void initialize__(int wid, int hei)
\r
162 api_canvas.Width = wid;
\r
163 api_canvas.Height = hei;
\r
164 api_canvas.MouseMove += Mouse.Canvas_MousePos;
\r
165 api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;
\r
166 api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;
\r
167 api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;
\r
168 panel.KeyDown += Keyboard.Canvas_KeyDown;
\r
169 panel.KeyUp += Keyboard.Canvas_KeyUp;
\r
171 HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");
\r
172 //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");
\r
173 htmlHost.SetStyleAttribute("width", (wid).ToString()+"px");
\r
174 htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");
\r
175 htmlHost.SetStyleAttribute("margin", "2em auto auto auto");
\r
178 pointPool = new System.Windows.Point[10000];
\r
179 brushPool = new SolidColorBrush[10000];
\r
180 linePool = new System.Windows.Shapes.Line[10000];
\r
181 rectPool = new System.Windows.Shapes.Rectangle[10000];
\r
182 for (int i = 0; i < 10000; i++)
\r
184 pointPool[i] = new System.Windows.Point();
\r
185 brushPool[i] = new SolidColorBrush();
\r
186 linePool[i] = new System.Windows.Shapes.Line();
\r
187 rectPool[i] = new System.Windows.Shapes.Rectangle();
\r
189 for (int i = 0; i < 300; i++)
\r
191 api_canvas.Children.Add(rectStack[i].poolNative(this));
\r
192 api_canvas.Children.Add(rectStack[i].poolNative(this));
\r
195 masterPool = new System.Windows.Controls.Canvas();
\r
196 prevPool = new System.Windows.Controls.Canvas();
\r
197 // masterPool.Children.Add()
\r
202 #region static initializer
\r
204 static System.Windows.Shapes.Line api_line;
\r
205 static System.Windows.Shapes.Path api_curve;
\r
206 static System.Windows.Shapes.Rectangle api_rect;
\r
207 static System.Windows.Shapes.Ellipse api_ellipse;
\r
208 static System.Windows.Shapes.Polygon api_polygon;
\r
209 static System.Windows.Shapes.Polyline api_polyline;
\r
210 static System.Windows.Media.Color api_color;
\r
211 static System.Windows.Media.SolidColorBrush api_fill;
\r
212 static System.Windows.Media.SolidColorBrush api_stroke;
\r
213 static System.Windows.Media.TranslateTransform api_translation;
\r
216 api_line = new System.Windows.Shapes.Line();
\r
217 api_curve = new System.Windows.Shapes.Path();
\r
218 api_rect = new System.Windows.Shapes.Rectangle();
\r
219 api_ellipse = new System.Windows.Shapes.Ellipse();
\r
220 api_polygon = new System.Windows.Shapes.Polygon();
\r
221 api_polyline = new System.Windows.Shapes.Polyline();
\r
222 api_color = new System.Windows.Media.Color();
\r
223 api_fill = new System.Windows.Media.SolidColorBrush();
\r
224 api_stroke = new System.Windows.Media.SolidColorBrush();
\r
225 api_translation = new System.Windows.Media.TranslateTransform();
\r
231 public override void clear(Color col)
\r
233 back_panel.fill = col;
\r
239 int nextIntervalFrame = 1, chacked = 0;
\r
242 //api_canvas.Dispatcher.BeginInvoke(flipexec);
\r
244 //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
\r
247 nextIntervalFrame = 1;
\r
256 Internal.Main.canvas_flag.WaitOne();
\r
258 public void executeFlip()
\r
262 nextIntervalFrame--;
\r
264 if (nextIntervalFrame <= 0)
\r
266 var rr = new Random();
\r
267 //Psychlops.AppState.statusBar = chacked.ToString();
\r
270 // prevPool.Children.Clear();
\r
271 // var tmp = new System.Windows.Controls.Canvas();
\r
272 // if (stack.Count > 0)
\r
275 //foreach (UIElement e in api_canvas.Children)
\r
278 // foreach (Internal.PrimitiveFigure f in stack)
\r
279 for (int i = 0; i < stackN - 2; i++)
\r
281 // tmp.Children.Add(f.toNative());
\r
282 // tmp.Children.Add(f.poolNative(this));
\r
283 // tmp.Children.Add(stack[i].poolNative(this));
\r
284 System.Windows.Controls.Canvas.SetLeft(api_canvas.Children[i + 1], rectStack[i].left);
\r
285 api_canvas.Children[0] = api_canvas.Children[0];
\r
290 // api_canvas.Children.Clear();
\r
291 // api_canvas.Children.Add(tmp);
\r
297 Psychlops.Internal.Main.canvas_flag.Set();
\r
301 System.Threading.Thread.Sleep(0);
\r
306 public double width { get { return width_; } }
\r
307 public double height { get { return height_; } }
\r
308 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }
\r
309 public double getWidth() { return width; }
\r
310 public double getHeight() { return height; }
\r
311 public override Point getCenter() { return center; }
\r
312 public double getHCenter() { return width / 2; }
\r
313 public double getVCenter() { return height / 2; }
\r
314 public double getRefreshRate() { return 60; }
\r
323 #region primitive tokenizer
\r
325 partial struct Point
\r
327 public static implicit operator System.Windows.Point(Point d)
\r
329 return new System.Windows.Point(d.x, d.y);
\r
333 partial struct Color
\r
335 public static implicit operator System.Windows.Media.Color(Color d)
\r
337 return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));
\r
339 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)
\r
341 return new SolidColorBrush { Color = d };
\r
345 partial struct Stroke
\r
347 public void apply(System.Windows.Shapes.Shape target)
\r
349 target.Stroke = this;
\r
350 //target.StrokeDashArray
\r
351 target.StrokeThickness = thick;
\r
353 public static implicit operator SolidColorBrush(Stroke d)
\r
355 return new SolidColorBrush { Color = d.color };
\r
363 return (Line)MemberwiseClone();
\r
365 public Line clone()
\r
367 return (Line)MemberwiseClone();
\r
369 public static implicit operator System.Windows.Shapes.Line(Line d)
\r
371 var tmp = new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };
\r
372 if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;
\r
373 else d.stroke.apply(tmp);
\r
376 public UIElement toNative() { return this; }
\r
378 public UIElement poolNative(Canvas d)
\r
380 var tmp = d.linePool[d.linePoolN];
\r
385 if (stroke.thick == 0.0) tmp.Stroke = fill;
\r
386 else stroke.apply(tmp);
\r
387 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
388 System.Windows.Controls.Canvas.SetTop(tmp, top);
\r
389 tmp.Visibility = Visibility.Visible;
\r
395 partial class Rectangle
\r
397 public Rectangle dup()
\r
399 return (Rectangle)MemberwiseClone();
\r
401 public Rectangle clone()
\r
403 return (Rectangle)MemberwiseClone();
\r
405 public void copyToStack(Templates.StackableDrawable d)
\r
407 var tmp = d.rectStack[d.rectStackN];
\r
411 d.stack[d.stackN] = tmp;
\r
415 public static implicit operator System.Windows.Rect(Rectangle d)
\r
417 return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);
\r
419 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)
\r
421 var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };
\r
422 d.stroke.apply(tmp);
\r
423 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
424 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
427 public UIElement toNative() { return this; }
\r
429 public UIElement poolNative(Canvas d)
\r
431 var tmp = d.rectPool[d.rectPoolN];
\r
433 tmp.Height = height;
\r
435 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
436 System.Windows.Controls.Canvas.SetTop(tmp, top);
\r
437 tmp.Visibility = Visibility.Visible;
\r
443 partial class Ellipse
\r
445 public Ellipse dup()
\r
447 return (Ellipse)MemberwiseClone();
\r
449 public Ellipse clone()
\r
451 return (Ellipse)MemberwiseClone();
\r
453 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)
\r
455 var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };
\r
456 d.stroke.apply(tmp);
\r
457 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
458 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
461 public UIElement toNative() { return this; }
\r
463 public UIElement poolNative(Canvas d)
\r
465 /* var tmp = d.rectPool[d.rectPoolN];
\r
467 tmp.Height = height;
\r
469 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
470 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
471 tmp.Visibility = Visibility.Visible;
\r
478 partial class Polygon
\r
480 public Polygon dup()
\r
482 return (Polygon)MemberwiseClone();
\r
484 public Polygon clone()
\r
486 return (Polygon)MemberwiseClone();
\r
488 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)
\r
490 var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };
\r
491 d.stroke.apply(tmp);
\r
492 foreach (Point p in d.vertices)
\r
496 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
497 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
500 public UIElement toNative() { return this; }
\r
502 public UIElement poolNative(Canvas d)
\r
504 /* var tmp = d.rectPool[d.rectPoolN];
\r
506 tmp.Height = height;
\r
508 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
509 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
510 tmp.Visibility = Visibility.Visible;
\r
518 partial class Letters
\r
520 #region static initializer
\r
521 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;
\r
522 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;
\r
523 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;
\r
526 FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();
\r
527 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);
\r
528 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);
\r
529 FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();
\r
530 FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);
\r
531 FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);
\r
532 FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);
\r
533 LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();
\r
534 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);
\r
535 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);
\r
536 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);
\r
537 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);
\r
540 public Letters dup()
\r
542 return (Letters)MemberwiseClone();
\r
544 public Letters clone()
\r
546 return (Letters)MemberwiseClone();
\r
548 public static implicit operator System.Windows.Controls.TextBlock(Letters d)
\r
550 //var zapi_shape = new System.Windows.Documents.Glyphs();
\r
551 var tmp = new System.Windows.Controls.TextBlock {
\r
552 Text = d.str, Width = 500, Height = 500,
\r
553 FontSize = d.font.size,
\r
554 //tmp.FontFamily = ,
\r
555 FontStyle = FONT_STYLE_BRIDGE[d.font.style],
\r
556 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],
\r
557 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],
\r
558 Foreground = d.fill
\r
563 case Letters.HorizontalAlign.left: break;
\r
564 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;
\r
565 case Letters.HorizontalAlign.right: left = tmp.Width; break;
\r
567 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);
\r
568 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);
\r
571 public UIElement toNative() { return this; }
\r
573 public UIElement poolNative(Canvas d)
\r
575 /* var tmp = d.rectPool[d.rectPoolN];
\r
577 tmp.Height = height;
\r
579 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
580 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
581 tmp.Visibility = Visibility.Visible;
\r
588 partial class Image
\r
590 internal void initialize__(int wid, int hei)
\r
592 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);
\r
593 //buffer = new WriteableBitmap(wid, hei);
\r
595 internal void create__(int wid, int hei)
\r
597 buffer = new WriteableBitmap(wid, hei);
\r
599 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);
\r
600 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);
\r
601 public void field__(System.Func<int, int, System.Windows.Media.Color> func)
\r
603 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);
\r
604 //buffer.ForEach(func);
\r
606 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
608 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);
\r
609 //buffer.ForEach(func);
\r
611 public void field___(System.Func<int, int, System.Windows.Media.Color> func)
\r
613 buffer.ForEach(func);
\r
615 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
617 buffer.ForEach(func);
\r
620 public Image clone()
\r
622 return (Image)MemberwiseClone();
\r
624 public static implicit operator System.Windows.Controls.Image(Image d)
\r
626 var tmp = new System.Windows.Controls.Image();
\r
627 tmp.Source = d.buffer;
\r
628 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
629 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
632 public UIElement toNative() { return this; }
\r
634 public UIElement poolNative(Canvas d)
\r
636 /* var tmp = d.rectPool[d.rectPoolN];
\r
638 tmp.Height = height;
\r
640 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
641 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
642 tmp.Visibility = Visibility.Visible;
\r