OSDN Git Service

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