OSDN Git Service

6216e532d9dce505438149b1d88a86ada7a66d10
[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 \r
13 \r
14 namespace Psychlops\r
15 {\r
16 \r
17         namespace Templates\r
18         {\r
19 \r
20                 public class StackableDrawable : Drawable\r
21                 {\r
22 //                      protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
23                         internal Internal.PrimitiveFigure[] stack;\r
24                         internal int stackN;\r
25                         internal Line[] lineStack;\r
26                         internal int lineStackN;\r
27                         internal Rectangle[] rectStack;\r
28                         internal int rectStackN;\r
29                         internal Ellipse[] ellipseStack;\r
30                         internal int ellipseStackN;\r
31                         internal Polygon[] polygonStack;\r
32                         internal int polygonStackN;\r
33                         internal Letters[] lettersStack;\r
34                         internal int lettersStackN;\r
35                         internal Image[] imageStack;\r
36                         internal int imageStackN;\r
37 \r
38 \r
39                         public StackableDrawable()\r
40                         {\r
41 //                              stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
42                                 stack = new Internal.PrimitiveFigure[40000];\r
43                                 lineStack = new Line[10000];\r
44                                 rectStack = new Rectangle[10000];\r
45                                 ellipseStack = new Ellipse[10000];\r
46                                 polygonStack = new Polygon[1000];\r
47                                 lettersStack = new Letters[1000];\r
48                                 imageStack = new Image[1000];\r
49                                 for (int i = 0; i < 10000; i++)\r
50                                 {\r
51                                         lineStack[i] = new Line(0,0,0,0);\r
52                                         rectStack[i] = new Rectangle();\r
53                                         ellipseStack[i] = new Ellipse();\r
54                                 }\r
55                                 for (int i = 0; i < 1000; i++)\r
56                                 {\r
57                                         polygonStack[i] = new Polygon();\r
58                                         lettersStack[i] = new Letters();\r
59                                         imageStack[i] = new Image(1,1);\r
60                                 }\r
61                         }\r
62 \r
63                         public void clear()\r
64                         {\r
65                                 clear(Color.black);\r
66                         }\r
67                         public virtual void clear(Color col)\r
68                         {\r
69                                 //rect(back_panel, col);\r
70                         }\r
71 \r
72                         public virtual void pix(int x, int y, Color col)\r
73                         {\r
74                         }\r
75 \r
76                         public virtual void line(Line drawee)\r
77                         {\r
78                                 //                              stack.Enqueue(drawee.clone());\r
79                                 drawee.copyToStack(this);\r
80                         }\r
81 \r
82                         public virtual void rect(Rectangle drawee)\r
83                         {\r
84                                 //stack.Enqueue(drawee.clone());\r
85 //                              stack.Enqueue(drawee.copyToStack(this));\r
86                                 drawee.copyToStack(this);\r
87                         }\r
88 \r
89                         public virtual void ellipse(Ellipse drawee)\r
90                         {\r
91                                 //                              stack.Enqueue(drawee.clone());\r
92                                 drawee.copyToStack(this);\r
93                         }\r
94 \r
95                         public virtual void polygon(Polygon drawee)\r
96                         {\r
97                                 //                              stack.Enqueue(drawee.clone());\r
98                                 drawee.copyToStack(this);\r
99                         }\r
100 \r
101                         public virtual void letters(Letters drawee)\r
102                         {\r
103                                 //                              stack.Enqueue(drawee.clone());\r
104                                 drawee.copyToStack(this);\r
105                         }\r
106 \r
107                         public virtual void image(Image drawee)\r
108                         {\r
109                                 //                              stack.Enqueue(drawee.clone());\r
110                                 drawee.copyToStack(this);\r
111                         }\r
112 \r
113                         public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
114                         public virtual void msg(string dstr, double x, double y, Color col)\r
115                         {\r
116                                 var let = new Letters(dstr);\r
117                                 let.locate(x, y);\r
118                                 let.fill = col;\r
119                                 this.letters(let);\r
120                         }\r
121                         public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
122                         public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
123 \r
124                         public virtual Point getCenter() { return new Point(0, 0, 0); }\r
125                 }\r
126 \r
127         }\r
128 \r
129         public class Canvas : Templates.StackableDrawable\r
130         {\r
131 \r
132                 internal System.Windows.Controls.Canvas masterPool, prevPool;\r
133                 internal System.Windows.Point[] pointPool;\r
134                 internal int pointPoolN;\r
135                 internal SolidColorBrush[] brushPool;\r
136                 internal int brushPoolN;\r
137                 internal System.Windows.Shapes.Line[] linePool;\r
138                 internal int linePoolN;\r
139                 internal System.Windows.Shapes.Rectangle[] rectPool;\r
140                 internal int rectPoolN;\r
141                 internal System.Windows.Shapes.Ellipse[] ellipsePool;\r
142                 internal int ellipsePoolN;\r
143                 internal System.Windows.Shapes.Polygon[] polygonPool;\r
144                 internal int polygonPoolN;\r
145                 internal System.Windows.Controls.TextBlock[] lettersPool;\r
146                 internal int lettersPoolN;\r
147                 internal System.Windows.Controls.Image[] imagePool;\r
148                 internal int imagePoolN;\r
149 \r
150                 #region initializer\r
151 \r
152                 internal delegate void TwoIntProcedure(int x, int y);\r
153                 internal delegate void SimpleProcedure();\r
154                 SimpleProcedure flipexec;\r
155 \r
156                 //public static System.Windows.Controls.Image default_buffer_frame;\r
157                 public static System.Windows.Controls.UserControl default_panel;\r
158                 public static System.Windows.Controls.Canvas default_api_canvas;\r
159                 public static WriteableBitmap default_buffer;\r
160                 //WriteableBitmap buffer;\r
161                 //System.Windows.Controls.Image instance;\r
162                 System.Windows.Controls.Canvas api_canvas;\r
163                 System.Windows.Controls.UserControl panel;\r
164                 Rectangle back_panel;\r
165                 double width_, height_;\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                 protected void initialize(int wid, int hei)\r
182                 {\r
183                         AsyncInitBool = false;\r
184                         width_ = wid;\r
185                         height_ = hei;\r
186                         api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
187                         while(!AsyncInitBool)\r
188                         {\r
189                         }\r
190                         Mouse._prime = api_canvas;\r
191                         Main.drawable = this;\r
192                         Main.canvas = this;\r
193 \r
194                         back_panel = new Rectangle(wid, hei);\r
195 \r
196                         flipexec = new SimpleProcedure(executeFlip);\r
197                 }\r
198                 protected void initialize__(int wid, int hei)\r
199                 {\r
200                         api_canvas.Width = wid;\r
201                         api_canvas.Height = hei;\r
202                         api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
203                         api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
204                         api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
205                         api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
206                         panel.KeyDown += Keyboard.Canvas_KeyDown;\r
207                         panel.KeyUp += Keyboard.Canvas_KeyUp;\r
208 \r
209                         HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
210                         //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
211                         htmlHost.SetStyleAttribute("width", (wid).ToString()+"px");\r
212                         htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
213                         htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
214 \r
215 \r
216                         pointPool = new System.Windows.Point[10000];\r
217                         brushPool = new SolidColorBrush[10000];\r
218                         linePool = new System.Windows.Shapes.Line[10000];\r
219                         rectPool = new System.Windows.Shapes.Rectangle[10000];\r
220                         ellipsePool = new System.Windows.Shapes.Ellipse[10000];\r
221                         for (int i = 0; i < 10000; i++)\r
222                         {\r
223                                 pointPool[i] = new System.Windows.Point();\r
224                                 brushPool[i] = new SolidColorBrush();\r
225                                 linePool[i] = new System.Windows.Shapes.Line();\r
226                                 rectPool[i] = new System.Windows.Shapes.Rectangle();\r
227                                 ellipsePool[i] = new System.Windows.Shapes.Ellipse();\r
228                         }\r
229                         polygonPool = new System.Windows.Shapes.Polygon[1000];\r
230                         lettersPool = new System.Windows.Controls.TextBlock[1000];\r
231                         for (int i = 0; i < 1000; i++)\r
232                         {\r
233                                 polygonPool[i] = new System.Windows.Shapes.Polygon();\r
234                                 lettersPool[i] = new System.Windows.Controls.TextBlock();\r
235                         }\r
236 \r
237                         masterPool = new System.Windows.Controls.Canvas();\r
238                         prevPool = new System.Windows.Controls.Canvas();\r
239                         api_canvas.Children.Add(masterPool);\r
240                         //api_canvas.Children.Remove(Internal.Main.widgetStack);\r
241 \r
242                         AsyncInitBool = true;\r
243                 }\r
244 \r
245                 #endregion\r
246 \r
247                 #region static initializer\r
248                 /*\r
249                 static System.Windows.Shapes.Line api_line;\r
250                 static System.Windows.Shapes.Path api_curve;\r
251                 static System.Windows.Shapes.Rectangle api_rect;\r
252                 static System.Windows.Shapes.Ellipse api_ellipse;\r
253                 static System.Windows.Shapes.Polygon api_polygon;\r
254                 static System.Windows.Shapes.Polyline api_polyline;\r
255                 static System.Windows.Media.Color api_color;\r
256                 static System.Windows.Media.SolidColorBrush api_fill;\r
257                 static System.Windows.Media.SolidColorBrush api_stroke;\r
258                 static System.Windows.Media.TranslateTransform api_translation;\r
259                 static Canvas()\r
260                 {\r
261                         api_line = new System.Windows.Shapes.Line();\r
262                         api_curve    = new System.Windows.Shapes.Path();\r
263                         api_rect     = new System.Windows.Shapes.Rectangle();\r
264                         api_ellipse  = new System.Windows.Shapes.Ellipse();\r
265                         api_polygon  = new System.Windows.Shapes.Polygon();\r
266                         api_polyline = new System.Windows.Shapes.Polyline();\r
267                         api_color       = new System.Windows.Media.Color();\r
268                         api_fill        = new System.Windows.Media.SolidColorBrush();\r
269                         api_stroke      = new System.Windows.Media.SolidColorBrush();\r
270                         api_translation = new System.Windows.Media.TranslateTransform();\r
271                 }\r
272                 */\r
273                 #endregion\r
274 \r
275 \r
276                 public override void clear(Color col)\r
277                 {\r
278                         back_panel.fill = col;\r
279                         stackN = 0;\r
280                         rect(back_panel);\r
281                 }\r
282 \r
283                 int nextIntervalFrame = 1, chacked = 0;\r
284                 public void flip(int n)\r
285                 {\r
286                         flip();\r
287                 }\r
288                 public void flip()\r
289                 {\r
290                         lock (this)\r
291                         {\r
292                                 nextIntervalFrame = 1;\r
293                                 chacked = 1;\r
294                         }\r
295                         //                      pointPoolN = 0;\r
296                         //                      brushPoolN = 0;\r
297                         linePoolN = 0;\r
298                         rectPoolN = 0;\r
299                         lineStackN = 0;\r
300                         rectStackN = 0;\r
301                         polygonStackN = 0;\r
302                         ellipseStackN = 0;\r
303                         lettersStackN = 0;\r
304                         imageStackN = 0;\r
305 \r
306                         pointPoolN = 0;\r
307                         brushPoolN = 0;\r
308                         linePoolN = 0;\r
309                         rectPoolN = 0;\r
310                         ellipsePoolN = 0;\r
311                         polygonPoolN = 0;\r
312                         lettersPoolN = 0;\r
313                         imagePoolN = 0;\r
314 \r
315                         Internal.Main.canvas_flag.WaitOne();\r
316                 }\r
317                 public void executeFlip()\r
318                 {\r
319                         lock (this)\r
320                         {\r
321                                 nextIntervalFrame--;\r
322                         }\r
323                         if (nextIntervalFrame <= 0)\r
324                         {\r
325                                 if (chacked > 0)\r
326                                 {\r
327                                         masterPool.Children.Clear();\r
328                                         if (stackN > 0)\r
329                                         {\r
330                                                 for (int i = 0; i < stackN - 2; i++)\r
331                                                 {\r
332                                                         masterPool.Children.Add(stack[i].poolNative(this));\r
333                                                 }\r
334                                                 stackN = 0;\r
335                                         }\r
336                                         lock (this)\r
337                                         {\r
338                                                 chacked = 0;\r
339                                         }\r
340                                         Psychlops.Internal.Main.canvas_flag.Set();\r
341                                 }\r
342                         }\r
343                         System.Threading.Thread.Sleep(0);\r
344                 }\r
345 \r
346 \r
347 \r
348                 #region Properties\r
349 \r
350                 public double width { get { return width_; } }\r
351                 public double height { get { return height_; } }\r
352                 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
353                 public double getWidth() { return width; }\r
354                 public double getHeight() { return height; }\r
355                 public override Point getCenter() { return center; }\r
356                 public double getHCenter() { return width / 2; }\r
357                 public double getVCenter() { return height / 2; }\r
358                 public double getRefreshRate() { return 60; }\r
359 \r
360                 #endregion\r
361 \r
362 \r
363         }\r
364 \r
365 \r
366 \r
367         #region primitive tokenizer\r
368 \r
369         partial struct Point\r
370         {\r
371                 public static implicit operator System.Windows.Point(Point d)\r
372                 {\r
373                         return new System.Windows.Point(d.x, d.y);\r
374                 }\r
375         }\r
376 \r
377         partial struct Color\r
378         {\r
379                 public static implicit operator System.Windows.Media.Color(Color d)\r
380                 {\r
381                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
382                 }\r
383                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
384                 {\r
385                         return new SolidColorBrush { Color = d };\r
386                 }\r
387         }\r
388 \r
389         partial struct Stroke\r
390         {\r
391                 public void apply(System.Windows.Shapes.Shape target)\r
392                 {\r
393                         target.Stroke = this;\r
394                         //target.StrokeDashArray\r
395                         target.StrokeThickness = thick;\r
396                 }\r
397                 public static implicit operator SolidColorBrush(Stroke d)\r
398                 {\r
399                         return new SolidColorBrush { Color = d.color };\r
400                 }\r
401         }\r
402 \r
403         partial class Line\r
404         {\r
405                 public Line dup()\r
406                 {\r
407                         return (Line)MemberwiseClone();\r
408                 }\r
409                 public Line clone()\r
410                 {\r
411                         return (Line)MemberwiseClone();\r
412                 }\r
413                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
414                 {\r
415                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
416                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
417                         else d.stroke.apply(tmp);\r
418                         return tmp;\r
419                 }\r
420                 public UIElement toNative() { return this; }\r
421 \r
422                 public void copyToStack(Templates.StackableDrawable d)\r
423                 {\r
424                         var tmp = d.lineStack[d.lineStackN];\r
425                         tmp.begin.x = begin.x;\r
426                         tmp.begin.y = begin.y;\r
427                         tmp.end.x = end.x;\r
428                         tmp.end.y = end.y;\r
429                         tmp.fill = fill;\r
430                         tmp.stroke = stroke;\r
431                         d.stack[d.stackN] = tmp;\r
432                         d.lineStackN++;\r
433                         d.stackN++;\r
434                 }\r
435                 public UIElement poolNative(Canvas d)\r
436                 {\r
437                         var tmp = d.linePool[d.linePoolN];\r
438                         tmp.X1 = begin.x;\r
439                         tmp.Y1 = begin.y;\r
440                         tmp.X2 = end.x;\r
441                         tmp.Y2 = end.y;\r
442                         if (stroke.thick == 0.0) tmp.Stroke = fill;\r
443                         else stroke.apply(tmp);\r
444                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
445                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
446                         tmp.Visibility = Visibility.Visible;\r
447                         d.linePoolN++;\r
448                         return tmp;\r
449                 }\r
450         }\r
451 \r
452         partial class Rectangle\r
453         {\r
454                 public Rectangle dup()\r
455                 {\r
456                         return (Rectangle)MemberwiseClone();\r
457                 }\r
458                 public Rectangle clone()\r
459                 {\r
460                         return (Rectangle)MemberwiseClone();\r
461                 }\r
462                 public static implicit operator System.Windows.Rect(Rectangle d)\r
463                 {\r
464                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
465                 }\r
466                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
467                 {\r
468                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
469                         d.stroke.apply(tmp);\r
470                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
471                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
472                         return tmp;\r
473                 }\r
474 \r
475                 public UIElement toNative() { return this; }\r
476                 public void copyToStack(Templates.StackableDrawable d)\r
477                 {\r
478                         var tmp = d.rectStack[d.rectStackN];\r
479                         tmp.v1 = v1;\r
480                         tmp.v2 = v2;\r
481                         tmp.fill = fill;\r
482                         d.stack[d.stackN] = tmp;\r
483                         d.rectStackN++;\r
484                         d.stackN++;\r
485                 }\r
486                 public UIElement poolNative(Canvas d)\r
487                 {\r
488                         var tmp = d.rectPool[d.rectPoolN];\r
489                         tmp.Width = width;\r
490                         tmp.Height = height;\r
491                         tmp.Fill = fill;\r
492                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
493                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
494                         tmp.Visibility = Visibility.Visible;\r
495                         d.rectPoolN++;\r
496                         return tmp;\r
497                 }\r
498         }\r
499 \r
500         partial class Ellipse\r
501         {\r
502                 public Ellipse dup()\r
503                 {\r
504                         return (Ellipse)MemberwiseClone();\r
505                 }\r
506                 public Ellipse clone()\r
507                 {\r
508                         return (Ellipse)MemberwiseClone();\r
509                 }\r
510                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
511                 {\r
512                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
513                         d.stroke.apply(tmp);\r
514                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
515                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
516                         return tmp;\r
517                 }\r
518 \r
519                 public UIElement toNative() { return this; }\r
520 \r
521                 public void copyToStack(Templates.StackableDrawable d)\r
522                 {\r
523                         var tmp = d.ellipseStack[d.ellipseStackN];\r
524                         tmp.datum = datum;\r
525                         tmp.xdiameter = xdiameter;\r
526                         tmp.ydiameter = ydiameter;\r
527                         tmp.fill = fill;\r
528                         d.stack[d.stackN] = tmp;\r
529                         d.ellipseStackN++;\r
530                         d.stackN++;\r
531                 }\r
532                 public UIElement poolNative(Canvas d)\r
533                 {\r
534                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
535                         tmp.Width = width;\r
536                         tmp.Height = height;\r
537                         tmp.Fill = fill;\r
538                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
539                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);\r
540                         tmp.Visibility = Visibility.Visible;\r
541                         d.ellipsePoolN++;\r
542                         return tmp;\r
543                 }\r
544         }\r
545 \r
546         partial class Polygon\r
547         {\r
548                 public Polygon dup()\r
549                 {\r
550                         return (Polygon)MemberwiseClone();\r
551                 }\r
552                 public Polygon clone()\r
553                 {\r
554                         return (Polygon)MemberwiseClone();\r
555                 }\r
556                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
557                 {\r
558                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
559                         d.stroke.apply(tmp);\r
560                         foreach (Point p in d.vertices)\r
561                         {\r
562                                 tmp.Points.Add(p);\r
563                         }\r
564                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
565                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
566                         return tmp;\r
567                 }\r
568                 public UIElement toNative() { return this; }\r
569 \r
570                 public void copyToStack(Templates.StackableDrawable d)\r
571                 {\r
572                         var tmp = d.polygonStack[d.polygonStackN];\r
573                         tmp.datum = datum;\r
574                         tmp.vertices.Clear();\r
575                         foreach (var v in vertices)\r
576                         {\r
577                                 tmp.vertices.Add(v);\r
578                         }\r
579                         tmp.fill = fill;\r
580                         d.stack[d.stackN] = tmp;\r
581                         d.polygonStackN++;\r
582                         d.stackN++;\r
583                 }\r
584                 public UIElement poolNative(Canvas d)\r
585                 {\r
586                         var tmp = d.polygonPool[d.polygonPoolN];\r
587                         tmp.Fill = fill;\r
588                         tmp.Points.Clear();\r
589                         foreach (var v in vertices)\r
590                         {\r
591                                 tmp.Points.Add(v);\r
592                         }\r
593                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
594                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
595                         tmp.Visibility = Visibility.Visible;\r
596                         d.polygonPoolN++;\r
597                         return tmp;\r
598                 }\r
599 \r
600         }\r
601         \r
602         partial class Letters\r
603         {\r
604                 #region static initializer\r
605                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
606                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
607                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
608                 static Letters()\r
609                 {\r
610                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
611                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
612                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
613                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
614                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
615                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
616                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
617                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
618                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
619                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
620                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
621                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
622                 }\r
623                 #endregion\r
624                 public Letters dup()\r
625                 {\r
626                         return (Letters)MemberwiseClone();\r
627                 }\r
628                 public Letters clone()\r
629                 {\r
630                         return (Letters)MemberwiseClone();\r
631                 }\r
632                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
633                 {\r
634                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
635                         var tmp = new System.Windows.Controls.TextBlock {\r
636                                 Text = d.str, Width = 500, Height = 500,\r
637                                 FontSize = d.font.size,\r
638                                 //tmp.FontFamily = ,\r
639                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
640                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
641                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
642                                 Foreground = d.fill\r
643                         };\r
644                         double left = 0;\r
645                         switch (d.align)\r
646                         {\r
647                                 case Letters.HorizontalAlign.left: break;\r
648                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
649                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
650                         }\r
651                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
652                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
653                         return tmp;\r
654                 }\r
655                 public UIElement toNative() { return this; }\r
656 \r
657                 public void copyToStack(Templates.StackableDrawable d)\r
658                 {\r
659                         var tmp = d.lettersStack[d.lettersStackN];\r
660                         tmp.str = str;\r
661                         tmp.datum = datum;\r
662                         tmp.fill = fill;\r
663                         d.stack[d.stackN] = tmp;\r
664                         d.lettersStackN++;\r
665                         d.stackN++;\r
666                 }\r
667                 public UIElement poolNative(Canvas d)\r
668                 {\r
669                         var tmp = d.lettersPool[d.lettersPoolN];\r
670                         tmp.Text = str;\r
671                         tmp.Width = 500;\r
672                         tmp.Height = 500;\r
673                         tmp.FontSize = font.size;\r
674                         //tmp.FontFamily = ,\r
675                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
676                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
677                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
678                         tmp.Foreground = fill;\r
679                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
680                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
681                         tmp.Visibility = Visibility.Visible;\r
682                         d.lettersPoolN++;\r
683                         return tmp;\r
684                 }\r
685         }\r
686 \r
687         partial class Image\r
688         {\r
689                 internal void initialize__(int wid, int hei)\r
690                 {\r
691                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
692                         //buffer = new WriteableBitmap(wid, hei);\r
693                 }\r
694                 internal void create__(int wid, int hei)\r
695                 {\r
696                         buffer = new WriteableBitmap(wid, hei);\r
697                         AsyncBool = true;\r
698                 }\r
699                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
700                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
701                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
702                 {\r
703                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
704                         //buffer.ForEach(func);\r
705                 }\r
706                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
707                 {\r
708                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
709                         //buffer.ForEach(func);\r
710                 }\r
711                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
712                 {\r
713                         buffer.ForEach(func);\r
714                 }\r
715                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
716                 {\r
717                         buffer.ForEach(func);\r
718                 }\r
719 \r
720                 public Image clone()\r
721                 {\r
722                         return (Image)MemberwiseClone();\r
723                 }\r
724                 public static implicit operator System.Windows.Controls.Image(Image d)\r
725                 {\r
726                         var tmp = new System.Windows.Controls.Image();\r
727                         tmp.Source = d.buffer;\r
728                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
729                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
730                         return tmp;\r
731                 }\r
732                 public UIElement toNative() { return this; }\r
733 \r
734                 public void copyToStack(Templates.StackableDrawable d)\r
735                 {\r
736                         var tmp = d.imageStack[d.imageStackN];\r
737                         tmp.datum = datum;\r
738                         tmp.buffer = buffer;\r
739                         tmp.self_rect = self_rect;\r
740                         d.stack[d.stackN] = tmp;\r
741                         d.imageStackN++;\r
742                         d.stackN++;\r
743                 }\r
744                 public UIElement poolNative(Canvas d)\r
745                 {\r
746                         var tmp = d.imagePool[d.imagePoolN];\r
747                         tmp.Source = buffer;\r
748                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
749                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
750                         tmp.Visibility = Visibility.Visible;\r
751                         d.rectPoolN++;\r
752                         return this;\r
753                 }\r
754 \r
755         }\r
756 \r
757         #endregion\r
758 \r
759 \r
760 }