OSDN Git Service

test
[psychlops/silverlight.git] / dev4 / psychlops / core / graphic / module.cs
1 using System;\r
2 using System.Windows;\r
3 \r
4 \r
5 namespace Psychlops\r
6 {\r
7         public static class StaticFunctions\r
8         {\r
9                 public static T[] NewArray<T>(int x)\r
10                         where T : new()\r
11                 {\r
12                         T[] t = new T[x];\r
13                         for (int i = 0; i < x; i++)\r
14                         {\r
15                                 t[i] = new T();\r
16                         }\r
17                         return t;\r
18                 }\r
19                 public static T[,] NewArray<T>(int x, int y)\r
20                         where T : new()\r
21                 {\r
22                         T[,] t = new T[x,y];\r
23                         for (int i = 0; i < x; i++)\r
24                         {\r
25                                 for (int j = 0; j < x; j++)\r
26                                 {\r
27                                         t[i,j] = new T();\r
28                                 }\r
29                         }\r
30                         return t;\r
31                 }\r
32                 public static T[,,] NewArray<T>(int x, int y, int z)\r
33                         where T : new()\r
34                 {\r
35                         T[,,] t = new T[x, y, z];\r
36                         for (int i = 0; i < x; i++)\r
37                         {\r
38                                 for (int j = 0; j < y; j++)\r
39                                 {\r
40                                         for (int k = 0; k < z; k++)\r
41                                         {\r
42                                                 t[i, j, k] = new T();\r
43                                         }\r
44                                 }\r
45                         }\r
46                         return t;\r
47                 }\r
48         }\r
49 \r
50         public partial struct Point\r
51         {\r
52                 public double x, y, z;\r
53                 public Point(double dx, double dy, double dz = 0.0)\r
54                 {\r
55                         x = dx;\r
56                         y = dy;\r
57                         z = dz;\r
58                 }\r
59                 public Point set(double dx, double dy, double dz = 0.0)\r
60                 {\r
61                         x = dx;\r
62                         y = dy;\r
63                         z = dz;\r
64                         return this;\r
65                 }\r
66 \r
67                 public static Point operator +(Point lhs, Point rhs)\r
68                 {\r
69                         return new Point(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);\r
70                 }\r
71                 public static Point operator -(Point lhs, Point rhs)\r
72                 {\r
73                         return new Point(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);\r
74                 }\r
75                 public override string ToString()\r
76                 {\r
77                         return "X:"+ x.ToString() + " Y:"+ y.ToString() + " Z:"+ z.ToString();\r
78                 }\r
79         }\r
80 \r
81 \r
82         public partial struct Color\r
83         {\r
84                 public double r, g, b, a;\r
85                 public Color(double lum)\r
86                 {\r
87                         r = g = b = lum;\r
88                         a = 1.0;\r
89                 }\r
90                 public Color(double red, double green, double blue, double alpha = 1.0)\r
91                 {\r
92                         r = red;\r
93                         g = green;\r
94                         b = blue;\r
95                         a = alpha;\r
96                 }\r
97                 public void set(double lum)\r
98                 {\r
99                         r = g = b = lum;\r
100                         a = 1.0;\r
101                 }\r
102                 public void set(double red, double green, double blue, double alpha = 1.0)\r
103                 {\r
104                         r = red;\r
105                         g = green;\r
106                         b = blue;\r
107                         a = alpha;\r
108                 }\r
109 \r
110                 public override string ToString()\r
111                 {\r
112                         return "R:" + r.ToString() + " G:" + g.ToString() + " B:" + b.ToString() + " A:" + a.ToString();\r
113                 }\r
114 \r
115                 public static readonly Color\r
116                         black = new Color(0, 0, 0, 1),\r
117                         red = new Color(1, 0, 0, 1),\r
118                         green = new Color(0, 1, 0, 1),\r
119                         blue = new Color(0, 0, 1, 1),\r
120                         yellow = new Color(1, 1, 0, 1),\r
121                         magenta = new Color(1, 0, 1, 1),\r
122                         cyan = new Color(0, 1, 1, 1),\r
123                         gray = new Color(.5, .5, .5, 1),\r
124                         white = new Color(1, 1, 1, 1),\r
125                         null_color = new Color(0, 0, 0, 0),\r
126                         transparent = new Color(0, 0, 0, 0);\r
127 \r
128         }\r
129 \r
130 \r
131         public interface Drawable\r
132         {\r
133                 Point getCenter();\r
134                 void clear(Color col);\r
135                 void pix(int x, int y, Color col);\r
136                 void line(Line drawee);\r
137                 void rect(Rectangle drawee);\r
138                 void ellipse(Ellipse drawee);\r
139                 void polygon(Polygon drawee);\r
140                 void letters(Letters drawee);\r
141                 void image(Image drawee);\r
142                 void group(Group drawee);\r
143                 void shader(ShaderField drawee);\r
144                 void msg(string s, double x, double y, Color c);\r
145         }\r
146 \r
147 \r
148 \r
149         public interface Figure\r
150         {\r
151                 Point datum { get; set; }\r
152                 Figure shift(Point p);\r
153                 Figure centering(Point p);\r
154                 void draw();\r
155         }\r
156         public static class FigureExtention\r
157         {\r
158                 public static Point getDatum(this Figure target)\r
159                 {\r
160                         return target.datum;\r
161                 }\r
162                 public static Point setDatum(this Figure target, Point p)\r
163                 {\r
164                         target.datum = p;\r
165                         return target.datum;\r
166                 }\r
167                 public static Figure shift(this Figure target, double x, double y, double z = 0.0)\r
168                 {\r
169                         return target.shift(new Point(x, y, z));\r
170                 }\r
171                 public static Figure centering(this Figure target)\r
172                 {\r
173                         return target.centering(Main.drawable.getCenter());\r
174                 }\r
175                 public static Figure centering(this Figure target, double x, double y, double z = 0.0)\r
176                 {\r
177                         return target.centering(new Point(x, y, z));\r
178                 }\r
179         }\r
180 \r
181         namespace Internal\r
182         {\r
183                 public interface PrimitiveFigure : Figure\r
184                 {\r
185                         UIElement toNative();\r
186                         UIElement poolNative(Canvas c);\r
187                 }\r
188         }\r
189 \r
190         public partial class Group : Internal.PrimitiveFigure\r
191         {\r
192                 System.Collections.Generic.List<Figure> list;\r
193                 System.Windows.Controls.Canvas cnvs;\r
194                 System.Windows.Media.TransformGroup trans;\r
195                 System.Windows.Media.TransformCollection transF;\r
196                 System.Windows.Media.RotateTransform rotateF;\r
197                 SimpleProcedure setRotation_;\r
198                 System.Windows.Media.ScaleTransform scaleF;\r
199                 SimpleProcedure setScaling_;\r
200                 System.Windows.Media.TranslateTransform translateF;\r
201 \r
202                 bool AsyncBool;\r
203                 double rotation_;\r
204                 public double rotation\r
205                 {\r
206                         get { return rotation_; }\r
207                         set { rotation_ = value; rotateF.Dispatcher.BeginInvoke(setRotation_); }\r
208                 }               \r
209                 public Point axis\r
210                 {\r
211                         get;\r
212                         set;\r
213                 }\r
214                 Point scaling_;\r
215                 public Point scaling\r
216                 {\r
217                         get { return scaling_; }\r
218                         set { scaling_ = value; scaleF.Dispatcher.BeginInvoke(setScaling_); }\r
219                 }\r
220 \r
221                 AppendFunc1 append_;\r
222 \r
223                 public Group()\r
224                 {\r
225                         setRotation_ = new SimpleProcedure(setRotation__);\r
226                         setScaling_ = new SimpleProcedure(setScaling__);\r
227                         append_ = new AppendFunc1(append__);\r
228                         list = new System.Collections.Generic.List<Figure>();\r
229                         AsyncBool = false;\r
230                         initialize__();\r
231                         while (!AsyncBool) { }\r
232                 }\r
233 \r
234                 public Group append(Internal.PrimitiveFigure fig)\r
235                 {\r
236                         list.Add(fig);\r
237                         cnvs.Dispatcher.BeginInvoke(append_, fig);\r
238                         return this;\r
239                 }\r
240 \r
241                 public Point datum { get; set; }\r
242                 public Figure shift(Point p)\r
243                 {\r
244                         datum = datum + p;\r
245                         return this;\r
246                 }\r
247                 public Figure centering(Point p)\r
248                 {\r
249                         datum = p;\r
250                         return this;\r
251                 }\r
252                 public void draw()\r
253                 {\r
254                         Main.drawable.group(this);\r
255                 }\r
256         }\r
257 \r
258 \r
259 \r
260         public partial class ShaderField : Internal.PrimitiveFigure\r
261         {\r
262                 public Point v1, v2;\r
263                 \r
264                 public ShaderField()\r
265                 {\r
266                         set(0,0);\r
267                 }\r
268                 public ShaderField(double wid, double hei)\r
269                 {\r
270                         initialized = false;\r
271                         set(wid, hei);\r
272                 }\r
273                 public ShaderField(Rectangle another)\r
274                 {\r
275                         v1 = another.v1;\r
276                         v2 = another.v2;\r
277                 }\r
278                 public ShaderField set(double wid, double hei)\r
279                 {\r
280                         v1.set(0, 0, 0);\r
281                         v2.set(wid, hei, 0);\r
282                         return this;\r
283                 }\r
284                 ShaderField set(Point po1, Point po2)\r
285                 {\r
286                         v1 = po1;\r
287                         v2 = po2;\r
288                         return this;\r
289                 }\r
290                 public ShaderField set(double l, double t, double r, double b)\r
291                 {\r
292                         v1.set(l, t, 0);\r
293                         v2.set(r, b, 0);\r
294                         return this;\r
295                 }\r
296                 public ShaderField set(ShaderField another)\r
297                 {\r
298                         v1 = another.v1;\r
299                         v2 = another.v2;\r
300                         return this;\r
301                 }\r
302 \r
303                 public ShaderField resize(double width, double height)\r
304                 {\r
305                         Point po = center;\r
306                         set(width, height);\r
307                         centering(po);\r
308                         return this;\r
309                 }\r
310 \r
311 \r
312                 public Point datum\r
313                 {\r
314                         get { return v1; }\r
315                         set { double w = width, h = height; v1 = value; v2 = v1 + new Point(w,h); }\r
316                 }\r
317                 public ShaderField move_to(Point p) { datum = p; return this; }\r
318                 public ShaderField move_to(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
319                 public ShaderField locate(Point p) { datum = p; return this; }\r
320                 public ShaderField locate(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
321 \r
322                 public Figure shift(Point p)\r
323                 {\r
324                         v1 += p;\r
325                         v2 += p;\r
326                         return this;\r
327                 }\r
328                 public Figure centering(Point p)\r
329                 {\r
330                         double h = width, v = height;\r
331                         v1.x = p.x - h / 2.0;\r
332                         v1.y = p.y - v / 2.0;\r
333                         v2.x = v1.x + h;\r
334                         v2.y = v1.y + v;\r
335                         return this;\r
336                 }\r
337 \r
338                 public virtual void draw()\r
339                 {\r
340                         Main.drawable.shader(this);\r
341                 }\r
342 \r
343                 internal Action setParameters = initialize___;\r
344                 internal Action initialize__ = initialize___;\r
345                 private static void initialize___() {}\r
346                 internal bool initialized = false;\r
347 \r
348                 public double left { get { return v1.x; } }\r
349                 public double top { get { return v1.y; } }\r
350                 public double right { get { return v2.x; } }\r
351                 public double bottom { get { return v2.y; } }\r
352                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
353                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
354                 public Point center\r
355                 {\r
356                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
357                         set { centering(value); }\r
358                 }\r
359                 public double getLeft() { return left; }\r
360                 public double getTop() { return top; }\r
361                 public double getRight() { return right; }\r
362                 public double getBottom() { return bottom; }\r
363                 public double getWidth() { return width; }\r
364                 public double getHeight() { return height; }\r
365                 public Point getCenter() { return center; }\r
366         }\r
367 \r
368 }