OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / graphic / psychlops_g_shape.h
1 /*
2  *  psychlops_g_shape.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/01/04 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9 #ifndef HEADER_PSYCHLOPS_GRAPHIC_SHAPE
10 #define HEADER_PSYCHLOPS_GRAPHIC_SHAPE
11
12
13 #include "psychlops_g_fundamental.h"
14 #include "psychlops_g_color.h"
15 #include "psychlops_g_module.h"
16
17 #include <deque>
18 #include <list>
19
20
21 namespace Psychlops {
22
23 namespace Devices\r
24 {\r
25         class CanvasBits;\r
26 }
27         struct Stroke {
28         public:
29                 enum Pattern { SOLID=0xFFFF, DASHED=0x0F0F, DOTTED=0xAAAA };
30
31                 Color color;
32                 double width;
33                 unsigned short pattern;
34
35                 Stroke();
36                 Stroke(const Color col, const double wid, const Pattern pat);
37                 Stroke& set(const Color col, const double wid, const Pattern pat);
38
39                 static const Stroke null_line;
40                 static const Stroke hair_line;
41         };
42
43
44         class Shape : virtual public Figure {
45         public:
46                 Color fill;
47                 Stroke stroke;
48                 Shape();
49
50                 virtual Shape& draw_base(Drawable &target);
51 #if !defined(_MSC_VER)
52                 virtual Shape& draw(Drawable &target = *Drawable::prime);
53 #endif
54                 virtual Shape& draw(const Color &col, Drawable &target = *Drawable::prime) = 0;
55                 virtual Shape& draw(const Stroke &strk, Drawable &target = *Drawable::prime) = 0;
56
57                 using Figure::centering;
58                 inline Shape& centering(const Figure& fig) { centering(fig.getDatum()); return *this; }
59                 inline Shape& centering(const Drawable &target = *Drawable::prime) { centering(target.getCenter()); return *this; }
60                 inline Shape& centering(const double x, const double y, const double z = 0) { centering(Point(x,y,z)); return *this; }
61                 inline Shape& shift(const double x, const double y, const double z = 0) { Point n=getDatum(); n=n+Point(x,y,z); setDatum(n); return *this; }\r
62
63 /*
64                 template<class T> class Colored : public T {
65                 public:
66                         Color color;
67                         Stroke stroke;
68                         Colored<T>() : T(), color(Color::null_color), stroke(Stroke::null_line) {}
69                         template<typename T1> Colored<T>(T1 a1) : T(a1), color(Color::null_color), stroke(Stroke::null_line) {}
70                         template<typename T1, typename T2> Colored<T>(T1 a1, T2 a2) : T(a1, a2), color(Color::null_color), stroke(Stroke::null_line) {}
71                         template<typename T1, typename T2, typename T3> Colored<T>(T1 a1, T2 a2, T3 a3) : T(a1, a2, a3), color(Color::null_color), stroke(Stroke::null_line) {}
72                         template<typename T1, typename T2, typename T3, typename T4> Colored<T>(T1 a1, T2 a2, T3 a3, T4 a4) : T(a1, a2, a3, a4), color(Color::null_color), stroke(Stroke::null_line) {}
73                         template<typename T1, typename T2, typename T3, typename T4, typename T5> Colored<T>(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) : T(a1, a2, a3, a4, a5), color(Color::null_color), stroke(Stroke::null_line) {}
74                         template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> Colored<T>(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) : T(a1, a2, a3, a4, a5, a6), color(Color::null_color), stroke(Stroke::null_line) {}
75                         //using T::draw;
76                         virtual Colored<T>& draw(Drawable &target = *Drawable::prime) {
77                                 if(color.getA()!=0) T::draw(color, target);
78                                 if(stroke.width!=0) T::draw(stroke, target);
79                                 return *this;
80                         }
81                         virtual Colored<T>& draw(const Color &col, Drawable &target = *Drawable::prime) { T::draw(col, target); return *this; }
82                         virtual Colored<T>& draw(const Stroke &strk, Drawable &target = *Drawable::prime) { T::draw(strk, target); return *this; }
83                 };
84  */
85         };
86
87
88
89
90         class Rectangle : virtual public Shape {
91                 friend class Point;
92                 friend class Canvas;\r
93                 friend class Devices::CanvasBits;
94                 friend class Image;
95
96         protected:
97                 double left;
98                 double top;
99                 double right;
100                 double bottom;
101
102         protected:
103                 template <typename X> inline void correctInversion(X &l, X &t, X &r, X &b);
104                 void setbypix(double l, double t, double r, double b);
105                 void setbypix(double width, double height);
106                 void set(double size);
107
108         public:
109                 Rectangle();
110                 Rectangle(double l, double t, double r, double b);
111                 Rectangle(double width, double height);
112                 //Rectangle(double width, double height);
113                 //Rectangle(double width, double height, double hcentre, double vcentre);
114                 Rectangle& set(const Rectangle &r);
115                 Rectangle& set(const Point &po1, const Point &po2);
116                 Rectangle& set(double l, double t, double r, double b);
117                 Rectangle& set(double width, double height);
118                 Rectangle dup();
119                 Rectangle& resize(double width, double height);
120                 Rectangle& locate(const Point &p);
121                 Rectangle& locate(double x, double y);\r
122
123                 virtual Rectangle& setDatum(const Point &po);
124                 virtual Rectangle& centering(const Drawable& target = *Drawable::prime);
125                 virtual Rectangle& centering(const Figure& fig);
126                 virtual Rectangle& centering(const Point &po);
127                 virtual Rectangle& centering(const double x, const double y, const double z = 0.0);
128                 virtual Rectangle& move_to(const double x, const double y, const double z = 0.0);
129                 virtual Rectangle& shift(const double h, const double v, const double d = 0.0);\r
130                 Rectangle& alignLeft(const double lef);\r
131                 Rectangle& alignTop(const double to_);\r
132                 Rectangle& alignRight(const double rig);\r
133                 Rectangle& alignBottom(const double bot);\r
134
135                 void clipped_by(const Rectangle &source);
136                 void clip(Rectangle &target);
137                 // obsolete: misspelled
138                 void cripped(const Rectangle &source);
139                 void crip(Rectangle &target);
140                 bool include(double x, double y) const;
141                 bool include(const Point &p) const;
142                 bool include(const Rectangle &rect) const;
143
144                 void setColor(Color col);
145                 virtual Rectangle& draw(Drawable &target = *Drawable::prime);
146                 virtual Rectangle& draw(const Color &col, Drawable &target = *Drawable::prime);
147                 virtual Rectangle& draw(const Stroke &strk, Drawable &target = *Drawable::prime);
148                 // obsolete
149                 Rectangle& display();
150                 Rectangle& display(const Color &col);
151
152
153                 double getWidth() const;
154                 double getHeight() const;
155                 virtual const Point getDatum() const;
156                 const Point getCenter() const;
157                 double getHcenter() const;
158                 double getVcenter() const;
159                 double getTop() const;
160                 double getLeft() const;
161                 double getBottom() const;
162                 double getRight() const;\r
163         };
164
165 \r
166         class Polygon;\r
167         class Line : virtual public Shape, virtual public FigureDatum {
168         public:
169                 using FigureDatum::datum;
170                 Point end;
171                 Line& setStart(const Point &dend);
172                 Point getStart() const;
173                 Line& setEnd(const Point &dend);
174                 Point getEnd() const;\r
175 \r
176                 Polygon *markerStart;\r
177                 Polygon *markerEnd;
178
179                 Line();
180                 Line(const double x1, const double y1, const double x2, const double y2);
181                 Line(const Point &dbegin, const Point &dend);
182                 Line& set(const double x1, const double y1, const double x2, const double y2);
183                 Line& set(const Point &dbegin, const Point &dend);
184
185                 virtual Line& setDatum(const Point &p);
186                 virtual Line& centering(const Point &p);
187                 virtual Line& shift(const double x, const double y, const double z = 0);
188                 virtual Line& draw(Drawable &target = *Drawable::prime);
189                 virtual Line& draw(const Color &col, Drawable &target = *Drawable::prime);
190                 virtual Line& draw(const Stroke &strk, Drawable &target = *Drawable::prime);
191
192                 using Shape::centering;
193         };
194
195         class Ellipse;
196         typedef Ellipse Oval;
197         class Ellipse : virtual public Shape, virtual public FigureDatum {
198         public:
199                 using FigureDatum::datum;
200                 double radius, v_radius;\r
201                 double sector_begin, sector_end;\r
202
203         public:
204                 Ellipse();
205                 Ellipse(const double d_radius, const double d_v_radius = -1, const Point& d_datum = Point());
206                 Ellipse(const Rectangle& rect);
207                 virtual ~Ellipse();
208                 Ellipse& set(const double d_radius, const double d_v_radius = -1, const Point& d_datum = Point());
209                 Ellipse& set(const Rectangle& rect);\r
210                 Ellipse& resize(double width, double height);
211                 virtual Ellipse& centering(const Point &p);
212                 virtual Ellipse& draw(Drawable &target = *Drawable::prime);
213                 virtual Ellipse& draw(const Color &col, Drawable &target = *Drawable::prime);
214                 virtual Ellipse& draw(const Stroke &strk, Drawable &target = *Drawable::prime);
215 \r
216                 virtual Ellipse& centering(const Figure& fig);
217                 virtual Ellipse& centering(const Drawable &target = *Drawable::prime);
218                 virtual Ellipse& centering(const double x, const double y, const double z = 0);
219                 virtual Ellipse& shift(const double x, const double y, const double z = 0);\r
220
221                 using Shape::centering;
222         };
223
224         class Polygon : virtual public Shape, virtual public FigureDatum {
225                 friend class Canvas;
226                 struct Vertex { Point point; };
227         protected:
228         public:
229                 std::deque<Point> vertices;
230                 using FigureDatum::datum;
231
232                 bool empty() const;
233
234                 Polygon();
235
236                 Polygon& append(const Point& p);
237                 Polygon& append(const double x, const double y, const double z = 0.0);
238 //              Polygon& append(const Point& p, const Color& col);
239 //              Polygon& append(const double x, const double y, const double z, const Color& col);
240 //              Polygon& append(const double x, const double y, const Color& col);
241
242                 virtual Polygon& centering(const Point &p);
243                 virtual Polygon& draw(Drawable &target = *Drawable::prime);
244                 virtual Polygon& draw(const Color& col, Drawable &target = *Drawable::prime);
245                 virtual Polygon& draw(const Stroke& strk, Drawable &target = *Drawable::prime);
246 \r
247                 virtual Polygon& centering(const Figure& fig);
248                 virtual Polygon& centering(const Drawable &target = *Drawable::prime);
249                 virtual Polygon& centering(const double x, const double y, const double z = 0);
250                 virtual Polygon& shift(const double x, const double y, const double z = 0);\r
251
252                 using Shape::centering;
253         };
254
255         class PolyLine : virtual public Shape, virtual public FigureDatum {
256                 friend class Canvas;
257                 struct Vertex { Point point; };
258         protected:
259         public:
260                 std::deque<Point> vertices;
261                 using FigureDatum::datum;
262                 bool empty() const;
263                 PolyLine();
264                 PolyLine& append(const Point& p);
265                 PolyLine& append(const double x, const double y, const double z = 0.0);
266                 virtual PolyLine& centering(const Point &p);
267                 virtual PolyLine& draw(Drawable &target = *Drawable::prime);
268                 virtual PolyLine& draw(const Color& col, Drawable &target = *Drawable::prime);
269                 virtual PolyLine& draw(const Stroke& strk, Drawable &target = *Drawable::prime);
270 \r
271                 virtual PolyLine& centering(const Figure& fig);
272                 virtual PolyLine& centering(const Drawable &target = *Drawable::prime);
273                 virtual PolyLine& centering(const double x, const double y, const double z = 0);
274                 virtual PolyLine& shift(const double x, const double y, const double z = 0);\r
275
276                 using Shape::centering;
277         };
278
279
280
281 }       /*      <- namespace Psycholops         */
282
283
284 #endif