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
24 public StackableDrawable()
\r
26 stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();
\r
33 public virtual void clear(Color col)
\r
35 //rect(back_panel, col);
\r
38 public virtual void pix(int x, int y, Color col)
\r
42 public virtual void line(Line drawee)
\r
44 stack.Enqueue(drawee.clone());
\r
47 public virtual void rect(Rectangle drawee)
\r
49 stack.Enqueue(drawee.clone());
\r
52 public virtual void ellipse(Ellipse drawee)
\r
54 stack.Enqueue(drawee.clone());
\r
57 public virtual void polygon(Polygon drawee)
\r
59 stack.Enqueue(drawee.clone());
\r
62 public virtual void letters(Letters drawee)
\r
64 stack.Enqueue(drawee.clone());
\r
67 public virtual void image(Image drawee)
\r
69 stack.Enqueue(drawee.clone());
\r
72 public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }
\r
73 public virtual void msg(string str, double x, double y, Color col)
\r
75 var let = new Letters(str);
\r
80 public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }
\r
81 public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }
\r
83 public virtual Point getCenter() { return new Point(0, 0, 0); }
\r
88 public class Canvas : Templates.StackableDrawable
\r
93 internal delegate void TwoIntProcedure(int x, int y);
\r
94 internal delegate void SimpleProcedure();
\r
95 SimpleProcedure flipexec;
\r
97 //public static System.Windows.Controls.Image default_buffer_frame;
\r
98 public static System.Windows.Controls.UserControl default_panel;
\r
99 public static System.Windows.Controls.Canvas default_api_canvas;
\r
100 public static WriteableBitmap default_buffer;
\r
101 //WriteableBitmap buffer;
\r
102 //System.Windows.Controls.Image instance;
\r
103 System.Windows.Controls.Canvas api_canvas;
\r
104 System.Windows.Controls.UserControl panel;
\r
105 Rectangle back_panel;
\r
106 double width_, height_;
\r
108 public Canvas(int wid, int hei)
\r
110 panel = default_panel;
\r
111 api_canvas = default_api_canvas;
\r
112 initialize(wid, hei);
\r
114 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)
\r
117 api_canvas = apicnvs;
\r
118 initialize(wid, hei);
\r
120 protected void initialize(int wid, int hei)
\r
125 api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);
\r
126 Mouse._prime = api_canvas;
\r
127 Main.drawable = this;
\r
128 Main.canvas = this;
\r
130 back_panel = new Rectangle(wid, hei);
\r
132 flipexec = new SimpleProcedure(executeFlip);
\r
134 protected void initialize__(int wid, int hei)
\r
136 api_canvas.Width = wid;
\r
137 api_canvas.Height = hei;
\r
138 api_canvas.MouseMove += Mouse.Canvas_MousePos;
\r
139 api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;
\r
140 api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;
\r
141 api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;
\r
142 panel.KeyDown += Keyboard.Canvas_KeyDown;
\r
143 panel.KeyUp += Keyboard.Canvas_KeyUp;
\r
145 HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");
\r
146 //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");
\r
147 htmlHost.SetStyleAttribute("width", (200+wid).ToString()+"px");
\r
148 htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");
\r
149 htmlHost.SetStyleAttribute("margin", "2em auto auto auto");
\r
154 #region static initializer
\r
156 static System.Windows.Shapes.Line api_line;
\r
157 static System.Windows.Shapes.Path api_curve;
\r
158 static System.Windows.Shapes.Rectangle api_rect;
\r
159 static System.Windows.Shapes.Ellipse api_ellipse;
\r
160 static System.Windows.Shapes.Polygon api_polygon;
\r
161 static System.Windows.Shapes.Polyline api_polyline;
\r
162 static System.Windows.Media.Color api_color;
\r
163 static System.Windows.Media.SolidColorBrush api_fill;
\r
164 static System.Windows.Media.SolidColorBrush api_stroke;
\r
165 static System.Windows.Media.TranslateTransform api_translation;
\r
168 api_line = new System.Windows.Shapes.Line();
\r
169 api_curve = new System.Windows.Shapes.Path();
\r
170 api_rect = new System.Windows.Shapes.Rectangle();
\r
171 api_ellipse = new System.Windows.Shapes.Ellipse();
\r
172 api_polygon = new System.Windows.Shapes.Polygon();
\r
173 api_polyline = new System.Windows.Shapes.Polyline();
\r
174 api_color = new System.Windows.Media.Color();
\r
175 api_fill = new System.Windows.Media.SolidColorBrush();
\r
176 api_stroke = new System.Windows.Media.SolidColorBrush();
\r
177 api_translation = new System.Windows.Media.TranslateTransform();
\r
183 public override void clear(Color col)
\r
185 back_panel.fill = col;
\r
189 int nextIntervalFrame = 1;
\r
192 //api_canvas.Dispatcher.BeginInvoke(flipexec);
\r
194 //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
\r
197 nextIntervalFrame = 1;
\r
199 Internal.Main.canvas_flag.WaitOne();
\r
201 public void executeFlip()
\r
205 nextIntervalFrame--;
\r
207 if (nextIntervalFrame <= 0)
\r
209 var tmp = new System.Windows.Controls.Canvas();
\r
210 if (stack.Count > 0)
\r
212 foreach (Internal.PrimitiveFigure f in stack)
\r
214 tmp.Children.Add(f.toNative());
\r
217 api_canvas.Children.Clear();
\r
218 api_canvas.Children.Add(tmp);
\r
220 Psychlops.Internal.Main.canvas_flag.Set();
\r
222 System.Threading.Thread.Sleep(0);
\r
227 public double width { get { return width_; } }
\r
228 public double height { get { return height_; } }
\r
229 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }
\r
230 public double getWidth() { return width; }
\r
231 public double getHeight() { return height; }
\r
232 public override Point getCenter() { return center; }
\r
233 public double getHCenter() { return width / 2; }
\r
234 public double getVCenter() { return height / 2; }
\r
235 public double getRefreshRate() { return 60; }
\r
244 #region primitive tokenizer
\r
246 partial struct Point
\r
248 public static implicit operator System.Windows.Point(Point d)
\r
250 return new System.Windows.Point(d.x, d.y);
\r
254 partial struct Color
\r
256 public static implicit operator System.Windows.Media.Color(Color d)
\r
258 return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));
\r
260 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)
\r
262 return new SolidColorBrush { Color = d };
\r
266 partial struct Stroke
\r
268 public void apply(System.Windows.Shapes.Shape target)
\r
270 target.Stroke = this;
\r
271 //target.StrokeDashArray
\r
272 target.StrokeThickness = thick;
\r
274 public static implicit operator SolidColorBrush(Stroke d)
\r
276 return new SolidColorBrush { Color = d.color };
\r
282 public Line clone()
\r
284 return (Line)MemberwiseClone();
\r
286 public static implicit operator System.Windows.Shapes.Line(Line d)
\r
288 var tmp = new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };
\r
289 if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;
\r
290 else d.stroke.apply(tmp);
\r
293 public UIElement toNative() { return this; }
\r
296 partial class Rectangle
\r
298 public Rectangle clone()
\r
300 return (Rectangle)MemberwiseClone();
\r
302 public static implicit operator System.Windows.Rect(Rectangle d)
\r
304 return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);
\r
306 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)
\r
308 var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };
\r
309 d.stroke.apply(tmp);
\r
310 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
311 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
314 public UIElement toNative() { return this; }
\r
317 partial class Ellipse
\r
319 public Ellipse clone()
\r
321 return (Ellipse)MemberwiseClone();
\r
323 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)
\r
325 var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };
\r
326 d.stroke.apply(tmp);
\r
327 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
328 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
331 public UIElement toNative() { return this; }
\r
334 partial class Polygon
\r
336 public Polygon clone()
\r
338 return (Polygon)MemberwiseClone();
\r
340 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)
\r
342 var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };
\r
343 d.stroke.apply(tmp);
\r
344 foreach (Point p in d.vertices)
\r
348 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
349 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
352 public UIElement toNative() { return this; }
\r
355 partial class Letters
\r
357 #region static initializer
\r
358 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;
\r
359 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;
\r
360 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;
\r
363 FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();
\r
364 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);
\r
365 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);
\r
366 FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();
\r
367 FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);
\r
368 FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);
\r
369 FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);
\r
370 LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();
\r
371 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);
\r
372 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);
\r
373 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);
\r
374 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);
\r
377 public Letters clone()
\r
379 return (Letters)MemberwiseClone();
\r
381 public static implicit operator System.Windows.Controls.TextBlock(Letters d)
\r
383 //var zapi_shape = new System.Windows.Documents.Glyphs();
\r
384 var tmp = new System.Windows.Controls.TextBlock {
\r
385 Text = d.str, Width = 500, Height = 500,
\r
386 FontSize = d.font.size,
\r
387 //tmp.FontFamily = ,
\r
388 FontStyle = FONT_STYLE_BRIDGE[d.font.style],
\r
389 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],
\r
390 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],
\r
391 Foreground = d.fill
\r
396 case Letters.HorizontalAlign.left: break;
\r
397 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;
\r
398 case Letters.HorizontalAlign.right: left = tmp.Width; break;
\r
400 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);
\r
401 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);
\r
404 public UIElement toNative() { return this; }
\r
407 partial class Image
\r
409 internal void initialize__(int wid, int hei)
\r
411 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);
\r
412 //buffer = new WriteableBitmap(wid, hei);
\r
414 internal void create__(int wid, int hei)
\r
416 buffer = new WriteableBitmap(wid, hei);
\r
418 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);
\r
419 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);
\r
420 public void field__(System.Func<int, int, System.Windows.Media.Color> func)
\r
422 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);
\r
423 //buffer.ForEach(func);
\r
425 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
427 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);
\r
428 //buffer.ForEach(func);
\r
430 public void field___(System.Func<int, int, System.Windows.Media.Color> func)
\r
432 buffer.ForEach(func);
\r
434 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
436 buffer.ForEach(func);
\r
439 public Image clone()
\r
441 return (Image)MemberwiseClone();
\r
443 public static implicit operator System.Windows.Controls.Image(Image d)
\r
445 var tmp = new System.Windows.Controls.Image();
\r
446 tmp.Source = d.buffer;
\r
447 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
448 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
451 public UIElement toNative() { return this; }
\r