OSDN Git Service

5b58b031df218d887f167a1c38330e60a548d251
[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) : base()\r
655                 {\r
656                         panel = default_panel;\r
657                         api_canvas = default_api_canvas;\r
658                         initialize(500, 500);\r
659                 }\r
660 \r
661 \r
662                 public void showFPS(bool sw = true) { }\r
663                 public void watchFPS(bool sw = true) { }\r
664 \r
665 \r
666                 public void clear(double lum)\r
667                 {\r
668                         clear(new Color(lum));\r
669                 }\r
670 \r
671                 #endregion\r
672 \r
673 \r
674         }\r
675 \r
676 \r
677 \r
678         #region primitive tokenizer\r
679 \r
680 \r
681         #region primitive\r
682 \r
683         partial struct Point\r
684         {\r
685                 public static implicit operator System.Windows.Point(Point d)\r
686                 {\r
687                         return new System.Windows.Point(d.x, d.y);\r
688                 }\r
689         }\r
690 \r
691         partial struct Color\r
692         {\r
693                 public static implicit operator System.Windows.Media.Color(Color d)\r
694                 {\r
695                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
696                 }\r
697                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
698                 {\r
699                         return new SolidColorBrush { Color = d };\r
700                 }\r
701                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
702                 {\r
703                         var tmp = d.brushPool[d.brushPoolN];\r
704                         tmp.Color = this;\r
705                         d.brushPoolN++;\r
706                         return tmp;\r
707                 }\r
708 \r
709         }\r
710 \r
711         partial struct Stroke\r
712         {\r
713                 public void apply(System.Windows.Shapes.Shape target)\r
714                 {\r
715                         target.Stroke = this;\r
716                         //target.StrokeDashArray\r
717                         target.StrokeThickness = thick;\r
718                 }\r
719                 public static implicit operator SolidColorBrush(Stroke d)\r
720                 {\r
721                         return new SolidColorBrush { Color = d.color };\r
722                 }\r
723                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
724                 {\r
725                         var tmp = d.brushPool[d.brushPoolN];\r
726                         tmp.Color = this.color;\r
727                         d.brushPoolN++;\r
728                         return tmp;\r
729                 }\r
730         }\r
731         \r
732         #endregion\r
733         \r
734         #region Line\r
735 \r
736         partial class Line\r
737         {\r
738                 public Line dup()\r
739                 {\r
740                         return (Line)MemberwiseClone();\r
741                 }\r
742                 public Line clone()\r
743                 {\r
744                         return (Line)MemberwiseClone();\r
745                 }\r
746                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
747                 {\r
748                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
749                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
750                         else d.stroke.apply(tmp);\r
751                         return tmp;\r
752                 }\r
753                 public UIElement toNative() { return this; }\r
754 \r
755                 public void copyToStack(Templates.StackableDrawable d)\r
756                 {\r
757                         var tmp = d.lineStack[d.lineStackN];\r
758                         tmp.begin.x = begin.x;\r
759                         tmp.begin.y = begin.y;\r
760                         tmp.end.x = end.x;\r
761                         tmp.end.y = end.y;\r
762                         tmp.fill = fill;\r
763                         tmp.stroke = stroke;\r
764                         d.stack[d.stackN] = tmp;\r
765                         d.lineStackN++;\r
766                         d.stackN++;\r
767                 }\r
768                 public UIElement poolNative(Canvas d)\r
769                 {\r
770                         var tmp = d.linePool[d.linePoolN];\r
771                         tmp.X1 = begin.x;\r
772                         tmp.Y1 = begin.y;\r
773                         tmp.X2 = end.x;\r
774                         tmp.Y2 = end.y;\r
775                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
776                         else stroke.apply(tmp);\r
777                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
778                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
779                         tmp.Visibility = Visibility.Visible;\r
780                         d.linePoolN++;\r
781                         return tmp;\r
782                 }\r
783                 public void modifyNative(System.Windows.Shapes.Line tmp, Canvas d)\r
784                 {\r
785                         tmp.X1 = begin.x;\r
786                         tmp.Y1 = begin.y;\r
787                         tmp.X2 = end.x;\r
788                         tmp.Y2 = end.y;\r
789                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
790                         else stroke.apply(tmp);\r
791                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
792                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
793                         tmp.Visibility = Visibility.Visible;\r
794                 }\r
795         }\r
796         \r
797         #endregion\r
798                 \r
799         #region Rectangle\r
800 \r
801         partial class Rectangle\r
802         {\r
803                 public Rectangle dup()\r
804                 {\r
805                         return (Rectangle)MemberwiseClone();\r
806                 }\r
807                 public Rectangle clone()\r
808                 {\r
809                         return (Rectangle)MemberwiseClone();\r
810                 }\r
811                 public static implicit operator System.Windows.Rect(Rectangle d)\r
812                 {\r
813                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
814                 }\r
815                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
816                 {\r
817                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
818                         d.stroke.apply(tmp);\r
819                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
820                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
821                         return tmp;\r
822                 }\r
823 \r
824                 public UIElement toNative() { return this; }\r
825                 public void copyToStack(Templates.StackableDrawable d)\r
826                 {\r
827                         var tmp = d.rectStack[d.rectStackN];\r
828                         tmp.v1 = v1;\r
829                         tmp.v2 = v2;\r
830                         tmp.fill = fill;\r
831                         d.stack[d.stackN] = tmp;\r
832                         d.rectStackN++;\r
833                         d.stackN++;\r
834                 }\r
835                 public UIElement poolNative(Canvas d)\r
836                 {\r
837                         var tmp = d.rectPool[d.rectPoolN];\r
838                         tmp.Width = width;\r
839                         tmp.Height = height;\r
840                         tmp.Fill = fill.getNativeFromStack(d);\r
841                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
842                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
843                         tmp.Visibility = Visibility.Visible;\r
844                         d.rectPoolN++;\r
845                         return tmp;\r
846                 }\r
847                 public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d)\r
848                 {\r
849                         tmp.Width = width;\r
850                         tmp.Height = height;\r
851                         tmp.Fill = fill.getNativeFromStack(d);\r
852                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
853                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
854                         tmp.Visibility = Visibility.Visible;\r
855                 }\r
856         }\r
857         \r
858         #endregion\r
859 \r
860         #region Ellipse\r
861 \r
862         partial class Ellipse\r
863         {\r
864                 public Ellipse dup()\r
865                 {\r
866                         return (Ellipse)MemberwiseClone();\r
867                 }\r
868                 public Ellipse clone()\r
869                 {\r
870                         return (Ellipse)MemberwiseClone();\r
871                 }\r
872                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
873                 {\r
874                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
875                         d.stroke.apply(tmp);\r
876                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
877                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
878                         return tmp;\r
879                 }\r
880 \r
881                 public UIElement toNative() { return this; }\r
882 \r
883                 public void copyToStack(Templates.StackableDrawable d)\r
884                 {\r
885                         var tmp = d.ellipseStack[d.ellipseStackN];\r
886                         tmp.datum = datum;\r
887                         tmp.xdiameter = xdiameter;\r
888                         tmp.ydiameter = ydiameter;\r
889                         tmp.fill = fill;\r
890                         d.stack[d.stackN] = tmp;\r
891                         d.ellipseStackN++;\r
892                         d.stackN++;\r
893                 }\r
894                 public UIElement poolNative(Canvas d)\r
895                 {\r
896                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
897                         tmp.Width = width;\r
898                         tmp.Height = height;\r
899                         tmp.Fill = fill.getNativeFromStack(d);\r
900                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
901                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
902                         tmp.Visibility = Visibility.Visible;\r
903                         d.ellipsePoolN++;\r
904                         return tmp;\r
905                 }\r
906                 public void modifyNative(System.Windows.Shapes.Ellipse tmp, Canvas d)\r
907                 {\r
908                         tmp.Width = width;\r
909                         tmp.Height = height;\r
910                         tmp.Fill = fill.getNativeFromStack(d);\r
911                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
912                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
913                         tmp.Visibility = Visibility.Visible;\r
914                 }\r
915         }\r
916 \r
917         #endregion\r
918 \r
919         #region Polygon\r
920 \r
921         partial class Polygon\r
922         {\r
923                 public Polygon dup()\r
924                 {\r
925                         return (Polygon)MemberwiseClone();\r
926                 }\r
927                 public Polygon clone()\r
928                 {\r
929                         return (Polygon)MemberwiseClone();\r
930                 }\r
931                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
932                 {\r
933                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
934                         d.stroke.apply(tmp);\r
935                         foreach (Point p in d.vertices)\r
936                         {\r
937                                 tmp.Points.Add(p);\r
938                         }\r
939                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
940                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
941                         return tmp;\r
942                 }\r
943                 public UIElement toNative() { return this; }\r
944 \r
945                 public void copyToStack(Templates.StackableDrawable d)\r
946                 {\r
947                         var tmp = d.polygonStack[d.polygonStackN];\r
948                         tmp.datum = datum;\r
949                         tmp.vertices.Clear();\r
950                         foreach (var v in vertices)\r
951                         {\r
952                                 tmp.vertices.Add(v);\r
953                         }\r
954                         tmp.fill = fill;\r
955                         d.stack[d.stackN] = tmp;\r
956                         d.polygonStackN++;\r
957                         d.stackN++;\r
958                 }\r
959                 public UIElement poolNative(Canvas d)\r
960                 {\r
961                         var tmp = d.polygonPool[d.polygonPoolN];\r
962                         tmp.Fill = fill.getNativeFromStack(d);\r
963                         tmp.Points.Clear();\r
964                         foreach (var v in vertices)\r
965                         {\r
966                                 tmp.Points.Add(v);\r
967                         }\r
968                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
969                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
970                         tmp.Visibility = Visibility.Visible;\r
971                         d.polygonPoolN++;\r
972                         return tmp;\r
973                 }\r
974                 public void modifyNative(System.Windows.Shapes.Polygon tmp, Canvas d)\r
975                 {\r
976                         tmp.Fill = fill.getNativeFromStack(d);\r
977                         tmp.Points.Clear();\r
978                         foreach (var v in vertices)\r
979                         {\r
980                                 tmp.Points.Add(v);\r
981                         }\r
982                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
983                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
984                         tmp.Visibility = Visibility.Visible;\r
985                 }\r
986 \r
987         }\r
988                 \r
989         #endregion\r
990 \r
991         #region Letters\r
992 \r
993         partial class Letters\r
994         {\r
995                 #region static initializer\r
996                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
997                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
998                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
999                 static Letters()\r
1000                 {\r
1001                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
1002                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
1003                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
1004                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
1005                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
1006                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
1007                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
1008                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
1009                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
1010                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
1011                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
1012                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
1013                 }\r
1014                 #endregion\r
1015                 public Letters dup()\r
1016                 {\r
1017                         return (Letters)MemberwiseClone();\r
1018                 }\r
1019                 public Letters clone()\r
1020                 {\r
1021                         return (Letters)MemberwiseClone();\r
1022                 }\r
1023                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
1024                 {\r
1025                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
1026                         var tmp = new System.Windows.Controls.TextBlock {\r
1027                                 Text = d.str, Width = 500, Height = 500,\r
1028                                 FontSize = d.font.size,\r
1029                                 //tmp.FontFamily = ,\r
1030                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
1031                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
1032                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
1033                                 Foreground = d.fill\r
1034                         };\r
1035                         double left = 0;\r
1036                         switch (d.align)\r
1037                         {\r
1038                                 case Letters.HorizontalAlign.left: break;\r
1039                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
1040                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
1041                         }\r
1042                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
1043                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
1044                         return tmp;\r
1045                 }\r
1046                 public UIElement toNative() { return this; }\r
1047 \r
1048                 public void copyToStack(Templates.StackableDrawable d)\r
1049                 {\r
1050                         var tmp = d.lettersStack[d.lettersStackN];\r
1051                         tmp.str = str;\r
1052                         tmp.datum = datum;\r
1053                         tmp.fill = fill;\r
1054                         d.stack[d.stackN] = tmp;\r
1055                         d.lettersStackN++;\r
1056                         d.stackN++;\r
1057                 }\r
1058                 public UIElement poolNative(Canvas d)\r
1059                 {\r
1060                         var tmp = d.lettersPool[d.lettersPoolN];\r
1061                         tmp.Text = str;\r
1062                         tmp.Width = 500;\r
1063                         tmp.Height = 500;\r
1064                         tmp.FontSize = font.size;\r
1065                         //tmp.FontFamily = ,\r
1066                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1067                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1068                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1069                         tmp.Foreground = fill.getNativeFromStack(d);\r
1070                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1071                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1072                         tmp.Visibility = Visibility.Visible;\r
1073                         d.lettersPoolN++;\r
1074                         return tmp;\r
1075                 }\r
1076                 public void modifyNative(System.Windows.Controls.TextBlock tmp, Canvas d)\r
1077                 {\r
1078                         tmp.Text = str;\r
1079                         tmp.Width = 500;\r
1080                         tmp.Height = 500;\r
1081                         tmp.FontSize = font.size;\r
1082                         //tmp.FontFamily = ,\r
1083                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1084                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1085                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1086                         tmp.Foreground = fill.getNativeFromStack(d);\r
1087                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1088                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1089                         tmp.Visibility = Visibility.Visible;\r
1090                 }\r
1091         }\r
1092         \r
1093         #endregion\r
1094         \r
1095         #region Image\r
1096 \r
1097         partial class Image\r
1098         {\r
1099                 internal void initialize__(int wid, int hei)\r
1100                 {\r
1101                         AsyncBool = false;\r
1102                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
1103                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1104                 }\r
1105                 internal void create__(int wid, int hei)\r
1106                 {\r
1107                         buffer = new WriteableBitmap(wid, hei);\r
1108                         AsyncBool = true;\r
1109                 }\r
1110                 internal void load__(string uri)\r
1111                 {\r
1112                         AsyncBool = false;\r
1113                         var ur = new System.Uri(uri,  System.UriKind.RelativeOrAbsolute);\r
1114                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action<Uri>(load_), ur);\r
1115                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1116                 }\r
1117                 internal void load_(Uri uri)\r
1118                 {\r
1119                         var bitmap = new BitmapImage();\r
1120                         bitmap.CreateOptions = BitmapCreateOptions.None;\r
1121                         bitmap.UriSource = uri;\r
1122                         //try\r
1123                         //{\r
1124                                 var wbm = new System.Windows.Media.Imaging.WriteableBitmap(bitmap);\r
1125                                 buffer = wbm;\r
1126                         //}\r
1127                         //catch (Exception e)\r
1128                         //{\r
1129                         //      buffer = new WriteableBitmap(64, 64);\r
1130                         //      buffer.ForEach(bitmap_drawChecker);\r
1131                         //}\r
1132                         self_rect.set(buffer.PixelWidth, buffer.PixelHeight);\r
1133                         AsyncBool = true;\r
1134                 }\r
1135                 static System.Windows.Media.Color[] CHECKER_C;\r
1136                 static Image()\r
1137                 {\r
1138                         CHECKER_C = new System.Windows.Media.Color[2];\r
1139                         CHECKER_C[0] = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);\r
1140                         CHECKER_C[1] = System.Windows.Media.Color.FromArgb(128,128,128,128);\r
1141                 }\r
1142                 static System.Windows.Media.Color bitmap_drawChecker(int x, int y)\r
1143                 {\r
1144                         return ((x / 4) + (y / 4)) % 2 == 0 ? CHECKER_C[0] : CHECKER_C[1];\r
1145                 }\r
1146                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
1147                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
1148                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
1149                 {\r
1150                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
1151                         //buffer.ForEach(func);\r
1152                 }\r
1153                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1154                 {\r
1155                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
1156                         //buffer.ForEach(func);\r
1157                 }\r
1158                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
1159                 {\r
1160                         buffer.ForEach(func);\r
1161                 }\r
1162                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1163                 {\r
1164                         buffer.ForEach(func);\r
1165                 }\r
1166 \r
1167                 public Image clone()\r
1168                 {\r
1169                         return (Image)MemberwiseClone();\r
1170                 }\r
1171                 public static implicit operator System.Windows.Controls.Image(Image d)\r
1172                 {\r
1173                         var tmp = new System.Windows.Controls.Image();\r
1174                         tmp.Source = d.buffer;\r
1175                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
1176                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
1177                         return tmp;\r
1178                 }\r
1179                 public UIElement toNative() { return this; }\r
1180 \r
1181                 public void copyToStack(Templates.StackableDrawable d)\r
1182                 {\r
1183                         var tmp = d.imageStack[d.imageStackN];\r
1184                         tmp.datum = datum;\r
1185                         tmp.buffer = buffer;\r
1186                         tmp.self_rect = self_rect;\r
1187                         d.stack[d.stackN] = tmp;\r
1188                         d.imageStackN++;\r
1189                         d.stackN++;\r
1190                 }\r
1191                 public UIElement poolNative(Canvas d)\r
1192                 {\r
1193                         var tmp = d.imagePool[d.imagePoolN];\r
1194                         tmp.Source = buffer;\r
1195                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1196                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1197                         tmp.Visibility = Visibility.Visible;\r
1198                         d.imagePoolN++;\r
1199                         return tmp;\r
1200                 }\r
1201                 public void modifyNative(System.Windows.Controls.Image tmp, Canvas d)\r
1202                 {\r
1203                         tmp.Source = buffer;\r
1204                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1205                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1206                         tmp.Visibility = Visibility.Visible;\r
1207                 }\r
1208 \r
1209         }\r
1210 \r
1211         #endregion\r
1212 \r
1213         #region Group\r
1214 \r
1215         partial class Group\r
1216         {\r
1217                 internal void initialize__()\r
1218                 {\r
1219                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.SimpleProcedure(create__));\r
1220                 }\r
1221                 internal void create__()\r
1222                 {\r
1223                         cnvs = new System.Windows.Controls.Canvas();\r
1224                         trans = new System.Windows.Media.TransformGroup();\r
1225                         transF = new System.Windows.Media.TransformCollection();\r
1226                         rotateF = new System.Windows.Media.RotateTransform();\r
1227                         scaleF = new System.Windows.Media.ScaleTransform();\r
1228                         translateF = new System.Windows.Media.TranslateTransform();\r
1229                         transF.Add(rotateF);\r
1230                         transF.Add(scaleF);\r
1231                         transF.Add(translateF);\r
1232                         trans.Children = transF;\r
1233                         cnvs.RenderTransform = trans;\r
1234                         AsyncBool = true;\r
1235                 }\r
1236                 public Group clone()\r
1237                 {\r
1238                         return (Group)MemberwiseClone();\r
1239                 }\r
1240 \r
1241                 delegate void AppendFunc1(Internal.PrimitiveFigure func);\r
1242                 void append__(Internal.PrimitiveFigure fig)\r
1243                 {\r
1244                         fig.centering(0, 0);\r
1245                         UIElement e = fig.toNative();\r
1246                         cnvs.Children.Add(e);\r
1247                         System.Windows.Controls.Canvas.SetLeft(e, fig.datum.x);\r
1248                         System.Windows.Controls.Canvas.SetTop(e, fig.datum.y);\r
1249                 }\r
1250                 delegate void SimpleProcedure();\r
1251                 void getRotation__() { rotation_ = rotateF.Angle; }\r
1252                 void setRotation__() { rotateF.Angle = rotation_; }\r
1253                 //void getTranslation__() { rotation_ = rotateF.Angle; }\r
1254                 void setTranslation__() { translateF.X = datum.x; translateF.Y = datum.y; }\r
1255                 void setScaling__() { scaleF.ScaleX = scaling_.x; scaleF.ScaleY = scaling_.y; }\r
1256 \r
1257                 public static implicit operator System.Windows.Controls.Canvas(Group d)\r
1258                 {\r
1259                         var tmp = d.cnvs;//new System.Windows.Controls.Canvas();\r
1260                         System.Windows.Controls.Canvas.SetLeft(d.cnvs, d.datum.x);\r
1261                         System.Windows.Controls.Canvas.SetTop(d.cnvs, d.datum.y);\r
1262                         return tmp;\r
1263                 }\r
1264                 public UIElement toNative() { return this; }\r
1265 \r
1266                 public void copyToStack(Templates.StackableDrawable d)\r
1267                 {\r
1268                         var tmp = d.groupStack[d.groupStackN];\r
1269                         tmp.datum = datum;\r
1270                         tmp.cnvs = cnvs;\r
1271                         d.stack[d.stackN] = tmp;\r
1272                         d.groupStackN++;\r
1273                         d.stackN++;\r
1274                 }\r
1275                 public UIElement poolNative(Canvas d)\r
1276                 {\r
1277                         //d.groupPool[d.groupPoolN] = cnvs;\r
1278                         //var tmp = d.groupPool[d.groupPoolN];\r
1279                         var tmp = cnvs;\r
1280                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1281                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1282                         tmp.Visibility = Visibility.Visible;\r
1283                         //d.groupPoolN++;\r
1284                         return tmp;\r
1285                 }\r
1286                 public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d)\r
1287                 {\r
1288                         d.groupPool[d.groupPoolN] = cnvs;\r
1289                         System.Windows.Controls.Canvas.SetLeft(cnvs, datum.x);\r
1290                         System.Windows.Controls.Canvas.SetTop(cnvs, datum.y);\r
1291                         tmp.Visibility = Visibility.Visible;\r
1292                 }\r
1293 \r
1294         }\r
1295         #endregion\r
1296         \r
1297         #endregion\r
1298 \r
1299 \r
1300 }