xref: /haiku/src/add-ons/tracker/zipomatic/ZipOMaticActivity.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
1 // license: public domain
2 // authors: jonas.sundstrom@kirilla.com
3 
4 
5 #include "ZipOMaticActivity.h"
6 
7 #include <stdio.h>
8 
9 
10 Activity::Activity(BRect frame, const char* name, uint32 resizingMode,
11 	uint32 flags)
12 	:
13 	BView(frame, name, resizingMode, flags),
14 	fIsRunning(false),
15 	fBitmap(NULL)
16 {
17 	fPattern.data[0] = 0x0f;
18 	fPattern.data[1] = 0x1e;
19 	fPattern.data[2] = 0x3c;
20 	fPattern.data[3] = 0x78;
21 	fPattern.data[4] = 0xf0;
22 	fPattern.data[5] = 0xe1;
23 	fPattern.data[6] = 0xc3;
24 	fPattern.data[7] = 0x87;
25 };
26 
27 
28 Activity::~Activity()
29 {
30 	delete fBitmap;
31 }
32 
33 
34 void
35 Activity::AllAttached()
36 {
37 	SetViewColor(B_TRANSPARENT_COLOR);
38 	_CreateBitmap();
39 }
40 
41 
42 void
43 Activity::Start()
44 {
45 	fIsRunning = true;
46 	Window()->SetPulseRate(100000);
47 	SetFlags(Flags() | B_PULSE_NEEDED);
48 	Invalidate();
49 }
50 
51 
52 void
53 Activity::Pause()
54 {
55 	Window()->SetPulseRate(500000);
56 	SetFlags(Flags() & (~B_PULSE_NEEDED));
57 	Invalidate();
58 }
59 
60 
61 void
62 Activity::Stop()
63 {
64 	fIsRunning = false;
65 	Window()->SetPulseRate(500000);
66 	SetFlags(Flags() & (~B_PULSE_NEEDED));
67 	Invalidate();
68 }
69 
70 
71 bool
72 Activity::IsRunning()
73 {
74 	return fIsRunning;
75 }
76 
77 
78 void
79 Activity::Pulse()
80 {
81 	uchar tmp = fPattern.data[7];
82 
83 	for (int j = 7; j > 0; --j)
84 		fPattern.data[j] = fPattern.data[j-1];
85 
86 	fPattern.data[0] = tmp;
87 
88 	Invalidate();
89 }
90 
91 
92 void
93 Activity::Draw(BRect rect)
94 {
95 	BRect viewRect = Bounds();
96 	BRect bitmapRect = fBitmap->Bounds();
97 
98 	if (bitmapRect != viewRect) {
99 		printf("Activity::Draw(): bitmapRect != viewRect\n");
100 		bitmapRect.PrintToStream();
101 		viewRect.PrintToStream();
102 
103 		delete fBitmap;
104 		_CreateBitmap();
105 	}
106 
107 	_DrawOnBitmap(IsRunning());
108 	SetDrawingMode(B_OP_COPY);
109 	DrawBitmap(fBitmap);
110 }
111 
112 
113 void
114 Activity::_DrawOnBitmap(bool running)
115 {
116 	if (fBitmap->Lock())
117 	{
118 		BRect rect = fBitmap->Bounds();
119 
120 		fBitmapView->SetDrawingMode(B_OP_COPY);
121 
122 		rgb_color color;
123 		color.red = 0;
124 		color.green = 0;
125 		color.blue = 0;
126 		color.alpha = 255;
127 
128 		if (running)
129 			color.blue = 200;
130 
131 		fBitmapView->SetHighColor(color);
132 
133 		// draw the pole
134 		rect.InsetBy(2,2);
135 		fBitmapView->FillRect(rect, fPattern);
136 
137 		// draw frame
138 
139 		// left
140 		color.red = 150;
141 		color.green = 150;
142 		color.blue = 150;
143 		fBitmapView->SetHighColor(color);
144 		fBitmapView->SetDrawingMode(B_OP_OVER);
145 		BPoint point_a = fBitmap->Bounds().LeftTop();
146 		BPoint point_b = fBitmap->Bounds().LeftBottom();
147 		point_b.y -= 1;
148 		fBitmapView->StrokeLine(point_a, point_b);
149 		point_a.x += 1;
150 		point_b.x += 1;
151 		point_b.y -= 1;
152 		fBitmapView->StrokeLine(point_a, point_b);
153 
154 		// top
155 		point_a = fBitmap->Bounds().LeftTop();
156 		point_b = fBitmap->Bounds().RightTop();
157 		point_b.x -= 1;
158 		fBitmapView->StrokeLine(point_a, point_b);
159 		point_a.y += 1;
160 		point_b.y += 1;
161 		point_b.x -= 1;
162 		fBitmapView->StrokeLine(point_a, point_b);
163 
164 		// right
165 		color.red = 255;
166 		color.green = 255;
167 		color.blue = 255;
168 		fBitmapView->SetHighColor(color);
169 		point_a = fBitmap->Bounds().RightTop();
170 		point_b = fBitmap->Bounds().RightBottom();
171 		fBitmapView->StrokeLine(point_a, point_b);
172 		point_a.y += 1;
173 		point_a.x -= 1;
174 		point_b.x -= 1;
175 		fBitmapView->StrokeLine(point_a, point_b);
176 
177 		// bottom
178 		point_a = fBitmap->Bounds().LeftBottom();
179 		point_b = fBitmap->Bounds().RightBottom();
180 		fBitmapView->StrokeLine(point_a, point_b);
181 		point_a.x += 1;
182 		point_a.y -= 1;
183 		point_b.y -= 1;
184 		fBitmapView->StrokeLine(point_a, point_b);
185 
186 		// some blending
187 		color.red = 150;
188 		color.green = 150;
189 		color.blue = 150;
190 		fBitmapView->SetHighColor(color);
191 		fBitmapView->SetDrawingMode(B_OP_SUBTRACT);
192 		fBitmapView->StrokeRect(rect);
193 
194 		rect.InsetBy(1,1);
195 		_LightenBitmapHighColor(& color);
196 		fBitmapView->StrokeRect(rect);
197 
198 		rect.InsetBy(1,1);
199 		_LightenBitmapHighColor(& color);
200 		fBitmapView->StrokeRect(rect);
201 
202 		rect.InsetBy(1,1);
203 		_LightenBitmapHighColor(& color);
204 		fBitmapView->StrokeRect(rect);
205 
206 		rect.InsetBy(1,1);
207 		_LightenBitmapHighColor(& color);
208 		fBitmapView->StrokeRect(rect);
209 
210 		fBitmapView->Sync();
211 		fBitmap->Unlock();
212 	}
213 }
214 
215 
216 void
217 Activity::_LightenBitmapHighColor(rgb_color* color)
218 {
219 	color->red -= 30;
220 	color->green -= 30;
221 	color->blue -= 30;
222 
223 	fBitmapView->SetHighColor(*color);
224 }
225 
226 
227 void
228 Activity::_CreateBitmap(void)
229 {
230 	BRect rect = Bounds();
231 	fBitmap = new BBitmap(rect, B_CMAP8, true);
232 	fBitmapView = new BView(Bounds(), "buffer", B_FOLLOW_NONE, 0);
233 	fBitmap->AddChild(fBitmapView);
234 }
235 
236 
237 void
238 Activity::FrameResized(float width, float height)
239 {
240 	delete fBitmap;
241 	_CreateBitmap();
242 	Invalidate();
243 }
244 
245