OSDN Git Service

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