OSDN Git Service

b21784c5e6bfff6b6a9083d1de4d077c33c5ba01
[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, double alpha)\r
70                 {\r
71                         r = red;\r
72                         g = green;\r
73                         b = blue;\r
74                         a = alpha;\r
75                 }\r
76                 public static implicit operator System.Windows.Media.Color(Color d)\r
77                 {\r
78                         return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
79                 }\r
80 \r
81                 public static readonly Color\r
82                         black = new Color(0, 0, 0, 1),\r
83                         red = new Color(1, 0, 0, 1),\r
84                         green = new Color(0, 1, 0, 1),\r
85                         blue = new Color(0, 0, 1, 1),\r
86                         yellow = new Color(1, 1, 0, 1),\r
87                         magenta = new Color(1, 0, 1, 1),\r
88                         cyan = new Color(0, 1, 1, 1),\r
89                         white = new Color(1, 1, 1, 1),\r
90                         gray = new Color(.5, .5, .5, 1);\r
91 \r
92         }\r
93 \r
94 \r
95         public interface Drawable\r
96         {\r
97                 Point getCenter();\r
98                 void clear(Color col);\r
99                 void pix(int x, int y, Color col);\r
100                 void line(Line drawee, Color col);\r
101                 void rect(Rectangle drawee, Color col);\r
102                 void ellipse(Ellipse drawee, Color col);\r
103                 void image(Image drawee);\r
104                 void polygon(Polygon drawee, Color col);\r
105         }\r
106 \r
107 \r
108 \r
109         public interface Figure\r
110         {\r
111                 Figure shift(Point p);\r
112                 Figure centering(Point p);\r
113                 void draw();\r
114         }\r
115         public static class FigureExtention\r
116         {\r
117                 public static Figure shift(this Figure target, double x, double y)\r
118                 {\r
119                         return target.shift(new Point(x, y));\r
120                 }\r
121                 public static Figure centering(this Figure target)\r
122                 {\r
123                         return target.centering(Main.drawable.getCenter());\r
124                 }\r
125         }\r
126 \r
127 \r
128 \r
129         public class Image : Figure\r
130         {\r
131                 public WriteableBitmap buffer;\r
132                 public Point datum;\r
133                 public Rectangle self_rect;\r
134 \r
135                 public Image(int wid, int hei)\r
136                 {\r
137                         buffer = new WriteableBitmap(wid, hei);\r
138                         self_rect = new Rectangle(wid, hei);\r
139                 }\r
140 \r
141                 /*public Image shift(double x, double y)\r
142                 {\r
143                         datum.x += x;\r
144                         datum.y += y;\r
145                         return this;\r
146                 }*/\r
147                 public Figure shift(Point p)\r
148                 {\r
149                         datum += p;\r
150                         return this;\r
151                 }\r
152                 public Figure centering(Point p)\r
153                 {\r
154                         datum.x = p.x - width / 2.0;\r
155                         datum.y = p.y - height / 2.0;\r
156                         return this;\r
157                 }\r
158 \r
159                 public void pix(int x, int y, Color col)\r
160                 {\r
161                         buffer.SetPixel(x, y, col);\r
162                 }\r
163 \r
164 \r
165                 public void field(System.Func<int,int,System.Windows.Media.Color> func)\r
166                 {\r
167                         buffer.ForEach(func);\r
168                 }\r
169                 public void field(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
170                 {\r
171                         buffer.ForEach(func);\r
172                 }\r
173 \r
174                 public void draw()\r
175                 {\r
176                         Main.drawable.image(this);\r
177                 }\r
178 \r
179                 public double width  { get { return self_rect.width; } }\r
180                 public double height { get { return self_rect.height; } }\r
181 \r
182         }\r
183 \r
184 }