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
19 public class StackableDrawable : Drawable
\r
21 protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;
\r
23 public StackableDrawable()
\r
25 stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();
\r
32 public virtual void clear(Color col)
\r
34 //rect(back_panel, col);
\r
37 public virtual void pix(int x, int y, Color col)
\r
41 public virtual void line(Line drawee)
\r
43 stack.Enqueue(drawee.clone());
\r
46 public virtual void rect(Rectangle drawee)
\r
48 stack.Enqueue(drawee.clone());
\r
51 public virtual void ellipse(Ellipse drawee)
\r
53 stack.Enqueue(drawee.clone());
\r
56 public virtual void polygon(Polygon drawee)
\r
58 stack.Enqueue(drawee.clone());
\r
61 public virtual void letters(Letters drawee)
\r
63 stack.Enqueue(drawee.clone());
\r
66 public virtual void image(Image drawee)
\r
68 stack.Enqueue(drawee.clone());
\r
71 public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }
\r
72 public virtual void msg(string str, double x, double y, Color col)
\r
74 var let = new Letters(str);
\r
79 public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }
\r
80 public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }
\r
82 public virtual Point getCenter() { return new Point(0, 0, 0); }
\r
87 public class Canvas : Templates.StackableDrawable
\r
92 internal delegate void TwoIntProcedure(int x, int y);
\r
93 internal delegate void SimpleProcedure();
\r
94 SimpleProcedure flipexec;
\r
96 //public static System.Windows.Controls.Image default_buffer_frame;
\r
97 public static System.Windows.Controls.UserControl default_panel;
\r
98 public static System.Windows.Controls.Canvas default_api_canvas;
\r
99 public static WriteableBitmap default_buffer;
\r
100 //WriteableBitmap buffer;
\r
101 //System.Windows.Controls.Image instance;
\r
102 System.Windows.Controls.Canvas api_canvas;
\r
103 System.Windows.Controls.UserControl panel;
\r
104 Rectangle back_panel;
\r
105 double width_, height_;
\r
107 public Canvas(int wid, int hei)
\r
109 panel = default_panel;
\r
110 api_canvas = default_api_canvas;
\r
111 initialize(wid, hei);
\r
113 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)
\r
116 api_canvas = apicnvs;
\r
117 initialize(wid, hei);
\r
119 protected void initialize(int wid, int hei)
\r
124 api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);
\r
125 Mouse._prime = api_canvas;
\r
126 Main.drawable = this;
\r
127 Main.canvas = this;
\r
129 back_panel = new Rectangle(wid, hei);
\r
131 flipexec = new SimpleProcedure(executeFlip);
\r
133 protected void initialize__(int wid, int hei)
\r
135 api_canvas.Width = wid;
\r
136 api_canvas.Height = hei;
\r
137 api_canvas.MouseMove += Mouse.Canvas_MousePos;
\r
138 api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;
\r
139 api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;
\r
140 api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;
\r
141 panel.KeyDown += Keyboard.Canvas_KeyDown;
\r
142 panel.KeyUp += Keyboard.Canvas_KeyUp;
\r
147 #region static initializer
\r
149 static System.Windows.Shapes.Line api_line;
\r
150 static System.Windows.Shapes.Path api_curve;
\r
151 static System.Windows.Shapes.Rectangle api_rect;
\r
152 static System.Windows.Shapes.Ellipse api_ellipse;
\r
153 static System.Windows.Shapes.Polygon api_polygon;
\r
154 static System.Windows.Shapes.Polyline api_polyline;
\r
155 static System.Windows.Media.Color api_color;
\r
156 static System.Windows.Media.SolidColorBrush api_fill;
\r
157 static System.Windows.Media.SolidColorBrush api_stroke;
\r
158 static System.Windows.Media.TranslateTransform api_translation;
\r
161 api_line = new System.Windows.Shapes.Line();
\r
162 api_curve = new System.Windows.Shapes.Path();
\r
163 api_rect = new System.Windows.Shapes.Rectangle();
\r
164 api_ellipse = new System.Windows.Shapes.Ellipse();
\r
165 api_polygon = new System.Windows.Shapes.Polygon();
\r
166 api_polyline = new System.Windows.Shapes.Polyline();
\r
167 api_color = new System.Windows.Media.Color();
\r
168 api_fill = new System.Windows.Media.SolidColorBrush();
\r
169 api_stroke = new System.Windows.Media.SolidColorBrush();
\r
170 api_translation = new System.Windows.Media.TranslateTransform();
\r
176 public override void clear(Color col)
\r
178 back_panel.fill = col;
\r
182 int nextIntervalFrame = 1;
\r
185 //api_canvas.Dispatcher.BeginInvoke(flipexec);
\r
187 //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
\r
190 nextIntervalFrame = 1;
\r
192 Internal.Main.canvas_flag.WaitOne();
\r
194 public void executeFlip()
\r
198 nextIntervalFrame--;
\r
200 if (nextIntervalFrame <= 0)
\r
202 var tmp = new System.Windows.Controls.Canvas();
\r
203 foreach (Internal.PrimitiveFigure f in stack)
\r
205 tmp.Children.Add(f.toNative());
\r
207 api_canvas.Children.Clear();
\r
208 api_canvas.Children.Add(tmp);
\r
210 Psychlops.Internal.Main.canvas_flag.Set();
\r
212 System.Threading.Thread.Sleep(0);
\r
217 public double width { get { return width_; } }
\r
218 public double height { get { return height_; } }
\r
219 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }
\r
220 public double getWidth() { return width; }
\r
221 public double getHeight() { return height; }
\r
222 public override Point getCenter() { return center; }
\r
223 public double getHCenter() { return width / 2; }
\r
224 public double getVCenter() { return height / 2; }
\r
225 public double getRefreshRate() { return 60; }
\r
234 #region primitive tokenizer
\r
236 partial struct Point
\r
238 public static implicit operator System.Windows.Point(Point d)
\r
240 return new System.Windows.Point(d.x, d.y);
\r
244 partial struct Color
\r
246 public static implicit operator System.Windows.Media.Color(Color d)
\r
248 return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));
\r
250 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)
\r
252 return new SolidColorBrush { Color = d };
\r
256 partial struct Stroke
\r
258 public void apply(System.Windows.Shapes.Shape target)
\r
260 target.Stroke = this;
\r
261 //target.StrokeDashArray
\r
262 target.StrokeThickness = thick;
\r
264 public static implicit operator SolidColorBrush(Stroke d)
\r
266 return new SolidColorBrush { Color = d.color };
\r
272 public Line clone()
\r
274 return (Line)MemberwiseClone();
\r
276 public static implicit operator System.Windows.Shapes.Line(Line d)
\r
278 var tmp = new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };
\r
279 if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;
\r
280 else d.stroke.apply(tmp);
\r
283 public UIElement toNative() { return this; }
\r
286 partial class Rectangle
\r
288 public Rectangle clone()
\r
290 return (Rectangle)MemberwiseClone();
\r
292 public static implicit operator System.Windows.Rect(Rectangle d)
\r
294 return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);
\r
296 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)
\r
298 var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };
\r
299 d.stroke.apply(tmp);
\r
300 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
301 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
304 public UIElement toNative() { return this; }
\r
307 partial class Ellipse
\r
309 public Ellipse clone()
\r
311 return (Ellipse)MemberwiseClone();
\r
313 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)
\r
315 var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };
\r
316 d.stroke.apply(tmp);
\r
317 System.Windows.Controls.Canvas.SetLeft(tmp, d.left);
\r
318 System.Windows.Controls.Canvas.SetTop(tmp, d.top);
\r
321 public UIElement toNative() { return this; }
\r
324 partial class Polygon
\r
326 public Polygon clone()
\r
328 return (Polygon)MemberwiseClone();
\r
330 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)
\r
332 var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };
\r
333 d.stroke.apply(tmp);
\r
334 foreach (Point p in d.vertices)
\r
338 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
339 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
342 public UIElement toNative() { return this; }
\r
345 partial class Letters
\r
347 #region static initializer
\r
348 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;
\r
349 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;
\r
350 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;
\r
353 FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();
\r
354 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);
\r
355 FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);
\r
356 FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();
\r
357 FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);
\r
358 FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);
\r
359 FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);
\r
360 LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();
\r
361 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);
\r
362 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);
\r
363 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);
\r
364 LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);
\r
367 public Letters clone()
\r
369 return (Letters)MemberwiseClone();
\r
371 public static implicit operator System.Windows.Controls.TextBlock(Letters d)
\r
373 //var zapi_shape = new System.Windows.Documents.Glyphs();
\r
374 var tmp = new System.Windows.Controls.TextBlock {
\r
375 Text = d.str, Width = 500, Height = 500,
\r
376 FontSize = d.font.size,
\r
377 //tmp.FontFamily = ,
\r
378 FontStyle = FONT_STYLE_BRIDGE[d.font.style],
\r
379 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],
\r
380 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],
\r
381 Foreground = d.fill
\r
386 case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;
\r
387 case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = tmp.Width / 2; break;
\r
388 case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = tmp.Width; break;
\r
390 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);
\r
391 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);
\r
394 public UIElement toNative() { return this; }
\r
397 partial class Image
\r
399 internal void initialize__(int wid, int hei)
\r
401 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);
\r
402 //buffer = new WriteableBitmap(wid, hei);
\r
404 internal void create__(int wid, int hei)
\r
406 buffer = new WriteableBitmap(wid, hei);
\r
408 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);
\r
409 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);
\r
410 public void field__(System.Func<int, int, System.Windows.Media.Color> func)
\r
412 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);
\r
413 //buffer.ForEach(func);
\r
415 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
417 Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);
\r
418 //buffer.ForEach(func);
\r
420 public void field___(System.Func<int, int, System.Windows.Media.Color> func)
\r
422 buffer.ForEach(func);
\r
424 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)
\r
426 buffer.ForEach(func);
\r
429 public Image clone()
\r
431 return (Image)MemberwiseClone();
\r
433 public static implicit operator System.Windows.Controls.Image(Image d)
\r
435 var tmp = new System.Windows.Controls.Image();
\r
436 tmp.Source = d.buffer;
\r
437 System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);
\r
438 System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);
\r
441 public UIElement toNative() { return this; }
\r