OSDN Git Service

clock interval
[psychlops/silverlight.git] / dev3 / 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 \r
24                         public StackableDrawable()\r
25                         {\r
26                                 stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
27                         }\r
28 \r
29                         public void clear()\r
30                         {\r
31                                 clear(Color.black);\r
32                         }\r
33                         public virtual void clear(Color col)\r
34                         {\r
35                                 //rect(back_panel, col);\r
36                         }\r
37 \r
38                         public virtual void pix(int x, int y, Color col)\r
39                         {\r
40                         }\r
41 \r
42                         public virtual void line(Line drawee)\r
43                         {\r
44                                 stack.Enqueue(drawee.clone());\r
45                         }\r
46 \r
47                         public virtual void rect(Rectangle drawee)\r
48                         {\r
49                                 stack.Enqueue(drawee.clone());\r
50                         }\r
51 \r
52                         public virtual void ellipse(Ellipse drawee)\r
53                         {\r
54                                 stack.Enqueue(drawee.clone());\r
55                         }\r
56 \r
57                         public virtual void polygon(Polygon drawee)\r
58                         {\r
59                                 stack.Enqueue(drawee.clone());\r
60                         }\r
61 \r
62                         public virtual void letters(Letters drawee)\r
63                         {\r
64                                 stack.Enqueue(drawee.clone());\r
65                         }\r
66 \r
67                         public virtual void image(Image drawee)\r
68                         {\r
69                                 stack.Enqueue(drawee.clone());\r
70                         }\r
71 \r
72                         public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
73                         public virtual void msg(string str, double x, double y, Color col)\r
74                         {\r
75                                 var let = new Letters(str);\r
76                                 let.locate(x, y);\r
77                                 let.fill = col;\r
78                                 this.letters(let);\r
79                         }\r
80                         public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
81                         public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
82 \r
83                         public virtual Point getCenter() { return new Point(0, 0, 0); }\r
84                 }\r
85 \r
86         }\r
87 \r
88         public class Canvas : Templates.StackableDrawable\r
89         {\r
90 \r
91                 #region initializer\r
92 \r
93                 internal delegate void TwoIntProcedure(int x, int y);\r
94                 internal delegate void SimpleProcedure();\r
95                 SimpleProcedure flipexec;\r
96 \r
97                 //public static System.Windows.Controls.Image default_buffer_frame;\r
98                 public static System.Windows.Controls.UserControl default_panel;\r
99                 public static System.Windows.Controls.Canvas default_api_canvas;\r
100                 public static WriteableBitmap default_buffer;\r
101                 //WriteableBitmap buffer;\r
102                 //System.Windows.Controls.Image instance;\r
103                 System.Windows.Controls.Canvas api_canvas;\r
104                 System.Windows.Controls.UserControl panel;\r
105                 Rectangle back_panel;\r
106                 double width_, height_;\r
107 \r
108                 public Canvas(int wid, int hei)\r
109                 {\r
110                         panel = default_panel;\r
111                         api_canvas = default_api_canvas;\r
112                         initialize(wid, hei);\r
113                 }\r
114                 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)\r
115                 {\r
116                         panel = system;\r
117                         api_canvas = apicnvs;\r
118                         initialize(wid, hei);\r
119                 }\r
120                 protected void initialize(int wid, int hei)\r
121                 {\r
122 \r
123                         width_ = wid;\r
124                         height_ = hei;\r
125                         api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
126                         Mouse._prime = api_canvas;\r
127                         Main.drawable = this;\r
128                         Main.canvas = this;\r
129 \r
130                         back_panel = new Rectangle(wid, hei);\r
131 \r
132                         flipexec = new SimpleProcedure(executeFlip);\r
133                 }\r
134                 protected void initialize__(int wid, int hei)\r
135                 {\r
136                         api_canvas.Width = wid;\r
137                         api_canvas.Height = hei;\r
138                         api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
139                         api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
140                         api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
141                         api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
142                         panel.KeyDown += Keyboard.Canvas_KeyDown;\r
143                         panel.KeyUp += Keyboard.Canvas_KeyUp;\r
144 \r
145                         HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
146                         //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
147                         htmlHost.SetStyleAttribute("width", (200+wid).ToString()+"px");\r
148                         htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
149                         htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
150                 }\r
151 \r
152                 #endregion\r
153 \r
154                 #region static initializer\r
155                 /*\r
156                 static System.Windows.Shapes.Line api_line;\r
157                 static System.Windows.Shapes.Path api_curve;\r
158                 static System.Windows.Shapes.Rectangle api_rect;\r
159                 static System.Windows.Shapes.Ellipse api_ellipse;\r
160                 static System.Windows.Shapes.Polygon api_polygon;\r
161                 static System.Windows.Shapes.Polyline api_polyline;\r
162                 static System.Windows.Media.Color api_color;\r
163                 static System.Windows.Media.SolidColorBrush api_fill;\r
164                 static System.Windows.Media.SolidColorBrush api_stroke;\r
165                 static System.Windows.Media.TranslateTransform api_translation;\r
166                 static Canvas()\r
167                 {\r
168                         api_line = new System.Windows.Shapes.Line();\r
169                         api_curve    = new System.Windows.Shapes.Path();\r
170                         api_rect     = new System.Windows.Shapes.Rectangle();\r
171                         api_ellipse  = new System.Windows.Shapes.Ellipse();\r
172                         api_polygon  = new System.Windows.Shapes.Polygon();\r
173                         api_polyline = new System.Windows.Shapes.Polyline();\r
174                         api_color       = new System.Windows.Media.Color();\r
175                         api_fill        = new System.Windows.Media.SolidColorBrush();\r
176                         api_stroke      = new System.Windows.Media.SolidColorBrush();\r
177                         api_translation = new System.Windows.Media.TranslateTransform();\r
178                 }\r
179                 */\r
180                 #endregion\r
181 \r
182 \r
183                 public override void clear(Color col)\r
184                 {\r
185                         back_panel.fill = col;\r
186                         rect(back_panel);\r
187                 }\r
188 \r
189                 int nextIntervalFrame = 1;\r
190                 public void flip()\r
191                 {\r
192                         //api_canvas.Dispatcher.BeginInvoke(flipexec);\r
193                         //flipexec();\r
194                         //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);\r
195                         lock (this)\r
196                         {\r
197                                 nextIntervalFrame = 1;\r
198                         }\r
199                         Internal.Main.canvas_flag.WaitOne();\r
200                 }\r
201                 public void executeFlip()\r
202                 {\r
203                         lock (this)\r
204                         {\r
205                                 nextIntervalFrame--;\r
206                         }\r
207                         if (nextIntervalFrame <= 0)\r
208                         {\r
209                                 var tmp = new System.Windows.Controls.Canvas();\r
210                                 if (stack.Count > 0)\r
211                                 {\r
212                                         foreach (Internal.PrimitiveFigure f in stack)\r
213                                         {\r
214                                                 tmp.Children.Add(f.toNative());\r
215                                         }\r
216                                 }\r
217                                 api_canvas.Children.Clear();\r
218                                 api_canvas.Children.Add(tmp);\r
219                                 stack.Clear();\r
220                                 Psychlops.Internal.Main.canvas_flag.Set();\r
221                         }\r
222                         System.Threading.Thread.Sleep(0);\r
223                 }\r
224 \r
225                 #region Properties\r
226 \r
227                 public double width { get { return width_; } }\r
228                 public double height { get { return height_; } }\r
229                 public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
230                 public double getWidth() { return width; }\r
231                 public double getHeight() { return height; }\r
232                 public override Point getCenter() { return center; }\r
233                 public double getHCenter() { return width / 2; }\r
234                 public double getVCenter() { return height / 2; }\r
235                 public double getRefreshRate() { return 60; }\r
236 \r
237                 #endregion\r
238 \r
239 \r
240         }\r
241 \r
242 \r
243 \r
244         #region primitive tokenizer\r
245 \r
246         partial struct Point\r
247         {\r
248                 public static implicit operator System.Windows.Point(Point d)\r
249                 {\r
250                         return new System.Windows.Point(d.x, d.y);\r
251                 }\r
252         }\r
253 \r
254         partial struct Color\r
255         {\r
256                 public static implicit operator System.Windows.Media.Color(Color d)\r
257                 {\r
258                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
259                 }\r
260                 public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
261                 {\r
262                         return new SolidColorBrush { Color = d };\r
263                 }\r
264         }\r
265 \r
266         partial struct Stroke\r
267         {\r
268                 public void apply(System.Windows.Shapes.Shape target)\r
269                 {\r
270                         target.Stroke = this;\r
271                         //target.StrokeDashArray\r
272                         target.StrokeThickness = thick;\r
273                 }\r
274                 public static implicit operator SolidColorBrush(Stroke d)\r
275                 {\r
276                         return new SolidColorBrush { Color = d.color };\r
277                 }\r
278         }\r
279 \r
280         partial class Line\r
281         {\r
282                 public Line clone()\r
283                 {\r
284                         return (Line)MemberwiseClone();\r
285                 }\r
286                 public static implicit operator System.Windows.Shapes.Line(Line d)\r
287                 {\r
288                         var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
289                         if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
290                         else d.stroke.apply(tmp);\r
291                         return tmp;\r
292                 }\r
293                 public UIElement toNative() { return this; }\r
294         }\r
295 \r
296         partial class Rectangle\r
297         {\r
298                 public Rectangle clone()\r
299                 {\r
300                         return (Rectangle)MemberwiseClone();\r
301                 }\r
302                 public static implicit operator System.Windows.Rect(Rectangle d)\r
303                 {\r
304                         return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
305                 }\r
306                 public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
307                 {\r
308                         var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
309                         d.stroke.apply(tmp);\r
310                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
311                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
312                         return tmp;\r
313                 }\r
314                 public UIElement toNative() { return this; }\r
315         }\r
316 \r
317         partial class Ellipse\r
318         {\r
319                 public Ellipse clone()\r
320                 {\r
321                         return (Ellipse)MemberwiseClone();\r
322                 }\r
323                 public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
324                 {\r
325                         var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
326                         d.stroke.apply(tmp);\r
327                         System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
328                         System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
329                         return tmp;\r
330                 }\r
331                 public UIElement toNative() { return this; }\r
332         }\r
333 \r
334         partial class Polygon\r
335         {\r
336                 public Polygon clone()\r
337                 {\r
338                         return (Polygon)MemberwiseClone();\r
339                 }\r
340                 public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
341                 {\r
342                         var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
343                         d.stroke.apply(tmp);\r
344                         foreach (Point p in d.vertices)\r
345                         {\r
346                                 tmp.Points.Add(p);\r
347                         }\r
348                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
349                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
350                         return tmp;\r
351                 }\r
352                 public UIElement toNative() { return this; }\r
353         }\r
354         \r
355         partial class Letters\r
356         {\r
357                 #region static initializer\r
358                 internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
359                 internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
360                 internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
361                 static Letters()\r
362                 {\r
363                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
364                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
365                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
366                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
367                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
368                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
369                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
370                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
371                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);\r
372                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);\r
373                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);\r
374                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);\r
375                 }\r
376                 #endregion\r
377                 public Letters clone()\r
378                 {\r
379                         return (Letters)MemberwiseClone();\r
380                 }\r
381                 public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
382                 {\r
383                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
384                         var tmp = new System.Windows.Controls.TextBlock {\r
385                                 Text = d.str, Width = 500, Height = 500,\r
386                                 FontSize = d.font.size,\r
387                                 //tmp.FontFamily = ,\r
388                                 FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
389                                 FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
390                                 TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
391                                 Foreground = d.fill\r
392                         };\r
393                         double left = 0;\r
394                         switch (d.align)\r
395                         {\r
396                                 case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;\r
397                                 case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = tmp.Width / 2; break;\r
398                                 case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = tmp.Width; break;\r
399                         }\r
400                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
401                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
402                         return tmp;\r
403                 }\r
404                 public UIElement toNative() { return this; }\r
405         }\r
406 \r
407         partial class Image\r
408         {\r
409                 internal void initialize__(int wid, int hei)\r
410                 {\r
411                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
412                         //buffer = new WriteableBitmap(wid, hei);\r
413                 }\r
414                 internal void create__(int wid, int hei)\r
415                 {\r
416                         buffer = new WriteableBitmap(wid, hei);\r
417                 }\r
418                 delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
419                 delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
420                 public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
421                 {\r
422                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
423                         //buffer.ForEach(func);\r
424                 }\r
425                 public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
426                 {\r
427                         Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
428                         //buffer.ForEach(func);\r
429                 }\r
430                 public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
431                 {\r
432                         buffer.ForEach(func);\r
433                 }\r
434                 public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
435                 {\r
436                         buffer.ForEach(func);\r
437                 }\r
438 \r
439                 public Image clone()\r
440                 {\r
441                         return (Image)MemberwiseClone();\r
442                 }\r
443                 public static implicit operator System.Windows.Controls.Image(Image d)\r
444                 {\r
445                         var tmp = new System.Windows.Controls.Image();\r
446                         tmp.Source = d.buffer;\r
447                         System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
448                         System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
449                         return tmp;\r
450                 }\r
451                 public UIElement toNative() { return this; }\r
452         }\r
453 \r
454         #endregion\r
455 \r
456 \r
457 }