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 (Internal.PrimitiveFigure f in stack)
\r
276 for (int i = 0; i < stackN - 2; i++ )
\r
278 // tmp.Children.Add(f.toNative());
\r
279 // tmp.Children.Add(f.poolNative(this));
\r
280 // tmp.Children.Add(stack[i].poolNative(this));
\r
281 System.Windows.Controls.Canvas.SetLeft(api_canvas.Children[i + 1], rectStack[i].left);
\r
282 api_canvas.Children[0] = api_canvas.Children[0];
\r
287 // api_canvas.Children.Clear();
\r
288 // api_canvas.Children.Add(tmp);
\r
294 Psychlops.Internal.Main.canvas_flag.Set();
\r
298 System.Threading.Thread.Sleep(0);
\r
303 public double width { get { return width_; } }
\r
304 public double height { get { return height_; } }
\r
305 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }
\r
306 public double getWidth() { return width; }
\r
307 public double getHeight() { return height; }
\r
308 public override Point getCenter() { return center; }
\r
309 public double getHCenter() { return width / 2; }
\r
310 public double getVCenter() { return height / 2; }
\r
311 public double getRefreshRate() { return 60; }
\r
320 #region primitive tokenizer
\r
322 partial struct Point
\r
324 public static implicit operator System.Windows.Point(Point d)
\r
326 return new System.Windows.Point(d.x, d.y);
\r
330 partial struct Color
\r
332 public static implicit operator System.Windows.Media.Color(Color d)
\r
334 return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));
\r
336 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)
\r
338 return new SolidColorBrush { Color = d };
\r
342 partial struct Stroke
\r
344 public void apply(System.Windows.Shapes.Shape target)
\r
346 target.Stroke = this;
\r
347 //target.StrokeDashArray
\r
348 target.StrokeThickness = thick;
\r
350 public static implicit operator SolidColorBrush(Stroke d)
\r
352 return new SolidColorBrush { Color = d.color };
\r
360 return (Line)MemberwiseClone();
\r
362 public Line clone()
\r
364 return (Line)MemberwiseClone();
\r
366 public static implicit operator System.Windows.Shapes.Line(Line d)
\r
368 var tmp = new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };
\r
369 if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;
\r
370 else d.stroke.apply(tmp);
\r
373 public UIElement toNative() { return this; }
\r
375 public UIElement poolNative(Canvas d)
\r
377 var tmp = d.linePool[d.linePoolN];
\r
382 if (stroke.thick == 0.0) tmp.Stroke = fill;
\r
383 else stroke.apply(tmp);
\r
384 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
385 System.Windows.Controls.Canvas.SetTop(tmp, top);
\r
386 tmp.Visibility = Visibility.Visible;
\r
392 partial class Rectangle
\r
394 public Rectangle dup()
\r
396 return (Rectangle)MemberwiseClone();
\r
398 public Rectangle clone()
\r
400 return (Rectangle)MemberwiseClone();
\r
402 public void copyToStack(Templates.StackableDrawable d)
\r
404 var tmp = d.rectStack[d.rectStackN];
\r
408 d.stack[d.stackN] = tmp;
\r
412 public static implicit operator System.Windows.Rect(Rectangle d)
\r
414 return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);
\r
416 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)
\r
418 var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };
\r
419 d.stroke.apply(tmp);
\r
420 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
421 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
424 public UIElement toNative() { return this; }
\r
426 public UIElement poolNative(Canvas d)
\r
428 var tmp = d.rectPool[d.rectPoolN];
\r
430 tmp.Height = height;
\r
432 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
433 System.Windows.Controls.Canvas.SetTop(tmp, top);
\r
434 tmp.Visibility = Visibility.Visible;
\r
440 partial class Ellipse
\r
442 public Ellipse dup()
\r
444 return (Ellipse)MemberwiseClone();
\r
446 public Ellipse clone()
\r
448 return (Ellipse)MemberwiseClone();
\r
450 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)
\r
452 var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };
\r
453 d.stroke.apply(tmp);
\r
454 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
455 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
458 public UIElement toNative() { return this; }
\r
460 public UIElement poolNative(Canvas d)
\r
462 /* var tmp = d.rectPool[d.rectPoolN];
\r
464 tmp.Height = height;
\r
466 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
467 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
468 tmp.Visibility = Visibility.Visible;
\r
475 partial class Polygon
\r
477 public Polygon dup()
\r
479 return (Polygon)MemberwiseClone();
\r
481 public Polygon clone()
\r
483 return (Polygon)MemberwiseClone();
\r
485 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)
\r
487 var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };
\r
488 d.stroke.apply(tmp);
\r
489 foreach (Point p in d.vertices)
\r
493 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
494 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
497 public UIElement toNative() { return this; }
\r
499 public UIElement poolNative(Canvas d)
\r
501 /* var tmp = d.rectPool[d.rectPoolN];
\r
503 tmp.Height = height;
\r
505 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
506 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
507 tmp.Visibility = Visibility.Visible;
\r
515 partial class Letters
\r
517 #region static initializer
\r
518 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;
\r
519 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;
\r
520 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;
\r
523 FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();
\r
524 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);
\r
525 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);
\r
526 FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();
\r
527 FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);
\r
528 FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);
\r
529 FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);
\r
530 LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();
\r
531 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);
\r
532 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);
\r
533 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);
\r
534 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);
\r
537 public Letters dup()
\r
539 return (Letters)MemberwiseClone();
\r
541 public Letters clone()
\r
543 return (Letters)MemberwiseClone();
\r
545 public static implicit operator System.Windows.Controls.TextBlock(Letters d)
\r
547 //var zapi_shape = new System.Windows.Documents.Glyphs();
\r
548 var tmp = new System.Windows.Controls.TextBlock {
\r
549 Text = d.str, Width = 500, Height = 500,
\r
550 FontSize = d.font.size,
\r
551 //tmp.FontFamily = ,
\r
552 FontStyle = FONT_STYLE_BRIDGE[d.font.style],
\r
553 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],
\r
554 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],
\r
555 Foreground = d.fill
\r
560 case Letters.HorizontalAlign.left: break;
\r
561 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;
\r
562 case Letters.HorizontalAlign.right: left = tmp.Width; break;
\r
564 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);
\r
565 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);
\r
568 public UIElement toNative() { return this; }
\r
570 public UIElement poolNative(Canvas d)
\r
572 /* var tmp = d.rectPool[d.rectPoolN];
\r
574 tmp.Height = height;
\r
576 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
577 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
578 tmp.Visibility = Visibility.Visible;
\r
585 partial class Image
\r
587 internal void initialize__(int wid, int hei)
\r
589 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);
\r
590 //buffer = new WriteableBitmap(wid, hei);
\r
592 internal void create__(int wid, int hei)
\r
594 buffer = new WriteableBitmap(wid, hei);
\r
596 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);
\r
597 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);
\r
598 public void field__(System.Func<int, int, System.Windows.Media.Color> func)
\r
600 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);
\r
601 //buffer.ForEach(func);
\r
603 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
605 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);
\r
606 //buffer.ForEach(func);
\r
608 public void field___(System.Func<int, int, System.Windows.Media.Color> func)
\r
610 buffer.ForEach(func);
\r
612 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
614 buffer.ForEach(func);
\r
617 public Image clone()
\r
619 return (Image)MemberwiseClone();
\r
621 public static implicit operator System.Windows.Controls.Image(Image d)
\r
623 var tmp = new System.Windows.Controls.Image();
\r
624 tmp.Source = d.buffer;
\r
625 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
626 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
629 public UIElement toNative() { return this; }
\r
631 public UIElement poolNative(Canvas d)
\r
633 /* var tmp = d.rectPool[d.rectPoolN];
\r
635 tmp.Height = height;
\r
637 System.Windows.Controls.Canvas.SetLeft(tmp, left);
\r
638 System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);
\r
639 tmp.Visibility = Visibility.Visible;
\r