OSDN Git Service

111
[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                         imagePool = new System.Windows.Controls.Image[100];\r
238                         for (int i = 0; i < 100; i++)\r
239                         {\r
240                                 imagePool[i] = new System.Windows.Controls.Image();\r
241                         }\r
242                          * */\r
243 \r
244                         masterPool = new System.Windows.Controls.Canvas();\r
245                         prevPool = new System.Windows.Controls.Canvas();\r
246                         api_canvas.Children.Add(masterPool);\r
247                         //api_canvas.Children.Remove(Internal.Main.widgetStack);\r
248 \r
249                         AsyncInitBool = true;\r
250                 }\r
251 \r
252                 #endregion\r
253 \r
254                 #region static initializer\r
255                 /*\r
256                 static System.Windows.Shapes.Line api_line;\r
257                 static System.Windows.Shapes.Path api_curve;\r
258                 static System.Windows.Shapes.Rectangle api_rect;\r
259                 static System.Windows.Shapes.Ellipse api_ellipse;\r
260                 static System.Windows.Shapes.Polygon api_polygon;\r
261                 static System.Windows.Shapes.Polyline api_polyline;\r
262                 static System.Windows.Media.Color api_color;\r
263                 static System.Windows.Media.SolidColorBrush api_fill;\r
264                 static System.Windows.Media.SolidColorBrush api_stroke;\r
265                 static System.Windows.Media.TranslateTransform api_translation;\r
266                 static Canvas()\r
267                 {\r
268                         api_line = new System.Windows.Shapes.Line();\r
269                         api_curve    = new System.Windows.Shapes.Path();\r
270                         api_rect     = new System.Windows.Shapes.Rectangle();\r
271                         api_ellipse  = new System.Windows.Shapes.Ellipse();\r
272                         api_polygon  = new System.Windows.Shapes.Polygon();\r
273                         api_polyline = new System.Windows.Shapes.Polyline();\r
274                         api_color       = new System.Windows.Media.Color();\r
275                         api_fill        = new System.Windows.Media.SolidColorBrush();\r
276                         api_stroke      = new System.Windows.Media.SolidColorBrush();\r
277                         api_translation = new System.Windows.Media.TranslateTransform();\r
278                 }\r
279                 */\r
280                 #endregion\r
281 \r
282 \r
283                 public override void clear(Color col)\r
284                 {\r
285                         back_panel.fill = col;\r
286                         stackN = 0;\r
287                         rect(back_panel);\r
288                 }\r
289 \r
290                 int nextIntervalFrame = 1, chacked = 0;\r
291                 public void flip(int n)\r
292                 {\r
293                         flip();\r
294                 }\r
295                 public void flip()\r
296                 {\r
297                         lock (this)\r
298                         {\r
299                                 nextIntervalFrame = 1;\r
300                                 chacked = 1;\r
301                         }\r
302                         //                      pointPoolN = 0;\r
303                         //                      brushPoolN = 0;\r
304                         linePoolN = 0;\r
305                         rectPoolN = 0;\r
306                         lineStackN = 0;\r
307                         rectStackN = 0;\r
308                         polygonStackN = 0;\r
309                         ellipseStackN = 0;\r
310                         lettersStackN = 0;\r
311                         imageStackN = 0;\r
312 \r
313                         pointPoolN = 0;\r
314                         brushPoolN = 0;\r
315                         linePoolN = 0;\r
316                         rectPoolN = 0;\r
317                         ellipsePoolN = 0;\r
318                         polygonPoolN = 0;\r
319                         lettersPoolN = 0;\r
320                         imagePoolN = 0;\r
321 \r
322                         Internal.Main.canvas_flag.WaitOne();\r
323                 }\r
324                 public void executeFlip()\r
325                 {\r
326                         lock (this)\r
327                         {\r
328                                 nextIntervalFrame--;\r
329                         }\r
330                         if (nextIntervalFrame <= 0)\r
331                         {\r
332                                 if (chacked > 0)\r
333                                 {\r
334                                         masterPool.Children.Clear();\r
335                                         if (stackN > 0)\r
336                                         {\r
337                                                 for (int i = 0; i < stackN - 2; i++)\r
338                                                 {\r
339                                                         masterPool.Children.Add(stack[i].poolNative(this));\r
340                                                 }\r
341                                                 stackN = 0;\r
342                                         }\r
343                                         lock (this)\r
344                                         {\r
345                                                 chacked = 0;\r
346                                         }\r
347                                         Psychlops.Internal.Main.canvas_flag.Set();\r
348                                 }\r
349                         }\r
350                         System.Threading.Thread.Sleep(0);\r
351                 }\r
352 \r
353 \r
354 \r
355                 #region Properties\r
356 \r
357                 public double width { get { return width_; } }\r
358                 public double height { get { return height_; } }\r
359                 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
360                 public double getWidth() { return width; }\r
361                 public double getHeight() { return height; }\r
362                 public override Point getCenter() { return center; }\r
363                 public double getHCenter() { return width / 2; }\r
364                 public double getVCenter() { return height / 2; }\r
365                 public double getRefreshRate() { return 60; }\r
366 \r
367                 #endregion\r
368 \r
369 \r
370         }\r
371 \r
372 \r
373 \r
374         #region primitive tokenizer\r
375 \r
376         partial struct Point\r
377         {\r
378                 public static implicit operator System.Windows.Point(Point d)\r
379                 {\r
380                         return new System.Windows.Point(d.x, d.y);\r
381                 }\r
382         }\r
383 \r
384         partial struct Color\r
385         {\r
386                 public static implicit operator System.Windows.Media.Color(Color d)\r
387                 {\r
388                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
389                 }\r
390                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
391                 {\r
392                         return new SolidColorBrush { Color = d };\r
393                 }\r
394         }\r
395 \r
396         partial struct Stroke\r
397         {\r
398                 public void apply(System.Windows.Shapes.Shape target)\r
399                 {\r
400                         target.Stroke = this;\r
401                         //target.StrokeDashArray\r
402                         target.StrokeThickness = thick;\r
403                 }\r
404                 public static implicit operator SolidColorBrush(Stroke d)\r
405                 {\r
406                         return new SolidColorBrush { Color = d.color };\r
407                 }\r
408         }\r
409 \r
410         partial class Line\r
411         {\r
412                 public Line dup()\r
413                 {\r
414                         return (Line)MemberwiseClone();\r
415                 }\r
416                 public Line clone()\r
417                 {\r
418                         return (Line)MemberwiseClone();\r
419                 }\r
420                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
421                 {\r
422                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
423                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
424                         else d.stroke.apply(tmp);\r
425                         return tmp;\r
426                 }\r
427                 public UIElement toNative() { return this; }\r
428 \r
429                 public void copyToStack(Templates.StackableDrawable d)\r
430                 {\r
431                         var tmp = d.lineStack[d.lineStackN];\r
432                         tmp.begin.x = begin.x;\r
433                         tmp.begin.y = begin.y;\r
434                         tmp.end.x = end.x;\r
435                         tmp.end.y = end.y;\r
436                         tmp.fill = fill;\r
437                         tmp.stroke = stroke;\r
438                         d.stack[d.stackN] = tmp;\r
439                         d.lineStackN++;\r
440                         d.stackN++;\r
441                 }\r
442                 public UIElement poolNative(Canvas d)\r
443                 {\r
444                         var tmp = d.linePool[d.linePoolN];\r
445                         tmp.X1 = begin.x;\r
446                         tmp.Y1 = begin.y;\r
447                         tmp.X2 = end.x;\r
448                         tmp.Y2 = end.y;\r
449                         if (stroke.thick == 0.0) tmp.Stroke = fill;\r
450                         else stroke.apply(tmp);\r
451                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
452                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
453                         tmp.Visibility = Visibility.Visible;\r
454                         d.linePoolN++;\r
455                         return tmp;\r
456                 }\r
457         }\r
458 \r
459         partial class Rectangle\r
460         {\r
461                 public Rectangle dup()\r
462                 {\r
463                         return (Rectangle)MemberwiseClone();\r
464                 }\r
465                 public Rectangle clone()\r
466                 {\r
467                         return (Rectangle)MemberwiseClone();\r
468                 }\r
469                 public static implicit operator System.Windows.Rect(Rectangle d)\r
470                 {\r
471                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
472                 }\r
473                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
474                 {\r
475                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
476                         d.stroke.apply(tmp);\r
477                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
478                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
479                         return tmp;\r
480                 }\r
481 \r
482                 public UIElement toNative() { return this; }\r
483                 public void copyToStack(Templates.StackableDrawable d)\r
484                 {\r
485                         var tmp = d.rectStack[d.rectStackN];\r
486                         tmp.v1 = v1;\r
487                         tmp.v2 = v2;\r
488                         tmp.fill = fill;\r
489                         d.stack[d.stackN] = tmp;\r
490                         d.rectStackN++;\r
491                         d.stackN++;\r
492                 }\r
493                 public UIElement poolNative(Canvas d)\r
494                 {\r
495                         var tmp = d.rectPool[d.rectPoolN];\r
496                         tmp.Width = width;\r
497                         tmp.Height = height;\r
498                         tmp.Fill = fill;\r
499                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
500                         System.Windows.Controls.Canvas.SetTop(tmp, top);\r
501                         tmp.Visibility = Visibility.Visible;\r
502                         d.rectPoolN++;\r
503                         return tmp;\r
504                 }\r
505         }\r
506 \r
507         partial class Ellipse\r
508         {\r
509                 public Ellipse dup()\r
510                 {\r
511                         return (Ellipse)MemberwiseClone();\r
512                 }\r
513                 public Ellipse clone()\r
514                 {\r
515                         return (Ellipse)MemberwiseClone();\r
516                 }\r
517                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
518                 {\r
519                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
520                         d.stroke.apply(tmp);\r
521                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
522                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
523                         return tmp;\r
524                 }\r
525 \r
526                 public UIElement toNative() { return this; }\r
527 \r
528                 public void copyToStack(Templates.StackableDrawable d)\r
529                 {\r
530                         var tmp = d.ellipseStack[d.ellipseStackN];\r
531                         tmp.datum = datum;\r
532                         tmp.xdiameter = xdiameter;\r
533                         tmp.ydiameter = ydiameter;\r
534                         tmp.fill = fill;\r
535                         d.stack[d.stackN] = tmp;\r
536                         d.ellipseStackN++;\r
537                         d.stackN++;\r
538                 }\r
539                 public UIElement poolNative(Canvas d)\r
540                 {\r
541                         var tmp = d.ellipsePool[d.ellipsePoolN];\r
542                         tmp.Width = width;\r
543                         tmp.Height = height;\r
544                         tmp.Fill = fill;\r
545                         System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
546                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);\r
547                         tmp.Visibility = Visibility.Visible;\r
548                         d.ellipsePoolN++;\r
549                         return tmp;\r
550                 }\r
551         }\r
552 \r
553         partial class Polygon\r
554         {\r
555                 public Polygon dup()\r
556                 {\r
557                         return (Polygon)MemberwiseClone();\r
558                 }\r
559                 public Polygon clone()\r
560                 {\r
561                         return (Polygon)MemberwiseClone();\r
562                 }\r
563                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
564                 {\r
565                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
566                         d.stroke.apply(tmp);\r
567                         foreach (Point p in d.vertices)\r
568                         {\r
569                                 tmp.Points.Add(p);\r
570                         }\r
571                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
572                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
573                         return tmp;\r
574                 }\r
575                 public UIElement toNative() { return this; }\r
576 \r
577                 public void copyToStack(Templates.StackableDrawable d)\r
578                 {\r
579                         var tmp = d.polygonStack[d.polygonStackN];\r
580                         tmp.datum = datum;\r
581                         tmp.vertices.Clear();\r
582                         foreach (var v in vertices)\r
583                         {\r
584                                 tmp.vertices.Add(v);\r
585                         }\r
586                         tmp.fill = fill;\r
587                         d.stack[d.stackN] = tmp;\r
588                         d.polygonStackN++;\r
589                         d.stackN++;\r
590                 }\r
591                 public UIElement poolNative(Canvas d)\r
592                 {\r
593                         var tmp = d.polygonPool[d.polygonPoolN];\r
594                         tmp.Fill = fill;\r
595                         tmp.Points.Clear();\r
596                         foreach (var v in vertices)\r
597                         {\r
598                                 tmp.Points.Add(v);\r
599                         }\r
600                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
601                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
602                         tmp.Visibility = Visibility.Visible;\r
603                         d.polygonPoolN++;\r
604                         return tmp;\r
605                 }\r
606 \r
607         }\r
608         \r
609         partial class Letters\r
610         {\r
611                 #region static initializer\r
612                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
613                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
614                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
615                 static Letters()\r
616                 {\r
617                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
618                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
619                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
620                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
621                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
622                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
623                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
624                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
625                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
626                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
627                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
628                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
629                 }\r
630                 #endregion\r
631                 public Letters dup()\r
632                 {\r
633                         return (Letters)MemberwiseClone();\r
634                 }\r
635                 public Letters clone()\r
636                 {\r
637                         return (Letters)MemberwiseClone();\r
638                 }\r
639                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
640                 {\r
641                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
642                         var tmp = new System.Windows.Controls.TextBlock {\r
643                                 Text = d.str, Width = 500, Height = 500,\r
644                                 FontSize = d.font.size,\r
645                                 //tmp.FontFamily = ,\r
646                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
647                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
648                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
649                                 Foreground = d.fill\r
650                         };\r
651                         double left = 0;\r
652                         switch (d.align)\r
653                         {\r
654                                 case Letters.HorizontalAlign.left: break;\r
655                                 case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
656                                 case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
657                         }\r
658                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
659                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
660                         return tmp;\r
661                 }\r
662                 public UIElement toNative() { return this; }\r
663 \r
664                 public void copyToStack(Templates.StackableDrawable d)\r
665                 {\r
666                         var tmp = d.lettersStack[d.lettersStackN];\r
667                         tmp.str = str;\r
668                         tmp.datum = datum;\r
669                         tmp.fill = fill;\r
670                         d.stack[d.stackN] = tmp;\r
671                         d.lettersStackN++;\r
672                         d.stackN++;\r
673                 }\r
674                 public UIElement poolNative(Canvas d)\r
675                 {\r
676                         var tmp = d.lettersPool[d.lettersPoolN];\r
677                         tmp.Text = str;\r
678                         tmp.Width = 500;\r
679                         tmp.Height = 500;\r
680                         tmp.FontSize = font.size;\r
681                         //tmp.FontFamily = ,\r
682                         tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
683                         tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
684                         tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
685                         tmp.Foreground = fill;\r
686                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
687                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
688                         tmp.Visibility = Visibility.Visible;\r
689                         d.lettersPoolN++;\r
690                         return tmp;\r
691                 }\r
692         }\r
693 \r
694         partial class Image\r
695         {\r
696                 internal void initialize__(int wid, int hei)\r
697                 {\r
698                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
699                         //buffer = new WriteableBitmap(wid, hei);\r
700                 }\r
701                 internal void create__(int wid, int hei)\r
702                 {\r
703                         buffer = new WriteableBitmap(wid, hei);\r
704                         AsyncBool = true;\r
705                 }\r
706                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
707                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
708                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
709                 {\r
710                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
711                         //buffer.ForEach(func);\r
712                 }\r
713                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
714                 {\r
715                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
716                         //buffer.ForEach(func);\r
717                 }\r
718                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
719                 {\r
720                         buffer.ForEach(func);\r
721                 }\r
722                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
723                 {\r
724                         buffer.ForEach(func);\r
725                 }\r
726 \r
727                 public Image clone()\r
728                 {\r
729                         return (Image)MemberwiseClone();\r
730                 }\r
731                 public static implicit operator System.Windows.Controls.Image(Image d)\r
732                 {\r
733                         var tmp = new System.Windows.Controls.Image();\r
734                         tmp.Source = d.buffer;\r
735                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
736                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
737                         return tmp;\r
738                 }\r
739                 public UIElement toNative() { return this; }\r
740 \r
741                 public void copyToStack(Templates.StackableDrawable d)\r
742                 {\r
743                         var tmp = d.imageStack[d.imageStackN];\r
744                         tmp.datum = datum;\r
745                         tmp.buffer = buffer;\r
746                         tmp.self_rect = self_rect;\r
747                         d.stack[d.stackN] = tmp;\r
748                         d.imageStackN++;\r
749                         d.stackN++;\r
750                 }\r
751                 public UIElement poolNative(Canvas d)\r
752                 {\r
753                         var tmp = d.imagePool[d.imagePoolN];\r
754                         tmp.Source = buffer;\r
755                         System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
756                         System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
757                         tmp.Visibility = Visibility.Visible;\r
758                         d.rectPoolN++;\r
759                         throw new Exception("Image.poolNative");\r
760                         return this;\r
761                 }\r
762 \r
763         }\r
764 \r
765         #endregion\r
766 \r
767 \r
768 }