OSDN Git Service

many
[psychlops/silverlight.git] / dev3 / psychlops / core / graphic / shape.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         public interface Shape : Internal.PrimitiveFigure\r
16         {\r
17                 Color fill { get; set; }\r
18                 Stroke stroke { get; set; }\r
19         }\r
20         public static class ShapeExtention\r
21         {\r
22                 public static void draw(this Shape drawee, Color c)\r
23                 {\r
24                         Color tmp_col = drawee.fill;\r
25                         Stroke tmp_strk = drawee.stroke;\r
26                         drawee.fill = c;\r
27                         drawee.stroke = new Stroke();\r
28                         drawee.draw();\r
29                         drawee.fill = tmp_col;\r
30                         drawee.stroke = tmp_strk;\r
31                 }\r
32                 public static void draw(this Shape drawee, Stroke strk)\r
33                 {\r
34                         Color tmp_col = drawee.fill;\r
35                         Stroke tmp_strk = drawee.stroke;\r
36                         drawee.fill = new Color(0,0,1,1);\r
37                         drawee.stroke = strk;\r
38                         drawee.draw();\r
39                         drawee.fill = tmp_col;\r
40                         drawee.stroke = tmp_strk;\r
41                 }\r
42         }\r
43 \r
44         public partial struct Stroke\r
45         {\r
46                 public double thick;\r
47                 public Color color;\r
48                 public Stroke(Color c, double t)\r
49                 {\r
50                         color = c;\r
51                         thick = t;\r
52                 }\r
53                 public void set(Color c, double t)\r
54                 {\r
55                         color = c;\r
56                         thick = t;\r
57                 }\r
58         }\r
59 \r
60         public partial class Line : Shape\r
61         {\r
62                 public Point begin, end;\r
63 \r
64                 public Line(double x1, double y1, double x2, double y2)\r
65                 {\r
66                         set(x1, y1, x2, y2);\r
67                 }\r
68                 public Line(Point v1, Point v2)\r
69                 {\r
70                         set(v1, v2);\r
71                 }\r
72                 public void set(double x1, double y1, double x2, double y2)\r
73                 {\r
74                         begin.set(x1, y1);\r
75                         end.set(x2, y2);\r
76                 }\r
77                 public void set(Point v1, Point v2)\r
78                 {\r
79                         begin = v1;\r
80                         end   = v2;\r
81                 }\r
82 \r
83                 public Figure shift(Point p)\r
84                 {\r
85                         begin += p;\r
86                         end   += p;\r
87                         return this;\r
88                 }\r
89                 public Figure centering(Point p)\r
90                 {\r
91                         double h = width, v = height;\r
92                         begin.x = p.x - h / 2.0;\r
93                         begin.y = p.y - v / 2.0;\r
94                         end.x = begin.x + h;\r
95                         end.y = begin.y + v;\r
96                         return this;\r
97                 }\r
98 \r
99                 public void draw()\r
100                 {\r
101                         Main.drawable.line(this);\r
102                 }\r
103 \r
104                 public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
105                 public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
106                 public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
107                 public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
108                 public double width { get { return Math.abs(begin.x - end.x); } }\r
109                 public double height { get { return Math.abs(begin.y - end.y); } }\r
110 \r
111                 public Color fill { get; set; }\r
112                 public Stroke stroke { get; set; }\r
113         }\r
114 \r
115 \r
116         public partial class Rectangle : Shape\r
117         {\r
118                 public Point v1, v2;\r
119 \r
120                 public Rectangle()\r
121                 {\r
122                         set(0,0);\r
123                 }\r
124                 public Rectangle(double wid, double hei)\r
125                 {\r
126                         set(wid, hei);\r
127                 }\r
128                 \r
129                 public Rectangle set(double wid, double hei)\r
130                 {\r
131                         v1.set(0, 0, 0);\r
132                         v2.set(wid, hei, 0);\r
133                         return this;\r
134                 }\r
135 \r
136                 public Figure shift(Point p)\r
137                 {\r
138                         v1 += p;\r
139                         v2 += p;\r
140                         return this;\r
141                 }\r
142                 public Figure centering(Point p)\r
143                 {\r
144                         double h = width, v = height;\r
145                         v1.x = p.x - h / 2.0;\r
146                         v1.y = p.y - v / 2.0;\r
147                         v2.x = v1.x + h;\r
148                         v2.y = v1.y + v;\r
149                         return this;\r
150                 }\r
151 \r
152                 public void draw()\r
153                 {\r
154                         Main.drawable.rect(this);\r
155                 }\r
156 \r
157                 public double left   { get { return v1.x; } }\r
158                 public double top    { get { return v1.y; } }\r
159                 public double right  { get { return v2.x; } }\r
160                 public double bottom { get { return v2.y; } }\r
161                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
162                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
163 \r
164 \r
165                 public Color fill { get; set; }\r
166                 public Stroke stroke { get; set; }\r
167 \r
168         }\r
169 \r
170 \r
171         public partial class Ellipse : Shape\r
172         {\r
173                 public Point datum;\r
174                 public double xdiameter, ydiameter;\r
175 \r
176                 public Ellipse()\r
177                 {\r
178                         set(0,0);\r
179                 }\r
180                 public Ellipse(double wid, double hei)\r
181                 {\r
182                         set(wid, hei);\r
183                 }\r
184 \r
185                 public Ellipse set(double wid, double hei)\r
186                 {\r
187                         xdiameter = wid;\r
188                         ydiameter = hei;\r
189                         return this;\r
190                 }\r
191                 public Figure shift(Point p)\r
192                 {\r
193                         datum += p;\r
194                         return this;\r
195                 }\r
196                 public Figure centering(Point p)\r
197                 {\r
198                         datum = p;\r
199                         return this;\r
200                 }\r
201 \r
202                 public void draw()\r
203                 {\r
204                         Main.drawable.ellipse(this);\r
205                 }\r
206 \r
207                 public double left { get { return datum.x - xdiameter/2.0; } }\r
208                 public double top { get { return datum.y - ydiameter / 2.0; } }\r
209                 public double right { get { return datum.x + xdiameter / 2.0; } }\r
210                 public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
211                 public double width { get { return Math.abs(xdiameter); } }\r
212                 public double height { get { return Math.abs(ydiameter); } }\r
213 \r
214                 public Color fill { get; set; }\r
215                 public Stroke stroke { get; set; }\r
216         }\r
217 \r
218 \r
219         public partial class Polygon : Shape\r
220         {\r
221                 public Point datum;\r
222                 public System.Collections.Generic.List<Point> vertices;\r
223 \r
224                 public Polygon()\r
225                 {\r
226                         vertices = new System.Collections.Generic.List<Point>();\r
227                 }\r
228                 public Polygon(double[] verts)\r
229                 {\r
230                         vertices = new System.Collections.Generic.List<Point>();\r
231                         for (int i=0; i < verts.Length; i+=2)\r
232                         {\r
233                                 vertices.Add(new Point(verts[i], verts[i+1]));\r
234                         }\r
235 \r
236                 }\r
237                 public Polygon(Point[] verts)\r
238                 {\r
239                         vertices = new System.Collections.Generic.List<Point>();\r
240                         foreach (Point p in verts)\r
241                         {\r
242                                 vertices.Add(p);\r
243                         }\r
244 \r
245                 }\r
246                 public Polygon append(Point p) { vertices.Add(p); return this; }\r
247                 public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
248                 public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
249 \r
250 \r
251                 public Figure shift(Point p)\r
252                 {\r
253                         datum += p;\r
254                         return this;\r
255                 }\r
256                 public Figure centering(Point p)\r
257                 {\r
258                         datum = p;\r
259                         return this;\r
260                 }\r
261 \r
262                 public void draw()\r
263                 {\r
264                         Main.drawable.polygon(this);\r
265                 }\r
266 \r
267                 public Color fill { get; set; }\r
268                 public Stroke stroke { get; set; }\r
269         }\r
270 \r
271 }