OSDN Git Service

flip ok
[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 \r
11 \r
12 \r
13 namespace Psychlops\r
14 {\r
15 \r
16         public class Canvas : Drawable\r
17         {\r
18                 //public static System.Windows.Controls.Image default_buffer_frame;\r
19                 public static System.Windows.Controls.UserControl default_panel;\r
20                 public static System.Windows.Controls.Canvas default_api_canvas;\r
21                 public static WriteableBitmap default_buffer;\r
22                 //WriteableBitmap buffer;\r
23                 //System.Windows.Controls.Image instance;\r
24                 System.Windows.Controls.Canvas api_canvas;\r
25                 System.Windows.Controls.UserControl panel;\r
26                 System.Windows.Shapes.Rectangle back_panel;\r
27                 System.Windows.Media.SolidColorBrush back_panel_color;\r
28 \r
29                 public Canvas(int wid, int hei)\r
30                 {\r
31                         panel = default_panel;\r
32                         api_canvas = default_api_canvas;\r
33                         //instance = default_buffer_frame;\r
34                         initialize(wid, hei);\r
35                 }\r
36                 public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs,  System.Windows.Controls.UserControl system)\r
37                 {\r
38                         panel = system;\r
39                         api_canvas = apicnvs;\r
40                         //instance = target;\r
41                         initialize(wid, hei);\r
42                 }\r
43                 protected void initialize(int wid, int hei)\r
44                 {\r
45                         //instance.Width = wid;\r
46                         //instance.Height = hei;\r
47                         //instance.Source = buffer;\r
48                         //buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
49                         //default_buffer = buffer;\r
50                         api_canvas.Width = wid;\r
51                         api_canvas.Height = hei;\r
52                         api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
53                         api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
54                         api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
55                         api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
56                         panel.KeyDown += Keyboard.Canvas_KeyDown;\r
57                         panel.KeyUp += Keyboard.Canvas_KeyUp;\r
58                         Mouse._prime = api_canvas;\r
59                         Main.drawable = this;\r
60 \r
61                         back_panel = new System.Windows.Shapes.Rectangle();\r
62                         back_panel.Width = wid;\r
63                         back_panel.Height = hei;\r
64                         back_panel_color = new SolidColorBrush();\r
65                         back_panel.Fill = back_panel_color;\r
66                 }\r
67                 public Point getCenter()\r
68                 {\r
69                         return new Point(api_canvas.Width / 2.0, api_canvas.Height / 2.0, 0);\r
70                 }\r
71 \r
72                 public void clear()\r
73                 {\r
74                         //buffer.Clear(Color.black);\r
75                         clear(Color.black);\r
76                 }\r
77                 public void clear(Color col)\r
78                 {\r
79                         api_canvas.Children.Clear();\r
80                         back_panel_color.Color = col;\r
81                         api_canvas.Children.Add(back_panel);\r
82                         //buffer.Clear(col);\r
83                 }\r
84 \r
85                 public void pix(int x, int y, Color col)\r
86                 {\r
87                         //buffer.SetPixel(x, y, col);\r
88                 }\r
89 \r
90                 #region static initializer\r
91                 /*\r
92                 static System.Windows.Shapes.Line api_line;\r
93                 static System.Windows.Shapes.Path api_curve;\r
94                 static System.Windows.Shapes.Rectangle api_rect;\r
95                 static System.Windows.Shapes.Ellipse api_ellipse;\r
96                 static System.Windows.Shapes.Polygon api_polygon;\r
97                 static System.Windows.Shapes.Polyline api_polyline;\r
98                 static System.Windows.Media.Color api_color;\r
99                 static System.Windows.Media.SolidColorBrush api_fill;\r
100                 static System.Windows.Media.SolidColorBrush api_stroke;\r
101                 static System.Windows.Media.TranslateTransform api_translation;\r
102                 */\r
103                 protected static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
104                 protected static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
105                 protected static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
106                 static Canvas()\r
107                 {\r
108                         FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
109                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
110                         FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
111                         FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
112                         FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
113                         FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
114                         FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
115                         LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
116                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);\r
117                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);\r
118                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);\r
119                         LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);\r
120 \r
121                         /*\r
122                         api_line = new System.Windows.Shapes.Line();\r
123                         api_curve    = new System.Windows.Shapes.Path();\r
124                         api_rect     = new System.Windows.Shapes.Rectangle();\r
125                         api_ellipse  = new System.Windows.Shapes.Ellipse();\r
126                         api_polygon  = new System.Windows.Shapes.Polygon();\r
127                         api_polyline = new System.Windows.Shapes.Polyline();\r
128                         api_color       = new System.Windows.Media.Color();\r
129                         api_fill        = new System.Windows.Media.SolidColorBrush();\r
130                         api_stroke      = new System.Windows.Media.SolidColorBrush();\r
131                         api_translation = new System.Windows.Media.TranslateTransform();\r
132                         */\r
133                 }\r
134                 #endregion\r
135 \r
136                 public void line(Line drawee, Color col)\r
137                 {\r
138                         //buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
139                         var zapi_shape = new System.Windows.Shapes.Line();\r
140                         var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
141                         zapi_shape.X1 = (int)drawee.begin.x;\r
142                         zapi_shape.Y1 = (int)drawee.begin.y;\r
143                         zapi_shape.X2 = (int)drawee.end.x;\r
144                         zapi_shape.Y2 = (int)drawee.end.y;\r
145                         zapi_fill.Color = col;\r
146                         zapi_fill.Opacity = 1.0;\r
147                         zapi_shape.Stroke = zapi_fill;\r
148                         zapi_shape.StrokeThickness = 1;\r
149                         api_canvas.Children.Add(zapi_shape);\r
150                         //System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
151                         //System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
152                 }\r
153                 public void rect(Rectangle drawee, Color col)\r
154                 {\r
155                         //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col);\r
156 \r
157                         var zapi_shape = new System.Windows.Shapes.Rectangle();\r
158                         var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
159                         zapi_shape.Width = drawee.width;\r
160                         zapi_shape.Height = drawee.height;\r
161                         zapi_fill.Color = col;\r
162                         zapi_shape.Fill = zapi_fill;\r
163                         api_canvas.Children.Add(zapi_shape);\r
164                         System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
165                         System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
166                 }\r
167                 public void ellipse(Ellipse drawee, Color col)\r
168                 {\r
169                         //buffer.DrawEllipse((int)(drawee.datum.x - drawee.xdiameter / 2), (int)(drawee.datum.y - drawee.ydiameter / 2), (int)(drawee.datum.x + drawee.xdiameter / 2), (int)(drawee.datum.y + drawee.ydiameter / 2), col);\r
170 \r
171                         var zapi_shape = new System.Windows.Shapes.Ellipse();\r
172                         var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
173                         zapi_shape.Width = drawee.width;\r
174                         zapi_shape.Height = drawee.height;\r
175                         zapi_fill.Color = col;\r
176                         zapi_shape.Fill = zapi_fill;\r
177                         api_canvas.Children.Add(zapi_shape);\r
178                         System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
179                         System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
180                 }\r
181                 public void polygon(Polygon drawee, Color col)\r
182                 {\r
183                         /*\r
184                         int[] ps = new int[drawee.vertices.Count];\r
185                         buffer.DrawPolyline(ps, col);\r
186                         */\r
187                         var zapi_shape = new System.Windows.Shapes.Polygon();\r
188                         var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
189                         int i = 0;\r
190                         foreach (Point p in drawee.vertices)\r
191                         {\r
192                                 zapi_shape.Points.Add(p);\r
193                         }\r
194                         zapi_fill.Color = col;\r
195                         zapi_shape.Fill = zapi_fill;\r
196                         api_canvas.Children.Add(zapi_shape);\r
197                         System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
198                         System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
199                 }\r
200                 public void letters(Letters drawee, Color col)\r
201                 {\r
202                         //var zapi_shape = new System.Windows.Documents.Glyphs();\r
203                         var zapi_shape = new System.Windows.Controls.TextBlock();\r
204                         var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
205                         zapi_shape.Width = width;\r
206                         zapi_shape.Height = height;\r
207                         zapi_shape.Text = drawee.str;\r
208                         //zapi_shape.FontFamily = ;\r
209                         zapi_shape.FontSize = drawee.font.size;\r
210                         zapi_shape.FontStyle = FONT_STYLE_BRIDGE[drawee.font.style];\r
211                         zapi_shape.FontWeight = FONT_WEIGHT_BRIDGE[drawee.font.weight];\r
212                         zapi_shape.TextAlignment = LETTERS_H_ALIGN_BRIDGE[drawee.align];\r
213                         zapi_fill.Color = col;\r
214                         zapi_shape.Foreground = zapi_fill;\r
215                         api_canvas.Children.Add(zapi_shape);\r
216                         double left = 0;\r
217                         switch(drawee.align)\r
218                         {\r
219                                 case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;\r
220                                 case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = zapi_shape.Width/2; break;\r
221                                 case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = zapi_shape.Width; break;\r
222                         }\r
223                         System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x - left);\r
224                         System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y - drawee.font.size);\r
225                 }\r
226                 public void image(Image drawee)\r
227                 {\r
228                         //buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\r
229                         var zapi_shape = new System.Windows.Controls.Image();\r
230                         zapi_shape.Source = drawee.buffer;\r
231                         api_canvas.Children.Add(zapi_shape);\r
232                         System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
233                         System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
234                 }\r
235 \r
236 \r
237                 public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
238                 public void msg(string str, double x, double y, Color col)\r
239                 {\r
240                         var let = new Letters(str);\r
241                         let.locate(x, y);\r
242                         this.letters(let, col);\r
243                 }\r
244                 public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
245                 public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
246 \r
247 \r
248                 public void flip()\r
249                 {\r
250                         //buffer.Invalidate();\r
251                 }\r
252 \r
253                 public double width { get { return api_canvas.Width; } }\r
254                 public double height { get { return api_canvas.Height; } }\r
255 \r
256         }\r
257 \r
258 }