OSDN Git Service

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