xref: /haiku/src/tests/servers/app/shape_test/main.cpp (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 #include <stdio.h>
2 #include <string.h>
3 
4 #include <Application.h>
5 #include <Message.h>
6 #include <Shape.h>
7 #include <View.h>
8 #include <Window.h>
9 
10 
11 static const char* kAppSignature = "application/x.vnd-Haiku.ShapeTest";
12 
13 
14 class TestView : public BView {
15 public:
16 								TestView(BRect frame, const char* name,
17 									uint32 resizeFlags, uint32 flags);
18 
19 	virtual	void				Draw(BRect updateRect);
20 };
21 
22 
23 TestView::TestView(BRect frame, const char* name, uint32 resizeFlags,
24 		uint32 flags)
25 	:
26 	BView(frame, name, resizeFlags, flags)
27 {
28 }
29 
30 
31 void
32 TestView::Draw(BRect updateRect)
33 {
34 	BRect r(Bounds());
35 
36 	SetDrawingMode(B_OP_ALPHA);
37 	SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
38 	SetHighColor(0, 0, 0, 240);
39 
40 	class TranslateIterator : public BShapeIterator {
41 	public:
42 		TranslateIterator()
43 			:
44 			fOffsetX(0),
45 			fOffsetY(0)
46 		{
47 		}
48 
49 		TranslateIterator(float offsetX, float offsetY)
50 			:
51 			fOffsetX(offsetX),
52 			fOffsetY(offsetY)
53 		{
54 		}
55 
56 		void SetOffset(float offsetX, float offsetY)
57 		{
58 			fOffsetX = offsetX;
59 			fOffsetY = offsetY;
60 		}
61 
62 		virtual status_t IterateMoveTo(BPoint* point)
63 		{
64 			point->x += fOffsetX;
65 			point->y += fOffsetY;
66 			return B_OK;
67 		}
68 
69 		virtual status_t IterateLineTo(int32 lineCount, BPoint* linePts)
70 		{
71 			while (lineCount--) {
72 				linePts->x += fOffsetX;
73 				linePts->y += fOffsetY;
74 				linePts++;
75 			}
76 			return B_OK;
77 		}
78 
79 		virtual status_t IterateBezierTo(int32 bezierCount, BPoint* bezierPts)
80 		{
81 			while (bezierCount--) {
82 				bezierPts[0].x += fOffsetX;
83 				bezierPts[0].y += fOffsetY;
84 				bezierPts[1].x += fOffsetX;
85 				bezierPts[1].y += fOffsetY;
86 				bezierPts[2].x += fOffsetX;
87 				bezierPts[2].y += fOffsetY;
88 				bezierPts += 3;
89 			}
90 			return B_OK;
91 		}
92 
93 		virtual status_t IterateArcTo(float& rx, float& ry, float& angle,
94 			bool largeArc, bool counterClockWise, BPoint& point)
95 		{
96 			point.x += fOffsetX;
97 			point.y += fOffsetY;
98 			return B_OK;
99 		}
100 
101 	private:
102 		float fOffsetX;
103 		float fOffsetY;
104 	} translator;
105 
106 	MovePenTo(B_ORIGIN);
107 
108 	const float arcRX = 50;
109 	const float arcRY = 80;
110 
111 	BShape shape;
112 	shape.MoveTo(BPoint(20, 10));
113 	shape.LineTo(BPoint(10, 90));
114 	shape.LineTo(BPoint(90, 100));
115 	shape.ArcTo(arcRX, arcRY, 45, true, true, BPoint(100, 20));
116 	shape.Close();
117 
118 	StrokeShape(&shape);
119 
120 	shape.Clear();
121 	shape.MoveTo(BPoint(20, 10));
122 	shape.LineTo(BPoint(10, 90));
123 	shape.LineTo(BPoint(90, 100));
124 	shape.ArcTo(arcRX, arcRY, 45, false, true, BPoint(100, 20));
125 	shape.Close();
126 
127 	translator.SetOffset(10, 10);
128 	translator.Iterate(&shape);
129 
130 	SetHighColor(140, 30, 50, 255);
131 	StrokeShape(&shape);
132 
133 	shape.Clear();
134 	shape.MoveTo(BPoint(20, 10));
135 	shape.LineTo(BPoint(10, 90));
136 	shape.LineTo(BPoint(90, 100));
137 	shape.ArcTo(arcRX, arcRY, 45, true, false, BPoint(100, 20));
138 	shape.Close();
139 
140 	translator.SetOffset(20, 20);
141 	translator.Iterate(&shape);
142 
143 	SetHighColor(140, 130, 50, 255);
144 	StrokeShape(&shape);
145 
146 	shape.Clear();
147 	shape.MoveTo(BPoint(20, 10));
148 	shape.LineTo(BPoint(10, 90));
149 	shape.LineTo(BPoint(90, 100));
150 	shape.ArcTo(arcRX, arcRY, 45, false, false, BPoint(100, 20));
151 	shape.Close();
152 
153 	translator.SetOffset(30, 30);
154 	translator.Iterate(&shape);
155 
156 	SetHighColor(40, 130, 150, 255);
157 	StrokeShape(&shape);
158 }
159 
160 
161 // #pragma mark -
162 
163 
164 int
165 main(int argc, char** argv)
166 {
167 	BApplication app(kAppSignature);
168 
169 	BWindow* window = new BWindow(BRect(50.0, 50.0, 300.0, 250.0),
170 		"BShape Test", B_TITLED_WINDOW,
171 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
172 
173 	BView* view = new TestView(window->Bounds(), "test", B_FOLLOW_ALL,
174 		B_WILL_DRAW);
175 	window->AddChild(view);
176 
177 	window->Show();
178 
179 	app.Run();
180 	return 0;
181 }
182