xref: /haiku/src/add-ons/screen_savers/leaves/Leaves.cpp (revision 57d51b24ffd6225014793bd152ec0e32883ad69c)
1 /*
2  * Copyright 2010-2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Deyan Genovski, deangenovski@gmail.com
7  *		Geoffry Song, goffrie@gmail.com
8  */
9 #include "Leaves.h"
10 
11 #include <stdlib.h>
12 #include <time.h>
13 
14 #include <AffineTransform.h>
15 #include <GradientLinear.h>
16 #include <Shape.h>
17 #include <Slider.h>
18 #include <TextView.h>
19 
20 
21 // path data for the leaf shape
22 static const BPoint kLeafBegin(56.24793f, 15.46287f);
23 static BPoint kLeafCurves[][3] = {
24 	{ BPoint(61.14, 28.89), BPoint(69.78, 38.25), BPoint(83.48, 44.17) },
25 	{ BPoint(99.46, 37.52), BPoint(113.27, 29.61), BPoint(134.91, 30.86) },
26 	{ BPoint(130.58, 36.53), BPoint(126.74, 42.44), BPoint(123.84, 48.81) },
27 	{ BPoint(131.81, 42.22), BPoint(137.53, 38.33), BPoint(144.37, 33.10) },
28 	{ BPoint(169.17, 23.55), BPoint(198.90, 15.55), BPoint(232.05, 10.51) },
29 	{ BPoint(225.49, 18.37), BPoint(219.31, 28.17), BPoint(217.41, 40.24) },
30 	{ BPoint(227.70, 26.60), BPoint(239.97, 14.63), BPoint(251.43, 8.36) },
31 	{ BPoint(288.89, 9.12), BPoint(322.73, 14.33), BPoint(346.69, 31.67) },
32 	{ BPoint(330.49, 37.85), BPoint(314.36, 44.25), BPoint(299.55, 54.17) },
33 	{ BPoint(292.48, 52.54), BPoint(289.31, 49.70), BPoint(285.62, 47.03) },
34 	{ BPoint(283.73, 54.61), BPoint(284.46, 57.94), BPoint(285.62, 60.60) },
35 	{ BPoint(259.78, 76.14), BPoint(233.24, 90.54), BPoint(202.41, 98.10) },
36 	{ BPoint(194.43, 95.36), BPoint(185.96, 92.39), BPoint(179.63, 88.33) },
37 	{ BPoint(180.15, 94.75), BPoint(182.73, 99.76), BPoint(185.62, 104.53) },
38 	{ BPoint(154.83, 119.46), BPoint(133.21, 118.97), BPoint(125.62, 94.88) },
39 	{ BPoint(124.70, 98.79), BPoint(124.11, 103.67), BPoint(124.19, 110.60) },
40 	{ BPoint(116.42, 111.81), BPoint(85.82, 99.60), BPoint(83.25, 51.96) },
41 	{ BPoint(62.50, 42.57), BPoint(58.12, 33.18), BPoint(50.98, 23.81) } };
42 static const int kLeafCurveCount = sizeof(kLeafCurves) / sizeof(kLeafCurves[0]);
43 static const float kLeafWidth = 372.f;
44 static const float kLeafHeight = 121.f;
45 
46 // leaf colors
47 static const rgb_color kColors[][2] = {
48 	{ {255, 114, 0, 255}, {255, 159, 0, 255} },
49 	{ {50, 160, 40, 255}, {125, 210, 32, 255} },
50 	{ {250, 190, 30, 255}, {255, 214, 125, 255} } };
51 static const int kColorCount = sizeof(kColors) / sizeof(kColors[0]);
52 
53 // bounds for settings
54 static const int kMinimumDropRate = 2, kMaximumDropRate = 50;
55 static const int kMinimumLeafSize = 50, kMaximumLeafSize = 1000;
56 static const int kMaximumSizeVariation = 200;
57 
58 enum {
59 	MSG_SET_DROP_RATE		= 'drop',
60 	MSG_SET_LEAF_SIZE		= 'size',
61 	MSG_SET_SIZE_VARIATION	= 'svry',
62 };
63 
64 
65 extern "C" BScreenSaver*
66 instantiate_screen_saver(BMessage* msg, image_id image)
67 {
68 	return new Leaves(msg, image);
69 }
70 
71 
72 Leaves::Leaves(BMessage* archive, image_id id)
73 	:
74 	BScreenSaver(archive, id),
75 	fDropRateSlider(NULL),
76 	fLeafSizeSlider(NULL),
77 	fSizeVariationSlider(NULL),
78 	fDropRate(10),
79 	fLeafSize(150),
80 	fSizeVariation(50)
81 {
82 	if (archive) {
83 		if (archive->FindInt32("Leaves drop rate", &fDropRate) != B_OK)
84 			fDropRate = 10;
85 		if (archive->FindInt32("Leaves size", &fLeafSize) != B_OK)
86 			fLeafSize = 150;
87 		if (archive->FindInt32("Leaves variation", &fSizeVariation) != B_OK)
88 			fSizeVariation = 50;
89 	}
90 }
91 
92 
93 void
94 Leaves::StartConfig(BView* view)
95 {
96 	BRect bounds = view->Bounds();
97 	bounds.InsetBy(10, 10);
98 	BRect frame(0, 0, bounds.Width(), 20);
99 
100 	fDropRateSlider = new BSlider(frame, "drop rate", "Drop rate:",
101 		new BMessage(MSG_SET_DROP_RATE), kMinimumDropRate, kMaximumDropRate,
102 		B_BLOCK_THUMB, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
103 	fDropRateSlider->SetValue(fDropRate);
104 	fDropRateSlider->ResizeToPreferred();
105 	bounds.bottom -= fDropRateSlider->Bounds().Height() * 1.5;
106 	fDropRateSlider->MoveTo(bounds.LeftBottom());
107 	view->AddChild(fDropRateSlider);
108 
109 	fLeafSizeSlider = new BSlider(frame, "leaf size", "Leaf size:",
110 		new BMessage(MSG_SET_LEAF_SIZE), kMinimumLeafSize, kMaximumLeafSize,
111 		B_BLOCK_THUMB, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
112 	fLeafSizeSlider->SetValue(fLeafSize);
113 	fLeafSizeSlider->ResizeToPreferred();
114 	bounds.bottom -= fLeafSizeSlider->Bounds().Height() * 1.5;
115 	fLeafSizeSlider->MoveTo(bounds.LeftBottom());
116 	view->AddChild(fLeafSizeSlider);
117 
118 	fSizeVariationSlider = new BSlider(frame, "variation", "Size variation:",
119 		new BMessage(MSG_SET_SIZE_VARIATION), 0, kMaximumSizeVariation,
120 		B_BLOCK_THUMB, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
121 	fSizeVariationSlider->SetValue(fSizeVariation);
122 	fSizeVariationSlider->ResizeToPreferred();
123 	bounds.bottom -= fSizeVariationSlider->Bounds().Height() * 1.5;
124 	fSizeVariationSlider->MoveTo(bounds.LeftBottom());
125 	view->AddChild(fSizeVariationSlider);
126 
127 	BTextView* textView = new BTextView(bounds, B_EMPTY_STRING,
128 		bounds.OffsetToCopy(0., 0.), B_FOLLOW_ALL, B_WILL_DRAW);
129 	textView->SetViewColor(view->ViewColor());
130 	textView->Insert("Leaves\n\n"
131 		"by Deyan Genovski, Geoffry Song\n\n");
132 	textView->SetStylable(true);
133 	textView->SetFontAndColor(0, 6, be_bold_font);
134 	textView->MakeEditable(false);
135 	view->AddChild(textView);
136 
137 	BWindow* window = view->Window();
138 	if (window) window->AddHandler(this);
139 
140 	fDropRateSlider->SetTarget(this);
141 	fLeafSizeSlider->SetTarget(this);
142 	fSizeVariationSlider->SetTarget(this);
143 }
144 
145 
146 status_t
147 Leaves::StartSaver(BView* view, bool preview)
148 {
149 	SetTickSize(10000000 / fDropRate);
150 	srand48(time(NULL));
151 
152 	view->SetLineMode(B_ROUND_CAP, B_ROUND_JOIN);
153 
154 	return B_OK;
155 }
156 
157 
158 status_t
159 Leaves::SaveState(BMessage* into) const
160 {
161 	status_t status;
162 	if ((status = into->AddInt32("Leaves drop rate", fDropRate)) != B_OK)
163 		return status;
164 	if ((status = into->AddInt32("Leaves size", fLeafSize)) != B_OK)
165 		return status;
166 	if ((status = into->AddInt32("Leaves variation", fSizeVariation)) != B_OK)
167 		return status;
168 	return B_OK;
169 }
170 
171 
172 void
173 Leaves::MessageReceived(BMessage* message)
174 {
175 	switch (message->what) {
176 		case MSG_SET_DROP_RATE:
177 			fDropRate = fDropRateSlider->Value();
178 			break;
179 		case MSG_SET_LEAF_SIZE:
180 			fLeafSize = fLeafSizeSlider->Value();
181 			break;
182 		case MSG_SET_SIZE_VARIATION:
183 			fSizeVariation = fSizeVariationSlider->Value();
184 			break;
185 		default:
186 			BHandler::MessageReceived(message);
187 	}
188 }
189 
190 
191 void
192 Leaves::Draw(BView* view, int32 frame)
193 {
194 	float scale = fLeafSize / kLeafWidth / (kMaximumLeafSize * 2);
195 	scale *= view->Bounds().Width();
196 	scale += scale * drand48() * fSizeVariation / 100.;
197 
198 	BAffineTransform transform;
199 	transform.TranslateBy(-kLeafWidth / 2, -kLeafHeight / 2);
200 		// draw the leaf centered on the point
201 	transform.RotateBy(drand48() * 2. * M_PI);
202 	if ((rand() & 64) == 0) transform.ScaleBy(-1., 1.);
203 		// flip half of the time
204 	transform.ScaleBy(scale);
205 	transform.TranslateBy(_RandomPoint(view->Bounds()));
206 
207 	BPoint center = transform.Apply(BPoint(kLeafWidth / 2, kLeafHeight / 2));
208 	BPoint gradientOffset = BPoint(60 * scale, 80 * scale);
209 	BGradientLinear gradient(center - gradientOffset, center + gradientOffset);
210 	int color = (rand() / 7) % kColorCount;
211 	gradient.AddColor(kColors[color][0], 0.f);
212 	gradient.AddColor(kColors[color][1], 255.f);
213 
214 	BShape leafShape;
215 	leafShape.MoveTo(transform.Apply(kLeafBegin));
216 	for (int i = 0; i < kLeafCurveCount; ++i) {
217 		BPoint control[3];
218 		for (int j = 0; j < 3; ++j)
219 			control[j] = transform.Apply(kLeafCurves[i][j]);
220 		leafShape.BezierTo(control);
221 	}
222 	leafShape.Close();
223 
224 	view->PushState();
225 	view->SetDrawingMode(B_OP_ALPHA);
226 	view->SetHighColor(0, 0, 0, 50);
227 	for (int i = 2; i >= 0; --i) {
228 		view->SetOrigin(i * 0.1, i * 0.3);
229 		view->SetPenSize(i * 2);
230 		view->StrokeShape(&leafShape);
231 	}
232 	view->PopState();
233 	view->FillShape(&leafShape, gradient);
234 }
235 
236 
237 inline BPoint
238 Leaves::_RandomPoint(const BRect& bound)
239 {
240 	return BPoint(drand48() * (bound.right - bound.left) + bound.left,
241 		drand48() * (bound.bottom - bound.top) + bound.top);
242 }
243 
244