OSDN Git Service

4d65ec4c6725bcba89a6bec154fbe86f46a278f2
[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 using System.Collections.Generic;\r
13  \r
14 \r
15 \r
16 namespace Psychlops\r
17 {\r
18 \r
19         internal static partial class CONST\r
20         {\r
21                 internal static readonly Int32 MAX_OBJ_N = 1500;\r
22                 internal static readonly Int32 MOBJ_N = 1000;\r
23                 internal static readonly Int32 COBJ_N = 300;\r
24                 internal static readonly Int32 HOBJ_N = 100;\r
25         }\r
26 \r
27         namespace Templates\r
28         {\r
29 \r
30                 public class StackableDrawable : Drawable\r
31                 {\r
32 //                      protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
33                         internal Internal.PrimitiveFigure[] stack;\r
34                         internal int stackN = 0;\r
35                         internal Line[] lineStack;\r
36                         internal int lineStackN = 0;\r
37                         internal Rectangle[] rectStack;\r
38                         internal int rectStackN = 0;\r
39                         internal Ellipse[] ellipseStack;\r
40                         internal int ellipseStackN = 0;\r
41                         internal Polygon[] polygonStack;\r
42                         internal int polygonStackN = 0;\r
43                         internal Letters[] lettersStack;\r
44                         internal int lettersStackN = 0;\r
45                         internal Image[] imageStack;\r
46                         internal int imageStackN = 0;\r
47                         internal Group[] groupStack;\r
48                         internal int groupStackN = 0;\r
49 \r
50                         public StackableDrawable()\r
51                         {\r
52                                 stack = new Internal.PrimitiveFigure[CONST.MAX_OBJ_N];\r
53                                 lineStack = new Line[CONST.MOBJ_N];\r
54                                 rectStack = new Rectangle[CONST.MOBJ_N];\r
55                                 ellipseStack = new Ellipse[CONST.MOBJ_N];\r
56                                 polygonStack = new Polygon[CONST.COBJ_N];\r
57                                 lettersStack = new Letters[CONST.COBJ_N];\r
58                                 imageStack = new Image[CONST.HOBJ_N];\r
59                                 groupStack = new Group[CONST.HOBJ_N];\r
60                                 for (int i = 0; i < CONST.MOBJ_N; i++)\r
61                                 {\r
62                                         lineStack[i] = new Line(0,0,0,0);\r
63                                         rectStack[i] = new Rectangle();\r
64                                         ellipseStack[i] = new Ellipse();\r
65                                 }\r
66                                 for (int i = 0; i < CONST.COBJ_N; i++)\r
67                                 {\r
68                                         polygonStack[i] = new Polygon();\r
69                                         lettersStack[i] = new Letters();\r
70                                 }\r
71                                 for (int i = 0; i < CONST.HOBJ_N; i++)\r
72                                 {\r
73                                         imageStack[i] = new Image();\r
74 //                                      groupStack[i] = new Group();\r
75                                 }\r
76                         }\r
77 \r
78                         public void clear() { clear(Color.black); }\r
79                         public virtual void clear(Color col) { } //rect(back_panel, col); }\r
80                         public virtual void pix(int x, int y, Color col) { }\r
81                         public virtual void line(Line drawee) { drawee.copyToStack(this); }\r
82                         public virtual void rect(Rectangle drawee) { drawee.copyToStack(this); }\r
83                         public virtual void ellipse(Ellipse drawee) { drawee.copyToStack(this); }\r
84                         public virtual void oval(Ellipse drawee) { drawee.copyToStack(this); }\r
85                         public virtual void polygon(Polygon drawee) { drawee.copyToStack(this); }\r
86                         public virtual void letters(Letters drawee) { drawee.copyToStack(this); }\r
87                         public virtual void image(Image drawee) { drawee.copyToStack(this); }\r
88                         public virtual void group(Group drawee) { drawee.copyToStack(this); }\r
89 \r
90                         public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
91                         public virtual void msg(string dstr, double x, double y, Color col)\r
92                         {\r
93                                 var let = new Letters(dstr);\r
94                                 let.locate(x, y);\r
95                                 let.fill = col;\r
96                                 this.letters(let);\r
97                         }\r
98                         public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
99                         public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
100 \r
101                         public virtual Point getCenter() { return new Point(0, 0, 0); }\r
102                 }\r
103 \r
104         }\r
105 \r
106         public class Canvas : Templates.StackableDrawable\r
107         {\r
108                 internal System.Windows.Controls.Canvas masterPool, prevPool;\r
109                 internal System.Windows.Point[] pointPool;\r
110                 internal int pointPoolN;\r
111                 internal SolidColorBrush[] brushPool;\r
112                 internal int brushPoolN;\r
113 \r
114                 internal System.Windows.Controls.Canvas[] UIElementPool;\r
115                 internal int UIElementPoolN;\r
116                 internal int lastVisibleN;\r
117 \r
118                 internal System.Windows.Shapes.Line[] linePool;\r
119                 internal int linePoolN;\r
120                 internal System.Windows.Shapes.Rectangle[] dummyRectPool;\r
121                 internal System.Windows.Shapes.Rectangle[] rectPool;\r
122                 internal int rectPoolN;\r
123                 internal System.Windows.Shapes.Ellipse[] ellipsePool;\r
124                 internal int ellipsePoolN;\r
125                 internal System.Windows.Shapes.Polygon[] polygonPool;\r
126                 internal int polygonPoolN;\r
127                 internal System.Windows.Controls.TextBlock[] lettersPool;\r
128                 internal int lettersPoolN;\r
129                 internal System.Windows.Controls.Image[] imagePool;\r
130                 internal int imagePoolN;\r
131                 internal Dictionary<int, bool> imagePoolT;\r
132                 internal System.Windows.Controls.Canvas[] groupPool;\r
133                 internal int groupPoolN;\r
134 \r
135                 #region initializer\r
136 \r
137                 internal delegate void TwoIntProcedure(int x, int y);\r
138                 internal delegate void SimpleProcedure();\r
139                 SimpleProcedure flipexec;\r
140 \r
141                 public static System.Windows.Controls.UserControl default_panel;\r
142                 public static System.Windows.Controls.Canvas default_api_canvas;\r
143                 public static WriteableBitmap default_buffer;\r
144                 internal System.Windows.Controls.Canvas api_canvas;\r
145                 internal System.Windows.Controls.UserControl panel;\r
146                 Rectangle back_panel;\r
147                 double width_, height_;\r
148                 Clock before;\r
149 \r
150                 public Canvas(int wid, int hei)\r
151                 {\r
152                         panel = default_panel;\r
153                         api_canvas = default_api_canvas;\r
154                         initialize(wid, hei);\r
155                 }\r
156                 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)\r
157                 {\r
158                         panel = system;\r
159                         api_canvas = apicnvs;\r
160                         initialize(wid, hei);\r
161                 }\r
162 \r
163                 protected bool AsyncInitBool;\r
164                 protected void initialize(int wid, int hei)\r
165                 {\r
166                         before = new Clock();\r
167                         before.update();\r
168                         var after = new Clock();\r
169                         AsyncInitBool = false;\r
170                         width_ = wid;\r
171                         height_ = hei;\r
172                         api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
173                         while (!AsyncInitBool)\r
174                         {\r
175                                 after.update();\r
176                                 if ((after - before).at_msec() > 1000) break;\r
177                         }\r
178                         Mouse._prime = api_canvas;\r
179                         Main.drawable = this;\r
180                         Main.canvas = this;\r
181 \r
182                         back_panel = new Rectangle(wid, hei);\r
183 \r
184                         flipexec = new SimpleProcedure(executeFlip);\r
185                 }\r
186                 protected void initialize__(int wid, int hei)\r
187                 {\r
188                         api_canvas.Width = wid;\r
189                         api_canvas.Height = hei+20;\r
190                         api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
191                         api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
192                         api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
193                         api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
194                         panel.KeyDown += Keyboard.Canvas_KeyDown;\r
195                         panel.KeyUp += Keyboard.Canvas_KeyUp;\r
196 \r
197                         HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
198                         //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
199                         htmlHost.SetStyleAttribute("width", (wid).ToString()+"px");\r
200                         htmlHost.SetStyleAttribute("height", (hei).ToString() + "px");\r
201                         htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
202 \r
203                         pointPool = new System.Windows.Point[CONST.MOBJ_N];\r
204                         brushPool = new SolidColorBrush[CONST.MOBJ_N];\r
205                         linePool = new System.Windows.Shapes.Line[CONST.MOBJ_N];\r
206                         rectPool = new System.Windows.Shapes.Rectangle[CONST.MOBJ_N];\r
207                         ellipsePool = new System.Windows.Shapes.Ellipse[CONST.MOBJ_N];\r
208                         for (int i = 0; i < CONST.MOBJ_N; i++)\r
209                         {\r
210                                 pointPool[i] = new System.Windows.Point();\r
211                                 brushPool[i] = new SolidColorBrush();\r
212                                 linePool[i] = new System.Windows.Shapes.Line();\r
213                                 rectPool[i] = new System.Windows.Shapes.Rectangle();\r
214                                 ellipsePool[i] = new System.Windows.Shapes.Ellipse();\r
215                         }\r
216                         polygonPool = new System.Windows.Shapes.Polygon[CONST.COBJ_N];\r
217                         lettersPool = new System.Windows.Controls.TextBlock[CONST.COBJ_N];\r
218                         for (int i = 0; i < CONST.COBJ_N; i++)\r
219                         {\r
220                                 polygonPool[i] = new System.Windows.Shapes.Polygon();\r
221                                 lettersPool[i] = new System.Windows.Controls.TextBlock();\r
222                         }\r
223                         imagePool = new System.Windows.Controls.Image[CONST.HOBJ_N];\r
224                         imagePoolT = new Dictionary<int, bool>(CONST.HOBJ_N);\r
225                         groupPool = new System.Windows.Controls.Canvas[CONST.HOBJ_N];\r
226                         for (int i = 0; i < CONST.HOBJ_N; i++)\r
227                         {\r
228                                 imagePool[i] = new System.Windows.Controls.Image();\r
229                                 imagePoolT.Add(imagePool[i].GetHashCode(), false);\r
230                                 groupPool[i] = new System.Windows.Controls.Canvas();\r
231                         }\r
232 \r
233                         masterPool = new System.Windows.Controls.Canvas();\r
234                         prevPool = new System.Windows.Controls.Canvas();\r
235                         api_canvas.Children.Add(masterPool);\r
236 \r
237                         //api_canvas.Children.Remove(Internal.Main.widgetStack);\r
238                         Psychlops.Internal.Main.widgetStack = new StackPanel();\r
239                         Psychlops.Internal.Main.widgetStack.Orientation = Orientation.Vertical;\r
240                         Psychlops.Internal.Main.widgetStack.Height = hei;\r
241                         api_canvas.Children.Add(Psychlops.Internal.Main.widgetStack);\r
242                         Internal.Main.statusBar.Visibility = Visibility.Collapsed;\r
243 \r
244 \r
245                         UIElementPool = new System.Windows.Controls.Canvas[CONST.MAX_OBJ_N];\r
246                         dummyRectPool = new System.Windows.Shapes.Rectangle[CONST.MAX_OBJ_N];\r
247                         for (int i = 0; i < CONST.MAX_OBJ_N; i++)\r
248                         {\r
249                                 UIElementPool[i] = new System.Windows.Controls.Canvas();\r
250                                 masterPool.Children.Add(UIElementPool[i]);\r
251                                 dummyRectPool[i] = new System.Windows.Shapes.Rectangle();\r
252                                 UIElementPool[i].Children.Add(dummyRectPool[i]);\r
253                                 dummyRectPool[i].Visibility = Visibility.Collapsed;\r
254                         }\r
255 \r
256                         AsyncInitBool = true;\r
257                 }\r
258 \r
259                 internal int findEmptyInPool(Dictionary<int, bool> pool)\r
260                 {\r
261                         /*\r
262                         foreach( KeyValuePair<int, bool> elem in pool)\r
263                         {\r
264                                 if(elem) \r
265                         }*/\r
266                         return 0;\r
267                 }\r
268 \r
269                 #endregion\r
270 \r
271                 #region static initializer\r
272                 /*\r
273                 static Canvas()\r
274                 {\r
275                 }\r
276                 */\r
277                 #endregion\r
278 \r
279 \r
280                 public override void clear(Color col)\r
281                 {\r
282                         back_panel.fill = col;\r
283                         stackN = 0;\r
284                         rect(back_panel);\r
285                 }\r
286 \r
287                 int nextIntervalFrame = 1, chacked = 0;\r
288                 public void flip(int n)\r
289                 {\r
290                         lock (this)\r
291                         {\r
292                                 nextIntervalFrame = n;\r
293                                 chacked = 1;\r
294                         }\r
295                         //pointStackN = 0;\r
296                         lineStackN = 0;\r
297                         rectStackN = 0;\r
298                         polygonStackN = 0;\r
299                         ellipseStackN = 0;\r
300                         lettersStackN = 0;\r
301                         imageStackN = 0;\r
302                         groupStackN = 0;\r
303 \r
304                         UIElementPoolN = 0;\r
305                         brushPoolN = 0;\r
306                         /*\r
307                         pointPoolN = 0;\r
308                         brushPoolN = 0;\r
309                         linePoolN = 0;\r
310                         rectPoolN = 0;\r
311                         ellipsePoolN = 0;\r
312                         polygonPoolN = 0;\r
313                         lettersPoolN = 0;\r
314                         imagePoolN = 0;\r
315                         groupPoolN = 0;\r
316                          * */\r
317 \r
318                         //executeFlip();\r
319                         Internal.Main.canvas_flag.WaitOne();\r
320                 }\r
321                 public void flip()\r
322                 {\r
323                         flip(1);\r
324                 }\r
325 \r
326 \r
327                 #region version modifyNative2\r
328                 public void executeFlip()\r
329                 {\r
330 //                      Clock after = new Clock();\r
331 //                      after.update();\r
332 //                      AppState.statusBar = ((after - before).at_msec().ToString()) + " msec";\r
333 \r
334                         Line lineS;\r
335                         Rectangle rectS;\r
336                         Ellipse ellipseS;\r
337                         Polygon polygonS;\r
338                         Letters lettersS;\r
339                         Image imageS;\r
340                         Group groupS;\r
341                         System.Windows.Shapes.Line lineP;\r
342                         System.Windows.Shapes.Rectangle rectP;\r
343                         System.Windows.Shapes.Ellipse ellipseP;\r
344                         System.Windows.Shapes.Polygon polygonP;\r
345                         System.Windows.Controls.TextBlock lettersP;\r
346                         System.Windows.Controls.Image imageP;\r
347                         System.Windows.Controls.Canvas groupP;\r
348 \r
349                         lock (this)\r
350                         {\r
351                                 nextIntervalFrame--;\r
352                         }\r
353 \r
354                         var cnv = UIElementPool[0];\r
355                         if (nextIntervalFrame <= 0)\r
356                         {\r
357                                 if (chacked > 0)\r
358                                 {\r
359                                         if (stackN > 0)\r
360                                         {\r
361                                                 for (int i = 0; i < stackN; i++)\r
362                                                 {\r
363                                                         if( null != (rectS = stack[i] as Rectangle) )\r
364                                                         {\r
365                                                                 if (null != (rectP = cnv.Children[0] as System.Windows.Shapes.Rectangle))\r
366                                                                 {\r
367                                                                         rectS.modifyNative(rectP, this);\r
368                                                                 }\r
369                                                                 else\r
370                                                                 {\r
371                                                                         cnv.Children.Clear();\r
372                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
373                                                                 }\r
374                                                         }\r
375                                                         else if (null != (lineS = stack[i] as Line))\r
376                                                         {\r
377                                                                 if (null != (lineP = cnv.Children[0] as System.Windows.Shapes.Line))\r
378                                                                 {\r
379                                                                         lineS.modifyNative(lineP, this);\r
380                                                                 }\r
381                                                                 else\r
382                                                                 {\r
383                                                                         cnv.Children.Clear();\r
384                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
385                                                                 }\r
386                                                         }\r
387                                                         else if (null != (ellipseS = stack[i] as Ellipse))\r
388                                                         {\r
389                                                                 if (null != (ellipseP = cnv.Children[0] as System.Windows.Shapes.Ellipse))\r
390                                                                 {\r
391                                                                         ellipseS.modifyNative(ellipseP, this);\r
392                                                                 }\r
393                                                                 else\r
394                                                                 {\r
395                                                                         cnv.Children.Clear();\r
396                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
397                                                                 }\r
398                                                         }\r
399                                                         else if (null != (polygonS = stack[i] as Polygon))\r
400                                                         {\r
401                                                                 if (null != (polygonP = cnv.Children[0] as System.Windows.Shapes.Polygon))\r
402                                                                 {\r
403                                                                         polygonS.modifyNative(polygonP, this);\r
404                                                                 }\r
405                                                                 else\r
406                                                                 {\r
407                                                                         cnv.Children.Clear();\r
408                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
409                                                                 }\r
410                                                         }\r
411                                                         else if (null != (lettersS = stack[i] as Letters))\r
412                                                         {\r
413                                                                 if (null != (lettersP = cnv.Children[0] as System.Windows.Controls.TextBlock))\r
414                                                                 {\r
415                                                                         lettersS.modifyNative(lettersP, this);\r
416                                                                 }\r
417                                                                 else\r
418                                                                 {\r
419                                                                         cnv.Children.Clear();\r
420                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
421                                                                 }\r
422                                                         }\r
423                                                         else if (null != (imageS = stack[i] as Image))\r
424                                                         {\r
425                                                                 if (null != (imageP = cnv.Children[0] as System.Windows.Controls.Image))\r
426                                                                 {\r
427                                                                         imageS.modifyNative(imageP, this);\r
428                                                                 }\r
429                                                                 else\r
430                                                                 {\r
431                                                                         cnv.Children.Clear();\r
432                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
433                                                                 }\r
434                                                         }\r
435                                                         else if (null != (groupS = stack[i] as Group))\r
436                                                         {\r
437                                                                 if (null != (groupP = cnv.Children[0] as System.Windows.Controls.Canvas))\r
438                                                                 {\r
439                                                                         groupS.modifyNative(groupP, this);\r
440                                                                 }\r
441                                                                 else\r
442                                                                 {\r
443                                                                         cnv.Children.Clear();\r
444                                                                         cnv.Children.Add(stack[i].poolNative(this));\r
445                                                                 }\r
446                                                         }\r
447                                                         cnv.Visibility = Visibility.Visible;\r
448                                                         UIElementPoolN++;\r
449                                                         cnv = UIElementPool[UIElementPoolN];\r
450                                                 }\r
451                                                 for (int i = UIElementPoolN; i < lastVisibleN; i++)\r
452                                                 {\r
453                                                         cnv = UIElementPool[i];\r
454                                                         cnv.Visibility = Visibility.Collapsed;\r
455                                                 }\r
456                                                 lastVisibleN = UIElementPoolN;\r
457                                                 stackN = 0;\r
458                                         }\r
459                                         lock (this)\r
460                                         {\r
461                                                 chacked = 0;\r
462                                         }\r
463                                         Psychlops.Internal.Main.canvas_flag.Set();\r
464                                 }\r
465                         }\r
466                         System.Threading.Thread.Sleep(0);\r
467                 }\r
468                 #endregion\r
469 \r
470 \r
471 \r
472                 #region version modifyNative\r
473                 /*\r
474                 public void executeFlip()\r
475                 {\r
476                         Line lineS;\r
477                         Rectangle rectS;\r
478                         Ellipse ellipseS;\r
479                         Polygon polygonS;\r
480                         Letters lettersS;\r
481                         Image imageS;\r
482                         Group groupS;\r
483                         System.Windows.Shapes.Line lineP;\r
484                         System.Windows.Shapes.Rectangle rectP;\r
485                         System.Windows.Shapes.Ellipse ellipseP;\r
486                         System.Windows.Shapes.Polygon polygonP;\r
487                         System.Windows.Controls.TextBlock lettersP;\r
488                         System.Windows.Controls.Image imageP;\r
489                         System.Windows.Controls.Canvas groupP;\r
490 \r
491                         lock (this)\r
492                         {\r
493                                 nextIntervalFrame--;\r
494                         }\r
495 \r
496                         var en = masterPool.Children.GetEnumerator();\r
497                         bool full = en.MoveNext();\r
498                         if (nextIntervalFrame <= 0)\r
499                         {\r
500                                 if (chacked > 0)\r
501                                 {\r
502                                         //masterPool.Children.Clear();\r
503                                         if (stackN > 0)\r
504                                         {\r
505                                                 for (int i = 0; i < stackN - 2; i++)\r
506                                                 {\r
507                                                         if (full == false)\r
508                                                         {\r
509                                                                 masterPool.Children.Add(stack[i].poolNative(this));\r
510                                                         }\r
511                                                         else\r
512                                                         {\r
513                                                                 if( null != (rectS = stack[i] as Rectangle) )\r
514                                                                 {\r
515                                                                         if (null != (rectP = en.Current as System.Windows.Shapes.Rectangle))\r
516                                                                         {\r
517                                                                                 rectS.modifyNative(rectP, this);\r
518                                                                         }\r
519                                                                 }\r
520                                                                 else if (null != (lineS = stack[i] as Line))\r
521                                                                 {\r
522                                                                         if (null != (lineP = en.Current as System.Windows.Shapes.Line))\r
523                                                                         {\r
524                                                                                 lineS.modifyNative(lineP, this);\r
525                                                                         }\r
526                                                                 }\r
527                                                                 else if (null != (ellipseS = stack[i] as Ellipse))\r
528                                                                 {\r
529                                                                         if (null != (ellipseP = en.Current as System.Windows.Shapes.Ellipse))\r
530                                                                         {\r
531                                                                                 ellipseS.modifyNative(ellipseP, this);\r
532                                                                         }\r
533                                                                         else\r
534                                                                         {\r
535                                                                                 masterPool.Children.Add(stack[i].poolNative(this));\r
536                                                                         }\r
537                                                                 }\r
538                                                                 else if (null != (polygonS = stack[i] as Polygon))\r
539                                                                 {\r
540                                                                         if (null != (polygonP = en.Current as System.Windows.Shapes.Polygon))\r
541                                                                         {\r
542                                                                                 polygonS.modifyNative(polygonP, this);\r
543                                                                         }\r
544                                                                 }\r
545                                                                 else if (null != (lettersS = stack[i] as Letters))\r
546                                                                 {\r
547                                                                         if (null != (lettersP = en.Current as System.Windows.Controls.TextBlock))\r
548                                                                         {\r
549                                                                                 lettersS.modifyNative(lettersP, this);\r
550                                                                         }\r
551                                                                 }\r
552                                                                 else if (null != (imageS = stack[i] as Image))\r
553                                                                 {\r
554                                                                         if (null != (imageP = en.Current as System.Windows.Controls.Image))\r
555                                                                         {\r
556                                                                                 imageS.modifyNative(imageP, this);\r
557                                                                         }\r
558                                                                 }\r
559                                                                 else if (null != (groupS = stack[i] as Group))\r
560                                                                 {\r
561                                                                         if (null != (groupP = en.Current as System.Windows.Controls.Canvas))\r
562                                                                         {\r
563                                                                                 groupS.modifyNative(groupP, this);\r
564                                                                         }\r
565                                                                 }\r
566                                                                 full = en.MoveNext();\r
567                                                         }\r
568                                                 }\r
569                                                 stackN = 0;\r
570                                         }\r
571                                         lock (this)\r
572                                         {\r
573                                                 chacked = 0;\r
574                                         }\r
575                                         Psychlops.Internal.Main.canvas_flag.Set();\r
576                                 }\r
577                         }\r
578                         System.Threading.Thread.Sleep(0);\r
579                 }\r
580                  * */\r
581                 #endregion\r
582 \r
583                 #region version poolNative 2\r
584                 /*\r
585                 public void executeFlip()\r
586                 {\r
587 \r
588                         lock (this)\r
589                         {\r
590                                 nextIntervalFrame--;\r
591                         }\r
592                         UIElementPoolN = 0;\r
593                         if (nextIntervalFrame <= 0)\r
594                         {\r
595                                 if (chacked > 0)\r
596                                 {\r
597                                         //masterPool.Children.Clear();\r
598                                         if (stackN > 0)\r
599                                         {\r
600                                                 for (int i = 0; i < stackN - 2; i++)\r
601                                                 {\r
602                                                         UIElementPool[UIElementPoolN] = stack[i].poolNative(this);\r
603                                                         UIElementPool[UIElementPoolN].Visibility = Visibility.Visible;\r
604                                                         UIElementPoolN++;\r
605 \r
606                                                 }\r
607                                                 for (int i = stackN - 2; i < 10000; i++)\r
608                                                 {\r
609                                                         UIElementPool[UIElementPoolN] = rectPool[i];\r
610                                                         UIElementPool[UIElementPoolN].Visibility = Visibility.Collapsed;\r
611                                                         UIElementPoolN++;\r
612                                                 }\r
613                                                 stackN = 0;\r
614                                         }\r
615                                         lock (this)\r
616                                         {\r
617                                                 chacked = 0;\r
618                                         }\r
619                                         Psychlops.Internal.Main.canvas_flag.Set();\r
620                                 }\r
621                         }\r
622                         System.Threading.Thread.Sleep(0);\r
623                 }\r
624                 */\r
625                 #endregion\r
626 \r
627 \r
628                 #region Properties\r
629 \r
630                 public double width { get { return width_; } }\r
631                 public double height { get { return height_; } }\r
632                 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
633                 public double getWidth() { return width; }\r
634                 public double getHeight() { return height; }\r
635                 public override Point getCenter() { return center; }\r
636                 public double getHCenter() { return width / 2; }\r
637                 public double getVCenter() { return height / 2; }\r
638                 public double getRefreshRate() { return 60; }\r
639 \r
640                 #endregion\r
641 \r
642 \r
643                 #region compatibitily trick\r
644 \r
645                 public enum Mode { window, fullscreen }\r
646                 public static readonly Mode window = Mode.window, fullscreen = Mode.fullscreen;\r
647 \r
648                 public Canvas(int wid, int hei, Mode mod)\r
649                 {\r
650                         panel = default_panel;\r
651                         api_canvas = default_api_canvas;\r
652                         initialize(500, 500);\r
653                 }\r
654                 public Canvas(Mode mod)\r
655                         : base()\r
656                 {\r
657                         panel = default_panel;\r
658                         api_canvas = default_api_canvas;\r
659                         initialize(500, 500);\r
660                 }\r
661 \r
662                 public Canvas(int wid, int hei, Mode mod, Display.DisplayName name)\r
663                 {\r
664                         panel = default_panel;\r
665                         api_canvas = default_api_canvas;\r
666                         initialize(500, 500);\r
667                 }\r
668                 public Canvas(Mode mod, Display.DisplayName name)\r
669                         : base()\r
670                 {\r
671                         panel = default_panel;\r
672                         api_canvas = default_api_canvas;\r
673                         initialize(500, 500);\r
674                 }\r
675 \r
676 \r
677                 public void showFPS(bool sw = true) { }\r
678                 public void watchFPS(bool sw = true) { }\r
679 \r
680 \r
681                 public void clear(double lum)\r
682                 {\r
683                         clear(new Color(lum));\r
684                 }\r
685 \r
686                 #endregion\r
687 \r
688 \r
689         }\r
690 \r
691 \r
692 \r
693         #region primitive tokenizer\r
694 \r
695 \r
696         #region primitive\r
697 \r
698         partial struct Point\r
699         {\r
700                 public static implicit operator System.Windows.Point(Point d)\r
701                 {\r
702                         return new System.Windows.Point(d.x, d.y);\r
703                 }\r
704         }\r
705 \r
706         partial struct Color\r
707         {\r
708                 public static implicit operator System.Windows.Media.Color(Color d)\r
709                 {\r
710                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
711                 }\r
712                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
713                 {\r
714                         return new SolidColorBrush { Color = d };\r
715                 }\r
716                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
717                 {\r
718                         var tmp = d.brushPool[d.brushPoolN];\r
719                         tmp.Color = this;\r
720                         d.brushPoolN++;\r
721                         return tmp;\r
722                 }\r
723 \r
724         }\r
725 \r
726         partial struct Stroke\r
727         {\r
728                 public void apply(System.Windows.Shapes.Shape target)\r
729                 {\r
730                         target.Stroke = this;\r
731                         //target.StrokeDashArray\r
732                         target.StrokeThickness = thick;\r
733                 }\r
734                 public static implicit operator SolidColorBrush(Stroke d)\r
735                 {\r
736                         return new SolidColorBrush { Color = d.color };\r
737                 }\r
738                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
739                 {\r
740                         var tmp = d.brushPool[d.brushPoolN];\r
741                         tmp.Color = this.color;\r
742                         d.brushPoolN++;\r
743                         return tmp;\r
744                 }\r
745         }\r
746         \r
747         #endregion\r
748         \r
749         #region Line\r
750 \r
751         partial class Line\r
752         {\r
753                 public Line dup()\r
754                 {\r
755                         return (Line)MemberwiseClone();\r
756                 }\r
757                 public Line clone()\r
758                 {\r
759                         return (Line)MemberwiseClone();\r
760                 }\r
761                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
762                 {\r
763                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
764                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
765                         else d.stroke.apply(tmp);\r
766                         return tmp;\r
767                 }\r
768                 public UIElement toNative() { return this; }\r
769 \r
770                 public void copyToStack(Templates.StackableDrawable d)\r
771                 {\r
772                         var tmp = d.lineStack[d.lineStackN];\r
773                         tmp.begin.x = begin.x;\r
774                         tmp.begin.y = begin.y;\r
775                         tmp.end.x = end.x;\r
776                         tmp.end.y = end.y;\r
777                         tmp.fill = fill;\r
778                         tmp.stroke = stroke;\r
779                         d.stack[d.stackN] = tmp;\r
780                         d.lineStackN++;\r
781                         d.stackN++;\r
782                 }\r
783                 public UIElement poolNative(Canvas d)\r
784                 {\r
785                         var tmp = d.linePool[d.linePoolN];\r
786                         tmp.X1 = begin.x;\r
787                         tmp.Y1 = begin.y;\r
788                         tmp.X2 = end.x;\r
789                         tmp.Y2 = end.y;\r
790                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
791                         else stroke.apply(tmp);\r
792                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
793                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
794                         tmp.Visibility = Visibility.Visible;\r
795                         d.linePoolN++;\r
796                         return tmp;\r
797                 }\r
798                 public void modifyNative(System.Windows.Shapes.Line tmp, Canvas d)\r
799                 {\r
800                         tmp.X1 = begin.x;\r
801                         tmp.Y1 = begin.y;\r
802                         tmp.X2 = end.x;\r
803                         tmp.Y2 = end.y;\r
804                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
805                         else stroke.apply(tmp);\r
806                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
807                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
808                         tmp.Visibility = Visibility.Visible;\r
809                 }\r
810         }\r
811         \r
812         #endregion\r
813                 \r
814         #region Rectangle\r
815 \r
816         partial class Rectangle\r
817         {\r
818                 public Rectangle dup()\r
819                 {\r
820                         return (Rectangle)MemberwiseClone();\r
821                 }\r
822                 public Rectangle clone()\r
823                 {\r
824                         return (Rectangle)MemberwiseClone();\r
825                 }\r
826                 public static implicit operator System.Windows.Rect(Rectangle d)\r
827                 {\r
828                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
829                 }\r
830                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
831                 {\r
832                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
833                         d.stroke.apply(tmp);\r
834                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
835                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
836                         return tmp;\r
837                 }\r
838 \r
839                 public UIElement toNative() { return this; }\r
840                 public void copyToStack(Templates.StackableDrawable d)\r
841                 {\r
842                         var tmp = d.rectStack[d.rectStackN];\r
843                         tmp.v1 = v1;\r
844                         tmp.v2 = v2;\r
845                         tmp.fill = fill;\r
846                         d.stack[d.stackN] = tmp;\r
847                         d.rectStackN++;\r
848                         d.stackN++;\r
849                 }\r
850                 public UIElement poolNative(Canvas d)\r
851                 {\r
852                         var tmp = d.rectPool[d.rectPoolN];\r
853                         tmp.Width = width;\r
854                         tmp.Height = height;\r
855                         tmp.Fill = fill.getNativeFromStack(d);\r
856                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
857                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
858                         tmp.Visibility = Visibility.Visible;\r
859                         d.rectPoolN++;\r
860                         return tmp;\r
861                 }\r
862                 public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d)\r
863                 {\r
864                         tmp.Width = width;\r
865                         tmp.Height = height;\r
866                         tmp.Fill = fill.getNativeFromStack(d);\r
867                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
868                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
869                         tmp.Visibility = Visibility.Visible;\r
870                 }\r
871         }\r
872         \r
873         #endregion\r
874 \r
875         #region Ellipse\r
876 \r
877         partial class Ellipse\r
878         {\r
879                 public Ellipse dup()\r
880                 {\r
881                         return (Ellipse)MemberwiseClone();\r
882                 }\r
883                 public Ellipse clone()\r
884                 {\r
885                         return (Ellipse)MemberwiseClone();\r
886                 }\r
887                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
888                 {\r
889                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
890                         d.stroke.apply(tmp);\r
891                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
892                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
893                         return tmp;\r
894                 }\r
895 \r
896                 public UIElement toNative() { return this; }\r
897 \r
898                 public void copyToStack(Templates.StackableDrawable d)\r
899                 {\r
900                         var tmp = d.ellipseStack[d.ellipseStackN];\r
901                         tmp.datum = datum;\r
902                         tmp.xdiameter = xdiameter;\r
903                         tmp.ydiameter = ydiameter;\r
904                         tmp.fill = fill;\r
905                         d.stack[d.stackN] = tmp;\r
906                         d.ellipseStackN++;\r
907                         d.stackN++;\r
908                 }\r
909                 public UIElement poolNative(Canvas d)\r
910                 {\r
911                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
912                         tmp.Width = width;\r
913                         tmp.Height = height;\r
914                         tmp.Fill = fill.getNativeFromStack(d);\r
915                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
916                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
917                         tmp.Visibility = Visibility.Visible;\r
918                         d.ellipsePoolN++;\r
919                         return tmp;\r
920                 }\r
921                 public void modifyNative(System.Windows.Shapes.Ellipse tmp, Canvas d)\r
922                 {\r
923                         tmp.Width = width;\r
924                         tmp.Height = height;\r
925                         tmp.Fill = fill.getNativeFromStack(d);\r
926                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
927                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
928                         tmp.Visibility = Visibility.Visible;\r
929                 }\r
930         }\r
931 \r
932         #endregion\r
933 \r
934         #region Polygon\r
935 \r
936         partial class Polygon\r
937         {\r
938                 public Polygon dup()\r
939                 {\r
940                         return (Polygon)MemberwiseClone();\r
941                 }\r
942                 public Polygon clone()\r
943                 {\r
944                         return (Polygon)MemberwiseClone();\r
945                 }\r
946                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
947                 {\r
948                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
949                         d.stroke.apply(tmp);\r
950                         foreach (Point p in d.vertices)\r
951                         {\r
952                                 tmp.Points.Add(p);\r
953                         }\r
954                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
955                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
956                         return tmp;\r
957                 }\r
958                 public UIElement toNative() { return this; }\r
959 \r
960                 public void copyToStack(Templates.StackableDrawable d)\r
961                 {\r
962                         var tmp = d.polygonStack[d.polygonStackN];\r
963                         tmp.datum = datum;\r
964                         tmp.vertices.Clear();\r
965                         foreach (var v in vertices)\r
966                         {\r
967                                 tmp.vertices.Add(v);\r
968                         }\r
969                         tmp.fill = fill;\r
970                         d.stack[d.stackN] = tmp;\r
971                         d.polygonStackN++;\r
972                         d.stackN++;\r
973                 }\r
974                 public UIElement poolNative(Canvas d)\r
975                 {\r
976                         var tmp = d.polygonPool[d.polygonPoolN];\r
977                         tmp.Fill = fill.getNativeFromStack(d);\r
978                         tmp.Points.Clear();\r
979                         foreach (var v in vertices)\r
980                         {\r
981                                 tmp.Points.Add(v);\r
982                         }\r
983                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
984                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
985                         tmp.Visibility = Visibility.Visible;\r
986                         d.polygonPoolN++;\r
987                         return tmp;\r
988                 }\r
989                 public void modifyNative(System.Windows.Shapes.Polygon tmp, Canvas d)\r
990                 {\r
991                         tmp.Fill = fill.getNativeFromStack(d);\r
992                         tmp.Points.Clear();\r
993                         foreach (var v in vertices)\r
994                         {\r
995                                 tmp.Points.Add(v);\r
996                         }\r
997                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
998                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
999                         tmp.Visibility = Visibility.Visible;\r
1000                 }\r
1001 \r
1002         }\r
1003                 \r
1004         #endregion\r
1005 \r
1006         #region Letters\r
1007 \r
1008         partial class Letters\r
1009         {\r
1010                 #region static initializer\r
1011                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
1012                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
1013                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
1014                 static Letters()\r
1015                 {\r
1016                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
1017                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
1018                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
1019                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
1020                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
1021                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
1022                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
1023                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
1024                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
1025                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
1026                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
1027                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
1028                 }\r
1029                 #endregion\r
1030                 public Letters dup()\r
1031                 {\r
1032                         return (Letters)MemberwiseClone();\r
1033                 }\r
1034                 public Letters clone()\r
1035                 {\r
1036                         return (Letters)MemberwiseClone();\r
1037                 }\r
1038                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
1039                 {\r
1040                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
1041                         var tmp = new System.Windows.Controls.TextBlock {\r
1042                                 Text = d.str, Width = 500, Height = 500,\r
1043                                 FontSize = d.font.size,\r
1044                                 //tmp.FontFamily = ,\r
1045                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
1046                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
1047                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
1048                                 Foreground = d.fill\r
1049                         };\r
1050                         double left = 0;\r
1051                         switch (d.align)\r
1052                         {\r
1053                                 case Letters.HorizontalAlign.left: break;\r
1054                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
1055                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
1056                         }\r
1057                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
1058                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
1059                         return tmp;\r
1060                 }\r
1061                 public UIElement toNative() { return this; }\r
1062 \r
1063                 public void copyToStack(Templates.StackableDrawable d)\r
1064                 {\r
1065                         var tmp = d.lettersStack[d.lettersStackN];\r
1066                         tmp.str = str;\r
1067                         tmp.datum = datum;\r
1068                         tmp.fill = fill;\r
1069                         d.stack[d.stackN] = tmp;\r
1070                         d.lettersStackN++;\r
1071                         d.stackN++;\r
1072                 }\r
1073                 public UIElement poolNative(Canvas d)\r
1074                 {\r
1075                         var tmp = d.lettersPool[d.lettersPoolN];\r
1076                         tmp.Text = str;\r
1077                         tmp.Width = 500;\r
1078                         tmp.Height = 500;\r
1079                         tmp.FontSize = font.size;\r
1080                         //tmp.FontFamily = ,\r
1081                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1082                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1083                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1084                         tmp.Foreground = fill.getNativeFromStack(d);\r
1085                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1086                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1087                         tmp.Visibility = Visibility.Visible;\r
1088                         d.lettersPoolN++;\r
1089                         return tmp;\r
1090                 }\r
1091                 public void modifyNative(System.Windows.Controls.TextBlock tmp, Canvas d)\r
1092                 {\r
1093                         tmp.Text = str;\r
1094                         tmp.Width = 500;\r
1095                         tmp.Height = 500;\r
1096                         tmp.FontSize = font.size;\r
1097                         //tmp.FontFamily = ,\r
1098                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1099                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1100                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1101                         tmp.Foreground = fill.getNativeFromStack(d);\r
1102                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1103                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1104                         tmp.Visibility = Visibility.Visible;\r
1105                 }\r
1106         }\r
1107         \r
1108         #endregion\r
1109         \r
1110         #region Image\r
1111 \r
1112         partial class Image\r
1113         {\r
1114                 internal void initialize__(int wid, int hei)\r
1115                 {\r
1116                         AsyncBool = false;\r
1117                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
1118                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1119                 }\r
1120                 internal void create__(int wid, int hei)\r
1121                 {\r
1122                         buffer = new WriteableBitmap(wid, hei);\r
1123                         AsyncBool = true;\r
1124                 }\r
1125                 internal void load__(string uri)\r
1126                 {\r
1127                         AsyncBool = false;\r
1128                         var ur = new System.Uri(uri,  System.UriKind.RelativeOrAbsolute);\r
1129                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action<Uri>(load_), ur);\r
1130                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1131                 }\r
1132                 internal void load_(Uri uri)\r
1133                 {\r
1134                         var bitmap = new BitmapImage();\r
1135                         bitmap.CreateOptions = BitmapCreateOptions.None;\r
1136                         bitmap.UriSource = uri;\r
1137                         //try\r
1138                         //{\r
1139                                 var wbm = new System.Windows.Media.Imaging.WriteableBitmap(bitmap);\r
1140                                 buffer = wbm;\r
1141                         //}\r
1142                         //catch (Exception e)\r
1143                         //{\r
1144                         //      buffer = new WriteableBitmap(64, 64);\r
1145                         //      buffer.ForEach(bitmap_drawChecker);\r
1146                         //}\r
1147                         self_rect.set(buffer.PixelWidth, buffer.PixelHeight);\r
1148                         AsyncBool = true;\r
1149                 }\r
1150                 static System.Windows.Media.Color[] CHECKER_C;\r
1151                 static Image()\r
1152                 {\r
1153                         CHECKER_C = new System.Windows.Media.Color[2];\r
1154                         CHECKER_C[0] = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);\r
1155                         CHECKER_C[1] = System.Windows.Media.Color.FromArgb(128,128,128,128);\r
1156                 }\r
1157                 static System.Windows.Media.Color bitmap_drawChecker(int x, int y)\r
1158                 {\r
1159                         return ((x / 4) + (y / 4)) % 2 == 0 ? CHECKER_C[0] : CHECKER_C[1];\r
1160                 }\r
1161                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
1162                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
1163                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
1164                 {\r
1165                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
1166                         //buffer.ForEach(func);\r
1167                 }\r
1168                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1169                 {\r
1170                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
1171                         //buffer.ForEach(func);\r
1172                 }\r
1173                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
1174                 {\r
1175                         buffer.ForEach(func);\r
1176                 }\r
1177                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1178                 {\r
1179                         buffer.ForEach(func);\r
1180                 }\r
1181 \r
1182                 public Image clone()\r
1183                 {\r
1184                         return (Image)MemberwiseClone();\r
1185                 }\r
1186                 public static implicit operator System.Windows.Controls.Image(Image d)\r
1187                 {\r
1188                         var tmp = new System.Windows.Controls.Image();\r
1189                         tmp.Source = d.buffer;\r
1190                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
1191                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
1192                         return tmp;\r
1193                 }\r
1194                 public UIElement toNative() { return this; }\r
1195 \r
1196                 public void copyToStack(Templates.StackableDrawable d)\r
1197                 {\r
1198                         var tmp = d.imageStack[d.imageStackN];\r
1199                         tmp.datum = datum;\r
1200                         tmp.buffer = buffer;\r
1201                         tmp.self_rect = self_rect;\r
1202                         d.stack[d.stackN] = tmp;\r
1203                         d.imageStackN++;\r
1204                         d.stackN++;\r
1205                 }\r
1206                 public UIElement poolNative(Canvas d)\r
1207                 {\r
1208                         var tmp = d.imagePool[d.imagePoolN];\r
1209                         tmp.Source = buffer;\r
1210                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1211                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1212                         tmp.Visibility = Visibility.Visible;\r
1213                         d.imagePoolN++;\r
1214                         return tmp;\r
1215                 }\r
1216                 public void modifyNative(System.Windows.Controls.Image tmp, Canvas d)\r
1217                 {\r
1218                         tmp.Source = buffer;\r
1219                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1220                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1221                         tmp.Visibility = Visibility.Visible;\r
1222                 }\r
1223 \r
1224         }\r
1225 \r
1226         #endregion\r
1227 \r
1228         #region Group\r
1229 \r
1230         partial class Group\r
1231         {\r
1232                 internal void initialize__()\r
1233                 {\r
1234                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.SimpleProcedure(create__));\r
1235                 }\r
1236                 internal void create__()\r
1237                 {\r
1238                         cnvs = new System.Windows.Controls.Canvas();\r
1239                         trans = new System.Windows.Media.TransformGroup();\r
1240                         transF = new System.Windows.Media.TransformCollection();\r
1241                         rotateF = new System.Windows.Media.RotateTransform();\r
1242                         scaleF = new System.Windows.Media.ScaleTransform();\r
1243                         translateF = new System.Windows.Media.TranslateTransform();\r
1244                         transF.Add(rotateF);\r
1245                         transF.Add(scaleF);\r
1246                         transF.Add(translateF);\r
1247                         trans.Children = transF;\r
1248                         cnvs.RenderTransform = trans;\r
1249                         AsyncBool = true;\r
1250                 }\r
1251                 public Group clone()\r
1252                 {\r
1253                         return (Group)MemberwiseClone();\r
1254                 }\r
1255 \r
1256                 delegate void AppendFunc1(Internal.PrimitiveFigure func);\r
1257                 void append__(Internal.PrimitiveFigure fig)\r
1258                 {\r
1259                         fig.centering(0, 0);\r
1260                         UIElement e = fig.toNative();\r
1261                         cnvs.Children.Add(e);\r
1262                         System.Windows.Controls.Canvas.SetLeft(e, fig.datum.x);\r
1263                         System.Windows.Controls.Canvas.SetTop(e, fig.datum.y);\r
1264                 }\r
1265                 delegate void SimpleProcedure();\r
1266                 void getRotation__() { rotation_ = rotateF.Angle; }\r
1267                 void setRotation__() { rotateF.Angle = rotation_; }\r
1268                 //void getTranslation__() { rotation_ = rotateF.Angle; }\r
1269                 void setTranslation__() { translateF.X = datum.x; translateF.Y = datum.y; }\r
1270                 void setScaling__() { scaleF.ScaleX = scaling_.x; scaleF.ScaleY = scaling_.y; }\r
1271 \r
1272                 public static implicit operator System.Windows.Controls.Canvas(Group d)\r
1273                 {\r
1274                         var tmp = d.cnvs;//new System.Windows.Controls.Canvas();\r
1275                         System.Windows.Controls.Canvas.SetLeft(d.cnvs, d.datum.x);\r
1276                         System.Windows.Controls.Canvas.SetTop(d.cnvs, d.datum.y);\r
1277                         return tmp;\r
1278                 }\r
1279                 public UIElement toNative() { return this; }\r
1280 \r
1281                 public void copyToStack(Templates.StackableDrawable d)\r
1282                 {\r
1283                         var tmp = d.groupStack[d.groupStackN];\r
1284                         tmp.datum = datum;\r
1285                         tmp.cnvs = cnvs;\r
1286                         d.stack[d.stackN] = tmp;\r
1287                         d.groupStackN++;\r
1288                         d.stackN++;\r
1289                 }\r
1290                 public UIElement poolNative(Canvas d)\r
1291                 {\r
1292                         //d.groupPool[d.groupPoolN] = cnvs;\r
1293                         //var tmp = d.groupPool[d.groupPoolN];\r
1294                         var tmp = cnvs;\r
1295                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1296                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1297                         tmp.Visibility = Visibility.Visible;\r
1298                         //d.groupPoolN++;\r
1299                         return tmp;\r
1300                 }\r
1301                 public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d)\r
1302                 {\r
1303                         d.groupPool[d.groupPoolN] = cnvs;\r
1304                         System.Windows.Controls.Canvas.SetLeft(cnvs, datum.x);\r
1305                         System.Windows.Controls.Canvas.SetTop(cnvs, datum.y);\r
1306                         tmp.Visibility = Visibility.Visible;\r
1307                 }\r
1308 \r
1309         }\r
1310         #endregion\r
1311         \r
1312         #endregion\r
1313 \r
1314 \r
1315 }