OSDN Git Service

2ecc730f9110bb5790e04d727ed478a9958b4a65
[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         }\r
133 \r
134 \r
135 \r
136         public interface Figure\r
137         {\r
138                 Figure shift(Point p);\r
139                 Figure centering(Point p);\r
140                 void draw();\r
141         }\r
142         public static class FigureExtention\r
143         {\r
144                 public static Figure shift(this Figure target, double x, double y)\r
145                 {\r
146                         return target.shift(new Point(x, y));\r
147                 }\r
148                 public static Figure centering(this Figure target)\r
149                 {\r
150                         return target.centering(Main.drawable.getCenter());\r
151                 }\r
152                 public static Figure centering(this Figure target, double x, double y)\r
153                 {\r
154                         return target.centering(new Point(x, y));\r
155                 }\r
156         }\r
157 \r
158         public class Image : Figure\r
159         {\r
160                 public WriteableBitmap buffer;\r
161                 public Point datum;\r
162                 public Rectangle self_rect;\r
163 \r
164                 public Image(int wid, int hei)\r
165                 {\r
166                         buffer = new WriteableBitmap(wid, hei);\r
167                         self_rect = new Rectangle(wid, hei);\r
168                 }\r
169 \r
170                 /*public Image shift(double x, double y)\r
171                 {\r
172                         datum.x += x;\r
173                         datum.y += y;\r
174                         return this;\r
175                 }*/\r
176                 public Figure shift(Point p)\r
177                 {\r
178                         datum += p;\r
179                         return this;\r
180                 }\r
181                 public Figure centering(Point p)\r
182                 {\r
183                         datum.x = p.x - width / 2.0;\r
184                         datum.y = p.y - height / 2.0;\r
185                         return this;\r
186                 }\r
187 \r
188                 public void pix(int x, int y, Color col)\r
189                 {\r
190                         buffer.SetPixel(x, y, col);\r
191                 }\r
192 \r
193 \r
194                 public void field(System.Func<int,int,System.Windows.Media.Color> func)\r
195                 {\r
196                         buffer.ForEach(func);\r
197                 }\r
198                 public void field(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
199                 {\r
200                         buffer.ForEach(func);\r
201                 }\r
202 \r
203                 public void draw()\r
204                 {\r
205                         Main.drawable.image(this);\r
206                 }\r
207 \r
208                 public double width  { get { return self_rect.width; } }\r
209                 public double height { get { return self_rect.height; } }\r
210 \r
211         }\r
212 \r
213 }