OSDN Git Service

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