OSDN Git Service

kb
[psychlops/silverlight.git] / dev3 / psychlops / core / graphic / module.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 struct Point\r
17         {\r
18                 public double x, y, z;\r
19                 public Point(double dx, double dy, double dz)\r
20                 {\r
21                         x = dx;\r
22                         y = dy;\r
23                         z = dz;\r
24                 }\r
25                 public Point(double dx, double dy)\r
26                 {\r
27                         x = dx;\r
28                         y = dy;\r
29                         z = 0.0;\r
30                 }\r
31                 public Point set(double dx, double dy, double dz)\r
32                 {\r
33                         x = dx;\r
34                         y = dy;\r
35                         z = dz;\r
36                         return this;\r
37                 }\r
38                 public Point set(double dx, double dy)\r
39                 {\r
40                         x = dx;\r
41                         y = dy;\r
42                         z = 0.0;\r
43                         return this;\r
44                 }\r
45 \r
46                 public static Point operator +(Point lhs, Point rhs)\r
47                 {\r
48                         return new Point(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);\r
49                 }\r
50                 public static Point operator -(Point lhs, Point rhs)\r
51                 {\r
52                         return new Point(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);\r
53                 }\r
54                 public static implicit operator System.Windows.Point(Point d)\r
55                 {\r
56                         return new System.Windows.Point(d.x, d.y);\r
57                 }\r
58         }\r
59 \r
60 \r
61         public struct Color\r
62         {\r
63                 public double r, g, b, a;\r
64                 public Color(double lum)\r
65                 {\r
66                         r = g = b = lum;\r
67                         a = 1.0;\r
68                 }\r
69                 public Color(double red, double green, double blue)\r
70                 {\r
71                         r = red;\r
72                         g = green;\r
73                         b = blue;\r
74                         a = 1.0;\r
75                 }\r
76                 public Color(double red, double green, double blue, double alpha)\r
77                 {\r
78                         r = red;\r
79                         g = green;\r
80                         b = blue;\r
81                         a = alpha;\r
82                 }\r
83                 public void set(double lum)\r
84                 {\r
85                         r = g = b = lum;\r
86                         a = 1.0;\r
87                 }\r
88                 public void set(double red, double green, double blue)\r
89                 {\r
90                         r = red;\r
91                         g = green;\r
92                         b = blue;\r
93                         a = 1.0;\r
94                 }\r
95                 public void set(double red, double green, double blue, double alpha)\r
96                 {\r
97                         r = red;\r
98                         g = green;\r
99                         b = blue;\r
100                         a = alpha;\r
101                 }\r
102                 public static implicit operator System.Windows.Media.Color(Color d)\r
103                 {\r
104                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
105                 }\r
106 \r
107                 public static readonly Color\r
108                         black = new Color(0, 0, 0, 1),\r
109                         red = new Color(1, 0, 0, 1),\r
110                         green = new Color(0, 1, 0, 1),\r
111                         blue = new Color(0, 0, 1, 1),\r
112                         yellow = new Color(1, 1, 0, 1),\r
113                         magenta = new Color(1, 0, 1, 1),\r
114                         cyan = new Color(0, 1, 1, 1),\r
115                         white = new Color(1, 1, 1, 1),\r
116                         gray = new Color(.5, .5, .5, 1);\r
117 \r
118         }\r
119 \r
120 \r
121         public interface Drawable\r
122         {\r
123                 Point getCenter();\r
124                 void clear(Color col);\r
125                 void pix(int x, int y, Color col);\r
126                 void line(Line drawee, Color col);\r
127                 void rect(Rectangle drawee, Color col);\r
128                 void ellipse(Ellipse drawee, Color col);\r
129                 void polygon(Polygon drawee, Color col);\r
130                 void letters(Letters drawee, Color col);\r
131                 void image(Image drawee);\r
132                 void msg(string s, double x, double y, Color c);\r
133         }\r
134 \r
135 \r
136 \r
137         public interface Figure\r
138         {\r
139                 Figure shift(Point p);\r
140                 Figure centering(Point p);\r
141                 void draw();\r
142         }\r
143         public static class FigureExtention\r
144         {\r
145                 public static Figure shift(this Figure target, double x, double y)\r
146                 {\r
147                         return target.shift(new Point(x, y));\r
148                 }\r
149                 public static Figure centering(this Figure target)\r
150                 {\r
151                         return target.centering(Main.drawable.getCenter());\r
152                 }\r
153                 public static Figure centering(this Figure target, double x, double y)\r
154                 {\r
155                         return target.centering(new Point(x, y));\r
156                 }\r
157         }\r
158 \r
159         public class Image : Figure\r
160         {\r
161                 public WriteableBitmap buffer;\r
162                 public Point datum;\r
163                 public Rectangle self_rect;\r
164 \r
165                 public Image(int wid, int hei)\r
166                 {\r
167                         buffer = new WriteableBitmap(wid, hei);\r
168                         self_rect = new Rectangle(wid, hei);\r
169                 }\r
170 \r
171                 /*public Image shift(double x, double y)\r
172                 {\r
173                         datum.x += x;\r
174                         datum.y += y;\r
175                         return this;\r
176                 }*/\r
177                 public Figure shift(Point p)\r
178                 {\r
179                         datum += p;\r
180                         return this;\r
181                 }\r
182                 public Figure centering(Point p)\r
183                 {\r
184                         datum.x = p.x - width / 2.0;\r
185                         datum.y = p.y - height / 2.0;\r
186                         return this;\r
187                 }\r
188 \r
189                 public void pix(int x, int y, Color col)\r
190                 {\r
191                         buffer.SetPixel(x, y, col);\r
192                 }\r
193 \r
194 \r
195                 public void field(System.Func<int,int,System.Windows.Media.Color> func)\r
196                 {\r
197                         buffer.ForEach(func);\r
198                 }\r
199                 public void field(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
200                 {\r
201                         buffer.ForEach(func);\r
202                 }\r
203 \r
204                 public void draw()\r
205                 {\r
206                         Main.drawable.image(this);\r
207                 }\r
208 \r
209                 public double width  { get { return self_rect.width; } }\r
210                 public double height { get { return self_rect.height; } }\r
211 \r
212         }\r
213 \r
214 }