OSDN Git Service

6adecfd3314ac29ae0fc8b3b7e99eab5cc8b2c85
[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                         white = new Color(1, 1, 1, 1),\r
124                         gray = new Color(.5, .5, .5, 1),\r
125                         null_color = new Color(0, 0, 0, 0);\r
126 \r
127         }\r
128 \r
129 \r
130         public interface Drawable\r
131         {\r
132                 Point getCenter();\r
133                 void clear(Color col);\r
134                 void pix(int x, int y, Color col);\r
135                 void line(Line drawee);\r
136                 void rect(Rectangle drawee);\r
137                 void ellipse(Ellipse drawee);\r
138                 void polygon(Polygon drawee);\r
139                 void letters(Letters drawee);\r
140                 void image(Image drawee);\r
141                 void group(Group drawee);\r
142                 void msg(string s, double x, double y, Color c);\r
143         }\r
144 \r
145 \r
146 \r
147         public interface Figure\r
148         {\r
149                 Point datum { get; set; }\r
150                 Figure shift(Point p);\r
151                 Figure centering(Point p);\r
152                 void draw();\r
153         }\r
154         public static class FigureExtention\r
155         {\r
156                 public static Point getDatum(this Figure target)\r
157                 {\r
158                         return target.datum;\r
159                 }\r
160                 public static Point setDatum(this Figure target, Point p)\r
161                 {\r
162                         target.datum = p;\r
163                         return target.datum;\r
164                 }\r
165                 public static Figure shift(this Figure target, double x, double y, double z = 0.0)\r
166                 {\r
167                         return target.shift(new Point(x, y, z));\r
168                 }\r
169                 public static Figure centering(this Figure target)\r
170                 {\r
171                         return target.centering(Main.drawable.getCenter());\r
172                 }\r
173                 public static Figure centering(this Figure target, double x, double y, double z = 0.0)\r
174                 {\r
175                         return target.centering(new Point(x, y, z));\r
176                 }\r
177         }\r
178 \r
179         namespace Internal\r
180         {\r
181                 public interface PrimitiveFigure : Figure\r
182                 {\r
183                         UIElement toNative();\r
184                         UIElement poolNative(Canvas c);\r
185                 }\r
186         }\r
187 \r
188         public partial class Group : Internal.PrimitiveFigure\r
189         {\r
190                 System.Collections.Generic.List<Figure> list;\r
191                 System.Windows.Controls.Canvas cnvs;\r
192                 System.Windows.Media.TransformGroup trans;\r
193                 System.Windows.Media.TransformCollection transF;\r
194                 System.Windows.Media.RotateTransform rotateF;\r
195                 SimpleProcedure setRotation_;\r
196                 System.Windows.Media.ScaleTransform scaleF;\r
197                 SimpleProcedure setScaling_;\r
198                 System.Windows.Media.TranslateTransform translateF;\r
199 \r
200                 bool AsyncBool;\r
201                 double rotation_;\r
202                 public double rotation\r
203                 {\r
204                         get { return rotation_; }\r
205                         set { rotation_ = value; rotateF.Dispatcher.BeginInvoke(setRotation_); }\r
206                 }               \r
207                 public Point axis\r
208                 {\r
209                         get;\r
210                         set;\r
211                 }\r
212                 Point scaling_;\r
213                 public Point scaling\r
214                 {\r
215                         get { return scaling_; }\r
216                         set { scaling_ = value; scaleF.Dispatcher.BeginInvoke(setScaling_); }\r
217                 }\r
218 \r
219                 AppendFunc1 append_;\r
220 \r
221                 public Group()\r
222                 {\r
223                         setRotation_ = new SimpleProcedure(setRotation__);\r
224                         setScaling_ = new SimpleProcedure(setScaling__);\r
225                         append_ = new AppendFunc1(append__);\r
226                         list = new System.Collections.Generic.List<Figure>();\r
227                         AsyncBool = false;\r
228                         initialize__();\r
229                         while (!AsyncBool) { }\r
230                 }\r
231 \r
232                 public Group append(Internal.PrimitiveFigure fig)\r
233                 {\r
234                         list.Add(fig);\r
235                         cnvs.Dispatcher.BeginInvoke(append_, fig);\r
236                         return this;\r
237                 }\r
238 \r
239                 public Point datum { get; set; }\r
240                 public Figure shift(Point p)\r
241                 {\r
242                         datum = datum + p;\r
243                         return this;\r
244                 }\r
245                 public Figure centering(Point p)\r
246                 {\r
247                         datum = p;\r
248                         return this;\r
249                 }\r
250                 public void draw()\r
251                 {\r
252                         Main.drawable.group(this);\r
253                 }\r
254         }\r
255 \r
256 }