OSDN Git Service

7f0267bb4fa2ef3b3764940fc751a41991135dae
[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 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         partial struct Color\r
736         {\r
737                 public static implicit operator System.Windows.Media.Color(Color d)\r
738                 {\r
739                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
740                 }\r
741                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
742                 {\r
743                         return new SolidColorBrush { Color = d };\r
744                 }\r
745                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
746                 {\r
747                         var tmp = d.brushPool[d.brushPoolN];\r
748                         tmp.Color = this;\r
749                         d.brushPoolN++;\r
750                         return tmp;\r
751                 }\r
752 \r
753         }\r
754 \r
755         partial struct Stroke\r
756         {\r
757                 public void apply(System.Windows.Shapes.Shape target)\r
758                 {\r
759                         target.Stroke = this;\r
760                         //target.StrokeDashArray\r
761                         target.StrokeThickness = thick;\r
762                 }\r
763                 public static implicit operator SolidColorBrush(Stroke d)\r
764                 {\r
765                         return new SolidColorBrush { Color = d.color };\r
766                 }\r
767                 public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d)\r
768                 {\r
769                         var tmp = d.brushPool[d.brushPoolN];\r
770                         tmp.Color = this.color;\r
771                         d.brushPoolN++;\r
772                         return tmp;\r
773                 }\r
774         }\r
775         \r
776         #endregion\r
777         \r
778         #region Line\r
779 \r
780         partial class Line\r
781         {\r
782                 public Line dup()\r
783                 {\r
784                         return (Line)MemberwiseClone();\r
785                 }\r
786                 public Line clone()\r
787                 {\r
788                         return (Line)MemberwiseClone();\r
789                 }\r
790                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
791                 {\r
792                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
793                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
794                         else d.stroke.apply(tmp);\r
795                         return tmp;\r
796                 }\r
797                 public UIElement toNative() { return this; }\r
798 \r
799                 public void copyToStack(Templates.StackableDrawable d)\r
800                 {\r
801                         var tmp = d.lineStack[d.lineStackN];\r
802                         tmp.begin.x = begin.x;\r
803                         tmp.begin.y = begin.y;\r
804                         tmp.end.x = end.x;\r
805                         tmp.end.y = end.y;\r
806                         tmp.fill = fill;\r
807                         tmp.stroke = stroke;\r
808                         d.stack[d.stackN] = tmp;\r
809                         d.lineStackN++;\r
810                         d.stackN++;\r
811                 }\r
812                 public UIElement poolNative(Canvas d)\r
813                 {\r
814                         var tmp = d.linePool[d.linePoolN];\r
815                         tmp.X1 = begin.x;\r
816                         tmp.Y1 = begin.y;\r
817                         tmp.X2 = end.x;\r
818                         tmp.Y2 = end.y;\r
819                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
820                         else stroke.apply(tmp);\r
821                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
822                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
823                         tmp.Visibility = Visibility.Visible;\r
824                         d.linePoolN++;\r
825                         return tmp;\r
826                 }\r
827                 public void modifyNative(System.Windows.Shapes.Line tmp, Canvas d)\r
828                 {\r
829                         tmp.X1 = begin.x;\r
830                         tmp.Y1 = begin.y;\r
831                         tmp.X2 = end.x;\r
832                         tmp.Y2 = end.y;\r
833                         if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d);\r
834                         else stroke.apply(tmp);\r
835                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
836                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
837                         tmp.Visibility = Visibility.Visible;\r
838                 }\r
839         }\r
840         \r
841         #endregion\r
842                 \r
843         #region Rectangle\r
844 \r
845         partial class Rectangle\r
846         {\r
847                 public Rectangle dup()\r
848                 {\r
849                         return (Rectangle)MemberwiseClone();\r
850                 }\r
851                 public Rectangle clone()\r
852                 {\r
853                         return (Rectangle)MemberwiseClone();\r
854                 }\r
855                 public static implicit operator System.Windows.Rect(Rectangle d)\r
856                 {\r
857                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
858                 }\r
859                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
860                 {\r
861                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
862                         d.stroke.apply(tmp);\r
863                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
864                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
865                         return tmp;\r
866                 }\r
867 \r
868                 public UIElement toNative() { return this; }\r
869                 public void copyToStack(Templates.StackableDrawable d)\r
870                 {\r
871                         var tmp = d.rectStack[d.rectStackN];\r
872                         tmp.v1 = v1;\r
873                         tmp.v2 = v2;\r
874                         tmp.fill = fill;\r
875                         d.stack[d.stackN] = tmp;\r
876                         d.rectStackN++;\r
877                         d.stackN++;\r
878                 }\r
879                 public UIElement poolNative(Canvas d)\r
880                 {\r
881                         var tmp = d.rectPool[d.rectPoolN];\r
882                         tmp.Width = width;\r
883                         tmp.Height = height;\r
884                         tmp.Fill = fill.getNativeFromStack(d);\r
885                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
886                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
887                         tmp.Visibility = Visibility.Visible;\r
888                         d.rectPoolN++;\r
889                         return tmp;\r
890                 }\r
891                 public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d)\r
892                 {\r
893                         tmp.Width = width;\r
894                         tmp.Height = height;\r
895                         tmp.Fill = fill.getNativeFromStack(d);\r
896                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
897                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
898                         tmp.Visibility = Visibility.Visible;\r
899                 }\r
900         }\r
901         \r
902         #endregion\r
903         \r
904         #region ShaderField\r
905 \r
906         partial class ShaderField\r
907         {\r
908                 public System.Windows.Media.Effects.Effect shader;\r
909                 protected static System.Windows.Media.SolidColorBrush dummyfill = null;\r
910 \r
911                 protected static void initializeShader()\r
912                 {\r
913                         dummyfill = new SolidColorBrush(System.Windows.Media.Colors.Blue);\r
914                 }\r
915 \r
916                 public UIElement toNative() { return null; }\r
917                 public void copyToStack(Templates.StackableDrawable d)\r
918                 {\r
919                         var tmp = d.shaderStack[d.shaderStackN];\r
920                         tmp.initialize__ = initialize__;\r
921                         tmp.setParameters = setParameters;\r
922                         tmp.v1 = v1;\r
923                         tmp.v2 = v2;\r
924                         tmp.shader = shader;\r
925                         d.stack[d.stackN] = tmp;\r
926                         d.shaderStackN++;\r
927                         d.stackN++;\r
928                 }\r
929                 public UIElement poolNative(Canvas d)\r
930                 {\r
931                         var tmp = d.shaderPool[d.shaderPoolN];\r
932                         tmp.Width = width;\r
933                         tmp.Height = height;\r
934                         if (!initialized) { initialize__(); }\r
935                         setParameters();\r
936                         tmp.Effect = shader;\r
937                         tmp.Fill = dummyfill;\r
938                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
939                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
940                         tmp.Visibility = Visibility.Visible;\r
941                         d.shaderPoolN++;\r
942                         return tmp;\r
943                 }\r
944                 public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d)\r
945                 {\r
946                         tmp.Width = width;\r
947                         tmp.Height = height;\r
948                         if (!initialized) { initialize__(); }\r
949                         setParameters();\r
950                         tmp.Effect = shader;\r
951                         tmp.Fill = dummyfill;\r
952                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
953                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
954                         tmp.Visibility = Visibility.Visible;\r
955                 }\r
956         }\r
957 \r
958         #endregion\r
959 \r
960         #region Ellipse\r
961 \r
962         partial class Ellipse\r
963         {\r
964                 public Ellipse dup()\r
965                 {\r
966                         return (Ellipse)MemberwiseClone();\r
967                 }\r
968                 public Ellipse clone()\r
969                 {\r
970                         return (Ellipse)MemberwiseClone();\r
971                 }\r
972                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
973                 {\r
974                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
975                         d.stroke.apply(tmp);\r
976                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
977                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
978                         return tmp;\r
979                 }\r
980 \r
981                 public UIElement toNative() { return this; }\r
982 \r
983                 public void copyToStack(Templates.StackableDrawable d)\r
984                 {\r
985                         var tmp = d.ellipseStack[d.ellipseStackN];\r
986                         tmp.datum = datum;\r
987                         tmp.xdiameter = xdiameter;\r
988                         tmp.ydiameter = ydiameter;\r
989                         tmp.fill = fill;\r
990                         d.stack[d.stackN] = tmp;\r
991                         d.ellipseStackN++;\r
992                         d.stackN++;\r
993                 }\r
994                 public UIElement poolNative(Canvas d)\r
995                 {\r
996                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
997                         tmp.Width = width;\r
998                         tmp.Height = height;\r
999                         tmp.Fill = fill.getNativeFromStack(d);\r
1000                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
1001                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
1002                         tmp.Visibility = Visibility.Visible;\r
1003                         d.ellipsePoolN++;\r
1004                         return tmp;\r
1005                 }\r
1006                 public void modifyNative(System.Windows.Shapes.Ellipse tmp, Canvas d)\r
1007                 {\r
1008                         tmp.Width = width;\r
1009                         tmp.Height = height;\r
1010                         tmp.Fill = fill.getNativeFromStack(d);\r
1011                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
1012                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
1013                         tmp.Visibility = Visibility.Visible;\r
1014                 }\r
1015         }\r
1016 \r
1017         #endregion\r
1018 \r
1019         #region Polygon\r
1020 \r
1021         partial class Polygon\r
1022         {\r
1023                 public Polygon dup()\r
1024                 {\r
1025                         return (Polygon)MemberwiseClone();\r
1026                 }\r
1027                 public Polygon clone()\r
1028                 {\r
1029                         return (Polygon)MemberwiseClone();\r
1030                 }\r
1031                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
1032                 {\r
1033                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
1034                         d.stroke.apply(tmp);\r
1035                         foreach (Point p in d.vertices)\r
1036                         {\r
1037                                 tmp.Points.Add(p);\r
1038                         }\r
1039                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
1040                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\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.polygonStack[d.polygonStackN];\r
1048                         tmp.datum = datum;\r
1049                         tmp.vertices.Clear();\r
1050                         foreach (var v in vertices)\r
1051                         {\r
1052                                 tmp.vertices.Add(v);\r
1053                         }\r
1054                         tmp.fill = fill;\r
1055                         d.stack[d.stackN] = tmp;\r
1056                         d.polygonStackN++;\r
1057                         d.stackN++;\r
1058                 }\r
1059                 public UIElement poolNative(Canvas d)\r
1060                 {\r
1061                         var tmp = d.polygonPool[d.polygonPoolN];\r
1062                         tmp.Fill = fill.getNativeFromStack(d);\r
1063                         tmp.Points.Clear();\r
1064                         foreach (var v in vertices)\r
1065                         {\r
1066                                 tmp.Points.Add(v);\r
1067                         }\r
1068                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1069                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1070                         tmp.Visibility = Visibility.Visible;\r
1071                         d.polygonPoolN++;\r
1072                         return tmp;\r
1073                 }\r
1074                 public void modifyNative(System.Windows.Shapes.Polygon tmp, Canvas d)\r
1075                 {\r
1076                         tmp.Fill = fill.getNativeFromStack(d);\r
1077                         tmp.Points.Clear();\r
1078                         foreach (var v in vertices)\r
1079                         {\r
1080                                 tmp.Points.Add(v);\r
1081                         }\r
1082                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1083                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1084                         tmp.Visibility = Visibility.Visible;\r
1085                 }\r
1086 \r
1087         }\r
1088                 \r
1089         #endregion\r
1090 \r
1091         #region Letters\r
1092 \r
1093         partial class Letters\r
1094         {\r
1095                 #region static initializer\r
1096                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
1097                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
1098                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
1099                 static Letters()\r
1100                 {\r
1101                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
1102                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
1103                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
1104                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
1105                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
1106                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
1107                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
1108                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
1109                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
1110                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
1111                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
1112                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
1113                 }\r
1114                 #endregion\r
1115                 public Letters dup()\r
1116                 {\r
1117                         return (Letters)MemberwiseClone();\r
1118                 }\r
1119                 public Letters clone()\r
1120                 {\r
1121                         return (Letters)MemberwiseClone();\r
1122                 }\r
1123                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
1124                 {\r
1125                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
1126                         var tmp = new System.Windows.Controls.TextBlock {\r
1127                                 Text = d.str, Width = 500, Height = 500,\r
1128                                 FontSize = d.font.size,\r
1129                                 //tmp.FontFamily = ,\r
1130                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
1131                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
1132                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
1133                                 Foreground = d.fill\r
1134                         };\r
1135                         double left = 0;\r
1136                         switch (d.align)\r
1137                         {\r
1138                                 case Letters.HorizontalAlign.left: break;\r
1139                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
1140                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
1141                         }\r
1142                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
1143                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
1144                         return tmp;\r
1145                 }\r
1146                 public UIElement toNative() { return this; }\r
1147 \r
1148                 public void copyToStack(Templates.StackableDrawable d)\r
1149                 {\r
1150                         var tmp = d.lettersStack[d.lettersStackN];\r
1151                         tmp.str = str;\r
1152                         tmp.datum = datum;\r
1153                         tmp.fill = fill;\r
1154                         d.stack[d.stackN] = tmp;\r
1155                         d.lettersStackN++;\r
1156                         d.stackN++;\r
1157                 }\r
1158                 public UIElement poolNative(Canvas d)\r
1159                 {\r
1160                         var tmp = d.lettersPool[d.lettersPoolN];\r
1161                         tmp.Text = str;\r
1162                         tmp.Width = 500;\r
1163                         tmp.Height = 500;\r
1164                         tmp.FontSize = font.size;\r
1165                         //tmp.FontFamily = ,\r
1166                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1167                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1168                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1169                         tmp.Foreground = fill.getNativeFromStack(d);\r
1170                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1171                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1172                         tmp.Visibility = Visibility.Visible;\r
1173                         d.lettersPoolN++;\r
1174                         return tmp;\r
1175                 }\r
1176                 public void modifyNative(System.Windows.Controls.TextBlock tmp, Canvas d)\r
1177                 {\r
1178                         tmp.Text = str;\r
1179                         tmp.Width = 500;\r
1180                         tmp.Height = 500;\r
1181                         tmp.FontSize = font.size;\r
1182                         //tmp.FontFamily = ,\r
1183                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
1184                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
1185                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
1186                         tmp.Foreground = fill.getNativeFromStack(d);\r
1187                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1188                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1189                         tmp.Visibility = Visibility.Visible;\r
1190                 }\r
1191         }\r
1192         \r
1193         #endregion\r
1194         \r
1195         #region Image\r
1196 \r
1197         partial class Image\r
1198         {\r
1199                 internal void initialize__(int wid, int hei)\r
1200                 {\r
1201                         AsyncBool = false;\r
1202                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action<int,int>(create__), wid, hei);\r
1203                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1204                 }\r
1205                 internal void create__(int wid, int hei)\r
1206                 {\r
1207                         buffer = new WriteableBitmap(wid, hei);\r
1208                         AsyncBool = true;\r
1209                 }\r
1210                 internal void load__(string uri)\r
1211                 {\r
1212                         AsyncBool = false;\r
1213                         var ur = new System.Uri(uri,  System.UriKind.RelativeOrAbsolute);\r
1214                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action<Uri>(load_), ur);\r
1215                         while (!AsyncBool) { System.Threading.Thread.Sleep(10); }\r
1216                 }\r
1217                 internal void load_(Uri uri)\r
1218                 {\r
1219                         var bitmap = new BitmapImage();\r
1220                         bitmap.CreateOptions = BitmapCreateOptions.None;\r
1221                         bitmap.UriSource = uri;\r
1222                         //try\r
1223                         //{\r
1224                                 var wbm = new System.Windows.Media.Imaging.WriteableBitmap(bitmap);\r
1225                                 buffer = wbm;\r
1226                         //}\r
1227                         //catch (Exception e)\r
1228                         //{\r
1229                         //      buffer = new WriteableBitmap(64, 64);\r
1230                         //      buffer.ForEach(bitmap_drawChecker);\r
1231                         //}\r
1232                         self_rect.set(buffer.PixelWidth, buffer.PixelHeight);\r
1233                         AsyncBool = true;\r
1234                 }\r
1235                 static System.Windows.Media.Color[] CHECKER_C;\r
1236                 static Image()\r
1237                 {\r
1238                         CHECKER_C = new System.Windows.Media.Color[2];\r
1239                         CHECKER_C[0] = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);\r
1240                         CHECKER_C[1] = System.Windows.Media.Color.FromArgb(128,128,128,128);\r
1241                 }\r
1242                 static System.Windows.Media.Color bitmap_drawChecker(int x, int y)\r
1243                 {\r
1244                         return ((x / 4) + (y / 4)) % 2 == 0 ? CHECKER_C[0] : CHECKER_C[1];\r
1245                 }\r
1246                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
1247                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
1248                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
1249                 {\r
1250                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
1251                         //buffer.ForEach(func);\r
1252                 }\r
1253                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1254                 {\r
1255                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
1256                         //buffer.ForEach(func);\r
1257                 }\r
1258                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
1259                 {\r
1260                         buffer.ForEach(func);\r
1261                 }\r
1262                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
1263                 {\r
1264                         buffer.ForEach(func);\r
1265                 }\r
1266 \r
1267                 public Image clone()\r
1268                 {\r
1269                         return (Image)MemberwiseClone();\r
1270                 }\r
1271                 public static implicit operator System.Windows.Controls.Image(Image d)\r
1272                 {\r
1273                         var tmp = new System.Windows.Controls.Image();\r
1274                         tmp.Source = d.buffer;\r
1275                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
1276                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
1277                         return tmp;\r
1278                 }\r
1279                 public UIElement toNative() { return this; }\r
1280 \r
1281                 public void copyToStack(Templates.StackableDrawable d)\r
1282                 {\r
1283                         var tmp = d.imageStack[d.imageStackN];\r
1284                         tmp.datum = datum;\r
1285                         tmp.buffer = buffer;\r
1286                         tmp.self_rect = self_rect;\r
1287                         d.stack[d.stackN] = tmp;\r
1288                         d.imageStackN++;\r
1289                         d.stackN++;\r
1290                 }\r
1291                 public UIElement poolNative(Canvas d)\r
1292                 {\r
1293                         var tmp = d.imagePool[d.imagePoolN];\r
1294                         tmp.Source = buffer;\r
1295                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1296                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1297                         tmp.Visibility = Visibility.Visible;\r
1298                         d.imagePoolN++;\r
1299                         return tmp;\r
1300                 }\r
1301                 public void modifyNative(System.Windows.Controls.Image tmp, Canvas d)\r
1302                 {\r
1303                         tmp.Source = buffer;\r
1304                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1305                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1306                         tmp.Visibility = Visibility.Visible;\r
1307                 }\r
1308 \r
1309         }\r
1310 \r
1311         #endregion\r
1312 \r
1313         #region Group\r
1314 \r
1315         partial class Group\r
1316         {\r
1317                 internal void initialize__()\r
1318                 {\r
1319                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action(create__));\r
1320                 }\r
1321                 internal void create__()\r
1322                 {\r
1323                         cnvs = new System.Windows.Controls.Canvas();\r
1324                         trans = new System.Windows.Media.TransformGroup();\r
1325                         transF = new System.Windows.Media.TransformCollection();\r
1326                         rotateF = new System.Windows.Media.RotateTransform();\r
1327                         scaleF = new System.Windows.Media.ScaleTransform();\r
1328                         translateF = new System.Windows.Media.TranslateTransform();\r
1329                         transF.Add(rotateF);\r
1330                         transF.Add(scaleF);\r
1331                         transF.Add(translateF);\r
1332                         trans.Children = transF;\r
1333                         cnvs.RenderTransform = trans;\r
1334                         AsyncBool = true;\r
1335                 }\r
1336                 public Group clone()\r
1337                 {\r
1338                         return (Group)MemberwiseClone();\r
1339                 }\r
1340 \r
1341                 delegate void AppendFunc1(Internal.PrimitiveFigure func);\r
1342                 void append__(Internal.PrimitiveFigure fig)\r
1343                 {\r
1344                         fig.centering(0, 0);\r
1345                         UIElement e = fig.toNative();\r
1346                         cnvs.Children.Add(e);\r
1347                         System.Windows.Controls.Canvas.SetLeft(e, fig.datum.x);\r
1348                         System.Windows.Controls.Canvas.SetTop(e, fig.datum.y);\r
1349                 }\r
1350                 delegate void SimpleProcedure();\r
1351                 void getRotation__() { rotation_ = rotateF.Angle; }\r
1352                 void setRotation__() { rotateF.Angle = rotation_; }\r
1353                 //void getTranslation__() { rotation_ = rotateF.Angle; }\r
1354                 void setTranslation__() { translateF.X = datum.x; translateF.Y = datum.y; }\r
1355                 void setScaling__() { scaleF.ScaleX = scaling_.x; scaleF.ScaleY = scaling_.y; }\r
1356 \r
1357                 public static implicit operator System.Windows.Controls.Canvas(Group d)\r
1358                 {\r
1359                         var tmp = d.cnvs;//new System.Windows.Controls.Canvas();\r
1360                         System.Windows.Controls.Canvas.SetLeft(d.cnvs, d.datum.x);\r
1361                         System.Windows.Controls.Canvas.SetTop(d.cnvs, d.datum.y);\r
1362                         return tmp;\r
1363                 }\r
1364                 public UIElement toNative() { return this; }\r
1365 \r
1366                 public void copyToStack(Templates.StackableDrawable d)\r
1367                 {\r
1368                         var tmp = d.groupStack[d.groupStackN];\r
1369                         tmp.datum = datum;\r
1370                         tmp.cnvs = cnvs;\r
1371                         d.stack[d.stackN] = tmp;\r
1372                         d.groupStackN++;\r
1373                         d.stackN++;\r
1374                 }\r
1375                 public UIElement poolNative(Canvas d)\r
1376                 {\r
1377                         //d.groupPool[d.groupPoolN] = cnvs;\r
1378                         //var tmp = d.groupPool[d.groupPoolN];\r
1379                         var tmp = cnvs;\r
1380                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
1381                         System.Windows.Controls.Canvas.SetTop(tmp, datum.y);\r
1382                         tmp.Visibility = Visibility.Visible;\r
1383                         //d.groupPoolN++;\r
1384                         return tmp;\r
1385                 }\r
1386                 public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d)\r
1387                 {\r
1388                         d.groupPool[d.groupPoolN] = cnvs;\r
1389                         System.Windows.Controls.Canvas.SetLeft(cnvs, datum.x);\r
1390                         System.Windows.Controls.Canvas.SetTop(cnvs, datum.y);\r
1391                         tmp.Visibility = Visibility.Visible;\r
1392                 }\r
1393 \r
1394         }\r
1395         #endregion\r
1396         \r
1397         #endregion\r
1398 \r
1399 \r
1400 }