OSDN Git Service

282d67a7da856f06a6bc24836da4de2de6ec0273
[psychlops/silverlight.git] / dev4 / psychlops / core / graphic / canvas.cs
1 using System;\r
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
11 \r
12 \r
13 \r
14 namespace Psychlops\r
15 {\r
16 \r
17         namespace Templates\r
18         {\r
19 \r
20                 public class StackableDrawable : Drawable\r
21                 {\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
29                         internal Ellipse[] ellipseStack;\r
30                         internal int ellipseStackN;\r
31                         internal Polygon[] polygonStack;\r
32                         internal int polygonStackN;\r
33                         internal Letters[] lettersStack;\r
34                         internal int lettersStackN;\r
35                         internal Image[] imageStack;\r
36                         internal int imageStackN;\r
37 \r
38 \r
39                         public StackableDrawable()\r
40                         {\r
41 //                              stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
42                                 stack = new Internal.PrimitiveFigure[40000];\r
43                                 lineStack = new Line[10000];\r
44                                 rectStack = new Rectangle[10000];\r
45                                 ellipseStack = new Ellipse[10000];\r
46                                 polygonStack = new Polygon[1000];\r
47                                 lettersStack = new Letters[1000];\r
48                                 imageStack = new Image[1000];\r
49                                 for (int i = 0; i < 10000; i++)\r
50                                 {\r
51                                         lineStack[i] = new Line(0,0,0,0);\r
52                                         rectStack[i] = new Rectangle();\r
53                                         ellipseStack[i] = new Ellipse();\r
54                                 }\r
55                                 for (int i = 0; i < 1000; i++)\r
56                                 {\r
57                                         polygonStack[i] = new Polygon();\r
58                                         lettersStack[i] = new Letters();\r
59                                         imageStack[i] = new Image(1,1);\r
60                                 }\r
61                         }\r
62 \r
63                         public void clear()\r
64                         {\r
65                                 clear(Color.black);\r
66                         }\r
67                         public virtual void clear(Color col)\r
68                         {\r
69                                 //rect(back_panel, col);\r
70                         }\r
71 \r
72                         public virtual void pix(int x, int y, Color col)\r
73                         {\r
74                         }\r
75 \r
76                         public virtual void line(Line drawee)\r
77                         {\r
78                                 //                              stack.Enqueue(drawee.clone());\r
79                                 drawee.copyToStack(this);\r
80                         }\r
81 \r
82                         public virtual void rect(Rectangle drawee)\r
83                         {\r
84                                 //stack.Enqueue(drawee.clone());\r
85 //                              stack.Enqueue(drawee.copyToStack(this));\r
86                                 drawee.copyToStack(this);\r
87                         }\r
88 \r
89                         public virtual void ellipse(Ellipse drawee)\r
90                         {\r
91                                 //                              stack.Enqueue(drawee.clone());\r
92                                 drawee.copyToStack(this);\r
93                         }\r
94 \r
95                         public virtual void polygon(Polygon drawee)\r
96                         {\r
97                                 //                              stack.Enqueue(drawee.clone());\r
98                                 drawee.copyToStack(this);\r
99                         }\r
100 \r
101                         public virtual void letters(Letters drawee)\r
102                         {\r
103                                 //                              stack.Enqueue(drawee.clone());\r
104                                 drawee.copyToStack(this);\r
105                         }\r
106 \r
107                         public virtual void image(Image drawee)\r
108                         {\r
109                                 //                              stack.Enqueue(drawee.clone());\r
110                                 drawee.copyToStack(this);\r
111                         }\r
112                         public virtual void group(Group drawee)\r
113                         {\r
114                                 drawee.copyToStack(this);\r
115                         }\r
116 \r
117                         public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
118                         public virtual void msg(string dstr, double x, double y, Color col)\r
119                         {\r
120                                 var let = new Letters(dstr);\r
121                                 let.locate(x, y);\r
122                                 let.fill = col;\r
123                                 this.letters(let);\r
124                         }\r
125                         public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
126                         public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
127 \r
128                         public virtual Point getCenter() { return new Point(0, 0, 0); }\r
129                 }\r
130 \r
131         }\r
132 \r
133         public class Canvas : Templates.StackableDrawable\r
134         {\r
135 \r
136                 internal System.Windows.Controls.Canvas masterPool, prevPool;\r
137                 internal System.Windows.Point[] pointPool;\r
138                 internal int pointPoolN;\r
139                 internal SolidColorBrush[] brushPool;\r
140                 internal int brushPoolN;\r
141 \r
142                 internal System.Windows.UIElement[] UIElementPool;\r
143                 internal int UIElementPoolN;\r
144 \r
145                 internal System.Windows.Shapes.Line[] linePool;\r
146                 internal int linePoolN;\r
147                 internal System.Windows.Shapes.Rectangle[] rectPool;\r
148                 internal int rectPoolN;\r
149                 internal System.Windows.Shapes.Ellipse[] ellipsePool;\r
150                 internal int ellipsePoolN;\r
151                 internal System.Windows.Shapes.Polygon[] polygonPool;\r
152                 internal int polygonPoolN;\r
153                 internal System.Windows.Controls.TextBlock[] lettersPool;\r
154                 internal int lettersPoolN;\r
155                 internal System.Windows.Controls.Image[] imagePool;\r
156                 internal int imagePoolN;\r
157 \r
158                 #region initializer\r
159 \r
160                 internal delegate void TwoIntProcedure(int x, int y);\r
161                 internal delegate void SimpleProcedure();\r
162                 SimpleProcedure flipexec;\r
163 \r
164                 //public static System.Windows.Controls.Image default_buffer_frame;\r
165                 public static System.Windows.Controls.UserControl default_panel;\r
166                 public static System.Windows.Controls.Canvas default_api_canvas;\r
167                 public static WriteableBitmap default_buffer;\r
168                 //WriteableBitmap buffer;\r
169                 //System.Windows.Controls.Image instance;\r
170                 System.Windows.Controls.Canvas api_canvas;\r
171                 System.Windows.Controls.UserControl panel;\r
172                 Rectangle back_panel;\r
173                 double width_, height_;\r
174 \r
175                 public Canvas(int wid, int hei)\r
176                 {\r
177                         panel = default_panel;\r
178                         api_canvas = default_api_canvas;\r
179                         initialize(wid, hei);\r
180                 }\r
181                 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)\r
182                 {\r
183                         panel = system;\r
184                         api_canvas = apicnvs;\r
185                         initialize(wid, hei);\r
186                 }\r
187 \r
188                 protected bool AsyncInitBool;\r
189                 protected void initialize(int wid, int hei)\r
190                 {\r
191                         AsyncInitBool = false;\r
192                         width_ = wid;\r
193                         height_ = hei;\r
194                         api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
195                         while(!AsyncInitBool)\r
196                         {\r
197                         }\r
198                         Mouse._prime = api_canvas;\r
199                         Main.drawable = this;\r
200                         Main.canvas = this;\r
201 \r
202                         back_panel = new Rectangle(wid, hei);\r
203 \r
204                         flipexec = new SimpleProcedure(executeFlip);\r
205                         AppState.statusBar = "";\r
206                 }\r
207                 protected void initialize__(int wid, int hei)\r
208                 {\r
209                         api_canvas.Width = wid;\r
210                         api_canvas.Height = hei;\r
211                         api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
212                         api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
213                         api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
214                         api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
215                         panel.KeyDown += Keyboard.Canvas_KeyDown;\r
216                         panel.KeyUp += Keyboard.Canvas_KeyUp;\r
217 \r
218                         HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
219                         //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
220                         htmlHost.SetStyleAttribute("width", (wid).ToString()+"px");\r
221                         htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
222                         htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
223 \r
224                         UIElementPool = new System.Windows.UIElement[10000];\r
225                         pointPool = new System.Windows.Point[10000];\r
226                         brushPool = new SolidColorBrush[10000];\r
227                         linePool = new System.Windows.Shapes.Line[10000];\r
228                         rectPool = new System.Windows.Shapes.Rectangle[10000];\r
229                         ellipsePool = new System.Windows.Shapes.Ellipse[10000];\r
230                         for (int i = 0; i < 10000; i++)\r
231                         {\r
232                                 pointPool[i] = new System.Windows.Point();\r
233                                 brushPool[i] = new SolidColorBrush();\r
234                                 linePool[i] = new System.Windows.Shapes.Line();\r
235                                 rectPool[i] = new System.Windows.Shapes.Rectangle();\r
236                                 ellipsePool[i] = new System.Windows.Shapes.Ellipse();\r
237                         }\r
238                         polygonPool = new System.Windows.Shapes.Polygon[1000];\r
239                         lettersPool = new System.Windows.Controls.TextBlock[1000];\r
240                         for (int i = 0; i < 1000; i++)\r
241                         {\r
242                                 polygonPool[i] = new System.Windows.Shapes.Polygon();\r
243                                 lettersPool[i] = new System.Windows.Controls.TextBlock();\r
244                         }\r
245                         imagePool = new System.Windows.Controls.Image[100];\r
246                         for (int i = 0; i < 100; i++)\r
247                         {\r
248                                 imagePool[i] = new System.Windows.Controls.Image();\r
249                         }\r
250 \r
251                         masterPool = new System.Windows.Controls.Canvas();\r
252                         prevPool = new System.Windows.Controls.Canvas();\r
253                         api_canvas.Children.Add(masterPool);\r
254                         //api_canvas.Children.Remove(Internal.Main.widgetStack);\r
255 \r
256                         /*\r
257                         for (int i = 0; i < 10000; i++)\r
258                         {\r
259                                 masterPool.Children.Add(UIElementPool[i]);\r
260                         }\r
261                          * */\r
262 \r
263 \r
264                         AsyncInitBool = true;\r
265                 }\r
266 \r
267                 #endregion\r
268 \r
269                 #region static initializer\r
270                 /*\r
271                 static System.Windows.Shapes.Line api_line;\r
272                 static System.Windows.Shapes.Path api_curve;\r
273                 static System.Windows.Shapes.Rectangle api_rect;\r
274                 static System.Windows.Shapes.Ellipse api_ellipse;\r
275                 static System.Windows.Shapes.Polygon api_polygon;\r
276                 static System.Windows.Shapes.Polyline api_polyline;\r
277                 static System.Windows.Media.Color api_color;\r
278                 static System.Windows.Media.SolidColorBrush api_fill;\r
279                 static System.Windows.Media.SolidColorBrush api_stroke;\r
280                 static System.Windows.Media.TranslateTransform api_translation;\r
281                 static Canvas()\r
282                 {\r
283                         api_line = new System.Windows.Shapes.Line();\r
284                         api_curve    = new System.Windows.Shapes.Path();\r
285                         api_rect     = new System.Windows.Shapes.Rectangle();\r
286                         api_ellipse  = new System.Windows.Shapes.Ellipse();\r
287                         api_polygon  = new System.Windows.Shapes.Polygon();\r
288                         api_polyline = new System.Windows.Shapes.Polyline();\r
289                         api_color       = new System.Windows.Media.Color();\r
290                         api_fill        = new System.Windows.Media.SolidColorBrush();\r
291                         api_stroke      = new System.Windows.Media.SolidColorBrush();\r
292                         api_translation = new System.Windows.Media.TranslateTransform();\r
293                 }\r
294                 */\r
295                 #endregion\r
296 \r
297 \r
298                 public override void clear(Color col)\r
299                 {\r
300                         back_panel.fill = col;\r
301                         stackN = 0;\r
302                         rect(back_panel);\r
303                 }\r
304 \r
305                 int nextIntervalFrame = 1, chacked = 0;\r
306                 public void flip(int n)\r
307                 {\r
308                         flip();\r
309                 }\r
310                 public void flip()\r
311                 {\r
312                         lock (this)\r
313                         {\r
314                                 nextIntervalFrame = 1;\r
315                                 chacked = 1;\r
316                         }\r
317                         //pointStackN = 0;\r
318                         //brushStackN = 0;\r
319                         lineStackN = 0;\r
320                         rectStackN = 0;\r
321                         polygonStackN = 0;\r
322                         ellipseStackN = 0;\r
323                         lettersStackN = 0;\r
324                         imageStackN = 0;\r
325 \r
326                         pointPoolN = 0;\r
327                         brushPoolN = 0;\r
328                         linePoolN = 0;\r
329                         rectPoolN = 0;\r
330                         ellipsePoolN = 0;\r
331                         polygonPoolN = 0;\r
332                         lettersPoolN = 0;\r
333                         imagePoolN = 0;\r
334 \r
335                         Internal.Main.canvas_flag.WaitOne();\r
336                 }\r
337                 #region version modifyNative\r
338                 public void executeFlip()\r
339                 {\r
340                         Line lineS;\r
341                         Rectangle rectS;\r
342                         Ellipse ellipseS;\r
343                         Polygon polygonS;\r
344                         Letters lettersS;\r
345                         Image imageS;\r
346                         System.Windows.Shapes.Line lineP;\r
347                         System.Windows.Shapes.Rectangle rectP;\r
348                         System.Windows.Shapes.Ellipse ellipseP;\r
349                         System.Windows.Shapes.Polygon polygonP;\r
350                         System.Windows.Controls.TextBlock lettersP;\r
351                         System.Windows.Controls.Image imageP;\r
352 \r
353                         lock (this)\r
354                         {\r
355                                 nextIntervalFrame--;\r
356                         }\r
357 \r
358                         var en = masterPool.Children.GetEnumerator();\r
359                         bool full = en.MoveNext();\r
360                         if (nextIntervalFrame <= 0)\r
361                         {\r
362                                 if (chacked > 0)\r
363                                 {\r
364                                         //masterPool.Children.Clear();\r
365                                         if (stackN > 0)\r
366                                         {\r
367                                                 for (int i = 0; i < stackN - 2; i++)\r
368                                                 {\r
369                                                         if (full == false)\r
370                                                         {\r
371                                                                 masterPool.Children.Add(stack[i].poolNative(this));\r
372                                                         }\r
373                                                         else\r
374                                                         {\r
375                                                                 if( null != (rectS = stack[i] as Rectangle) )\r
376                                                                 {\r
377                                                                         if (null != (rectP = en.Current as System.Windows.Shapes.Rectangle))\r
378                                                                         {\r
379                                                                                 rectS.modifyNative(rectP, this);\r
380                                                                         }\r
381                                                                 }\r
382                                                                 else if (null != (lineS = stack[i] as Line))\r
383                                                                 {\r
384                                                                         if (null != (lineP = en.Current as System.Windows.Shapes.Line))\r
385                                                                         {\r
386                                                                                 lineS.modifyNative(lineP, this);\r
387                                                                         }\r
388                                                                 }\r
389                                                                 else if (null != (ellipseS = stack[i] as Ellipse))\r
390                                                                 {\r
391                                                                         if (null != (ellipseP = en.Current as System.Windows.Shapes.Ellipse))\r
392                                                                         {\r
393                                                                                 ellipseS.modifyNative(ellipseP, this);\r
394                                                                         }\r
395                                                                 }\r
396                                                                 else if (null != (polygonS = stack[i] as Polygon))\r
397                                                                 {\r
398                                                                         if (null != (polygonP = en.Current as System.Windows.Shapes.Polygon))\r
399                                                                         {\r
400                                                                                 polygonS.modifyNative(polygonP, this);\r
401                                                                         }\r
402                                                                 }\r
403                                                                 else if (null != (lettersS = stack[i] as Letters))\r
404                                                                 {\r
405                                                                         if (null != (lettersP = en.Current as System.Windows.Controls.TextBlock))\r
406                                                                         {\r
407                                                                                 lettersS.modifyNative(lettersP, this);\r
408                                                                         }\r
409                                                                 }\r
410                                                                 else if (null != (imageS = stack[i] as Image))\r
411                                                                 {\r
412                                                                         if (null != (imageP = en.Current as System.Windows.Controls.Image))\r
413                                                                         {\r
414                                                                                 imageS.modifyNative(imageP, this);\r
415                                                                         }\r
416                                                                 }\r
417                                                                 full = en.MoveNext();\r
418                                                         }\r
419                                                 }\r
420                                                 stackN = 0;\r
421                                         }\r
422                                         lock (this)\r
423                                         {\r
424                                                 chacked = 0;\r
425                                         }\r
426                                         Psychlops.Internal.Main.canvas_flag.Set();\r
427                                 }\r
428                         }\r
429                         System.Threading.Thread.Sleep(0);\r
430                 }\r
431                 #endregion\r
432 \r
433                 /*\r
434                 #region version poolNative 2\r
435                 public void executeFlip()\r
436                 {\r
437 \r
438                         lock (this)\r
439                         {\r
440                                 nextIntervalFrame--;\r
441                         }\r
442                         UIElementPoolN = 0;\r
443                         if (nextIntervalFrame <= 0)\r
444                         {\r
445                                 if (chacked > 0)\r
446                                 {\r
447                                         //masterPool.Children.Clear();\r
448                                         if (stackN > 0)\r
449                                         {\r
450                                                 for (int i = 0; i < stackN - 2; i++)\r
451                                                 {\r
452                                                         UIElementPool[UIElementPoolN] = stack[i].poolNative(this);\r
453                                                         UIElementPool[UIElementPoolN].Visibility = Visibility.Visible;\r
454                                                         UIElementPoolN++;\r
455 \r
456                                                 }\r
457                                                 for (int i = stackN - 2; i < 10000; i++)\r
458                                                 {\r
459                                                         UIElementPool[UIElementPoolN] = rectPool[i];\r
460                                                         UIElementPool[UIElementPoolN].Visibility = Visibility.Collapsed;\r
461                                                         UIElementPoolN++;\r
462                                                 }\r
463                                                 stackN = 0;\r
464                                         }\r
465                                         lock (this)\r
466                                         {\r
467                                                 chacked = 0;\r
468                                         }\r
469                                         Psychlops.Internal.Main.canvas_flag.Set();\r
470                                 }\r
471                         }\r
472                         System.Threading.Thread.Sleep(0);\r
473                 }\r
474                 #endregion\r
475                 */\r
476 \r
477 \r
478                 #region Properties\r
479 \r
480                 public double width { get { return width_; } }\r
481                 public double height { get { return height_; } }\r
482                 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
483                 public double getWidth() { return width; }\r
484                 public double getHeight() { return height; }\r
485                 public override Point getCenter() { return center; }\r
486                 public double getHCenter() { return width / 2; }\r
487                 public double getVCenter() { return height / 2; }\r
488                 public double getRefreshRate() { return 60; }\r
489 \r
490                 #endregion\r
491 \r
492 \r
493         }\r
494 \r
495 \r
496 \r
497         #region primitive tokenizer\r
498 \r
499         partial struct Point\r
500         {\r
501                 public static implicit operator System.Windows.Point(Point d)\r
502                 {\r
503                         return new System.Windows.Point(d.x, d.y);\r
504                 }\r
505         }\r
506 \r
507         partial struct Color\r
508         {\r
509                 public static implicit operator System.Windows.Media.Color(Color d)\r
510                 {\r
511                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
512                 }\r
513                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
514                 {\r
515                         return new SolidColorBrush { Color = d };\r
516                 }\r
517         }\r
518 \r
519         partial struct Stroke\r
520         {\r
521                 public void apply(System.Windows.Shapes.Shape target)\r
522                 {\r
523                         target.Stroke = this;\r
524                         //target.StrokeDashArray\r
525                         target.StrokeThickness = thick;\r
526                 }\r
527                 public static implicit operator SolidColorBrush(Stroke d)\r
528                 {\r
529                         return new SolidColorBrush { Color = d.color };\r
530                 }\r
531         }\r
532 \r
533         partial class Line\r
534         {\r
535                 public Line dup()\r
536                 {\r
537                         return (Line)MemberwiseClone();\r
538                 }\r
539                 public Line clone()\r
540                 {\r
541                         return (Line)MemberwiseClone();\r
542                 }\r
543                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
544                 {\r
545                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
546                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
547                         else d.stroke.apply(tmp);\r
548                         return tmp;\r
549                 }\r
550                 public UIElement toNative() { return this; }\r
551 \r
552                 public void copyToStack(Templates.StackableDrawable d)\r
553                 {\r
554                         var tmp = d.lineStack[d.lineStackN];\r
555                         tmp.begin.x = begin.x;\r
556                         tmp.begin.y = begin.y;\r
557                         tmp.end.x = end.x;\r
558                         tmp.end.y = end.y;\r
559                         tmp.fill = fill;\r
560                         tmp.stroke = stroke;\r
561                         d.stack[d.stackN] = tmp;\r
562                         d.lineStackN++;\r
563                         d.stackN++;\r
564                 }\r
565                 public UIElement poolNative(Canvas d)\r
566                 {\r
567                         var tmp = d.linePool[d.linePoolN];\r
568                         tmp.X1 = begin.x;\r
569                         tmp.Y1 = begin.y;\r
570                         tmp.X2 = end.x;\r
571                         tmp.Y2 = end.y;\r
572                         if (stroke.thick == 0.0) tmp.Stroke = fill;\r
573                         else stroke.apply(tmp);\r
574                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
575                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
576                         tmp.Visibility = Visibility.Visible;\r
577                         d.linePoolN++;\r
578                         return tmp;\r
579                 }\r
580                 public void modifyNative(System.Windows.Shapes.Line tmp, Canvas d)\r
581                 {\r
582                         tmp.X1 = begin.x;\r
583                         tmp.Y1 = begin.y;\r
584                         tmp.X2 = end.x;\r
585                         tmp.Y2 = end.y;\r
586                         if (stroke.thick == 0.0) tmp.Stroke = fill;\r
587                         else stroke.apply(tmp);\r
588                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
589                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
590                         tmp.Visibility = Visibility.Visible;\r
591                 }\r
592         }\r
593 \r
594         partial class Rectangle\r
595         {\r
596                 public Rectangle dup()\r
597                 {\r
598                         return (Rectangle)MemberwiseClone();\r
599                 }\r
600                 public Rectangle clone()\r
601                 {\r
602                         return (Rectangle)MemberwiseClone();\r
603                 }\r
604                 public static implicit operator System.Windows.Rect(Rectangle d)\r
605                 {\r
606                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
607                 }\r
608                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
609                 {\r
610                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
611                         d.stroke.apply(tmp);\r
612                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
613                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
614                         return tmp;\r
615                 }\r
616 \r
617                 public UIElement toNative() { return this; }\r
618                 public void copyToStack(Templates.StackableDrawable d)\r
619                 {\r
620                         var tmp = d.rectStack[d.rectStackN];\r
621                         tmp.v1 = v1;\r
622                         tmp.v2 = v2;\r
623                         tmp.fill = fill;\r
624                         d.stack[d.stackN] = tmp;\r
625                         d.rectStackN++;\r
626                         d.stackN++;\r
627                 }\r
628                 public UIElement poolNative(Canvas d)\r
629                 {\r
630                         var tmp = d.rectPool[d.rectPoolN];\r
631                         tmp.Width = width;\r
632                         tmp.Height = height;\r
633                         tmp.Fill = fill;\r
634                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
635                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
636                         tmp.Visibility = Visibility.Visible;\r
637                         d.rectPoolN++;\r
638                         return tmp;\r
639                 }\r
640                 public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d)\r
641                 {\r
642                         tmp.Width = width;\r
643                         tmp.Height = height;\r
644                         tmp.Fill = fill;\r
645                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
646                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
647                         tmp.Visibility = Visibility.Visible;\r
648                 }\r
649         }\r
650 \r
651         partial class Ellipse\r
652         {\r
653                 public Ellipse dup()\r
654                 {\r
655                         return (Ellipse)MemberwiseClone();\r
656                 }\r
657                 public Ellipse clone()\r
658                 {\r
659                         return (Ellipse)MemberwiseClone();\r
660                 }\r
661                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
662                 {\r
663                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
664                         d.stroke.apply(tmp);\r
665                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
666                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
667                         return tmp;\r
668                 }\r
669 \r
670                 public UIElement toNative() { return this; }\r
671 \r
672                 public void copyToStack(Templates.StackableDrawable d)\r
673                 {\r
674                         var tmp = d.ellipseStack[d.ellipseStackN];\r
675                         tmp.datum = datum;\r
676                         tmp.xdiameter = xdiameter;\r
677                         tmp.ydiameter = ydiameter;\r
678                         tmp.fill = fill;\r
679                         d.stack[d.stackN] = tmp;\r
680                         d.ellipseStackN++;\r
681                         d.stackN++;\r
682                 }\r
683                 public UIElement poolNative(Canvas d)\r
684                 {\r
685                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
686                         tmp.Width = width;\r
687                         tmp.Height = height;\r
688                         tmp.Fill = fill;\r
689                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
690                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
691                         tmp.Visibility = Visibility.Visible;\r
692                         d.ellipsePoolN++;\r
693                         return tmp;\r
694                 }\r
695                 public void modifyNative(System.Windows.Shapes.Ellipse tmp, Canvas d)\r
696                 {\r
697                         tmp.Width = width;\r
698                         tmp.Height = height;\r
699                         tmp.Fill = fill;\r
700                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
701                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
702                         tmp.Visibility = Visibility.Visible;\r
703                 }\r
704         }\r
705 \r
706         partial class Polygon\r
707         {\r
708                 public Polygon dup()\r
709                 {\r
710                         return (Polygon)MemberwiseClone();\r
711                 }\r
712                 public Polygon clone()\r
713                 {\r
714                         return (Polygon)MemberwiseClone();\r
715                 }\r
716                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
717                 {\r
718                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
719                         d.stroke.apply(tmp);\r
720                         foreach (Point p in d.vertices)\r
721                         {\r
722                                 tmp.Points.Add(p);\r
723                         }\r
724                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
725                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
726                         return tmp;\r
727                 }\r
728                 public UIElement toNative() { return this; }\r
729 \r
730                 public void copyToStack(Templates.StackableDrawable d)\r
731                 {\r
732                         var tmp = d.polygonStack[d.polygonStackN];\r
733                         tmp.datum = datum;\r
734                         tmp.vertices.Clear();\r
735                         foreach (var v in vertices)\r
736                         {\r
737                                 tmp.vertices.Add(v);\r
738                         }\r
739                         tmp.fill = fill;\r
740                         d.stack[d.stackN] = tmp;\r
741                         d.polygonStackN++;\r
742                         d.stackN++;\r
743                 }\r
744                 public UIElement poolNative(Canvas d)\r
745                 {\r
746                         var tmp = d.polygonPool[d.polygonPoolN];\r
747                         tmp.Fill = fill;\r
748                         tmp.Points.Clear();\r
749                         foreach (var v in vertices)\r
750                         {\r
751                                 tmp.Points.Add(v);\r
752                         }\r
753                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
754                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
755                         tmp.Visibility = Visibility.Visible;\r
756                         d.polygonPoolN++;\r
757                         return tmp;\r
758                 }\r
759                 public void modifyNative(System.Windows.Shapes.Polygon tmp, Canvas d)\r
760                 {\r
761                         tmp.Fill = fill;\r
762                         tmp.Points.Clear();\r
763                         foreach (var v in vertices)\r
764                         {\r
765                                 tmp.Points.Add(v);\r
766                         }\r
767                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
768                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
769                         tmp.Visibility = Visibility.Visible;\r
770                 }\r
771 \r
772         }\r
773         \r
774         partial class Letters\r
775         {\r
776                 #region static initializer\r
777                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
778                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
779                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
780                 static Letters()\r
781                 {\r
782                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
783                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
784                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
785                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
786                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
787                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
788                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
789                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
790                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
791                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
792                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
793                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
794                 }\r
795                 #endregion\r
796                 public Letters dup()\r
797                 {\r
798                         return (Letters)MemberwiseClone();\r
799                 }\r
800                 public Letters clone()\r
801                 {\r
802                         return (Letters)MemberwiseClone();\r
803                 }\r
804                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
805                 {\r
806                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
807                         var tmp = new System.Windows.Controls.TextBlock {\r
808                                 Text = d.str, Width = 500, Height = 500,\r
809                                 FontSize = d.font.size,\r
810                                 //tmp.FontFamily = ,\r
811                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
812                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
813                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
814                                 Foreground = d.fill\r
815                         };\r
816                         double left = 0;\r
817                         switch (d.align)\r
818                         {\r
819                                 case Letters.HorizontalAlign.left: break;\r
820                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
821                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
822                         }\r
823                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
824                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
825                         return tmp;\r
826                 }\r
827                 public UIElement toNative() { return this; }\r
828 \r
829                 public void copyToStack(Templates.StackableDrawable d)\r
830                 {\r
831                         var tmp = d.lettersStack[d.lettersStackN];\r
832                         tmp.str = str;\r
833                         tmp.datum = datum;\r
834                         tmp.fill = fill;\r
835                         d.stack[d.stackN] = tmp;\r
836                         d.lettersStackN++;\r
837                         d.stackN++;\r
838                 }\r
839                 public UIElement poolNative(Canvas d)\r
840                 {\r
841                         var tmp = d.lettersPool[d.lettersPoolN];\r
842                         tmp.Text = str;\r
843                         tmp.Width = 500;\r
844                         tmp.Height = 500;\r
845                         tmp.FontSize = font.size;\r
846                         //tmp.FontFamily = ,\r
847                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
848                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
849                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
850                         tmp.Foreground = fill;\r
851                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
852                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
853                         tmp.Visibility = Visibility.Visible;\r
854                         d.lettersPoolN++;\r
855                         return tmp;\r
856                 }\r
857                 public void modifyNative(System.Windows.Controls.TextBlock tmp, Canvas d)\r
858                 {\r
859                         tmp.Text = str;\r
860                         tmp.Width = 500;\r
861                         tmp.Height = 500;\r
862                         tmp.FontSize = font.size;\r
863                         //tmp.FontFamily = ,\r
864                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
865                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
866                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
867                         tmp.Foreground = fill;\r
868                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
869                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
870                         tmp.Visibility = Visibility.Visible;\r
871                 }\r
872         }\r
873 \r
874         partial class Image\r
875         {\r
876                 internal void initialize__(int wid, int hei)\r
877                 {\r
878                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
879                         //buffer = new WriteableBitmap(wid, hei);\r
880                 }\r
881                 internal void create__(int wid, int hei)\r
882                 {\r
883                         buffer = new WriteableBitmap(wid, hei);\r
884                         AsyncBool = true;\r
885                 }\r
886                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
887                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
888                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
889                 {\r
890                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
891                         //buffer.ForEach(func);\r
892                 }\r
893                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
894                 {\r
895                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
896                         //buffer.ForEach(func);\r
897                 }\r
898                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
899                 {\r
900                         buffer.ForEach(func);\r
901                 }\r
902                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
903                 {\r
904                         buffer.ForEach(func);\r
905                 }\r
906 \r
907                 public Image clone()\r
908                 {\r
909                         return (Image)MemberwiseClone();\r
910                 }\r
911                 public static implicit operator System.Windows.Controls.Image(Image d)\r
912                 {\r
913                         var tmp = new System.Windows.Controls.Image();\r
914                         tmp.Source = d.buffer;\r
915                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
916                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
917                         return tmp;\r
918                 }\r
919                 public UIElement toNative() { return this; }\r
920 \r
921                 public void copyToStack(Templates.StackableDrawable d)\r
922                 {\r
923                         var tmp = d.imageStack[d.imageStackN];\r
924                         tmp.datum = datum;\r
925                         tmp.buffer = buffer;\r
926                         tmp.self_rect = self_rect;\r
927                         d.stack[d.stackN] = tmp;\r
928                         d.imageStackN++;\r
929                         d.stackN++;\r
930                 }\r
931                 public UIElement poolNative(Canvas d)\r
932                 {\r
933                         var tmp = d.imagePool[d.imagePoolN];\r
934                         tmp.Source = buffer;\r
935                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
936                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
937                         tmp.Visibility = Visibility.Visible;\r
938                         d.imagePoolN++;\r
939                         return this;\r
940                 }\r
941                 public void modifyNative(System.Windows.Controls.Image tmp, Canvas d)\r
942                 {\r
943                         tmp.Source = buffer;\r
944                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
945                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
946                         tmp.Visibility = Visibility.Visible;\r
947                 }\r
948 \r
949         }\r
950 \r
951 \r
952         partial class Group\r
953         {\r
954                 internal void initialize__()\r
955                 {\r
956                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.SimpleProcedure(create__));\r
957                 }\r
958                 internal void create__()\r
959                 {\r
960                         cnvs = new System.Windows.Controls.Canvas();\r
961                         AsyncBool = true;\r
962                 }\r
963                 delegate void AppendFunc1(System.Func<Figure> func);\r
964                 public void append__(Shape fig)\r
965                 {\r
966                 }\r
967 \r
968                 public Group clone()\r
969                 {\r
970                         return (Group)MemberwiseClone();\r
971                 }\r
972 \r
973                 public static implicit operator System.Windows.Controls.Canvas(Group d)\r
974                 {\r
975                         var tmp = new System.Windows.Controls.Canvas();\r
976 /*                      foreach (var item in list)\r
977                         {\r
978                                 tmp.Children.Add(d.);\r
979                         }\r
980                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
981                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
982 */                      return tmp;\r
983                 }\r
984                 public UIElement toNative() { return this; }\r
985 \r
986                 public void copyToStack(Templates.StackableDrawable d)\r
987                 {\r
988 /*                      var tmp = d.imageStack[d.imageStackN];\r
989                         tmp.datum = datum;\r
990                         tmp.buffer = buffer;\r
991                         tmp.self_rect = self_rect;\r
992                         d.stack[d.stackN] = tmp;\r
993                         d.imageStackN++;\r
994                         d.stackN++;\r
995 */              }\r
996                 public UIElement poolNative(Canvas d)\r
997                 {\r
998 /*                      var tmp = d.imagePool[d.imagePoolN];\r
999                         tmp.Source = buffer;\r
1000                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1001                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1002                         tmp.Visibility = Visibility.Visible;\r
1003                         d.imagePoolN++;\r
1004 */                      return this;\r
1005                 }\r
1006                 public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d)\r
1007                 {\r
1008 /*                      tmp.Source = buffer;\r
1009                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1010                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1011                         tmp.Visibility = Visibility.Visible;\r
1012 */              }\r
1013 \r
1014         }\r
1015         \r
1016         #endregion\r
1017 \r
1018 \r
1019 }